DxilOperations.cpp 91 KB

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