DxilOperations.cpp 71 KB

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