llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Inliner loses the original alignment for passing by value argument

Open igor-breger opened this issue 6 years ago • 3 comments

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
}

igor-breger avatar Jun 24 '20 11:06 igor-breger

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:

  1. Assign the issue to you.
  2. Fix the issue locally.
  3. Run the test suite locally. 3.1) Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  4. Create a git commit
  5. Run git clang-format HEAD~1 to format your changes.
  6. 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

llvmbot avatar Aug 17 '23 23:08 llvmbot

@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?

Endilll avatar Aug 18 '23 06:08 Endilll

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

arsenm avatar Aug 21 '23 20:08 arsenm