Skip to content

Commit 889c42b

Browse files
[CodeGen] Do not emit call site attributes for LLVM intrinsics.
When a function is declared with the `asm(...)` attribute to change the mangled name to reference a LLVM intrinsic, the AST->IR translation for the function declaration already skips any function/parameter attribute that is either deduced from the AST function declaration or implied by language options/target properties. Instead the attributes associated to the LLVM intrinsic function are being generated. When emitting calls to such function declarations, however, call site attributes are emitted based on the AST function declaration or language options/target properties. This can lead to undesired outcomes, e.g. the call site of a LLVM intrinsic marked `convergent` when the language options implies `convergent` by default. This commit fixes the call lowering logic to ignore all attributes in the case the generate call instruction is an intrinsic call, such that the only attributes from the intrinsic declaration will be implicitly considered.
1 parent 1eaa179 commit 889c42b

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5793,8 +5793,14 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
57935793
}
57945794

57955795
// Apply the attributes and calling convention.
5796-
CI->setAttributes(Attrs);
5797-
CI->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
5796+
5797+
// If this is a call to an intrinsic, ignore the attributes that would
5798+
// normally be deduced from the AST function declaration + the default
5799+
// attributes imposed by the language and/or target.
5800+
if (!isa<llvm::IntrinsicInst>(CI)) {
5801+
CI->setAttributes(Attrs);
5802+
CI->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
5803+
}
57985804

57995805
// Apply various metadata.
58005806

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
2+
3+
float llvm_sin_f32(float) asm("llvm.sin.f32");
4+
5+
float test(float a) {
6+
return llvm_sin_f32(a);
7+
}
8+
9+
// CHECK: call float @llvm.sin.f32(float {{%.+}}){{$}}
10+
11+
// CHECK: declare float @llvm.sin.f32(float) [[ATTRS:#[0-9]+]]
12+
13+
// CHECK: attributes [[ATTRS]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

clang/test/CodeGenCUDA/surface.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ __attribute__((device)) int suld_2d_zero(surface<void, 2>, int, int) asm("llvm.n
2929

3030
// DEVICE-LABEL: i32 @_Z3fooii(i32 noundef %x, i32 noundef %y)
3131
// DEVICE: call i64 @llvm.nvvm.texsurf.handle.internal.p1(ptr addrspace(1) @surf)
32-
// DEVICE: call noundef i32 @llvm.nvvm.suld.2d.i32.zero(i64 %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}})
32+
// DEVICE: call i32 @llvm.nvvm.suld.2d.i32.zero(i64 %{{.*}}, i32 %{{.*}}, i32 %{{.*}})
3333
__attribute__((device)) int foo(int x, int y) {
3434
return suld_2d_zero(surf, x, y);
3535
}

0 commit comments

Comments
 (0)