summaryrefslogtreecommitdiffstats
path: root/source/slang
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2025-05-15 12:51:29 -0700
committerGitHub <noreply@github.com>2025-05-15 12:51:29 -0700
commit0d6312f3be66f4bff9eec9606228db3edc309e2c (patch)
treea868e452e2d25a88f8a6cdee109a280edd11c0fb /source/slang
parentd961f4438ef865028d289148d22e0fb5c0d8319a (diff)
Add checking for hlsl register semantic. (#7118)
* Add checking for hlsl register semantic. * Fix. * Fix test. * Fix switch error. * Fix tests.
Diffstat (limited to 'source/slang')
-rw-r--r--source/slang/slang-ast-type.cpp3
-rw-r--r--source/slang/slang-check-decl.cpp91
-rw-r--r--source/slang/slang-check-type.cpp12
-rw-r--r--source/slang/slang-diagnostic-defs.h7
-rw-r--r--source/slang/slang.cpp7
5 files changed, 116 insertions, 4 deletions
diff --git a/source/slang/slang-ast-type.cpp b/source/slang/slang-ast-type.cpp
index ff4cc0d10..18f3f90bc 100644
--- a/source/slang/slang-ast-type.cpp
+++ b/source/slang/slang-ast-type.cpp
@@ -396,8 +396,7 @@ Type* TypeType::_createCanonicalTypeOverride()
void GenericDeclRefType::_toTextOverride(StringBuilder& out)
{
- // TODO: what is appropriate here?
- out << toSlice("<DeclRef<GenericDecl>>");
+ out << getDeclRef();
}
Type* GenericDeclRefType::_createCanonicalTypeOverride()
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<HLSLRegisterSemantic>(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<VarDeclBase>(varDecl));
+ varType = unwrapModifiedType(unwrapArrayType(varType));
+ bool isValid = true;
+ if (auto resType = as<ResourceType>(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<HLSLByteAddressBufferType>(varType) || as<HLSLStructuredBufferType>(varType) ||
+ as<RaytracingAccelerationStructureType>(varType))
+ {
+ if (registerName[0] != 't')
+ {
+ isValid = false;
+ }
+ }
+ else if (
+ as<HLSLRWByteAddressBufferType>(varType) ||
+ as<HLSLRasterizerOrderedByteAddressBufferType>(varType) ||
+ as<HLSLRWStructuredBufferType>(varType) || as<HLSLConsumeStructuredBufferType>(varType) ||
+ as<HLSLAppendStructuredBufferType>(varType) ||
+ as<HLSLRasterizerOrderedStructuredBufferType>(varType))
+ {
+ if (registerName[0] != 'u')
+ {
+ isValid = false;
+ }
+ }
+ else if (as<SamplerStateType>(varType))
+ {
+ if (registerName[0] != 's')
+ {
+ isValid = false;
+ }
+ }
+ else if (as<ConstantBufferType>(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)
diff --git a/source/slang/slang-check-type.cpp b/source/slang/slang-check-type.cpp
index d32903175..b5f5240a5 100644
--- a/source/slang/slang-check-type.cpp
+++ b/source/slang/slang-check-type.cpp
@@ -331,17 +331,27 @@ bool SemanticsVisitor::CoerceToProperTypeImpl(
if (outProperType)
args.add(ExtractGenericArgVal(valParam->initExpr));
}
- else if (auto constraintParam = as<GenericTypeConstraintDecl>(member))
+ }
+
+ for (Decl* member : genericDeclRef.getDecl()->members)
+ {
+ if (auto constraintParam = as<GenericTypeConstraintDecl>(member))
{
auto genericParam = as<DeclRefType>(constraintParam->sub.type)->getDeclRef();
if (!genericParam)
return false;
auto genericTypeParamDecl = as<GenericTypeParamDecl>(genericParam.getDecl());
if (!genericTypeParamDecl)
+ {
+ diagSink->diagnose(typeExp.exp, Diagnostics::genericTypeNeedsArgs, typeExp);
return false;
+ }
auto defaultType = CheckProperType(genericTypeParamDecl->initType);
if (!defaultType)
+ {
+ diagSink->diagnose(typeExp.exp, Diagnostics::genericTypeNeedsArgs, typeExp);
return false;
+ }
auto witness =
tryGetSubtypeWitness(defaultType, CheckProperType(constraintParam->sup));
if (!witness)
diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h
index 808d867b6..6bedcfe3e 100644
--- a/source/slang/slang-diagnostic-defs.h
+++ b/source/slang/slang-diagnostic-defs.h
@@ -1366,6 +1366,13 @@ DIAGNOSTIC(
Error,
variableCannotBePushAndSpecializationConstant,
"'$0' cannot be a push constant and a specialization constant at the same time")
+
+DIAGNOSTIC(31221, Error, invalidHLSLRegisterName, "invalid HLSL register name '$0'.")
+DIAGNOSTIC(
+ 31222,
+ Error,
+ invalidHLSLRegisterNameForType,
+ "invalid HLSL register name '$0' for type '$1'.")
// Enums
DIAGNOSTIC(32000, Error, invalidEnumTagType, "invalid tag type for 'enum': '$0'")
diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp
index c7f6c920c..7fc519c03 100644
--- a/source/slang/slang.cpp
+++ b/source/slang/slang.cpp
@@ -2784,7 +2784,12 @@ Type* ComponentType::getTypeFromString(String const& typeStr, DiagnosticSink* si
SLANG_AST_BUILDER_RAII(linkage->getASTBuilder());
Expr* typeExpr = linkage->parseTermString(typeStr, scope);
- type = checkProperType(linkage, TypeExp(typeExpr), sink);
+ SharedSemanticsContext sharedSemanticsContext(linkage, nullptr, sink);
+ SemanticsVisitor visitor(&sharedSemanticsContext);
+ type = visitor.TranslateTypeNode(typeExpr);
+ auto typeOut = visitor.tryCoerceToProperType(TypeExp(type));
+ if (typeOut.type)
+ type = typeOut.type;
if (type)
{