diff options
author | sallto <68823230+sallto@users.noreply.github.com> | 2025-04-26 21:38:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-26 21:38:58 +0200 |
commit | 419a2cb218245b90ace9e0a460d94057e7091002 (patch) | |
tree | 017df6e5966b52995fdd589d8defd3f89dc7dc19 /llvm/lib/Transforms/Utils/InlineFunction.cpp | |
parent | 2f08927fd5f44418482dd583d3d451acc6669fe6 (diff) | |
download | llvm-419a2cb218245b90ace9e0a460d94057e7091002.zip llvm-419a2cb218245b90ace9e0a460d94057e7091002.tar.gz llvm-419a2cb218245b90ace9e0a460d94057e7091002.tar.bz2 |
[Inliner] Preserve alignment of byval arguments (#137455)
Previously the inliner always produced a memcpy with alignment 1 for src
and destination, leading to potentially suboptimal Codegen.
Since the Src ptr alignment is only available through the CallBase it
has to be passed to HandleByValArgumentInit. Dst Alignment is already
known so it doesn't have to be passed along.
If there is no specified Src Alignment my changes cause the ptr to have
no align data attached instead of align 1 as before (see
inline-tail.ll), I believe this is fine but since I'm a first time
contributor, please confirm.
My changes are already covered by 4 existing regression tests, so I did
not add any additional ones.
The example from #45778 now results in:
```C
opt -S -passes=inline,instcombine,sroa,instcombine test.ll
define dso_local i32 @test(ptr %t) {
entry:
%.sroa.0.0.copyload = load ptr, ptr %t, align 8 # this used to be align 1 in the original issue
%arrayidx.i = getelementptr inbounds nuw i8, ptr %.sroa.0.0.copyload, i64 24
%0 = load i32, ptr %arrayidx.i, align 4
ret i32 %0
}
```
Fixes #45778.
Diffstat (limited to 'llvm/lib/Transforms/Utils/InlineFunction.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/InlineFunction.cpp | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp index 295518f..7a91620 100644 --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -1703,7 +1703,8 @@ static void AddAlignmentAssumptions(CallBase &CB, InlineFunctionInfo &IFI) { } static void HandleByValArgumentInit(Type *ByValType, Value *Dst, Value *Src, - Module *M, BasicBlock *InsertBlock, + MaybeAlign SrcAlign, Module *M, + BasicBlock *InsertBlock, InlineFunctionInfo &IFI, Function *CalledFunc) { IRBuilder<> Builder(InsertBlock, InsertBlock->begin()); @@ -1711,11 +1712,10 @@ static void HandleByValArgumentInit(Type *ByValType, Value *Dst, Value *Src, Value *Size = Builder.getInt64(M->getDataLayout().getTypeStoreSize(ByValType)); - // Always generate a memcpy of alignment 1 here because we don't know - // the alignment of the src pointer. Other optimizations can infer - // better alignment. - CallInst *CI = Builder.CreateMemCpy(Dst, /*DstAlign*/ Align(1), Src, - /*SrcAlign*/ Align(1), Size); + Align DstAlign = Dst->getPointerAlignment(M->getDataLayout()); + + // Generate a memcpy with the correct alignments. + CallInst *CI = Builder.CreateMemCpy(Dst, DstAlign, Src, SrcAlign, Size); // The verifier requires that all calls of debug-info-bearing functions // from debug-info-bearing functions have a debug location (for inlining @@ -2629,9 +2629,12 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, struct ByValInit { Value *Dst; Value *Src; + MaybeAlign SrcAlign; Type *Ty; }; - // Keep a list of pair (dst, src) to emit byval initializations. + // Keep a list of tuples (dst, src, src_align) to emit byval + // initializations. Src Alignment is only available though the callbase, + // therefore has to be saved. SmallVector<ByValInit, 4> ByValInits; // When inlining a function that contains noalias scope metadata, @@ -2661,8 +2664,9 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, &CB, CalledFunc, IFI, CalledFunc->getParamAlign(ArgNo)); if (ActualArg != *AI) - ByValInits.push_back( - {ActualArg, (Value *)*AI, CB.getParamByValType(ArgNo)}); + ByValInits.push_back({ActualArg, (Value *)*AI, + CB.getParamAlign(ArgNo), + CB.getParamByValType(ArgNo)}); } VMap[&*I] = ActualArg; @@ -2712,8 +2716,9 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, // Inject byval arguments initialization. for (ByValInit &Init : ByValInits) - HandleByValArgumentInit(Init.Ty, Init.Dst, Init.Src, Caller->getParent(), - &*FirstNewBlock, IFI, CalledFunc); + HandleByValArgumentInit(Init.Ty, Init.Dst, Init.Src, Init.SrcAlign, + Caller->getParent(), &*FirstNewBlock, IFI, + CalledFunc); std::optional<OperandBundleUse> ParentDeopt = CB.getOperandBundle(LLVMContext::OB_deopt); |