1 //===- VectorBuilder.cpp - Builder for VP Intrinsics ----------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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>
24 void VectorBuilder::handleError(const char *ErrorMsg
) const {
25 if (ErrorHandling
== Behavior::SilentlyReturnNone
)
27 report_fatal_error(ErrorMsg
);
30 Module
&VectorBuilder::getModule() const {
31 return *Builder
.GetInsertBlock()->getModule();
34 Value
*VectorBuilder::getAllTrueMask() {
35 return Builder
.getAllOnesMask(StaticVectorLength
);
38 Value
&VectorBuilder::requestMask() {
42 return *getAllTrueMask();
45 Value
&VectorBuilder::requestEVL() {
46 if (ExplicitVectorLength
)
47 return *ExplicitVectorLength
;
49 assert(!StaticVectorLength
.isScalable() && "TODO vscale lowering");
50 auto *IntTy
= Builder
.getInt32Ty();
51 return *ConstantInt::get(IntTy
, StaticVectorLength
.getFixedValue());
54 Value
*VectorBuilder::createVectorInstruction(unsigned Opcode
, Type
*ReturnTy
,
55 ArrayRef
<Value
*> InstOpArray
,
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
);
63 Value
*VectorBuilder::createSimpleReduction(Intrinsic::ID RdxID
,
65 ArrayRef
<Value
*> InstOpArray
,
67 auto VPID
= VPIntrinsic::getForIntrinsic(RdxID
);
68 assert(VPReductionIntrinsic::isVPReduction(VPID
) &&
69 "No VPIntrinsic for this reduction");
70 return createVectorInstructionImpl(VPID
, ValTy
, InstOpArray
, Name
);
73 Value
*VectorBuilder::createVectorInstructionImpl(Intrinsic::ID VPID
,
75 ArrayRef
<Value
*> InstOpArray
,
77 auto MaskPosOpt
= VPIntrinsic::getMaskParamPos(VPID
);
78 auto VLenPosOpt
= VPIntrinsic::getVectorLengthParamPos(VPID
);
79 size_t NumInstParams
= InstOpArray
.size();
81 NumInstParams
+ MaskPosOpt
.has_value() + VLenPosOpt
.has_value();
83 SmallVector
<Value
*, 6> IntrinParams
;
85 // Whether the mask and vlen parameter are at the end of the parameter list.
86 bool TrailingMaskAndVLen
=
87 std::min
<size_t>(MaskPosOpt
.value_or(NumInstParams
),
88 VLenPosOpt
.value_or(NumInstParams
)) >= NumInstParams
;
90 if (TrailingMaskAndVLen
) {
91 // Fast path for trailing mask, vector length.
92 IntrinParams
.append(InstOpArray
.begin(), InstOpArray
.end());
93 IntrinParams
.resize(NumVPParams
);
95 IntrinParams
.resize(NumVPParams
);
96 // Insert mask and evl operands in between the instruction operands.
97 for (size_t VPParamIdx
= 0, ParamIdx
= 0; VPParamIdx
< NumVPParams
;
99 if (MaskPosOpt
== VPParamIdx
|| VLenPosOpt
== VPParamIdx
)
101 assert(ParamIdx
< NumInstParams
);
102 IntrinParams
[VPParamIdx
] = InstOpArray
[ParamIdx
++];
107 IntrinParams
[*MaskPosOpt
] = &requestMask();
109 IntrinParams
[*VLenPosOpt
] = &requestEVL();
111 auto *VPDecl
= VPIntrinsic::getOrInsertDeclarationForParams(
112 &getModule(), VPID
, ReturnTy
, IntrinParams
);
113 return Builder
.CreateCall(VPDecl
, IntrinParams
, Name
);