This is an archive of the discontinued LLVM Phabricator instance.

[VP] Introducing VectorBuilder, the VP intrinsic builder
ClosedPublic

Authored by simoll on Jul 1 2021, 7:29 AM.

Details

Summary

VectorBuilder wraps around an IRBuilder and VectorBuilder::createVectorInstructions emits VP intrinsics as if they were regular instructions.

Diff Detail

Event Timeline

simoll created this revision.Jul 1 2021, 7:29 AM
simoll requested review of this revision.Jul 1 2021, 7:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 1 2021, 7:29 AM
simoll retitled this revision from [VP] Introduction VectorBuilder, the VP intrinsic builder to [VP] Introducing VectorBuilder, the VP intrinsic builder.Jul 1 2021, 7:30 AM
simoll updated this revision to Diff 355891.Jul 1 2021, 8:21 AM
  • Cleanup
  • Better test coverage (paths where no evl and/or no mask set).
craig.topper added inline comments.Jul 20 2021, 8:31 AM
llvm/lib/IR/VectorBuilder.cpp
31

Doesn't BasicBlock have getModule?

simoll updated this revision to Diff 365137.Aug 9 2021, 3:20 AM
simoll marked an inline comment as done.

BasicBlock::getModule

rcorcs added a subscriber: rcorcs.Aug 9 2021, 3:31 AM
craig.topper added inline comments.Aug 9 2021, 9:57 AM
llvm/include/llvm/IR/VectorBuilder.h
93

Aren't Twine's usually passed by const reference?

simoll updated this revision to Diff 367437.Aug 19 2021, 2:41 AM
simoll marked an inline comment as done.
  • const Twine&
  • Rebased

LGTM though we should probably add extra tests for vp.select and vp.reduce.* now that they're in.

llvm/lib/IR/VectorBuilder.cpp
68

I'm thinking .hasValue() is a bit clearer than (bool)

70

don't need the parentheses around the equality comparison here or below.

simoll updated this revision to Diff 405701.Feb 3 2022, 10:13 AM
simoll marked 2 inline comments as done.
craig.topper added inline comments.Feb 3 2022, 10:39 AM
llvm/include/llvm/IR/VectorBuilder.h
27

Do we really need this typedef? Mainly asking because it went into the llvm namespace.

29

Can we move this into the class so it becomes VectorBuilder::Behavior?

84

How do you set StaticVectorLength to a scalable value?

llvm/lib/IR/VectorBuilder.cpp
76

Does this assume the Mask and EVL are at the end? If they aren't then ParamIdx isn't the correct index here if the Mask and EVL were earlier. Assuming there aren't empty slots for them in InstOpArray.

If they are always at the end and we're going to assume, that then there's not a lot of point for handling them inside the loop. You can just copy the InstOps, then add mask and evl if they exist.

llvm/unittests/IR/VectorBuilderTest.cpp
22

nit: int->unsigned. That's at least what most of the interfaces this gets passed to use.

32

Use std::make_unique instead of creating a unique_ptr for the return.

simoll updated this revision to Diff 405901.Feb 4 2022, 2:35 AM
simoll marked 4 inline comments as done.

Added fast path for trailing mask, evl in parameter list

simoll added a comment.Feb 4 2022, 2:35 AM

vp.merge and vp.select do not ignore masked-off lanes. We probably shouldn't treat their mask parameters as "masks" in the VP sense. I suggest we stop reporting their MaskPos in VPintrinsics.def.

llvm/include/llvm/IR/VectorBuilder.h
84

Scalable types aren't supported in this first patch. Planned for a followup.

llvm/lib/IR/VectorBuilder.cpp
76

There is an issue with ParamIdx at the moment. If the loop is past the first mask or evl parameter the ParamIdx will be off by one for the other one. It is okay for the other parameters though (note that there is no else in front of the last if).

simoll updated this revision to Diff 409184.Feb 16 2022, 2:39 AM

Rebased onto vp.merge|select-mask-is-not-a-vp-mask patch.

Where are we with this patch?

craig.topper added inline comments.Feb 28 2022, 11:47 AM
llvm/lib/IR/VectorBuilder.cpp
86

This loop doesn't work if VL is before mask and they are adjacent to each other. VL will increment VPParamIdx, but don't re-check mask after adding VL.

I think the for statement should be reponsible for incrementing VPParamIdx only. And ParamIdx should only be incremented in the loop body then a parameter from InstOpsArray is pushed. There should be no increments of VPParamIdx in the loop body.

simoll updated this revision to Diff 412383.Mar 2 2022, 5:28 AM
simoll marked an inline comment as done.

Improved parameter list construction following @craig.topper 's suggestion. Also, fast path (both mask and evl at tail) improved to allow for any ordering of the two.

Herald added a project: Restricted Project. · View Herald TranscriptMar 2 2022, 5:28 AM
craig.topper accepted this revision.Mar 2 2022, 2:16 PM

LGTM you can address the comments if you want, but I don't want to hold this up anymore.

llvm/lib/IR/VectorBuilder.cpp
81

What if we just called IntrinParams.resize(NumVPParams); and then assigned the mask and vl into their slots?

