DxilOperations.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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 "llvm/ADT/ArrayRef.h"
  14. #include "llvm/IR/LLVMContext.h"
  15. #include "llvm/IR/Module.h"
  16. #include "llvm/IR/Type.h"
  17. #include "llvm/IR/Constants.h"
  18. #include "llvm/IR/Instructions.h"
  19. using namespace llvm;
  20. using std::vector;
  21. using std::string;
  22. namespace hlsl {
  23. using OC = OP::OpCode;
  24. using OCC = OP::OpCodeClass;
  25. //------------------------------------------------------------------------------
  26. //
  27. // OP class const-static data and related static methods.
  28. //
  29. /* <py>
  30. import hctdb_instrhelp
  31. </py> */
  32. /* <py::lines('OPCODE-OLOADS')>hctdb_instrhelp.get_oloads_props()</py>*/
  33. // OPCODE-OLOADS:BEGIN
  34. const OP::OpCodeProperty OP::m_OpCodeProps[(unsigned)OP::OpCode::NumOpCodes] = {
  35. // OpCode OpCode name, OpCodeClass OpCodeClass name, void, h, f, d, i1, i8, i16, i32, i64 function attribute
  36. // Temporary, indexable, input, output registers void, h, f, d, i1, i8, i16, i32, i64 function attribute
  37. { OC::TempRegLoad, "TempRegLoad", OCC::TempRegLoad, "tempRegLoad", false, true, true, false, false, false, true, true, false, Attribute::ReadOnly, },
  38. { OC::TempRegStore, "TempRegStore", OCC::TempRegStore, "tempRegStore", false, true, true, false, false, false, true, true, false, Attribute::None, },
  39. { OC::MinPrecXRegLoad, "MinPrecXRegLoad", OCC::MinPrecXRegLoad, "minPrecXRegLoad", false, true, false, false, false, false, true, false, false, Attribute::ReadOnly, },
  40. { OC::MinPrecXRegStore, "MinPrecXRegStore", OCC::MinPrecXRegStore, "minPrecXRegStore", false, true, false, false, false, false, true, false, false, Attribute::None, },
  41. { OC::LoadInput, "LoadInput", OCC::LoadInput, "loadInput", false, true, true, false, false, false, true, true, false, Attribute::ReadNone, },
  42. { OC::StoreOutput, "StoreOutput", OCC::StoreOutput, "storeOutput", false, true, true, false, false, false, true, true, false, Attribute::None, },
  43. // Unary float void, h, f, d, i1, i8, i16, i32, i64 function attribute
  44. { OC::FAbs, "FAbs", OCC::Unary, "unary", false, true, true, true, false, false, false, false, false, Attribute::ReadNone, },
  45. { OC::Saturate, "Saturate", OCC::Unary, "unary", false, true, true, true, false, false, false, false, false, Attribute::ReadNone, },
  46. { OC::IsNaN, "IsNaN", OCC::IsSpecialFloat, "isSpecialFloat", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  47. { OC::IsInf, "IsInf", OCC::IsSpecialFloat, "isSpecialFloat", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  48. { OC::IsFinite, "IsFinite", OCC::IsSpecialFloat, "isSpecialFloat", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  49. { OC::IsNormal, "IsNormal", OCC::IsSpecialFloat, "isSpecialFloat", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  50. { OC::Cos, "Cos", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  51. { OC::Sin, "Sin", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  52. { OC::Tan, "Tan", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  53. { OC::Acos, "Acos", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  54. { OC::Asin, "Asin", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  55. { OC::Atan, "Atan", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  56. { OC::Hcos, "Hcos", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  57. { OC::Hsin, "Hsin", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  58. { OC::Htan, "Htan", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  59. { OC::Exp, "Exp", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  60. { OC::Frc, "Frc", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  61. { OC::Log, "Log", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  62. { OC::Sqrt, "Sqrt", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  63. { OC::Rsqrt, "Rsqrt", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  64. // Unary float - rounding void, h, f, d, i1, i8, i16, i32, i64 function attribute
  65. { OC::Round_ne, "Round_ne", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  66. { OC::Round_ni, "Round_ni", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  67. { OC::Round_pi, "Round_pi", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  68. { OC::Round_z, "Round_z", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  69. // Unary int void, h, f, d, i1, i8, i16, i32, i64 function attribute
  70. { OC::Bfrev, "Bfrev", OCC::Unary, "unary", false, false, false, false, false, false, true, true, true, Attribute::ReadNone, },
  71. { OC::Countbits, "Countbits", OCC::UnaryBits, "unaryBits", false, false, false, false, false, false, true, true, true, Attribute::ReadNone, },
  72. { OC::FirstbitLo, "FirstbitLo", OCC::UnaryBits, "unaryBits", false, false, false, false, false, false, true, true, true, Attribute::ReadNone, },
  73. { OC::FirstbitHi, "FirstbitHi", OCC::UnaryBits, "unaryBits", false, false, false, false, false, false, true, true, true, Attribute::ReadNone, },
  74. { OC::FirstbitSHi, "FirstbitSHi", OCC::UnaryBits, "unaryBits", false, false, false, false, false, false, true, true, true, Attribute::ReadNone, },
  75. // Binary float void, h, f, d, i1, i8, i16, i32, i64 function attribute
  76. { OC::FMax, "FMax", OCC::Binary, "binary", false, true, true, true, false, false, false, false, false, Attribute::ReadNone, },
  77. { OC::FMin, "FMin", OCC::Binary, "binary", false, true, true, true, false, false, false, false, false, Attribute::ReadNone, },
  78. // Binary int void, h, f, d, i1, i8, i16, i32, i64 function attribute
  79. { OC::IMax, "IMax", OCC::Binary, "binary", false, false, false, false, false, false, true, true, true, Attribute::ReadNone, },
  80. { OC::IMin, "IMin", OCC::Binary, "binary", false, false, false, false, false, false, true, true, true, Attribute::ReadNone, },
  81. { OC::UMax, "UMax", OCC::Binary, "binary", false, false, false, false, false, false, true, true, true, Attribute::ReadNone, },
  82. { OC::UMin, "UMin", OCC::Binary, "binary", false, false, false, false, false, false, true, true, true, Attribute::ReadNone, },
  83. // Binary int with two outputs void, h, f, d, i1, i8, i16, i32, i64 function attribute
  84. { OC::IMul, "IMul", OCC::BinaryWithTwoOuts, "binaryWithTwoOuts", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  85. { OC::UMul, "UMul", OCC::BinaryWithTwoOuts, "binaryWithTwoOuts", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  86. { OC::UDiv, "UDiv", OCC::BinaryWithTwoOuts, "binaryWithTwoOuts", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  87. // Binary int with carry void, h, f, d, i1, i8, i16, i32, i64 function attribute
  88. { OC::IAddc, "IAddc", OCC::BinaryWithCarry, "binaryWithCarry", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  89. { OC::UAddc, "UAddc", OCC::BinaryWithCarry, "binaryWithCarry", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  90. { OC::ISubc, "ISubc", OCC::BinaryWithCarry, "binaryWithCarry", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  91. { OC::USubc, "USubc", OCC::BinaryWithCarry, "binaryWithCarry", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  92. // Tertiary float void, h, f, d, i1, i8, i16, i32, i64 function attribute
  93. { OC::FMad, "FMad", OCC::Tertiary, "tertiary", false, true, true, true, false, false, false, false, false, Attribute::ReadNone, },
  94. { OC::Fma, "Fma", OCC::Tertiary, "tertiary", false, false, false, true, false, false, false, false, false, Attribute::ReadNone, },
  95. // Tertiary int void, h, f, d, i1, i8, i16, i32, i64 function attribute
  96. { OC::IMad, "IMad", OCC::Tertiary, "tertiary", false, false, false, false, false, false, true, true, true, Attribute::ReadNone, },
  97. { OC::UMad, "UMad", OCC::Tertiary, "tertiary", false, false, false, false, false, false, true, true, true, Attribute::ReadNone, },
  98. { OC::Msad, "Msad", OCC::Tertiary, "tertiary", false, false, false, false, false, false, false, true, true, Attribute::ReadNone, },
  99. { OC::Ibfe, "Ibfe", OCC::Tertiary, "tertiary", false, false, false, false, false, false, false, true, true, Attribute::ReadNone, },
  100. { OC::Ubfe, "Ubfe", OCC::Tertiary, "tertiary", false, false, false, false, false, false, false, true, true, Attribute::ReadNone, },
  101. // Quaternary void, h, f, d, i1, i8, i16, i32, i64 function attribute
  102. { OC::Bfi, "Bfi", OCC::Quaternary, "quaternary", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  103. // Dot void, h, f, d, i1, i8, i16, i32, i64 function attribute
  104. { OC::Dot2, "Dot2", OCC::Dot2, "dot2", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  105. { OC::Dot3, "Dot3", OCC::Dot3, "dot3", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  106. { OC::Dot4, "Dot4", OCC::Dot4, "dot4", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  107. // Resources void, h, f, d, i1, i8, i16, i32, i64 function attribute
  108. { OC::CreateHandle, "CreateHandle", OCC::CreateHandle, "createHandle", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  109. { OC::CBufferLoad, "CBufferLoad", OCC::CBufferLoad, "cbufferLoad", false, true, true, true, false, true, true, true, true, Attribute::ReadOnly, },
  110. { OC::CBufferLoadLegacy, "CBufferLoadLegacy", OCC::CBufferLoadLegacy, "cbufferLoadLegacy", false, true, true, true, false, false, true, true, false, Attribute::ReadOnly, },
  111. // Resources - sample void, h, f, d, i1, i8, i16, i32, i64 function attribute
  112. { OC::Sample, "Sample", OCC::Sample, "sample", false, true, true, false, false, false, false, false, false, Attribute::ReadOnly, },
  113. { OC::SampleBias, "SampleBias", OCC::SampleBias, "sampleBias", false, true, true, false, false, false, false, false, false, Attribute::ReadOnly, },
  114. { OC::SampleLevel, "SampleLevel", OCC::SampleLevel, "sampleLevel", false, true, true, false, false, false, false, false, false, Attribute::ReadOnly, },
  115. { OC::SampleGrad, "SampleGrad", OCC::SampleGrad, "sampleGrad", false, true, true, false, false, false, false, false, false, Attribute::ReadOnly, },
  116. { OC::SampleCmp, "SampleCmp", OCC::SampleCmp, "sampleCmp", false, true, true, false, false, false, false, false, false, Attribute::ReadOnly, },
  117. { OC::SampleCmpLevelZero, "SampleCmpLevelZero", OCC::SampleCmpLevelZero, "sampleCmpLevelZero", false, true, true, false, false, false, false, false, false, Attribute::ReadOnly, },
  118. // Resources void, h, f, d, i1, i8, i16, i32, i64 function attribute
  119. { OC::TextureLoad, "TextureLoad", OCC::TextureLoad, "textureLoad", false, true, true, false, false, false, true, true, false, Attribute::ReadOnly, },
  120. { OC::TextureStore, "TextureStore", OCC::TextureStore, "textureStore", false, true, true, false, false, false, true, true, false, Attribute::None, },
  121. { OC::BufferLoad, "BufferLoad", OCC::BufferLoad, "bufferLoad", false, true, true, false, false, false, true, true, true, Attribute::ReadOnly, },
  122. { OC::BufferStore, "BufferStore", OCC::BufferStore, "bufferStore", false, true, true, false, false, false, true, true, true, Attribute::None, },
  123. { OC::BufferUpdateCounter, "BufferUpdateCounter", OCC::BufferUpdateCounter, "bufferUpdateCounter", true, false, false, false, false, false, false, false, false, Attribute::None, },
  124. { OC::CheckAccessFullyMapped, "CheckAccessFullyMapped", OCC::CheckAccessFullyMapped, "checkAccessFullyMapped", false, false, false, false, false, false, false, true, false, Attribute::ReadOnly, },
  125. { OC::GetDimensions, "GetDimensions", OCC::GetDimensions, "getDimensions", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  126. // Resources - gather void, h, f, d, i1, i8, i16, i32, i64 function attribute
  127. { OC::TextureGather, "TextureGather", OCC::TextureGather, "textureGather", false, false, true, false, false, false, false, true, false, Attribute::ReadOnly, },
  128. { OC::TextureGatherCmp, "TextureGatherCmp", OCC::TextureGatherCmp, "textureGatherCmp", false, false, true, false, false, false, false, true, false, Attribute::ReadOnly, },
  129. // Resources - sample void, h, f, d, i1, i8, i16, i32, i64 function attribute
  130. { OC::Texture2DMSGetSamplePosition, "Texture2DMSGetSamplePosition", OCC::Texture2DMSGetSamplePosition, "texture2DMSGetSamplePosition", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  131. { OC::RenderTargetGetSamplePosition, "RenderTargetGetSamplePosition", OCC::RenderTargetGetSamplePosition, "renderTargetGetSamplePosition", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  132. { OC::RenderTargetGetSampleCount, "RenderTargetGetSampleCount", OCC::RenderTargetGetSampleCount, "renderTargetGetSampleCount", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  133. // Synchronization void, h, f, d, i1, i8, i16, i32, i64 function attribute
  134. { OC::AtomicBinOp, "AtomicBinOp", OCC::AtomicBinOp, "atomicBinOp", false, false, false, false, false, false, false, true, false, Attribute::None, },
  135. { OC::AtomicCompareExchange, "AtomicCompareExchange", OCC::AtomicCompareExchange, "atomicCompareExchange", false, false, false, false, false, false, false, true, false, Attribute::None, },
  136. { OC::Barrier, "Barrier", OCC::Barrier, "barrier", true, false, false, false, false, false, false, false, false, Attribute::None, },
  137. // Pixel shader void, h, f, d, i1, i8, i16, i32, i64 function attribute
  138. { OC::CalculateLOD, "CalculateLOD", OCC::CalculateLOD, "calculateLOD", false, false, true, false, false, false, false, false, false, Attribute::ReadOnly, },
  139. { OC::Discard, "Discard", OCC::Discard, "discard", true, false, false, false, false, false, false, false, false, Attribute::None, },
  140. { OC::DerivCoarseX, "DerivCoarseX", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  141. { OC::DerivCoarseY, "DerivCoarseY", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  142. { OC::DerivFineX, "DerivFineX", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  143. { OC::DerivFineY, "DerivFineY", OCC::Unary, "unary", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  144. { OC::EvalSnapped, "EvalSnapped", OCC::EvalSnapped, "evalSnapped", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  145. { OC::EvalSampleIndex, "EvalSampleIndex", OCC::EvalSampleIndex, "evalSampleIndex", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  146. { OC::EvalCentroid, "EvalCentroid", OCC::EvalCentroid, "evalCentroid", false, true, true, false, false, false, false, false, false, Attribute::ReadNone, },
  147. { OC::SampleIndex, "SampleIndex", OCC::SampleIndex, "sampleIndex", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  148. { OC::Coverage, "Coverage", OCC::Coverage, "coverage", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  149. { OC::InnerCoverage, "InnerCoverage", OCC::InnerCoverage, "innerCoverage", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  150. // Compute shader void, h, f, d, i1, i8, i16, i32, i64 function attribute
  151. { OC::ThreadId, "ThreadId", OCC::ThreadId, "threadId", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  152. { OC::GroupId, "GroupId", OCC::GroupId, "groupId", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  153. { OC::ThreadIdInGroup, "ThreadIdInGroup", OCC::ThreadIdInGroup, "threadIdInGroup", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  154. { OC::FlattenedThreadIdInGroup, "FlattenedThreadIdInGroup", OCC::FlattenedThreadIdInGroup, "flattenedThreadIdInGroup", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  155. // Geometry shader void, h, f, d, i1, i8, i16, i32, i64 function attribute
  156. { OC::EmitStream, "EmitStream", OCC::EmitStream, "emitStream", true, false, false, false, false, false, false, false, false, Attribute::None, },
  157. { OC::CutStream, "CutStream", OCC::CutStream, "cutStream", true, false, false, false, false, false, false, false, false, Attribute::None, },
  158. { OC::EmitThenCutStream, "EmitThenCutStream", OCC::EmitThenCutStream, "emitThenCutStream", true, false, false, false, false, false, false, false, false, Attribute::None, },
  159. { OC::GSInstanceID, "GSInstanceID", OCC::GSInstanceID, "gsInstanceID", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  160. // Double precision void, h, f, d, i1, i8, i16, i32, i64 function attribute
  161. { OC::MakeDouble, "MakeDouble", OCC::MakeDouble, "makeDouble", false, false, false, true, false, false, false, false, false, Attribute::ReadNone, },
  162. { OC::SplitDouble, "SplitDouble", OCC::SplitDouble, "splitDouble", false, false, false, true, false, false, false, false, false, Attribute::ReadNone, },
  163. // Domain and hull shader void, h, f, d, i1, i8, i16, i32, i64 function attribute
  164. { OC::LoadOutputControlPoint, "LoadOutputControlPoint", OCC::LoadOutputControlPoint, "loadOutputControlPoint", false, true, true, false, false, false, true, true, false, Attribute::ReadNone, },
  165. { OC::LoadPatchConstant, "LoadPatchConstant", OCC::LoadPatchConstant, "loadPatchConstant", false, true, true, false, false, false, true, true, false, Attribute::ReadNone, },
  166. // Domain shader void, h, f, d, i1, i8, i16, i32, i64 function attribute
  167. { OC::DomainLocation, "DomainLocation", OCC::DomainLocation, "domainLocation", false, false, true, false, false, false, false, false, false, Attribute::ReadNone, },
  168. // Hull shader void, h, f, d, i1, i8, i16, i32, i64 function attribute
  169. { OC::StorePatchConstant, "StorePatchConstant", OCC::StorePatchConstant, "storePatchConstant", false, true, true, false, false, false, true, true, false, Attribute::None, },
  170. { OC::OutputControlPointID, "OutputControlPointID", OCC::OutputControlPointID, "outputControlPointID", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  171. { OC::PrimitiveID, "PrimitiveID", OCC::PrimitiveID, "primitiveID", false, false, false, false, false, false, false, true, false, Attribute::ReadNone, },
  172. // Other void, h, f, d, i1, i8, i16, i32, i64 function attribute
  173. { OC::CycleCounterLegacy, "CycleCounterLegacy", OCC::CycleCounterLegacy, "cycleCounterLegacy", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  174. // Wave void, h, f, d, i1, i8, i16, i32, i64 function attribute
  175. { OC::WaveIsFirstLane, "WaveIsFirstLane", OCC::WaveIsFirstLane, "waveIsFirstLane", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  176. { OC::WaveGetLaneIndex, "WaveGetLaneIndex", OCC::WaveGetLaneIndex, "waveGetLaneIndex", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  177. { OC::WaveGetLaneCount, "WaveGetLaneCount", OCC::WaveGetLaneCount, "waveGetLaneCount", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  178. { OC::WaveAnyTrue, "WaveAnyTrue", OCC::WaveAnyTrue, "waveAnyTrue", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  179. { OC::WaveAllTrue, "WaveAllTrue", OCC::WaveAllTrue, "waveAllTrue", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  180. { OC::WaveActiveAllEqual, "WaveActiveAllEqual", OCC::WaveActiveAllEqual, "waveActiveAllEqual", false, true, true, true, true, true, true, true, true, Attribute::ReadOnly, },
  181. { OC::WaveActiveBallot, "WaveActiveBallot", OCC::WaveActiveBallot, "waveActiveBallot", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  182. { OC::WaveReadLaneAt, "WaveReadLaneAt", OCC::WaveReadLaneAt, "waveReadLaneAt", false, true, true, true, true, true, true, true, true, Attribute::ReadOnly, },
  183. { OC::WaveReadLaneFirst, "WaveReadLaneFirst", OCC::WaveReadLaneFirst, "waveReadLaneFirst", false, true, true, false, true, true, true, true, true, Attribute::ReadOnly, },
  184. { OC::WaveActiveOp, "WaveActiveOp", OCC::WaveActiveOp, "waveActiveOp", false, true, true, true, true, true, true, true, true, Attribute::ReadOnly, },
  185. { OC::WaveActiveBit, "WaveActiveBit", OCC::WaveActiveBit, "waveActiveBit", false, false, false, false, false, true, true, true, true, Attribute::ReadOnly, },
  186. { OC::WavePrefixOp, "WavePrefixOp", OCC::WavePrefixOp, "wavePrefixOp", false, true, true, true, false, true, true, true, true, Attribute::ReadOnly, },
  187. { OC::QuadReadLaneAt, "QuadReadLaneAt", OCC::QuadReadLaneAt, "quadReadLaneAt", false, true, true, true, true, true, true, true, true, Attribute::ReadOnly, },
  188. { OC::QuadOp, "QuadOp", OCC::QuadOp, "quadOp", false, true, true, true, false, true, true, true, true, Attribute::ReadOnly, },
  189. // Bitcasts with different sizes void, h, f, d, i1, i8, i16, i32, i64 function attribute
  190. { OC::BitcastI16toF16, "BitcastI16toF16", OCC::BitcastI16toF16, "bitcastI16toF16", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  191. { OC::BitcastF16toI16, "BitcastF16toI16", OCC::BitcastF16toI16, "bitcastF16toI16", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  192. { OC::BitcastI32toF32, "BitcastI32toF32", OCC::BitcastI32toF32, "bitcastI32toF32", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  193. { OC::BitcastF32toI32, "BitcastF32toI32", OCC::BitcastF32toI32, "bitcastF32toI32", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  194. { OC::BitcastI64toF64, "BitcastI64toF64", OCC::BitcastI64toF64, "bitcastI64toF64", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  195. { OC::BitcastF64toI64, "BitcastF64toI64", OCC::BitcastF64toI64, "bitcastF64toI64", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  196. // Legacy floating-point void, h, f, d, i1, i8, i16, i32, i64 function attribute
  197. { OC::LegacyF32ToF16, "LegacyF32ToF16", OCC::LegacyF32ToF16, "legacyF32ToF16", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  198. { OC::LegacyF16ToF32, "LegacyF16ToF32", OCC::LegacyF16ToF32, "legacyF16ToF32", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  199. // Double precision void, h, f, d, i1, i8, i16, i32, i64 function attribute
  200. { OC::LegacyDoubleToFloat, "LegacyDoubleToFloat", OCC::LegacyDoubleToFloat, "legacyDoubleToFloat", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  201. { OC::LegacyDoubleToSInt32, "LegacyDoubleToSInt32", OCC::LegacyDoubleToSInt32, "legacyDoubleToSInt32", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  202. { OC::LegacyDoubleToUInt32, "LegacyDoubleToUInt32", OCC::LegacyDoubleToUInt32, "legacyDoubleToUInt32", true, false, false, false, false, false, false, false, false, Attribute::ReadNone, },
  203. // Wave void, h, f, d, i1, i8, i16, i32, i64 function attribute
  204. { OC::WaveAllBitCount, "WaveAllBitCount", OCC::WaveAllOp, "waveAllOp", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  205. { OC::WavePrefixBitCount, "WavePrefixBitCount", OCC::WavePrefixOp, "wavePrefixOp", true, false, false, false, false, false, false, false, false, Attribute::ReadOnly, },
  206. };
  207. // OPCODE-OLOADS:END
  208. const char *OP::m_OverloadTypeName[kNumTypeOverloads] = {
  209. "void", "f16", "f32", "f64", "i1", "i8", "i16", "i32", "i64"
  210. };
  211. const char *OP::m_NamePrefix = "dx.op.";
  212. // Keep sync with DXIL::AtomicBinOpCode
  213. static const char *AtomicBinOpCodeName[] = {
  214. "AtomicAdd",
  215. "AtomicAnd",
  216. "AtomicOr",
  217. "AtomicXor",
  218. "AtomicIMin",
  219. "AtomicIMax",
  220. "AtomicUMin",
  221. "AtomicUMax",
  222. "AtomicExchange",
  223. "AtomicInvalid" // Must be last.
  224. };
  225. unsigned OP::GetTypeSlot(Type *pType) {
  226. Type::TypeID T = pType->getTypeID();
  227. switch (T) {
  228. case Type::VoidTyID: return 0;
  229. case Type::HalfTyID: return 1;
  230. case Type::FloatTyID: return 2;
  231. case Type::DoubleTyID: return 3;
  232. case Type::IntegerTyID: {
  233. IntegerType *pIT = dyn_cast<IntegerType>(pType);
  234. unsigned Bits = pIT->getBitWidth();
  235. switch (Bits) {
  236. case 1: return 4;
  237. case 8: return 5;
  238. case 16: return 6;
  239. case 32: return 7;
  240. case 64: return 8;
  241. }
  242. }
  243. }
  244. return UINT_MAX;
  245. }
  246. const char *OP::GetOverloadTypeName(unsigned TypeSlot) {
  247. DXASSERT(TypeSlot < kNumTypeOverloads, "otherwise caller passed OOB index");
  248. return m_OverloadTypeName[TypeSlot];
  249. }
  250. const char *OP::GetOpCodeName(OpCode OpCode) {
  251. DXASSERT(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes, "otherwise caller passed OOB index");
  252. return m_OpCodeProps[(unsigned)OpCode].pOpCodeName;
  253. }
  254. const char *OP::GetAtomicOpName(DXIL::AtomicBinOpCode OpCode) {
  255. unsigned opcode = static_cast<unsigned>(OpCode);
  256. DXASSERT_LOCALVAR(opcode, opcode < static_cast<unsigned>(DXIL::AtomicBinOpCode::Invalid), "otherwise caller passed OOB index");
  257. return AtomicBinOpCodeName[static_cast<unsigned>(OpCode)];
  258. }
  259. const OP::OpCodeClass OP::GetOpCodeClass(OpCode OpCode) {
  260. DXASSERT(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes, "otherwise caller passed OOB index");
  261. return m_OpCodeProps[(unsigned)OpCode].OpCodeClass;
  262. }
  263. const char *OP::GetOpCodeClassName(OpCode OpCode) {
  264. DXASSERT(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes, "otherwise caller passed OOB index");
  265. return m_OpCodeProps[(unsigned)OpCode].pOpCodeClassName;
  266. }
  267. bool OP::IsOverloadLegal(OpCode OpCode, Type *pType) {
  268. DXASSERT(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes, "otherwise caller passed OOB index");
  269. unsigned TypeSlot = GetTypeSlot(pType);
  270. return TypeSlot != UINT_MAX && m_OpCodeProps[(unsigned)OpCode].bAllowOverload[TypeSlot];
  271. }
  272. bool OP::CheckOpCodeTable() {
  273. for (unsigned i = 0; i < (unsigned)OpCode::NumOpCodes; i++) {
  274. if ((unsigned)m_OpCodeProps[i].OpCode != i)
  275. return false;
  276. }
  277. return true;
  278. }
  279. bool OP::IsDxilOpFunc(const llvm::Function *F) {
  280. StringRef name = F->getName();
  281. return name.startswith(OP::m_NamePrefix);
  282. }
  283. bool OP::IsDxilOpFuncCallInst(const llvm::Instruction *I) {
  284. const CallInst *CI = dyn_cast<CallInst>(I);
  285. if (CI == nullptr) return false;
  286. return IsDxilOpFunc(CI->getCalledFunction());
  287. }
  288. bool OP::IsDxilOpFuncCallInst(const llvm::Instruction *I, OpCode opcode) {
  289. if (!IsDxilOpFuncCallInst(I)) return false;
  290. return llvm::cast<llvm::ConstantInt>(I->getOperand(0))->getZExtValue() == (unsigned)opcode;
  291. }
  292. OP::OpCode OP::GetDxilOpFuncCallInst(const llvm::Instruction *I) {
  293. DXASSERT(IsDxilOpFuncCallInst(I), "else caller didn't call IsDxilOpFuncCallInst to check");
  294. return (OP::OpCode)llvm::cast<llvm::ConstantInt>(I->getOperand(0))->getZExtValue();
  295. }
  296. bool OP::IsDxilOpWave(OpCode C) {
  297. unsigned op = (unsigned)C;
  298. /* <py::lines('OPCODE-WAVE')>hctdb_instrhelp.get_instrs_pred("op", "is_wave")</py>*/
  299. // OPCODE-WAVE:BEGIN
  300. // Instructions: WaveIsFirstLane=112, WaveGetLaneIndex=113,
  301. // WaveGetLaneCount=114, WaveAnyTrue=115, WaveAllTrue=116,
  302. // WaveActiveAllEqual=117, WaveActiveBallot=118, WaveReadLaneAt=119,
  303. // WaveReadLaneFirst=120, WaveActiveOp=121, WaveActiveBit=122,
  304. // WavePrefixOp=123, QuadReadLaneAt=124, QuadOp=125, WaveAllBitCount=137,
  305. // WavePrefixBitCount=138
  306. return 112 <= op && op <= 125 || 137 <= op && op <= 138;
  307. // OPCODE-WAVE:END
  308. }
  309. bool OP::IsDxilOpGradient(OpCode C) {
  310. unsigned op = (unsigned)C;
  311. /* <py::lines('OPCODE-GRADIENT')>hctdb_instrhelp.get_instrs_pred("op", "is_gradient")</py>*/
  312. // OPCODE-GRADIENT:BEGIN
  313. // Instructions: Sample=62, SampleBias=63, SampleCmp=66, TextureGather=75,
  314. // TextureGatherCmp=76, CalculateLOD=83, DerivCoarseX=85, DerivCoarseY=86,
  315. // DerivFineX=87, DerivFineY=88
  316. return 62 <= op && op <= 63 || op == 66 || 75 <= op && op <= 76 || op == 83 || 85 <= op && op <= 88;
  317. // OPCODE-GRADIENT:END
  318. }
  319. static Type *GetOrCreateStructType(LLVMContext &Ctx, ArrayRef<Type*> types, StringRef Name, Module *pModule) {
  320. if (StructType *ST = pModule->getTypeByName(Name)) {
  321. // TODO: validate the exist type match types if needed.
  322. return ST;
  323. }
  324. else
  325. return StructType::create(Ctx, types, Name);
  326. }
  327. //------------------------------------------------------------------------------
  328. //
  329. // OP methods.
  330. //
  331. OP::OP(LLVMContext &Ctx, Module *pModule)
  332. : m_Ctx(Ctx)
  333. , m_pModule(pModule) {
  334. memset(m_pResRetType, 0, sizeof(m_pResRetType));
  335. memset(m_pCBufferRetType, 0, sizeof(m_pCBufferRetType));
  336. memset(m_OpCodeClassCache, 0, sizeof(m_OpCodeClassCache));
  337. static_assert(_countof(OP::m_OpCodeProps) == (size_t)OP::OpCode::NumOpCodes, "forgot to update OP::m_OpCodeProps");
  338. m_pHandleType = GetOrCreateStructType(m_Ctx, Type::getInt8PtrTy(m_Ctx), "dx.types.Handle", pModule);
  339. Type *DimsType[4] = { Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx) };
  340. m_pDimensionsType = GetOrCreateStructType(m_Ctx, DimsType, "dx.types.Dimensions", pModule);
  341. Type *SamplePosType[2] = { Type::getFloatTy(m_Ctx), Type::getFloatTy(m_Ctx) };
  342. m_pSamplePosType = GetOrCreateStructType(m_Ctx, SamplePosType, "dx.types.SamplePos", pModule);
  343. Type *I32cTypes[2] = { Type::getInt32Ty(m_Ctx), Type::getInt1Ty(m_Ctx) };
  344. m_pBinaryWithCarryType = GetOrCreateStructType(m_Ctx, I32cTypes, "dx.types.i32c", pModule);
  345. Type *TwoI32Types[2] = { Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx) };
  346. m_pBinaryWithTwoOutputsType = GetOrCreateStructType(m_Ctx, TwoI32Types, "dx.types.twoi32", pModule);
  347. Type *SplitDoubleTypes[2] = { Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx) }; // Lo, Hi.
  348. m_pSplitDoubleType = GetOrCreateStructType(m_Ctx, SplitDoubleTypes, "dx.types.splitdouble", pModule);
  349. Type *Int4Types[4] = { Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx), Type::getInt32Ty(m_Ctx) }; // HiHi, HiLo, LoHi, LoLo
  350. m_pInt4Type = GetOrCreateStructType(m_Ctx, Int4Types, "dx.types.fouri32", pModule);
  351. }
  352. Function *OP::GetOpFunc(OpCode OpCode, Type *pOverloadType) {
  353. DXASSERT(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes, "otherwise caller passed OOB OpCode");
  354. _Analysis_assume_(0 <= (unsigned)OpCode && OpCode < OpCode::NumOpCodes);
  355. DXASSERT(IsOverloadLegal(OpCode, pOverloadType), "otherwise the caller requested illegal operation overload (eg HLSL function with unsupported types for mapped intrinsic function)");
  356. unsigned TypeSlot = GetTypeSlot(pOverloadType);
  357. Function *&F = m_OpCodeClassCache[(unsigned)m_OpCodeProps[(unsigned)OpCode].OpCodeClass].pOverloads[TypeSlot];
  358. if (F != nullptr)
  359. return F;
  360. vector<Type*> ArgTypes; // RetType is ArgTypes[0]
  361. Type *pETy = pOverloadType;
  362. Type *pRes = GetHandleType();
  363. Type *pDim = GetDimensionsType();
  364. Type *pPos = GetSamplePosType();
  365. Type *pV = Type::getVoidTy(m_Ctx);
  366. Type *pI1 = Type::getInt1Ty(m_Ctx);
  367. Type *pI8 = Type::getInt8Ty(m_Ctx);
  368. Type *pI16 = Type::getInt16Ty(m_Ctx);
  369. Type *pI32 = Type::getInt32Ty(m_Ctx);
  370. Type *pPI32 = Type::getInt32PtrTy(m_Ctx); (pPI32); // Currently unused.
  371. Type *pI64 = Type::getInt64Ty(m_Ctx); (pI64); // Currently unused.
  372. Type *pF16 = Type::getHalfTy(m_Ctx);
  373. Type *pF32 = Type::getFloatTy(m_Ctx);
  374. Type *pPF32 = Type::getFloatPtrTy(m_Ctx);
  375. Type *pI32C = GetBinaryWithCarryType();
  376. Type *p2I32 = GetBinaryWithTwoOutputsType();
  377. Type *pF64 = Type::getDoubleTy(m_Ctx);
  378. Type *pSDT = GetSplitDoubleType(); // Split double type.
  379. Type *pI4S = GetInt4Type(); // 4 i32s in a struct.
  380. std::string funcName = (Twine(OP::m_NamePrefix) + Twine(GetOpCodeClassName(OpCode))).str();
  381. // Add ret type to the name.
  382. if (pOverloadType != pV) {
  383. funcName = Twine(funcName).concat(".").concat(GetOverloadTypeName(TypeSlot)).str();
  384. }
  385. // Try to find exist function with the same name in the module.
  386. if (Function *existF = m_pModule->getFunction(funcName)) {
  387. F = existF;
  388. return F;
  389. }
  390. #define A(_x) ArgTypes.emplace_back(_x)
  391. #define RRT(_y) A(GetResRetType(_y))
  392. #define CBRT(_y) A(GetCBufferRetType(_y))
  393. /* <py::lines('OPCODE-OLOAD-FUNCS')>hctdb_instrhelp.get_oloads_funcs()</py>*/
  394. switch (OpCode) { // return OpCode
  395. // OPCODE-OLOAD-FUNCS:BEGIN
  396. // Temporary, indexable, input, output registers
  397. case OpCode::TempRegLoad: A(pETy); A(pI32); A(pI32); break;
  398. case OpCode::TempRegStore: A(pV); A(pI32); A(pI32); A(pETy); break;
  399. case OpCode::MinPrecXRegLoad: A(pETy); A(pI32); A(pPF32);A(pI32); A(pI8); break;
  400. case OpCode::MinPrecXRegStore: A(pV); A(pI32); A(pPF32);A(pI32); A(pI8); A(pETy); break;
  401. case OpCode::LoadInput: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); A(pI32); break;
  402. case OpCode::StoreOutput: A(pV); A(pI32); A(pI32); A(pI32); A(pI8); A(pETy); break;
  403. // Unary float
  404. case OpCode::FAbs: A(pETy); A(pI32); A(pETy); break;
  405. case OpCode::Saturate: A(pETy); A(pI32); A(pETy); break;
  406. case OpCode::IsNaN: A(pI1); A(pI32); A(pETy); break;
  407. case OpCode::IsInf: A(pI1); A(pI32); A(pETy); break;
  408. case OpCode::IsFinite: A(pI1); A(pI32); A(pETy); break;
  409. case OpCode::IsNormal: A(pI1); A(pI32); A(pETy); break;
  410. case OpCode::Cos: A(pETy); A(pI32); A(pETy); break;
  411. case OpCode::Sin: A(pETy); A(pI32); A(pETy); break;
  412. case OpCode::Tan: A(pETy); A(pI32); A(pETy); break;
  413. case OpCode::Acos: A(pETy); A(pI32); A(pETy); break;
  414. case OpCode::Asin: A(pETy); A(pI32); A(pETy); break;
  415. case OpCode::Atan: A(pETy); A(pI32); A(pETy); break;
  416. case OpCode::Hcos: A(pETy); A(pI32); A(pETy); break;
  417. case OpCode::Hsin: A(pETy); A(pI32); A(pETy); break;
  418. case OpCode::Htan: A(pETy); A(pI32); A(pETy); break;
  419. case OpCode::Exp: A(pETy); A(pI32); A(pETy); break;
  420. case OpCode::Frc: A(pETy); A(pI32); A(pETy); break;
  421. case OpCode::Log: A(pETy); A(pI32); A(pETy); break;
  422. case OpCode::Sqrt: A(pETy); A(pI32); A(pETy); break;
  423. case OpCode::Rsqrt: A(pETy); A(pI32); A(pETy); break;
  424. // Unary float - rounding
  425. case OpCode::Round_ne: A(pETy); A(pI32); A(pETy); break;
  426. case OpCode::Round_ni: A(pETy); A(pI32); A(pETy); break;
  427. case OpCode::Round_pi: A(pETy); A(pI32); A(pETy); break;
  428. case OpCode::Round_z: A(pETy); A(pI32); A(pETy); break;
  429. // Unary int
  430. case OpCode::Bfrev: A(pETy); A(pI32); A(pETy); break;
  431. case OpCode::Countbits: A(pI32); A(pI32); A(pETy); break;
  432. case OpCode::FirstbitLo: A(pI32); A(pI32); A(pETy); break;
  433. case OpCode::FirstbitHi: A(pI32); A(pI32); A(pETy); break;
  434. case OpCode::FirstbitSHi: A(pI32); A(pI32); A(pETy); break;
  435. // Binary float
  436. case OpCode::FMax: A(pETy); A(pI32); A(pETy); A(pETy); break;
  437. case OpCode::FMin: A(pETy); A(pI32); A(pETy); A(pETy); break;
  438. // Binary int
  439. case OpCode::IMax: A(pETy); A(pI32); A(pETy); A(pETy); break;
  440. case OpCode::IMin: A(pETy); A(pI32); A(pETy); A(pETy); break;
  441. case OpCode::UMax: A(pETy); A(pI32); A(pETy); A(pETy); break;
  442. case OpCode::UMin: A(pETy); A(pI32); A(pETy); A(pETy); break;
  443. // Binary int with two outputs
  444. case OpCode::IMul: A(p2I32); A(pI32); A(pETy); A(pETy); break;
  445. case OpCode::UMul: A(p2I32); A(pI32); A(pETy); A(pETy); break;
  446. case OpCode::UDiv: A(p2I32); A(pI32); A(pETy); A(pETy); break;
  447. // Binary int with carry
  448. case OpCode::IAddc: A(pI32C); A(pI32); A(pETy); A(pETy); break;
  449. case OpCode::UAddc: A(pI32C); A(pI32); A(pETy); A(pETy); break;
  450. case OpCode::ISubc: A(pI32C); A(pI32); A(pETy); A(pETy); break;
  451. case OpCode::USubc: A(pI32C); A(pI32); A(pETy); A(pETy); break;
  452. // Tertiary float
  453. case OpCode::FMad: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  454. case OpCode::Fma: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  455. // Tertiary int
  456. case OpCode::IMad: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  457. case OpCode::UMad: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  458. case OpCode::Msad: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  459. case OpCode::Ibfe: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  460. case OpCode::Ubfe: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); break;
  461. // Quaternary
  462. case OpCode::Bfi: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); break;
  463. // Dot
  464. case OpCode::Dot2: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); break;
  465. case OpCode::Dot3: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); break;
  466. case OpCode::Dot4: A(pETy); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); A(pETy); break;
  467. // Resources
  468. case OpCode::CreateHandle: A(pRes); A(pI32); A(pI8); A(pI32); A(pI32); A(pI1); break;
  469. case OpCode::CBufferLoad: A(pETy); A(pI32); A(pRes); A(pI32); A(pI32); break;
  470. case OpCode::CBufferLoadLegacy: CBRT(pETy); A(pI32); A(pRes); A(pI32); break;
  471. // Resources - sample
  472. 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;
  473. 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;
  474. 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;
  475. 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;
  476. 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;
  477. 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;
  478. // Resources
  479. case OpCode::TextureLoad: RRT(pETy); A(pI32); A(pRes); A(pI32); A(pI32); A(pI32); A(pI32); A(pI32); A(pI32); A(pI32); break;
  480. 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;
  481. case OpCode::BufferLoad: RRT(pETy); A(pI32); A(pRes); A(pI32); A(pI32); break;
  482. case OpCode::BufferStore: A(pV); A(pI32); A(pRes); A(pI32); A(pI32); A(pETy); A(pETy); A(pETy); A(pETy); A(pI8); break;
  483. case OpCode::BufferUpdateCounter: A(pI32); A(pI32); A(pRes); A(pI8); break;
  484. case OpCode::CheckAccessFullyMapped: A(pI1); A(pI32); A(pI32); break;
  485. case OpCode::GetDimensions: A(pDim); A(pI32); A(pRes); A(pI32); break;
  486. // Resources - gather
  487. 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;
  488. 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;
  489. // Resources - sample
  490. case OpCode::Texture2DMSGetSamplePosition:A(pPos); A(pI32); A(pRes); A(pI32); break;
  491. case OpCode::RenderTargetGetSamplePosition:A(pPos); A(pI32); A(pI32); break;
  492. case OpCode::RenderTargetGetSampleCount:A(pI32); A(pI32); break;
  493. // Synchronization
  494. case OpCode::AtomicBinOp: A(pI32); A(pI32); A(pRes); A(pI32); A(pI32); A(pI32); A(pI32); A(pI32); break;
  495. case OpCode::AtomicCompareExchange: A(pI32); A(pI32); A(pRes); A(pI32); A(pI32); A(pI32); A(pI32); A(pI32); break;
  496. case OpCode::Barrier: A(pV); A(pI32); A(pI32); break;
  497. // Pixel shader
  498. case OpCode::CalculateLOD: A(pF32); A(pI32); A(pRes); A(pRes); A(pF32); A(pF32); A(pF32); A(pI1); break;
  499. case OpCode::Discard: A(pV); A(pI32); A(pI1); break;
  500. case OpCode::DerivCoarseX: A(pETy); A(pI32); A(pETy); break;
  501. case OpCode::DerivCoarseY: A(pETy); A(pI32); A(pETy); break;
  502. case OpCode::DerivFineX: A(pETy); A(pI32); A(pETy); break;
  503. case OpCode::DerivFineY: A(pETy); A(pI32); A(pETy); break;
  504. case OpCode::EvalSnapped: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); A(pI32); A(pI32); break;
  505. case OpCode::EvalSampleIndex: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); A(pI32); break;
  506. case OpCode::EvalCentroid: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); break;
  507. case OpCode::SampleIndex: A(pI32); A(pI32); break;
  508. case OpCode::Coverage: A(pI32); A(pI32); break;
  509. case OpCode::InnerCoverage: A(pI32); A(pI32); break;
  510. // Compute shader
  511. case OpCode::ThreadId: A(pI32); A(pI32); A(pI32); break;
  512. case OpCode::GroupId: A(pI32); A(pI32); A(pI32); break;
  513. case OpCode::ThreadIdInGroup: A(pI32); A(pI32); A(pI32); break;
  514. case OpCode::FlattenedThreadIdInGroup:A(pI32); A(pI32); break;
  515. // Geometry shader
  516. case OpCode::EmitStream: A(pV); A(pI32); A(pI8); break;
  517. case OpCode::CutStream: A(pV); A(pI32); A(pI8); break;
  518. case OpCode::EmitThenCutStream: A(pV); A(pI32); A(pI8); break;
  519. case OpCode::GSInstanceID: A(pI32); A(pI32); break;
  520. // Double precision
  521. case OpCode::MakeDouble: A(pF64); A(pI32); A(pI32); A(pI32); break;
  522. case OpCode::SplitDouble: A(pSDT); A(pI32); A(pF64); break;
  523. // Domain and hull shader
  524. case OpCode::LoadOutputControlPoint: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); A(pI32); break;
  525. case OpCode::LoadPatchConstant: A(pETy); A(pI32); A(pI32); A(pI32); A(pI8); break;
  526. // Domain shader
  527. case OpCode::DomainLocation: A(pF32); A(pI32); A(pI8); break;
  528. // Hull shader
  529. case OpCode::StorePatchConstant: A(pV); A(pI32); A(pI32); A(pI32); A(pI8); A(pETy); break;
  530. case OpCode::OutputControlPointID: A(pI32); A(pI32); break;
  531. case OpCode::PrimitiveID: A(pI32); A(pI32); break;
  532. // Other
  533. case OpCode::CycleCounterLegacy: A(p2I32); A(pI32); break;
  534. // Wave
  535. case OpCode::WaveIsFirstLane: A(pI1); A(pI32); break;
  536. case OpCode::WaveGetLaneIndex: A(pI32); A(pI32); break;
  537. case OpCode::WaveGetLaneCount: A(pI32); A(pI32); break;
  538. case OpCode::WaveAnyTrue: A(pI1); A(pI32); A(pI1); break;
  539. case OpCode::WaveAllTrue: A(pI1); A(pI32); A(pI1); break;
  540. case OpCode::WaveActiveAllEqual: A(pI1); A(pI32); A(pETy); break;
  541. case OpCode::WaveActiveBallot: A(pI4S); A(pI32); A(pI1); break;
  542. case OpCode::WaveReadLaneAt: A(pETy); A(pI32); A(pETy); A(pI32); break;
  543. case OpCode::WaveReadLaneFirst: A(pETy); A(pI32); A(pETy); break;
  544. case OpCode::WaveActiveOp: A(pETy); A(pI32); A(pETy); A(pI8); A(pI8); break;
  545. case OpCode::WaveActiveBit: A(pETy); A(pI32); A(pETy); A(pI8); break;
  546. case OpCode::WavePrefixOp: A(pETy); A(pI32); A(pETy); A(pI8); A(pI8); break;
  547. case OpCode::QuadReadLaneAt: A(pETy); A(pI32); A(pETy); A(pI32); break;
  548. case OpCode::QuadOp: A(pETy); A(pI32); A(pETy); A(pI8); break;
  549. // Bitcasts with different sizes
  550. case OpCode::BitcastI16toF16: A(pF16); A(pI32); A(pI16); break;
  551. case OpCode::BitcastF16toI16: A(pI16); A(pI32); A(pF16); break;
  552. case OpCode::BitcastI32toF32: A(pF32); A(pI32); A(pI32); break;
  553. case OpCode::BitcastF32toI32: A(pI32); A(pI32); A(pF32); break;
  554. case OpCode::BitcastI64toF64: A(pF64); A(pI32); A(pI64); break;
  555. case OpCode::BitcastF64toI64: A(pI64); A(pI32); A(pF64); break;
  556. // Legacy floating-point
  557. case OpCode::LegacyF32ToF16: A(pI32); A(pI32); A(pF32); break;
  558. case OpCode::LegacyF16ToF32: A(pF32); A(pI32); A(pI32); break;
  559. // Double precision
  560. case OpCode::LegacyDoubleToFloat: A(pF32); A(pI32); A(pF64); break;
  561. case OpCode::LegacyDoubleToSInt32: A(pI32); A(pI32); A(pF64); break;
  562. case OpCode::LegacyDoubleToUInt32: A(pI32); A(pI32); A(pF64); break;
  563. // Wave
  564. case OpCode::WaveAllBitCount: A(pI32); A(pI32); A(pI1); break;
  565. case OpCode::WavePrefixBitCount: A(pI32); A(pI32); A(pI1); break;
  566. // OPCODE-OLOAD-FUNCS:END
  567. default: DXASSERT(false, "otherwise unhandled case"); break;
  568. }
  569. #undef RRT
  570. #undef A
  571. FunctionType *pFT;
  572. DXASSERT(ArgTypes.size() > 1, "otherwise forgot to initialize arguments");
  573. pFT = FunctionType::get(ArgTypes[0], ArrayRef<Type*>(&ArgTypes[1], ArgTypes.size()-1), false);
  574. if (pOverloadType != pV) {
  575. F = Function::Create(pFT, GlobalValue::LinkageTypes::ExternalLinkage,
  576. funcName,
  577. m_pModule);
  578. } else {
  579. F = Function::Create(pFT, GlobalValue::LinkageTypes::ExternalLinkage,
  580. funcName,
  581. m_pModule);
  582. }
  583. F->setCallingConv(CallingConv::C);
  584. F->addFnAttr(Attribute::NoUnwind);
  585. if (m_OpCodeProps[(unsigned)OpCode].FuncAttr != Attribute::None)
  586. F->addFnAttr(m_OpCodeProps[(unsigned)OpCode].FuncAttr);
  587. return F;
  588. }
  589. Type *OP::GetHandleType() const {
  590. return m_pHandleType;
  591. }
  592. Type *OP::GetDimensionsType() const
  593. {
  594. return m_pDimensionsType;
  595. }
  596. Type *OP::GetSamplePosType() const
  597. {
  598. return m_pSamplePosType;
  599. }
  600. Type *OP::GetBinaryWithCarryType() const {
  601. return m_pBinaryWithCarryType;
  602. }
  603. Type *OP::GetBinaryWithTwoOutputsType() const {
  604. return m_pBinaryWithTwoOutputsType;
  605. }
  606. Type *OP::GetSplitDoubleType() const {
  607. return m_pSplitDoubleType;
  608. }
  609. Type *OP::GetInt4Type() const {
  610. return m_pInt4Type;
  611. }
  612. Type *OP::GetResRetType(Type *pOverloadType) {
  613. unsigned TypeSlot = GetTypeSlot(pOverloadType);
  614. if (m_pResRetType[TypeSlot] == nullptr) {
  615. string TypeName("dx.types.ResRet.");
  616. TypeName += GetOverloadTypeName(TypeSlot);
  617. Type *FieldTypes[5] = { pOverloadType, pOverloadType, pOverloadType, pOverloadType, Type::getInt32Ty(m_Ctx) };
  618. m_pResRetType[TypeSlot] = GetOrCreateStructType(m_Ctx, FieldTypes, TypeName, m_pModule);
  619. }
  620. return m_pResRetType[TypeSlot];
  621. }
  622. Type *OP::GetCBufferRetType(Type *pOverloadType) {
  623. unsigned TypeSlot = GetTypeSlot(pOverloadType);
  624. if (m_pCBufferRetType[TypeSlot] == nullptr) {
  625. string TypeName("dx.types.CBufRet.");
  626. TypeName += GetOverloadTypeName(TypeSlot);
  627. if (!pOverloadType->isDoubleTy()) {
  628. Type *FieldTypes[4] = { pOverloadType, pOverloadType, pOverloadType, pOverloadType };
  629. m_pCBufferRetType[TypeSlot] = GetOrCreateStructType(m_Ctx, FieldTypes, TypeName, m_pModule);
  630. } else {
  631. Type *FieldTypes[2] = { pOverloadType, pOverloadType };
  632. m_pCBufferRetType[TypeSlot] = GetOrCreateStructType(m_Ctx, FieldTypes, TypeName, m_pModule);
  633. }
  634. }
  635. return m_pCBufferRetType[TypeSlot];
  636. }
  637. //------------------------------------------------------------------------------
  638. //
  639. // LLVM utility methods.
  640. //
  641. Constant *OP::GetI1Const(bool v) {
  642. return Constant::getIntegerValue(IntegerType::get(m_Ctx, 1), APInt(1, v));
  643. }
  644. Constant *OP::GetI8Const(char v) {
  645. return Constant::getIntegerValue(IntegerType::get(m_Ctx, 8), APInt(8, v));
  646. }
  647. Constant *OP::GetU8Const(unsigned char v) {
  648. return GetI8Const((char)v);
  649. }
  650. Constant *OP::GetI16Const(int v) {
  651. return Constant::getIntegerValue(IntegerType::get(m_Ctx, 16), APInt(16, v));
  652. }
  653. Constant *OP::GetU16Const(unsigned v) {
  654. return GetI16Const((int)v);
  655. }
  656. Constant *OP::GetI32Const(int v) {
  657. return Constant::getIntegerValue(IntegerType::get(m_Ctx, 32), APInt(32, v));
  658. }
  659. Constant *OP::GetU32Const(unsigned v) {
  660. return GetI32Const((int)v);
  661. }
  662. Constant *OP::GetU64Const(unsigned long long v) {
  663. return Constant::getIntegerValue(IntegerType::get(m_Ctx, 64), APInt(64, v));
  664. }
  665. Constant *OP::GetFloatConst(float v) {
  666. return ConstantFP::get(m_Ctx, APFloat(v));
  667. }
  668. Constant *OP::GetDoubleConst(double v) {
  669. return ConstantFP::get(m_Ctx, APFloat(v));
  670. }
  671. } // namespace hlsl