This is an archive of the discontinued LLVM Phabricator instance.

[ISEL] Canonicalize STEP_VECTOR to LHS if RHS is a splat.
ClosedPublic

Authored by sdesmalen on Jan 28 2022, 5:11 AM.

Details

Summary

This helps recognise patterns where we're trying to match STEP_VECTOR
patterns to INDEX instructions that take a GPR for the Start/Step.

The reason for canonicalising this operation to the LHS is
because it will already be canonicalised to the LHS if the RHS
is a constant splat vector.

Diff Detail

Event Timeline

sdesmalen created this revision.Jan 28 2022, 5:11 AM
sdesmalen requested review of this revision.Jan 28 2022, 5:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 28 2022, 5:11 AM

This change is inspired by D117900 where it tries to fold some linear transform of step_vector into the gather/scatter addressing modes.

RKSimon added inline comments.Jan 28 2022, 7:21 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5662

I expect we're going to need this for a lot of the DAG combines as well - is it worth adding a helper wrapper for isConstantIntBuildVectorOrConstantInt/isConstantFPBuildVectorOrConstantFP with SPLAT_VECTOR and STEP_VECTOR as well?

sdesmalen added inline comments.Jan 31 2022, 4:54 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5662

I'd rather avoid creating another isConstant.... method, because we already have so many of them and despite the name, isConstantIntBuildVectorOrConstantInt already supports SPLAT_VECTOR (it would actually be good to clean that up a bit at some point).

For this patch, it may be worth moving this commutativity code as a whole to a separate function (e.g. swapCommutativeOperands), would that help?

sdesmalen updated this revision to Diff 404507.Jan 31 2022, 7:00 AM

Moved some of the logic to a separate function (trySwapOpsCommutativeBinop).

paulwalker-arm added inline comments.Feb 1 2022, 3:23 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5634

This comment is now out of date. I'd just remove the comment given you've moved the code into a function with CommutativeBinop in the name.

5648

What about changing this to binop(splat(x), not_splat(y)) -> binop(not_splat(y), splat(x))? That would solve your problem and be truly canonical.

RKSimon added inline comments.Feb 1 2022, 4:02 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5632

Sorry for bikeshedding - but maybe canonicalizeCommutativeBinop is a better name?

sdesmalen added inline comments.Feb 1 2022, 4:14 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5648

Unfortunately that has other side-effects, e.g. for:

getelementptr i8, i8* %base, <vscale x 2 x i32> %offsets

This would swap the splat(%base) and %offsets so that getUniformBase no longer recognises the %base as the scalar base pointer.

I wonder if this means the proposed canonicalisation is too specific, and therefore not worth it?

paulwalker-arm added inline comments.Feb 1 2022, 4:24 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5648

Not sure I understand what you're saying here. Doesn't this just mean there's code that needs to be changed to check for the new canonical form?

Just wanted to say that I'm not suggesting this patch implements the binop(splat(x), not_splat(y)) -> binop(not_splat(y), splat(x)) idea in one go. I just think that if we agree that it's ultimately the better canonical form then I'm happy to accept this patch as a step in the right direction to unblock your current work and we can fix the rest up later (well sooner rather than later :) )

sdesmalen updated this revision to Diff 404906.Feb 1 2022, 6:15 AM
  • Renamed trySwapOpsCommutativeBinop->canonicalizeCommutativeBinop.
  • Removed stale comment.

Just wanted to say that I'm not suggesting this patch implements the binop(splat(x), not_splat(y)) -> binop(not_splat(y), splat(x)) idea in one go. I just think that if we agree that it's ultimately the better canonical form then I'm happy to accept this patch as a step in the right direction to unblock your current work and we can fix the rest up later (well sooner rather than later :) )

Okay that makes sense, thanks for clarifying!

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5632

That's indeed a much better name, thank you :)

paulwalker-arm accepted this revision.Feb 1 2022, 6:19 AM
This revision is now accepted and ready to land.Feb 1 2022, 6:19 AM