NVPTXTargetTransformInfo.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===-- NVPTXTargetTransformInfo.h - NVPTX specific TTI ---------*- 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. /// \file
  9. /// This file a TargetTransformInfo::Concept conforming object specific to the
  10. /// NVPTX target machine. It uses the target's detailed information to
  11. /// provide more precise answers to certain TTI queries, while letting the
  12. /// target independent and default TTI implementations handle the rest.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_LIB_TARGET_NVPTX_NVPTXTARGETTRANSFORMINFO_H
  16. #define LLVM_LIB_TARGET_NVPTX_NVPTXTARGETTRANSFORMINFO_H
  17. #include "NVPTXTargetMachine.h"
  18. #include "MCTargetDesc/NVPTXBaseInfo.h"
  19. #include "llvm/Analysis/TargetTransformInfo.h"
  20. #include "llvm/CodeGen/BasicTTIImpl.h"
  21. #include "llvm/CodeGen/TargetLowering.h"
  22. #include <optional>
  23. namespace llvm {
  24. class NVPTXTTIImpl : public BasicTTIImplBase<NVPTXTTIImpl> {
  25. typedef BasicTTIImplBase<NVPTXTTIImpl> BaseT;
  26. typedef TargetTransformInfo TTI;
  27. friend BaseT;
  28. const NVPTXSubtarget *ST;
  29. const NVPTXTargetLowering *TLI;
  30. const NVPTXSubtarget *getST() const { return ST; };
  31. const NVPTXTargetLowering *getTLI() const { return TLI; };
  32. public:
  33. explicit NVPTXTTIImpl(const NVPTXTargetMachine *TM, const Function &F)
  34. : BaseT(TM, F.getParent()->getDataLayout()), ST(TM->getSubtargetImpl()),
  35. TLI(ST->getTargetLowering()) {}
  36. bool hasBranchDivergence() { return true; }
  37. bool isSourceOfDivergence(const Value *V);
  38. unsigned getFlatAddressSpace() const {
  39. return AddressSpace::ADDRESS_SPACE_GENERIC;
  40. }
  41. bool canHaveNonUndefGlobalInitializerInAddressSpace(unsigned AS) const {
  42. return AS != AddressSpace::ADDRESS_SPACE_SHARED &&
  43. AS != AddressSpace::ADDRESS_SPACE_LOCAL && AS != ADDRESS_SPACE_PARAM;
  44. }
  45. std::optional<Instruction *> instCombineIntrinsic(InstCombiner &IC,
  46. IntrinsicInst &II) const;
  47. // Loads and stores can be vectorized if the alignment is at least as big as
  48. // the load/store we want to vectorize.
  49. bool isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes, Align Alignment,
  50. unsigned AddrSpace) const {
  51. return Alignment >= ChainSizeInBytes;
  52. }
  53. bool isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes, Align Alignment,
  54. unsigned AddrSpace) const {
  55. return isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment, AddrSpace);
  56. }
  57. // NVPTX has infinite registers of all kinds, but the actual machine doesn't.
  58. // We conservatively return 1 here which is just enough to enable the
  59. // vectorizers but disables heuristics based on the number of registers.
  60. // FIXME: Return a more reasonable number, while keeping an eye on
  61. // LoopVectorizer's unrolling heuristics.
  62. unsigned getNumberOfRegisters(bool Vector) const { return 1; }
  63. // Only <2 x half> should be vectorized, so always return 32 for the vector
  64. // register size.
  65. TypeSize getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const {
  66. return TypeSize::getFixed(32);
  67. }
  68. unsigned getMinVectorRegisterBitWidth() const { return 32; }
  69. // We don't want to prevent inlining because of target-cpu and -features
  70. // attributes that were added to newer versions of LLVM/Clang: There are
  71. // no incompatible functions in PTX, ptxas will throw errors in such cases.
  72. bool areInlineCompatible(const Function *Caller,
  73. const Function *Callee) const {
  74. return true;
  75. }
  76. // Increase the inlining cost threshold by a factor of 5, reflecting that
  77. // calls are particularly expensive in NVPTX.
  78. unsigned getInliningThresholdMultiplier() { return 5; }
  79. InstructionCost getArithmeticInstrCost(
  80. unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
  81. TTI::OperandValueInfo Op1Info = {TTI::OK_AnyValue, TTI::OP_None},
  82. TTI::OperandValueInfo Op2Info = {TTI::OK_AnyValue, TTI::OP_None},
  83. ArrayRef<const Value *> Args = ArrayRef<const Value *>(),
  84. const Instruction *CxtI = nullptr);
  85. void getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
  86. TTI::UnrollingPreferences &UP,
  87. OptimizationRemarkEmitter *ORE);
  88. void getPeelingPreferences(Loop *L, ScalarEvolution &SE,
  89. TTI::PeelingPreferences &PP);
  90. bool hasVolatileVariant(Instruction *I, unsigned AddrSpace) {
  91. // Volatile loads/stores are only supported for shared and global address
  92. // spaces, or for generic AS that maps to them.
  93. if (!(AddrSpace == llvm::ADDRESS_SPACE_GENERIC ||
  94. AddrSpace == llvm::ADDRESS_SPACE_GLOBAL ||
  95. AddrSpace == llvm::ADDRESS_SPACE_SHARED))
  96. return false;
  97. switch(I->getOpcode()){
  98. default:
  99. return false;
  100. case Instruction::Load:
  101. case Instruction::Store:
  102. return true;
  103. }
  104. }
  105. };
  106. } // end namespace llvm
  107. #endif