From 9a23a9aab3721828526c921db1e779008e133e8f Mon Sep 17 00:00:00 2001 From: Yong He Date: Sat, 8 Jun 2024 05:12:49 -0700 Subject: SPIRV `Block` decoration fixes. (#4303) * SPIRV `Block` decoration fixes. - SPIRV does not allow duplicate `Block` decorations. So we shouldn't be generating them. - Also fixes duplication of OpName. - SPIRV and HLSL do not allow ConstantBuffer with trailing unsized arrays. Added a check in the front-end against such code. * Convert failing cross-compile tests to filecheck. --------- Co-authored-by: Jay Kwak <82421531+jkwak-work@users.noreply.github.com> --- source/slang/slang-check-decl.cpp | 87 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'source/slang/slang-check-decl.cpp') diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 95528b185..bf61a6c2e 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -2101,6 +2101,19 @@ namespace Slang { getSink()->diagnose(varDecl->type.exp->loc, Diagnostics::incompleteTypeCannotBeUsedInBuffer, elementType); } + if (doesTypeHaveTag(elementType, TypeTag::Unsized)) + { + // If the element type is unsized, it can only be an array of resource types that we can legalize out. + // Ordinary unsized arrays are not allowed in a constant buffer since we cannot translate it to + // valid HLSL or SPIRV. + ArrayExpressionType* trailingArrayType = nullptr; + VarDeclBase* trailingArrayField = getTrailingUnsizedArrayElement(elementType, varDecl, trailingArrayType); + if (trailingArrayField && !isOpaqueHandleType(trailingArrayType->getElementType())) + { + getSink()->diagnose(trailingArrayField->loc, Diagnostics::cannotUseUnsizedTypeInConstantBuffer, trailingArrayType); + getSink()->diagnose(varDecl->loc, Diagnostics::seeConstantBufferDefinition); + } + } } else if (varDecl->findModifier()) { @@ -10420,6 +10433,80 @@ namespace Slang return defaultVis; } + VarDeclBase* getTrailingUnsizedArrayElement(Type* type, VarDeclBase* parentVar, ArrayExpressionType*& outArrayType) + { + while (auto modifiedType = as(type)) + type = modifiedType->getBase(); + HashSet seenTypes; + for (;;) + { + if (auto arrayType = as(type)) + { + if (arrayType->isUnsized()) + { + outArrayType = arrayType; + return parentVar; + } + else + return nullptr; + } + else if (auto declRefType = as(type)) + { + if (auto aggTypeDecl = declRefType->getDeclRef().as()) + { + auto varDecls = aggTypeDecl.getDecl()->getMembersOfType(); + if (varDecls.getCount() == 0) + return nullptr; + VarDeclBase* lastVarDecl = nullptr; + for (auto varDecl : varDecls) + { + if (isEffectivelyStatic(varDecl)) + continue; + lastVarDecl = varDecl; + } + auto lastMember = _getMemberDeclRef( + getCurrentASTBuilder(), aggTypeDecl, lastVarDecl).as(); + auto varType = getType(getCurrentASTBuilder(), lastMember); + if (!varType) + return nullptr; + if (!seenTypes.add(type)) + return nullptr; + type = varType; + parentVar = lastMember.getDecl(); + continue; + } + } + } + return nullptr; + } + + bool isOpaqueHandleType(Type* type) + { + while (auto modifiedType = as(type)) + type = modifiedType->getBase(); + if (as(type)) + return true; + if (as(type)) + return true; + if (as(type)) + return true; + if (as(type)) + return true; + if (as(type)) + return true; + if (as(type)) + return true; + if (as(type)) + return true; + if (as(type)) + return true; + if (as(type)) + return true; + if (as(type)) + return true; + return false; + } + void diagnoseCapabilityProvenance(CompilerOptionSet& optionSet, DiagnosticSink* sink, Decl* decl, CapabilityAtom atomToFind, bool optionallyNeverPrintDecl) { HashSet printedDecls; -- cgit v1.2.3