DxilOperations.cpp 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilOperations.cpp //
  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. // Implementation of DXIL operation tables. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "dxc/HLSL/DxilOperations.h"
  12. #include "dxc/Support/Global.h"
  13. #include "dxc/HLSL/DxilModule.h"
  14. #include "dxc/HLSL/HLModule.h"
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/IR/LLVMContext.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/IR/Type.h"
  19. #include "llvm/IR/Constants.h"
  20. #include "llvm/IR/Instructions.h"
  21. using namespace llvm;
  22. using std::vector;
  23. using std::string;
  24. namespace hlsl {
  25. using OC = OP::OpCode;
  26. using OCC = OP::OpCodeClass;
  27. //------------------------------------------------------------------------------
  28. //
  29. // OP class const-static data and related static methods.
  30. //
  31. /* <py>
  32. import hctdb_instrhelp
  33. </py> */
  34. /* <py::lines('OPCODE-OLOADS')>hctdb_instrhelp.get_oloads_props()</py>*/
  35. // OPCODE-OLOADS:BEGIN
  36. const OP::OpCodeProperty OP::m_OpCodeProps[(unsigned)OP::OpCode::NumOpCodes] = {
  37. // OpCode OpCode name, OpCodeClass OpCodeClass name, void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  38. // Temporary, indexable, input, output registers void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  39. { OC::TempRegLoad, "TempRegLoad", OCC::TempRegLoad, "tempRegLoad", false, true, true, false, false, false, true, true, false, false, false, Attribute::ReadOnly, },
  40. { OC::TempRegStore, "TempRegStore", OCC::TempRegStore, "tempRegStore", false, true, true, false, false, false, true, true, false, false, false, Attribute::None, },
  41. { OC::MinPrecXRegLoad, "MinPrecXRegLoad", OCC::MinPrecXRegLoad, "minPrecXRegLoad", false, true, false, false, false, false, true, false, false, false, false, Attribute::ReadOnly, },
  42. { OC::MinPrecXRegStore, "MinPrecXRegStore", OCC::MinPrecXRegStore, "minPrecXRegStore", false, true, false, false, false, false, true, false, false, false, false, Attribute::None, },
  43. { OC::LoadInput, "LoadInput", OCC::LoadInput, "loadInput", false, true, true, false, false, false, true, true, false, false, false, Attribute::ReadNone, },
  44. { OC::StoreOutput, "StoreOutput", OCC::StoreOutput, "storeOutput", false, true, true, false, false, false, true, true, false, false, false, Attribute::None, },
  45. // Unary float void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  46. { OC::FAbs, "FAbs", OCC::Unary, "unary", false, true, true, true, false, false, false, false, false, false, false, Attribute::ReadNone, },
  47. { OC::Saturate, "Saturate", OCC::Unary, "unary", false, true, true, true, false, false, false, false, false, false, false, Attribute::ReadNone, },
  48. { OC::IsNaN, "IsNaN", OCC::IsSpecialFloat, "isSpecialFloat", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  49. { OC::IsInf, "IsInf", OCC::IsSpecialFloat, "isSpecialFloat", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  50. { OC::IsFinite, "IsFinite", OCC::IsSpecialFloat, "isSpecialFloat", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  51. { OC::IsNormal, "IsNormal", OCC::IsSpecialFloat, "isSpecialFloat", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  52. { OC::Cos, "Cos", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  53. { OC::Sin, "Sin", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  54. { OC::Tan, "Tan", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  55. { OC::Acos, "Acos", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  56. { OC::Asin, "Asin", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  57. { OC::Atan, "Atan", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  58. { OC::Hcos, "Hcos", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  59. { OC::Hsin, "Hsin", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  60. { OC::Htan, "Htan", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  61. { OC::Exp, "Exp", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  62. { OC::Frc, "Frc", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  63. { OC::Log, "Log", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  64. { OC::Sqrt, "Sqrt", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  65. { OC::Rsqrt, "Rsqrt", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  66. // Unary float - rounding void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  67. { OC::Round_ne, "Round_ne", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  68. { OC::Round_ni, "Round_ni", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  69. { OC::Round_pi, "Round_pi", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  70. { OC::Round_z, "Round_z", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  71. // Unary int void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  72. { OC::Bfrev, "Bfrev", OCC::Unary, "unary", false, false, false, false, false, false, true, true, true, false, false, Attribute::ReadNone, },
  73. { OC::Countbits, "Countbits", OCC::UnaryBits, "unaryBits", false, false, false, false, false, false, true, true, true, false, false, Attribute::ReadNone, },
  74. { OC::FirstbitLo, "FirstbitLo", OCC::UnaryBits, "unaryBits", false, false, false, false, false, false, true, true, true, false, false, Attribute::ReadNone, },
  75. // Unary uint void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  76. { OC::FirstbitHi, "FirstbitHi", OCC::UnaryBits, "unaryBits", false, false, false, false, false, false, true, true, true, false, false, Attribute::ReadNone, },
  77. // Unary int void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  78. { OC::FirstbitSHi, "FirstbitSHi", OCC::UnaryBits, "unaryBits", false, false, false, false, false, false, true, true, true, false, false, Attribute::ReadNone, },
  79. // Binary float void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  80. { OC::FMax, "FMax", OCC::Binary, "binary", false, true, true, true, false, false, false, false, false, false, false, Attribute::ReadNone, },
  81. { OC::FMin, "FMin", OCC::Binary, "binary", false, true, true, true, false, false, false, false, false, false, false, Attribute::ReadNone, },
  82. // Binary int void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  83. { OC::IMax, "IMax", OCC::Binary, "binary", false, false, false, false, false, false, true, true, true, false, false, Attribute::ReadNone, },
  84. { OC::IMin, "IMin", OCC::Binary, "binary", false, false, false, false, false, false, true, true, true, false, false, Attribute::ReadNone, },
  85. // Binary uint void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  86. { OC::UMax, "UMax", OCC::Binary, "binary", false, false, false, false, false, false, true, true, true, false, false, Attribute::ReadNone, },
  87. { OC::UMin, "UMin", OCC::Binary, "binary", false, false, false, false, false, false, true, true, true, false, false, Attribute::ReadNone, },
  88. // Binary int with two outputs void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  89. { OC::IMul, "IMul", OCC::BinaryWithTwoOuts, "binaryWithTwoOuts", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  90. // Binary uint with two outputs void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  91. { OC::UMul, "UMul", OCC::BinaryWithTwoOuts, "binaryWithTwoOuts", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  92. { OC::UDiv, "UDiv", OCC::BinaryWithTwoOuts, "binaryWithTwoOuts", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  93. // Binary uint with carry or borrow void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  94. { OC::UAddc, "UAddc", OCC::BinaryWithCarryOrBorrow, "binaryWithCarryOrBorrow", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  95. { OC::USubb, "USubb", OCC::BinaryWithCarryOrBorrow, "binaryWithCarryOrBorrow", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  96. // Tertiary float void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  97. { OC::FMad, "FMad", OCC::Tertiary, "tertiary", false, true, true, true, false, false, false, false, false, false, false, Attribute::ReadNone, },
  98. { OC::Fma, "Fma", OCC::Tertiary, "tertiary", false, false, false, true, false, false, false, false, false, false, false, Attribute::ReadNone, },
  99. // Tertiary int void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  100. { OC::IMad, "IMad", OCC::Tertiary, "tertiary", false, false, false, false, false, false, true, true, true, false, false, Attribute::ReadNone, },
  101. // Tertiary uint void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  102. { OC::UMad, "UMad", OCC::Tertiary, "tertiary", false, false, false, false, false, false, true, true, true, false, false, Attribute::ReadNone, },
  103. // Tertiary int void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  104. { OC::Msad, "Msad", OCC::Tertiary, "tertiary", false, false, false, false, false, false, false, true, true, false, false, Attribute::ReadNone, },
  105. { OC::Ibfe, "Ibfe", OCC::Tertiary, "tertiary", false, false, false, false, false, false, false, true, true, false, false, Attribute::ReadNone, },
  106. // Tertiary uint void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  107. { OC::Ubfe, "Ubfe", OCC::Tertiary, "tertiary", false, false, false, false, false, false, false, true, true, false, false, Attribute::ReadNone, },
  108. // Quaternary void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  109. { OC::Bfi, "Bfi", OCC::Quaternary, "quaternary", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  110. // Dot void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  111. { OC::Dot2, "Dot2", OCC::Dot2, "dot2", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  112. { OC::Dot3, "Dot3", OCC::Dot3, "dot3", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  113. { OC::Dot4, "Dot4", OCC::Dot4, "dot4", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  114. // Resources void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  115. { OC::CreateHandle, "CreateHandle", OCC::CreateHandle, "createHandle", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  116. { OC::CBufferLoad, "CBufferLoad", OCC::CBufferLoad, "cbufferLoad", false, true, true, true, false, true, true, true, true, false, false, Attribute::ReadOnly, },
  117. { OC::CBufferLoadLegacy, "CBufferLoadLegacy", OCC::CBufferLoadLegacy, "cbufferLoadLegacy", false, true, true, true, false, false, true, true, true, false, false, Attribute::ReadOnly, },
  118. // Resources - sample void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  119. { OC::Sample, "Sample", OCC::Sample, "sample", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  120. { OC::SampleBias, "SampleBias", OCC::SampleBias, "sampleBias", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  121. { OC::SampleLevel, "SampleLevel", OCC::SampleLevel, "sampleLevel", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  122. { OC::SampleGrad, "SampleGrad", OCC::SampleGrad, "sampleGrad", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  123. { OC::SampleCmp, "SampleCmp", OCC::SampleCmp, "sampleCmp", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  124. { OC::SampleCmpLevelZero, "SampleCmpLevelZero", OCC::SampleCmpLevelZero, "sampleCmpLevelZero", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  125. // Resources void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  126. { OC::TextureLoad, "TextureLoad", OCC::TextureLoad, "textureLoad", false, true, true, false, false, false, true, true, false, false, false, Attribute::ReadOnly, },
  127. { OC::TextureStore, "TextureStore", OCC::TextureStore, "textureStore", false, true, true, false, false, false, true, true, false, false, false, Attribute::None, },
  128. { OC::BufferLoad, "BufferLoad", OCC::BufferLoad, "bufferLoad", false, true, true, false, false, false, true, true, false, false, false, Attribute::ReadOnly, },
  129. { OC::BufferStore, "BufferStore", OCC::BufferStore, "bufferStore", false, true, true, false, false, false, true, true, false, false, false, Attribute::None, },
  130. { OC::BufferUpdateCounter, "BufferUpdateCounter", OCC::BufferUpdateCounter, "bufferUpdateCounter", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  131. { OC::CheckAccessFullyMapped, "CheckAccessFullyMapped", OCC::CheckAccessFullyMapped, "checkAccessFullyMapped", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadOnly, },
  132. { OC::GetDimensions, "GetDimensions", OCC::GetDimensions, "getDimensions", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  133. // Resources - gather void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  134. { OC::TextureGather, "TextureGather", OCC::TextureGather, "textureGather", false, true, true, false, false, false, true, true, false, false, false, Attribute::ReadOnly, },
  135. { OC::TextureGatherCmp, "TextureGatherCmp", OCC::TextureGatherCmp, "textureGatherCmp", false, true, true, false, false, false, true, true, false, false, false, Attribute::ReadOnly, },
  136. // Resources - sample void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  137. { OC::Texture2DMSGetSamplePosition, "Texture2DMSGetSamplePosition", OCC::Texture2DMSGetSamplePosition, "texture2DMSGetSamplePosition", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  138. { OC::RenderTargetGetSamplePosition, "RenderTargetGetSamplePosition", OCC::RenderTargetGetSamplePosition, "renderTargetGetSamplePosition", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  139. { OC::RenderTargetGetSampleCount, "RenderTargetGetSampleCount", OCC::RenderTargetGetSampleCount, "renderTargetGetSampleCount", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  140. // Synchronization void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  141. { OC::AtomicBinOp, "AtomicBinOp", OCC::AtomicBinOp, "atomicBinOp", false, false, false, false, false, false, false, true, false, false, false, Attribute::None, },
  142. { OC::AtomicCompareExchange, "AtomicCompareExchange", OCC::AtomicCompareExchange, "atomicCompareExchange", false, false, false, false, false, false, false, true, false, false, false, Attribute::None, },
  143. { OC::Barrier, "Barrier", OCC::Barrier, "barrier", true, false, false, false, false, false, false, false, false, false, false, Attribute::NoDuplicate, },
  144. // Pixel shader void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  145. { OC::CalculateLOD, "CalculateLOD", OCC::CalculateLOD, "calculateLOD", false, false, true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  146. { OC::Discard, "Discard", OCC::Discard, "discard", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  147. { OC::DerivCoarseX, "DerivCoarseX", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  148. { OC::DerivCoarseY, "DerivCoarseY", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  149. { OC::DerivFineX, "DerivFineX", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  150. { OC::DerivFineY, "DerivFineY", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  151. { OC::EvalSnapped, "EvalSnapped", OCC::EvalSnapped, "evalSnapped", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  152. { OC::EvalSampleIndex, "EvalSampleIndex", OCC::EvalSampleIndex, "evalSampleIndex", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  153. { OC::EvalCentroid, "EvalCentroid", OCC::EvalCentroid, "evalCentroid", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  154. { OC::SampleIndex, "SampleIndex", OCC::SampleIndex, "sampleIndex", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  155. { OC::Coverage, "Coverage", OCC::Coverage, "coverage", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  156. { OC::InnerCoverage, "InnerCoverage", OCC::InnerCoverage, "innerCoverage", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  157. // Compute shader void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  158. { OC::ThreadId, "ThreadId", OCC::ThreadId, "threadId", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  159. { OC::GroupId, "GroupId", OCC::GroupId, "groupId", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  160. { OC::ThreadIdInGroup, "ThreadIdInGroup", OCC::ThreadIdInGroup, "threadIdInGroup", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  161. { OC::FlattenedThreadIdInGroup, "FlattenedThreadIdInGroup", OCC::FlattenedThreadIdInGroup, "flattenedThreadIdInGroup", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  162. // Geometry shader void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  163. { OC::EmitStream, "EmitStream", OCC::EmitStream, "emitStream", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  164. { OC::CutStream, "CutStream", OCC::CutStream, "cutStream", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  165. { OC::EmitThenCutStream, "EmitThenCutStream", OCC::EmitThenCutStream, "emitThenCutStream", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  166. { OC::GSInstanceID, "GSInstanceID", OCC::GSInstanceID, "gsInstanceID", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  167. // Double precision void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  168. { OC::MakeDouble, "MakeDouble", OCC::MakeDouble, "makeDouble", false, false, false, true, false, false, false, false, false, false, false, Attribute::ReadNone, },
  169. { OC::SplitDouble, "SplitDouble", OCC::SplitDouble, "splitDouble", false, false, false, true, false, false, false, false, false, false, false, Attribute::ReadNone, },
  170. // Domain and hull shader void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  171. { OC::LoadOutputControlPoint, "LoadOutputControlPoint", OCC::LoadOutputControlPoint, "loadOutputControlPoint", false, true, true, false, false, false, true, true, false, false, false, Attribute::ReadNone, },
  172. { OC::LoadPatchConstant, "LoadPatchConstant", OCC::LoadPatchConstant, "loadPatchConstant", false, true, true, false, false, false, true, true, false, false, false, Attribute::ReadNone, },
  173. // Domain shader void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  174. { OC::DomainLocation, "DomainLocation", OCC::DomainLocation, "domainLocation", false, false, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  175. // Hull shader void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  176. { OC::StorePatchConstant, "StorePatchConstant", OCC::StorePatchConstant, "storePatchConstant", false, true, true, false, false, false, true, true, false, false, false, Attribute::None, },
  177. { OC::OutputControlPointID, "OutputControlPointID", OCC::OutputControlPointID, "outputControlPointID", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  178. { OC::PrimitiveID, "PrimitiveID", OCC::PrimitiveID, "primitiveID", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  179. // Other void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  180. { OC::CycleCounterLegacy, "CycleCounterLegacy", OCC::CycleCounterLegacy, "cycleCounterLegacy", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  181. // Wave void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  182. { OC::WaveIsFirstLane, "WaveIsFirstLane", OCC::WaveIsFirstLane, "waveIsFirstLane", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  183. { OC::WaveGetLaneIndex, "WaveGetLaneIndex", OCC::WaveGetLaneIndex, "waveGetLaneIndex", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  184. { OC::WaveGetLaneCount, "WaveGetLaneCount", OCC::WaveGetLaneCount, "waveGetLaneCount", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  185. { OC::WaveAnyTrue, "WaveAnyTrue", OCC::WaveAnyTrue, "waveAnyTrue", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  186. { OC::WaveAllTrue, "WaveAllTrue", OCC::WaveAllTrue, "waveAllTrue", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  187. { OC::WaveActiveAllEqual, "WaveActiveAllEqual", OCC::WaveActiveAllEqual, "waveActiveAllEqual", false, true, true, true, true, true, true, true, true, false, false, Attribute::None, },
  188. { OC::WaveActiveBallot, "WaveActiveBallot", OCC::WaveActiveBallot, "waveActiveBallot", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  189. { OC::WaveReadLaneAt, "WaveReadLaneAt", OCC::WaveReadLaneAt, "waveReadLaneAt", false, true, true, true, true, true, true, true, true, false, false, Attribute::None, },
  190. { OC::WaveReadLaneFirst, "WaveReadLaneFirst", OCC::WaveReadLaneFirst, "waveReadLaneFirst", false, true, true, false, true, true, true, true, true, false, false, Attribute::None, },
  191. { OC::WaveActiveOp, "WaveActiveOp", OCC::WaveActiveOp, "waveActiveOp", false, true, true, true, true, true, true, true, true, false, false, Attribute::None, },
  192. { OC::WaveActiveBit, "WaveActiveBit", OCC::WaveActiveBit, "waveActiveBit", false, false, false, false, false, true, true, true, true, false, false, Attribute::None, },
  193. { OC::WavePrefixOp, "WavePrefixOp", OCC::WavePrefixOp, "wavePrefixOp", false, true, true, true, false, true, true, true, true, false, false, Attribute::None, },
  194. { OC::QuadReadLaneAt, "QuadReadLaneAt", OCC::QuadReadLaneAt, "quadReadLaneAt", false, true, true, true, true, true, true, true, true, false, false, Attribute::None, },
  195. { OC::QuadOp, "QuadOp", OCC::QuadOp, "quadOp", false, true, true, true, false, true, true, true, true, false, false, Attribute::None, },
  196. // Bitcasts with different sizes void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  197. { OC::BitcastI16toF16, "BitcastI16toF16", OCC::BitcastI16toF16, "bitcastI16toF16", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  198. { OC::BitcastF16toI16, "BitcastF16toI16", OCC::BitcastF16toI16, "bitcastF16toI16", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  199. { OC::BitcastI32toF32, "BitcastI32toF32", OCC::BitcastI32toF32, "bitcastI32toF32", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  200. { OC::BitcastF32toI32, "BitcastF32toI32", OCC::BitcastF32toI32, "bitcastF32toI32", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  201. { OC::BitcastI64toF64, "BitcastI64toF64", OCC::BitcastI64toF64, "bitcastI64toF64", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  202. { OC::BitcastF64toI64, "BitcastF64toI64", OCC::BitcastF64toI64, "bitcastF64toI64", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  203. // Legacy floating-point void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  204. { OC::LegacyF32ToF16, "LegacyF32ToF16", OCC::LegacyF32ToF16, "legacyF32ToF16", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  205. { OC::LegacyF16ToF32, "LegacyF16ToF32", OCC::LegacyF16ToF32, "legacyF16ToF32", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  206. // Double precision void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  207. { OC::LegacyDoubleToFloat, "LegacyDoubleToFloat", OCC::LegacyDoubleToFloat, "legacyDoubleToFloat", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  208. { OC::LegacyDoubleToSInt32, "LegacyDoubleToSInt32", OCC::LegacyDoubleToSInt32, "legacyDoubleToSInt32", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  209. { OC::LegacyDoubleToUInt32, "LegacyDoubleToUInt32", OCC::LegacyDoubleToUInt32, "legacyDoubleToUInt32", true, false, false, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  210. // Wave void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  211. { OC::WaveAllBitCount, "WaveAllBitCount", OCC::WaveAllOp, "waveAllOp", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  212. { OC::WavePrefixBitCount, "WavePrefixBitCount", OCC::WavePrefixOp, "wavePrefixOp", true, false, false, false, false, false, false, false, false, false, false, Attribute::None, },
  213. // Pixel shader void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  214. { OC::AttributeAtVertex, "AttributeAtVertex", OCC::AttributeAtVertex, "attributeAtVertex", false, true, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  215. // Graphics shader void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  216. { OC::ViewID, "ViewID", OCC::ViewID, "viewID", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  217. // Resources void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  218. { OC::RawBufferLoad, "RawBufferLoad", OCC::RawBufferLoad, "rawBufferLoad", false, true, true, false, false, false, true, true, false, false, false, Attribute::ReadOnly, },
  219. { OC::RawBufferStore, "RawBufferStore", OCC::RawBufferStore, "rawBufferStore", false, true, true, false, false, false, true, true, false, false, false, Attribute::None, },
  220. // Raytracing uint System Values void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  221. { OC::InstanceID, "InstanceID", OCC::InstanceID, "instanceID", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  222. { OC::InstanceIndex, "InstanceIndex", OCC::InstanceIndex, "instanceIndex", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  223. { OC::HitKind, "HitKind", OCC::HitKind, "hitKind", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  224. { OC::RayFlags, "RayFlags", OCC::RayFlags, "rayFlags", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  225. // Ray Dispatch Arguments void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  226. { OC::DispatchRaysIndex, "DispatchRaysIndex", OCC::DispatchRaysIndex, "dispatchRaysIndex", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  227. { OC::DispatchRaysDimensions, "DispatchRaysDimensions", OCC::DispatchRaysDimensions, "dispatchRaysDimensions", false, false, false, false, false, false, false, true, false, false, false, Attribute::ReadNone, },
  228. // Ray Vectors void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  229. { OC::WorldRayOrigin, "WorldRayOrigin", OCC::WorldRayOrigin, "worldRayOrigin", false, false, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  230. { OC::WorldRayDirection, "WorldRayDirection", OCC::WorldRayDirection, "worldRayDirection", false, false, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  231. { OC::ObjectRayOrigin, "ObjectRayOrigin", OCC::ObjectRayOrigin, "objectRayOrigin", false, false, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  232. { OC::ObjectRayDirection, "ObjectRayDirection", OCC::ObjectRayDirection, "objectRayDirection", false, false, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  233. // Ray Transforms void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  234. { OC::ObjectToWorld, "ObjectToWorld", OCC::ObjectToWorld, "objectToWorld", false, false, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  235. { OC::WorldToObject, "WorldToObject", OCC::WorldToObject, "worldToObject", false, false, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  236. // RayT void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  237. { OC::RayTMin, "RayTMin", OCC::RayTMin, "rayTMin", false, false, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  238. { OC::RayTCurrent, "RayTCurrent", OCC::RayTCurrent, "rayTCurrent", false, false, true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  239. // AnyHit Terminals void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  240. { OC::IgnoreHit, "IgnoreHit", OCC::IgnoreHit, "ignoreHit", true, false, false, false, false, false, false, false, false, false, false, Attribute::NoReturn, },
  241. { OC::AcceptHitAndEndSearch, "AcceptHitAndEndSearch", OCC::AcceptHitAndEndSearch, "acceptHitAndEndSearch", true, false, false, false, false, false, false, false, false, false, false, Attribute::NoReturn, },
  242. // Indirect Shader Invocation void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  243. { OC::TraceRay, "TraceRay", OCC::TraceRay, "traceRay", false, false, false, false, false, false, false, false, false, true, false, Attribute::None, },
  244. { OC::ReportHit, "ReportHit", OCC::ReportHit, "reportHit", false, false, false, false, false, false, false, false, false, true, false, Attribute::None, },
  245. { OC::CallShader, "CallShader", OCC::CallShader, "callShader", false, false, false, false, false, false, false, false, false, true, false, Attribute::None, },
  246. // Library create handle from resource struct (like HL intrinsic) void, h, f, d, i1, i8, i16, i32, i64, udt, obj, function attribute
  247. { OC::CreateHandleFromResourceStructForLib, "CreateHandleFromResourceStructForLib", OCC::CreateHandleFromResourceStructForLib, "createHandleFromResourceStructForLib", false, false, false, false, false, false, false, false, false, false, true, Attribute::ReadOnly, },
  248. };
  249. // OPCODE-OLOADS:END
  250. const char *OP::m_OverloadTypeName[kNumTypeOverloads] = {
  251. "void", "f16", "f32", "f64", "i1", "i8", "i16", "i32", "i64", "udt",
  252. };
  253. const char *OP::m_NamePrefix = "dx.op.";
  254. const char *OP::m_TypePrefix = "dx.types.";
  255. const char *OP::m_MatrixTypePrefix = "class.matrix."; // Allowed in library
  256. // Keep sync with DXIL::AtomicBinOpCode
  257. static const char *AtomicBinOpCodeName[] = {
  258. "AtomicAdd",
  259. "AtomicAnd",
  260. "AtomicOr",
  261. "AtomicXor",
  262. "AtomicIMin",
  263. "AtomicIMax",
  264. "AtomicUMin",
  265. "AtomicUMax",
  266. "AtomicExchange",
  267. "AtomicInvalid" // Must be last.
  268. };
  269. unsigned OP::GetTypeSlot(Type *pType) {
  270. Type::TypeID T = pType->getTypeID();
  271. switch (T) {
  272. case Type::VoidTyID: return 0;
  273. case Type::HalfTyID: return 1;
  274. case Type::FloatTyID: return 2;
  275. case Type::DoubleTyID: return 3;
  276. case Type::IntegerTyID: {
  277. IntegerType *pIT = dyn_cast<IntegerType>(pType);
  278. unsigned Bits = pIT->getBitWidth();
  279. switch (Bits) {
  280. case 1: return 4;
  281. case 8: return 5;
  282. case 16: return 6;
  283. case 32: return 7;
  284. case 64: return 8;
  285. }
  286. }
  287. case Type::PointerTyID: return 9;
  288. case Type::StructTyID: return 10;
  289. default:
  290. break;
  291. }
  292. return UINT_MAX;
  293. }
  294. const char *OP::GetOverloadTypeName(unsigned TypeSlot) {
  295. DXASSERT(TypeSlot < kUserDefineTypeSlot, "otherwise caller passed OOB index");
  296. return m_OverloadTypeName[TypeSlot];
  297. }
  298. llvm::StringRef OP::GetTypeName(Type *Ty, std::string &str) {
  299. unsigned TypeSlot = OP::GetTypeSlot(Ty);
  300. if (TypeSlot < kUserDefineTypeSlot) {
  301. return GetOverloadTypeName(TypeSlot);
  302. } else if (TypeSlot == kUserDefineTypeSlot) {
  303. if (Ty->isPointerTy())
  304. Ty = Ty->getPointerElementType();
  305. StructType *ST = cast<StructType>(Ty);
  306. return ST->getStructName();
  307. } else if (TypeSlot == kObjectTypeSlot) {
  308. StructType *ST = cast<StructType>(Ty);
  309. return ST->getStructName();
  310. } else {
  311. raw_string_ostream os(str);
  312. Ty->print(os);
  313. os.flush();
  314. return str;
  315. }
  316. }
  317. const char *OP::GetOpCodeName(OpCode OpCode) {
  318. DXASSERT(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes, "otherwise caller passed OOB index");
  319. return m_OpCodeProps[(unsigned)OpCode].pOpCodeName;
  320. }
  321. const char *OP::GetAtomicOpName(DXIL::AtomicBinOpCode OpCode) {
  322. unsigned opcode = static_cast<unsigned>(OpCode);
  323. DXASSERT_LOCALVAR(opcode, opcode < static_cast<unsigned>(DXIL::AtomicBinOpCode::Invalid), "otherwise caller passed OOB index");
  324. return AtomicBinOpCodeName[static_cast<unsigned>(OpCode)];
  325. }
  326. OP::OpCodeClass OP::GetOpCodeClass(OpCode OpCode) {
  327. DXASSERT(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes, "otherwise caller passed OOB index");
  328. return m_OpCodeProps[(unsigned)OpCode].OpCodeClass;
  329. }
  330. const char *OP::GetOpCodeClassName(OpCode OpCode) {
  331. DXASSERT(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes, "otherwise caller passed OOB index");
  332. return m_OpCodeProps[(unsigned)OpCode].pOpCodeClassName;
  333. }
  334. bool OP::IsOverloadLegal(OpCode OpCode, Type *pType) {
  335. DXASSERT(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes, "otherwise caller passed OOB index");
  336. unsigned TypeSlot = GetTypeSlot(pType);
  337. return TypeSlot != UINT_MAX && m_OpCodeProps[(unsigned)OpCode].bAllowOverload[TypeSlot];
  338. }
  339. bool OP::CheckOpCodeTable() {
  340. for (unsigned i = 0; i < (unsigned)OpCode::NumOpCodes; i++) {
  341. if ((unsigned)m_OpCodeProps[i].OpCode != i)
  342. return false;
  343. }
  344. return true;
  345. }
  346. bool OP::IsDxilOpFuncName(StringRef name) {
  347. return name.startswith(OP::m_NamePrefix);
  348. }
  349. bool OP::IsDxilOpFunc(const llvm::Function *F) {
  350. if (!F->hasName())
  351. return false;
  352. return IsDxilOpFuncName(F->getName());
  353. }
  354. bool OP::IsDxilOpTypeName(StringRef name) {
  355. return name.startswith(m_TypePrefix) || name.startswith(m_MatrixTypePrefix);
  356. }
  357. bool OP::IsDxilOpType(llvm::StructType *ST) {
  358. if (!ST->hasName())
  359. return false;
  360. StringRef Name = ST->getName();
  361. return IsDxilOpTypeName(Name);
  362. }
  363. bool OP::IsDupDxilOpType(llvm::StructType *ST) {
  364. if (!ST->hasName())
  365. return false;
  366. StringRef Name = ST->getName();
  367. if (!IsDxilOpTypeName(Name))
  368. return false;
  369. size_t DotPos = Name.rfind('.');
  370. if (DotPos == 0 || DotPos == StringRef::npos || Name.back() == '.' ||
  371. !isdigit(static_cast<unsigned char>(Name[DotPos + 1])))
  372. return false;
  373. return true;
  374. }
  375. StructType *OP::GetOriginalDxilOpType(llvm::StructType *ST, llvm::Module &M) {
  376. DXASSERT(IsDupDxilOpType(ST), "else should not call GetOriginalDxilOpType");
  377. StringRef Name = ST->getName();
  378. size_t DotPos = Name.rfind('.');
  379. StructType *OriginalST = M.getTypeByName(Name.substr(0, DotPos));
  380. DXASSERT(OriginalST, "else name collison without original type");
  381. DXASSERT(ST->isLayoutIdentical(OriginalST),
  382. "else invalid layout for dxil types");
  383. return OriginalST;
  384. }
  385. bool OP::IsDxilOpFuncCallInst(const llvm::Instruction *I) {
  386. const CallInst *CI = dyn_cast<CallInst>(I);
  387. if (CI == nullptr) return false;
  388. return IsDxilOpFunc(CI->getCalledFunction());
  389. }
  390. bool OP::IsDxilOpFuncCallInst(const llvm::Instruction *I, OpCode opcode) {
  391. if (!IsDxilOpFuncCallInst(I)) return false;
  392. return llvm::cast<llvm::ConstantInt>(I->getOperand(0))->getZExtValue() == (unsigned)opcode;
  393. }
  394. OP::OpCode OP::GetDxilOpFuncCallInst(const llvm::Instruction *I) {
  395. DXASSERT(IsDxilOpFuncCallInst(I), "else caller didn't call IsDxilOpFuncCallInst to check");
  396. return (OP::OpCode)llvm::cast<llvm::ConstantInt>(I->getOperand(0))->getZExtValue();
  397. }
  398. bool OP::IsDxilOpWave(OpCode C) {
  399. unsigned op = (unsigned)C;
  400. /* <py::lines('OPCODE-WAVE')>hctdb_instrhelp.get_instrs_pred("op", "is_wave")</py>*/
  401. // OPCODE-WAVE:BEGIN
  402. // Instructions: WaveIsFirstLane=110, WaveGetLaneIndex=111,
  403. // WaveGetLaneCount=112, WaveAnyTrue=113, WaveAllTrue=114,
  404. // WaveActiveAllEqual=115, WaveActiveBallot=116, WaveReadLaneAt=117,
  405. // WaveReadLaneFirst=118, WaveActiveOp=119, WaveActiveBit=120,
  406. // WavePrefixOp=121, QuadReadLaneAt=122, QuadOp=123, WaveAllBitCount=135,
  407. // WavePrefixBitCount=136
  408. return 110 <= op && op <= 123 || 135 <= op && op <= 136;
  409. // OPCODE-WAVE:END
  410. }
  411. bool OP::IsDxilOpGradient(OpCode C) {
  412. unsigned op = (unsigned)C;
  413. /* <py::lines('OPCODE-GRADIENT')>hctdb_instrhelp.get_instrs_pred("op", "is_gradient")</py>*/
  414. // OPCODE-GRADIENT:BEGIN
  415. // Instructions: Sample=60, SampleBias=61, SampleCmp=64, TextureGather=73,
  416. // TextureGatherCmp=74, CalculateLOD=81, DerivCoarseX=83, DerivCoarseY=84,
  417. // DerivFineX=85, DerivFineY=86
  418. return 60 <= op && op <= 61 || op == 64 || 73 <= op && op <= 74 || op == 81 || 83 <= op && op <= 86;
  419. // OPCODE-GRADIENT:END
  420. }
  421. static Type *GetOrCreateStructType(LLVMContext &Ctx, ArrayRef<Type*> types, StringRef Name, Module *pModule) {
  422. if (StructType *ST = pModule->getTypeByName(Name)) {
  423. // TODO: validate the exist type match types if needed.
  424. return ST;
  425. }
  426. else
  427. return StructType::create(Ctx, types, Name);
  428. }
  429. //------------------------------------------------------------------------------
  430. //
  431. // OP methods.
  432. //
  433. OP::OP(LLVMContext &Ctx, Module *pModule)
  434. : m_Ctx(Ctx)
  435. , m_pModule(pModule)
  436. , m_LowPrecisionMode(DXIL::LowPrecisionMode::Undefined) {
  437. memset(m_pResRetType, 0, sizeof(m_pResRetType));
  438. memset(m_pCBufferRetType, 0, sizeof(m_pCBufferRetType));
  439. memset(m_OpCodeClassCache, 0, sizeof(m_OpCodeClassCache));
  440. static_assert(_countof(OP::m_OpCodeProps) == (size_t)OP::OpCode::NumOpCodes, "forgot to update OP::m_OpCodeProps");
  441. m_pHandleType = GetOrCreateStructType(m_Ctx, Type::getInt8PtrTy(m_Ctx), "dx.types.Handle", pModule);
  442. Type *DimsType[4] = { Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx) };
  443. m_pDimensionsType = GetOrCreateStructType(m_Ctx, DimsType, "dx.types.Dimensions", pModule);
  444. Type *SamplePosType[2] = { Type::getFloatTy(m_Ctx), Type::getFloatTy(m_Ctx) };
  445. m_pSamplePosType = GetOrCreateStructType(m_Ctx, SamplePosType, "dx.types.SamplePos", pModule);
  446. Type *I32cTypes[2] = { Type::getInt32Ty(m_Ctx), Type::getInt1Ty(m_Ctx) };
  447. m_pBinaryWithCarryType = GetOrCreateStructType(m_Ctx, I32cTypes, "dx.types.i32c", pModule);
  448. Type *TwoI32Types[2] = { Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx) };
  449. m_pBinaryWithTwoOutputsType = GetOrCreateStructType(m_Ctx, TwoI32Types, "dx.types.twoi32", pModule);
  450. Type *SplitDoubleTypes[2] = { Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx) }; // Lo, Hi.
  451. m_pSplitDoubleType = GetOrCreateStructType(m_Ctx, SplitDoubleTypes, "dx.types.splitdouble", pModule);
  452. Type *Int4Types[4] = { Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx) }; // HiHi, HiLo, LoHi, LoLo
  453. m_pInt4Type = GetOrCreateStructType(m_Ctx, Int4Types, "dx.types.fouri32", pModule);
  454. // Try to find existing intrinsic function.
  455. RefreshCache();
  456. }
  457. void OP::RefreshCache() {
  458. for (Function &F : m_pModule->functions()) {
  459. if (OP::IsDxilOpFunc(&F) && !F.user_empty()) {
  460. CallInst *CI = cast<CallInst>(*F.user_begin());
  461. OpCode OpCode = OP::GetDxilOpFuncCallInst(CI);
  462. Type *pOverloadType = OP::GetOverloadType(OpCode, &F);
  463. Function *OpFunc = GetOpFunc(OpCode, pOverloadType);
  464. (OpFunc);
  465. DXASSERT_NOMSG(OpFunc == &F);
  466. }
  467. }
  468. }
  469. void OP::UpdateCache(OpCodeClass opClass, Type * Ty, llvm::Function *F) {
  470. m_OpCodeClassCache[(unsigned)opClass].pOverloads[Ty] = F;
  471. m_FunctionToOpClass[F] = opClass;
  472. }
  473. Function *OP::GetOpFunc(OpCode OpCode, Type *pOverloadType) {
  474. DXASSERT(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes, "otherwise caller passed OOB OpCode");
  475. _Analysis_assume_(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes);
  476. DXASSERT(IsOverloadLegal(OpCode, pOverloadType), "otherwise the caller requested illegal operation overload (eg HLSL function with unsupported types for mapped intrinsic function)");
  477. OpCodeClass opClass = m_OpCodeProps[(unsigned)OpCode].OpCodeClass;
  478. Function *&F = m_OpCodeClassCache[(unsigned)opClass].pOverloads[pOverloadType];
  479. if (F != nullptr) {
  480. UpdateCache(opClass, pOverloadType, F);
  481. return F;
  482. }
  483. vector<Type*> ArgTypes; // RetType is ArgTypes[0]
  484. Type *pETy = pOverloadType;
  485. Type *pRes = GetHandleType();
  486. Type *pDim = GetDimensionsType();
  487. Type *pPos = GetSamplePosType();
  488. Type *pV = Type::getVoidTy(m_Ctx);
  489. Type *pI1 = Type::getInt1Ty(m_Ctx);
  490. Type *pI8 = Type::getInt8Ty(m_Ctx);
  491. Type *pI16 = Type::getInt16Ty(m_Ctx);
  492. Type *pI32 = Type::getInt32Ty(m_Ctx);
  493. Type *pPI32 = Type::getInt32PtrTy(m_Ctx); (pPI32); // Currently unused.
  494. Type *pI64 = Type::getInt64Ty(m_Ctx); (pI64); // Currently unused.
  495. Type *pF16 = Type::getHalfTy(m_Ctx);
  496. Type *pF32 = Type::getFloatTy(m_Ctx);
  497. Type *pPF32 = Type::getFloatPtrTy(m_Ctx);
  498. Type *pI32C = GetBinaryWithCarryType();
  499. Type *p2I32 = GetBinaryWithTwoOutputsType();
  500. Type *pF64 = Type::getDoubleTy(m_Ctx);
  501. Type *pSDT = GetSplitDoubleType(); // Split double type.
  502. Type *pI4S = GetInt4Type(); // 4 i32s in a struct.
  503. Type *udt = pOverloadType;
  504. Type *obj = pOverloadType;
  505. std::string funcName = (Twine(OP::m_NamePrefix) + Twine(GetOpCodeClassName(OpCode))).str();
  506. // Add ret type to the name.
  507. if (pOverloadType != pV) {
  508. std::string typeName;
  509. funcName = Twine(funcName).concat(".").concat(GetTypeName(pOverloadType, typeName)).str();
  510. }
  511. // Try to find exist function with the same name in the module.
  512. if (Function *existF = m_pModule->getFunction(funcName)) {
  513. F = existF;
  514. UpdateCache(opClass, pOverloadType, F);
  515. return F;
  516. }
  517. #define A(_x) ArgTypes.emplace_back(_x)
  518. #define RRT(_y) A(GetResRetType(_y))
  519. #define CBRT(_y) A(GetCBufferRetType(_y))
  520. /* <py::lines('OPCODE-OLOAD-FUNCS')>hctdb_instrhelp.get_oloads_funcs()</py>*/
  521. switch (OpCode) { // return OpCode
  522. // OPCODE-OLOAD-FUNCS:BEGIN
  523. // Temporary, indexable, input, output registers
  524. case OpCode::TempRegLoad: A(pETy); A(pI32); A(pI32); break;
  525. case OpCode::TempRegStore: A(pV); A(pI32); A(pI32); A(pETy); break;
  526. case OpCode::MinPrecXRegLoad: A(pETy); A(pI32); A(pPF32);A(pI32); A(pI8); break;
  527. case OpCode::MinPrecXRegStore: A(pV); A(pI32); A(pPF32);A(pI32); A(pI8); A(pETy); break;
  528. case OpCode::LoadInput: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); A(pI32); break;
  529. case OpCode::StoreOutput: A(pV); A(pI32); A(pI32); A(pI32); A(pI8); A(pETy); break;
  530. // Unary float
  531. case OpCode::FAbs: A(pETy); A(pI32); A(pETy); break;
  532. case OpCode::Saturate: A(pETy); A(pI32); A(pETy); break;
  533. case OpCode::IsNaN: A(pI1); A(pI32); A(pETy); break;
  534. case OpCode::IsInf: A(pI1); A(pI32); A(pETy); break;
  535. case OpCode::IsFinite: A(pI1); A(pI32); A(pETy); break;
  536. case OpCode::IsNormal: A(pI1); A(pI32); A(pETy); break;
  537. case OpCode::Cos: A(pETy); A(pI32); A(pETy); break;
  538. case OpCode::Sin: A(pETy); A(pI32); A(pETy); break;
  539. case OpCode::Tan: A(pETy); A(pI32); A(pETy); break;
  540. case OpCode::Acos: A(pETy); A(pI32); A(pETy); break;
  541. case OpCode::Asin: A(pETy); A(pI32); A(pETy); break;
  542. case OpCode::Atan: A(pETy); A(pI32); A(pETy); break;
  543. case OpCode::Hcos: A(pETy); A(pI32); A(pETy); break;
  544. case OpCode::Hsin: A(pETy); A(pI32); A(pETy); break;
  545. case OpCode::Htan: A(pETy); A(pI32); A(pETy); break;
  546. case OpCode::Exp: A(pETy); A(pI32); A(pETy); break;
  547. case OpCode::Frc: A(pETy); A(pI32); A(pETy); break;
  548. case OpCode::Log: A(pETy); A(pI32); A(pETy); break;
  549. case OpCode::Sqrt: A(pETy); A(pI32); A(pETy); break;
  550. case OpCode::Rsqrt: A(pETy); A(pI32); A(pETy); break;
  551. // Unary float - rounding
  552. case OpCode::Round_ne: A(pETy); A(pI32); A(pETy); break;
  553. case OpCode::Round_ni: A(pETy); A(pI32); A(pETy); break;
  554. case OpCode::Round_pi: A(pETy); A(pI32); A(pETy); break;
  555. case OpCode::Round_z: A(pETy); A(pI32); A(pETy); break;
  556. // Unary int
  557. case OpCode::Bfrev: A(pETy); A(pI32); A(pETy); break;
  558. case OpCode::Countbits: A(pI32); A(pI32); A(pETy); break;
  559. case OpCode::FirstbitLo: A(pI32); A(pI32); A(pETy); break;
  560. // Unary uint
  561. case OpCode::FirstbitHi: A(pI32); A(pI32); A(pETy); break;
  562. // Unary int
  563. case OpCode::FirstbitSHi: A(pI32); A(pI32); A(pETy); break;
  564. // Binary float
  565. case OpCode::FMax: A(pETy); A(pI32); A(pETy); A(pETy); break;
  566. case OpCode::FMin: A(pETy); A(pI32); A(pETy); A(pETy); break;
  567. // Binary int
  568. case OpCode::IMax: A(pETy); A(pI32); A(pETy); A(pETy); break;
  569. case OpCode::IMin: A(pETy); A(pI32); A(pETy); A(pETy); break;
  570. // Binary uint
  571. case OpCode::UMax: A(pETy); A(pI32); A(pETy); A(pETy); break;
  572. case OpCode::UMin: A(pETy); A(pI32); A(pETy); A(pETy); break;
  573. // Binary int with two outputs
  574. case OpCode::IMul: A(p2I32); A(pI32); A(pETy); A(pETy); break;
  575. // Binary uint with two outputs
  576. case OpCode::UMul: A(p2I32); A(pI32); A(pETy); A(pETy); break;
  577. case OpCode::UDiv: A(p2I32); A(pI32); A(pETy); A(pETy); break;
  578. // Binary uint with carry or borrow
  579. case OpCode::UAddc: A(pI32C); A(pI32); A(pETy); A(pETy); break;
  580. case OpCode::USubb: A(pI32C); A(pI32); A(pETy); A(pETy); break;
  581. // Tertiary float
  582. case OpCode::FMad: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  583. case OpCode::Fma: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  584. // Tertiary int
  585. case OpCode::IMad: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  586. // Tertiary uint
  587. case OpCode::UMad: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  588. // Tertiary int
  589. case OpCode::Msad: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  590. case OpCode::Ibfe: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  591. // Tertiary uint
  592. case OpCode::Ubfe: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  593. // Quaternary
  594. case OpCode::Bfi: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); break;
  595. // Dot
  596. case OpCode::Dot2: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); break;
  597. case OpCode::Dot3: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); break;
  598. case OpCode::Dot4: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); break;
  599. // Resources
  600. case OpCode::CreateHandle: A(pRes); A(pI32); A(pI8); A(pI32); A(pI32); A(pI1); break;
  601. case OpCode::CBufferLoad: A(pETy); A(pI32); A(pRes); A(pI32); A(pI32); break;
  602. case OpCode::CBufferLoadLegacy: CBRT(pETy); A(pI32); A(pRes); A(pI32); break;
  603. // Resources - sample
  604. case OpCode::Sample: RRT(pETy); A(pI32); A(pRes); A(pRes); A(pF32); A(pF32); A(pF32); A(pF32); A(pI32); A(pI32); A(pI32); A(pF32); break;
  605. case OpCode::SampleBias: RRT(pETy); A(pI32); A(pRes); A(pRes); A(pF32); A(pF32); A(pF32); A(pF32); A(pI32); A(pI32); A(pI32); A(pF32); A(pF32); break;
  606. case OpCode::SampleLevel: RRT(pETy); A(pI32); A(pRes); A(pRes); A(pF32); A(pF32); A(pF32); A(pF32); A(pI32); A(pI32); A(pI32); A(pF32); break;
  607. case OpCode::SampleGrad: RRT(pETy); A(pI32); A(pRes); A(pRes); A(pF32); A(pF32); A(pF32); A(pF32); A(pI32); A(pI32); A(pI32); A(pF32); A(pF32); A(pF32); A(pF32); A(pF32); A(pF32); A(pF32); break;
  608. case OpCode::SampleCmp: RRT(pETy); A(pI32); A(pRes); A(pRes); A(pF32); A(pF32); A(pF32); A(pF32); A(pI32); A(pI32); A(pI32); A(pF32); A(pF32); break;
  609. case OpCode::SampleCmpLevelZero: RRT(pETy); A(pI32); A(pRes); A(pRes); A(pF32); A(pF32); A(pF32); A(pF32); A(pI32); A(pI32); A(pI32); A(pF32); break;
  610. // Resources
  611. case OpCode::TextureLoad: RRT(pETy); A(pI32); A(pRes); A(pI32); A(pI32); A(pI32); A(pI32); A(pI32); A(pI32); A(pI32); break;
  612. case OpCode::TextureStore: A(pV); A(pI32); A(pRes); A(pI32); A(pI32); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); A(pI8); break;
  613. case OpCode::BufferLoad: RRT(pETy); A(pI32); A(pRes); A(pI32); A(pI32); break;
  614. case OpCode::BufferStore: A(pV); A(pI32); A(pRes); A(pI32); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); A(pI8); break;
  615. case OpCode::BufferUpdateCounter: A(pI32); A(pI32); A(pRes); A(pI8); break;
  616. case OpCode::CheckAccessFullyMapped: A(pI1); A(pI32); A(pI32); break;
  617. case OpCode::GetDimensions: A(pDim); A(pI32); A(pRes); A(pI32); break;
  618. // Resources - gather
  619. case OpCode::TextureGather: RRT(pETy); A(pI32); A(pRes); A(pRes); A(pF32); A(pF32); A(pF32); A(pF32); A(pI32); A(pI32); A(pI32); break;
  620. case OpCode::TextureGatherCmp: RRT(pETy); A(pI32); A(pRes); A(pRes); A(pF32); A(pF32); A(pF32); A(pF32); A(pI32); A(pI32); A(pI32); A(pF32); break;
  621. // Resources - sample
  622. case OpCode::Texture2DMSGetSamplePosition:A(pPos); A(pI32); A(pRes); A(pI32); break;
  623. case OpCode::RenderTargetGetSamplePosition:A(pPos); A(pI32); A(pI32); break;
  624. case OpCode::RenderTargetGetSampleCount:A(pI32); A(pI32); break;
  625. // Synchronization
  626. case OpCode::AtomicBinOp: A(pI32); A(pI32); A(pRes); A(pI32); A(pI32); A(pI32); A(pI32); A(pI32); break;
  627. case OpCode::AtomicCompareExchange: A(pI32); A(pI32); A(pRes); A(pI32); A(pI32); A(pI32); A(pI32); A(pI32); break;
  628. case OpCode::Barrier: A(pV); A(pI32); A(pI32); break;
  629. // Pixel shader
  630. case OpCode::CalculateLOD: A(pF32); A(pI32); A(pRes); A(pRes); A(pF32); A(pF32); A(pF32); A(pI1); break;
  631. case OpCode::Discard: A(pV); A(pI32); A(pI1); break;
  632. case OpCode::DerivCoarseX: A(pETy); A(pI32); A(pETy); break;
  633. case OpCode::DerivCoarseY: A(pETy); A(pI32); A(pETy); break;
  634. case OpCode::DerivFineX: A(pETy); A(pI32); A(pETy); break;
  635. case OpCode::DerivFineY: A(pETy); A(pI32); A(pETy); break;
  636. case OpCode::EvalSnapped: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); A(pI32); A(pI32); break;
  637. case OpCode::EvalSampleIndex: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); A(pI32); break;
  638. case OpCode::EvalCentroid: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); break;
  639. case OpCode::SampleIndex: A(pI32); A(pI32); break;
  640. case OpCode::Coverage: A(pI32); A(pI32); break;
  641. case OpCode::InnerCoverage: A(pI32); A(pI32); break;
  642. // Compute shader
  643. case OpCode::ThreadId: A(pI32); A(pI32); A(pI32); break;
  644. case OpCode::GroupId: A(pI32); A(pI32); A(pI32); break;
  645. case OpCode::ThreadIdInGroup: A(pI32); A(pI32); A(pI32); break;
  646. case OpCode::FlattenedThreadIdInGroup:A(pI32); A(pI32); break;
  647. // Geometry shader
  648. case OpCode::EmitStream: A(pV); A(pI32); A(pI8); break;
  649. case OpCode::CutStream: A(pV); A(pI32); A(pI8); break;
  650. case OpCode::EmitThenCutStream: A(pV); A(pI32); A(pI8); break;
  651. case OpCode::GSInstanceID: A(pI32); A(pI32); break;
  652. // Double precision
  653. case OpCode::MakeDouble: A(pF64); A(pI32); A(pI32); A(pI32); break;
  654. case OpCode::SplitDouble: A(pSDT); A(pI32); A(pF64); break;
  655. // Domain and hull shader
  656. case OpCode::LoadOutputControlPoint: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); A(pI32); break;
  657. case OpCode::LoadPatchConstant: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); break;
  658. // Domain shader
  659. case OpCode::DomainLocation: A(pF32); A(pI32); A(pI8); break;
  660. // Hull shader
  661. case OpCode::StorePatchConstant: A(pV); A(pI32); A(pI32); A(pI32); A(pI8); A(pETy); break;
  662. case OpCode::OutputControlPointID: A(pI32); A(pI32); break;
  663. case OpCode::PrimitiveID: A(pI32); A(pI32); break;
  664. // Other
  665. case OpCode::CycleCounterLegacy: A(p2I32); A(pI32); break;
  666. // Wave
  667. case OpCode::WaveIsFirstLane: A(pI1); A(pI32); break;
  668. case OpCode::WaveGetLaneIndex: A(pI32); A(pI32); break;
  669. case OpCode::WaveGetLaneCount: A(pI32); A(pI32); break;
  670. case OpCode::WaveAnyTrue: A(pI1); A(pI32); A(pI1); break;
  671. case OpCode::WaveAllTrue: A(pI1); A(pI32); A(pI1); break;
  672. case OpCode::WaveActiveAllEqual: A(pI1); A(pI32); A(pETy); break;
  673. case OpCode::WaveActiveBallot: A(pI4S); A(pI32); A(pI1); break;
  674. case OpCode::WaveReadLaneAt: A(pETy); A(pI32); A(pETy); A(pI32); break;
  675. case OpCode::WaveReadLaneFirst: A(pETy); A(pI32); A(pETy); break;
  676. case OpCode::WaveActiveOp: A(pETy); A(pI32); A(pETy); A(pI8); A(pI8); break;
  677. case OpCode::WaveActiveBit: A(pETy); A(pI32); A(pETy); A(pI8); break;
  678. case OpCode::WavePrefixOp: A(pETy); A(pI32); A(pETy); A(pI8); A(pI8); break;
  679. case OpCode::QuadReadLaneAt: A(pETy); A(pI32); A(pETy); A(pI32); break;
  680. case OpCode::QuadOp: A(pETy); A(pI32); A(pETy); A(pI8); break;
  681. // Bitcasts with different sizes
  682. case OpCode::BitcastI16toF16: A(pF16); A(pI32); A(pI16); break;
  683. case OpCode::BitcastF16toI16: A(pI16); A(pI32); A(pF16); break;
  684. case OpCode::BitcastI32toF32: A(pF32); A(pI32); A(pI32); break;
  685. case OpCode::BitcastF32toI32: A(pI32); A(pI32); A(pF32); break;
  686. case OpCode::BitcastI64toF64: A(pF64); A(pI32); A(pI64); break;
  687. case OpCode::BitcastF64toI64: A(pI64); A(pI32); A(pF64); break;
  688. // Legacy floating-point
  689. case OpCode::LegacyF32ToF16: A(pI32); A(pI32); A(pF32); break;
  690. case OpCode::LegacyF16ToF32: A(pF32); A(pI32); A(pI32); break;
  691. // Double precision
  692. case OpCode::LegacyDoubleToFloat: A(pF32); A(pI32); A(pF64); break;
  693. case OpCode::LegacyDoubleToSInt32: A(pI32); A(pI32); A(pF64); break;
  694. case OpCode::LegacyDoubleToUInt32: A(pI32); A(pI32); A(pF64); break;
  695. // Wave
  696. case OpCode::WaveAllBitCount: A(pI32); A(pI32); A(pI1); break;
  697. case OpCode::WavePrefixBitCount: A(pI32); A(pI32); A(pI1); break;
  698. // Pixel shader
  699. case OpCode::AttributeAtVertex: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); A(pI8); break;
  700. // Graphics shader
  701. case OpCode::ViewID: A(pI32); A(pI32); break;
  702. // Resources
  703. case OpCode::RawBufferLoad: RRT(pETy); A(pI32); A(pRes); A(pI32); A(pI32); A(pI8); A(pI32); break;
  704. case OpCode::RawBufferStore: A(pV); A(pI32); A(pRes); A(pI32); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); A(pI8); A(pI32); break;
  705. // Raytracing uint System Values
  706. case OpCode::InstanceID: A(pI32); A(pI32); break;
  707. case OpCode::InstanceIndex: A(pI32); A(pI32); break;
  708. case OpCode::HitKind: A(pI32); A(pI32); break;
  709. case OpCode::RayFlags: A(pI32); A(pI32); break;
  710. // Ray Dispatch Arguments
  711. case OpCode::DispatchRaysIndex: A(pI32); A(pI32); A(pI8); break;
  712. case OpCode::DispatchRaysDimensions: A(pI32); A(pI32); A(pI8); break;
  713. // Ray Vectors
  714. case OpCode::WorldRayOrigin: A(pF32); A(pI32); A(pI8); break;
  715. case OpCode::WorldRayDirection: A(pF32); A(pI32); A(pI8); break;
  716. case OpCode::ObjectRayOrigin: A(pF32); A(pI32); A(pI8); break;
  717. case OpCode::ObjectRayDirection: A(pF32); A(pI32); A(pI8); break;
  718. // Ray Transforms
  719. case OpCode::ObjectToWorld: A(pF32); A(pI32); A(pI32); A(pI8); break;
  720. case OpCode::WorldToObject: A(pF32); A(pI32); A(pI32); A(pI8); break;
  721. // RayT
  722. case OpCode::RayTMin: A(pF32); A(pI32); break;
  723. case OpCode::RayTCurrent: A(pF32); A(pI32); break;
  724. // AnyHit Terminals
  725. case OpCode::IgnoreHit: A(pV); A(pI32); break;
  726. case OpCode::AcceptHitAndEndSearch: A(pV); A(pI32); break;
  727. // Indirect Shader Invocation
  728. case OpCode::TraceRay: A(pV); A(pI32); A(pRes); A(pI32); A(pI32); A(pI32); A(pI32); A(pI32); A(pF32); A(pF32); A(pF32); A(pF32); A(pF32); A(pF32); A(pF32); A(pF32); A(udt); break;
  729. case OpCode::ReportHit: A(pI1); A(pI32); A(pF32); A(pI32); A(udt); break;
  730. case OpCode::CallShader: A(pV); A(pI32); A(pI32); A(udt); break;
  731. // Library create handle from resource struct (like HL intrinsic)
  732. case OpCode::CreateHandleFromResourceStructForLib:A(pRes); A(pI32); A(obj); break;
  733. // OPCODE-OLOAD-FUNCS:END
  734. default: DXASSERT(false, "otherwise unhandled case"); break;
  735. }
  736. #undef RRT
  737. #undef A
  738. FunctionType *pFT;
  739. DXASSERT(ArgTypes.size() > 1, "otherwise forgot to initialize arguments");
  740. pFT = FunctionType::get(ArgTypes[0], ArrayRef<Type*>(&ArgTypes[1], ArgTypes.size()-1), false);
  741. F = cast<Function>(m_pModule->getOrInsertFunction(funcName, pFT));
  742. UpdateCache(opClass, pOverloadType, F);
  743. F->setCallingConv(CallingConv::C);
  744. F->addFnAttr(Attribute::NoUnwind);
  745. if (m_OpCodeProps[(unsigned)OpCode].FuncAttr != Attribute::None)
  746. F->addFnAttr(m_OpCodeProps[(unsigned)OpCode].FuncAttr);
  747. return F;
  748. }
  749. const SmallDenseMap<llvm::Type *, llvm::Function *, 8> &
  750. OP::GetOpFuncList(OpCode OpCode) const {
  751. DXASSERT(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes,
  752. "otherwise caller passed OOB OpCode");
  753. _Analysis_assume_(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes);
  754. return m_OpCodeClassCache[(unsigned)m_OpCodeProps[(unsigned)OpCode]
  755. .OpCodeClass]
  756. .pOverloads;
  757. }
  758. void OP::RemoveFunction(Function *F) {
  759. if (OP::IsDxilOpFunc(F)) {
  760. OpCodeClass opClass = m_FunctionToOpClass[F];
  761. for (auto it : m_OpCodeClassCache[(unsigned)opClass].pOverloads) {
  762. if (it.second == F) {
  763. m_OpCodeClassCache[(unsigned)opClass].pOverloads.erase(it.first);
  764. m_FunctionToOpClass.erase(F);
  765. break;
  766. }
  767. }
  768. }
  769. }
  770. bool OP::GetOpCodeClass(const Function *F, OP::OpCodeClass &opClass) {
  771. auto iter = m_FunctionToOpClass.find(F);
  772. if (iter == m_FunctionToOpClass.end()) {
  773. // When no user, cannot get opcode.
  774. DXASSERT(F->user_empty() || !IsDxilOpFunc(F), "dxil function without an opcode class mapping?");
  775. return false;
  776. }
  777. opClass = iter->second;
  778. return true;
  779. }
  780. bool OP::UseMinPrecision() {
  781. if (m_LowPrecisionMode == DXIL::LowPrecisionMode::Undefined) {
  782. if (&m_pModule->GetDxilModule()) {
  783. m_LowPrecisionMode = m_pModule->GetDxilModule().GetUseMinPrecision() ?
  784. DXIL::LowPrecisionMode::UseMinPrecision : DXIL::LowPrecisionMode::UseNativeLowPrecision;
  785. }
  786. else if (&m_pModule->GetHLModule()) {
  787. m_LowPrecisionMode = m_pModule->GetHLModule().GetHLOptions().bUseMinPrecision ?
  788. DXIL::LowPrecisionMode::UseMinPrecision : DXIL::LowPrecisionMode::UseNativeLowPrecision;
  789. }
  790. else {
  791. DXASSERT(false, "otherwise module doesn't contain either HLModule or Dxil Module.");
  792. }
  793. }
  794. return m_LowPrecisionMode == DXIL::LowPrecisionMode::UseMinPrecision;
  795. }
  796. uint64_t OP::GetAllocSizeForType(llvm::Type *Ty) {
  797. return m_pModule->getDataLayout().getTypeAllocSize(Ty);
  798. }
  799. llvm::Type *OP::GetOverloadType(OpCode OpCode, llvm::Function *F) {
  800. DXASSERT(F, "not work on nullptr");
  801. Type *Ty = F->getReturnType();
  802. FunctionType *FT = F->getFunctionType();
  803. /* <py::lines('OPCODE-OLOAD-TYPES')>hctdb_instrhelp.get_funcs_oload_type()</py>*/
  804. switch (OpCode) { // return OpCode
  805. // OPCODE-OLOAD-TYPES:BEGIN
  806. case OpCode::TempRegStore:
  807. case OpCode::CallShader:
  808. DXASSERT_NOMSG(FT->getNumParams() > 2);
  809. return FT->getParamType(2);
  810. case OpCode::MinPrecXRegStore:
  811. case OpCode::StoreOutput:
  812. case OpCode::BufferStore:
  813. case OpCode::StorePatchConstant:
  814. case OpCode::RawBufferStore:
  815. DXASSERT_NOMSG(FT->getNumParams() > 4);
  816. return FT->getParamType(4);
  817. case OpCode::IsNaN:
  818. case OpCode::IsInf:
  819. case OpCode::IsFinite:
  820. case OpCode::IsNormal:
  821. case OpCode::Countbits:
  822. case OpCode::FirstbitLo:
  823. case OpCode::FirstbitHi:
  824. case OpCode::FirstbitSHi:
  825. case OpCode::IMul:
  826. case OpCode::UMul:
  827. case OpCode::UDiv:
  828. case OpCode::UAddc:
  829. case OpCode::USubb:
  830. case OpCode::WaveActiveAllEqual:
  831. case OpCode::CreateHandleFromResourceStructForLib:
  832. DXASSERT_NOMSG(FT->getNumParams() > 1);
  833. return FT->getParamType(1);
  834. case OpCode::TextureStore:
  835. DXASSERT_NOMSG(FT->getNumParams() > 5);
  836. return FT->getParamType(5);
  837. case OpCode::TraceRay:
  838. DXASSERT_NOMSG(FT->getNumParams() > 15);
  839. return FT->getParamType(15);
  840. case OpCode::ReportHit:
  841. DXASSERT_NOMSG(FT->getNumParams() > 3);
  842. return FT->getParamType(3);
  843. case OpCode::CreateHandle:
  844. case OpCode::BufferUpdateCounter:
  845. case OpCode::GetDimensions:
  846. case OpCode::Texture2DMSGetSamplePosition:
  847. case OpCode::RenderTargetGetSamplePosition:
  848. case OpCode::RenderTargetGetSampleCount:
  849. case OpCode::Barrier:
  850. case OpCode::Discard:
  851. case OpCode::EmitStream:
  852. case OpCode::CutStream:
  853. case OpCode::EmitThenCutStream:
  854. case OpCode::CycleCounterLegacy:
  855. case OpCode::WaveIsFirstLane:
  856. case OpCode::WaveGetLaneIndex:
  857. case OpCode::WaveGetLaneCount:
  858. case OpCode::WaveAnyTrue:
  859. case OpCode::WaveAllTrue:
  860. case OpCode::WaveActiveBallot:
  861. case OpCode::BitcastI16toF16:
  862. case OpCode::BitcastF16toI16:
  863. case OpCode::BitcastI32toF32:
  864. case OpCode::BitcastF32toI32:
  865. case OpCode::BitcastI64toF64:
  866. case OpCode::BitcastF64toI64:
  867. case OpCode::LegacyF32ToF16:
  868. case OpCode::LegacyF16ToF32:
  869. case OpCode::LegacyDoubleToFloat:
  870. case OpCode::LegacyDoubleToSInt32:
  871. case OpCode::LegacyDoubleToUInt32:
  872. case OpCode::WaveAllBitCount:
  873. case OpCode::WavePrefixBitCount:
  874. case OpCode::IgnoreHit:
  875. case OpCode::AcceptHitAndEndSearch:
  876. return Type::getVoidTy(m_Ctx);
  877. case OpCode::CheckAccessFullyMapped:
  878. case OpCode::AtomicBinOp:
  879. case OpCode::AtomicCompareExchange:
  880. case OpCode::SampleIndex:
  881. case OpCode::Coverage:
  882. case OpCode::InnerCoverage:
  883. case OpCode::ThreadId:
  884. case OpCode::GroupId:
  885. case OpCode::ThreadIdInGroup:
  886. case OpCode::FlattenedThreadIdInGroup:
  887. case OpCode::GSInstanceID:
  888. case OpCode::OutputControlPointID:
  889. case OpCode::PrimitiveID:
  890. case OpCode::ViewID:
  891. case OpCode::InstanceID:
  892. case OpCode::InstanceIndex:
  893. case OpCode::HitKind:
  894. case OpCode::RayFlags:
  895. case OpCode::DispatchRaysIndex:
  896. case OpCode::DispatchRaysDimensions:
  897. return IntegerType::get(m_Ctx, 32);
  898. case OpCode::CalculateLOD:
  899. case OpCode::DomainLocation:
  900. case OpCode::WorldRayOrigin:
  901. case OpCode::WorldRayDirection:
  902. case OpCode::ObjectRayOrigin:
  903. case OpCode::ObjectRayDirection:
  904. case OpCode::ObjectToWorld:
  905. case OpCode::WorldToObject:
  906. case OpCode::RayTMin:
  907. case OpCode::RayTCurrent:
  908. return Type::getFloatTy(m_Ctx);
  909. case OpCode::MakeDouble:
  910. case OpCode::SplitDouble:
  911. return Type::getDoubleTy(m_Ctx);
  912. case OpCode::CBufferLoadLegacy:
  913. case OpCode::Sample:
  914. case OpCode::SampleBias:
  915. case OpCode::SampleLevel:
  916. case OpCode::SampleGrad:
  917. case OpCode::SampleCmp:
  918. case OpCode::SampleCmpLevelZero:
  919. case OpCode::TextureLoad:
  920. case OpCode::BufferLoad:
  921. case OpCode::TextureGather:
  922. case OpCode::TextureGatherCmp:
  923. case OpCode::RawBufferLoad:
  924. {
  925. StructType *ST = cast<StructType>(Ty);
  926. return ST->getElementType(0);
  927. }
  928. // OPCODE-OLOAD-TYPES:END
  929. default: return Ty;
  930. }
  931. }
  932. Type *OP::GetHandleType() const {
  933. return m_pHandleType;
  934. }
  935. Type *OP::GetDimensionsType() const
  936. {
  937. return m_pDimensionsType;
  938. }
  939. Type *OP::GetSamplePosType() const
  940. {
  941. return m_pSamplePosType;
  942. }
  943. Type *OP::GetBinaryWithCarryType() const {
  944. return m_pBinaryWithCarryType;
  945. }
  946. Type *OP::GetBinaryWithTwoOutputsType() const {
  947. return m_pBinaryWithTwoOutputsType;
  948. }
  949. Type *OP::GetSplitDoubleType() const {
  950. return m_pSplitDoubleType;
  951. }
  952. Type *OP::GetInt4Type() const {
  953. return m_pInt4Type;
  954. }
  955. bool OP::IsResRetType(llvm::Type *Ty) {
  956. for (Type *ResTy : m_pResRetType) {
  957. if (Ty == ResTy)
  958. return true;
  959. }
  960. return false;
  961. }
  962. Type *OP::GetResRetType(Type *pOverloadType) {
  963. unsigned TypeSlot = GetTypeSlot(pOverloadType);
  964. if (m_pResRetType[TypeSlot] == nullptr) {
  965. string TypeName("dx.types.ResRet.");
  966. TypeName += GetOverloadTypeName(TypeSlot);
  967. Type *FieldTypes[5] = { pOverloadType, pOverloadType, pOverloadType, pOverloadType, Type::getInt32Ty(m_Ctx) };
  968. m_pResRetType[TypeSlot] = GetOrCreateStructType(m_Ctx, FieldTypes, TypeName, m_pModule);
  969. }
  970. return m_pResRetType[TypeSlot];
  971. }
  972. Type *OP::GetCBufferRetType(Type *pOverloadType) {
  973. unsigned TypeSlot = GetTypeSlot(pOverloadType);
  974. if (m_pCBufferRetType[TypeSlot] == nullptr) {
  975. string TypeName("dx.types.CBufRet.");
  976. TypeName += GetOverloadTypeName(TypeSlot);
  977. Type *i64Ty = Type::getInt64Ty(pOverloadType->getContext());
  978. Type *i16Ty = Type::getInt16Ty(pOverloadType->getContext());
  979. if (pOverloadType->isDoubleTy() || pOverloadType == i64Ty) {
  980. Type *FieldTypes[2] = { pOverloadType, pOverloadType };
  981. m_pCBufferRetType[TypeSlot] = GetOrCreateStructType(m_Ctx, FieldTypes, TypeName, m_pModule);
  982. }
  983. else if (!UseMinPrecision() && (pOverloadType->isHalfTy() || pOverloadType == i16Ty)) {
  984. TypeName += ".8"; // dx.types.CBufRet.fp16.8 for buffer of 8 halves
  985. Type *FieldTypes[8] = {
  986. pOverloadType, pOverloadType, pOverloadType, pOverloadType,
  987. pOverloadType, pOverloadType, pOverloadType, pOverloadType,
  988. };
  989. m_pCBufferRetType[TypeSlot] = GetOrCreateStructType(m_Ctx, FieldTypes, TypeName, m_pModule);
  990. }
  991. else {
  992. Type *FieldTypes[4] = { pOverloadType, pOverloadType, pOverloadType, pOverloadType };
  993. m_pCBufferRetType[TypeSlot] = GetOrCreateStructType(m_Ctx, FieldTypes, TypeName, m_pModule);
  994. }
  995. }
  996. return m_pCBufferRetType[TypeSlot];
  997. }
  998. //------------------------------------------------------------------------------
  999. //
  1000. // LLVM utility methods.
  1001. //
  1002. Constant *OP::GetI1Const(bool v) {
  1003. return Constant::getIntegerValue(IntegerType::get(m_Ctx, 1), APInt(1, v));
  1004. }
  1005. Constant *OP::GetI8Const(char v) {
  1006. return Constant::getIntegerValue(IntegerType::get(m_Ctx, 8), APInt(8, v));
  1007. }
  1008. Constant *OP::GetU8Const(unsigned char v) {
  1009. return GetI8Const((char)v);
  1010. }
  1011. Constant *OP::GetI16Const(int v) {
  1012. return Constant::getIntegerValue(IntegerType::get(m_Ctx, 16), APInt(16, v));
  1013. }
  1014. Constant *OP::GetU16Const(unsigned v) {
  1015. return GetI16Const((int)v);
  1016. }
  1017. Constant *OP::GetI32Const(int v) {
  1018. return Constant::getIntegerValue(IntegerType::get(m_Ctx, 32), APInt(32, v));
  1019. }
  1020. Constant *OP::GetU32Const(unsigned v) {
  1021. return GetI32Const((int)v);
  1022. }
  1023. Constant *OP::GetU64Const(unsigned long long v) {
  1024. return Constant::getIntegerValue(IntegerType::get(m_Ctx, 64), APInt(64, v));
  1025. }
  1026. Constant *OP::GetFloatConst(float v) {
  1027. return ConstantFP::get(m_Ctx, APFloat(v));
  1028. }
  1029. Constant *OP::GetDoubleConst(double v) {
  1030. return ConstantFP::get(m_Ctx, APFloat(v));
  1031. }
  1032. } // namespace hlsl