This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner] Update most ADD combines to support general vector combines
ClosedPublic

Authored by RKSimon on Oct 7 2016, 11:51 AM.

Details

Summary

Add a number of helper functions to match scalar or vector equivalent constant/splat values to allow most of the combine patterns to be used by vectors.

Diff Detail

Repository
rL LLVM

Event Timeline

RKSimon updated this revision to Diff 73961.Oct 7 2016, 11:51 AM
RKSimon retitled this revision from to [DAGCombiner] Update most ADD combines to support general vector combines.
RKSimon updated this object.
RKSimon set the repository for this revision to rL LLVM.
RKSimon added a subscriber: llvm-commits.
mkuper added inline comments.Oct 7 2016, 12:38 PM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
829

What if we have a BUILD_VECTOR of constants where one of the constants going into it is opaque?

RKSimon added inline comments.Oct 7 2016, 3:36 PM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
829

I see what you're saying. For the current cases it won't have much effect - some of the combines would switch ops around but the expected constant folding will just fail. I'll change isNonOpaqueConstantOrConstantVector to check for opaques in the build vector, and make a separate isConstantOrConstantVector helper as well which we can use in a couple of places. Thorough tests of opaque vector elements might be tricky though.

RKSimon updated this revision to Diff 74050.Oct 8 2016, 3:03 PM

Updated isNonOpaqueConstantOrConstantVector to isNonOpaqueConstantOrNonOpaqueConstantVector to check that no vector elements are opaque constants. Also added isConstantOrConstantVector helper that doesn't care about opaque. Both of these allow undef vector elements.

Updated the helpers to not allow implicit truncation in build vectors - this will be necessary for the reuse of these helpers in other combines (e.g. SUB and MUL). This isn't necessary for the isNullConstantOrNullSplatConstant case.

mkuper added inline comments.Oct 10 2016, 12:14 PM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
858

Maybe merge isConstantOrConstantVector and isNonOpaqueConstantOrNonOpaqueConstantVector and have a nonOpaque parameter?

RKSimon added inline comments.Oct 10 2016, 12:29 PM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
858

Will do - it'll help reduce the length of those function names!

RKSimon updated this revision to Diff 74166.Oct 10 2016, 1:07 PM

Added NoOpaques optional argument to isConstantOrConstantVector and removed isNonOpaqueConstantOrNonOpaqueConstantVector helper

mkuper added inline comments.Oct 10 2016, 1:16 PM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
831

"return !(NoOpaques && Const->isOpaque())" ?
(assuming I got the logic right :-) )

zvi added a subscriber: zvi.Oct 10 2016, 10:18 PM
RKSimon updated this revision to Diff 74201.Oct 11 2016, 3:02 AM

Fixed opaque typo

mkuper accepted this revision.Oct 11 2016, 9:07 AM
mkuper edited edge metadata.

LGTM

Although I'm curious - do you see this in real code?
To the best of my understanding, the major goal of DAG-level constant folding is to get rid of constants that weren't visible on the IR level. This happens with scalars quite a lot (e.g. because of GEP lowering), but I'm not sure I understand where "new" vector constants come from.

This revision is now accepted and ready to land.Oct 11 2016, 9:07 AM

LGTM

Although I'm curious - do you see this in real code?
To the best of my understanding, the major goal of DAG-level constant folding is to get rid of constants that weren't visible on the IR level. This happens with scalars quite a lot (e.g. because of GEP lowering), but I'm not sure I understand where "new" vector constants come from.

Thanks Michael, the only pattern from visitADD I've seen in the wild is "(a+b) -> (a|b)", but this will hopefully be the first stage in making most of the DAGCombiner combines properly support vector patterns when they do. Both visitSUB and visitAND/visitOR/visitXOR have a lot more cases that do occur but I wanted to start with a relatively easy one! These along with better vector support for computeKnownBits (et al.) should have a greater (accumulative) effect on vector IR.

This revision was automatically updated to reflect the committed changes.

Updated isNonOpaqueConstantOrConstantVector to isNonOpaqueConstantOrNonOpaqueConstantVector to check that no vector elements are opaque constants. Also added isConstantOrConstantVector helper that doesn't care about opaque. Both of these allow undef vector elements.

Updated the helpers to not allow implicit truncation in build vectors - this will be necessary for the reuse of these helpers in other combines (e.g. SUB and MUL). This isn't necessary for the isNullConstantOrNullSplatConstant case.

Hi @RKSimon

I know this CR is quite old, I am trying to remove the "not allow implicit truncation" restriction for one optimization. I am somewhat new to this part of the compiler, can you please explain to me why this is necessary for SUB and MUL combines. An example would be great. Thanks !

Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJun 14 2022, 9:34 AM
Herald added subscribers: jsji, pengfei. · View Herald Transcript

Updated isNonOpaqueConstantOrConstantVector to isNonOpaqueConstantOrNonOpaqueConstantVector to check that no vector elements are opaque constants. Also added isConstantOrConstantVector helper that doesn't care about opaque. Both of these allow undef vector elements.

Updated the helpers to not allow implicit truncation in build vectors - this will be necessary for the reuse of these helpers in other combines (e.g. SUB and MUL). This isn't necessary for the isNullConstantOrNullSplatConstant case.

Hi @RKSimon

I know this CR is quite old, I am trying to remove the "not allow implicit truncation" restriction for one optimization. I am somewhat new to this part of the compiler, can you please explain to me why this is necessary for SUB and MUL combines. An example would be great. Thanks !

I can't remember tbh - best way to find out would be for you to remove the restriction and run check-llvm-codegen to see what changes/regresses/asserts/explodes....

Updated isNonOpaqueConstantOrConstantVector to isNonOpaqueConstantOrNonOpaqueConstantVector to check that no vector elements are opaque constants. Also added isConstantOrConstantVector helper that doesn't care about opaque. Both of these allow undef vector elements.

Updated the helpers to not allow implicit truncation in build vectors - this will be necessary for the reuse of these helpers in other combines (e.g. SUB and MUL). This isn't necessary for the isNullConstantOrNullSplatConstant case.

Hi @RKSimon

I know this CR is quite old, I am trying to remove the "not allow implicit truncation" restriction for one optimization. I am somewhat new to this part of the compiler, can you please explain to me why this is necessary for SUB and MUL combines. An example would be great. Thanks !

I can't remember tbh - best way to find out would be for you to remove the restriction and run check-llvm-codegen to see what changes/regresses/asserts/explodes....

so, I've ran check-llvm-codegen on all available targets and things are fine.