This is an archive of the discontinued LLVM Phabricator instance.

[ConstraintElim] Queue facts and checks directly.
ClosedPublic

Authored by fhahn on Nov 21 2022, 11:36 AM.

Details

Summary

This allows interleaving facts and checks in a single block. In
particular this enables using facts from assumes for conditions in the
same block that come after the assume.

This could be extended to only try to simplify checks at the point where
a condition is used.

Diff Detail

Event Timeline

fhahn created this revision.Nov 21 2022, 11:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 21 2022, 11:36 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
fhahn requested review of this revision.Nov 21 2022, 11:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 21 2022, 11:36 AM
nikic accepted this revision.Dec 5 2022, 7:38 AM

LGTM. A question I had is why we use this kind of upfront collection + sort approach at all. Wouldn't it be possible to do essentially the same in a single walk over the dominator tree?

This revision is now accepted and ready to land.Dec 5 2022, 7:38 AM
This revision was landed with ongoing or failed builds.Dec 5 2022, 8:44 AM
This revision was automatically updated to reflect the committed changes.
fhahn added a comment.Dec 5 2022, 9:04 AM

LGTM. A question I had is why we use this kind of upfront collection + sort approach at all. Wouldn't it be possible to do essentially the same in a single walk over the dominator tree?

Thanks! IIRC the main reason was to t make it easier to inject additional conditions for which no direct IR instruction exists, but that's not used in the version on main at the moment. I haven't thought a lot about what a single DT walk would look like, but a straight forward walk would probably be a recursive implementation and the worklist approach seems slightly simpler compared to that. I could look into the DT walk approach a bit more, if you think there's a significant benefit.

uabelho added a subscriber: uabelho.Dec 6 2022, 4:33 AM

Hi,
The following starts crashing with this patch:

opt -passes=constraint-elimination bbi-76661.ll -o /dev/null

I get

opt: ../lib/IR/Instruction.cpp:124: bool llvm::Instruction::comesBefore(const llvm::Instruction *) const: Assertion `Parent == Other->Parent && "cross-BB instruction order comparison"' failed.

bjope added a subscriber: bjope.Dec 6 2022, 5:04 AM
fhahn added a comment.Dec 6 2022, 5:29 AM

Hi,
The following starts crashing with this patch:

opt -passes=constraint-elimination bbi-76661.ll -o /dev/null

I get

opt: ../lib/IR/Instruction.cpp:124: bool llvm::Instruction::comesBefore(const llvm::Instruction *) const: Assertion `Parent == Other->Parent && "cross-BB instruction order comparison"' failed.

Thanks for the report. Is it possible this is over-reduced? I cannot reproduce it locally or on godbolt: https://llvm.godbolt.org/z/T8f1q33hq

bjope added a comment.Dec 6 2022, 6:54 AM

! In D138452#3974119, @fhahn wrote:

Thanks for the report. Is it possible this is over-reduced? I cannot reproduce it locally or on godbolt: https://llvm.godbolt.org/z/T8f1q33hq

uabelho seem to be offline right now, but I'll try to verify if the reproducer works for me or not. (Or I'll try to make a new reproducer, because I can see the test case that failed.) It might take me awhile though, but I'm on to it.

bjope added a comment.Dec 6 2022, 6:59 AM

Ok, here is a godbolt repro: https://llvm.godbolt.org/z/98PvGaTe9

The difference is that the branch in the entry block now looks like:

br i1 %0, label %trap, label %1
fhahn added a comment.Dec 6 2022, 3:49 PM

Ok, here is a godbolt repro: https://llvm.godbolt.org/z/98PvGaTe9

The difference is that the branch in the entry block now looks like:

br i1 %0, label %trap, label %1

Thanks, interestingly this doesn't seem to crash on macOS, but I was able to reproduce on Linux. Should be fixed in 9eda78107c4d

bjope added a comment.Dec 7 2022, 3:53 AM

Ok, here is a godbolt repro: https://llvm.godbolt.org/z/98PvGaTe9

The difference is that the branch in the entry block now looks like:

br i1 %0, label %trap, label %1

Thanks, interestingly this doesn't seem to crash on macOS, but I was able to reproduce on Linux. Should be fixed in 9eda78107c4d

Great! Fix seem to work. I've cherrypicked the fix and re-enabled ConstraintElimination in our (downstream) fuzzy testing again. Time will tell if we find more problems :-)

Hi,
The following starts crashing with this patch:

opt -passes=constraint-elimination bbi-76661.ll -o /dev/null

I get

opt: ../lib/IR/Instruction.cpp:124: bool llvm::Instruction::comesBefore(const llvm::Instruction *) const: Assertion `Parent == Other->Parent && "cross-BB instruction order comparison"' failed.

Thanks for the report. Is it possible this is over-reduced? I cannot reproduce it locally or on godbolt: https://llvm.godbolt.org/z/T8f1q33hq

(I've been OOO a couple of days)
Strange. I tried again and the reproducer I uploaded indeed crashes for me on the commit on main that I tried on last week (83b3304dd2a).
Anyway, I see that @bjope fixed another repro so you could solve this.
Thanks both of you!