[llvm] [Attributor] Pack out arguments into a struct (PR #119267)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 10 21:08:17 PST 2024
================
@@ -2963,6 +2963,132 @@ bool Attributor::shouldSeedAttribute(AbstractAttribute &AA) {
return Result;
}
+// For now: argument can be put in the struct if it's write only and
+// has no aliases.
+static bool canBeComapctedInAStruct(const Argument &Arg, Attributor &A,
+ const AbstractAttribute &QueryingAA) {
+ IRPosition ArgPosition = IRPosition::argument(Arg);
+ // Check if Arg has no alias.
+ auto *AAliasInfo =
+ A.getAAFor<AANoAlias>(QueryingAA, ArgPosition, DepClassTy::NONE);
+ if (!AAliasInfo || !AAliasInfo->isKnownNoAlias())
+ return false;
+
+ // Check if Arg is write-only.
+ const auto *MemBehaviorAA =
+ A.getAAFor<AAMemoryBehavior>(QueryingAA, ArgPosition, DepClassTy::NONE);
+ if (!MemBehaviorAA || !MemBehaviorAA->isKnownWriteOnly())
+ return false;
+
+ return true;
+}
+
+static void replaceArgRetWithStructRetCalls(Function &OldFunction,
+ Function &NewFunction) {
+ for (auto UseItr = OldFunction.use_begin(); UseItr != OldFunction.use_end();
+ ++UseItr) {
+ CallBase *Call = dyn_cast<CallBase>(UseItr->getUser());
+ if (!Call)
+ continue;
+
+ IRBuilder<> Builder(Call);
+ SmallVector<Value *, 8> NewArgs;
+ for (unsigned ArgIdx = 0; ArgIdx < Call->arg_size(); ++ArgIdx)
+ if (std::find_if(OldFunction.arg_begin(), OldFunction.arg_end(),
+ [&](Argument &Arg) {
+ return &Arg == Call->getArgOperand(ArgIdx);
+ }) == OldFunction.arg_end())
+ NewArgs.push_back(Call->getArgOperand(ArgIdx));
+
+ CallInst *NewCall = Builder.CreateCall(&NewFunction, NewArgs);
+ Call->replaceAllUsesWith(NewCall);
+ Call->eraseFromParent();
+ }
+}
+
+static bool convertOutArgsToRetStruct(Function &F, Attributor &A,
+ AbstractAttribute &QueryingAA) {
+ // Get valid ptr args.
+ DenseMap<Argument *, Type *> PtrToType;
+ for (unsigned ArgIdx = 0; ArgIdx < F.arg_size(); ++ArgIdx) {
+ Argument *Arg = F.getArg(ArgIdx);
+ if (Arg->getType()->isPointerTy() &&
+ canBeComapctedInAStruct(*Arg, A, QueryingAA)) {
+ // Get the the type of the pointer through its users
+ for (auto UseItr = Arg->use_begin(); UseItr != Arg->use_end(); ++UseItr) {
+ auto *Store = dyn_cast<StoreInst>(UseItr->getUser());
+ if (Store)
+ PtrToType[Arg] = Store->getValueOperand()->getType();
+ }
+ }
+ }
+
+ // If there is no valid candidates then return false.
+ if (PtrToType.empty())
+ return false;
+
+ // Create the new struct return type.
+ SmallVector<Type *, 4> OutStructElements;
+ if (auto *OriginalFuncTy = F.getReturnType(); !OriginalFuncTy->isVoidTy())
----------------
shiltian wrote:
Isn't this the C++17 recommended use?
https://github.com/llvm/llvm-project/pull/119267
More information about the llvm-commits
mailing list