https://github.com/jhuber6 created 
https://github.com/llvm/llvm-project/pull/188872

Summary:
The 'function' check requires inserting eight bytes of magic before each
function. The HIP runtime expects and enforces 256 byte alignment. When
the instrumentation inserts the eight bytes this is done after the
alignment, which means that the HIP runtime then points the PC to an
invalid instruction by truncating the address to 256 byte alignment. The
OpenMP runtime doesn't do this.

The purpose of this function is to handle indirect calls, and it's
impossible to indirectly call a kernel anyway, so we should just
suppress this in this case. The only other solution would be to add the
alignment back before we emit the label, but that would be meaningless
because it'd just replace the magic bytes with zeroes.


>From b7f4503c4e644d2bf297b21572d3361c15c7e06c Mon Sep 17 00:00:00 2001
From: Joseph Huber <[email protected]>
Date: Thu, 26 Mar 2026 18:05:57 -0500
Subject: [PATCH] [HIP] Fix ubsan function checks applied to kernel functions

Summary:
The 'function' check requires inserting eight bytes of magic before each
function. The HIP runtime expects and enforces 256 byte alignment. When
the instrumentation inserts the eight bytes this is done after the
alignment, which means that the HIP runtime then points the PC to an
invalid instruction by truncating the address to 256 byte alignment. The
OpenMP runtime doesn't do this.

The purpose of this function is to handle indirect calls, and it's
impossible to indirectly call a kernel anyway, so we should just
suppress this in this case. The only other solution would be to add the
alignment back before we emit the label, but that would be meaningless
because it'd just replace the magic bytes with zeroes.
---
 clang/lib/CodeGen/CodeGenFunction.cpp |  8 ++++++--
 clang/test/CodeGen/AMDGPU/sanitizer.c | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGen/AMDGPU/sanitizer.c

diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp 
b/clang/lib/CodeGen/CodeGenFunction.cpp
index a64cec801a173..e6593e9fbe46f 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1033,9 +1033,13 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, 
QualType RetTy,
   }
 
   // If we are checking function types, emit a function type signature as
-  // prologue data.
+  // prologue data. Kernel functions have strict alignment requirements and
+  // cannot be call indirectly so we do not instrument them.
   if (FD && SanOpts.has(SanitizerKind::Function) &&
-      !FD->getType()->isCFIUncheckedCalleeFunctionType()) {
+      !FD->getType()->isCFIUncheckedCalleeFunctionType() &&
+      Fn->getCallingConv() != llvm::CallingConv::SPIR_KERNEL &&
+      Fn->getCallingConv() != llvm::CallingConv::AMDGPU_KERNEL &&
+      Fn->getCallingConv() != llvm::CallingConv::PTX_Kernel) {
     if (llvm::Constant *PrologueSig = getPrologueSignature(CGM, FD)) {
       llvm::LLVMContext &Ctx = Fn->getContext();
       llvm::MDBuilder MDB(Ctx);
diff --git a/clang/test/CodeGen/AMDGPU/sanitizer.c 
b/clang/test/CodeGen/AMDGPU/sanitizer.c
new file mode 100644
index 0000000000000..3b41e4dd8babe
--- /dev/null
+++ b/clang/test/CodeGen/AMDGPU/sanitizer.c
@@ -0,0 +1,20 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --version 6
+// RUN: %clang_cc1 -cc1 -triple amdgcn-amd-amdhsa -emit-llvm 
-fsanitize=function %s -o - | FileCheck %s
+
+// CHECK-LABEL: define dso_local void @function(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] !func_sanitize [[META2:![0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    ret void
+//
+void function() {}
+
+// CHECK-LABEL: define dso_local amdgpu_kernel void @kernel(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    call void @function() #[[ATTR1:[0-9]+]]
+// CHECK-NEXT:    ret void
+//
+[[clang::device_kernel]] void kernel() { function(); }
+//.
+// CHECK: [[META2]] = !{i32 -1056584962, i32 1717976574}
+//.

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

Reply via email to