DxilOperations.cpp 72 KB

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