aboutsummaryrefslogtreecommitdiff
path: root/libc/src/assert/gpu/__assert_fail.cpp
blob: b239088df69b01f16805763d386a329f108ed5b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//===-- GPU definition of a libc internal assert macro ----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/assert/__assert_fail.h"

#include "src/__support/CPP/atomic.h"
#include "src/__support/GPU/utils.h"
#include "src/__support/libc_assert.h"
#include "src/stdlib/abort.h"

namespace LIBC_NAMESPACE {

// A single-use lock to allow only a single thread to print the assertion.
static cpp::Atomic<uint32_t> lock = 0;

LLVM_LIBC_FUNCTION(void, __assert_fail,
                   (const char *assertion, const char *file, unsigned line,
                    const char *function)) {
  uint64_t mask = gpu::get_lane_mask();
  // We only want a single work group or warp to handle the assertion. Each
  // group attempts to claim the lock, if it is already claimed we simply exit.
  uint32_t claimed = gpu::is_first_lane(mask)
                         ? !lock.fetch_or(1, cpp::MemoryOrder::ACQUIRE)
                         : 0;
  if (!gpu::broadcast_value(mask, claimed))
    gpu::end_program();

  // Only a single line should be printed if an assertion is hit.
  if (gpu::is_first_lane(mask))
    LIBC_NAMESPACE::report_assertion_failure(assertion, file, line, function);
  LIBC_NAMESPACE::abort();
}

} // namespace LIBC_NAMESPACE