This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner][RISCV] Make foldBinOpIntoSelect work correctly with opaque constants.
ClosedPublic

Authored by craig.topper on Oct 21 2022, 10:52 AM.

Details

Summary

The CanFoldNonConst doesn't work correctly with opaque constants
because getNode won't constant fold constants if one is opaque. Even
if the operation is AND/OR. This can lead to infinite loops.

This patch does the folding manually in the DAGCombine. Alternatively,
we could improve getNode but that seemed likely to have bigger impact
and possibly increase compile time for the additional checks. We wouldn't
want to directly constant fold because we need to preserve the opaque flag.

Still need to add the test, but wanted to get a check that this was
the right direction.

Fixes PR58511.

Diff Detail

Event Timeline

craig.topper created this revision.Oct 21 2022, 10:52 AM
craig.topper requested review of this revision.Oct 21 2022, 10:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 21 2022, 10:52 AM
craig.topper retitled this revision from [DAGCombiner][WIP] Make foldBinOpIntoSelect work correctly with opaque constants. to [DAGCombiner][RISCV] Make foldBinOpIntoSelect work correctly with opaque constants..Oct 21 2022, 11:02 AM
spatel accepted this revision.Oct 21 2022, 12:58 PM

LGTM

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
2288

Add a note here or to the text above to explain that we don't re-use an opaque 0/1 because that could inf-loop.

This revision is now accepted and ready to land.Oct 21 2022, 12:58 PM
craig.topper added inline comments.Oct 21 2022, 1:26 PM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
2288

The 0/-1 weren’t opaque. I was just being paranoid about using 0/-1 that might be mixed with undef. Do I need to worry about that here?

spatel added inline comments.Oct 21 2022, 2:07 PM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
2288

Ah, I misunderstood how this was looping.
Still worth a note that we're creating 0/-1 directly here rather than relying on getNode because CBO could be opaque?

craig.topper added inline comments.Oct 22 2022, 6:32 PM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
2288

For posterity, we loop between these two states

Combining: t15: i32 = or t13, OpaqueConstant:i32<2048>                           
Creating new node: t23: i32 = or Constant:i32<-1>, OpaqueConstant:i32<2048>      
Creating new node: t24: i32 = or Constant:i32<0>, OpaqueConstant:i32<2048>       
Creating new node: t25: i32 = select t7, t23, t24                                
 ... into: t25: i32 = select t7, t23, t24                                        
                                                                                 
Combining: t19: i64 = any_extend t25                                             
                                                                                 
Combining: t25: i32 = select t7, t23, t24                                        
Creating new node: t26: i32 = select t7, Constant:i32<-1>, Constant:i32<0>       
Creating new node: t27: i32 = or t26, OpaqueConstant:i32<2048>                   
 ... into: t27: i32 = or t26, OpaqueConstant:i32<2048>
This revision was landed with ongoing or failed builds.Oct 22 2022, 7:14 PM
This revision was automatically updated to reflect the committed changes.