1533145c4SJoseph Huber //===-- GPU definition of a libc internal assert macro ----------*- C++ -*-===// 2533145c4SJoseph Huber // 3533145c4SJoseph Huber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4533145c4SJoseph Huber // See https://llvm.org/LICENSE.txt for license information. 5533145c4SJoseph Huber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6533145c4SJoseph Huber // 7533145c4SJoseph Huber //===----------------------------------------------------------------------===// 8533145c4SJoseph Huber 9533145c4SJoseph Huber #include "src/assert/__assert_fail.h" 10533145c4SJoseph Huber 11533145c4SJoseph Huber #include "src/__support/CPP/atomic.h" 12533145c4SJoseph Huber #include "src/__support/GPU/utils.h" 13533145c4SJoseph Huber #include "src/__support/libc_assert.h" 14*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 15533145c4SJoseph Huber #include "src/stdlib/abort.h" 16533145c4SJoseph Huber 17*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 18533145c4SJoseph Huber 19533145c4SJoseph Huber // A single-use lock to allow only a single thread to print the assertion. 20533145c4SJoseph Huber static cpp::Atomic<uint32_t> lock = 0; 21533145c4SJoseph Huber 22533145c4SJoseph Huber LLVM_LIBC_FUNCTION(void, __assert_fail, 23533145c4SJoseph Huber (const char *assertion, const char *file, unsigned line, 24533145c4SJoseph Huber const char *function)) { 25533145c4SJoseph Huber uint64_t mask = gpu::get_lane_mask(); 26533145c4SJoseph Huber // We only want a single work group or warp to handle the assertion. Each 27533145c4SJoseph Huber // group attempts to claim the lock, if it is already claimed we simply exit. 28533145c4SJoseph Huber uint32_t claimed = gpu::is_first_lane(mask) 29533145c4SJoseph Huber ? !lock.fetch_or(1, cpp::MemoryOrder::ACQUIRE) 30533145c4SJoseph Huber : 0; 3168801985SJoseph Huber if (!gpu::broadcast_value(mask, claimed)) 3268801985SJoseph Huber gpu::end_program(); 33533145c4SJoseph Huber 34533145c4SJoseph Huber // Only a single line should be printed if an assertion is hit. 35533145c4SJoseph Huber if (gpu::is_first_lane(mask)) 36b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::report_assertion_failure(assertion, file, line, function); 379bcf9dc9SJoseph Huber gpu::sync_lane(mask); 38b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::abort(); 39533145c4SJoseph Huber } 40533145c4SJoseph Huber 41*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 42