DeclResultIdMapper.cpp 96 KB

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