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/stdlib/abort.h"
16 namespace LIBC_NAMESPACE
{
18 // A single-use lock to allow only a single thread to print the assertion.
19 static cpp::Atomic
<uint32_t> lock
= 0;
21 LLVM_LIBC_FUNCTION(void, __assert_fail
,
22 (const char *assertion
, const char *file
, unsigned line
,
23 const char *function
)) {
24 uint64_t mask
= gpu::get_lane_mask();
25 // We only want a single work group or warp to handle the assertion. Each
26 // group attempts to claim the lock, if it is already claimed we simply exit.
27 uint32_t claimed
= gpu::is_first_lane(mask
)
28 ? !lock
.fetch_or(1, cpp::MemoryOrder::ACQUIRE
)
30 if (!gpu::broadcast_value(mask
, claimed
))
33 // Only a single line should be printed if an assertion is hit.
34 if (gpu::is_first_lane(mask
))
35 LIBC_NAMESPACE::report_assertion_failure(assertion
, file
, line
, function
);
37 LIBC_NAMESPACE::abort();
40 } // namespace LIBC_NAMESPACE