This is an archive of the discontinued LLVM Phabricator instance.

[VectorCombine] Support AND/UREM indices that require freezing.
ClosedPublic

Authored by fhahn on Aug 5 2021, 9:39 AM.

Details

Summary

38b098be6605 limited scalarization to indices that are known non-poison.
For certain patterns that restrict the range of an index, we can insert
a freeze of the original value, to prevent propagation of poison.

Diff Detail

Event Timeline

fhahn created this revision.Aug 5 2021, 9:39 AM
fhahn requested review of this revision.Aug 5 2021, 9:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 5 2021, 9:39 AM
spatel added inline comments.
llvm/lib/Transforms/Vectorize/VectorCombine.cpp
779

Other than the name, this looks like it could be a generic freeze utility. Should we put it in some header for other potential callers? cc @aqjune @hyeongyukim

812

Use InsertPointGuard so caller doesn't have to manually reset it.

861

stray punctuation

aqjune added inline comments.Aug 8 2021, 5:39 PM
llvm/lib/Transforms/Vectorize/VectorCombine.cpp
779

Not sure whether this class can be reused, but I agree that having a header containing utilities for dealing with freeze is a good idea (or probably reuse Local.h?).
It's because we might want to keep freezes in a specific pattern for easier optimization. Currently, it is InstCombine's work to keep freeze-including expressions in a specific pattern, but it is kind of hidden.
In the case of LoopUnswitch, naively freezing the branch condition caused many assembly diffs.
To resolve this regression, we are experimenting with (1) pushing freeze into icmp arguments (which could resolve assembly diffs a lot: https://reviews.llvm.org/D106041#2922020) (2) replacing all uses of a branch condition with the frozen one.
Once the scheme is set, I'd like to have FreezeBrCond(BranchInst) that performs what LoopUnswitch is supposed to do.

fhahn updated this revision to Diff 366352.Aug 13 2021, 1:57 PM

Addressed comments, thanks: 1) Use insert point guard and 2) remove stray ;

fhahn marked 2 inline comments as done.Aug 13 2021, 1:58 PM
fhahn added inline comments.
llvm/lib/Transforms/Vectorize/VectorCombine.cpp
779

Other than the name, this looks like it could be a generic freeze utility. Should we put it in some header for other potential callers? cc @aqjune @hyeongyukim

I tried to keep things as general as possible here. But for now it serves a very specific use case, so maybe it would make sense to wait with moving this to a header until there are similar use cases?

lebedev.ri added inline comments.Aug 13 2021, 2:05 PM
llvm/lib/Transforms/Vectorize/VectorCombine.cpp
808–810

I think you want to assert that User is an user of ToFreeze.

810
810

Why do you have to go through all the uses of ToFreeze, even though you only want to fix User?
Can't you just go through the operands of User?

nikic added a subscriber: nikic.Aug 13 2021, 2:20 PM

Is these some specific use case for handling this special case?

fhahn updated this revision to Diff 366610.Aug 16 2021, 6:18 AM

Added assertion as suggested and also just update operands of UserI, thanks!

Is these some specific use case for handling this special case?

LLVM doesn't do a great job when lowering operations on largish vectors in general, e.g. originating from uses of extended vectors in C/C++ or other frontends. The patch in particular improves the lowering of code like below

using vec_ty = float __attribute__((ext_vector_type(32)));

void foo(vec_ty *ptr, int idx) {
  (*ptr)[idx&31] = 0.0;
}

https://godbolt.org/z/cq97c4KG5

fhahn marked 3 inline comments as done.Aug 16 2021, 6:20 AM
fhahn added inline comments.
llvm/lib/Transforms/Vectorize/VectorCombine.cpp
810

Can't you just go through the operands of User?

For the current version of the API yes! Updated, thanks!

lebedev.ri added inline comments.Aug 16 2021, 6:27 AM
llvm/lib/Transforms/Vectorize/VectorCombine.cpp
790

Assertion message

811

is_contained(ToFreeze->users(), &UserI)?

fhahn updated this revision to Diff 366617.Aug 16 2021, 7:02 AM
fhahn marked an inline comment as done.

Use is_contained and add assertion message, thanks!

lebedev.ri accepted this revision.Aug 16 2021, 7:26 AM

Seems fine to me, thanks.

llvm/lib/Transforms/Vectorize/VectorCombine.cpp
785–786

maybe

816
843–844

@nlopes I wanted to check what can we do for potentially OOB insertions,
and i guess this is an alive2 problem:
https://alive2.llvm.org/ce/z/VhUXF-
https://alive2.llvm.org/ce/z/qHk9Wv
?

989–991

This should either contain a FIXME comment, or a comment saying that here freeze won't help.

This revision is now accepted and ready to land.Aug 16 2021, 7:26 AM
nlopes added inline comments.Aug 16 2021, 9:40 AM
llvm/lib/Transforms/Vectorize/VectorCombine.cpp
843–844

Both tests look correct to me.
The 2nd one is funny because the 'and' doesn't do anything. A proper masking should be 'and 7'.

fhahn updated this revision to Diff 372189.Sep 13 2021, 2:32 AM
fhahn marked 9 inline comments as done.

Address outstanding comments. I'll land this version in a bit.

llvm/lib/Transforms/Vectorize/VectorCombine.cpp
785–786

I think for constructors it is quite common to have names matching the directly initialized fields for arguments. To make things consistent for the constructor, I renamed to S argument to Status.