This is an archive of the discontinued LLVM Phabricator instance.

[AggressiveInstCombine] Handle the insert point of the merged load correctly.
ClosedPublic

Authored by bipmis on Nov 1 2022, 3:12 PM.

Details

Summary

This patch updates the load insert point of the merged load in AggressiveInstCombine() as implemeted in
https://reviews.llvm.org/D135137
This is done to handle the reported test breaks.

Diff Detail

Event Timeline

bipmis requested review of this revision.Nov 1 2022, 3:12 PM
bipmis created this revision.
asmok-g added a subscriber: asmok-g.Nov 2 2022, 7:14 AM
asbirlea added inline comments.
llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
755

Is it possible for InsertPoint to be nullptr here?

We found another case, that looks like this where the p3 is read out of order. We are looking into getting that fixed too.

define i32 @loadCombine_4consecutive_badinsert(ptr %p) {
; LE-LABEL: @loadCombine_4consecutive_badinsert(
; LE-NEXT:    [[P3:%.*]] = getelementptr i8, ptr [[P:%.*]], i32 3
; LE-NEXT:    [[L1:%.*]] = load i32, ptr [[P]], align 1
; LE-NEXT:    store i8 0, ptr [[P3]], align 1
; LE-NEXT:    ret i32 [[L1]]
;
  %p1 = getelementptr i8, ptr %p, i32 1
  %p2 = getelementptr i8, ptr %p, i32 2
  %p3 = getelementptr i8, ptr %p, i32 3
  %l2 = load i8, ptr %p1
  store i8 0, ptr %p3, align 1
  %l3 = load i8, ptr %p2
  %l4 = load i8, ptr %p3
  %l1 = load i8, ptr %p

  %e1 = zext i8 %l1 to i32
  %e2 = zext i8 %l2 to i32
  %e3 = zext i8 %l3 to i32
  %e4 = zext i8 %l4 to i32

  %s2 = shl i32 %e2, 8
  %s3 = shl i32 %e3, 16
  %s4 = shl i32 %e4, 24

  %o1 = or i32 %e1, %s2
  %o2 = or i32 %o1, %s3
  %o3 = or i32 %o2, %s4
  ret i32 %o3
}
bipmis updated this revision to Diff 474234.Nov 9 2022, 5:14 AM

Update the patch to handle various corner cases of Alias Analysis by handling the insert point of the load and associated pointer. For 2 loads move the insert point to the one which occurs first. Additionally look for clobber in the merged load when the merged load occurs later.

dmgreen added inline comments.Nov 10 2022, 9:03 AM
llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
794

If the pointer operand isn't in the same block, is that a problem? If they are in different blocks, and we know all the loads are in the same block, then we know the pointer operand dominates the RootInsert I think. It wont need the moveBefore below.

bipmis added inline comments.Nov 18 2022, 7:52 AM
llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
794

Good Point. Will update the patch with this change.

bipmis updated this revision to Diff 476842.Nov 21 2022, 3:31 AM
bipmis added a subscriber: dstuttard.

Handle review comments on Load and Load pointer in separate BB's.
@dstuttard Would be good if the patch can be tested with non-Opaque Pointers as well. I have done a sanity test and it should work OK. Thanks.

dmgreen accepted this revision.Nov 23 2022, 6:45 AM

Thanks for the update. This LGTM.

This revision is now accepted and ready to land.Nov 23 2022, 6:45 AM
This revision was landed with ongoing or failed builds.Nov 29 2022, 2:54 AM
This revision was automatically updated to reflect the committed changes.

Hi, this patch is creating malformed IR for some cases. Test case here: https://github.com/llvm/llvm-project/issues/62756

Hi, this patch is creating malformed IR for some cases. Test case here: https://github.com/llvm/llvm-project/issues/62756

Thanks for reporting. I can see the issue and have fixed it in https://reviews.llvm.org/D150864.
I have not optimised it exclusively in the AggressiveInstCombine Pass because the InstCombine does the same and generates a single GEP for the nested patterns which can be reduced. This is then Load merged by the AggressiveInstCombine.