This is an archive of the discontinued LLVM Phabricator instance.

GlobalISel/Utils: Refactor constant splat match functions
ClosedPublic

Authored by Petar.Avramovic on Jun 16 2021, 11:53 AM.

Details

Summary

Add generic helper function that matches constant splat. It also has an option
to match constant splat with undef (some elements can be undef but not all).
Add util function and matcher for G_FCONSTANT splat.

Diff Detail

Event Timeline

Petar.Avramovic requested review of this revision.Jun 16 2021, 11:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 16 2021, 11:53 AM
foad added inline comments.Jun 17 2021, 2:07 AM
llvm/include/llvm/CodeGen/GlobalISel/Utils.h
362

Would a return type of Optional<ValueAndVReg> (like getConstantVRegValWithLookThrough) be more useful? Actually I'm not sure if returning the register is useful, maybe just Optional<APInt>?

llvm/lib/CodeGen/GlobalISel/Utils.cpp
1022

Isn't there an existing utility you can call to do this?

llvm/include/llvm/CodeGen/GlobalISel/Utils.h
362

ValueAndVReg would be fine but in later patch APFloat would be useful. I did it with reg so that once it is matched caller knows if it matched int or float constant and can get APInt or APFloat and check what it needs.

llvm/lib/CodeGen/GlobalISel/Utils.cpp
1022

There is code that does similar thing with look through in a few places but not a utility function.

paquette added inline comments.Jun 28 2021, 9:37 AM
llvm/lib/CodeGen/GlobalISel/Utils.cpp
1019

This looks really similar to getBuildVectorConstantSplat. Would it make sense to improve that function instead?

1022

getDefIgnoringCopies doesn't work here?

Petar.Avramovic retitled this revision from GlobalISel/Utils: Add util function and matcher for constant splat to GlobalISel/Utils: Refactor constant splat match functions.
Petar.Avramovic edited the summary of this revision. (Show Details)

Make use of the more generic helper function in existing splat matcher functions.

paquette added inline comments.Jul 7 2021, 1:27 PM
llvm/lib/CodeGen/GlobalISel/Utils.cpp
374

This will need a nullptr check.

I'm not sure about how I feel about adding functions for generic opcode checks, since I think it could end up being confusing if we don't add them for every opcode. :/

That being said, these do look nicer than using MI->getOpcode() everywhere.

(Maybe someone else can give a stronger opinion here.)

aemerson added inline comments.Jul 7 2021, 4:12 PM
llvm/lib/CodeGen/GlobalISel/Utils.cpp
374

I don't think alone this helper has much benefit. I think it would be nice to have some wrapper classes around MachineInstr to do some of that while also proving some abstractions over operand accessors etc. I haven't got around to it but was on my todo list.

Petar.Avramovic added inline comments.Jul 8 2021, 3:02 AM
llvm/lib/CodeGen/GlobalISel/Utils.cpp
374

I meant to add it for a number of opcodes (~50).
I did grep of '==' and '!=' compares with 'TargetOpcode::G_' Here are some stats:
there are 44 different opcodes with 1 or 2 mentions, in total 58 compares.
there are 49 different opcodes with more that 3 mentions (G_CONSTANT has the most; 30). In total 450 compares
190 are asserts.
Would result in nicer code formatting.
I will directly check opcode for this patch.

374

I like this better than util function.

Remove isImplicitDef.

arsenm added inline comments.Aug 6 2021, 6:15 AM
llvm/lib/CodeGen/GlobalISel/Utils.cpp
948

Use mi_match?

957

Typo contant

958
  • instead of ->Value?
Petar.Avramovic added inline comments.Aug 6 2021, 9:13 AM
llvm/lib/CodeGen/GlobalISel/Utils.cpp
948

There is nothing to match for G_IMPLICIT_DEF only opcode, but there is this new GenericMachineInstr wrapper around MachineInstr which does opcode checks.

958

I dind't get this. What should be the change again?

foad added a comment.Sep 17 2021, 3:35 AM

Generally looks OK to me. Any more comments @paquette?

llvm/lib/CodeGen/GlobalISel/Utils.cpp
988

What does "Allows nan splat matching" mean?

llvm/lib/CodeGen/GlobalISel/Utils.cpp
988

Splat of NaN value, if we get value as APFloat all comparisons with it are false and we cant check for splat. We check for splat using APInt and check if constant is g_fconstant with getFConstantVRegValWithLookThrough and get APFloat.

foad added inline comments.Sep 17 2021, 5:08 AM
llvm/lib/CodeGen/GlobalISel/Utils.cpp
988

OK, I just assumed that any floating point splat detection code would use bitwiseIsEqual instead of operator== anyway.

Remove comment about constant splat with NaN value. Seems unnecessary to mention it since we match all constants.

paquette added inline comments.Sep 17 2021, 10:34 AM
llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
111

Could this function be a one-liner?

return getFConstantSplat(Reg, MRI) || getFConstantVRegValWithLookThrough(Reg, MRI);

llvm/lib/CodeGen/GlobalISel/Utils.cpp
933

check that this is not nullptr?

939

Maybe use

for (MachineOperand &Op : MI->uses()) {
   ...

Addressed review comments.

This revision is now accepted and ready to land.Sep 20 2021, 9:44 AM
This revision was landed with ongoing or failed builds.Sep 21 2021, 3:10 AM
This revision was automatically updated to reflect the committed changes.