From 0d6312f3be66f4bff9eec9606228db3edc309e2c Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 15 May 2025 12:51:29 -0700 Subject: Add checking for hlsl register semantic. (#7118) * Add checking for hlsl register semantic. * Fix. * Fix test. * Fix switch error. * Fix tests. --- source/slang/slang-check-decl.cpp | 91 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 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 e7ce8721a..f340bd6fd 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -95,6 +95,8 @@ struct SemanticsDeclAttributesVisitor : public SemanticsDeclVisitorBase, void checkVarDeclCommon(VarDeclBase* varDecl); + void checkHLSLRegisterSemantic(VarDeclBase* varDecl, HLSLRegisterSemantic* registerSematnic); + void visitVarDecl(VarDecl* varDecl) { checkVarDeclCommon(varDecl); } // Synthesize the constructor declaration for a struct during header visit, as we @@ -12606,6 +12608,10 @@ void SemanticsDeclAttributesVisitor::checkVarDeclCommon(VarDeclBase* varDecl) { hasPushConstAttr = true; } + else if (auto registerSemantic = as(modifier)) + { + checkHLSLRegisterSemantic(varDecl, registerSemantic); + } } if (hasSpecConstAttr && hasPushConstAttr) { @@ -12623,6 +12629,91 @@ void SemanticsDeclAttributesVisitor::checkVarDeclCommon(VarDeclBase* varDecl) } } +void SemanticsDeclAttributesVisitor::checkHLSLRegisterSemantic( + VarDeclBase* varDecl, + HLSLRegisterSemantic* registerSemantic) +{ + auto registerName = registerSemantic->registerName.getContent(); + if (registerName.getLength() < 1) + { + getSink()->diagnose( + registerSemantic->registerName.getLoc(), + Diagnostics::invalidHLSLRegisterName, + registerSemantic->registerName); + return; + } + + // Check to make sure the HLSL semantic register name is consistent with the resource type. + + auto varType = getType(m_astBuilder, DeclRef(varDecl)); + varType = unwrapModifiedType(unwrapArrayType(varType)); + bool isValid = true; + if (auto resType = as(varType)) + { + switch (registerName[0]) + { + case 't': + if (resType->getAccess() != SLANG_RESOURCE_ACCESS_READ) + isValid = false; + break; + case 'u': + if (resType->getAccess() == SLANG_RESOURCE_ACCESS_READ) + isValid = false; + break; + case 's': + if (!resType->isCombined()) + isValid = false; + break; + default: + isValid = false; + break; + } + } + else if ( + as(varType) || as(varType) || + as(varType)) + { + if (registerName[0] != 't') + { + isValid = false; + } + } + else if ( + as(varType) || + as(varType) || + as(varType) || as(varType) || + as(varType) || + as(varType)) + { + if (registerName[0] != 'u') + { + isValid = false; + } + } + else if (as(varType)) + { + if (registerName[0] != 's') + { + isValid = false; + } + } + else if (as(varType)) + { + if (registerName[0] != 'b') + { + isValid = false; + } + } + if (!isValid) + { + getSink()->diagnose( + registerSemantic->registerName.getLoc(), + Diagnostics::invalidHLSLRegisterNameForType, + registerName, + varType); + } +} + void SemanticsDeclAttributesVisitor::checkForwardDerivativeOfAttribute( FunctionDeclBase* funcDecl, ForwardDerivativeOfAttribute* attr) -- cgit v1.2.3