LLVM 19.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 return Builder.getAllOnesMask(StaticVectorLength);
36}
37
38Value &VectorBuilder::requestMask() {
39 if (Mask)
40 return *Mask;
41
42 return *getAllTrueMask();
43}
44
45Value &VectorBuilder::requestEVL() {
46 if (ExplicitVectorLength)
47 return *ExplicitVectorLength;
48
49 assert(!StaticVectorLength.isScalable() && "TODO vscale lowering");
50 auto *IntTy = Builder.getInt32Ty();
51 return *ConstantInt::get(IntTy, StaticVectorLength.getFixedValue());
52}
53
55 ArrayRef<Value *> InstOpArray,
56 const Twine &Name) {
57 auto VPID = VPIntrinsic::getForOpcode(Opcode);
58 if (VPID == Intrinsic::not_intrinsic)
59 return returnWithError<Value *>("No VPIntrinsic for this opcode");
60 return createVectorInstructionImpl(VPID, ReturnTy, InstOpArray, Name);
61}
62
64 ArrayRef<Value *> InstOpArray,
65 const Twine &Name) {
66 Intrinsic::ID VPID;
67 switch (Kind) {
68 case RecurKind::Add:
69 VPID = Intrinsic::vp_reduce_add;
70 break;
71 case RecurKind::Mul:
72 VPID = Intrinsic::vp_reduce_mul;
73 break;
74 case RecurKind::And:
75 VPID = Intrinsic::vp_reduce_and;
76 break;
77 case RecurKind::Or:
78 VPID = Intrinsic::vp_reduce_or;
79 break;
80 case RecurKind::Xor:
81 VPID = Intrinsic::vp_reduce_xor;
82 break;
84 case RecurKind::FAdd:
85 VPID = Intrinsic::vp_reduce_fadd;
86 break;
87 case RecurKind::FMul:
88 VPID = Intrinsic::vp_reduce_fmul;
89 break;
90 case RecurKind::SMax:
91 VPID = Intrinsic::vp_reduce_smax;
92 break;
93 case RecurKind::SMin:
94 VPID = Intrinsic::vp_reduce_smin;
95 break;
96 case RecurKind::UMax:
97 VPID = Intrinsic::vp_reduce_umax;
98 break;
99 case RecurKind::UMin:
100 VPID = Intrinsic::vp_reduce_umin;
101 break;
102 case RecurKind::FMax:
103 VPID = Intrinsic::vp_reduce_fmax;
104 break;
105 case RecurKind::FMin:
106 VPID = Intrinsic::vp_reduce_fmin;
107 break;
109 VPID = Intrinsic::vp_reduce_fmaximum;
110 break;
112 VPID = Intrinsic::vp_reduce_fminimum;
113 break;
114 default:
115 llvm_unreachable("No VPIntrinsic for this reduction");
116 }
117 return createVectorInstructionImpl(VPID, ValTy, InstOpArray, Name);
118}
119
120Value *VectorBuilder::createVectorInstructionImpl(Intrinsic::ID VPID,
121 Type *ReturnTy,
122 ArrayRef<Value *> InstOpArray,
123 const Twine &Name) {
124 auto MaskPosOpt = VPIntrinsic::getMaskParamPos(VPID);
125 auto VLenPosOpt = VPIntrinsic::getVectorLengthParamPos(VPID);
126 size_t NumInstParams = InstOpArray.size();
127 size_t NumVPParams =
128 NumInstParams + MaskPosOpt.has_value() + VLenPosOpt.has_value();
129
130 SmallVector<Value *, 6> IntrinParams;
131
132 // Whether the mask and vlen parameter are at the end of the parameter list.
133 bool TrailingMaskAndVLen =
134 std::min<size_t>(MaskPosOpt.value_or(NumInstParams),
135 VLenPosOpt.value_or(NumInstParams)) >= NumInstParams;
136
137 if (TrailingMaskAndVLen) {
138 // Fast path for trailing mask, vector length.
139 IntrinParams.append(InstOpArray.begin(), InstOpArray.end());
140 IntrinParams.resize(NumVPParams);
141 } else {
142 IntrinParams.resize(NumVPParams);
143 // Insert mask and evl operands in between the instruction operands.
144 for (size_t VPParamIdx = 0, ParamIdx = 0; VPParamIdx < NumVPParams;
145 ++VPParamIdx) {
146 if ((MaskPosOpt && MaskPosOpt.value_or(NumVPParams) == VPParamIdx) ||
147 (VLenPosOpt && VLenPosOpt.value_or(NumVPParams) == VPParamIdx))
148 continue;
149 assert(ParamIdx < NumInstParams);
150 IntrinParams[VPParamIdx] = InstOpArray[ParamIdx++];
151 }
152 }
153
154 if (MaskPosOpt)
155 IntrinParams[*MaskPosOpt] = &requestMask();
156 if (VLenPosOpt)
157 IntrinParams[*VLenPosOpt] = &requestEVL();
158
159 auto *VPDecl = VPIntrinsic::getDeclarationForParams(&getModule(), VPID,
160 ReturnTy, IntrinParams);
161 return Builder.CreateCall(VPDecl, IntrinParams, Name);
162}
163
164} // 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:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
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:290
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:523
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:171
Value * getAllOnesMask(ElementCount NumElts)
Return an all true boolean vector (mask) with NumElts lanes.
Definition: IRBuilder.h:845
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2417
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:696
void resize(size_type N)
Definition: SmallVector.h:651
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
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 * createSimpleTargetReduction(RecurKind Kind, Type *ValTy, ArrayRef< Value * > VecOpArray, const Twine &Name=Twine())
Emit a VP reduction intrinsic call for recurrence kind.
Value * createVectorInstruction(unsigned Opcode, Type *ReturnTy, ArrayRef< Value * > VecOpArray, const Twine &Name=Twine())
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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:167
RecurKind
These are the kinds of recurrences that we support.
Definition: IVDescriptors.h:34
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ Or
Bitwise or logical OR of integers.
@ FMinimum
FP min with llvm.minimum semantics.
@ Mul
Product of integers.
@ Xor
Bitwise or logical XOR of integers.
@ FMax
FP max implemented in terms of select(cmp()).
@ FMaximum
FP max with llvm.maximum semantics.
@ FMulAdd
Sum of float products with llvm.fmuladd(a * b + sum).
@ FMul
Product of floats.
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ And
Bitwise or logical AND of integers.
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ FMin
FP min implemented in terms of select(cmp()).
@ Add
Sum of integers.
@ FAdd
Sum of floats.
@ UMax
Unsigned integer max implemented in terms of select(cmp()).