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 16 namespace LIBC_NAMESPACE { 17 18 // A single-use lock to allow only a single thread to print the assertion. 19 static cpp::Atomic<uint32_t> lock = 0; 20 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) 29 : 0; 30 if (!gpu::broadcast_value(mask, 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(mask)) 35 LIBC_NAMESPACE::report_assertion_failure(assertion, file, line, function); 36 LIBC_NAMESPACE::abort(); 37 } 38 39 } // namespace LIBC_NAMESPACE 40