HLOperations.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // HLOperations.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Implentation of High Level DXIL operations. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include <string>
  13. namespace llvm {
  14. class Module;
  15. class Function;
  16. class CallInst;
  17. class Argument;
  18. class StringRef;
  19. class FunctionType;
  20. }
  21. namespace hlsl {
  22. enum class HLOpcodeGroup {
  23. NotHL,
  24. HLExtIntrinsic,
  25. HLIntrinsic,
  26. HLCast,
  27. HLInit,
  28. HLBinOp,
  29. HLUnOp,
  30. HLSubscript,
  31. HLMatLoadStore,
  32. HLSelect,
  33. HLCreateHandle,
  34. NumOfHLOps
  35. };
  36. enum class HLBinaryOpcode {
  37. Invalid,
  38. Mul,
  39. Div,
  40. Rem,
  41. Add,
  42. Sub,
  43. Shl,
  44. Shr,
  45. LT,
  46. GT,
  47. LE,
  48. GE,
  49. EQ,
  50. NE,
  51. And,
  52. Xor,
  53. Or,
  54. LAnd,
  55. LOr,
  56. UDiv,
  57. URem,
  58. UShr,
  59. ULT,
  60. UGT,
  61. ULE,
  62. UGE,
  63. NumOfBO,
  64. };
  65. enum class HLUnaryOpcode {
  66. Invalid,
  67. PostInc,
  68. PostDec,
  69. PreInc,
  70. PreDec,
  71. Plus,
  72. Minus,
  73. Not,
  74. LNot,
  75. NumOfUO,
  76. };
  77. enum class HLSubscriptOpcode {
  78. DefaultSubscript,
  79. ColMatSubscript,
  80. RowMatSubscript,
  81. ColMatElement,
  82. RowMatElement,
  83. DoubleSubscript,
  84. CBufferSubscript,
  85. VectorSubscript, // Only for bool vector, other vector type will use GEP directly.
  86. };
  87. enum class HLCastOpcode {
  88. DefaultCast,
  89. UnsignedUnsignedCast,
  90. FromUnsignedCast,
  91. ToUnsignedCast,
  92. ColMatrixToVecCast,
  93. RowMatrixToVecCast,
  94. ColMatrixToRowMatrix,
  95. RowMatrixToColMatrix,
  96. HandleToResCast,
  97. };
  98. enum class HLMatLoadStoreOpcode {
  99. ColMatLoad,
  100. ColMatStore,
  101. RowMatLoad,
  102. RowMatStore,
  103. };
  104. extern const char * const HLPrefix;
  105. HLOpcodeGroup GetHLOpcodeGroup(llvm::Function *F);
  106. HLOpcodeGroup GetHLOpcodeGroupByName(const llvm::Function *F);
  107. llvm::StringRef GetHLOpcodeGroupNameByAttr(llvm::Function *F);
  108. llvm::StringRef GetHLLowerStrategy(llvm::Function *F);
  109. unsigned GetHLOpcode(const llvm::CallInst *CI);
  110. unsigned GetRowMajorOpcode(HLOpcodeGroup group, unsigned opcode);
  111. void SetHLLowerStrategy(llvm::Function *F, llvm::StringRef S);
  112. // For intrinsic opcode.
  113. bool HasUnsignedOpcode(unsigned opcode);
  114. unsigned GetUnsignedOpcode(unsigned opcode);
  115. // For HLBinaryOpcode.
  116. bool HasUnsignedOpcode(HLBinaryOpcode opcode);
  117. HLBinaryOpcode GetUnsignedOpcode(HLBinaryOpcode opcode);
  118. llvm::StringRef GetHLOpcodeGroupName(HLOpcodeGroup op);
  119. namespace HLOperandIndex {
  120. // Opcode parameter.
  121. const unsigned kOpcodeIdx = 0;
  122. // Matrix store.
  123. const unsigned kMatStoreDstPtrOpIdx = 1;
  124. const unsigned kMatStoreValOpIdx = 2;
  125. // Matrix load.
  126. const unsigned kMatLoadPtrOpIdx = 1;
  127. // Normal subscipts.
  128. const unsigned kSubscriptObjectOpIdx = 1;
  129. const unsigned kSubscriptIndexOpIdx = 2;
  130. // Double subscripts.
  131. const unsigned kDoubleSubscriptMipLevelOpIdx = 3;
  132. // Matrix subscripts.
  133. const unsigned kMatSubscriptMatOpIdx = 1;
  134. const unsigned kMatSubscriptSubOpIdx = 2;
  135. // Matrix init.
  136. const unsigned kMatArrayInitMatOpIdx = 1;
  137. const unsigned kMatArrayInitFirstArgOpIdx = 2;
  138. // Array Init.
  139. const unsigned kArrayInitPtrOpIdx = 1;
  140. const unsigned kArrayInitFirstArgOpIdx = 2;
  141. // Normal Init.
  142. const unsigned kInitFirstArgOpIdx = 1;
  143. // Unary operators.
  144. const unsigned kUnaryOpSrc0Idx = 1;
  145. // Binary operators.
  146. const unsigned kBinaryOpSrc0Idx = 1;
  147. const unsigned kBinaryOpSrc1Idx = 2;
  148. // Trinary operators.
  149. const unsigned kTrinaryOpSrc0Idx = 1;
  150. const unsigned kTrinaryOpSrc1Idx = 2;
  151. const unsigned kTrinaryOpSrc2Idx = 3;
  152. // Interlocked.
  153. const unsigned kInterlockedDestOpIndex = 1;
  154. const unsigned kInterlockedValueOpIndex = 2;
  155. const unsigned kInterlockedOriginalValueOpIndex = 3;
  156. // InterlockedCompareExchange.
  157. const unsigned kInterlockedCmpDestOpIndex = 1;
  158. const unsigned kInterlockedCmpCompareValueOpIndex = 2;
  159. const unsigned kInterlockedCmpValueOpIndex = 3;
  160. const unsigned kInterlockedCmpOriginalValueOpIndex = 4;
  161. // Lerp.
  162. const unsigned kLerpOpXIdx = 1;
  163. const unsigned kLerpOpYIdx = 2;
  164. const unsigned kLerpOpSIdx = 3;
  165. // ProcessTessFactorIsoline.
  166. const unsigned kProcessTessFactorRawDetailFactor = 1;
  167. const unsigned kProcessTessFactorRawDensityFactor = 2;
  168. const unsigned kProcessTessFactorRoundedDetailFactor = 3;
  169. const unsigned kProcessTessFactorRoundedDensityFactor = 4;
  170. // ProcessTessFactor.
  171. const unsigned kProcessTessFactorRawEdgeFactor = 1;
  172. const unsigned kProcessTessFactorInsideScale = 2;
  173. const unsigned kProcessTessFactorRoundedEdgeFactor = 3;
  174. const unsigned kProcessTessFactorRoundedInsideFactor = 4;
  175. const unsigned kProcessTessFactorUnRoundedInsideFactor = 5;
  176. // Reflect.
  177. const unsigned kReflectOpIIdx = 1;
  178. const unsigned kReflectOpNIdx = 2;
  179. // Refract
  180. const unsigned kRefractOpIIdx = 1;
  181. const unsigned kRefractOpNIdx = 2;
  182. const unsigned kRefractOpEtaIdx = 3;
  183. // SmoothStep.
  184. const unsigned kSmoothStepOpMinIdx = 1;
  185. const unsigned kSmoothStepOpMaxIdx = 2;
  186. const unsigned kSmoothStepOpXIdx = 3;
  187. // Clamp
  188. const unsigned kClampOpXIdx = 1;
  189. const unsigned kClampOpMinIdx = 2;
  190. const unsigned kClampOpMaxIdx = 3;
  191. // Object functions.
  192. const unsigned kHandleOpIdx = 1;
  193. // Store.
  194. const unsigned kStoreOffsetOpIdx = 2;
  195. const unsigned kStoreValOpIdx = 3;
  196. // Load.
  197. const unsigned kBufLoadAddrOpIdx = 2;
  198. const unsigned kBufLoadStatusOpIdx = 3;
  199. const unsigned kRWTexLoadStatusOpIdx = 3;
  200. const unsigned kTexLoadOffsetOpIdx = 3;
  201. const unsigned kTexLoadStatusOpIdx = 4;
  202. // Load for Texture2DMS
  203. const unsigned kTex2DMSLoadSampleIdxOpIdx = 3;
  204. const unsigned kTex2DMSLoadOffsetOpIdx = 4;
  205. const unsigned kTex2DMSLoadStatusOpIdx = 5;
  206. // mips.Operator.
  207. const unsigned kMipLoadAddrOpIdx = 3;
  208. const unsigned kMipLoadOffsetOpIdx = 4;
  209. const unsigned kMipLoadStatusOpIdx = 5;
  210. // Sample.
  211. const unsigned kSampleSamplerArgIndex = 2;
  212. const unsigned kSampleCoordArgIndex = 3;
  213. const unsigned kSampleOffsetArgIndex = 4;
  214. const unsigned kSampleClampArgIndex = 5;
  215. const unsigned kSampleStatusArgIndex = 6;
  216. // SampleG.
  217. const unsigned kSampleGDDXArgIndex = 4;
  218. const unsigned kSampleGDDYArgIndex = 5;
  219. const unsigned kSampleGOffsetArgIndex = 6;
  220. const unsigned kSampleGClampArgIndex = 7;
  221. const unsigned kSampleGStatusArgIndex = 8;
  222. // SampleCmp.
  223. const unsigned kSampleCmpCmpValArgIndex = 4;
  224. const unsigned kSampleCmpOffsetArgIndex = 5;
  225. const unsigned kSampleCmpClampArgIndex = 6;
  226. const unsigned kSampleCmpStatusArgIndex = 7;
  227. // SampleBias.
  228. const unsigned kSampleBBiasArgIndex = 4;
  229. const unsigned kSampleBOffsetArgIndex = 5;
  230. const unsigned kSampleBClampArgIndex = 6;
  231. const unsigned kSampleBStatusArgIndex = 7;
  232. // SampleLevel.
  233. const unsigned kSampleLLevelArgIndex = 4;
  234. const unsigned kSampleLOffsetArgIndex = 5;
  235. const unsigned kSampleLStatusArgIndex = 6;
  236. // SampleCmpLevelZero.
  237. const unsigned kSampleCmpLZCmpValArgIndex = 4;
  238. const unsigned kSampleCmpLZOffsetArgIndex = 5;
  239. const unsigned kSampleCmpLZStatusArgIndex = 6;
  240. // Gather.
  241. const unsigned kGatherSamplerArgIndex = 2;
  242. const unsigned kGatherCoordArgIndex = 3;
  243. const unsigned kGatherOffsetArgIndex = 4;
  244. const unsigned kGatherStatusArgIndex = 5;
  245. const unsigned kGatherSampleOffsetArgIndex = 5;
  246. const unsigned kGatherStatusWithSampleOffsetArgIndex = 8;
  247. // GatherCmp.
  248. const unsigned kGatherCmpCmpValArgIndex = 4;
  249. const unsigned kGatherCmpOffsetArgIndex = 5;
  250. const unsigned kGatherCmpStatusArgIndex = 6;
  251. const unsigned kGatherCmpSampleOffsetArgIndex = 6;
  252. const unsigned kGatherCmpStatusWithSampleOffsetArgIndex = 9;
  253. // StreamAppend.
  254. const unsigned kStreamAppendStreamOpIndex = 1;
  255. const unsigned kStreamAppendDataOpIndex = 2;
  256. // Append.
  257. const unsigned kAppendValOpIndex = 2;
  258. // Interlocked.
  259. const unsigned kObjectInterlockedDestOpIndex = 2;
  260. const unsigned kObjectInterlockedValueOpIndex = 3;
  261. const unsigned kObjectInterlockedOriginalValueOpIndex = 4;
  262. // InterlockedCompareExchange.
  263. const unsigned kObjectInterlockedCmpDestOpIndex = 2;
  264. const unsigned kObjectInterlockedCmpCompareValueOpIndex = 3;
  265. const unsigned kObjectInterlockedCmpValueOpIndex = 4;
  266. const unsigned kObjectInterlockedCmpOriginalValueOpIndex = 5;
  267. // GetSamplePosition.
  268. const unsigned kGetSamplePositionSampleIdxOpIndex = 2;
  269. // GetDimensions.
  270. const unsigned kGetDimensionsMipLevelOpIndex = 2;
  271. const unsigned kGetDimensionsMipWidthOpIndex = 3;
  272. const unsigned kGetDimensionsNoMipWidthOpIndex = 2;
  273. // WaveAllEqual.
  274. const unsigned kWaveAllEqualValueOpIdx = 1;
  275. // CreateHandle.
  276. const unsigned kCreateHandleResourceOpIdx = 1;
  277. const unsigned kCreateHandleIndexOpIdx = 2; // Only for array of cbuffer.
  278. } // namespace HLOperandIndex
  279. llvm::Function *GetOrCreateHLFunction(llvm::Module &M,
  280. llvm::FunctionType *funcTy,
  281. HLOpcodeGroup group, unsigned opcode);
  282. llvm::Function *GetOrCreateHLFunction(llvm::Module &M,
  283. llvm::FunctionType *funcTy,
  284. HLOpcodeGroup group,
  285. llvm::StringRef *groupName,
  286. llvm::StringRef *fnName,
  287. unsigned opcode);
  288. llvm::Function *GetOrCreateHLFunctionWithBody(llvm::Module &M,
  289. llvm::FunctionType *funcTy,
  290. HLOpcodeGroup group,
  291. unsigned opcode,
  292. llvm::StringRef name);
  293. } // namespace hlsl