LLVM  15.0.0git
VectorBuilder.cpp
Go to the documentation of this file.
1 //===- VectorBuilder.cpp - Builder for VP Intrinsics ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the VectorBuilder class, which is used as a convenient
10 // way to create VP intrinsics as if they were LLVM instructions with a
11 // consistent and simplified interface.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include <llvm/ADT/SmallVector.h>
16 #include <llvm/IR/FPEnv.h>
17 #include <llvm/IR/Instructions.h>
18 #include <llvm/IR/IntrinsicInst.h>
19 #include <llvm/IR/Intrinsics.h>
20 #include <llvm/IR/VectorBuilder.h>
21 
22 namespace llvm {
23 
24 void VectorBuilder::handleError(const char *ErrorMsg) const {
25  if (ErrorHandling == Behavior::SilentlyReturnNone)
26  return;
27  report_fatal_error(ErrorMsg);
28 }
29 
31  return *Builder.GetInsertBlock()->getModule();
32 }
33 
35  auto *BoolTy = Builder.getInt1Ty();
36  auto *MaskTy = VectorType::get(BoolTy, StaticVectorLength);
37  return ConstantInt::getAllOnesValue(MaskTy);
38 }
39 
40 Value &VectorBuilder::requestMask() {
41  if (Mask)
42  return *Mask;
43 
44  return *getAllTrueMask();
45 }
46 
47 Value &VectorBuilder::requestEVL() {
48  if (ExplicitVectorLength)
49  return *ExplicitVectorLength;
50 
51  assert(!StaticVectorLength.isScalable() && "TODO vscale lowering");
52  auto *IntTy = Builder.getInt32Ty();
53  return *ConstantInt::get(IntTy, StaticVectorLength.getFixedValue());
54 }
55 
57  ArrayRef<Value *> InstOpArray,
58  const Twine &Name) {
59  auto VPID = VPIntrinsic::getForOpcode(Opcode);
60  if (VPID == Intrinsic::not_intrinsic)
61  return returnWithError<Value *>("No VPIntrinsic for this opcode");
62 
63  auto MaskPosOpt = VPIntrinsic::getMaskParamPos(VPID);
64  auto VLenPosOpt = VPIntrinsic::getVectorLengthParamPos(VPID);
65  size_t NumInstParams = InstOpArray.size();
66  size_t NumVPParams =
67  NumInstParams + MaskPosOpt.has_value() + VLenPosOpt.has_value();
68 
69  SmallVector<Value *, 6> IntrinParams;
70 
71  // Whether the mask and vlen parameter are at the end of the parameter list.
72  bool TrailingMaskAndVLen =
73  std::min<size_t>(MaskPosOpt.value_or(NumInstParams),
74  VLenPosOpt.value_or(NumInstParams)) >= NumInstParams;
75 
76  if (TrailingMaskAndVLen) {
77  // Fast path for trailing mask, vector length.
78  IntrinParams.append(InstOpArray.begin(), InstOpArray.end());
79  IntrinParams.resize(NumVPParams);
80  } else {
81  IntrinParams.resize(NumVPParams);
82  // Insert mask and evl operands in between the instruction operands.
83  for (size_t VPParamIdx = 0, ParamIdx = 0; VPParamIdx < NumVPParams;
84  ++VPParamIdx) {
85  if ((MaskPosOpt && MaskPosOpt.value_or(NumVPParams) == VPParamIdx) ||
86  (VLenPosOpt && VLenPosOpt.value_or(NumVPParams) == VPParamIdx))
87  continue;
88  assert(ParamIdx < NumInstParams);
89  IntrinParams[VPParamIdx] = InstOpArray[ParamIdx++];
90  }
91  }
92 
93  if (MaskPosOpt)
94  IntrinParams[*MaskPosOpt] = &requestMask();
95  if (VLenPosOpt)
96  IntrinParams[*VLenPosOpt] = &requestEVL();
97 
98  auto *VPDecl = VPIntrinsic::getDeclarationForParams(&getModule(), VPID,
99  ReturnTy, IntrinParams);
100  return Builder.CreateCall(VPDecl, IntrinParams, Name);
101 }
102 
103 } // namespace llvm
llvm::VPIntrinsic::getVectorLengthParamPos
static Optional< unsigned > getVectorLengthParamPos(Intrinsic::ID IntrinsicID)
Definition: IntrinsicInst.cpp:350
llvm::IRBuilderBase::getInt32Ty
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:501
VectorBuilder.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
IntrinsicInst.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::VectorBuilder::Behavior::SilentlyReturnNone
@ SilentlyReturnNone
llvm::VectorBuilder::createVectorInstruction
Value * createVectorInstruction(unsigned Opcode, Type *ReturnTy, ArrayRef< Value * > VecOpArray, const Twine &Name=Twine())
Definition: VectorBuilder.cpp:56
llvm::VectorBuilder::getAllTrueMask
Value * getAllTrueMask()
Definition: VectorBuilder.cpp:34
llvm::VPIntrinsic::getDeclarationForParams
static Function * getDeclarationForParams(Module *M, Intrinsic::ID, Type *ReturnType, ArrayRef< Value * > Params)
Declares a llvm.vp.
Definition: IntrinsicInst.cpp:489
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::Intrinsic::not_intrinsic
@ not_intrinsic
Definition: Intrinsics.h:45
llvm::LinearPolySize::isScalable
bool isScalable() const
Returns whether the size is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:298
llvm::SmallVectorImpl::append
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:667
Intrinsics.h
llvm::Constant::getAllOnesValue
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:395
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:143
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:619
llvm::ConstantInt::get
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:926
llvm::VPIntrinsic::getForOpcode
static Intrinsic::ID getForOpcode(unsigned OC)
The llvm.vp.* intrinsics for this instruction Opcode.
Definition: IntrinsicInst.cpp:434
llvm::BasicBlock::getModule
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:147
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::LinearPolySize::getFixedValue
ScalarTy getFixedValue() const
Definition: TypeSize.h:312
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
FPEnv.h
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:152
llvm::IRBuilderBase::GetInsertBlock
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:173
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::VPIntrinsic::getMaskParamPos
static Optional< unsigned > getMaskParamPos(Intrinsic::ID IntrinsicID)
Definition: IntrinsicInst.cpp:337
llvm::IRBuilderBase::getInt1Ty
IntegerType * getInt1Ty()
Fetch the type representing a single bit.
Definition: IRBuilder.h:486
Instructions.h
SmallVector.h
llvm::VectorBuilder::getModule
Module & getModule() const
Definition: VectorBuilder.cpp:30
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::VectorType::get
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:668
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:153
llvm::IRBuilderBase::CreateCall
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2229