This is an archive of the discontinued LLVM Phabricator instance.

Do not traverse ConstantData use-list in LoopInterchange
ClosedPublic

Authored by willir on Jan 14 2021, 12:32 PM.

Details

Summary

The findInnerReductionPhi function is looking for reduction variables,
which cannot be constants; also, walking users of some Constants (particularly ConstantData)
doesn't make semantic sense in a function pass since its users can be spread among different
functions or modules that happen to be in the same LLVMContext

Diff Detail

Event Timeline

willir created this revision.Jan 14 2021, 12:32 PM
willir requested review of this revision.Jan 14 2021, 12:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 14 2021, 12:32 PM
dexonsmith added a subscriber: fhahn.

@fhahn, can you help with the review here? This is one of only a few instances of walking the use-list of ConstantData. The broader context is an old RFC that @willir is working with me on:
https://lists.llvm.org/pipermail/llvm-dev/2016-September/105157.html

(I know the motivation and context, but I'm not the right person to review functional changes to this pass.)

llvm/lib/Transforms/Scalar/LoopInterchange.cpp
663–675

It looks like sometimes findInnerReductionPhi is running on ConstantData, which can come from another function (or module), and will eventually lose its use-lists (no one should walk the use-list of i1 0; usually anything they learn from that will be wrong).

  • @willir's current patch changes the order of iteration. I think we'll need to check compile-time if we do this.
  • Another option would be to return nullptr on isa<ConstantData>(), if it's correct.
  • If that's correct, should this return nullptr on isa<Constant>() more generally?
fhahn added inline comments.Jan 16 2021, 7:24 AM
llvm/lib/Transforms/Scalar/LoopInterchange.cpp
663–675

If that's correct, should this return nullptr on isa<Constant>() more generally?

I think findInnerReductionPhi should never return anything other than nullptr for constants; it is looking for a reduction results, which should always be an instruction.

So returning nullptr on isa<Constant> seems the best way forward. It would also be good to add a test with LCSSA phis with constant incoming values, e.g something like the below to llvm/test/Transforms/LoopInterchange/reductions-across-inner-and-outer-loop.ll

define i64 @test1([100 x [100 x i64]]* %Arr) {
entry:
  br label %for1.header

for1.header:                                         ; preds = %for1.inc, %entry
  %indvars.iv23 = phi i64 [ 0, %entry ], [ %indvars.iv.next24, %for1.inc ]
  %sum.outer = phi i64 [ 0, %entry ], [ %const.lcssa, %for1.inc ]
  br label %for2

for2:                                        ; preds = %for2, %for1.header
  %indvars.iv = phi i64 [ 0, %for1.header ], [ %indvars.iv.next.3, %for2 ]
  %sum.inner = phi i64 [ %sum.outer, %for1.header ], [ %sum.inc, %for2 ]
  %arrayidx = getelementptr inbounds [100 x [100 x i64]], [100 x [100 x i64]]* %Arr, i64 0, i64 %indvars.iv, i64 %indvars.iv23
  %lv = load i64, i64* %arrayidx, align 4
  %sum.inc = add i64 %sum.inner, %lv
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 1
  %exit1 = icmp eq i64 %indvars.iv.next.3, 100
  br i1 %exit1, label %for1.inc, label %for2

for1.inc:                                ; preds = %for2
  %const.lcssa = phi i64 [ 1, %for2 ]
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, 1
  %exit2 = icmp eq i64 %indvars.iv.next24, 100
  br i1 %exit2, label %for1.loopexit, label %for1.header

for1.loopexit:                                 ; preds = %for1.inc
  %const.lcssa2 = phi i64 [ %const.lcssa, %for1.inc ]
  ret i64 %const.lcssa2
}
willir updated this revision to Diff 317331.Jan 18 2021, 5:26 AM

Make findInnerReductionPhi return nullptr on isa<Constant> as suggested
by @fhahn.
Add a test for that case.

fhahn added inline comments.Jan 18 2021, 12:04 PM
llvm/lib/Transforms/Scalar/LoopInterchange.cpp
664

Could you add a comment with the reasoning for the early exit here?

willir updated this revision to Diff 317437.Jan 18 2021, 4:22 PM

Add a comment with the reasoning for the early exit

231084fg removed rG LLVM Github Monorepo as the repository for this revision.
231084fg changed the visibility from "Public (No Login Required)" to "231084fg (Federica Govoni)".
231084fg removed a project: Restricted Project.
231084fg removed subscribers: fhahn, llvm-commits, hiraditya.
Herald added a project: Restricted Project. · View Herald TranscriptJan 18 2021, 5:15 PM
willir changed the visibility from "231084fg (Federica Govoni)" to "Public (No Login Required)".
willir added subscribers: fhahn, llvm-commits, hiraditya.
fhahn accepted this revision.Jan 20 2021, 7:51 AM

LGTM, thanks!

Do not traverse ConstantData use-list in LookInterchange

There's a typo in the title: LookInterchange -> LoopInterchange. Please adjust before committing.

llvm/lib/Transforms/Scalar/LoopInterchange.cpp
666

thanks for the comment. The bit about walking users of constants seems a bit verbose and I think it would be better to have this rule documented in a single place, rather than adding it at all the places that are going to be updated. I'd remove it here.

This revision is now accepted and ready to land.Jan 20 2021, 7:51 AM
dexonsmith accepted this revision.Jan 20 2021, 12:19 PM

LGTM too; agreed the comment could be trimmed down too, but I think it's important to have something about that in the commit message.

@willir, I can commit this for you once you update the patch.

willir retitled this revision from Do not traverse ConstantData use-list in LookInterchange to Do not traverse ConstantData use-list in LoopInterchange.Jan 20 2021, 6:41 PM
willir edited the summary of this revision. (Show Details)
willir updated this revision to Diff 318086.Jan 20 2021, 6:45 PM
willir edited the summary of this revision. (Show Details)

Trim down the comment

Thanks for the review. I hope the current comment looks better.

Here is my git info:

  • GIT_AUTHOR_NAME="Anton Rapetov"
  • GIT_AUTHOR_EMAIL="willir29@gmail.com"

Pushed in bfec9148a042c8fd6093ae0d54c784211a295c6c with some minor tweaks to the comments and commit message.