DxilOperations.cpp 91 KB

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