[clang] [llvm] [IPO] Optimise variadic functions (PR #92850)

via llvm-commits llvm-commits at lists.llvm.org
Mon May 20 20:45:28 PDT 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 560c2fd3d427a5e2dc2361abde1142f3fda40253 15061bfbc2dc06de5bac32628389386cadaa5632 -- clang/test/CodeGen/voidptr-vaarg.c clang/test/CodeGenCXX/inline-then-fold-variadics.cpp llvm/include/llvm/Transforms/IPO/ExpandVariadics.h llvm/lib/Transforms/IPO/ExpandVariadics.cpp clang/test/CodeGen/aarch64-ABI-align-packed.c llvm/include/llvm/InitializePasses.h llvm/lib/Passes/PassBuilder.cpp llvm/lib/Passes/PassBuilderPipelines.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/include/llvm/Transforms/IPO/ExpandVariadics.h b/llvm/include/llvm/Transforms/IPO/ExpandVariadics.h
index 02f3aa45ef..f7c9618bad 100644
--- a/llvm/include/llvm/Transforms/IPO/ExpandVariadics.h
+++ b/llvm/include/llvm/Transforms/IPO/ExpandVariadics.h
@@ -18,9 +18,9 @@ class OptimizationLevel;
 
 enum class ExpandVariadicsMode {
   Unspecified, // Use the implementation defaults
-  Disable, // Disable the pass entirely
-  Optimize, // Optimise without changing ABI
-  Lowering, // Change variadic calling convention
+  Disable,     // Disable the pass entirely
+  Optimize,    // Optimise without changing ABI
+  Lowering,    // Change variadic calling convention
 };
 
 class ExpandVariadicsPass : public PassInfoMixin<ExpandVariadicsPass> {
diff --git a/llvm/lib/Transforms/IPO/ExpandVariadics.cpp b/llvm/lib/Transforms/IPO/ExpandVariadics.cpp
index c04e16cb7b..e27c391d88 100644
--- a/llvm/lib/Transforms/IPO/ExpandVariadics.cpp
+++ b/llvm/lib/Transforms/IPO/ExpandVariadics.cpp
@@ -106,8 +106,7 @@ public:
   virtual Value *initializeVAList(LLVMContext &Ctx, IRBuilder<> &Builder,
                                   AllocaInst *, Value * /*buffer*/) = 0;
 
-  struct VAArgSlotInfo
-  {
+  struct VAArgSlotInfo {
     Align Align;   // With respect to the call frame
     bool Indirect; // Passed via a pointer
     bool Unknown;  // Cannot analyse this type, cannot transform the call
@@ -178,7 +177,6 @@ public:
   bool runOnModule(Module &M) override;
   bool runOnFunction(Module &M, IRBuilder<> &Builder, Function *F);
 
-
   bool rewriteABI() { return Mode == ExpandVariadicsMode::Lowering; }
 
   void memcpyVAListPointers(const DataLayout &DL, IRBuilder<> &Builder,
@@ -190,7 +188,6 @@ public:
                                ConstantInt::get(Type::getInt32Ty(Ctx), Size));
   }
 
-
   template <Intrinsic::ID ID, typename InstructionType>
   bool expandIntrinsicUsers(Module &M, IRBuilder<> &Builder,
                             PointerType *ArgType) {
@@ -217,7 +214,6 @@ public:
   bool expandVAIntrinsicCall(IRBuilder<> &Builder, const DataLayout &DL,
                              VACopyInst *Inst);
 
-  
   FunctionType *inlinableVariadicFunctionType(Module &M, FunctionType *FTy) {
     // The type of "FTy" with the ... removed and a va_list appended
     SmallVector<Type *> ArgTypes(FTy->param_begin(), FTy->param_end());
@@ -492,7 +488,8 @@ bool ExpandVariadics::runOnFunction(Module &M, IRBuilder<> &Builder,
   assert(VariadicWrapper->isDeclaration());
   assert(OriginalFunction->use_empty());
 
-  // Create a new function taking va_list containing the implementation of the original
+  // Create a new function taking va_list containing the implementation of the
+  // original
   Function *FixedArityReplacement =
       deriveFixedArityReplacement(M, Builder, OriginalFunction);
   assert(OriginalFunction->isDeclaration());
@@ -511,7 +508,6 @@ bool ExpandVariadics::runOnFunction(Module &M, IRBuilder<> &Builder,
   // 2. a variadic function that unconditionally calls a fixed arity replacement
   // 3. a fixed arity function equivalent to the original function
 
-  
   // Replace known calls to the variadic with calls to the va_list equivalent
   for (User *U : llvm::make_early_inc_range(VariadicWrapper->users())) {
     if (CallBase *CB = dyn_cast<CallBase>(U)) {
@@ -524,13 +520,11 @@ bool ExpandVariadics::runOnFunction(Module &M, IRBuilder<> &Builder,
     }
   }
 
-
   Function *const ExternallyAccessible =
-      rewriteABI() ? FixedArityReplacement : VariadicWrapper;  
+      rewriteABI() ? FixedArityReplacement : VariadicWrapper;
   Function *const InternalOnly =
-    rewriteABI() ? VariadicWrapper : FixedArityReplacement;
+      rewriteABI() ? VariadicWrapper : FixedArityReplacement;
 
-  
   // care needed over other attributes, metadata etc
 
   ExternallyAccessible->setLinkage(OriginalFunction->getLinkage());
@@ -548,10 +542,10 @@ bool ExpandVariadics::runOnFunction(Module &M, IRBuilder<> &Builder,
   if (rewriteABI()) {
     // All known calls to the function have been removed by expandCall
     // Resolve everything else by replace all uses with
-    
+
     VariadicWrapper->replaceAllUsesWith(FixedArityReplacement);
-    
-    assert (VariadicWrapper->use_empty());
+
+    assert(VariadicWrapper->use_empty());
     VariadicWrapper->eraseFromParent();
   }
 
@@ -613,10 +607,8 @@ ExpandVariadics::deriveFixedArityReplacement(Module &M, IRBuilder<> &Builder,
   NF->setName(F.getName() + ".valist");
   NF->IsNewDbgInfoFormat = F.IsNewDbgInfoFormat;
 
-
   AttrBuilder ParamAttrs(Ctx);
 
-
   AttributeList Attrs = NF->getAttributes();
   Attrs = Attrs.addParamAttributes(Ctx, NFTy->getNumParams() - 1, ParamAttrs);
   NF->setAttributes(Attrs);
@@ -658,9 +650,11 @@ ExpandVariadics::defineVariadicWrapper(Module &M, IRBuilder<> &Builder,
   auto *BB = BasicBlock::Create(Ctx, "entry", &F);
   Builder.SetInsertPoint(BB);
 
-  AllocaInst *VaListInstance = Builder.CreateAlloca(VaListTy, nullptr, "va_list");
+  AllocaInst *VaListInstance =
+      Builder.CreateAlloca(VaListTy, nullptr, "va_list");
 
-  Builder.CreateLifetimeStart(VaListInstance, sizeOfAlloca(Ctx, DL, VaListInstance));
+  Builder.CreateLifetimeStart(VaListInstance,
+                              sizeOfAlloca(Ctx, DL, VaListInstance));
 
   Builder.CreateIntrinsic(Intrinsic::vastart, {DL.getAllocaPtrType(Ctx)},
                           {VaListInstance});
@@ -674,9 +668,11 @@ ExpandVariadics::defineVariadicWrapper(Module &M, IRBuilder<> &Builder,
   CallInst *Result = Builder.CreateCall(FixedArityReplacement, Args);
   Result->setTailCallKind(CallInst::TCK_Tail);
 
-  Builder.CreateIntrinsic(Intrinsic::vaend, {DL.getAllocaPtrType(Ctx)}, {VaListInstance});
-  Builder.CreateLifetimeEnd(VaListInstance, sizeOfAlloca(Ctx, DL, VaListInstance));
-  
+  Builder.CreateIntrinsic(Intrinsic::vaend, {DL.getAllocaPtrType(Ctx)},
+                          {VaListInstance});
+  Builder.CreateLifetimeEnd(VaListInstance,
+                            sizeOfAlloca(Ctx, DL, VaListInstance));
+
   if (Result->getType()->isVoidTy())
     Builder.CreateRetVoid();
   else
@@ -725,7 +721,6 @@ bool ExpandVariadics::expandCall(Module &M, IRBuilder<> &Builder, CallBase *CB,
 
   uint64_t CurrentOffset = 0;
 
-
   for (unsigned I = FuncType->getNumParams(), E = CB->arg_size(); I < E; ++I) {
     Value *ArgVal = CB->getArgOperand(I);
     bool IsByVal = CB->paramHasAttr(I, Attribute::ByVal);
@@ -863,8 +858,7 @@ bool ExpandVariadics::expandCall(Module &M, IRBuilder<> &Builder, CallBase *CB,
     if (!ABI->vaListPassedInSSARegister()) {
       Type *VaListTy = ABI->vaListType(Ctx);
       Builder.SetInsertPointPastAllocas(CBF);
-      Builder.SetCurrentDebugLocation(
-          CB->getStableDebugLoc());
+      Builder.SetCurrentDebugLocation(CB->getStableDebugLoc());
       VaList = Builder.CreateAlloca(VaListTy, nullptr, "va_list");
       Builder.SetInsertPoint(CB);
       Builder.CreateLifetimeStart(VaList, sizeOfAlloca(Ctx, DL, VaList));
@@ -890,7 +884,7 @@ bool ExpandVariadics::expandCall(Module &M, IRBuilder<> &Builder, CallBase *CB,
   // Assert won't be true once InvokeInst is implemented in a later patch,
   // current invariant is established by callinstRewritable() at the start
   assert(isa<CallInst>(CB));
-  
+
   if (CallInst *CI = dyn_cast<CallInst>(CB)) {
 
     Value *Dst = NF ? NF : CI->getCalledOperand();
@@ -959,7 +953,8 @@ bool ExpandVariadics::expandVAIntrinsicCall(IRBuilder<> &Builder,
   } else {
     // Otherwise emit a vacopy to pick up target-specific handling if any
     auto &Ctx = Builder.getContext();
-    Builder.CreateIntrinsic(Intrinsic::vacopy, {DL.getAllocaPtrType(Ctx)}, {VaStartArg, PassedVaList});
+    Builder.CreateIntrinsic(Intrinsic::vacopy, {DL.getAllocaPtrType(Ctx)},
+                            {VaStartArg, PassedVaList});
   }
 
   Inst->eraseFromParent();
@@ -996,25 +991,28 @@ template <uint32_t MinAlign, uint32_t MaxAlign> Align clampAlign(Align A) {
   return A;
 }
 
-  bool simpleScalarType(Type* Parameter)
-  {
-    // This is a stop-gap. The MVP can optimise x64 and aarch64 on linux
-    // for sufficiently simple calls.
-    if (Parameter->isDoubleTy()) return true;
-
-    if (Parameter->isIntegerTy(32)) return true;
-    if (Parameter->isIntegerTy(64)) return true;
+bool simpleScalarType(Type *Parameter) {
+  // This is a stop-gap. The MVP can optimise x64 and aarch64 on linux
+  // for sufficiently simple calls.
+  if (Parameter->isDoubleTy())
+    return true;
 
-    if (Parameter->isPointerTy()) { return true; }
+  if (Parameter->isIntegerTy(32))
+    return true;
+  if (Parameter->isIntegerTy(64))
+    return true;
 
-    return false;
+  if (Parameter->isPointerTy()) {
+    return true;
   }
-  
+
+  return false;
+}
 
 struct AArch64 final : public VariadicABIInfo {
   // https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst
   // big endian, little endian ILP32 have their own triples
-  
+
   bool vaListPassedInSSARegister() override { return false; }
 
   Type *vaListType(LLVMContext &Ctx) override {
@@ -1089,7 +1087,6 @@ struct AArch64 final : public VariadicABIInfo {
   }
 };
 
-
 struct Wasm final : public VariadicABIInfo {
   bool vaListPassedInSSARegister() override { return true; }
 
@@ -1121,7 +1118,6 @@ struct Wasm final : public VariadicABIInfo {
   }
 };
 
-  
 struct X64SystemV final : public VariadicABIInfo {
   bool vaListPassedInSSARegister() override { return false; }
 
@@ -1154,7 +1150,7 @@ struct X64SystemV final : public VariadicABIInfo {
     // purpose and floating point regions empty, such that only the overflow
     // area is used. That means a single contiguous struct can be the backing
     // store and simpler code to optimise in the inlining case.
-    
+
     Idxs[2] = ConstantInt::get(I32, 0);
     Builder.CreateStore(
         ConstantInt::get(I32, 48),
@@ -1187,12 +1183,12 @@ struct X64SystemV final : public VariadicABIInfo {
     // X64 behaviour in clang:
     // Slots are at least eight byte aligned and at most naturally aligned
     // This matches clang, not the ABI docs.
-    
+
     Align A = clampAlign<8, 0u>(DL.getABITypeAlign(Parameter));
     bool Indirect = false;
     bool Unknown = !simpleScalarType(Parameter);
-    return {A, Indirect, Unknown}; 
- }
+    return {A, Indirect, Unknown};
+  }
 };
 
 std::unique_ptr<VariadicABIInfo>
@@ -1204,8 +1200,7 @@ VariadicABIInfo::create(llvm::Triple const &Triple) {
     return std::make_unique<AArch64>();
   }
 
-
-  case Triple::wasm32: {    
+  case Triple::wasm32: {
     return std::make_unique<Wasm>();
   }
 
@@ -1243,8 +1238,6 @@ PreservedAnalyses ExpandVariadicsPass::run(Module &M, ModuleAnalysisManager &) {
 ExpandVariadicsPass::ExpandVariadicsPass(OptimizationLevel Level)
     : ExpandVariadicsPass(Level == OptimizationLevel::O0
                               ? ExpandVariadicsMode::Disable
-                          : ExpandVariadicsMode::Optimize)
-{
-}
+                              : ExpandVariadicsMode::Optimize) {}
 
 ExpandVariadicsPass::ExpandVariadicsPass(ExpandVariadicsMode M) : Mode(M) {}

``````````

</details>


https://github.com/llvm/llvm-project/pull/92850


More information about the llvm-commits mailing list