@@ -2142,9 +2142,26 @@ bool lowerBuiltinCallsToVariables(Module *M) {
21422142 bool IsVec = F.getFunctionType ()->getNumParams () > 0 ;
21432143 Type *GVType =
21442144 IsVec ? FixedVectorType::get (F.getReturnType (), 3 ) : F.getReturnType ();
2145- auto *BV = new GlobalVariable (
2146- *M, GVType, /* isConstant=*/ true , GlobalValue::ExternalLinkage, nullptr ,
2147- BuiltinVarName, 0 , GlobalVariable::NotThreadLocal, SPIRAS_Input);
2145+ GlobalVariable *BV = nullptr ;
2146+ // Consider the following LLVM IR:
2147+ // @__spirv_BuiltInLocalInvocationId = <Global constant>
2148+ // .....
2149+ // define spir_kernel void @kernel1(....) {
2150+ // %3 = tail call i64 @_Z12get_local_idj(i32 0)
2151+ // .....
2152+ // return void
2153+ // }
2154+ // During the OCLToSPIRV pass, the opencl call will get lowered to
2155+ // yet another global variable with the name
2156+ // '@__spirv_BuiltInLocalInvocationId'. In such a case, we would want to
2157+ // create only a single global variable with this name.
2158+ if (GlobalVariable *GV = M->getGlobalVariable (BuiltinVarName))
2159+ BV = GV;
2160+ else
2161+ BV = new GlobalVariable (*M, GVType, /* isConstant=*/ true ,
2162+ GlobalValue::ExternalLinkage, nullptr ,
2163+ BuiltinVarName, 0 , GlobalVariable::NotThreadLocal,
2164+ SPIRAS_Input);
21482165 for (auto *U : F.users ()) {
21492166 auto *CI = dyn_cast<CallInst>(U);
21502167 assert (CI && " invalid instruction" );
0 commit comments