This is an archive of the discontinued LLVM Phabricator instance.

[DAG] FoldConstantArithmetic - add initial support for undef elements in bitcasted binop constant folding
ClosedPublic

Authored by RKSimon on Jul 31 2022, 6:21 AM.

Details

Summary

FoldConstantArithmetic can fold constant vectors hidden behind bitcasts (e.g. vXi64 -> v2Xi32 on 32-bit platforms), but currently bails if either vector contains undef elements. These undefs can often occur due to SimplifyDemandedBits/VectorElts calls recognising that the upper bits are often unnecessary (e.g. funnel-shift/rotate implicit-modulo and AND masks).

This patch adds a basic 'FoldValueWithUndef' handler that will attempt to constant fold if one or both of the ops are undef - so far this just handles the AND and MUL cases where we always fold to zero.

The RISCV codegen increase is interesting - it looks like the BUILD_VECTOR lowering was loading a constant pool entry but now (with all elements defined constant) it materializes it instead?

Diff Detail

Event Timeline

RKSimon created this revision.Jul 31 2022, 6:21 AM
RKSimon requested review of this revision.Jul 31 2022, 6:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 31 2022, 6:21 AM
tschuett added inline comments.
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5602

Do you need to hide this behind a lambda or could you pull it into a function because it is getting more powerful over time?

RKSimon added inline comments.Jul 31 2022, 7:04 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5602

No, it could be exposed like FoldValue() is - that is currently a static helper above FoldConstantArithmetic. We could move it into TLI later if it'd help target nodes.

xiangzhangllvm added inline comments.Jul 31 2022, 6:37 PM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5601

Maybe better to add a small case here, some like
"old nodes" --> "new nodes"

5607

Do we need to make sure the C1 C2 has no other uses ?

RKSimon added inline comments.Aug 1 2022, 4:05 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5607

I don't think we have any one use limits on constant folding?

RKSimon updated this revision to Diff 448988.Aug 1 2022, 4:21 AM

Pull out FoldValueWithUndef lambda to match FoldValue helper

RKSimon added inline comments.Aug 1 2022, 8:28 AM
llvm/test/CodeGen/RISCV/srem-seteq-illegal-types.ll
667

@craig.topper I'm assuming this amount of code isn't an improvement just because we avoided a load?

xiangzhangllvm added inline comments.Aug 1 2022, 6:04 PM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5607

Yes, sorry my mistake at first.
take following case for example:

A = and(x, undef)
C = A op B

If we directly let "A = 0" (I think A=undef before), seems it may break the undef propagation in other use. for example C when "op" can do undef propagation (but not constant zero propagation).

RKSimon added inline comments.Aug 2 2022, 2:13 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5607
A = and(x, undef)
C = A op B

A can't (or shouldn't) propagate undef: https://alive2.llvm.org/ce/z/0EB60y - we almost certainly have cases of that in code, which need addressing

xiangzhangllvm added inline comments.Aug 2 2022, 6:00 PM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5607

Yes, thanks for your cases! it really not propagate.

But sorry for I am still some puzzle here:
Do you know why we can't (or shouldn't) propagate undef ?

To me, I can't see the real difference between

A = undef

and

A = and(x, undef)
efriedma added inline comments.Aug 3 2022, 11:30 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5607

and(0, undef) is not undef; it's zero. By contrast, and(0, poison) is poison. This is the fundamental difference between undef and poison. See https://llvm.org/docs/LangRef.html#undefined-values .

xiangzhangllvm accepted this revision.Aug 3 2022, 5:43 PM

LGTM

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5607

Very clear now, thanks very much for your explain!

This revision is now accepted and ready to land.Aug 3 2022, 5:43 PM
This revision was landed with ongoing or failed builds.Aug 8 2022, 3:54 AM
This revision was automatically updated to reflect the committed changes.