https://github.com/Sirraide created 
https://github.com/llvm/llvm-project/pull/243600

For `__builtin_abort()`, we currently output PTX that fails to link; NVCC 
instead rejects the call entirely. I do think supporting it makes sense, and 
there isn't really anything sensible you can do other than emit a trap here I 
believe.

Fixes #143911.

Assisted-by: Codex

>From 633d457ae0f9dc1050324a6b80545fae1f99e85b Mon Sep 17 00:00:00 2001
From: Ambrose Leeb <[email protected]>
Date: Tue, 17 Nov 2026 20:00:20 +0000
Subject: [PATCH] [Clang] [CUDA] Emit a trap for __builtin_abort() on the
 device side

Currently, we output ptx that fails to link; NVCC instead rejects
__builtin_abort() entirely; I do think supporting it makes sense, and
there isn't really anything sensible you can do other than emit a trap
here I believe.

Fixes #143911.

Assisted-by: Codex
---
 clang/docs/ReleaseNotes.md              |  3 ++
 clang/lib/CodeGen/CGBuiltin.cpp         |  7 ++++
 clang/test/CodeGenCUDA/builtin-abort.cu | 52 +++++++++++++++++++++++++
 3 files changed, 62 insertions(+)
 create mode 100644 clang/test/CodeGenCUDA/builtin-abort.cu

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 7a0169be43980e..00c1a019240430 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1340,6 +1340,9 @@ documentation](CIR/index.md) for more information 
regarding ClangIR's status and
 
 #### CUDA/HIP Language Changes
 
+- `__builtin_abort()` in CUDA and HIP device code now emits a trap instruction
+  rather than a call to `abort()`. (#GH143911)
+
 - HIP compilations now add the `include/libhipcxx` directory from the selected
   ROCm installation to the header search path when it exists. This allows
   libhipcxx headers to be included with paths such as `<cuda/std/atomic>`.
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index e154b9e752fd5c..ae99e9dbe0c40f 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -4187,6 +4187,13 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
     Function *F = CGM.getIntrinsic(Intrinsic::clear_cache, {CGM.DefaultPtrTy});
     return RValue::get(Builder.CreateCall(F, {Begin, End}));
   }
+  case Builtin::BI__builtin_abort:
+    // On the device side we might not have a definition of 'abort()', so emit
+    // a trap instead.
+    if (!getLangOpts().CUDAIsDevice)
+      break;
+    EmitTrapCall(Intrinsic::trap);
+    return RValue::get(nullptr);
   case Builtin::BI__builtin_trap:
     EmitTrapCall(Intrinsic::trap);
     return RValue::get(nullptr);
diff --git a/clang/test/CodeGenCUDA/builtin-abort.cu 
b/clang/test/CodeGenCUDA/builtin-abort.cu
new file mode 100644
index 00000000000000..c78bb281b9fe6b
--- /dev/null
+++ b/clang/test/CodeGenCUDA/builtin-abort.cu
@@ -0,0 +1,52 @@
+// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fcuda-is-device \
+// RUN:   -emit-llvm -disable-llvm-passes -o - %s | \
+// RUN:   FileCheck %s --check-prefix=DEVICE --implicit-check-not=@abort
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu \
+// RUN:   -emit-llvm -disable-llvm-passes -o - %s | \
+// RUN:   FileCheck %s --check-prefix=HOST [email protected]
+// RUN: %clang_cc1 -x hip -triple amdgpu9.00-amd-amdhsa -fcuda-is-device \
+// RUN:   -emit-llvm -disable-llvm-passes -o - %s | \
+// RUN:   FileCheck %s --check-prefix=DEVICE --implicit-check-not=@abort
+// RUN: %clang_cc1 -x hip -triple x86_64-unknown-linux-gnu \
+// RUN:   -emit-llvm -disable-llvm-passes -o - %s | \
+// RUN:   FileCheck %s --check-prefix=HOST [email protected]
+// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-linux-gnu \
+// RUN:   -emit-llvm -disable-llvm-passes -o - %s | \
+// RUN:   FileCheck %s --check-prefix=HOST [email protected]
+
+#include "Inputs/cuda.h"
+
+namespace gh143911 {
+// DEVICE-LABEL: define{{.*}} @_ZN8gh1439111fEv(
+// DEVICE: call void @llvm.trap()
+// DEVICE-NEXT: unreachable
+__global__ void f() {
+  __builtin_abort();
+}
+} // namespace gh143911
+
+extern "C" {
+// DEVICE-LABEL: define{{.*}} @device_abort(
+// DEVICE: call void @llvm.trap()
+// DEVICE-NEXT: unreachable
+__device__ void device_abort() {
+  __builtin_abort();
+}
+
+// DEVICE-LABEL: define{{.*}} @shared_abort(
+// DEVICE: call void @llvm.trap()
+// DEVICE-NEXT: unreachable
+// HOST-LABEL: define{{.*}} @shared_abort(
+// HOST: call void @abort()
+// HOST-NEXT: unreachable
+__host__ __device__ void shared_abort() {
+  __builtin_abort();
+}
+
+// HOST-LABEL: define{{.*}} @host_abort(
+// HOST: call void @abort()
+// HOST-NEXT: unreachable
+__host__ void host_abort() {
+  __builtin_abort();
+}
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to