This is an archive of the discontinued LLVM Phabricator instance.

[Reland][LSR] Hoist IVInc to loop header if its all uses are in the loop header
ClosedPublic

Authored by bcl5980 on Nov 23 2022, 9:03 PM.

Details

Summary

Original code will cause crash when the load/store memory type is structure because isIndexedLoadLegal/isIndexedStore doesn't support struct type.
So we limit the load/store memory type to integer.

Origin commit message:
When the latch block is different from header block, IVInc will be expanded in the latch loop. We can't generate the post index load/store this case.
But if the IVInc only used in the loop, actually we still can use the post index load/store because when exit loop we don't care the last IVInc value.
So, trying to hoist IVInc to help backend to generate more post index load/store.

Fix #53625

Diff Detail

Event Timeline

bcl5980 created this revision.Nov 23 2022, 9:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 23 2022, 9:03 PM
bcl5980 requested review of this revision.Nov 23 2022, 9:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 23 2022, 9:03 PM
bcl5980 updated this revision to Diff 477682.Nov 23 2022, 9:32 PM
bcl5980 updated this revision to Diff 477683.Nov 23 2022, 9:52 PM
bcl5980 updated this revision to Diff 477856.Nov 24 2022, 9:55 PM

Update tests.

eopXD added a subscriber: eopXD.Nov 29 2022, 7:38 AM

Minor comments on style and redundant code, the change looks reasonable to me and resolves the problem.

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
5646

Personally I think isAllUsedInBB (or isUserAllWithinBB) is a more suitable name that follows general naming rules across the codebase.

5646

Also, maybe a comment for the function for clarity.

5658

CanHoistIVInc -> canHoistIVInc.

5668

The condition here makes the previous early-return condition redundant.

5668

Possibly a negative test case for this?

5671

For the sake of my own knowledge, may I ask why does this necessary has to be an Instruction?

5675

This looks familiar with mayUsePostIncMode

5701

No need to change the nested loop here, can do

/* ... */ = canHoistIVInc(TTI, Fixup, Uses[LUIdx], /* ... */);
eopXD added a reviewer: Restricted Project.Nov 29 2022, 7:44 AM
eopXD added inline comments.
llvm/test/Transforms/LoopStrengthReduce/ivincs-hoist.ll
4 ↗(On Diff #477856)

As a generic test case here (not under a specific target), is there other ways to show the effect of this patch without adding this target triple?

bcl5980 added inline comments.Nov 30 2022, 1:27 AM
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
5668

I'm not sure how to get the assertion. Why not in latch means it is in the header?

5671

I can change to Value here, but I feel strange if it is a constant.

5675

It looks mayUsePostIncMode is not what I want. It only checks the instruction is legal but haven't check the instruction opcode.

llvm/test/Transforms/LoopStrengthReduce/ivincs-hoist.ll
4 ↗(On Diff #477856)

The headache thing is we must have a target to enable the post-index load/store to enable the optimization. I don't know how to show the code change without target triple info.

bcl5980 updated this revision to Diff 478849.Nov 30 2022, 1:28 AM

addresss comments.

eopXD added a comment.Dec 20 2022, 2:27 AM

Sorry for the delay. Minor comments left and I think we are good to land this.

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
5668

Use LLVM_DEBUG and have the following at the first line of the test case (.ll file)

; REQUIRES: asserts
5675
if ((isa<LoadInst>(I) || isa<StoreInst>(I)) && mayUsePostIncMode(...)

Just trying my best to reuse existing code :)

llvm/test/Transforms/LoopStrengthReduce/ivincs-hoist.ll
4 ↗(On Diff #477856)

Then I guess the target triple here should not be added. You already have the test case under AaArch64 for this.

bcl5980 added inline comments.Jan 2 2023, 7:34 PM
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
5675

It looks I can't find an easy way to reuse mayUsePostIncMode. It need the paramter SCEV that I can't find here.

bcl5980 updated this revision to Diff 485909.Jan 2 2023, 7:37 PM
eopXD accepted this revision.Jan 3 2023, 6:13 PM

LGTM, thank you.

This revision is now accepted and ready to land.Jan 3 2023, 6:13 PM
fhahn added inline comments.Jan 4 2023, 6:04 AM
llvm/test/Transforms/LoopStrengthReduce/AArch64/pr53625.ll
3

This should not be needed, the tests don't require asserts.

fhahn added inline comments.Jan 4 2023, 6:10 AM
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
5646

nit: Check if all users of \p V are in \p BB?

name should use are instead of is and Users, as this checks all users.

5648

nit: use any_of?

5656

...all IVInc users are ...

5677

can just return (isa<LoadInst>(I) &&.....

llvm/test/Transforms/LoopStrengthReduce/AArch64/pr53625.ll
100

nit: drop redundant indvars. prefix to make IR more concise.

bcl5980 updated this revision to Diff 486450.Jan 4 2023, 7:18 PM

address comments

@fhahn , do you have any other comments for the patch?

This revision was landed with ongoing or failed builds.Jan 10 2023, 2:34 AM
This revision was automatically updated to reflect the committed changes.
tobiasjs added a subscriber: tobiasjs.EditedJan 10 2023, 7:43 AM

This commit seems to cause a regression in numba tests. The call to TTI.isIndexedLoadLegal(...) calls with type that has TypeID StructTyID, which causes an abort in getValueType because AllowUnknown is false by default.

This commit seems to cause a regression in numba tests. The call to TTI.isIndexedLoadLegal(...) calls with type that has TypeID StructTyID, which causes an abort in getValueType because AllowUnknown is false by default.

@tobiasjs , can you help to upload the regression case?

bcl5980 updated this revision to Diff 496352.Feb 10 2023, 12:31 AM

Fix crash when the load/store memory type is struct.

bcl5980 retitled this revision from [LSR] Hoist IVInc to loop header if its all uses are in the loop header to [Reland][LSR] Hoist IVInc to loop header if its all uses are in the loop header.Feb 10 2023, 12:32 AM
bcl5980 edited the summary of this revision. (Show Details)
bcl5980 updated this revision to Diff 496358.Feb 10 2023, 12:50 AM

rebase code.