xref: /llvm-project/llvm/include/llvm/IR/VectorBuilder.h (revision 3e8840ba71bfcceeb598c2ca28d2d8784e24ba1e)
1 //===- llvm/VectorBuilder.h - Builder for VP Intrinsics ---------*- C++ -*-===//
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 defines the VectorBuilder class, which is used as a convenient way
10 // to create VP intrinsics as if they were LLVM instructions with a consistent
11 // and simplified interface.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_VECTORBUILDER_H
16 #define LLVM_IR_VECTORBUILDER_H
17 
18 #include <llvm/IR/IRBuilder.h>
19 #include <llvm/IR/InstrTypes.h>
20 #include <llvm/IR/Instruction.h>
21 #include <llvm/IR/Value.h>
22 
23 namespace llvm {
24 
25 class VectorBuilder {
26 public:
27   enum class Behavior {
28     // Abort if the requested VP intrinsic could not be created.
29     // This is useful for strict consistency.
30     ReportAndAbort = 0,
31 
32     // Return a default-initialized value if the requested VP intrinsic could
33     // not be created.
34     // This is useful for a defensive fallback to non-VP code.
35     SilentlyReturnNone = 1,
36   };
37 
38 private:
39   IRBuilderBase &Builder;
40   Behavior ErrorHandling;
41 
42   // Explicit mask parameter.
43   Value *Mask;
44   // Explicit vector length parameter.
45   Value *ExplicitVectorLength;
46   // Compile-time vector length.
47   ElementCount StaticVectorLength;
48 
49   // Get mask/evl value handles for the current configuration.
50   Value &requestMask();
51   Value &requestEVL();
52 
53   void handleError(const char *ErrorMsg) const;
54   template <typename RetType>
55   RetType returnWithError(const char *ErrorMsg) const {
56     handleError(ErrorMsg);
57     return RetType();
58   }
59 
60   /// Helper function for creating VP intrinsic call.
61   Value *createVectorInstructionImpl(Intrinsic::ID VPID, Type *ReturnTy,
62                                      ArrayRef<Value *> VecOpArray,
63                                      const Twine &Name = Twine());
64 
65 public:
66   VectorBuilder(IRBuilderBase &Builder,
67                 Behavior ErrorHandling = Behavior::ReportAndAbort)
68       : Builder(Builder), ErrorHandling(ErrorHandling), Mask(nullptr),
69         ExplicitVectorLength(nullptr),
70         StaticVectorLength(ElementCount::getFixed(0)) {}
71 
72   Module &getModule() const;
73   LLVMContext &getContext() const { return Builder.getContext(); }
74 
75   // All-true mask for the currently configured explicit vector length.
76   Value *getAllTrueMask();
77 
78   VectorBuilder &setMask(Value *NewMask) {
79     Mask = NewMask;
80     return *this;
81   }
82   VectorBuilder &setEVL(Value *NewExplicitVectorLength) {
83     ExplicitVectorLength = NewExplicitVectorLength;
84     return *this;
85   }
86   VectorBuilder &setStaticVL(unsigned NewFixedVL) {
87     StaticVectorLength = ElementCount::getFixed(NewFixedVL);
88     return *this;
89   }
90   // TODO: setStaticVL(ElementCount) for scalable types.
91 
92   // Emit a VP intrinsic call that mimics a regular instruction.
93   // This operation behaves according to the VectorBuilderBehavior.
94   // \p Opcode      The functional instruction opcode of the emitted intrinsic.
95   // \p ReturnTy    The return type of the operation.
96   // \p VecOpArray  The operand list.
97   Value *createVectorInstruction(unsigned Opcode, Type *ReturnTy,
98                                  ArrayRef<Value *> VecOpArray,
99                                  const Twine &Name = Twine());
100 
101   /// Emit a VP reduction intrinsic call for recurrence kind.
102   /// \param RdxID       The intrinsic ID of llvm.vector.reduce.*
103   /// \param ValTy       The type of operand which the reduction operation is
104   ///                    performed.
105   /// \param VecOpArray  The operand list.
106   Value *createSimpleReduction(Intrinsic::ID RdxID, Type *ValTy,
107                                ArrayRef<Value *> VecOpArray,
108                                const Twine &Name = Twine());
109 };
110 
111 } // namespace llvm
112 
113 #endif // LLVM_IR_VECTORBUILDER_H
114