This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] NEW Baseline tests for InstCombine optimization to merge GEP instructions with constant indices
ClosedPublic

Authored by huangjd on May 11 2022, 5:37 PM.

Details

Summary

Splitted the merge constant-indexed GEP optimization into two smaller transformations: 1. Merging GEP of GEP if both are constant-indexed. 2. Swapping constant indexed GEP in a chain of (non-constant) GEP to the end, so that 1 can be applied repeatedly.
There is existing code to partially handle transformation 1, but it only deals with limited cases

Unit tests are breaking down into two parts for the 2 transformations.

Diff Detail

Event Timeline

huangjd created this revision.May 11 2022, 5:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 11 2022, 5:37 PM
huangjd requested review of this revision.May 11 2022, 5:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 11 2022, 5:37 PM
nikic added inline comments.May 12 2022, 8:58 AM
llvm/test/Transforms/InstCombine/gep-canonicalize-constant-indices.ll
63

Isn't this test the same as multipleUses3? The comment on multipleUses3 seems correct (not valid to swap).

89

The usual way to add extra uses is call void @use(ptr %2).

llvm/test/Transforms/InstCombine/gep-merge-constant-indices.ll
163

What's + a in this comment?

173

Maybe I missed it, but it would be good to test a case where neither element type can be used, e.g. gep i24, 1 and gep i32, 1. Total offset is 7, which is not a multiple of 3 or 4.

huangjd added inline comments.May 12 2022, 11:24 AM
llvm/test/Transforms/InstCombine/gep-canonicalize-constant-indices.ll
63

This one

%2 = ptrtoint ptr **%p** to i64

The expression originally evaluates to

((i32*) p + 1) + (i64) p

After swapping it is

((i32*) p + (i64) p) + 1

which is still equivalent

nikic added inline comments.May 12 2022, 11:50 AM
llvm/test/Transforms/InstCombine/gep-canonicalize-constant-indices.ll
63

Oh, I see, the extra use is on %p here, not on %1.

huangjd updated this revision to Diff 429064.May 12 2022, 1:44 PM

Added more unit tests since existing InstCombine can handle merging GEP with partially constant index under some conditions.

huangjd marked 3 inline comments as done.May 12 2022, 1:45 PM
huangjd added inline comments.
llvm/test/Transforms/InstCombine/gep-merge-constant-indices.ll
163

typo, fixed.

173

Added more test cases

huangjd marked 3 inline comments as done.May 12 2022, 1:46 PM
huangjd updated this revision to Diff 429101.May 12 2022, 4:32 PM

Added one more edge case test

huangjd updated this revision to Diff 429112.May 12 2022, 6:47 PM

Fixed incorrect pointer expressions in comment

nikic accepted this revision.May 13 2022, 3:38 AM

LGTM

This revision is now accepted and ready to land.May 13 2022, 3:38 AM
huangjd updated this revision to Diff 430202.May 17 2022, 3:53 PM

Updated tests:

  1. Added more tests on swapping to handle conflict with LICM case
  2. Fixed some bugs on test cases.
  3. Removed some GEP merge test cases because with canonicalization, two-way merging is no longer needed
nlopes added a subscriber: nlopes.May 22 2022, 1:59 PM
nlopes added inline comments.
llvm/test/Transforms/InstCombine/gep-merge-constant-indices.ll
109

This transformation is incorrect, as the inbounds constraint is applied for each index, from left-to-right. Thus we can't introduce the -1 before the positive increment.
Not sure we need a condition this strong, but it allows easier decomposition of 1 gep into multiple ones.

Do we have a bug report tracking this issue or any patch under review?

nlopes added inline comments.May 26 2022, 8:42 AM
llvm/test/Transforms/InstCombine/gep-merge-constant-indices.ll
109

ping?

nikic added inline comments.May 26 2022, 8:57 AM
llvm/test/Transforms/InstCombine/gep-merge-constant-indices.ll
109

Can you please file an issue and assign it to me?