Skip to content

Commit 63ae5de

Browse files
add initial solution by adding a shadow functin into the module when there is global variable but no function
1 parent 78c6554 commit 63ae5de

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
@@ -3013,6 +3013,36 @@ bool SPIRVEmitIntrinsics::runOnModule(Module &M) {
30133013
parseFunDeclarations(M);
30143014
insertConstantsForFPFastMathDefault(M);
30153015

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

0 commit comments

Comments
 (0)