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-exception
6//
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/stdlib/abort.h"
15
16namespace LIBC_NAMESPACE {
17
18// A single-use lock to allow only a single thread to print the assertion.
19static cpp::Atomic<uint32_t> lock = 0;
20
21LLVM_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(lane_mask: mask)
28 ? !lock.fetch_or(mask: 1, mem_ord: cpp::MemoryOrder::ACQUIRE)
29 : 0;
30 if (!gpu::broadcast_value(mask, x: claimed))
31 gpu::end_program();
32
33 // Only a single line should be printed if an assertion is hit.
34 if (gpu::is_first_lane(lane_mask: mask))
35 LIBC_NAMESPACE::report_assertion_failure(assertion, filename: file, line, funcname: function);
36 gpu::sync_lane(mask);
37 LIBC_NAMESPACE::abort();
38}
39
40} // namespace LIBC_NAMESPACE
41

source code of libc/src/assert/gpu/__assert_fail.cpp