VectorBuilder.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include <llvm/ADT/SmallVector.h>
  15. #include <llvm/IR/FPEnv.h>
  16. #include <llvm/IR/Instructions.h>
  17. #include <llvm/IR/IntrinsicInst.h>
  18. #include <llvm/IR/Intrinsics.h>
  19. #include <llvm/IR/VectorBuilder.h>
  20. namespace llvm {
  21. void VectorBuilder::handleError(const char *ErrorMsg) const {
  22. if (ErrorHandling == Behavior::SilentlyReturnNone)
  23. return;
  24. report_fatal_error(ErrorMsg);
  25. }
  26. Module &VectorBuilder::getModule() const {
  27. return *Builder.GetInsertBlock()->getModule();
  28. }
  29. Value *VectorBuilder::getAllTrueMask() {
  30. auto *BoolTy = Builder.getInt1Ty();
  31. auto *MaskTy = VectorType::get(BoolTy, StaticVectorLength);
  32. return ConstantInt::getAllOnesValue(MaskTy);
  33. }
  34. Value &VectorBuilder::requestMask() {
  35. if (Mask)
  36. return *Mask;
  37. return *getAllTrueMask();
  38. }
  39. Value &VectorBuilder::requestEVL() {
  40. if (ExplicitVectorLength)
  41. return *ExplicitVectorLength;
  42. assert(!StaticVectorLength.isScalable() && "TODO vscale lowering");
  43. auto *IntTy = Builder.getInt32Ty();
  44. return *ConstantInt::get(IntTy, StaticVectorLength.getFixedValue());
  45. }
  46. Value *VectorBuilder::createVectorInstruction(unsigned Opcode, Type *ReturnTy,
  47. ArrayRef<Value *> InstOpArray,
  48. const Twine &Name) {
  49. auto VPID = VPIntrinsic::getForOpcode(Opcode);
  50. if (VPID == Intrinsic::not_intrinsic)
  51. return returnWithError<Value *>("No VPIntrinsic for this opcode");
  52. auto MaskPosOpt = VPIntrinsic::getMaskParamPos(VPID);
  53. auto VLenPosOpt = VPIntrinsic::getVectorLengthParamPos(VPID);
  54. size_t NumInstParams = InstOpArray.size();
  55. size_t NumVPParams =
  56. NumInstParams + MaskPosOpt.has_value() + VLenPosOpt.has_value();
  57. SmallVector<Value *, 6> IntrinParams;
  58. // Whether the mask and vlen parameter are at the end of the parameter list.
  59. bool TrailingMaskAndVLen =
  60. std::min<size_t>(MaskPosOpt.value_or(NumInstParams),
  61. VLenPosOpt.value_or(NumInstParams)) >= NumInstParams;
  62. if (TrailingMaskAndVLen) {
  63. // Fast path for trailing mask, vector length.
  64. IntrinParams.append(InstOpArray.begin(), InstOpArray.end());
  65. IntrinParams.resize(NumVPParams);
  66. } else {
  67. IntrinParams.resize(NumVPParams);
  68. // Insert mask and evl operands in between the instruction operands.
  69. for (size_t VPParamIdx = 0, ParamIdx = 0; VPParamIdx < NumVPParams;
  70. ++VPParamIdx) {
  71. if ((MaskPosOpt && MaskPosOpt.value_or(NumVPParams) == VPParamIdx) ||
  72. (VLenPosOpt && VLenPosOpt.value_or(NumVPParams) == VPParamIdx))
  73. continue;
  74. assert(ParamIdx < NumInstParams);
  75. IntrinParams[VPParamIdx] = InstOpArray[ParamIdx++];
  76. }
  77. }
  78. if (MaskPosOpt)
  79. IntrinParams[*MaskPosOpt] = &requestMask();
  80. if (VLenPosOpt)
  81. IntrinParams[*VLenPosOpt] = &requestEVL();
  82. auto *VPDecl = VPIntrinsic::getDeclarationForParams(&getModule(), VPID,
  83. ReturnTy, IntrinParams);
  84. return Builder.CreateCall(VPDecl, IntrinParams, Name);
  85. }
  86. } // namespace llvm