Skip to content

Commit 388e124

Browse files
add initial solution by adding a shadow functin into the module when there is global variable but no function
1 parent 19043b2 commit 388e124

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3020,6 +3020,36 @@ bool SPIRVEmitIntrinsics::runOnModule(Module &M) {
30203020
parseFunDeclarations(M);
30213021
insertConstantsForFPFastMathDefault(M);
30223022

3023+
// If there are no functions but there is at least one global variable,
3024+
// create a shadow function to "anchor" global handling in codegen.
3025+
bool HasAnyFunction = false;
3026+
for (auto &F : M)
3027+
if (!F.isDeclaration() && !F.isIntrinsic())
3028+
HasAnyFunction = true;
3029+
3030+
Function *ShadowFunc = nullptr;
3031+
if (!HasAnyFunction) {
3032+
for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ) {
3033+
GlobalVariable *GV = &*GI++;
3034+
if (GV->hasInternalLinkage()) {
3035+
GV->eraseFromParent();
3036+
Changed = true;
3037+
} else if (ShadowFunc == nullptr) {
3038+
LLVMContext &Ctx = M.getContext();
3039+
auto *FTy = FunctionType::get(Type::getVoidTy(Ctx), /*isVarArg=*/false);
3040+
ShadowFunc = Function::Create(
3041+
FTy, GlobalValue::InternalLinkage, "__spirv_globals_entry", &M);
3042+
3043+
// Create a basic block and insert a ret void
3044+
BasicBlock *BB = BasicBlock::Create(Ctx, "entry", ShadowFunc);
3045+
IRBuilder<> B(BB);
3046+
B.CreateRetVoid();
3047+
3048+
Changed = true;
3049+
}
3050+
}
3051+
}
3052+
30233053
TodoType.clear();
30243054
for (auto &F : M)
30253055
Changed |= runOnFunction(F);

0 commit comments

Comments
 (0)