Page MenuHomePhabricator

[VP,Integer,#2] ExpandVectorPredication pass
Needs ReviewPublic

Authored by simoll on Apr 15 2020, 6:38 AM.

Details

Summary

This patch implements expansion of llvm.vp.* intrinsics (https://llvm.org/docs/LangRef.html#vector-predication-intrinsics).

VP expansion is required for targets that do not implement VP code generation. Since expansion is controllable with TTI, targets can switch on the VP intrinsics they do support in their backend offering a smooth transition strategy for VP code generation (VE, RISC-V V, ARM SVE, AVX512, ..).

ExpandVectorPredication

The ExpandVectorPredication pass that folds the EVL parameter into the vector mask and/or lowers a VP intrinsics to a standard IR instruction.

Diff Detail

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes
craig.topper added inline comments.Jun 4 2020, 12:18 AM
llvm/include/llvm/IR/PredicatedInst.h
34 ↗(On Diff #268204)

Can this inherit from Instruction? Its a little surprising that it has Instruction in its name but has to be casted to Instruction.

llvm/lib/CodeGen/ExpandVectorPredication.cpp
99

This isn't safe for sdiv/srem is it? INT_MIN/-1 is overflow and will trap.

248

Do we need the nullptr here? I had to lookup what argument that is.

simoll planned changes to this revision.Jun 4 2020, 1:33 AM
simoll marked 12 inline comments as done.

@craig.topper Thanks for your comments! I'll update shortly.

llvm/include/llvm/IR/PredicatedInst.h
34 ↗(On Diff #268204)

Conceptually, PredicatedInstruction is a super class of Instruction: An Instruction is a PredicatedInstruction with the mask/evl parameters hard-wired to "all lanes true". That's why we can't make Instruction an ancestor of this one.
I'll clarify this with a comment.

llvm/lib/CodeGen/ExpandVectorPredication.cpp
71

This really should have its own enum.

77

Moved this function in to the CachingVPExpander class and added a lane bits cache there.

99

Good point. I'll fix this.

llvm/lib/IR/PredicatedInst.cpp
28 ↗(On Diff #268204)

Turns out we do. Fixed.

simoll updated this revision to Diff 268387.Jun 4 2020, 1:50 AM
simoll marked 4 inline comments as done.
simoll added a reviewer: craig.topper.

Addressed @craig.topper 's comments

simoll marked an inline comment as done.Jun 4 2020, 2:11 AM
simoll added inline comments.
llvm/lib/IR/IntrinsicInst.cpp
370–373

This unrelated change fixes a bug (ParamFactor does not make sense as a return value here and is not initialized!). I'll push a fix in a separate commit along with a test for this case.

simoll updated this revision to Diff 268436.Jun 4 2020, 5:29 AM

Fixed VPIntrinsic::canIgnoreVectorLength separately (a0dfdda4e5e8) and rebased onto that patch.

There are still more "auto" variables that are pointers in this patch.

llvm/include/llvm/IR/PredicatedInst.h
34 ↗(On Diff #268204)

But a VPIntrinsic is also an Instruction isn't it? At least the CallInst for the vp intrinsic is an Instruction.

simoll planned changes to this revision.Jun 5 2020, 1:17 AM
simoll marked an inline comment as done.

Will fix remaining auto* cases.

llvm/include/llvm/IR/PredicatedInst.h
34 ↗(On Diff #268204)

The whole reason for having the PredicatedInstruction class is that we want to add a new perspective on the IR where all instructions may be predicated (through a mask and evl). There are no VPIntrinsics in that perspective.

In the standard IR perspective, using the Instruction type, VPIntrinsics are just regular CallInsts calling intrinsics.

simoll updated this revision to Diff 268724.Jun 5 2020, 3:00 AM
  • Use auto*
  • Rebased
craig.topper added inline comments.Jun 5 2020, 3:13 PM
llvm/include/llvm/IR/PredicatedInst.h
34 ↗(On Diff #268204)

I was just wanting to get rid of the need to cast to Instruction in copyIRFlags and getParent and replaceOperation etc. Everything this class represents must be an Instruction whether its predicated or not. No type safety comes from this class inheriting from User instead of Instruction since a CallInst to a VP intrinsic is also an Instruction.

simoll planned changes to this revision.Jun 8 2020, 12:56 AM
simoll marked an inline comment as done.

Will rephrase this patch with VPIntrinsic instead of PredicatedInstruction since the purpose of the latter will only become clear with generalized pattern matching/lifting optimizations to VP.

llvm/include/llvm/IR/PredicatedInst.h
34 ↗(On Diff #268204)

That's exactly the point: a PredicatedInstruction cannot implicitly cast to an Instruction because that would be an implicit change of perspective.

simoll updated this revision to Diff 269123.Jun 8 2020, 1:54 AM
  • Removed PredicatedInstruction, which isn't strictly necessary for this patch.
  • Rebased.
simoll edited the summary of this revision. (Show Details)Jun 8 2020, 2:01 AM
khchen added a subscriber: khchen.Sep 5 2020, 12:09 AM
simoll updated this revision to Diff 296121.Oct 5 2020, 2:49 AM
  • adapted to get.active.mask changes (ult instead of ule)
  • rebased
simoll updated this revision to Diff 296136.Oct 5 2020, 3:17 AM

fixed for privatized ElementCount members.

Any more feedback for this patch?

Aside from my nitpicks, I was wondering if it's possible to add tests for the other strategies. Even if that involved a strategy that you could force on the command-line, since I know we don't have real TTIs just now. It would be nice to see that the pass behaves in the various ways it's meant to.

llvm/lib/CodeGen/ExpandVectorPredication.cpp
313

stray lower-case l in EVL (or should it be %evl?)

318

typo (reassess), also preferably with a capital R to match the style of the other comments

352

typo: strictly

365

Comment trails off here.

387

You shouldn't need the parentheses in these switch cases, also the formatting with the breaks looks a little off to me.

Linking this to the SelectionDAG patch: D91441

simoll planned changes to this revision.Nov 16 2020, 5:23 AM
simoll marked 3 inline comments as done.

Aside from my nitpicks, I was wondering if it's possible to add tests for the other strategies. Even if that involved a strategy that you could force on the command-line, since I know we don't have real TTIs just now. It would be nice to see that the pass behaves in the various ways it's meant to.

I like that idea! I'll add some cl::opts to override the target strategy for tests. Thx :)

simoll updated this revision to Diff 306066.Nov 18 2020, 5:22 AM
simoll marked 2 inline comments as done.
  • Added override options to override the expansion strategy.
  • Added expansion tests for fixed and scalable types and all possible expansion strategies.
  • Rebased.
simoll added inline comments.Nov 18 2020, 5:23 AM
llvm/include/llvm/IR/Intrinsics.td
1302 ↗(On Diff #306066)

FYI I'll commit the attribute changes to the VP intrinsics separately.

simoll updated this revision to Diff 306142.Nov 18 2020, 9:52 AM

Rebased onto "speculatable" VP intrinsic patch.

Ping. Where do we stand with this patch, can we LGTM or are there comments?

This all seams fairly reasonable to me, just some minor nitpicks. I'm not confident in giving this the final approval, but hopefully this can serve as a ping to those who are.

llvm/include/llvm/Analysis/TargetTransformInfo.h
1390

BIKESHED: "doNothing" sounds like a nop, not a test

llvm/lib/CodeGen/ExpandVectorPredication.cpp
138

This could conceivably work for scalable vectors after https://lists.llvm.org/pipermail/llvm-dev/2021-January/147943.html is merged

226–229

please name the type.

249

please name the type

255

please name types where the typename does not appear on the RHS.

303–304

please name the type

323–324

should these get undefined after the include?

simoll updated this revision to Diff 328094.Mar 4 2021, 2:40 AM
simoll marked 6 inline comments as done.

Addressed @ctetreau 's comments. Thx!

simoll updated this revision to Diff 328142.Mar 4 2021, 5:49 AM

Modernize tests, replace undef -> poison

simoll added inline comments.Mar 4 2021, 7:03 AM
llvm/lib/CodeGen/ExpandVectorPredication.cpp
138

Yep. We can switch to stepvector once its merged.

323–324

These get undefined in the included file.

Looks good to me in general, but I'm similarly not comfortable giving the final approval.

llvm/lib/CodeGen/ExpandVectorPredication.cpp
227

super nit: I'm not sure FirstOp and SndOp reads very well. I see that MemoryBuiltins uses FstParam and SndParam, but more common would be something like Op0 and Op1?

simoll updated this revision to Diff 334163.Tue, Mar 30, 7:38 AM
simoll marked an inline comment as done.

NFC

  • Addressed super nit
  • Formatting
  • Rebased