NVPTXTargetTransformInfo.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. //===-- NVPTXTargetTransformInfo.cpp - NVPTX specific TTI -----------------===//
  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. #include "NVPTXTargetTransformInfo.h"
  9. #include "NVPTXUtilities.h"
  10. #include "llvm/Analysis/LoopInfo.h"
  11. #include "llvm/Analysis/TargetTransformInfo.h"
  12. #include "llvm/Analysis/ValueTracking.h"
  13. #include "llvm/CodeGen/BasicTTIImpl.h"
  14. #include "llvm/CodeGen/CostTable.h"
  15. #include "llvm/CodeGen/TargetLowering.h"
  16. #include "llvm/IR/IntrinsicsNVPTX.h"
  17. #include "llvm/Support/Debug.h"
  18. using namespace llvm;
  19. #define DEBUG_TYPE "NVPTXtti"
  20. // Whether the given intrinsic reads threadIdx.x/y/z.
  21. static bool readsThreadIndex(const IntrinsicInst *II) {
  22. switch (II->getIntrinsicID()) {
  23. default: return false;
  24. case Intrinsic::nvvm_read_ptx_sreg_tid_x:
  25. case Intrinsic::nvvm_read_ptx_sreg_tid_y:
  26. case Intrinsic::nvvm_read_ptx_sreg_tid_z:
  27. return true;
  28. }
  29. }
  30. static bool readsLaneId(const IntrinsicInst *II) {
  31. return II->getIntrinsicID() == Intrinsic::nvvm_read_ptx_sreg_laneid;
  32. }
  33. // Whether the given intrinsic is an atomic instruction in PTX.
  34. static bool isNVVMAtomic(const IntrinsicInst *II) {
  35. switch (II->getIntrinsicID()) {
  36. default: return false;
  37. case Intrinsic::nvvm_atomic_load_inc_32:
  38. case Intrinsic::nvvm_atomic_load_dec_32:
  39. case Intrinsic::nvvm_atomic_add_gen_f_cta:
  40. case Intrinsic::nvvm_atomic_add_gen_f_sys:
  41. case Intrinsic::nvvm_atomic_add_gen_i_cta:
  42. case Intrinsic::nvvm_atomic_add_gen_i_sys:
  43. case Intrinsic::nvvm_atomic_and_gen_i_cta:
  44. case Intrinsic::nvvm_atomic_and_gen_i_sys:
  45. case Intrinsic::nvvm_atomic_cas_gen_i_cta:
  46. case Intrinsic::nvvm_atomic_cas_gen_i_sys:
  47. case Intrinsic::nvvm_atomic_dec_gen_i_cta:
  48. case Intrinsic::nvvm_atomic_dec_gen_i_sys:
  49. case Intrinsic::nvvm_atomic_inc_gen_i_cta:
  50. case Intrinsic::nvvm_atomic_inc_gen_i_sys:
  51. case Intrinsic::nvvm_atomic_max_gen_i_cta:
  52. case Intrinsic::nvvm_atomic_max_gen_i_sys:
  53. case Intrinsic::nvvm_atomic_min_gen_i_cta:
  54. case Intrinsic::nvvm_atomic_min_gen_i_sys:
  55. case Intrinsic::nvvm_atomic_or_gen_i_cta:
  56. case Intrinsic::nvvm_atomic_or_gen_i_sys:
  57. case Intrinsic::nvvm_atomic_exch_gen_i_cta:
  58. case Intrinsic::nvvm_atomic_exch_gen_i_sys:
  59. case Intrinsic::nvvm_atomic_xor_gen_i_cta:
  60. case Intrinsic::nvvm_atomic_xor_gen_i_sys:
  61. return true;
  62. }
  63. }
  64. bool NVPTXTTIImpl::isSourceOfDivergence(const Value *V) {
  65. // Without inter-procedural analysis, we conservatively assume that arguments
  66. // to __device__ functions are divergent.
  67. if (const Argument *Arg = dyn_cast<Argument>(V))
  68. return !isKernelFunction(*Arg->getParent());
  69. if (const Instruction *I = dyn_cast<Instruction>(V)) {
  70. // Without pointer analysis, we conservatively assume values loaded from
  71. // generic or local address space are divergent.
  72. if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
  73. unsigned AS = LI->getPointerAddressSpace();
  74. return AS == ADDRESS_SPACE_GENERIC || AS == ADDRESS_SPACE_LOCAL;
  75. }
  76. // Atomic instructions may cause divergence. Atomic instructions are
  77. // executed sequentially across all threads in a warp. Therefore, an earlier
  78. // executed thread may see different memory inputs than a later executed
  79. // thread. For example, suppose *a = 0 initially.
  80. //
  81. // atom.global.add.s32 d, [a], 1
  82. //
  83. // returns 0 for the first thread that enters the critical region, and 1 for
  84. // the second thread.
  85. if (I->isAtomic())
  86. return true;
  87. if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
  88. // Instructions that read threadIdx are obviously divergent.
  89. if (readsThreadIndex(II) || readsLaneId(II))
  90. return true;
  91. // Handle the NVPTX atomic instrinsics that cannot be represented as an
  92. // atomic IR instruction.
  93. if (isNVVMAtomic(II))
  94. return true;
  95. }
  96. // Conservatively consider the return value of function calls as divergent.
  97. // We could analyze callees with bodies more precisely using
  98. // inter-procedural analysis.
  99. if (isa<CallInst>(I))
  100. return true;
  101. }
  102. return false;
  103. }
  104. // Convert NVVM intrinsics to target-generic LLVM code where possible.
  105. static Instruction *simplifyNvvmIntrinsic(IntrinsicInst *II, InstCombiner &IC) {
  106. // Each NVVM intrinsic we can simplify can be replaced with one of:
  107. //
  108. // * an LLVM intrinsic,
  109. // * an LLVM cast operation,
  110. // * an LLVM binary operation, or
  111. // * ad-hoc LLVM IR for the particular operation.
  112. // Some transformations are only valid when the module's
  113. // flush-denormals-to-zero (ftz) setting is true/false, whereas other
  114. // transformations are valid regardless of the module's ftz setting.
  115. enum FtzRequirementTy {
  116. FTZ_Any, // Any ftz setting is ok.
  117. FTZ_MustBeOn, // Transformation is valid only if ftz is on.
  118. FTZ_MustBeOff, // Transformation is valid only if ftz is off.
  119. };
  120. // Classes of NVVM intrinsics that can't be replaced one-to-one with a
  121. // target-generic intrinsic, cast op, or binary op but that we can nonetheless
  122. // simplify.
  123. enum SpecialCase {
  124. SPC_Reciprocal,
  125. };
  126. // SimplifyAction is a poor-man's variant (plus an additional flag) that
  127. // represents how to replace an NVVM intrinsic with target-generic LLVM IR.
  128. struct SimplifyAction {
  129. // Invariant: At most one of these Optionals has a value.
  130. Optional<Intrinsic::ID> IID;
  131. Optional<Instruction::CastOps> CastOp;
  132. Optional<Instruction::BinaryOps> BinaryOp;
  133. Optional<SpecialCase> Special;
  134. FtzRequirementTy FtzRequirement = FTZ_Any;
  135. SimplifyAction() = default;
  136. SimplifyAction(Intrinsic::ID IID, FtzRequirementTy FtzReq)
  137. : IID(IID), FtzRequirement(FtzReq) {}
  138. // Cast operations don't have anything to do with FTZ, so we skip that
  139. // argument.
  140. SimplifyAction(Instruction::CastOps CastOp) : CastOp(CastOp) {}
  141. SimplifyAction(Instruction::BinaryOps BinaryOp, FtzRequirementTy FtzReq)
  142. : BinaryOp(BinaryOp), FtzRequirement(FtzReq) {}
  143. SimplifyAction(SpecialCase Special, FtzRequirementTy FtzReq)
  144. : Special(Special), FtzRequirement(FtzReq) {}
  145. };
  146. // Try to generate a SimplifyAction describing how to replace our
  147. // IntrinsicInstr with target-generic LLVM IR.
  148. const SimplifyAction Action = [II]() -> SimplifyAction {
  149. switch (II->getIntrinsicID()) {
  150. // NVVM intrinsics that map directly to LLVM intrinsics.
  151. case Intrinsic::nvvm_ceil_d:
  152. return {Intrinsic::ceil, FTZ_Any};
  153. case Intrinsic::nvvm_ceil_f:
  154. return {Intrinsic::ceil, FTZ_MustBeOff};
  155. case Intrinsic::nvvm_ceil_ftz_f:
  156. return {Intrinsic::ceil, FTZ_MustBeOn};
  157. case Intrinsic::nvvm_fabs_d:
  158. return {Intrinsic::fabs, FTZ_Any};
  159. case Intrinsic::nvvm_fabs_f:
  160. return {Intrinsic::fabs, FTZ_MustBeOff};
  161. case Intrinsic::nvvm_fabs_ftz_f:
  162. return {Intrinsic::fabs, FTZ_MustBeOn};
  163. case Intrinsic::nvvm_floor_d:
  164. return {Intrinsic::floor, FTZ_Any};
  165. case Intrinsic::nvvm_floor_f:
  166. return {Intrinsic::floor, FTZ_MustBeOff};
  167. case Intrinsic::nvvm_floor_ftz_f:
  168. return {Intrinsic::floor, FTZ_MustBeOn};
  169. case Intrinsic::nvvm_fma_rn_d:
  170. return {Intrinsic::fma, FTZ_Any};
  171. case Intrinsic::nvvm_fma_rn_f:
  172. return {Intrinsic::fma, FTZ_MustBeOff};
  173. case Intrinsic::nvvm_fma_rn_ftz_f:
  174. return {Intrinsic::fma, FTZ_MustBeOn};
  175. case Intrinsic::nvvm_fmax_d:
  176. return {Intrinsic::maxnum, FTZ_Any};
  177. case Intrinsic::nvvm_fmax_f:
  178. return {Intrinsic::maxnum, FTZ_MustBeOff};
  179. case Intrinsic::nvvm_fmax_ftz_f:
  180. return {Intrinsic::maxnum, FTZ_MustBeOn};
  181. case Intrinsic::nvvm_fmin_d:
  182. return {Intrinsic::minnum, FTZ_Any};
  183. case Intrinsic::nvvm_fmin_f:
  184. return {Intrinsic::minnum, FTZ_MustBeOff};
  185. case Intrinsic::nvvm_fmin_ftz_f:
  186. return {Intrinsic::minnum, FTZ_MustBeOn};
  187. case Intrinsic::nvvm_round_d:
  188. return {Intrinsic::round, FTZ_Any};
  189. case Intrinsic::nvvm_round_f:
  190. return {Intrinsic::round, FTZ_MustBeOff};
  191. case Intrinsic::nvvm_round_ftz_f:
  192. return {Intrinsic::round, FTZ_MustBeOn};
  193. case Intrinsic::nvvm_sqrt_rn_d:
  194. return {Intrinsic::sqrt, FTZ_Any};
  195. case Intrinsic::nvvm_sqrt_f:
  196. // nvvm_sqrt_f is a special case. For most intrinsics, foo_ftz_f is the
  197. // ftz version, and foo_f is the non-ftz version. But nvvm_sqrt_f adopts
  198. // the ftz-ness of the surrounding code. sqrt_rn_f and sqrt_rn_ftz_f are
  199. // the versions with explicit ftz-ness.
  200. return {Intrinsic::sqrt, FTZ_Any};
  201. case Intrinsic::nvvm_sqrt_rn_f:
  202. return {Intrinsic::sqrt, FTZ_MustBeOff};
  203. case Intrinsic::nvvm_sqrt_rn_ftz_f:
  204. return {Intrinsic::sqrt, FTZ_MustBeOn};
  205. case Intrinsic::nvvm_trunc_d:
  206. return {Intrinsic::trunc, FTZ_Any};
  207. case Intrinsic::nvvm_trunc_f:
  208. return {Intrinsic::trunc, FTZ_MustBeOff};
  209. case Intrinsic::nvvm_trunc_ftz_f:
  210. return {Intrinsic::trunc, FTZ_MustBeOn};
  211. // NVVM intrinsics that map to LLVM cast operations.
  212. //
  213. // Note that llvm's target-generic conversion operators correspond to the rz
  214. // (round to zero) versions of the nvvm conversion intrinsics, even though
  215. // most everything else here uses the rn (round to nearest even) nvvm ops.
  216. case Intrinsic::nvvm_d2i_rz:
  217. case Intrinsic::nvvm_f2i_rz:
  218. case Intrinsic::nvvm_d2ll_rz:
  219. case Intrinsic::nvvm_f2ll_rz:
  220. return {Instruction::FPToSI};
  221. case Intrinsic::nvvm_d2ui_rz:
  222. case Intrinsic::nvvm_f2ui_rz:
  223. case Intrinsic::nvvm_d2ull_rz:
  224. case Intrinsic::nvvm_f2ull_rz:
  225. return {Instruction::FPToUI};
  226. case Intrinsic::nvvm_i2d_rz:
  227. case Intrinsic::nvvm_i2f_rz:
  228. case Intrinsic::nvvm_ll2d_rz:
  229. case Intrinsic::nvvm_ll2f_rz:
  230. return {Instruction::SIToFP};
  231. case Intrinsic::nvvm_ui2d_rz:
  232. case Intrinsic::nvvm_ui2f_rz:
  233. case Intrinsic::nvvm_ull2d_rz:
  234. case Intrinsic::nvvm_ull2f_rz:
  235. return {Instruction::UIToFP};
  236. // NVVM intrinsics that map to LLVM binary ops.
  237. case Intrinsic::nvvm_add_rn_d:
  238. return {Instruction::FAdd, FTZ_Any};
  239. case Intrinsic::nvvm_add_rn_f:
  240. return {Instruction::FAdd, FTZ_MustBeOff};
  241. case Intrinsic::nvvm_add_rn_ftz_f:
  242. return {Instruction::FAdd, FTZ_MustBeOn};
  243. case Intrinsic::nvvm_mul_rn_d:
  244. return {Instruction::FMul, FTZ_Any};
  245. case Intrinsic::nvvm_mul_rn_f:
  246. return {Instruction::FMul, FTZ_MustBeOff};
  247. case Intrinsic::nvvm_mul_rn_ftz_f:
  248. return {Instruction::FMul, FTZ_MustBeOn};
  249. case Intrinsic::nvvm_div_rn_d:
  250. return {Instruction::FDiv, FTZ_Any};
  251. case Intrinsic::nvvm_div_rn_f:
  252. return {Instruction::FDiv, FTZ_MustBeOff};
  253. case Intrinsic::nvvm_div_rn_ftz_f:
  254. return {Instruction::FDiv, FTZ_MustBeOn};
  255. // The remainder of cases are NVVM intrinsics that map to LLVM idioms, but
  256. // need special handling.
  257. //
  258. // We seem to be missing intrinsics for rcp.approx.{ftz.}f32, which is just
  259. // as well.
  260. case Intrinsic::nvvm_rcp_rn_d:
  261. return {SPC_Reciprocal, FTZ_Any};
  262. case Intrinsic::nvvm_rcp_rn_f:
  263. return {SPC_Reciprocal, FTZ_MustBeOff};
  264. case Intrinsic::nvvm_rcp_rn_ftz_f:
  265. return {SPC_Reciprocal, FTZ_MustBeOn};
  266. // We do not currently simplify intrinsics that give an approximate
  267. // answer. These include:
  268. //
  269. // - nvvm_cos_approx_{f,ftz_f}
  270. // - nvvm_ex2_approx_{d,f,ftz_f}
  271. // - nvvm_lg2_approx_{d,f,ftz_f}
  272. // - nvvm_sin_approx_{f,ftz_f}
  273. // - nvvm_sqrt_approx_{f,ftz_f}
  274. // - nvvm_rsqrt_approx_{d,f,ftz_f}
  275. // - nvvm_div_approx_{ftz_d,ftz_f,f}
  276. // - nvvm_rcp_approx_ftz_d
  277. //
  278. // Ideally we'd encode them as e.g. "fast call @llvm.cos", where "fast"
  279. // means that fastmath is enabled in the intrinsic. Unfortunately only
  280. // binary operators (currently) have a fastmath bit in SelectionDAG, so
  281. // this information gets lost and we can't select on it.
  282. //
  283. // TODO: div and rcp are lowered to a binary op, so these we could in
  284. // theory lower them to "fast fdiv".
  285. default:
  286. return {};
  287. }
  288. }();
  289. // If Action.FtzRequirementTy is not satisfied by the module's ftz state, we
  290. // can bail out now. (Notice that in the case that IID is not an NVVM
  291. // intrinsic, we don't have to look up any module metadata, as
  292. // FtzRequirementTy will be FTZ_Any.)
  293. if (Action.FtzRequirement != FTZ_Any) {
  294. StringRef Attr = II->getFunction()
  295. ->getFnAttribute("denormal-fp-math-f32")
  296. .getValueAsString();
  297. DenormalMode Mode = parseDenormalFPAttribute(Attr);
  298. bool FtzEnabled = Mode.Output != DenormalMode::IEEE;
  299. if (FtzEnabled != (Action.FtzRequirement == FTZ_MustBeOn))
  300. return nullptr;
  301. }
  302. // Simplify to target-generic intrinsic.
  303. if (Action.IID) {
  304. SmallVector<Value *, 4> Args(II->args());
  305. // All the target-generic intrinsics currently of interest to us have one
  306. // type argument, equal to that of the nvvm intrinsic's argument.
  307. Type *Tys[] = {II->getArgOperand(0)->getType()};
  308. return CallInst::Create(
  309. Intrinsic::getDeclaration(II->getModule(), *Action.IID, Tys), Args);
  310. }
  311. // Simplify to target-generic binary op.
  312. if (Action.BinaryOp)
  313. return BinaryOperator::Create(*Action.BinaryOp, II->getArgOperand(0),
  314. II->getArgOperand(1), II->getName());
  315. // Simplify to target-generic cast op.
  316. if (Action.CastOp)
  317. return CastInst::Create(*Action.CastOp, II->getArgOperand(0), II->getType(),
  318. II->getName());
  319. // All that's left are the special cases.
  320. if (!Action.Special)
  321. return nullptr;
  322. switch (*Action.Special) {
  323. case SPC_Reciprocal:
  324. // Simplify reciprocal.
  325. return BinaryOperator::Create(
  326. Instruction::FDiv, ConstantFP::get(II->getArgOperand(0)->getType(), 1),
  327. II->getArgOperand(0), II->getName());
  328. }
  329. llvm_unreachable("All SpecialCase enumerators should be handled in switch.");
  330. }
  331. Optional<Instruction *>
  332. NVPTXTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
  333. if (Instruction *I = simplifyNvvmIntrinsic(&II, IC)) {
  334. return I;
  335. }
  336. return None;
  337. }
  338. InstructionCost NVPTXTTIImpl::getArithmeticInstrCost(
  339. unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
  340. TTI::OperandValueKind Opd1Info, TTI::OperandValueKind Opd2Info,
  341. TTI::OperandValueProperties Opd1PropInfo,
  342. TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args,
  343. const Instruction *CxtI) {
  344. // Legalize the type.
  345. std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
  346. int ISD = TLI->InstructionOpcodeToISD(Opcode);
  347. switch (ISD) {
  348. default:
  349. return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info,
  350. Opd2Info,
  351. Opd1PropInfo, Opd2PropInfo);
  352. case ISD::ADD:
  353. case ISD::MUL:
  354. case ISD::XOR:
  355. case ISD::OR:
  356. case ISD::AND:
  357. // The machine code (SASS) simulates an i64 with two i32. Therefore, we
  358. // estimate that arithmetic operations on i64 are twice as expensive as
  359. // those on types that can fit into one machine register.
  360. if (LT.second.SimpleTy == MVT::i64)
  361. return 2 * LT.first;
  362. // Delegate other cases to the basic TTI.
  363. return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info,
  364. Opd2Info,
  365. Opd1PropInfo, Opd2PropInfo);
  366. }
  367. }
  368. void NVPTXTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
  369. TTI::UnrollingPreferences &UP,
  370. OptimizationRemarkEmitter *ORE) {
  371. BaseT::getUnrollingPreferences(L, SE, UP, ORE);
  372. // Enable partial unrolling and runtime unrolling, but reduce the
  373. // threshold. This partially unrolls small loops which are often
  374. // unrolled by the PTX to SASS compiler and unrolling earlier can be
  375. // beneficial.
  376. UP.Partial = UP.Runtime = true;
  377. UP.PartialThreshold = UP.Threshold / 4;
  378. }
  379. void NVPTXTTIImpl::getPeelingPreferences(Loop *L, ScalarEvolution &SE,
  380. TTI::PeelingPreferences &PP) {
  381. BaseT::getPeelingPreferences(L, SE, PP);
  382. }