-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[SPIRV] Add support for pointers to functions with aggregate args/returns as global variables / constant initialisers #169595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
6697790
c1e9fc1
211f7f1
ac3bfbc
2682f90
7897636
197e0f5
4c57b00
ebca521
56afa1e
5405cea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ | |
| #include "llvm/Analysis/ValueTracking.h" | ||
| #include "llvm/CodeGen/IntrinsicLowering.h" | ||
| #include "llvm/IR/IRBuilder.h" | ||
| #include "llvm/IR/InstIterator.h" | ||
| #include "llvm/IR/Instructions.h" | ||
| #include "llvm/IR/IntrinsicInst.h" | ||
| #include "llvm/IR/Intrinsics.h" | ||
| #include "llvm/IR/IntrinsicsSPIRV.h" | ||
|
|
@@ -41,6 +43,7 @@ class SPIRVPrepareFunctions : public ModulePass { | |
| const SPIRVTargetMachine &TM; | ||
| bool substituteIntrinsicCalls(Function *F); | ||
| Function *removeAggregateTypesFromSignature(Function *F); | ||
| bool removeAggregateTypesFromCalls(Function *F); | ||
|
|
||
| public: | ||
| static char ID; | ||
|
|
@@ -469,6 +472,23 @@ bool SPIRVPrepareFunctions::substituteIntrinsicCalls(Function *F) { | |
| return Changed; | ||
| } | ||
|
|
||
| static void | ||
| addFunctionTypeMutation(NamedMDNode *NMD, | ||
| SmallVector<std::pair<int, Type *>> ChangedTys, | ||
| StringRef Name) { | ||
|
|
||
| LLVMContext &Ctx = NMD->getParent()->getContext(); | ||
| Type *I32Ty = IntegerType::getInt32Ty(Ctx); | ||
|
|
||
| SmallVector<Metadata *> MDArgs; | ||
| MDArgs.push_back(MDString::get(Ctx, Name)); | ||
| transform(ChangedTys, std::back_inserter(MDArgs), [=, &Ctx](auto &&CTy) { | ||
| return MDNode::get( | ||
| Ctx, {ConstantAsMetadata::get(ConstantInt::get(I32Ty, CTy.first, true)), | ||
| ValueAsMetadata::get(Constant::getNullValue(CTy.second))}); | ||
| }); | ||
| NMD->addOperand(MDNode::get(Ctx, MDArgs)); | ||
| } | ||
| // Returns F if aggregate argument/return types are not present or cloned F | ||
| // function with the types replaced by i32 types. The change in types is | ||
| // noted in 'spv.cloned_funcs' metadata for later restoration. | ||
|
|
@@ -503,7 +523,8 @@ SPIRVPrepareFunctions::removeAggregateTypesFromSignature(Function *F) { | |
| FunctionType *NewFTy = | ||
| FunctionType::get(RetType, ArgTypes, F->getFunctionType()->isVarArg()); | ||
| Function *NewF = | ||
| Function::Create(NewFTy, F->getLinkage(), F->getName(), *F->getParent()); | ||
| Function::Create(NewFTy, F->getLinkage(), F->getAddressSpace(), | ||
| F->getName(), F->getParent()); | ||
|
|
||
| ValueToValueMapTy VMap; | ||
| auto NewFArgIt = NewF->arg_begin(); | ||
|
|
@@ -518,22 +539,17 @@ SPIRVPrepareFunctions::removeAggregateTypesFromSignature(Function *F) { | |
| Returns); | ||
| NewF->takeName(F); | ||
|
|
||
| NamedMDNode *FuncMD = | ||
| F->getParent()->getOrInsertNamedMetadata("spv.cloned_funcs"); | ||
| SmallVector<Metadata *, 2> MDArgs; | ||
| MDArgs.push_back(MDString::get(B.getContext(), NewF->getName())); | ||
| for (auto &ChangedTyP : ChangedTypes) | ||
| MDArgs.push_back(MDNode::get( | ||
| B.getContext(), | ||
| {ConstantAsMetadata::get(B.getInt32(ChangedTyP.first)), | ||
| ValueAsMetadata::get(Constant::getNullValue(ChangedTyP.second))})); | ||
| MDNode *ThisFuncMD = MDNode::get(B.getContext(), MDArgs); | ||
| FuncMD->addOperand(ThisFuncMD); | ||
| addFunctionTypeMutation( | ||
| NewF->getParent()->getOrInsertNamedMetadata("spv.cloned_funcs"), | ||
| std::move(ChangedTypes), NewF->getName()); | ||
|
|
||
| for (auto *U : make_early_inc_range(F->users())) { | ||
| if (auto *CI = dyn_cast<CallInst>(U)) | ||
| CI->mutateFunctionType(NewF->getFunctionType()); | ||
| U->replaceUsesOfWith(F, NewF); | ||
| if (auto *C = dyn_cast<Constant>(U)) | ||
| C->handleOperandChange(F, NewF); | ||
| else | ||
| U->replaceUsesOfWith(F, NewF); | ||
| } | ||
|
Comment on lines
546
to
554
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've stumbled with the
for (auto *U : F->users()) {
auto *CI = dyn_cast<CallInst>(U);
if (CI && CI->getCalledFunction() == F)
CI->mutateFunctionType(NewF->getFunctionType());
}
F->replaceAllUsesWith(NewF);
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ok, I believe I understand the concern, this'll do bad things if As for using RAUW, I don't quite think we can do that, since it does |
||
|
|
||
| // register the mutation | ||
|
|
@@ -543,11 +559,79 @@ SPIRVPrepareFunctions::removeAggregateTypesFromSignature(Function *F) { | |
| return NewF; | ||
| } | ||
|
|
||
| // Mutates indirect callsites iff if aggregate argument/return types are present | ||
| // with the types replaced by i32 types. The change in types is noted in | ||
| // 'spv.mutated_callsites' metadata for later restoration. | ||
| bool SPIRVPrepareFunctions::removeAggregateTypesFromCalls(Function *F) { | ||
| if (F->isDeclaration() || F->isIntrinsic()) | ||
| return false; | ||
|
|
||
| SmallVector<std::pair<CallBase *, FunctionType *>> Calls; | ||
| for (auto &&I : instructions(F)) { | ||
| if (auto *CB = dyn_cast<CallBase>(&I)) { | ||
| if (!CB->getCalledOperand() || CB->getCalledFunction()) | ||
| continue; | ||
| if (CB->getType()->isAggregateType() || | ||
| any_of(CB->args(), [](auto &&Arg) { | ||
| return Arg->getType()->isAggregateType(); | ||
| })) | ||
| Calls.emplace_back(CB, nullptr); | ||
| } | ||
| } | ||
|
|
||
| if (Calls.empty()) | ||
| return false; | ||
|
|
||
| IRBuilder<> B(F->getContext()); | ||
|
|
||
| for (auto &&[CB, NewFnTy] : Calls) { | ||
| SmallVector<std::pair<int, Type *>> ChangedTypes; | ||
| SmallVector<Type *> NewArgTypes; | ||
|
|
||
| Type* RetTy = CB->getType(); | ||
| if (RetTy->isAggregateType()) { | ||
| ChangedTypes.emplace_back(-1, RetTy); | ||
| RetTy = B.getInt32Ty(); | ||
| } | ||
|
|
||
| for (auto &&Arg : CB->args()) { | ||
| if (Arg->getType()->isAggregateType()) { | ||
| NewArgTypes.push_back(B.getInt32Ty()); | ||
| ChangedTypes.emplace_back(Arg.getOperandNo(), Arg->getType()); | ||
| } else { | ||
| NewArgTypes.push_back(Arg->getType()); | ||
| } | ||
| } | ||
| NewFnTy = FunctionType::get(RetTy, NewArgTypes, | ||
| CB->getFunctionType()->isVarArg()); | ||
|
|
||
| if (!CB->hasName()) | ||
| CB->setName("spv.mutated_callsite." + F->getName()); | ||
| else | ||
| CB->setName( | ||
| "spv.named_mutated_callsite." + F->getName() + "." + CB->getName()); | ||
|
|
||
| addFunctionTypeMutation( | ||
| F->getParent()->getOrInsertNamedMetadata("spv.mutated_callsites"), | ||
| std::move(ChangedTypes), CB->getName()); | ||
| } | ||
|
|
||
| for (auto &&[CB, NewFTy] : Calls) { | ||
| if (NewFTy->getReturnType() != CB->getType()) | ||
| TM.getSubtarget<SPIRVSubtarget>(*F).getSPIRVGlobalRegistry()->addMutated( | ||
| CB, CB->getType()); | ||
| CB->mutateFunctionType(NewFTy); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool SPIRVPrepareFunctions::runOnModule(Module &M) { | ||
| bool Changed = false; | ||
| for (Function &F : M) { | ||
| Changed |= substituteIntrinsicCalls(&F); | ||
| Changed |= sortBlocks(F); | ||
| Changed |= removeAggregateTypesFromCalls(&F); | ||
| } | ||
|
|
||
| std::vector<Function *> FuncsWorklist; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.