Skip to content

Commit e0fe1c5

Browse files
ldrummtstellar
authored andcommitted
[OpenCL] Respect calling convention for builtin
`__translate_sampler_initializer` has a calling convention of `spir_func`, but clang generated calls to it using the default CC. Instruction Combining was lowering these mismatching calling conventions to `store i1* undef` which itself was subsequently lowered to a trap instruction by simplifyCFG resulting in runtime `SIGILL` There are arguably two bugs here: but whether there's any wisdom in converting an obviously invalid call into a runtime crash over aborting with a sensible error message will require further discussion. So for now it's enough to set the right calling convention on the runtime helper. Reviewed By: svenh, bader Differential Revision: https://reviews.llvm.org/D98411 (cherry picked from commit fcfd3fd)
1 parent c1831fc commit e0fe1c5

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -6215,15 +6215,17 @@ llvm::SanitizerStatReport &CodeGenModule::getSanStats() {
62156215

62166216
return *SanStats;
62176217
}
6218+
62186219
llvm::Value *
62196220
CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E,
62206221
CodeGenFunction &CGF) {
62216222
llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType());
6222-
auto SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr());
6223-
auto FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false);
6224-
return CGF.Builder.CreateCall(CreateRuntimeFunction(FTy,
6225-
"__translate_sampler_initializer"),
6226-
{C});
6223+
auto *SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr());
6224+
auto *FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false);
6225+
auto *Call = CGF.Builder.CreateCall(
6226+
CreateRuntimeFunction(FTy, "__translate_sampler_initializer"), {C});
6227+
Call->setCallingConv(Call->getCalledFunction()->getCallingConv());
6228+
return Call;
62276229
}
62286230

62296231
CharUnits CodeGenModule::getNaturalPointeeTypeAlignment(

clang/test/CodeGenOpenCL/sampler.cl

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ kernel void foo(sampler_t smp_par) {
3939
// Case 2b
4040
sampler_t smp = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_NORMALIZED_COORDS_TRUE | CLK_FILTER_NEAREST;
4141
// CHECK: [[smp_ptr:%[A-Za-z0-9_\.]+]] = alloca %opencl.sampler_t addrspace(2)*
42-
// CHECK: [[SAMP:%[0-9]+]] = call %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 19)
42+
// CHECK: [[SAMP:%[0-9]+]] = call spir_func %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 19)
4343
// CHECK: store %opencl.sampler_t addrspace(2)* [[SAMP]], %opencl.sampler_t addrspace(2)** [[smp_ptr]]
4444

4545
// Case 1b
@@ -56,12 +56,12 @@ kernel void foo(sampler_t smp_par) {
5656

5757
// Case 1a/2a
5858
fnc4smp(glb_smp);
59-
// CHECK: [[SAMP:%[0-9]+]] = call %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 35)
59+
// CHECK: [[SAMP:%[0-9]+]] = call spir_func %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 35)
6060
// CHECK: call spir_func void [[FUNCNAME]](%opencl.sampler_t addrspace(2)* [[SAMP]])
6161

6262
// Case 1a/2c
6363
fnc4smp(glb_smp_const);
64-
// CHECK: [[SAMP:%[0-9]+]] = call %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 35)
64+
// CHECK: [[SAMP:%[0-9]+]] = call spir_func %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 35)
6565
// CHECK: call spir_func void [[FUNCNAME]](%opencl.sampler_t addrspace(2)* [[SAMP]])
6666

6767
// Case 1c
@@ -70,20 +70,20 @@ kernel void foo(sampler_t smp_par) {
7070
// CHECK: call spir_func void [[FUNCNAME]](%opencl.sampler_t addrspace(2)* [[SAMP]])
7171

7272
fnc4smp(5);
73-
// CHECK: [[SAMP:%[0-9]+]] = call %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 5)
73+
// CHECK: [[SAMP:%[0-9]+]] = call spir_func %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 5)
7474
// CHECK: call spir_func void [[FUNCNAME]](%opencl.sampler_t addrspace(2)* [[SAMP]])
7575

7676
const sampler_t const_smp = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_NORMALIZED_COORDS_TRUE | CLK_FILTER_LINEAR;
7777
fnc4smp(const_smp);
78-
// CHECK: [[CONST_SAMP:%[0-9]+]] = call %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 35)
78+
// CHECK: [[CONST_SAMP:%[0-9]+]] = call spir_func %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 35)
7979
// CHECK: store %opencl.sampler_t addrspace(2)* [[CONST_SAMP]], %opencl.sampler_t addrspace(2)** [[CONST_SMP_PTR:%[a-zA-Z0-9]+]]
8080
fnc4smp(const_smp);
8181
// CHECK: [[SAMP:%[0-9]+]] = load %opencl.sampler_t addrspace(2)*, %opencl.sampler_t addrspace(2)** [[CONST_SMP_PTR]]
8282
// CHECK: call spir_func void [[FUNCNAME]](%opencl.sampler_t addrspace(2)* [[SAMP]])
8383

8484
constant sampler_t constant_smp = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_NORMALIZED_COORDS_TRUE | CLK_FILTER_LINEAR;
8585
fnc4smp(constant_smp);
86-
// CHECK: [[SAMP:%[0-9]+]] = call %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 35)
86+
// CHECK: [[SAMP:%[0-9]+]] = call spir_func %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 35)
8787
// CHECK: call spir_func void [[FUNCNAME]](%opencl.sampler_t addrspace(2)* [[SAMP]])
8888

8989
// TODO: enable sampler initialization with non-constant integer.

0 commit comments

Comments
 (0)