DeclResultIdMapper.cpp 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477
  1. //===--- DeclResultIdMapper.cpp - DeclResultIdMapper impl --------*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "DeclResultIdMapper.h"
  10. #include <algorithm>
  11. #include <cstring>
  12. #include <sstream>
  13. #include <unordered_map>
  14. #include "dxc/HLSL/DxilConstants.h"
  15. #include "dxc/HLSL/DxilTypeSystem.h"
  16. #include "clang/AST/Expr.h"
  17. #include "clang/AST/HlslTypes.h"
  18. #include "clang/AST/RecursiveASTVisitor.h"
  19. #include "llvm/ADT/SmallBitVector.h"
  20. #include "llvm/ADT/StringSet.h"
  21. namespace clang {
  22. namespace spirv {
  23. namespace {
  24. /// \brief Returns the stage variable's register assignment for the given Decl.
  25. const hlsl::RegisterAssignment *getResourceBinding(const NamedDecl *decl) {
  26. for (auto *annotation : decl->getUnusualAnnotations()) {
  27. if (auto *reg = dyn_cast<hlsl::RegisterAssignment>(annotation)) {
  28. return reg;
  29. }
  30. }
  31. return nullptr;
  32. }
  33. /// \brief Returns true if the given declaration has a primitive type qualifier.
  34. /// Returns false otherwise.
  35. inline bool hasGSPrimitiveTypeQualifier(const Decl *decl) {
  36. return decl->hasAttr<HLSLTriangleAttr>() ||
  37. decl->hasAttr<HLSLTriangleAdjAttr>() ||
  38. decl->hasAttr<HLSLPointAttr>() || decl->hasAttr<HLSLLineAttr>() ||
  39. decl->hasAttr<HLSLLineAdjAttr>();
  40. }
  41. /// \brief Deduces the parameter qualifier for the given decl.
  42. hlsl::DxilParamInputQual deduceParamQual(const DeclaratorDecl *decl,
  43. bool asInput) {
  44. const auto type = decl->getType();
  45. if (hlsl::IsHLSLInputPatchType(type))
  46. return hlsl::DxilParamInputQual::InputPatch;
  47. if (hlsl::IsHLSLOutputPatchType(type))
  48. return hlsl::DxilParamInputQual::OutputPatch;
  49. // TODO: Add support for multiple output streams.
  50. if (hlsl::IsHLSLStreamOutputType(type))
  51. return hlsl::DxilParamInputQual::OutStream0;
  52. // The inputs to the geometry shader that have a primitive type qualifier
  53. // must use 'InputPrimitive'.
  54. if (hasGSPrimitiveTypeQualifier(decl))
  55. return hlsl::DxilParamInputQual::InputPrimitive;
  56. return asInput ? hlsl::DxilParamInputQual::In : hlsl::DxilParamInputQual::Out;
  57. }
  58. /// \brief Deduces the HLSL SigPoint for the given decl appearing in the given
  59. /// shader model.
  60. const hlsl::SigPoint *deduceSigPoint(const DeclaratorDecl *decl, bool asInput,
  61. const hlsl::ShaderModel::Kind kind,
  62. bool forPCF) {
  63. return hlsl::SigPoint::GetSigPoint(hlsl::SigPointFromInputQual(
  64. deduceParamQual(decl, asInput), kind, forPCF));
  65. }
  66. /// Returns the type of the given decl. If the given decl is a FunctionDecl,
  67. /// returns its result type.
  68. inline QualType getTypeOrFnRetType(const DeclaratorDecl *decl) {
  69. if (const auto *funcDecl = dyn_cast<FunctionDecl>(decl)) {
  70. return funcDecl->getReturnType();
  71. }
  72. return decl->getType();
  73. }
  74. /// Returns the number of base classes if this type is a derived class/struct.
  75. /// Returns zero otherwise.
  76. inline uint32_t getNumBaseClasses(QualType type) {
  77. if (const auto *cxxDecl = type->getAsCXXRecordDecl())
  78. return cxxDecl->getNumBases();
  79. return 0;
  80. }
  81. } // anonymous namespace
  82. std::string StageVar::getSemanticStr() const {
  83. // A special case for zero index, which is equivalent to no index.
  84. // Use what is in the source code.
  85. // TODO: this looks like a hack to make the current tests happy.
  86. // Should consider remove it and fix all tests.
  87. if (semanticInfo.index == 0)
  88. return semanticInfo.str;
  89. std::ostringstream ss;
  90. ss << semanticInfo.name.str() << semanticInfo.index;
  91. return ss.str();
  92. }
  93. uint32_t CounterIdAliasPair::get(ModuleBuilder &builder,
  94. TypeTranslator &translator) const {
  95. if (isAlias) {
  96. const uint32_t counterVarType = builder.getPointerType(
  97. translator.getACSBufferCounter(), spv::StorageClass::Uniform);
  98. return builder.createLoad(counterVarType, resultId);
  99. }
  100. return resultId;
  101. }
  102. const CounterIdAliasPair *
  103. CounterVarFields::get(const llvm::SmallVectorImpl<uint32_t> &indices) const {
  104. for (const auto &field : fields)
  105. if (field.indices == indices)
  106. return &field.counterVar;
  107. return nullptr;
  108. }
  109. bool CounterVarFields::assign(const CounterVarFields &srcFields,
  110. ModuleBuilder &builder,
  111. TypeTranslator &translator) const {
  112. for (const auto &field : fields) {
  113. const auto *srcField = srcFields.get(field.indices);
  114. if (!srcField)
  115. return false;
  116. field.counterVar.assign(*srcField, builder, translator);
  117. }
  118. return true;
  119. }
  120. bool CounterVarFields::assign(const CounterVarFields &srcFields,
  121. const llvm::SmallVector<uint32_t, 4> &dstPrefix,
  122. const llvm::SmallVector<uint32_t, 4> &srcPrefix,
  123. ModuleBuilder &builder,
  124. TypeTranslator &translator) const {
  125. if (dstPrefix.empty() && srcPrefix.empty())
  126. return assign(srcFields, builder, translator);
  127. llvm::SmallVector<uint32_t, 4> srcIndices = srcPrefix;
  128. // If whole has the given prefix, appends all elements after the prefix in
  129. // whole to srcIndices.
  130. const auto applyDiff =
  131. [&srcIndices](const llvm::SmallVector<uint32_t, 4> &whole,
  132. const llvm::SmallVector<uint32_t, 4> &prefix) -> bool {
  133. uint32_t i = 0;
  134. for (; i < prefix.size(); ++i)
  135. if (whole[i] != prefix[i]) {
  136. break;
  137. }
  138. if (i == prefix.size()) {
  139. for (; i < whole.size(); ++i)
  140. srcIndices.push_back(whole[i]);
  141. return true;
  142. }
  143. return false;
  144. };
  145. for (const auto &field : fields)
  146. if (applyDiff(field.indices, dstPrefix)) {
  147. const auto *srcField = srcFields.get(srcIndices);
  148. if (!srcField)
  149. return false;
  150. field.counterVar.assign(*srcField, builder, translator);
  151. for (uint32_t i = srcPrefix.size(); i < srcIndices.size(); ++i)
  152. srcIndices.pop_back();
  153. }
  154. return true;
  155. }
  156. SemanticInfo DeclResultIdMapper::getStageVarSemantic(const NamedDecl *decl) {
  157. for (auto *annotation : decl->getUnusualAnnotations()) {
  158. if (auto *sema = dyn_cast<hlsl::SemanticDecl>(annotation)) {
  159. llvm::StringRef semanticStr = sema->SemanticName;
  160. llvm::StringRef semanticName;
  161. uint32_t index = 0;
  162. hlsl::Semantic::DecomposeNameAndIndex(semanticStr, &semanticName, &index);
  163. const auto *semantic = hlsl::Semantic::GetByName(semanticName);
  164. return {semanticStr, semantic, semanticName, index, sema->Loc};
  165. }
  166. }
  167. return {};
  168. }
  169. bool DeclResultIdMapper::createStageOutputVar(const DeclaratorDecl *decl,
  170. uint32_t storedValue,
  171. bool forPCF) {
  172. QualType type = getTypeOrFnRetType(decl);
  173. // Output stream types (PointStream, LineStream, TriangleStream) are
  174. // translated as their underlying struct types.
  175. if (hlsl::IsHLSLStreamOutputType(type))
  176. type = hlsl::GetHLSLResourceResultType(type);
  177. const auto *sigPoint =
  178. deduceSigPoint(decl, /*asInput=*/false, shaderModel.GetKind(), forPCF);
  179. // HS output variables are created using the other overload. For the rest,
  180. // none of them should be created as arrays.
  181. assert(sigPoint->GetKind() != hlsl::DXIL::SigPointKind::HSCPOut);
  182. SemanticInfo inheritSemantic = {};
  183. return createStageVars(sigPoint, decl, /*asInput=*/false, type,
  184. /*arraySize=*/0, "out.var", llvm::None, &storedValue,
  185. // Write back of stage output variables in GS is
  186. // manually controlled by .Append() intrinsic method,
  187. // implemented in writeBackOutputStream(). So
  188. // noWriteBack should be set to true for GS.
  189. shaderModel.IsGS(), &inheritSemantic);
  190. }
  191. bool DeclResultIdMapper::createStageOutputVar(const DeclaratorDecl *decl,
  192. uint32_t arraySize,
  193. uint32_t invocationId,
  194. uint32_t storedValue) {
  195. assert(shaderModel.IsHS());
  196. QualType type = getTypeOrFnRetType(decl);
  197. const auto *sigPoint =
  198. hlsl::SigPoint::GetSigPoint(hlsl::DXIL::SigPointKind::HSCPOut);
  199. SemanticInfo inheritSemantic = {};
  200. return createStageVars(sigPoint, decl, /*asInput=*/false, type, arraySize,
  201. "out.var", invocationId, &storedValue,
  202. /*noWriteBack=*/false, &inheritSemantic);
  203. }
  204. bool DeclResultIdMapper::createStageInputVar(const ParmVarDecl *paramDecl,
  205. uint32_t *loadedValue,
  206. bool forPCF) {
  207. uint32_t arraySize = 0;
  208. QualType type = paramDecl->getType();
  209. // Deprive the outermost arrayness for HS/DS/GS and use arraySize
  210. // to convey that information
  211. if (hlsl::IsHLSLInputPatchType(type)) {
  212. arraySize = hlsl::GetHLSLInputPatchCount(type);
  213. type = hlsl::GetHLSLInputPatchElementType(type);
  214. } else if (hlsl::IsHLSLOutputPatchType(type)) {
  215. arraySize = hlsl::GetHLSLOutputPatchCount(type);
  216. type = hlsl::GetHLSLOutputPatchElementType(type);
  217. }
  218. if (hasGSPrimitiveTypeQualifier(paramDecl)) {
  219. const auto *typeDecl = astContext.getAsConstantArrayType(type);
  220. arraySize = static_cast<uint32_t>(typeDecl->getSize().getZExtValue());
  221. type = typeDecl->getElementType();
  222. }
  223. const auto *sigPoint = deduceSigPoint(paramDecl, /*asInput=*/true,
  224. shaderModel.GetKind(), forPCF);
  225. SemanticInfo inheritSemantic = {};
  226. return createStageVars(sigPoint, paramDecl, /*asInput=*/true, type, arraySize,
  227. "in.var", llvm::None, loadedValue,
  228. /*noWriteBack=*/false, &inheritSemantic);
  229. }
  230. const DeclResultIdMapper::DeclSpirvInfo *
  231. DeclResultIdMapper::getDeclSpirvInfo(const ValueDecl *decl) const {
  232. auto it = astDecls.find(decl);
  233. if (it != astDecls.end())
  234. return &it->second;
  235. return nullptr;
  236. }
  237. SpirvEvalInfo DeclResultIdMapper::getDeclEvalInfo(const ValueDecl *decl) {
  238. if (const auto *info = getDeclSpirvInfo(decl)) {
  239. if (info->indexInCTBuffer >= 0) {
  240. // If this is a VarDecl inside a HLSLBufferDecl, we need to do an extra
  241. // OpAccessChain to get the pointer to the variable since we created
  242. // a single variable for the whole buffer object.
  243. const uint32_t varType = typeTranslator.translateType(
  244. // Should only have VarDecls in a HLSLBufferDecl.
  245. cast<VarDecl>(decl)->getType(),
  246. // We need to set decorateLayout here to avoid creating SPIR-V
  247. // instructions for the current type without decorations.
  248. info->info.getLayoutRule());
  249. const uint32_t elemId = theBuilder.createAccessChain(
  250. theBuilder.getPointerType(varType, info->info.getStorageClass()),
  251. info->info, {theBuilder.getConstantInt32(info->indexInCTBuffer)});
  252. return info->info.substResultId(elemId);
  253. } else {
  254. return *info;
  255. }
  256. }
  257. emitFatalError("found unregistered decl", decl->getLocation())
  258. << decl->getName();
  259. emitNote("please file a bug report on "
  260. "https://github.com/Microsoft/DirectXShaderCompiler/issues with "
  261. "source code if possible",
  262. {});
  263. return 0;
  264. }
  265. uint32_t DeclResultIdMapper::createFnParam(const ParmVarDecl *param) {
  266. bool isAlias = false;
  267. auto &info = astDecls[param].info;
  268. const uint32_t type =
  269. getTypeAndCreateCounterForPotentialAliasVar(param, &isAlias, &info);
  270. const uint32_t ptrType =
  271. theBuilder.getPointerType(type, spv::StorageClass::Function);
  272. const uint32_t id = theBuilder.addFnParam(ptrType, param->getName());
  273. info.setResultId(id);
  274. return id;
  275. }
  276. void DeclResultIdMapper::createCounterVarForDecl(const DeclaratorDecl *decl) {
  277. const QualType declType = getTypeOrFnRetType(decl);
  278. if (!counterVars.count(decl) &&
  279. TypeTranslator::isRWAppendConsumeSBuffer(declType)) {
  280. createCounterVar(decl, /*declId=*/0, /*isAlias=*/true);
  281. } else if (!fieldCounterVars.count(decl) && declType->isStructureType() &&
  282. // Exclude other resource types which are represented as structs
  283. !hlsl::IsHLSLResourceType(declType)) {
  284. createFieldCounterVars(decl);
  285. }
  286. }
  287. SpirvEvalInfo DeclResultIdMapper::createFnVar(const VarDecl *var,
  288. llvm::Optional<uint32_t> init) {
  289. bool isAlias = false;
  290. auto &info = astDecls[var].info;
  291. const uint32_t type =
  292. getTypeAndCreateCounterForPotentialAliasVar(var, &isAlias, &info);
  293. const uint32_t id = theBuilder.addFnVar(type, var->getName(), init);
  294. info.setResultId(id);
  295. return info;
  296. }
  297. SpirvEvalInfo DeclResultIdMapper::createFileVar(const VarDecl *var,
  298. llvm::Optional<uint32_t> init) {
  299. bool isAlias = false;
  300. auto &info = astDecls[var].info;
  301. const uint32_t type =
  302. getTypeAndCreateCounterForPotentialAliasVar(var, &isAlias, &info);
  303. const uint32_t id = theBuilder.addModuleVar(type, spv::StorageClass::Private,
  304. var->getName(), init);
  305. info.setResultId(id).setStorageClass(spv::StorageClass::Private);
  306. return info;
  307. }
  308. SpirvEvalInfo DeclResultIdMapper::createExternVar(const VarDecl *var) {
  309. auto storageClass = spv::StorageClass::UniformConstant;
  310. auto rule = LayoutRule::Void;
  311. bool isACRWSBuffer = false; // Whether is {Append|Consume|RW}StructuredBuffer
  312. if (var->getAttr<HLSLGroupSharedAttr>()) {
  313. // For CS groupshared variables
  314. storageClass = spv::StorageClass::Workgroup;
  315. } else if (TypeTranslator::isResourceType(var)) {
  316. // See through the possible outer arrays
  317. QualType resourceType = var->getType();
  318. while (resourceType->isArrayType()) {
  319. resourceType = resourceType->getAsArrayTypeUnsafe()->getElementType();
  320. }
  321. const llvm::StringRef typeName =
  322. resourceType->getAs<RecordType>()->getDecl()->getName();
  323. // These types are all translated into OpTypeStruct with BufferBlock
  324. // decoration. They should follow standard storage buffer layout,
  325. // which GLSL std430 rules statisfies.
  326. if (typeName == "StructuredBuffer" || typeName == "ByteAddressBuffer" ||
  327. typeName == "RWByteAddressBuffer") {
  328. storageClass = spv::StorageClass::Uniform;
  329. rule = spirvOptions.sBufferLayoutRule;
  330. } else if (typeName == "RWStructuredBuffer" ||
  331. typeName == "AppendStructuredBuffer" ||
  332. typeName == "ConsumeStructuredBuffer") {
  333. storageClass = spv::StorageClass::Uniform;
  334. rule = spirvOptions.sBufferLayoutRule;
  335. isACRWSBuffer = true;
  336. }
  337. } else {
  338. // This is a stand-alone externally-visiable non-resource-type variable.
  339. // They should be grouped into the $Globals cbuffer. We create that cbuffer
  340. // and record all variables inside it upon seeing the first such variable.
  341. if (astDecls.count(var) == 0)
  342. createGlobalsCBuffer(var);
  343. return astDecls[var].info;
  344. }
  345. uint32_t varType = typeTranslator.translateType(var->getType(), rule);
  346. // Require corresponding capability for accessing 16-bit data.
  347. if (storageClass == spv::StorageClass::Uniform &&
  348. spirvOptions.enable16BitTypes &&
  349. typeTranslator.isOrContains16BitType(var->getType())) {
  350. theBuilder.addExtension(Extension::KHR_16bit_storage,
  351. "16-bit types in resource", var->getLocation());
  352. theBuilder.requireCapability(spv::Capability::StorageUniformBufferBlock16);
  353. }
  354. const uint32_t id = theBuilder.addModuleVar(varType, storageClass,
  355. var->getName(), llvm::None);
  356. const auto info =
  357. SpirvEvalInfo(id).setStorageClass(storageClass).setLayoutRule(rule);
  358. astDecls[var] = info;
  359. // Variables in Workgroup do not need descriptor decorations.
  360. if (storageClass == spv::StorageClass::Workgroup)
  361. return info;
  362. const auto *regAttr = getResourceBinding(var);
  363. const auto *bindingAttr = var->getAttr<VKBindingAttr>();
  364. const auto *counterBindingAttr = var->getAttr<VKCounterBindingAttr>();
  365. resourceVars.emplace_back(id, regAttr, bindingAttr, counterBindingAttr);
  366. if (const auto *inputAttachment = var->getAttr<VKInputAttachmentIndexAttr>())
  367. theBuilder.decorateInputAttachmentIndex(id, inputAttachment->getIndex());
  368. if (isACRWSBuffer) {
  369. // For {Append|Consume|RW}StructuredBuffer, we need to always create another
  370. // variable for its associated counter.
  371. createCounterVar(var, id, /*isAlias=*/false);
  372. }
  373. return info;
  374. }
  375. uint32_t DeclResultIdMapper::getMatrixStructType(const VarDecl *matVar,
  376. spv::StorageClass sc,
  377. LayoutRule rule) {
  378. const auto matType = matVar->getType();
  379. assert(TypeTranslator::isMxNMatrix(matType));
  380. auto &context = *theBuilder.getSPIRVContext();
  381. llvm::SmallVector<const Decoration *, 4> decorations;
  382. const bool isRowMajor = typeTranslator.isRowMajorMatrix(matType);
  383. uint32_t stride;
  384. (void)typeTranslator.getAlignmentAndSize(matType, rule, &stride);
  385. decorations.push_back(Decoration::getOffset(context, 0, 0));
  386. decorations.push_back(Decoration::getMatrixStride(context, stride, 0));
  387. decorations.push_back(isRowMajor ? Decoration::getColMajor(context, 0)
  388. : Decoration::getRowMajor(context, 0));
  389. decorations.push_back(Decoration::getBlock(context));
  390. // Get the type for the wrapping struct
  391. const std::string structName = "type." + matVar->getName().str();
  392. return theBuilder.getStructType({typeTranslator.translateType(matType)},
  393. structName, {}, decorations);
  394. }
  395. uint32_t DeclResultIdMapper::createStructOrStructArrayVarOfExplicitLayout(
  396. const DeclContext *decl, int arraySize, const ContextUsageKind usageKind,
  397. llvm::StringRef typeName, llvm::StringRef varName) {
  398. // cbuffers are translated into OpTypeStruct with Block decoration.
  399. // tbuffers are translated into OpTypeStruct with BufferBlock decoration.
  400. // Push constants are translated into OpTypeStruct with Block decoration.
  401. //
  402. // Both cbuffers and tbuffers have the SPIR-V Uniform storage class.
  403. // Push constants have the SPIR-V PushConstant storage class.
  404. const bool forCBuffer = usageKind == ContextUsageKind::CBuffer;
  405. const bool forTBuffer = usageKind == ContextUsageKind::TBuffer;
  406. const bool forGlobals = usageKind == ContextUsageKind::Globals;
  407. const bool forPC = usageKind == ContextUsageKind::PushConstant;
  408. auto &context = *theBuilder.getSPIRVContext();
  409. const LayoutRule layoutRule =
  410. (forCBuffer || forGlobals)
  411. ? spirvOptions.cBufferLayoutRule
  412. : (forTBuffer ? spirvOptions.tBufferLayoutRule
  413. : spirvOptions.sBufferLayoutRule);
  414. const auto *blockDec = forTBuffer ? Decoration::getBufferBlock(context)
  415. : Decoration::getBlock(context);
  416. const llvm::SmallVector<const Decl *, 4> &declGroup =
  417. typeTranslator.collectDeclsInDeclContext(decl);
  418. auto decorations = typeTranslator.getLayoutDecorations(declGroup, layoutRule);
  419. decorations.push_back(blockDec);
  420. // Collect the type and name for each field
  421. llvm::SmallVector<uint32_t, 4> fieldTypes;
  422. llvm::SmallVector<llvm::StringRef, 4> fieldNames;
  423. uint32_t fieldIndex = 0;
  424. for (const auto *subDecl : declGroup) {
  425. // The field can only be FieldDecl (for normal structs) or VarDecl (for
  426. // HLSLBufferDecls).
  427. assert(isa<VarDecl>(subDecl) || isa<FieldDecl>(subDecl));
  428. const auto *declDecl = cast<DeclaratorDecl>(subDecl);
  429. // All fields are qualified with const. It will affect the debug name.
  430. // We don't need it here.
  431. auto varType = declDecl->getType();
  432. varType.removeLocalConst();
  433. fieldTypes.push_back(typeTranslator.translateType(varType, layoutRule));
  434. fieldNames.push_back(declDecl->getName());
  435. // Require corresponding capability for accessing 16-bit data.
  436. if (spirvOptions.enable16BitTypes &&
  437. typeTranslator.isOrContains16BitType(varType)) {
  438. theBuilder.addExtension(Extension::KHR_16bit_storage,
  439. "16-bit types in resource",
  440. declDecl->getLocation());
  441. theBuilder.requireCapability(
  442. (forCBuffer || forGlobals)
  443. ? spv::Capability::StorageUniform16
  444. : forPC ? spv::Capability::StoragePushConstant16
  445. : spv::Capability::StorageUniformBufferBlock16);
  446. }
  447. // tbuffer/TextureBuffers are non-writable SSBOs. OpMemberDecorate
  448. // NonWritable must be applied to all fields.
  449. if (forTBuffer) {
  450. decorations.push_back(Decoration::getNonWritable(
  451. *theBuilder.getSPIRVContext(), fieldIndex));
  452. }
  453. ++fieldIndex;
  454. }
  455. // Get the type for the whole struct
  456. uint32_t resultType =
  457. theBuilder.getStructType(fieldTypes, typeName, fieldNames, decorations);
  458. // Make an array if requested.
  459. if (arraySize > 0) {
  460. resultType = theBuilder.getArrayType(
  461. resultType, theBuilder.getConstantUint32(arraySize));
  462. } else if (arraySize == -1) {
  463. // Runtime arrays of cbuffer/tbuffer needs additional capability.
  464. theBuilder.addExtension(Extension::EXT_descriptor_indexing,
  465. "runtime array of resources", {});
  466. theBuilder.requireCapability(spv::Capability::RuntimeDescriptorArrayEXT);
  467. resultType = theBuilder.getRuntimeArrayType(resultType);
  468. }
  469. // Register the <type-id> for this decl
  470. ctBufferPCTypeIds[decl] = resultType;
  471. const auto sc =
  472. forPC ? spv::StorageClass::PushConstant : spv::StorageClass::Uniform;
  473. // Create the variable for the whole struct / struct array.
  474. return theBuilder.addModuleVar(resultType, sc, varName);
  475. }
  476. uint32_t DeclResultIdMapper::createCTBuffer(const HLSLBufferDecl *decl) {
  477. const auto usageKind =
  478. decl->isCBuffer() ? ContextUsageKind::CBuffer : ContextUsageKind::TBuffer;
  479. const std::string structName = "type." + decl->getName().str();
  480. // The front-end does not allow arrays of cbuffer/tbuffer.
  481. const uint32_t bufferVar = createStructOrStructArrayVarOfExplicitLayout(
  482. decl, /*arraySize*/ 0, usageKind, structName, decl->getName());
  483. // We still register all VarDecls seperately here. All the VarDecls are
  484. // mapped to the <result-id> of the buffer object, which means when querying
  485. // querying the <result-id> for a certain VarDecl, we need to do an extra
  486. // OpAccessChain.
  487. int index = 0;
  488. for (const auto *subDecl : decl->decls()) {
  489. if (TypeTranslator::shouldSkipInStructLayout(subDecl))
  490. continue;
  491. const auto *varDecl = cast<VarDecl>(subDecl);
  492. astDecls[varDecl] =
  493. SpirvEvalInfo(bufferVar)
  494. .setStorageClass(spv::StorageClass::Uniform)
  495. .setLayoutRule(decl->isCBuffer() ? spirvOptions.cBufferLayoutRule
  496. : spirvOptions.tBufferLayoutRule);
  497. astDecls[varDecl].indexInCTBuffer = index++;
  498. }
  499. resourceVars.emplace_back(bufferVar, getResourceBinding(decl),
  500. decl->getAttr<VKBindingAttr>(),
  501. decl->getAttr<VKCounterBindingAttr>());
  502. return bufferVar;
  503. }
  504. uint32_t DeclResultIdMapper::createCTBuffer(const VarDecl *decl) {
  505. const RecordType *recordType = nullptr;
  506. int arraySize = 0;
  507. // In case we have an array of ConstantBuffer/TextureBuffer:
  508. if (const auto *arrayType = decl->getType()->getAsArrayTypeUnsafe()) {
  509. recordType = arrayType->getElementType()->getAs<RecordType>();
  510. if (const auto *caType =
  511. astContext.getAsConstantArrayType(decl->getType())) {
  512. arraySize = static_cast<uint32_t>(caType->getSize().getZExtValue());
  513. } else {
  514. arraySize = -1;
  515. }
  516. } else {
  517. recordType = decl->getType()->getAs<RecordType>();
  518. }
  519. if (!recordType) {
  520. emitError("constant/texture buffer type %0 unimplemented",
  521. decl->getLocStart())
  522. << decl->getType();
  523. return 0;
  524. }
  525. const auto *context = cast<HLSLBufferDecl>(decl->getDeclContext());
  526. const auto usageKind = context->isCBuffer() ? ContextUsageKind::CBuffer
  527. : ContextUsageKind::TBuffer;
  528. const char *ctBufferName =
  529. context->isCBuffer() ? "ConstantBuffer." : "TextureBuffer.";
  530. const std::string structName = "type." + std::string(ctBufferName) +
  531. recordType->getDecl()->getName().str();
  532. const uint32_t bufferVar = createStructOrStructArrayVarOfExplicitLayout(
  533. recordType->getDecl(), arraySize, usageKind, structName, decl->getName());
  534. // We register the VarDecl here.
  535. astDecls[decl] =
  536. SpirvEvalInfo(bufferVar)
  537. .setStorageClass(spv::StorageClass::Uniform)
  538. .setLayoutRule(context->isCBuffer() ? spirvOptions.cBufferLayoutRule
  539. : spirvOptions.tBufferLayoutRule);
  540. resourceVars.emplace_back(bufferVar, getResourceBinding(context),
  541. decl->getAttr<VKBindingAttr>(),
  542. decl->getAttr<VKCounterBindingAttr>());
  543. return bufferVar;
  544. }
  545. uint32_t DeclResultIdMapper::createPushConstant(const VarDecl *decl) {
  546. // The front-end errors out if non-struct type push constant is used.
  547. const auto *recordType = decl->getType()->getAs<RecordType>();
  548. assert(recordType);
  549. const std::string structName =
  550. "type.PushConstant." + recordType->getDecl()->getName().str();
  551. const uint32_t var = createStructOrStructArrayVarOfExplicitLayout(
  552. recordType->getDecl(), /*arraySize*/ 0, ContextUsageKind::PushConstant,
  553. structName, decl->getName());
  554. // Register the VarDecl
  555. astDecls[decl] = SpirvEvalInfo(var)
  556. .setStorageClass(spv::StorageClass::PushConstant)
  557. .setLayoutRule(spirvOptions.sBufferLayoutRule);
  558. // Do not push this variable into resourceVars since it does not need
  559. // descriptor set.
  560. return var;
  561. }
  562. void DeclResultIdMapper::createGlobalsCBuffer(const VarDecl *var) {
  563. if (astDecls.count(var) != 0)
  564. return;
  565. const auto *context = var->getTranslationUnitDecl();
  566. const uint32_t globals = createStructOrStructArrayVarOfExplicitLayout(
  567. context, /*arraySize*/ 0, ContextUsageKind::Globals, "type.$Globals",
  568. "$Globals");
  569. resourceVars.emplace_back(globals, nullptr, nullptr, nullptr);
  570. uint32_t index = 0;
  571. for (const auto *decl : typeTranslator.collectDeclsInDeclContext(context))
  572. if (const auto *varDecl = dyn_cast<VarDecl>(decl)) {
  573. if (!spirvOptions.noWarnIgnoredFeatures) {
  574. if (const auto *init = varDecl->getInit())
  575. emitWarning(
  576. "variable '%0' will be placed in $Globals so initializer ignored",
  577. init->getExprLoc())
  578. << var->getName() << init->getSourceRange();
  579. }
  580. if (const auto *attr = varDecl->getAttr<VKBindingAttr>()) {
  581. emitError("variable '%0' will be placed in $Globals so cannot have "
  582. "vk::binding attribute",
  583. attr->getLocation())
  584. << var->getName();
  585. return;
  586. }
  587. astDecls[varDecl] = SpirvEvalInfo(globals)
  588. .setStorageClass(spv::StorageClass::Uniform)
  589. .setLayoutRule(spirvOptions.cBufferLayoutRule);
  590. astDecls[varDecl].indexInCTBuffer = index++;
  591. }
  592. }
  593. uint32_t DeclResultIdMapper::getOrRegisterFnResultId(const FunctionDecl *fn) {
  594. if (const auto *info = getDeclSpirvInfo(fn))
  595. return info->info;
  596. auto &info = astDecls[fn].info;
  597. bool isAlias = false;
  598. const uint32_t type =
  599. getTypeAndCreateCounterForPotentialAliasVar(fn, &isAlias, &info);
  600. const uint32_t id = theBuilder.getSPIRVContext()->takeNextId();
  601. info.setResultId(id);
  602. // No need to dereference to get the pointer. Function returns that are
  603. // stand-alone aliases are already pointers to values. All other cases should
  604. // be normal rvalues.
  605. if (!isAlias ||
  606. !TypeTranslator::isAKindOfStructuredOrByteBuffer(fn->getReturnType()))
  607. info.setRValue();
  608. return id;
  609. }
  610. const CounterIdAliasPair *DeclResultIdMapper::getCounterIdAliasPair(
  611. const DeclaratorDecl *decl, const llvm::SmallVector<uint32_t, 4> *indices) {
  612. if (!decl)
  613. return nullptr;
  614. if (indices) {
  615. // Indices are provided. Walk through the fields of the decl.
  616. const auto counter = fieldCounterVars.find(decl);
  617. if (counter != fieldCounterVars.end())
  618. return counter->second.get(*indices);
  619. } else {
  620. // No indices. Check the stand-alone entities.
  621. const auto counter = counterVars.find(decl);
  622. if (counter != counterVars.end())
  623. return &counter->second;
  624. }
  625. return nullptr;
  626. }
  627. const CounterVarFields *
  628. DeclResultIdMapper::getCounterVarFields(const DeclaratorDecl *decl) {
  629. if (!decl)
  630. return nullptr;
  631. const auto found = fieldCounterVars.find(decl);
  632. if (found != fieldCounterVars.end())
  633. return &found->second;
  634. return nullptr;
  635. }
  636. void DeclResultIdMapper::registerSpecConstant(const VarDecl *decl,
  637. uint32_t specConstant) {
  638. astDecls[decl].info.setResultId(specConstant).setRValue().setSpecConstant();
  639. }
  640. void DeclResultIdMapper::createCounterVar(
  641. const DeclaratorDecl *decl, uint32_t declId, bool isAlias,
  642. const llvm::SmallVector<uint32_t, 4> *indices) {
  643. std::string counterName = "counter.var." + decl->getName().str();
  644. if (indices) {
  645. // Append field indices to the name
  646. for (const auto index : *indices)
  647. counterName += "." + std::to_string(index);
  648. }
  649. uint32_t counterType = typeTranslator.getACSBufferCounter();
  650. // {RW|Append|Consume}StructuredBuffer are all in Uniform storage class.
  651. // Alias counter variables should be created into the Private storage class.
  652. const spv::StorageClass sc =
  653. isAlias ? spv::StorageClass::Private : spv::StorageClass::Uniform;
  654. if (isAlias) {
  655. // Apply an extra level of pointer for alias counter variable
  656. counterType =
  657. theBuilder.getPointerType(counterType, spv::StorageClass::Uniform);
  658. }
  659. const uint32_t counterId =
  660. theBuilder.addModuleVar(counterType, sc, counterName);
  661. if (!isAlias) {
  662. // Non-alias counter variables should be put in to resourceVars so that
  663. // descriptors can be allocated for them.
  664. resourceVars.emplace_back(counterId, getResourceBinding(decl),
  665. decl->getAttr<VKBindingAttr>(),
  666. decl->getAttr<VKCounterBindingAttr>(), true);
  667. assert(declId);
  668. theBuilder.decorateCounterBufferId(declId, counterId);
  669. }
  670. if (indices)
  671. fieldCounterVars[decl].append(*indices, counterId);
  672. else
  673. counterVars[decl] = {counterId, isAlias};
  674. }
  675. void DeclResultIdMapper::createFieldCounterVars(
  676. const DeclaratorDecl *rootDecl, const DeclaratorDecl *decl,
  677. llvm::SmallVector<uint32_t, 4> *indices) {
  678. const QualType type = getTypeOrFnRetType(decl);
  679. const auto *recordType = type->getAs<RecordType>();
  680. assert(recordType);
  681. const auto *recordDecl = recordType->getDecl();
  682. for (const auto *field : recordDecl->fields()) {
  683. // Build up the index chain
  684. indices->push_back(getNumBaseClasses(type) + field->getFieldIndex());
  685. const QualType fieldType = field->getType();
  686. if (TypeTranslator::isRWAppendConsumeSBuffer(fieldType))
  687. createCounterVar(rootDecl, /*declId=*/0, /*isAlias=*/true, indices);
  688. else if (fieldType->isStructureType() &&
  689. !hlsl::IsHLSLResourceType(fieldType))
  690. // Go recursively into all nested structs
  691. createFieldCounterVars(rootDecl, field, indices);
  692. indices->pop_back();
  693. }
  694. }
  695. uint32_t
  696. DeclResultIdMapper::getCTBufferPushConstantTypeId(const DeclContext *decl) {
  697. const auto found = ctBufferPCTypeIds.find(decl);
  698. assert(found != ctBufferPCTypeIds.end());
  699. return found->second;
  700. }
  701. std::vector<uint32_t> DeclResultIdMapper::collectStageVars() const {
  702. std::vector<uint32_t> vars;
  703. for (auto var : glPerVertex.getStageInVars())
  704. vars.push_back(var);
  705. for (auto var : glPerVertex.getStageOutVars())
  706. vars.push_back(var);
  707. for (const auto &var : stageVars)
  708. vars.push_back(var.getSpirvId());
  709. return vars;
  710. }
  711. namespace {
  712. /// A class for managing stage input/output locations to avoid duplicate uses of
  713. /// the same location.
  714. class LocationSet {
  715. public:
  716. /// Maximum number of indices supported
  717. const static uint32_t kMaxIndex = 2;
  718. /// Maximum number of locations supported
  719. // Typically we won't have that many stage input or output variables.
  720. // Using 64 should be fine here.
  721. const static uint32_t kMaxLoc = 64;
  722. LocationSet() {
  723. for (uint32_t i = 0; i < kMaxIndex; ++i) {
  724. usedLocs[i].resize(kMaxLoc);
  725. nextLoc[i] = 0;
  726. }
  727. }
  728. /// Uses the given location.
  729. void useLoc(uint32_t loc, uint32_t index = 0) {
  730. assert(index < kMaxIndex);
  731. usedLocs[index].set(loc);
  732. }
  733. /// Uses the next |count| available location.
  734. int useNextLocs(uint32_t count, uint32_t index = 0) {
  735. assert(index < kMaxIndex);
  736. auto &locs = usedLocs[index];
  737. auto &next = nextLoc[index];
  738. while (locs[next])
  739. next++;
  740. int toUse = next;
  741. for (uint32_t i = 0; i < count; ++i) {
  742. assert(!locs[next]);
  743. locs.set(next++);
  744. }
  745. return toUse;
  746. }
  747. /// Returns true if the given location number is already used.
  748. bool isLocUsed(uint32_t loc, uint32_t index = 0) {
  749. assert(index < kMaxIndex);
  750. return usedLocs[index][loc];
  751. }
  752. private:
  753. llvm::SmallBitVector usedLocs[kMaxIndex]; ///< All previously used locations
  754. uint32_t nextLoc[kMaxIndex]; ///< Next available location
  755. };
  756. /// A class for managing resource bindings to avoid duplicate uses of the same
  757. /// set and binding number.
  758. class BindingSet {
  759. public:
  760. /// Uses the given set and binding number.
  761. void useBinding(uint32_t binding, uint32_t set) {
  762. usedBindings[set].insert(binding);
  763. }
  764. /// Uses the next avaiable binding number in set 0.
  765. uint32_t useNextBinding(uint32_t set) {
  766. auto &binding = usedBindings[set];
  767. auto &next = nextBindings[set];
  768. while (binding.count(next))
  769. ++next;
  770. binding.insert(next);
  771. return next++;
  772. }
  773. private:
  774. ///< set number -> set of used binding number
  775. llvm::DenseMap<uint32_t, llvm::DenseSet<uint32_t>> usedBindings;
  776. ///< set number -> next available binding number
  777. llvm::DenseMap<uint32_t, uint32_t> nextBindings;
  778. };
  779. } // namespace
  780. bool DeclResultIdMapper::checkSemanticDuplication(bool forInput) {
  781. llvm::StringSet<> seenSemantics;
  782. bool success = true;
  783. for (const auto &var : stageVars) {
  784. auto s = var.getSemanticStr();
  785. if (s.empty()) {
  786. // We translate WaveGetLaneCount() and WaveGetLaneIndex() into builtin
  787. // variables. Those variables are inserted into the normal stage IO
  788. // processing pipeline, but with the semantics as empty strings.
  789. assert(var.isSpirvBuitin());
  790. continue;
  791. }
  792. if (forInput && var.getSigPoint()->IsInput()) {
  793. if (seenSemantics.count(s)) {
  794. emitError("input semantic '%0' used more than once", {}) << s;
  795. success = false;
  796. }
  797. seenSemantics.insert(s);
  798. } else if (!forInput && var.getSigPoint()->IsOutput()) {
  799. if (seenSemantics.count(s)) {
  800. emitError("output semantic '%0' used more than once", {}) << s;
  801. success = false;
  802. }
  803. seenSemantics.insert(s);
  804. }
  805. }
  806. return success;
  807. }
  808. bool DeclResultIdMapper::finalizeStageIOLocations(bool forInput) {
  809. if (!checkSemanticDuplication(forInput))
  810. return false;
  811. // Returns false if the given StageVar is an input/output variable without
  812. // explicit location assignment. Otherwise, returns true.
  813. const auto locAssigned = [forInput, this](const StageVar &v) {
  814. if (forInput == isInputStorageClass(v))
  815. // No need to assign location for builtins. Treat as assigned.
  816. return v.isSpirvBuitin() || v.getLocationAttr() != nullptr;
  817. // For the ones we don't care, treat as assigned.
  818. return true;
  819. };
  820. // If we have explicit location specified for all input/output variables,
  821. // use them instead assign by ourselves.
  822. if (std::all_of(stageVars.begin(), stageVars.end(), locAssigned)) {
  823. LocationSet locSet;
  824. bool noError = true;
  825. for (const auto &var : stageVars) {
  826. // Skip builtins & those stage variables we are not handling for this call
  827. if (var.isSpirvBuitin() || forInput != isInputStorageClass(var))
  828. continue;
  829. const auto *attr = var.getLocationAttr();
  830. const auto loc = attr->getNumber();
  831. const auto attrLoc = attr->getLocation(); // Attr source code location
  832. const auto idx = var.getIndexAttr() ? var.getIndexAttr()->getNumber() : 0;
  833. if (loc >= LocationSet::kMaxLoc) {
  834. emitError("stage %select{output|input}0 location #%1 too large",
  835. attrLoc)
  836. << forInput << loc;
  837. return false;
  838. }
  839. // Make sure the same location is not assigned more than once
  840. if (locSet.isLocUsed(loc, idx)) {
  841. emitError("stage %select{output|input}0 location #%1 already assigned",
  842. attrLoc)
  843. << forInput << loc;
  844. noError = false;
  845. }
  846. locSet.useLoc(loc, idx);
  847. theBuilder.decorateLocation(var.getSpirvId(), loc);
  848. if (var.getIndexAttr())
  849. theBuilder.decorateIndex(var.getSpirvId(), idx);
  850. }
  851. return noError;
  852. }
  853. std::vector<const StageVar *> vars;
  854. LocationSet locSet;
  855. for (const auto &var : stageVars) {
  856. if (var.isSpirvBuitin() || forInput != isInputStorageClass(var))
  857. continue;
  858. if (var.getLocationAttr()) {
  859. // We have checked that not all of the stage variables have explicit
  860. // location assignment.
  861. emitError("partial explicit stage %select{output|input}0 location "
  862. "assignment via vk::location(X) unsupported",
  863. {})
  864. << forInput;
  865. return false;
  866. }
  867. const auto &semaInfo = var.getSemanticInfo();
  868. // We should special rules for SV_Target: the location number comes from the
  869. // semantic string index.
  870. if (semaInfo.isTarget()) {
  871. theBuilder.decorateLocation(var.getSpirvId(), semaInfo.index);
  872. locSet.useLoc(semaInfo.index);
  873. } else {
  874. vars.push_back(&var);
  875. }
  876. }
  877. // If alphabetical ordering was requested, sort by semantic string.
  878. // Since HS includes 2 sets of outputs (patch-constant output and
  879. // OutputPatch), running into location mismatches between HS and DS is very
  880. // likely. In order to avoid location mismatches between HS and DS, use
  881. // alphabetical ordering.
  882. if (spirvOptions.stageIoOrder == "alpha" ||
  883. (!forInput && shaderModel.IsHS()) || (forInput && shaderModel.IsDS())) {
  884. // Sort stage input/output variables alphabetically
  885. std::sort(vars.begin(), vars.end(),
  886. [](const StageVar *a, const StageVar *b) {
  887. return a->getSemanticStr() < b->getSemanticStr();
  888. });
  889. }
  890. for (const auto *var : vars)
  891. theBuilder.decorateLocation(var->getSpirvId(),
  892. locSet.useNextLocs(var->getLocationCount()));
  893. return true;
  894. }
  895. namespace {
  896. /// A class for maintaining the binding number shift requested for descriptor
  897. /// sets.
  898. class BindingShiftMapper {
  899. public:
  900. explicit BindingShiftMapper(const llvm::SmallVectorImpl<int32_t> &shifts)
  901. : masterShift(0) {
  902. assert(shifts.size() % 2 == 0);
  903. if (shifts.size() == 2 && shifts[1] == -1) {
  904. masterShift = shifts[0];
  905. } else {
  906. for (uint32_t i = 0; i < shifts.size(); i += 2)
  907. perSetShift[shifts[i + 1]] = shifts[i];
  908. }
  909. }
  910. /// Returns the shift amount for the given set.
  911. int32_t getShiftForSet(int32_t set) const {
  912. const auto found = perSetShift.find(set);
  913. if (found != perSetShift.end())
  914. return found->second;
  915. return masterShift;
  916. }
  917. private:
  918. uint32_t masterShift; /// Shift amount applies to all sets.
  919. llvm::DenseMap<int32_t, int32_t> perSetShift;
  920. };
  921. } // namespace
  922. bool DeclResultIdMapper::decorateResourceBindings() {
  923. // For normal resource, we support 3 approaches of setting binding numbers:
  924. // - m1: [[vk::binding(...)]]
  925. // - m2: :register(...)
  926. // - m3: None
  927. //
  928. // For associated counters, we support 2 approaches:
  929. // - c1: [[vk::counter_binding(...)]
  930. // - c2: None
  931. //
  932. // In combination, we need to handle 9 cases:
  933. // - 3 cases for nomral resoures (m1, m2, m3)
  934. // - 6 cases for associated counters (mX * cY)
  935. //
  936. // In the following order:
  937. // - m1, mX * c1
  938. // - m2
  939. // - m3, mX * c2
  940. BindingSet bindingSet;
  941. // Decorates the given varId of the given category with set number
  942. // setNo, binding number bindingNo. Ignores overlaps.
  943. const auto tryToDecorate = [this, &bindingSet](const uint32_t varId,
  944. const uint32_t setNo,
  945. const uint32_t bindingNo) {
  946. bindingSet.useBinding(bindingNo, setNo);
  947. theBuilder.decorateDSetBinding(varId, setNo, bindingNo);
  948. };
  949. for (const auto &var : resourceVars) {
  950. if (var.isCounter()) {
  951. if (const auto *vkCBinding = var.getCounterBinding()) {
  952. // Process mX * c1
  953. uint32_t set = 0;
  954. if (const auto *vkBinding = var.getBinding())
  955. set = vkBinding->getSet();
  956. if (const auto *reg = var.getRegister())
  957. set = reg->RegisterSpace;
  958. tryToDecorate(var.getSpirvId(), set, vkCBinding->getBinding());
  959. }
  960. } else {
  961. if (const auto *vkBinding = var.getBinding()) {
  962. // Process m1
  963. tryToDecorate(var.getSpirvId(), vkBinding->getSet(),
  964. vkBinding->getBinding());
  965. }
  966. }
  967. }
  968. BindingShiftMapper bShiftMapper(spirvOptions.bShift);
  969. BindingShiftMapper tShiftMapper(spirvOptions.tShift);
  970. BindingShiftMapper sShiftMapper(spirvOptions.sShift);
  971. BindingShiftMapper uShiftMapper(spirvOptions.uShift);
  972. // Process m2
  973. for (const auto &var : resourceVars)
  974. if (!var.isCounter() && !var.getBinding())
  975. if (const auto *reg = var.getRegister()) {
  976. const uint32_t set = reg->RegisterSpace;
  977. uint32_t binding = reg->RegisterNumber;
  978. switch (reg->RegisterType) {
  979. case 'b':
  980. binding += bShiftMapper.getShiftForSet(set);
  981. break;
  982. case 't':
  983. binding += tShiftMapper.getShiftForSet(set);
  984. break;
  985. case 's':
  986. binding += sShiftMapper.getShiftForSet(set);
  987. break;
  988. case 'u':
  989. binding += uShiftMapper.getShiftForSet(set);
  990. break;
  991. case 'c':
  992. // For setting packing offset. Does not affect binding.
  993. break;
  994. default:
  995. llvm_unreachable("unknown register type found");
  996. }
  997. tryToDecorate(var.getSpirvId(), set, binding);
  998. }
  999. for (const auto &var : resourceVars) {
  1000. if (var.isCounter()) {
  1001. if (!var.getCounterBinding()) {
  1002. // Process mX * c2
  1003. uint32_t set = 0;
  1004. if (const auto *vkBinding = var.getBinding())
  1005. set = vkBinding->getSet();
  1006. else if (const auto *reg = var.getRegister())
  1007. set = reg->RegisterSpace;
  1008. theBuilder.decorateDSetBinding(var.getSpirvId(), set,
  1009. bindingSet.useNextBinding(set));
  1010. }
  1011. } else if (!var.getBinding() && !var.getRegister()) {
  1012. // Process m3
  1013. theBuilder.decorateDSetBinding(var.getSpirvId(), 0,
  1014. bindingSet.useNextBinding(0));
  1015. }
  1016. }
  1017. return true;
  1018. }
  1019. bool DeclResultIdMapper::createStageVars(const hlsl::SigPoint *sigPoint,
  1020. const NamedDecl *decl, bool asInput,
  1021. QualType type, uint32_t arraySize,
  1022. const llvm::StringRef namePrefix,
  1023. llvm::Optional<uint32_t> invocationId,
  1024. uint32_t *value, bool noWriteBack,
  1025. SemanticInfo *inheritSemantic) {
  1026. // invocationId should only be used for handling HS per-vertex output.
  1027. if (invocationId.hasValue()) {
  1028. assert(shaderModel.IsHS() && arraySize != 0 && !asInput);
  1029. }
  1030. assert(inheritSemantic);
  1031. if (type->isVoidType()) {
  1032. // No stage variables will be created for void type.
  1033. return true;
  1034. }
  1035. uint32_t typeId = typeTranslator.translateType(type);
  1036. // We have several cases regarding HLSL semantics to handle here:
  1037. // * If the currrent decl inherits a semantic from some enclosing entity,
  1038. // use the inherited semantic no matter whether there is a semantic
  1039. // attached to the current decl.
  1040. // * If there is no semantic to inherit,
  1041. // * If the current decl is a struct,
  1042. // * If the current decl has a semantic, all its members inhert this
  1043. // decl's semantic, with the index sequentially increasing;
  1044. // * If the current decl does not have a semantic, all its members
  1045. // should have semantics attached;
  1046. // * If the current decl is not a struct, it should have semantic attached.
  1047. auto thisSemantic = getStageVarSemantic(decl);
  1048. // Which semantic we should use for this decl
  1049. auto *semanticToUse = &thisSemantic;
  1050. // Enclosing semantics override internal ones
  1051. if (inheritSemantic->isValid()) {
  1052. if (thisSemantic.isValid()) {
  1053. emitWarning(
  1054. "internal semantic '%0' overridden by enclosing semantic '%1'",
  1055. thisSemantic.loc)
  1056. << thisSemantic.str << inheritSemantic->str;
  1057. }
  1058. semanticToUse = inheritSemantic;
  1059. }
  1060. if (semanticToUse->isValid() &&
  1061. // Structs with attached semantics will be handled later.
  1062. !type->isStructureType()) {
  1063. // Found semantic attached directly to this Decl. This means we need to
  1064. // map this decl to a single stage variable.
  1065. if (!validateVKAttributes(decl))
  1066. return false;
  1067. const auto semanticKind = semanticToUse->getKind();
  1068. // Error out when the given semantic is invalid in this shader model
  1069. if (hlsl::SigPoint::GetInterpretation(semanticKind, sigPoint->GetKind(),
  1070. shaderModel.GetMajor(),
  1071. shaderModel.GetMinor()) ==
  1072. hlsl::DXIL::SemanticInterpretationKind::NA) {
  1073. emitError("invalid usage of semantic '%0' in shader profile %1",
  1074. decl->getLocation())
  1075. << semanticToUse->str << shaderModel.GetName();
  1076. return false;
  1077. }
  1078. if (!validateVKBuiltins(decl, sigPoint))
  1079. return false;
  1080. const auto *builtinAttr = decl->getAttr<VKBuiltInAttr>();
  1081. // Special handling of certain mappings between HLSL semantics and
  1082. // SPIR-V builtins:
  1083. // * SV_CullDistance/SV_ClipDistance are outsourced to GlPerVertex.
  1084. // * SV_DomainLocation can refer to a float2, whereas TessCoord is a float3.
  1085. // To ensure SPIR-V validity, we must create a float3 and extract a
  1086. // float2 from it before passing it to the main function.
  1087. // * SV_TessFactor is an array of size 2 for isoline patch, array of size 3
  1088. // for tri patch, and array of size 4 for quad patch, but it must always
  1089. // be an array of size 4 in SPIR-V for Vulkan.
  1090. // * SV_InsideTessFactor is a single float for tri patch, and an array of
  1091. // size 2 for a quad patch, but it must always be an array of size 2 in
  1092. // SPIR-V for Vulkan.
  1093. // * SV_Coverage is an uint value, but the builtin it corresponds to,
  1094. // SampleMask, must be an array of integers.
  1095. // * SV_InnerCoverage is an uint value, but the corresponding builtin,
  1096. // FullyCoveredEXT, must be an boolean value.
  1097. // * SV_DispatchThreadID, SV_GroupThreadID, and SV_GroupID are allowed to be
  1098. // uint, uint2, or uint3, but the corresponding builtins
  1099. // (GlobalInvocationId, LocalInvocationId, WorkgroupId) must be a uint3.
  1100. if (glPerVertex.tryToAccess(sigPoint->GetKind(), semanticKind,
  1101. semanticToUse->index, invocationId, value,
  1102. noWriteBack))
  1103. return true;
  1104. const uint32_t srcTypeId = typeId; // Variable type in source code
  1105. uint32_t srcVecElemTypeId = 0; // Variable element type if vector
  1106. switch (semanticKind) {
  1107. case hlsl::Semantic::Kind::DomainLocation:
  1108. typeId = theBuilder.getVecType(theBuilder.getFloat32Type(), 3);
  1109. break;
  1110. case hlsl::Semantic::Kind::TessFactor:
  1111. typeId = theBuilder.getArrayType(theBuilder.getFloat32Type(),
  1112. theBuilder.getConstantUint32(4));
  1113. break;
  1114. case hlsl::Semantic::Kind::InsideTessFactor:
  1115. typeId = theBuilder.getArrayType(theBuilder.getFloat32Type(),
  1116. theBuilder.getConstantUint32(2));
  1117. break;
  1118. case hlsl::Semantic::Kind::Coverage:
  1119. typeId = theBuilder.getArrayType(typeId, theBuilder.getConstantUint32(1));
  1120. break;
  1121. case hlsl::Semantic::Kind::InnerCoverage:
  1122. typeId = theBuilder.getBoolType();
  1123. break;
  1124. case hlsl::Semantic::Kind::Barycentrics:
  1125. typeId = theBuilder.getVecType(theBuilder.getFloat32Type(), 2);
  1126. break;
  1127. case hlsl::Semantic::Kind::DispatchThreadID:
  1128. case hlsl::Semantic::Kind::GroupThreadID:
  1129. case hlsl::Semantic::Kind::GroupID:
  1130. // Keep the original integer signedness
  1131. srcVecElemTypeId = typeTranslator.translateType(
  1132. hlsl::IsHLSLVecType(type) ? hlsl::GetHLSLVecElementType(type) : type);
  1133. typeId = theBuilder.getVecType(srcVecElemTypeId, 3);
  1134. break;
  1135. }
  1136. // Handle the extra arrayness
  1137. const uint32_t elementTypeId = typeId; // Array element's type
  1138. if (arraySize != 0)
  1139. typeId = theBuilder.getArrayType(typeId,
  1140. theBuilder.getConstantUint32(arraySize));
  1141. StageVar stageVar(
  1142. sigPoint, *semanticToUse, builtinAttr, typeId,
  1143. // For HS/DS/GS, we have already stripped the outmost arrayness on type.
  1144. typeTranslator.getLocationCount(type));
  1145. const auto name = namePrefix.str() + "." + stageVar.getSemanticStr();
  1146. const uint32_t varId =
  1147. createSpirvStageVar(&stageVar, decl, name, semanticToUse->loc);
  1148. if (varId == 0)
  1149. return false;
  1150. stageVar.setSpirvId(varId);
  1151. stageVar.setLocationAttr(decl->getAttr<VKLocationAttr>());
  1152. stageVar.setIndexAttr(decl->getAttr<VKIndexAttr>());
  1153. stageVars.push_back(stageVar);
  1154. // Emit OpDecorate* instructions to link this stage variable with the HLSL
  1155. // semantic it is created for
  1156. theBuilder.decorateHlslSemantic(varId, stageVar.getSemanticInfo().str);
  1157. // We have semantics attached to this decl, which means it must be a
  1158. // function/parameter/variable. All are DeclaratorDecls.
  1159. stageVarIds[cast<DeclaratorDecl>(decl)] = varId;
  1160. // Mark that we have used one index for this semantic
  1161. ++semanticToUse->index;
  1162. // Require extension and capability if using 16-bit types
  1163. if (typeTranslator.getElementSpirvBitwidth(type) == 16) {
  1164. theBuilder.addExtension(Extension::KHR_16bit_storage,
  1165. "16-bit stage IO variables", decl->getLocation());
  1166. theBuilder.requireCapability(spv::Capability::StorageInputOutput16);
  1167. }
  1168. // TODO: the following may not be correct?
  1169. if (sigPoint->GetSignatureKind() ==
  1170. hlsl::DXIL::SignatureKind::PatchConstant)
  1171. theBuilder.decorate(varId, spv::Decoration::Patch);
  1172. // Decorate with interpolation modes for pixel shader input variables
  1173. if (shaderModel.IsPS() && sigPoint->IsInput() &&
  1174. // BaryCoord*AMD buitins already encode the interpolation mode.
  1175. semanticKind != hlsl::Semantic::Kind::Barycentrics)
  1176. decoratePSInterpolationMode(decl, type, varId);
  1177. if (asInput) {
  1178. *value = theBuilder.createLoad(typeId, varId);
  1179. // Fix ups for corner cases
  1180. // Special handling of SV_TessFactor DS patch constant input.
  1181. // TessLevelOuter is always an array of size 4 in SPIR-V, but
  1182. // SV_TessFactor could be an array of size 2, 3, or 4 in HLSL. Only the
  1183. // relevant indexes must be loaded.
  1184. if (semanticKind == hlsl::Semantic::Kind::TessFactor &&
  1185. hlsl::GetArraySize(type) != 4) {
  1186. llvm::SmallVector<uint32_t, 4> components;
  1187. const auto f32TypeId = theBuilder.getFloat32Type();
  1188. const auto tessFactorSize = hlsl::GetArraySize(type);
  1189. const auto arrType = theBuilder.getArrayType(
  1190. f32TypeId, theBuilder.getConstantUint32(tessFactorSize));
  1191. for (uint32_t i = 0; i < tessFactorSize; ++i)
  1192. components.push_back(
  1193. theBuilder.createCompositeExtract(f32TypeId, *value, {i}));
  1194. *value = theBuilder.createCompositeConstruct(arrType, components);
  1195. }
  1196. // Special handling of SV_InsideTessFactor DS patch constant input.
  1197. // TessLevelInner is always an array of size 2 in SPIR-V, but
  1198. // SV_InsideTessFactor could be an array of size 1 (scalar) or size 2 in
  1199. // HLSL. If SV_InsideTessFactor is a scalar, only extract index 0 of
  1200. // TessLevelInner.
  1201. else if (semanticKind == hlsl::Semantic::Kind::InsideTessFactor &&
  1202. // Some developers use float[1] instead of a scalar float.
  1203. (!type->isArrayType() || hlsl::GetArraySize(type) == 1)) {
  1204. const auto f32Type = theBuilder.getFloat32Type();
  1205. *value = theBuilder.createCompositeExtract(f32Type, *value, {0});
  1206. if (type->isArrayType()) // float[1]
  1207. *value = theBuilder.createCompositeConstruct(
  1208. theBuilder.getArrayType(f32Type, theBuilder.getConstantUint32(1)),
  1209. {*value});
  1210. }
  1211. // SV_DomainLocation can refer to a float2 or a float3, whereas TessCoord
  1212. // is always a float3. To ensure SPIR-V validity, a float3 stage variable
  1213. // is created, and we must extract a float2 from it before passing it to
  1214. // the main function.
  1215. else if (semanticKind == hlsl::Semantic::Kind::DomainLocation &&
  1216. hlsl::GetHLSLVecSize(type) != 3) {
  1217. const auto domainLocSize = hlsl::GetHLSLVecSize(type);
  1218. *value = theBuilder.createVectorShuffle(
  1219. theBuilder.getVecType(theBuilder.getFloat32Type(), domainLocSize),
  1220. *value, *value, {0, 1});
  1221. }
  1222. // Special handling of SV_Coverage, which is an uint value. We need to
  1223. // read SampleMask and extract its first element.
  1224. else if (semanticKind == hlsl::Semantic::Kind::Coverage) {
  1225. *value = theBuilder.createCompositeExtract(srcTypeId, *value, {0});
  1226. }
  1227. // Special handling of SV_InnerCoverage, which is an uint value. We need
  1228. // to read FullyCoveredEXT, which is a boolean value, and convert it to an
  1229. // uint value. According to D3D12 "Conservative Rasterization" doc: "The
  1230. // Pixel Shader has a 32-bit scalar integer System Generate Value
  1231. // available: InnerCoverage. This is a bit-field that has bit 0 from the
  1232. // LSB set to 1 for a given conservatively rasterized pixel, only when
  1233. // that pixel is guaranteed to be entirely inside the current primitive.
  1234. // All other input register bits must be set to 0 when bit 0 is not set,
  1235. // but are undefined when bit 0 is set to 1 (essentially, this bit-field
  1236. // represents a Boolean value where false must be exactly 0, but true can
  1237. // be any odd (i.e. bit 0 set) non-zero value)."
  1238. else if (semanticKind == hlsl::Semantic::Kind::InnerCoverage) {
  1239. const auto constOne = theBuilder.getConstantUint32(1);
  1240. const auto constZero = theBuilder.getConstantUint32(0);
  1241. *value = theBuilder.createSelect(theBuilder.getUint32Type(), *value,
  1242. constOne, constZero);
  1243. }
  1244. // Special handling of SV_Barycentrics, which is a float3, but the
  1245. // underlying stage input variable is a float2 (only provides the first
  1246. // two components). Calculate the third element.
  1247. else if (semanticKind == hlsl::Semantic::Kind::Barycentrics) {
  1248. const auto f32Type = theBuilder.getFloat32Type();
  1249. const auto x = theBuilder.createCompositeExtract(f32Type, *value, {0});
  1250. const auto y = theBuilder.createCompositeExtract(f32Type, *value, {1});
  1251. const auto xy =
  1252. theBuilder.createBinaryOp(spv::Op::OpFAdd, f32Type, x, y);
  1253. const auto z = theBuilder.createBinaryOp(
  1254. spv::Op::OpFSub, f32Type, theBuilder.getConstantFloat32(1), xy);
  1255. const auto v3f32Type = theBuilder.getVecType(f32Type, 3);
  1256. *value = theBuilder.createCompositeConstruct(v3f32Type, {x, y, z});
  1257. }
  1258. // Special handling of SV_DispatchThreadID and SV_GroupThreadID, which may
  1259. // be a uint or uint2, but the underlying stage input variable is a uint3.
  1260. // The last component(s) should be discarded in needed.
  1261. else if ((semanticKind == hlsl::Semantic::Kind::DispatchThreadID ||
  1262. semanticKind == hlsl::Semantic::Kind::GroupThreadID ||
  1263. semanticKind == hlsl::Semantic::Kind::GroupID) &&
  1264. (!hlsl::IsHLSLVecType(type) ||
  1265. hlsl::GetHLSLVecSize(type) != 3)) {
  1266. assert(srcVecElemTypeId);
  1267. const auto vecSize =
  1268. hlsl::IsHLSLVecType(type) ? hlsl::GetHLSLVecSize(type) : 1;
  1269. if (vecSize == 1)
  1270. *value =
  1271. theBuilder.createCompositeExtract(srcVecElemTypeId, *value, {0});
  1272. else if (vecSize == 2)
  1273. *value = theBuilder.createVectorShuffle(
  1274. theBuilder.getVecType(srcVecElemTypeId, 2), *value, *value,
  1275. {0, 1});
  1276. }
  1277. } else {
  1278. if (noWriteBack)
  1279. return true;
  1280. // Negate SV_Position.y if requested
  1281. if (semanticToUse->semantic->GetKind() == hlsl::Semantic::Kind::Position)
  1282. *value = invertYIfRequested(*value);
  1283. uint32_t ptr = varId;
  1284. // Special handling of SV_TessFactor HS patch constant output.
  1285. // TessLevelOuter is always an array of size 4 in SPIR-V, but
  1286. // SV_TessFactor could be an array of size 2, 3, or 4 in HLSL. Only the
  1287. // relevant indexes must be written to.
  1288. if (semanticKind == hlsl::Semantic::Kind::TessFactor &&
  1289. hlsl::GetArraySize(type) != 4) {
  1290. const auto f32TypeId = theBuilder.getFloat32Type();
  1291. const auto tessFactorSize = hlsl::GetArraySize(type);
  1292. for (uint32_t i = 0; i < tessFactorSize; ++i) {
  1293. const uint32_t ptrType =
  1294. theBuilder.getPointerType(f32TypeId, spv::StorageClass::Output);
  1295. ptr = theBuilder.createAccessChain(ptrType, varId,
  1296. theBuilder.getConstantUint32(i));
  1297. theBuilder.createStore(
  1298. ptr, theBuilder.createCompositeExtract(f32TypeId, *value, i));
  1299. }
  1300. }
  1301. // Special handling of SV_InsideTessFactor HS patch constant output.
  1302. // TessLevelInner is always an array of size 2 in SPIR-V, but
  1303. // SV_InsideTessFactor could be an array of size 1 (scalar) or size 2 in
  1304. // HLSL. If SV_InsideTessFactor is a scalar, only write to index 0 of
  1305. // TessLevelInner.
  1306. else if (semanticKind == hlsl::Semantic::Kind::InsideTessFactor &&
  1307. // Some developers use float[1] instead of a scalar float.
  1308. (!type->isArrayType() || hlsl::GetArraySize(type) == 1)) {
  1309. const auto f32Type = theBuilder.getFloat32Type();
  1310. ptr = theBuilder.createAccessChain(
  1311. theBuilder.getPointerType(f32Type, spv::StorageClass::Output),
  1312. varId, theBuilder.getConstantUint32(0));
  1313. if (type->isArrayType()) // float[1]
  1314. *value = theBuilder.createCompositeExtract(f32Type, *value, {0});
  1315. theBuilder.createStore(ptr, *value);
  1316. }
  1317. // Special handling of SV_Coverage, which is an unit value. We need to
  1318. // write it to the first element in the SampleMask builtin.
  1319. else if (semanticKind == hlsl::Semantic::Kind::Coverage) {
  1320. ptr = theBuilder.createAccessChain(
  1321. theBuilder.getPointerType(srcTypeId, spv::StorageClass::Output),
  1322. varId, theBuilder.getConstantUint32(0));
  1323. theBuilder.createStore(ptr, *value);
  1324. }
  1325. // Special handling of HS ouput, for which we write to only one
  1326. // element in the per-vertex data array: the one indexed by
  1327. // SV_ControlPointID.
  1328. else if (invocationId.hasValue()) {
  1329. const uint32_t ptrType =
  1330. theBuilder.getPointerType(elementTypeId, spv::StorageClass::Output);
  1331. const uint32_t index = invocationId.getValue();
  1332. ptr = theBuilder.createAccessChain(ptrType, varId, index);
  1333. theBuilder.createStore(ptr, *value);
  1334. }
  1335. // For all normal cases
  1336. else {
  1337. theBuilder.createStore(ptr, *value);
  1338. }
  1339. }
  1340. return true;
  1341. }
  1342. // If the decl itself doesn't have semantic string attached and there is no
  1343. // one to inherit, it should be a struct having all its fields with semantic
  1344. // strings.
  1345. if (!semanticToUse->isValid() && !type->isStructureType()) {
  1346. emitError("semantic string missing for shader %select{output|input}0 "
  1347. "variable '%1'",
  1348. decl->getLocation())
  1349. << asInput << decl->getName();
  1350. return false;
  1351. }
  1352. const auto *structDecl = type->getAs<RecordType>()->getDecl();
  1353. if (asInput) {
  1354. // If this decl translates into multiple stage input variables, we need to
  1355. // load their values into a composite.
  1356. llvm::SmallVector<uint32_t, 4> subValues;
  1357. // If we have base classes, we need to handle them first.
  1358. if (const auto *cxxDecl = type->getAsCXXRecordDecl())
  1359. for (auto base : cxxDecl->bases()) {
  1360. uint32_t subValue = 0;
  1361. if (!createStageVars(sigPoint, base.getType()->getAsCXXRecordDecl(),
  1362. asInput, base.getType(), arraySize, namePrefix,
  1363. invocationId, &subValue, noWriteBack,
  1364. semanticToUse))
  1365. return false;
  1366. subValues.push_back(subValue);
  1367. }
  1368. for (const auto *field : structDecl->fields()) {
  1369. uint32_t subValue = 0;
  1370. if (!createStageVars(sigPoint, field, asInput, field->getType(),
  1371. arraySize, namePrefix, invocationId, &subValue,
  1372. noWriteBack, semanticToUse))
  1373. return false;
  1374. subValues.push_back(subValue);
  1375. }
  1376. if (arraySize == 0) {
  1377. *value = theBuilder.createCompositeConstruct(typeId, subValues);
  1378. return true;
  1379. }
  1380. // Handle the extra level of arrayness.
  1381. // We need to return an array of structs. But we get arrays of fields
  1382. // from visiting all fields. So now we need to extract all the elements
  1383. // at the same index of each field arrays and compose a new struct out
  1384. // of them.
  1385. const uint32_t structType = typeTranslator.translateType(type);
  1386. const uint32_t arrayType = theBuilder.getArrayType(
  1387. structType, theBuilder.getConstantUint32(arraySize));
  1388. llvm::SmallVector<uint32_t, 16> arrayElements;
  1389. for (uint32_t arrayIndex = 0; arrayIndex < arraySize; ++arrayIndex) {
  1390. llvm::SmallVector<uint32_t, 8> fields;
  1391. // If we have base classes, we need to handle them first.
  1392. if (const auto *cxxDecl = type->getAsCXXRecordDecl()) {
  1393. uint32_t baseIndex = 0;
  1394. for (auto base : cxxDecl->bases()) {
  1395. const auto baseType = typeTranslator.translateType(base.getType());
  1396. fields.push_back(theBuilder.createCompositeExtract(
  1397. baseType, subValues[baseIndex++], {arrayIndex}));
  1398. }
  1399. }
  1400. // Extract the element at index arrayIndex from each field
  1401. for (const auto *field : structDecl->fields()) {
  1402. const uint32_t fieldType =
  1403. typeTranslator.translateType(field->getType());
  1404. fields.push_back(theBuilder.createCompositeExtract(
  1405. fieldType,
  1406. subValues[getNumBaseClasses(type) + field->getFieldIndex()],
  1407. {arrayIndex}));
  1408. }
  1409. // Compose a new struct out of them
  1410. arrayElements.push_back(
  1411. theBuilder.createCompositeConstruct(structType, fields));
  1412. }
  1413. *value = theBuilder.createCompositeConstruct(arrayType, arrayElements);
  1414. } else {
  1415. // If we have base classes, we need to handle them first.
  1416. if (const auto *cxxDecl = type->getAsCXXRecordDecl()) {
  1417. uint32_t baseIndex = 0;
  1418. for (auto base : cxxDecl->bases()) {
  1419. uint32_t subValue = 0;
  1420. if (!noWriteBack)
  1421. subValue = theBuilder.createCompositeExtract(
  1422. typeTranslator.translateType(base.getType()), *value,
  1423. {baseIndex++});
  1424. if (!createStageVars(sigPoint, base.getType()->getAsCXXRecordDecl(),
  1425. asInput, base.getType(), arraySize, namePrefix,
  1426. invocationId, &subValue, noWriteBack,
  1427. semanticToUse))
  1428. return false;
  1429. }
  1430. }
  1431. // Unlike reading, which may require us to read stand-alone builtins and
  1432. // stage input variables and compose an array of structs out of them,
  1433. // it happens that we don't need to write an array of structs in a bunch
  1434. // for all shader stages:
  1435. //
  1436. // * VS: output is a single struct, without extra arrayness
  1437. // * HS: output is an array of structs, with extra arrayness,
  1438. // but we only write to the struct at the InvocationID index
  1439. // * DS: output is a single struct, without extra arrayness
  1440. // * GS: output is controlled by OpEmitVertex, one vertex per time
  1441. //
  1442. // The interesting shader stage is HS. We need the InvocationID to write
  1443. // out the value to the correct array element.
  1444. for (const auto *field : structDecl->fields()) {
  1445. const uint32_t fieldType = typeTranslator.translateType(field->getType());
  1446. uint32_t subValue = 0;
  1447. if (!noWriteBack)
  1448. subValue = theBuilder.createCompositeExtract(
  1449. fieldType, *value,
  1450. {getNumBaseClasses(type) + field->getFieldIndex()});
  1451. if (!createStageVars(sigPoint, field, asInput, field->getType(),
  1452. arraySize, namePrefix, invocationId, &subValue,
  1453. noWriteBack, semanticToUse))
  1454. return false;
  1455. }
  1456. }
  1457. return true;
  1458. }
  1459. bool DeclResultIdMapper::writeBackOutputStream(const NamedDecl *decl,
  1460. QualType type, uint32_t value) {
  1461. assert(shaderModel.IsGS()); // Only for GS use
  1462. if (hlsl::IsHLSLStreamOutputType(type))
  1463. type = hlsl::GetHLSLResourceResultType(type);
  1464. if (hasGSPrimitiveTypeQualifier(decl))
  1465. type = astContext.getAsConstantArrayType(type)->getElementType();
  1466. auto semanticInfo = getStageVarSemantic(decl);
  1467. if (semanticInfo.isValid()) {
  1468. // Found semantic attached directly to this Decl. Write the value for this
  1469. // Decl to the corresponding stage output variable.
  1470. const uint32_t srcTypeId = typeTranslator.translateType(type);
  1471. // Handle SV_Position, SV_ClipDistance, and SV_CullDistance
  1472. if (glPerVertex.tryToAccess(
  1473. hlsl::DXIL::SigPointKind::GSOut, semanticInfo.semantic->GetKind(),
  1474. semanticInfo.index, llvm::None, &value, /*noWriteBack=*/false))
  1475. return true;
  1476. // Query the <result-id> for the stage output variable generated out
  1477. // of this decl.
  1478. // We have semantic string attached to this decl; therefore, it must be a
  1479. // DeclaratorDecl.
  1480. const auto found = stageVarIds.find(cast<DeclaratorDecl>(decl));
  1481. // We should have recorded its stage output variable previously.
  1482. assert(found != stageVarIds.end());
  1483. // Negate SV_Position.y if requested
  1484. if (semanticInfo.semantic->GetKind() == hlsl::Semantic::Kind::Position)
  1485. value = invertYIfRequested(value);
  1486. theBuilder.createStore(found->second, value);
  1487. return true;
  1488. }
  1489. // If the decl itself doesn't have semantic string attached, it should be
  1490. // a struct having all its fields with semantic strings.
  1491. if (!type->isStructureType()) {
  1492. emitError("semantic string missing for shader output variable '%0'",
  1493. decl->getLocation())
  1494. << decl->getName();
  1495. return false;
  1496. }
  1497. // If we have base classes, we need to handle them first.
  1498. if (const auto *cxxDecl = type->getAsCXXRecordDecl()) {
  1499. uint32_t baseIndex = 0;
  1500. for (auto base : cxxDecl->bases()) {
  1501. const auto baseType = typeTranslator.translateType(base.getType());
  1502. const auto subValue =
  1503. theBuilder.createCompositeExtract(baseType, value, {baseIndex++});
  1504. if (!writeBackOutputStream(base.getType()->getAsCXXRecordDecl(),
  1505. base.getType(), subValue))
  1506. return false;
  1507. }
  1508. }
  1509. const auto *structDecl = type->getAs<RecordType>()->getDecl();
  1510. // Write out each field
  1511. for (const auto *field : structDecl->fields()) {
  1512. const uint32_t fieldType = typeTranslator.translateType(field->getType());
  1513. const uint32_t subValue = theBuilder.createCompositeExtract(
  1514. fieldType, value, {getNumBaseClasses(type) + field->getFieldIndex()});
  1515. if (!writeBackOutputStream(field, field->getType(), subValue))
  1516. return false;
  1517. }
  1518. return true;
  1519. }
  1520. uint32_t DeclResultIdMapper::invertYIfRequested(uint32_t position) {
  1521. // Negate SV_Position.y if requested
  1522. if (spirvOptions.invertY) {
  1523. const auto f32Type = theBuilder.getFloat32Type();
  1524. const auto v4f32Type = theBuilder.getVecType(f32Type, 4);
  1525. const auto oldY = theBuilder.createCompositeExtract(f32Type, position, {1});
  1526. const auto newY =
  1527. theBuilder.createUnaryOp(spv::Op::OpFNegate, f32Type, oldY);
  1528. position = theBuilder.createCompositeInsert(v4f32Type, position, {1}, newY);
  1529. }
  1530. return position;
  1531. }
  1532. void DeclResultIdMapper::decoratePSInterpolationMode(const NamedDecl *decl,
  1533. QualType type,
  1534. uint32_t varId) {
  1535. const QualType elemType = typeTranslator.getElementType(type);
  1536. if (elemType->isBooleanType() || elemType->isIntegerType()) {
  1537. // TODO: Probably we can call hlsl::ValidateSignatureElement() for the
  1538. // following check.
  1539. if (decl->getAttr<HLSLLinearAttr>() || decl->getAttr<HLSLCentroidAttr>() ||
  1540. decl->getAttr<HLSLNoPerspectiveAttr>() ||
  1541. decl->getAttr<HLSLSampleAttr>()) {
  1542. emitError("only nointerpolation mode allowed for integer input "
  1543. "parameters in pixel shader",
  1544. decl->getLocation());
  1545. } else {
  1546. theBuilder.decorate(varId, spv::Decoration::Flat);
  1547. }
  1548. } else {
  1549. // Do nothing for HLSLLinearAttr since its the default
  1550. // Attributes can be used together. So cannot use else if.
  1551. if (decl->getAttr<HLSLCentroidAttr>())
  1552. theBuilder.decorate(varId, spv::Decoration::Centroid);
  1553. if (decl->getAttr<HLSLNoInterpolationAttr>())
  1554. theBuilder.decorate(varId, spv::Decoration::Flat);
  1555. if (decl->getAttr<HLSLNoPerspectiveAttr>())
  1556. theBuilder.decorate(varId, spv::Decoration::NoPerspective);
  1557. if (decl->getAttr<HLSLSampleAttr>()) {
  1558. theBuilder.requireCapability(spv::Capability::SampleRateShading);
  1559. theBuilder.decorate(varId, spv::Decoration::Sample);
  1560. }
  1561. }
  1562. }
  1563. uint32_t DeclResultIdMapper::getBuiltinVar(spv::BuiltIn builtIn) {
  1564. // Guarantee uniqueness
  1565. switch (builtIn) {
  1566. case spv::BuiltIn::SubgroupSize:
  1567. if (laneCountBuiltinId)
  1568. return laneCountBuiltinId;
  1569. break;
  1570. case spv::BuiltIn::SubgroupLocalInvocationId:
  1571. if (laneIndexBuiltinId)
  1572. return laneIndexBuiltinId;
  1573. break;
  1574. default:
  1575. // Only allow the two cases we know about
  1576. assert(false && "unsupported builtin case");
  1577. return 0;
  1578. }
  1579. theBuilder.requireCapability(spv::Capability::GroupNonUniform);
  1580. uint32_t type = theBuilder.getUint32Type();
  1581. // Create a dummy StageVar for this builtin variable
  1582. const uint32_t varId =
  1583. theBuilder.addStageBuiltinVar(type, spv::StorageClass::Input, builtIn);
  1584. const hlsl::SigPoint *sigPoint =
  1585. hlsl::SigPoint::GetSigPoint(hlsl::SigPointFromInputQual(
  1586. hlsl::DxilParamInputQual::In, shaderModel.GetKind(),
  1587. /*isPatchConstant=*/false));
  1588. StageVar stageVar(sigPoint, /*semaInfo=*/{}, /*builtinAttr=*/nullptr, type,
  1589. /*locCount=*/0);
  1590. stageVar.setIsSpirvBuiltin();
  1591. stageVar.setSpirvId(varId);
  1592. stageVars.push_back(stageVar);
  1593. switch (builtIn) {
  1594. case spv::BuiltIn::SubgroupSize:
  1595. laneCountBuiltinId = varId;
  1596. break;
  1597. case spv::BuiltIn::SubgroupLocalInvocationId:
  1598. laneIndexBuiltinId = varId;
  1599. break;
  1600. }
  1601. return varId;
  1602. }
  1603. uint32_t DeclResultIdMapper::createSpirvStageVar(StageVar *stageVar,
  1604. const NamedDecl *decl,
  1605. const llvm::StringRef name,
  1606. SourceLocation srcLoc) {
  1607. using spv::BuiltIn;
  1608. const auto sigPoint = stageVar->getSigPoint();
  1609. const auto semanticKind = stageVar->getSemanticInfo().getKind();
  1610. const auto sigPointKind = sigPoint->GetKind();
  1611. const uint32_t type = stageVar->getSpirvTypeId();
  1612. spv::StorageClass sc = getStorageClassForSigPoint(sigPoint);
  1613. if (sc == spv::StorageClass::Max)
  1614. return 0;
  1615. stageVar->setStorageClass(sc);
  1616. // [[vk::builtin(...)]] takes precedence.
  1617. if (const auto *builtinAttr = stageVar->getBuiltInAttr()) {
  1618. const auto spvBuiltIn =
  1619. llvm::StringSwitch<BuiltIn>(builtinAttr->getBuiltIn())
  1620. .Case("PointSize", BuiltIn::PointSize)
  1621. .Case("HelperInvocation", BuiltIn::HelperInvocation)
  1622. .Case("BaseVertex", BuiltIn::BaseVertex)
  1623. .Case("BaseInstance", BuiltIn::BaseInstance)
  1624. .Case("DrawIndex", BuiltIn::DrawIndex)
  1625. .Case("DeviceIndex", BuiltIn::DeviceIndex)
  1626. .Default(BuiltIn::Max);
  1627. assert(spvBuiltIn != BuiltIn::Max); // The frontend should guarantee this.
  1628. switch (spvBuiltIn) {
  1629. case BuiltIn::BaseVertex:
  1630. case BuiltIn::BaseInstance:
  1631. case BuiltIn::DrawIndex:
  1632. theBuilder.addExtension(Extension::KHR_shader_draw_parameters,
  1633. builtinAttr->getBuiltIn(),
  1634. builtinAttr->getLocation());
  1635. theBuilder.requireCapability(spv::Capability::DrawParameters);
  1636. break;
  1637. case BuiltIn::DeviceIndex:
  1638. theBuilder.addExtension(Extension::KHR_device_group,
  1639. stageVar->getSemanticStr(), srcLoc);
  1640. theBuilder.requireCapability(spv::Capability::DeviceGroup);
  1641. break;
  1642. }
  1643. return theBuilder.addStageBuiltinVar(type, sc, spvBuiltIn);
  1644. }
  1645. // The following translation assumes that semantic validity in the current
  1646. // shader model is already checked, so it only covers valid SigPoints for
  1647. // each semantic.
  1648. switch (semanticKind) {
  1649. // According to DXIL spec, the Position SV can be used by all SigPoints
  1650. // other than PCIn, HSIn, GSIn, PSOut, CSIn.
  1651. // According to Vulkan spec, the Position BuiltIn can only be used
  1652. // by VSOut, HS/DS/GS In/Out.
  1653. case hlsl::Semantic::Kind::Position: {
  1654. switch (sigPointKind) {
  1655. case hlsl::SigPoint::Kind::VSIn:
  1656. case hlsl::SigPoint::Kind::PCOut:
  1657. case hlsl::SigPoint::Kind::DSIn:
  1658. return theBuilder.addStageIOVar(type, sc, name.str());
  1659. case hlsl::SigPoint::Kind::VSOut:
  1660. case hlsl::SigPoint::Kind::HSCPIn:
  1661. case hlsl::SigPoint::Kind::HSCPOut:
  1662. case hlsl::SigPoint::Kind::DSCPIn:
  1663. case hlsl::SigPoint::Kind::DSOut:
  1664. case hlsl::SigPoint::Kind::GSVIn:
  1665. case hlsl::SigPoint::Kind::GSOut:
  1666. stageVar->setIsSpirvBuiltin();
  1667. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::Position);
  1668. case hlsl::SigPoint::Kind::PSIn:
  1669. stageVar->setIsSpirvBuiltin();
  1670. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::FragCoord);
  1671. default:
  1672. llvm_unreachable("invalid usage of SV_Position sneaked in");
  1673. }
  1674. }
  1675. // According to DXIL spec, the VertexID SV can only be used by VSIn.
  1676. // According to Vulkan spec, the VertexIndex BuiltIn can only be used by
  1677. // VSIn.
  1678. case hlsl::Semantic::Kind::VertexID: {
  1679. stageVar->setIsSpirvBuiltin();
  1680. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::VertexIndex);
  1681. }
  1682. // According to DXIL spec, the InstanceID SV can be used by VSIn, VSOut,
  1683. // HSCPIn, HSCPOut, DSCPIn, DSOut, GSVIn, GSOut, PSIn.
  1684. // According to Vulkan spec, the InstanceIndex BuitIn can only be used by
  1685. // VSIn.
  1686. case hlsl::Semantic::Kind::InstanceID: {
  1687. switch (sigPointKind) {
  1688. case hlsl::SigPoint::Kind::VSIn:
  1689. stageVar->setIsSpirvBuiltin();
  1690. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::InstanceIndex);
  1691. case hlsl::SigPoint::Kind::VSOut:
  1692. case hlsl::SigPoint::Kind::HSCPIn:
  1693. case hlsl::SigPoint::Kind::HSCPOut:
  1694. case hlsl::SigPoint::Kind::DSCPIn:
  1695. case hlsl::SigPoint::Kind::DSOut:
  1696. case hlsl::SigPoint::Kind::GSVIn:
  1697. case hlsl::SigPoint::Kind::GSOut:
  1698. case hlsl::SigPoint::Kind::PSIn:
  1699. return theBuilder.addStageIOVar(type, sc, name.str());
  1700. default:
  1701. llvm_unreachable("invalid usage of SV_InstanceID sneaked in");
  1702. }
  1703. }
  1704. // According to DXIL spec, the Depth{|GreaterEqual|LessEqual} SV can only be
  1705. // used by PSOut.
  1706. // According to Vulkan spec, the FragDepth BuiltIn can only be used by PSOut.
  1707. case hlsl::Semantic::Kind::Depth:
  1708. case hlsl::Semantic::Kind::DepthGreaterEqual:
  1709. case hlsl::Semantic::Kind::DepthLessEqual: {
  1710. stageVar->setIsSpirvBuiltin();
  1711. // Vulkan requires the DepthReplacing execution mode to write to FragDepth.
  1712. theBuilder.addExecutionMode(entryFunctionId,
  1713. spv::ExecutionMode::DepthReplacing, {});
  1714. if (semanticKind == hlsl::Semantic::Kind::DepthGreaterEqual)
  1715. theBuilder.addExecutionMode(entryFunctionId,
  1716. spv::ExecutionMode::DepthGreater, {});
  1717. else if (semanticKind == hlsl::Semantic::Kind::DepthLessEqual)
  1718. theBuilder.addExecutionMode(entryFunctionId,
  1719. spv::ExecutionMode::DepthLess, {});
  1720. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::FragDepth);
  1721. }
  1722. // According to DXIL spec, the ClipDistance/CullDistance SV can be used by all
  1723. // SigPoints other than PCIn, HSIn, GSIn, PSOut, CSIn.
  1724. // According to Vulkan spec, the ClipDistance/CullDistance BuiltIn can only
  1725. // be
  1726. // used by VSOut, HS/DS/GS In/Out.
  1727. case hlsl::Semantic::Kind::ClipDistance:
  1728. case hlsl::Semantic::Kind::CullDistance: {
  1729. switch (sigPointKind) {
  1730. case hlsl::SigPoint::Kind::VSIn:
  1731. case hlsl::SigPoint::Kind::PCOut:
  1732. case hlsl::SigPoint::Kind::DSIn:
  1733. return theBuilder.addStageIOVar(type, sc, name.str());
  1734. case hlsl::SigPoint::Kind::VSOut:
  1735. case hlsl::SigPoint::Kind::HSCPIn:
  1736. case hlsl::SigPoint::Kind::HSCPOut:
  1737. case hlsl::SigPoint::Kind::DSCPIn:
  1738. case hlsl::SigPoint::Kind::DSOut:
  1739. case hlsl::SigPoint::Kind::GSVIn:
  1740. case hlsl::SigPoint::Kind::GSOut:
  1741. case hlsl::SigPoint::Kind::PSIn:
  1742. llvm_unreachable("should be handled in gl_PerVertex struct");
  1743. default:
  1744. llvm_unreachable(
  1745. "invalid usage of SV_ClipDistance/SV_CullDistance sneaked in");
  1746. }
  1747. }
  1748. // According to DXIL spec, the IsFrontFace SV can only be used by GSOut and
  1749. // PSIn.
  1750. // According to Vulkan spec, the FrontFacing BuitIn can only be used in PSIn.
  1751. case hlsl::Semantic::Kind::IsFrontFace: {
  1752. switch (sigPointKind) {
  1753. case hlsl::SigPoint::Kind::GSOut:
  1754. return theBuilder.addStageIOVar(type, sc, name.str());
  1755. case hlsl::SigPoint::Kind::PSIn:
  1756. stageVar->setIsSpirvBuiltin();
  1757. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::FrontFacing);
  1758. default:
  1759. llvm_unreachable("invalid usage of SV_IsFrontFace sneaked in");
  1760. }
  1761. }
  1762. // According to DXIL spec, the Target SV can only be used by PSOut.
  1763. // There is no corresponding builtin decoration in SPIR-V. So generate normal
  1764. // Vulkan stage input/output variables.
  1765. case hlsl::Semantic::Kind::Target:
  1766. // An arbitrary semantic is defined by users. Generate normal Vulkan stage
  1767. // input/output variables.
  1768. case hlsl::Semantic::Kind::Arbitrary: {
  1769. return theBuilder.addStageIOVar(type, sc, name.str());
  1770. // TODO: patch constant function in hull shader
  1771. }
  1772. // According to DXIL spec, the DispatchThreadID SV can only be used by CSIn.
  1773. // According to Vulkan spec, the GlobalInvocationId can only be used in CSIn.
  1774. case hlsl::Semantic::Kind::DispatchThreadID: {
  1775. stageVar->setIsSpirvBuiltin();
  1776. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::GlobalInvocationId);
  1777. }
  1778. // According to DXIL spec, the GroupID SV can only be used by CSIn.
  1779. // According to Vulkan spec, the WorkgroupId can only be used in CSIn.
  1780. case hlsl::Semantic::Kind::GroupID: {
  1781. stageVar->setIsSpirvBuiltin();
  1782. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::WorkgroupId);
  1783. }
  1784. // According to DXIL spec, the GroupThreadID SV can only be used by CSIn.
  1785. // According to Vulkan spec, the LocalInvocationId can only be used in CSIn.
  1786. case hlsl::Semantic::Kind::GroupThreadID: {
  1787. stageVar->setIsSpirvBuiltin();
  1788. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::LocalInvocationId);
  1789. }
  1790. // According to DXIL spec, the GroupIndex SV can only be used by CSIn.
  1791. // According to Vulkan spec, the LocalInvocationIndex can only be used in
  1792. // CSIn.
  1793. case hlsl::Semantic::Kind::GroupIndex: {
  1794. stageVar->setIsSpirvBuiltin();
  1795. return theBuilder.addStageBuiltinVar(type, sc,
  1796. BuiltIn::LocalInvocationIndex);
  1797. }
  1798. // According to DXIL spec, the OutputControlID SV can only be used by HSIn.
  1799. // According to Vulkan spec, the InvocationId BuiltIn can only be used in
  1800. // HS/GS In.
  1801. case hlsl::Semantic::Kind::OutputControlPointID: {
  1802. stageVar->setIsSpirvBuiltin();
  1803. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::InvocationId);
  1804. }
  1805. // According to DXIL spec, the PrimitiveID SV can only be used by PCIn, HSIn,
  1806. // DSIn, GSIn, GSOut, and PSIn.
  1807. // According to Vulkan spec, the PrimitiveId BuiltIn can only be used in
  1808. // HS/DS/PS In, GS In/Out.
  1809. case hlsl::Semantic::Kind::PrimitiveID: {
  1810. // PrimitiveId requires either Tessellation or Geometry capability.
  1811. // Need to require one for PSIn.
  1812. if (sigPointKind == hlsl::SigPoint::Kind::PSIn)
  1813. theBuilder.requireCapability(spv::Capability::Geometry);
  1814. // Translate to PrimitiveId BuiltIn for all valid SigPoints.
  1815. stageVar->setIsSpirvBuiltin();
  1816. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::PrimitiveId);
  1817. }
  1818. // According to DXIL spec, the TessFactor SV can only be used by PCOut and
  1819. // DSIn.
  1820. // According to Vulkan spec, the TessLevelOuter BuiltIn can only be used in
  1821. // PCOut and DSIn.
  1822. case hlsl::Semantic::Kind::TessFactor: {
  1823. stageVar->setIsSpirvBuiltin();
  1824. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::TessLevelOuter);
  1825. }
  1826. // According to DXIL spec, the InsideTessFactor SV can only be used by PCOut
  1827. // and DSIn.
  1828. // According to Vulkan spec, the TessLevelInner BuiltIn can only be used in
  1829. // PCOut and DSIn.
  1830. case hlsl::Semantic::Kind::InsideTessFactor: {
  1831. stageVar->setIsSpirvBuiltin();
  1832. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::TessLevelInner);
  1833. }
  1834. // According to DXIL spec, the DomainLocation SV can only be used by DSIn.
  1835. // According to Vulkan spec, the TessCoord BuiltIn can only be used in DSIn.
  1836. case hlsl::Semantic::Kind::DomainLocation: {
  1837. stageVar->setIsSpirvBuiltin();
  1838. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::TessCoord);
  1839. }
  1840. // According to DXIL spec, the GSInstanceID SV can only be used by GSIn.
  1841. // According to Vulkan spec, the InvocationId BuiltIn can only be used in
  1842. // HS/GS In.
  1843. case hlsl::Semantic::Kind::GSInstanceID: {
  1844. stageVar->setIsSpirvBuiltin();
  1845. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::InvocationId);
  1846. }
  1847. // According to DXIL spec, the SampleIndex SV can only be used by PSIn.
  1848. // According to Vulkan spec, the SampleId BuiltIn can only be used in PSIn.
  1849. case hlsl::Semantic::Kind::SampleIndex: {
  1850. theBuilder.requireCapability(spv::Capability::SampleRateShading);
  1851. stageVar->setIsSpirvBuiltin();
  1852. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::SampleId);
  1853. }
  1854. // According to DXIL spec, the StencilRef SV can only be used by PSOut.
  1855. case hlsl::Semantic::Kind::StencilRef: {
  1856. theBuilder.addExtension(Extension::EXT_shader_stencil_export,
  1857. stageVar->getSemanticStr(), srcLoc);
  1858. theBuilder.requireCapability(spv::Capability::StencilExportEXT);
  1859. stageVar->setIsSpirvBuiltin();
  1860. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::FragStencilRefEXT);
  1861. }
  1862. // According to DXIL spec, the ViewID SV can only be used by PSIn.
  1863. case hlsl::Semantic::Kind::Barycentrics: {
  1864. theBuilder.addExtension(Extension::AMD_shader_explicit_vertex_parameter,
  1865. stageVar->getSemanticStr(), srcLoc);
  1866. stageVar->setIsSpirvBuiltin();
  1867. // Selecting the correct builtin according to interpolation mode
  1868. auto bi = BuiltIn::Max;
  1869. if (decl->hasAttr<HLSLNoPerspectiveAttr>()) {
  1870. if (decl->hasAttr<HLSLCentroidAttr>()) {
  1871. bi = BuiltIn::BaryCoordNoPerspCentroidAMD;
  1872. } else if (decl->hasAttr<HLSLSampleAttr>()) {
  1873. bi = BuiltIn::BaryCoordNoPerspSampleAMD;
  1874. } else {
  1875. bi = BuiltIn::BaryCoordNoPerspAMD;
  1876. }
  1877. } else {
  1878. if (decl->hasAttr<HLSLCentroidAttr>()) {
  1879. bi = BuiltIn::BaryCoordSmoothCentroidAMD;
  1880. } else if (decl->hasAttr<HLSLSampleAttr>()) {
  1881. bi = BuiltIn::BaryCoordSmoothSampleAMD;
  1882. } else {
  1883. bi = BuiltIn::BaryCoordSmoothAMD;
  1884. }
  1885. }
  1886. return theBuilder.addStageBuiltinVar(type, sc, bi);
  1887. }
  1888. // According to DXIL spec, the RenderTargetArrayIndex SV can only be used by
  1889. // VSIn, VSOut, HSCPIn, HSCPOut, DSIn, DSOut, GSVIn, GSOut, PSIn.
  1890. // According to Vulkan spec, the Layer BuiltIn can only be used in GSOut and
  1891. // PSIn.
  1892. case hlsl::Semantic::Kind::RenderTargetArrayIndex: {
  1893. switch (sigPointKind) {
  1894. case hlsl::SigPoint::Kind::VSIn:
  1895. case hlsl::SigPoint::Kind::HSCPIn:
  1896. case hlsl::SigPoint::Kind::HSCPOut:
  1897. case hlsl::SigPoint::Kind::PCOut:
  1898. case hlsl::SigPoint::Kind::DSIn:
  1899. case hlsl::SigPoint::Kind::DSCPIn:
  1900. case hlsl::SigPoint::Kind::GSVIn:
  1901. return theBuilder.addStageIOVar(type, sc, name.str());
  1902. case hlsl::SigPoint::Kind::VSOut:
  1903. case hlsl::SigPoint::Kind::DSOut:
  1904. theBuilder.addExtension(Extension::EXT_shader_viewport_index_layer,
  1905. "SV_RenderTargetArrayIndex", srcLoc);
  1906. theBuilder.requireCapability(
  1907. spv::Capability::ShaderViewportIndexLayerEXT);
  1908. stageVar->setIsSpirvBuiltin();
  1909. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::Layer);
  1910. case hlsl::SigPoint::Kind::GSOut:
  1911. case hlsl::SigPoint::Kind::PSIn:
  1912. theBuilder.requireCapability(spv::Capability::Geometry);
  1913. stageVar->setIsSpirvBuiltin();
  1914. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::Layer);
  1915. default:
  1916. llvm_unreachable("invalid usage of SV_RenderTargetArrayIndex sneaked in");
  1917. }
  1918. }
  1919. // According to DXIL spec, the ViewportArrayIndex SV can only be used by
  1920. // VSIn, VSOut, HSCPIn, HSCPOut, DSIn, DSOut, GSVIn, GSOut, PSIn.
  1921. // According to Vulkan spec, the ViewportIndex BuiltIn can only be used in
  1922. // GSOut and PSIn.
  1923. case hlsl::Semantic::Kind::ViewPortArrayIndex: {
  1924. switch (sigPointKind) {
  1925. case hlsl::SigPoint::Kind::VSIn:
  1926. case hlsl::SigPoint::Kind::HSCPIn:
  1927. case hlsl::SigPoint::Kind::HSCPOut:
  1928. case hlsl::SigPoint::Kind::PCOut:
  1929. case hlsl::SigPoint::Kind::DSIn:
  1930. case hlsl::SigPoint::Kind::DSCPIn:
  1931. case hlsl::SigPoint::Kind::GSVIn:
  1932. return theBuilder.addStageIOVar(type, sc, name.str());
  1933. case hlsl::SigPoint::Kind::VSOut:
  1934. case hlsl::SigPoint::Kind::DSOut:
  1935. theBuilder.addExtension(Extension::EXT_shader_viewport_index_layer,
  1936. "SV_ViewPortArrayIndex", srcLoc);
  1937. theBuilder.requireCapability(
  1938. spv::Capability::ShaderViewportIndexLayerEXT);
  1939. stageVar->setIsSpirvBuiltin();
  1940. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::ViewportIndex);
  1941. case hlsl::SigPoint::Kind::GSOut:
  1942. case hlsl::SigPoint::Kind::PSIn:
  1943. theBuilder.requireCapability(spv::Capability::MultiViewport);
  1944. stageVar->setIsSpirvBuiltin();
  1945. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::ViewportIndex);
  1946. default:
  1947. llvm_unreachable("invalid usage of SV_ViewportArrayIndex sneaked in");
  1948. }
  1949. }
  1950. // According to DXIL spec, the Coverage SV can only be used by PSIn and PSOut.
  1951. // According to Vulkan spec, the SampleMask BuiltIn can only be used in
  1952. // PSIn and PSOut.
  1953. case hlsl::Semantic::Kind::Coverage: {
  1954. stageVar->setIsSpirvBuiltin();
  1955. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::SampleMask);
  1956. }
  1957. // According to DXIL spec, the ViewID SV can only be used by VSIn, PCIn,
  1958. // HSIn, DSIn, GSIn, PSIn.
  1959. // According to Vulkan spec, the ViewIndex BuiltIn can only be used in
  1960. // VS/HS/DS/GS/PS input.
  1961. case hlsl::Semantic::Kind::ViewID: {
  1962. theBuilder.addExtension(Extension::KHR_multiview,
  1963. stageVar->getSemanticStr(), srcLoc);
  1964. theBuilder.requireCapability(spv::Capability::MultiView);
  1965. stageVar->setIsSpirvBuiltin();
  1966. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::ViewIndex);
  1967. }
  1968. // According to DXIL spec, the InnerCoverage SV can only be used as PSIn.
  1969. // According to Vulkan spec, the FullyCoveredEXT BuiltIn can only be used as
  1970. // PSIn.
  1971. case hlsl::Semantic::Kind::InnerCoverage: {
  1972. theBuilder.addExtension(Extension::EXT_fragment_fully_covered,
  1973. stageVar->getSemanticStr(), srcLoc);
  1974. theBuilder.requireCapability(spv::Capability::FragmentFullyCoveredEXT);
  1975. stageVar->setIsSpirvBuiltin();
  1976. return theBuilder.addStageBuiltinVar(type, sc, BuiltIn::FullyCoveredEXT);
  1977. }
  1978. default:
  1979. emitError("semantic %0 unimplemented", srcLoc)
  1980. << stageVar->getSemanticStr();
  1981. break;
  1982. }
  1983. return 0;
  1984. }
  1985. bool DeclResultIdMapper::validateVKAttributes(const NamedDecl *decl) {
  1986. bool success = true;
  1987. if (const auto *idxAttr = decl->getAttr<VKIndexAttr>()) {
  1988. if (!shaderModel.IsPS()) {
  1989. emitError("vk::index only allowed in pixel shader",
  1990. idxAttr->getLocation());
  1991. success = false;
  1992. }
  1993. const auto *locAttr = decl->getAttr<VKLocationAttr>();
  1994. if (!locAttr) {
  1995. emitError("vk::index should be used together with vk::location for "
  1996. "dual-source blending",
  1997. idxAttr->getLocation());
  1998. success = false;
  1999. } else {
  2000. const auto locNumber = locAttr->getNumber();
  2001. if (locNumber != 0) {
  2002. emitError("dual-source blending should use vk::location 0",
  2003. locAttr->getLocation());
  2004. success = false;
  2005. }
  2006. }
  2007. const auto idxNumber = idxAttr->getNumber();
  2008. if (idxNumber != 0 && idxNumber != 1) {
  2009. emitError("dual-source blending only accepts 0 or 1 as vk::index",
  2010. idxAttr->getLocation());
  2011. success = false;
  2012. }
  2013. }
  2014. return success;
  2015. }
  2016. bool DeclResultIdMapper::validateVKBuiltins(const NamedDecl *decl,
  2017. const hlsl::SigPoint *sigPoint) {
  2018. bool success = true;
  2019. if (const auto *builtinAttr = decl->getAttr<VKBuiltInAttr>()) {
  2020. // The front end parsing only allows vk::builtin to be attached to a
  2021. // function/parameter/variable; all of them are DeclaratorDecls.
  2022. const auto declType = getTypeOrFnRetType(cast<DeclaratorDecl>(decl));
  2023. const auto loc = builtinAttr->getLocation();
  2024. if (decl->hasAttr<VKLocationAttr>()) {
  2025. emitError("cannot use vk::builtin and vk::location together", loc);
  2026. success = false;
  2027. }
  2028. const llvm::StringRef builtin = builtinAttr->getBuiltIn();
  2029. if (builtin == "HelperInvocation") {
  2030. if (!declType->isBooleanType()) {
  2031. emitError("HelperInvocation builtin must be of boolean type", loc);
  2032. success = false;
  2033. }
  2034. if (sigPoint->GetKind() != hlsl::SigPoint::Kind::PSIn) {
  2035. emitError(
  2036. "HelperInvocation builtin can only be used as pixel shader input",
  2037. loc);
  2038. success = false;
  2039. }
  2040. } else if (builtin == "PointSize") {
  2041. if (!declType->isFloatingType()) {
  2042. emitError("PointSize builtin must be of float type", loc);
  2043. success = false;
  2044. }
  2045. switch (sigPoint->GetKind()) {
  2046. case hlsl::SigPoint::Kind::VSOut:
  2047. case hlsl::SigPoint::Kind::HSCPIn:
  2048. case hlsl::SigPoint::Kind::HSCPOut:
  2049. case hlsl::SigPoint::Kind::DSCPIn:
  2050. case hlsl::SigPoint::Kind::DSOut:
  2051. case hlsl::SigPoint::Kind::GSVIn:
  2052. case hlsl::SigPoint::Kind::GSOut:
  2053. case hlsl::SigPoint::Kind::PSIn:
  2054. break;
  2055. default:
  2056. emitError("PointSize builtin cannot be used as %0", loc)
  2057. << sigPoint->GetName();
  2058. success = false;
  2059. }
  2060. } else if (builtin == "BaseVertex" || builtin == "BaseInstance" ||
  2061. builtin == "DrawIndex") {
  2062. if (!declType->isSpecificBuiltinType(BuiltinType::Kind::Int) &&
  2063. !declType->isSpecificBuiltinType(BuiltinType::Kind::UInt)) {
  2064. emitError("%0 builtin must be of 32-bit scalar integer type", loc)
  2065. << builtin;
  2066. success = false;
  2067. }
  2068. if (sigPoint->GetKind() != hlsl::SigPoint::Kind::VSIn) {
  2069. emitError("%0 builtin can only be used in vertex shader input", loc)
  2070. << builtin;
  2071. success = false;
  2072. }
  2073. } else if (builtin == "DeviceIndex") {
  2074. if (getStorageClassForSigPoint(sigPoint) != spv::StorageClass::Input) {
  2075. emitError("%0 builtin can only be used as shader input", loc)
  2076. << builtin;
  2077. success = false;
  2078. }
  2079. if (!declType->isSpecificBuiltinType(BuiltinType::Kind::Int) &&
  2080. !declType->isSpecificBuiltinType(BuiltinType::Kind::UInt)) {
  2081. emitError("%0 builtin must be of 32-bit scalar integer type", loc)
  2082. << builtin;
  2083. success = false;
  2084. }
  2085. }
  2086. }
  2087. return success;
  2088. }
  2089. spv::StorageClass
  2090. DeclResultIdMapper::getStorageClassForSigPoint(const hlsl::SigPoint *sigPoint) {
  2091. // This translation is done based on the HLSL reference (see docs/dxil.rst).
  2092. const auto sigPointKind = sigPoint->GetKind();
  2093. const auto signatureKind = sigPoint->GetSignatureKind();
  2094. spv::StorageClass sc = spv::StorageClass::Max;
  2095. switch (signatureKind) {
  2096. case hlsl::DXIL::SignatureKind::Input:
  2097. sc = spv::StorageClass::Input;
  2098. break;
  2099. case hlsl::DXIL::SignatureKind::Output:
  2100. sc = spv::StorageClass::Output;
  2101. break;
  2102. case hlsl::DXIL::SignatureKind::Invalid: {
  2103. // There are some special cases in HLSL (See docs/dxil.rst):
  2104. // SignatureKind is "invalid" for PCIn, HSIn, GSIn, and CSIn.
  2105. switch (sigPointKind) {
  2106. case hlsl::DXIL::SigPointKind::PCIn:
  2107. case hlsl::DXIL::SigPointKind::HSIn:
  2108. case hlsl::DXIL::SigPointKind::GSIn:
  2109. case hlsl::DXIL::SigPointKind::CSIn:
  2110. sc = spv::StorageClass::Input;
  2111. break;
  2112. default:
  2113. llvm_unreachable("Found invalid SigPoint kind for semantic");
  2114. }
  2115. break;
  2116. }
  2117. case hlsl::DXIL::SignatureKind::PatchConstant: {
  2118. // There are some special cases in HLSL (See docs/dxil.rst):
  2119. // SignatureKind is "PatchConstant" for PCOut and DSIn.
  2120. switch (sigPointKind) {
  2121. case hlsl::DXIL::SigPointKind::PCOut:
  2122. // Patch Constant Output (Output of Hull which is passed to Domain).
  2123. sc = spv::StorageClass::Output;
  2124. break;
  2125. case hlsl::DXIL::SigPointKind::DSIn:
  2126. // Domain Shader regular input - Patch Constant data plus system values.
  2127. sc = spv::StorageClass::Input;
  2128. break;
  2129. default:
  2130. llvm_unreachable("Found invalid SigPoint kind for semantic");
  2131. }
  2132. break;
  2133. }
  2134. default:
  2135. llvm_unreachable("Found invalid SigPoint kind for semantic");
  2136. }
  2137. return sc;
  2138. }
  2139. uint32_t DeclResultIdMapper::getTypeAndCreateCounterForPotentialAliasVar(
  2140. const DeclaratorDecl *decl, bool *shouldBeAlias, SpirvEvalInfo *info) {
  2141. if (const auto *varDecl = dyn_cast<VarDecl>(decl)) {
  2142. // This method is only intended to be used to create SPIR-V variables in the
  2143. // Function or Private storage class.
  2144. assert(!varDecl->isExternallyVisible() || varDecl->isStaticDataMember());
  2145. }
  2146. const QualType type = getTypeOrFnRetType(decl);
  2147. // Whether we should generate this decl as an alias variable.
  2148. bool genAlias = false;
  2149. if (const auto *buffer = dyn_cast<HLSLBufferDecl>(decl->getDeclContext())) {
  2150. // For ConstantBuffer and TextureBuffer
  2151. if (buffer->isConstantBufferView())
  2152. genAlias = true;
  2153. } else if (TypeTranslator::isOrContainsAKindOfStructuredOrByteBuffer(type)) {
  2154. genAlias = true;
  2155. }
  2156. if (shouldBeAlias)
  2157. *shouldBeAlias = genAlias;
  2158. if (genAlias) {
  2159. needsLegalization = true;
  2160. createCounterVarForDecl(decl);
  2161. if (info)
  2162. info->setContainsAliasComponent(true);
  2163. }
  2164. return typeTranslator.translateType(type);
  2165. }
  2166. } // end namespace spirv
  2167. } // end namespace clang