llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: Ambrose Leeb (Sirraide)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/243600.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.md (+3) 
- (modified) clang/lib/CodeGen/CGBuiltin.cpp (+7) 
- (added) clang/test/CodeGenCUDA/builtin-abort.cu (+52) 


``````````diff
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();
+}
+}

``````````

</details>


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

Reply via email to