DxilOperations.cpp 73 KB

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