LLVM 17.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
16#include <llvm/IR/FPEnv.h>
19#include <llvm/IR/Intrinsics.h>
21
22namespace llvm {
23
24void 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
40Value &VectorBuilder::requestMask() {
41 if (Mask)
42 return *Mask;
43
44 return *getAllTrueMask();
45}
46
47Value &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
std::string Name
This file contains the declarations of entities that describe floating point environment and related ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:152
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:163
iterator begin() const
Definition: ArrayRef.h:151
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:146
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:887
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:403
IntegerType * getInt1Ty()
Fetch the type representing a single bit.
Definition: IRBuilder.h:497
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:512
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:174
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2293
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:687
void resize(size_type N)
Definition: SmallVector.h:642
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Function * getDeclarationForParams(Module *M, Intrinsic::ID, Type *ReturnType, ArrayRef< Value * > Params)
Declares a llvm.vp.
static std::optional< unsigned > getMaskParamPos(Intrinsic::ID IntrinsicID)
static std::optional< unsigned > getVectorLengthParamPos(Intrinsic::ID IntrinsicID)
static Intrinsic::ID getForOpcode(unsigned OC)
The llvm.vp.* intrinsics for this instruction Opcode.
LLVM Value Representation.
Definition: Value.h:74
Module & getModule() const
Value * createVectorInstruction(unsigned Opcode, Type *ReturnTy, ArrayRef< Value * > VecOpArray, const Twine &Name=Twine())
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:670
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:182
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:166
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:145