DxilOperations.cpp 70 KB

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