DxilOperations.cpp 83 KB

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