VectorBuilder.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/VectorBuilder.h - Builder for VP Intrinsics ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the VectorBuilder class, which is used as a convenient way
  15. // to create VP intrinsics as if they were LLVM instructions with a consistent
  16. // and simplified interface.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_IR_VECTORBUILDER_H
  20. #define LLVM_IR_VECTORBUILDER_H
  21. #include <llvm/IR/IRBuilder.h>
  22. #include <llvm/IR/InstrTypes.h>
  23. #include <llvm/IR/Instruction.h>
  24. #include <llvm/IR/Value.h>
  25. namespace llvm {
  26. class VectorBuilder {
  27. public:
  28. enum class Behavior {
  29. // Abort if the requested VP intrinsic could not be created.
  30. // This is useful for strict consistency.
  31. ReportAndAbort = 0,
  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. private:
  38. IRBuilderBase &Builder;
  39. Behavior ErrorHandling;
  40. // Explicit mask parameter.
  41. Value *Mask;
  42. // Explicit vector length parameter.
  43. Value *ExplicitVectorLength;
  44. // Compile-time vector length.
  45. ElementCount StaticVectorLength;
  46. // Get mask/evl value handles for the current configuration.
  47. Value &requestMask();
  48. Value &requestEVL();
  49. void handleError(const char *ErrorMsg) const;
  50. template <typename RetType>
  51. RetType returnWithError(const char *ErrorMsg) const {
  52. handleError(ErrorMsg);
  53. return RetType();
  54. }
  55. public:
  56. VectorBuilder(IRBuilderBase &Builder,
  57. Behavior ErrorHandling = Behavior::ReportAndAbort)
  58. : Builder(Builder), ErrorHandling(ErrorHandling), Mask(nullptr),
  59. ExplicitVectorLength(nullptr),
  60. StaticVectorLength(ElementCount::getFixed(0)) {}
  61. Module &getModule() const;
  62. LLVMContext &getContext() const { return Builder.getContext(); }
  63. // All-true mask for the currently configured explicit vector length.
  64. Value *getAllTrueMask();
  65. VectorBuilder &setMask(Value *NewMask) {
  66. Mask = NewMask;
  67. return *this;
  68. }
  69. VectorBuilder &setEVL(Value *NewExplicitVectorLength) {
  70. ExplicitVectorLength = NewExplicitVectorLength;
  71. return *this;
  72. }
  73. VectorBuilder &setStaticVL(unsigned NewFixedVL) {
  74. StaticVectorLength = ElementCount::getFixed(NewFixedVL);
  75. return *this;
  76. }
  77. // TODO: setStaticVL(ElementCount) for scalable types.
  78. // Emit a VP intrinsic call that mimics a regular instruction.
  79. // This operation behaves according to the VectorBuilderBehavior.
  80. // \p Opcode The functional instruction opcode of the emitted intrinsic.
  81. // \p ReturnTy The return type of the operation.
  82. // \p VecOpArray The operand list.
  83. Value *createVectorInstruction(unsigned Opcode, Type *ReturnTy,
  84. ArrayRef<Value *> VecOpArray,
  85. const Twine &Name = Twine());
  86. };
  87. } // namespace llvm
  88. #endif // LLVM_IR_VECTORBUILDER_H
  89. #ifdef __GNUC__
  90. #pragma GCC diagnostic pop
  91. #endif