This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking][InstCombine] Add a new API to allow to ignore poison generating flags or metadatas when implying poison
ClosedPublic

Authored by StephenFan on Apr 27 2023, 8:30 PM.

Details

Summary

This patch add a new API impliesPoisonIgnoreFlagsOrMetadatas which is
the same as impliesPoison but ignoring poison generating flags or
metadatas in the process of implying poison and recording these ignored
instructions.

In InstCombineSelect, replacing impliesPoison with
impliesPoisonIgnoreFlagsOrMetadatas to allow more patterns like
select i1 %a, i1 %b, i1 false to be optimized to and/or instructions
by droping the poison generating flags or metadatas.

Diff Detail

Event Timeline

StephenFan created this revision.Apr 27 2023, 8:30 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 27 2023, 8:30 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
StephenFan requested review of this revision.Apr 27 2023, 8:30 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 27 2023, 8:30 PM
StephenFan added inline comments.Apr 27 2023, 8:54 PM
llvm/test/Transforms/InstCombine/ispow2.ll
917–921

I think this regression can be fixed If InstCombine matches this pattern.

goldstein.w.n added inline comments.Apr 27 2023, 11:34 PM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2922

Imo this should be split into a sepera

llvm/test/Transforms/InstCombine/ispow2.ll
917–921

What changed in the impl that caused this?

goldstein.w.n added inline comments.Apr 27 2023, 11:38 PM
llvm/lib/Analysis/ValueTracking.cpp
6560

true?

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2945

Will you ever not loop through instruction and do dropPoisonGenerating...? Maybe it should done by impliesPoisonIgnore... and if its not always with a bool to control it.

nikic added inline comments.Apr 28 2023, 2:17 AM
llvm/lib/Analysis/ValueTracking.cpp
6537

Hm, maybe just directly expose the API with the optional arg? Not sure we need one without the arg and one with it required... No strong opinion though.

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2945

If it's part of a larger transform, one may only want to drop the flags after additional conditions have been checked as well. Analysis utils generally aren't allowed to modify instructions.

2950

Why did this code get moved?

llvm/test/Transforms/InstCombine/ispow2.ll
917–921

I expect this got handled by the select value equivalence fold before. It's okay to regress for now because it was enabled as a side-effect of https://github.com/llvm/llvm-project/commit/bf23b4031eeabfccd46a25ce68414d45ae761304, and this patch basically undoes the effects of that one on this file.

goldstein.w.n added inline comments.Apr 28 2023, 8:07 AM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2922

err, ignore this. started writing it but forgot to delete.

goldstein.w.n added inline comments.Apr 30 2023, 10:03 PM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2945

then maybe an API in instcombine, or at least a static helper/lambda in the file.

StephenFan updated this revision to Diff 519871.May 5 2023, 8:17 AM

Move back codes that optimize select

goldstein.w.n added inline comments.May 12 2023, 3:10 PM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2932

Still in favor of putting this loop in some API function. It can be in InstCombine.

nikic added inline comments.May 14 2023, 5:51 AM
llvm/test/Transforms/InstCombine/and-fcmp.ll
393

Okay, now I understand why you moved the foldLogicOfFCmps() call. Please feel free to move it back, just add a comment to explain why we do it first.

Use lambda to drop posion flags or metadata and move back codes.

StephenFan added inline comments.May 14 2023, 10:21 PM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2945

I am sorry that I ignored your comment.

nikic accepted this revision.May 17 2023, 5:23 AM

LGTM

llvm/include/llvm/Analysis/ValueTracking.h
875

metadatas -> metadata

876

instructions ignored -> ignored instructions

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2926

SmallVectorImpl<Instruction *> & -> ArrayRef<Instruction *>

This revision is now accepted and ready to land.May 17 2023, 5:23 AM

update comments and change to arrayref

fhahn added a subscriber: fhahn.May 25 2023, 8:16 AM

I am seeing regressions due to dropping poison generating flags. For the test case below, we now drop the inbounds from %gep to select with and but it is not clear to me that replacing select with and outweighs the loss of poison generating flags?

The particular case I am looking at requires inbounds on the GEP to trigger further optimizations.

define i1 @test(ptr %base, i64 noundef %len, ptr %p2) {
bb:
  %gep = getelementptr inbounds i32, ptr %base, i64 %len
  %c.1 = icmp uge ptr %p2, %base
  %c.2 = icmp ult ptr %p2, %gep
  %select = select i1 %c.1, i1 %c.2, i1 false
  ret i1 %select
}
fhahn added a comment.May 26 2023, 1:01 PM

I added a phase-ordering test that shows a slight regression in 7c91d82ab912fae8bafc1137d4c9b17bcfb7eba7. It would be great if you could take a look. Perhaps this shouldn't be applied if poison-generating flags need to be dropped. Or only be dropped late in the pipeline.

nikic added a comment.May 26 2023, 1:05 PM

I would be okay with reverting the change. I don't think we had particularly strong motivation for it (iirc it was basically just that some selects were not converted, without actually blocking any following optimizations), so if it causes regressions in practice, it's probably best to not do it. Our optimizations for logical and/or are pretty good.

I would be okay with reverting the change. I don't think we had particularly strong motivation for it (iirc it was basically just that some selects were not converted, without actually blocking any following optimizations), so if it causes regressions in practice, it's probably best to not do it. Our optimizations for logical and/or are pretty good.

What about just handling poison generating metadata?

fhahn added a comment.May 29 2023, 7:46 AM

I would be okay with reverting the change. I don't think we had particularly strong motivation for it (iirc it was basically just that some selects were not converted, without actually blocking any following optimizations), so if it causes regressions in practice, it's probably best to not do it. Our optimizations for logical and/or are pretty good.

What about just handling poison generating metadata?

I went ahead and reverted the change for now. I am not sure, but I would assume dropping poison generating metadata could lead to similarly missed optimizations unfortunately.