llvm/unittests/IR/VectorBuilderTest.cpp
131

Can simplify a little by merging the isa and cast to a single dyn_cast.

static bool isLegalConstEVL(Value *Val, unsigned ExpectedEVL) {
  auto *ConstEVL = dyn_cast<ConstantInt>(Val);
  if (!ConstEVL)
    return false;

  // Value check.
  
  if (ConstEVL->getZExtValue() != ExpectedEVL)
    return false;

  // Type check.
  return ConstEVL->getType()->isIntegerTy(32);
}

Or go really packed and do.

static bool isLegalConstEVL(Value *Val, unsigned ExpectedEVL) {
  auto *ConstEVL = dyn_cast<ConstantInt>(Val);
  return ConstEVL && ConstEVL->getZExtValue() == ExpectedEVL && ConstEVL->getType()->isIntegerTy(32);
}
This revision is now accepted and ready to land.Mar 2 2022, 2:16 PM
simoll marked an inline comment as done.Mar 2 2022, 11:58 PM
simoll added inline comments.
llvm/lib/IR/VectorBuilder.cpp
81

I like it

llvm/unittests/IR/VectorBuilderTest.cpp
131

The first one. This isn't code golf, sir ;-)

simoll updated this revision to Diff 412620.Mar 3 2022, 12:45 AM
simoll marked an inline comment as done.

Cleaner code for the parameter list construction

This revision was automatically updated to reflect the committed changes.
simoll added a comment.Mar 3 2022, 5:10 AM

Breaking the clang-cmake-x86_64-avx2-linux bot: https://lab.llvm.org/buildbot/#/builders/110/builds/10912
I suspect Behavior Behavior = Behavior::[..] doesn't go down well with that version of gcc. Waiting to find out which version of gcc is used there to reproduce.

Just a note, this also breaks implicit module build as the newly added header indirectly includes llvm/IR/Attributes.inc which needs to be generated first. If you do a module build (-DLLVM_ENABLE_MODULES=ON), you are likely to get error:

09:05:38 While building module 'LLVM_MC' imported from /System/Volumes/Data/jenkins/workspace/apple-clang-stage2-configure-RA_master/llvm-project/llvm/lib/MC/MCAsmBackend.cpp:9:
09:05:38 While building module 'LLVM_IR' imported from /System/Volumes/Data/jenkins/workspace/apple-clang-stage2-configure-RA_master/llvm-project/llvm/include/llvm/MC/MCPseudoProbe.h:49:
09:05:38 While building module 'LLVM_intrinsic_gen' imported from /System/Volumes/Data/jenkins/workspace/apple-clang-stage2-configure-RA_master/llvm-project/llvm/include/llvm/IR/VectorBuilder.h:18:
09:05:38 In file included from <module-includes>:1:
09:05:38 In file included from /System/Volumes/Data/jenkins/workspace/apple-clang-stage2-configure-RA_master/llvm-project/llvm/include/llvm/IR/Argument.h:17:
09:05:38 /System/Volumes/Data/jenkins/workspace/apple-clang-stage2-configure-RA_master/llvm-project/llvm/include/llvm/IR/Attributes.h:75:14: fatal error: 'llvm/IR/Attributes.inc' file not found
09:05:38     #include "llvm/IR/Attributes.inc"
09:05:38              ^~~~~~~~~~~~~~~~~~~~~~~~
09:05:38 While building module 'LLVM_MC' imported from /System/Volumes/Data/jenkins/workspace/apple-clang-stage2-configure-RA_master/llvm-project/llvm/lib/MC/MCAsmBackend.cpp:9:
09:05:38 While building module 'LLVM_IR' imported from /System/Volumes/Data/jenkins/workspace/apple-clang-stage2-configure-RA_master/llvm-project/llvm/include/llvm/MC/MCPseudoProbe.h:49:
09:05:38 In file included from <module-includes>:62:
09:05:38 /System/Volumes/Data/jenkins/workspace/apple-clang-stage2-configure-RA_master/llvm-project/llvm/include/llvm/IR/VectorBuilder.h:18:10: fatal error: could not build module 'LLVM_intrinsic_gen'
09:05:38 #include <llvm/IR/IRBuilder.h>
09:05:38  ~~~~~~~~^

You probably need to do something in the module map to fix this (see: module LLVM_intrinsic_gen in llvm/include/llvm/module.modulemap )

craig.topper added inline comments.Mar 3 2022, 1:38 PM
llvm/include/llvm/IR/VectorBuilder.h
22

Is anything from PatternMatch being used?

simoll added inline comments.Mar 4 2022, 2:39 AM
llvm/include/llvm/IR/VectorBuilder.h
22

Definitely not in this header, no

simoll reopened this revision.Mar 4 2022, 7:15 AM
This revision is now accepted and ready to land.Mar 4 2022, 7:15 AM
simoll updated this revision to Diff 413002.Mar 4 2022, 7:17 AM
  • Add module map entry
  • Rename Behavior variable (this may be confusing gcc 5.4; not confirmed)
  • Remove PatternMatch include
This revision was landed with ongoing or failed builds.Mar 7 2022, 1:03 AM
This revision was automatically updated to reflect the committed changes.