DxilOperations.cpp 67 KB

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