Inliner loses the original alignment for passing by value argument
| Bugzilla Link | 46433 |
| Version | trunk |
| OS | Windows NT |
| CC | @ayalz,@gchatelet |
Extended Description
Looks like the passing of the struct by value, to be inlined into a memcpy with align 1 source pointer casted to i8*, to be sroa’d, loses the original alignment. In this case target sensitive to alignment generate sub-optimal code.
C code example
typedef struct
{
int* ptr1;
int* ptr2;
int part;
} LabelsType ;
int call1(LabelsType _labels, int tileNum) {
return _labels.ptr1[tileNum];
}
int test(LabelsType * t)
{
return call1(*t, 6);
}
clang -O0 -S -emit-llvm
%struct.LabelsType = type { i32*, i32*, i32 }
define dso_local i32 @call1(%struct.LabelsType* byval(%struct.LabelsType) align 8 %_labels, i32 %tileNum) {
entry:
%tileNum.addr = alloca i32, align 4
store i32 %tileNum, i32* %tileNum.addr, align 4
%ptr1 = getelementptr inbounds %struct.LabelsType, %struct.LabelsType* %_labels, i32 0, i32 0
# originally we have align 8
%0 = load i32*, i32** %ptr1, align 8
%1 = load i32, i32* %tileNum.addr, align 4
%idxprom = sext i32 %1 to i64
%arrayidx = getelementptr inbounds i32, i32* %0, i64 %idxprom
%2 = load i32, i32* %arrayidx, align 4
ret i32 %2
}
define dso_local i32 @test(%struct.LabelsType* %t) {
entry:
%t.addr = alloca %struct.LabelsType*, align 8
store %struct.LabelsType* %t, %struct.LabelsType** %t.addr, align 8
%0 = load %struct.LabelsType*, %struct.LabelsType** %t.addr, align 8
%call = call i32 @call1(%struct.LabelsType* byval(%struct.LabelsType) align 8 %0, i32 6)
ret i32 %call
}
opt -S --inline -instcombine -sroa -instcombine <opt0 file>
define dso_local i32 @call1(%struct.LabelsType* byval(%struct.LabelsType) align 8 %_labels, i32 %tileNum) {
entry:
%ptr1 = getelementptr inbounds %struct.LabelsType, %struct.LabelsType* %_labels, i64 0, i32 0
%0 = load i32*, i32** %ptr1, align 8
%idxprom = sext i32 %tileNum to i64
%arrayidx = getelementptr inbounds i32, i32* %0, i64 %idxprom
%1 = load i32, i32* %arrayidx, align 4
ret i32 %1
}
define dso_local i32 @test(%struct.LabelsType* %t) {
entry:
%.sroa.0.0..cast.sroa_idx = getelementptr inbounds %struct.LabelsType, %struct.LabelsType* %t, i64 0, i32 0
# we got align 1
%.sroa.0.0.copyload = load i32*, i32** %.sroa.0.0..cast.sroa_idx, align 1
%arrayidx.i = getelementptr inbounds i32, i32* %.sroa.0.0.copyload, i64 6
%0 = load i32, i32* %arrayidx.i, align 4
ret i32 %0
}
For similar code (manual inlining)
int test1(LabelsType * t)
{
return t->ptr1[6];
}
We got align 8 load ( %0 = load i32*, i32** %ptr1, align 8 ):
define dso_local i32 @test1(%struct.LabelsType* nocapture readonly %t) local_unnamed_addr #0 {
entry:
%ptr1 = getelementptr inbounds %struct.LabelsType, %struct.LabelsType* %t, i64 0, i32 0
%0 = load i32*, i32** %ptr1, align 8, !tbaa !2
%arrayidx = getelementptr inbounds i32, i32* %0, i64 6
%1 = load i32, i32* %arrayidx, align 4, !tbaa !8
ret i32 %1
}
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Assign the issue to you.
- Fix the issue locally.
- Run the test suite locally.
3.1) Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a
gitcommit - Run
git clang-format HEAD~1to format your changes. - Submit the patch to Phabricator. 6.1) Detailed instructions can be found here
For more instructions on how to submit a patch to LLVM, see our documentation.
If you have any further questions about this issue, don't hesitate to ask via a comment on this Github issue.
@llvm/issue-subscribers-good-first-issue
@arsenm Can you provide an analysis and point to relevant functions or lines of code, in order to make it easier for a newcomer to pick this up?
Seems to be intentional:
// 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);
but this isn't quite right, you still have attributes from the call site
HandleByValArgumentInit should account for those
// Calculate the vector of arguments to pass into the function cloner, which
// matches up the formal to the actual argument values.
auto AI = CB.arg_begin();
unsigned ArgNo = 0;
for (Function::arg_iterator I = CalledFunc->arg_begin(),
E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) {
Value *ActualArg = *AI;
// When byval arguments actually inlined, we need to make the copy implied
// by them explicit. However, we don't do this if the callee is readonly
// or readnone, because the copy would be unneeded: the callee doesn't
// modify the struct.
if (CB.isByValArgument(ArgNo)) {
ActualArg = HandleByValArgument(CB.getParamByValType(ArgNo), ActualArg,
&CB, CalledFunc, IFI,
CalledFunc->getParamAlign(ArgNo));
if (ActualArg != *AI)
ByValInits.push_back(
{ActualArg, (Value *)*AI, CB.getParamByValType(ArgNo)});
}
VMap[&*I] = ActualArg;
}
This should be checking CB.getParamAlign(ArgNo) and carried along with the byval type