1 //===-- GPU definition of a libc internal assert macro ----------*- C++ -*-===//
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-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/assert/__assert_fail.h"
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"
17 namespace LIBC_NAMESPACE_DECL
{
19 // A single-use lock to allow only a single thread to print the assertion.
20 static cpp::Atomic
<uint32_t> lock
= 0;
22 LLVM_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. Each
27 // 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
)
31 if (!gpu::broadcast_value(mask
, claimed
))
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
);
38 LIBC_NAMESPACE::abort();
41 } // namespace LIBC_NAMESPACE_DECL