Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:11

0001 //===- llvm/VectorBuilder.h - Builder for VP Intrinsics ---------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the VectorBuilder class, which is used as a convenient way
0010 // to create VP intrinsics as if they were LLVM instructions with a consistent
0011 // and simplified interface.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_IR_VECTORBUILDER_H
0016 #define LLVM_IR_VECTORBUILDER_H
0017 
0018 #include <llvm/IR/IRBuilder.h>
0019 #include <llvm/IR/InstrTypes.h>
0020 #include <llvm/IR/Instruction.h>
0021 #include <llvm/IR/Value.h>
0022 
0023 namespace llvm {
0024 
0025 class VectorBuilder {
0026 public:
0027   enum class Behavior {
0028     // Abort if the requested VP intrinsic could not be created.
0029     // This is useful for strict consistency.
0030     ReportAndAbort = 0,
0031 
0032     // Return a default-initialized value if the requested VP intrinsic could
0033     // not be created.
0034     // This is useful for a defensive fallback to non-VP code.
0035     SilentlyReturnNone = 1,
0036   };
0037 
0038 private:
0039   IRBuilderBase &Builder;
0040   Behavior ErrorHandling;
0041 
0042   // Explicit mask parameter.
0043   Value *Mask;
0044   // Explicit vector length parameter.
0045   Value *ExplicitVectorLength;
0046   // Compile-time vector length.
0047   ElementCount StaticVectorLength;
0048 
0049   // Get mask/evl value handles for the current configuration.
0050   Value &requestMask();
0051   Value &requestEVL();
0052 
0053   void handleError(const char *ErrorMsg) const;
0054   template <typename RetType>
0055   RetType returnWithError(const char *ErrorMsg) const {
0056     handleError(ErrorMsg);
0057     return RetType();
0058   }
0059 
0060   /// Helper function for creating VP intrinsic call.
0061   Value *createVectorInstructionImpl(Intrinsic::ID VPID, Type *ReturnTy,
0062                                      ArrayRef<Value *> VecOpArray,
0063                                      const Twine &Name = Twine());
0064 
0065 public:
0066   VectorBuilder(IRBuilderBase &Builder,
0067                 Behavior ErrorHandling = Behavior::ReportAndAbort)
0068       : Builder(Builder), ErrorHandling(ErrorHandling), Mask(nullptr),
0069         ExplicitVectorLength(nullptr),
0070         StaticVectorLength(ElementCount::getFixed(0)) {}
0071 
0072   Module &getModule() const;
0073   LLVMContext &getContext() const { return Builder.getContext(); }
0074 
0075   // All-true mask for the currently configured explicit vector length.
0076   Value *getAllTrueMask();
0077 
0078   VectorBuilder &setMask(Value *NewMask) {
0079     Mask = NewMask;
0080     return *this;
0081   }
0082   VectorBuilder &setEVL(Value *NewExplicitVectorLength) {
0083     ExplicitVectorLength = NewExplicitVectorLength;
0084     return *this;
0085   }
0086   VectorBuilder &setStaticVL(unsigned NewFixedVL) {
0087     StaticVectorLength = ElementCount::getFixed(NewFixedVL);
0088     return *this;
0089   }
0090   // TODO: setStaticVL(ElementCount) for scalable types.
0091 
0092   // Emit a VP intrinsic call that mimics a regular instruction.
0093   // This operation behaves according to the VectorBuilderBehavior.
0094   // \p Opcode      The functional instruction opcode of the emitted intrinsic.
0095   // \p ReturnTy    The return type of the operation.
0096   // \p VecOpArray  The operand list.
0097   Value *createVectorInstruction(unsigned Opcode, Type *ReturnTy,
0098                                  ArrayRef<Value *> VecOpArray,
0099                                  const Twine &Name = Twine());
0100 
0101   /// Emit a VP reduction intrinsic call for recurrence kind.
0102   /// \param RdxID       The intrinsic ID of llvm.vector.reduce.*
0103   /// \param ValTy       The type of operand which the reduction operation is
0104   ///                    performed.
0105   /// \param VecOpArray  The operand list.
0106   Value *createSimpleReduction(Intrinsic::ID RdxID, Type *ValTy,
0107                                ArrayRef<Value *> VecOpArray,
0108                                const Twine &Name = Twine());
0109 };
0110 
0111 } // namespace llvm
0112 
0113 #endif // LLVM_IR_VECTORBUILDER_H