42 lines · cpp
1//===-- GPU definition of a libc internal assert macro ----------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/assert/__assert_fail.h"10 11#include "src/__support/CPP/atomic.h"12#include "src/__support/GPU/utils.h"13#include "src/__support/libc_assert.h"14#include "src/__support/macros/config.h"15#include "src/stdlib/abort.h"16 17namespace LIBC_NAMESPACE_DECL {18 19// A single-use lock to allow only a single thread to print the assertion.20static cpp::Atomic<uint32_t> lock = 0;21 22LLVM_LIBC_FUNCTION(void, __assert_fail,23 (const char *assertion, const char *file, unsigned line,24 const char *function)) {25 uint64_t mask = gpu::get_lane_mask();26 // We only want a single work group or warp to handle the assertion. Each27 // group attempts to claim the lock, if it is already claimed we simply exit.28 uint32_t claimed = gpu::is_first_lane(mask)29 ? !lock.fetch_or(1, cpp::MemoryOrder::ACQUIRE)30 : 0;31 if (!gpu::broadcast_value(mask, claimed))32 gpu::end_program();33 34 // Only a single line should be printed if an assertion is hit.35 if (gpu::is_first_lane(mask))36 LIBC_NAMESPACE::report_assertion_failure(assertion, file, line, function);37 gpu::sync_lane(mask);38 LIBC_NAMESPACE::abort();39}40 41} // namespace LIBC_NAMESPACE_DECL42