This is an archive of the discontinued LLVM Phabricator instance.

GlobalISel/Utils: Refactor integer/float constant match functions
ClosedPublic

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

Details

Summary

Rework getConstantstVRegValWithLookThrough in order to make it clear if we
are matching integer/float constant only or any constant(default).
Add helper functions that get DefVReg and APInt/APFloat from constant instr
getIConstantVRegValWithLookThrough: integer constant, only G_CONSTANT
getFConstantVRegValWithLookThrough: float constant, only G_FCONSTANT
getAnyConstantVRegValWithLookThrough: either G_CONSTANT or G_FCONSTANT

Rename getConstantVRegVal and getConstantVRegSExtVal to getIConstantVRegVal
and getIConstantVRegSExtVal. These now only match G_CONSTANT as described
in comment.

Relevant matchers now return both DefVReg and APInt/APFloat.

Replace existing uses of getConstantstVRegValWithLookThrough and
getConstantVRegVal with new helper functions. Any constant match is
only required in:
ConstantFoldBinOp: for constant argument that was bit-cast of float to int
getAArch64VectorSplat: AArch64::G_DUP operands can be any constant
amdgpu select for G_BUILD_VECTOR_TRUNC: operands can be any constant

In other places use integer only constant match.

Diff Detail

Event Timeline

Petar.Avramovic requested review of this revision.Jun 16 2021, 11:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 16 2021, 11:52 AM

I'm not sure if I like having two toggles here because now people could set HandleFConstants == false and HandleIConstants == false. :/

I think that it would be nice if we could expose helper functions like so:

  • getIConstantVRegValWithLookThrough => integer constant case only
  • getFConstantVRegValWithLookThrough => fp constant case only
  • getAnyConstantVRegValWithLookThrough => any constant

And then have getConstantVRegValWithLookThrough be inaccessible outside of Utils.

This would be a bit more invasive of a change, but I think it would be a readability win overall.

Petar.Avramovic retitled this revision from GlobalISel/Utils: Allow constant match to not match integer constants to GlobalISel/Utils: Refactor integer/float constant match functions.
Petar.Avramovic edited the summary of this revision. (Show Details)

Try to have more strict match functions. Match G or G_F Constant only when there is a reason for it.

Petar.Avramovic added inline comments.Jul 7 2021, 8:00 AM
llvm/lib/CodeGen/GlobalISel/Utils.cpp
354–413

I thought to make this public. There are many opcode checks like this, most often in asserts.
Do you find it useful?

What is the motivation behind adding G to the function names? Given that GlobalISel uses G_* as a prefix for all opcode names, it seems kind of redundant to me.

llvm/lib/CodeGen/GlobalISel/Utils.cpp
354–413

I don't know if it really improves on the existing opcode checks all too much.

(This would also probably need a check for nullptr or an assert?)

363

Check for nullptr or assert?

arsenm added a comment.Jul 7 2021, 3:33 PM

Do we even really need G_FCONSTANT? I've always thought having a separate node/opcode for these was just pointless complexity that can just as well be tracked with an APInt

llvm/lib/CodeGen/GlobalISel/Utils.cpp
354–413

I think a function to check a single case is just noise

354–413

Or just use a reference, shouldn't handle null

I think it would be easier to think about removing it if we had FP types.

I think it helps with handling things like selects/phis in AArch64 right now.

arsenm added inline comments.Jul 7 2021, 3:43 PM
llvm/include/llvm/CodeGen/GlobalISel/Utils.h
181–182

This name is way less readable

What is the motivation behind adding G to the function names?

GlobalISel does not mention IConstant it is just G_CONSTANT but has FConstant (G_FCONSTANT). It thought that GCst in name associates better with G_CONSTANT instruction (which we know to be integer constant). Matchers use this naming style m_GAdd - integer add vs m_GFAdd - float.
Only Constant is ambiguous and is good name for generic helper function.

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

This guards nullptr from entering into isGConstant and other opcodes checks.

328

No nullptrs here either.

Petar.Avramovic edited the summary of this revision. (Show Details)

Rebase and ping.

Rebase and ping.

Rebase and ping.

Rebase and ping.
Brief summary:
Add support for g_fconstant matching and option to return APFloat.
Rename getConstantVRegValWithLookThrough into get{I|F|Any}ConstantVRegValWithLookThrough. Remove HandleFConstant argument, use function with appropriate name instead.
Add matchers for Reg and APInt/Float: g_constant m_GCst(&ValueAndVReg) and g_fconstant m_GFCst(&FPValueAndVReg)

foad added a comment.Sep 16 2021, 3:10 AM

@paquette do you have further comments on this? We have various (approved) patches waiting on this one.

This revision is now accepted and ready to land.Sep 16 2021, 10:03 AM
This revision was landed with ongoing or failed builds.Sep 17 2021, 2:38 AM
This revision was automatically updated to reflect the committed changes.