summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2025-01-15 20:43:01 -0800
committerGitHub <noreply@github.com>2025-01-15 20:43:01 -0800
commit387f2be1e48a805ef0da34510a5ae0ebc0ba9c3e (patch)
tree8769f2b809fae510cfab39ff430063978ec3abb3 /source
parent6db69eae7ecfb5111a48723d3f3c4104c7da880a (diff)
Inline global constants that contains opaque handles for legalization. (#6098)
* Inline global constants that contains opaque handles for legalization. * Add diagnostics on opaque type global variables. * Fix. * Fix test.
Diffstat (limited to 'source')
-rw-r--r--source/slang/slang-ast-decl.h1
-rw-r--r--source/slang/slang-check-conformance.cpp9
-rw-r--r--source/slang/slang-check-decl.cpp12
-rw-r--r--source/slang/slang-diagnostic-defs.h4
-rw-r--r--source/slang/slang-emit.cpp3
-rw-r--r--source/slang/slang-ir-legalize-global-values.cpp45
-rw-r--r--source/slang/slang-ir-legalize-global-values.h8
-rw-r--r--source/slang/slang-ir-legalize-types.cpp2
8 files changed, 82 insertions, 2 deletions
diff --git a/source/slang/slang-ast-decl.h b/source/slang/slang-ast-decl.h
index 911455f17..81e7337c7 100644
--- a/source/slang/slang-ast-decl.h
+++ b/source/slang/slang-ast-decl.h
@@ -141,6 +141,7 @@ enum class TypeTag
Unsized = 1,
Incomplete = 2,
LinkTimeSized = 4,
+ Opaque = 8,
};
// Declaration of a type that represents some sort of aggregate
diff --git a/source/slang/slang-check-conformance.cpp b/source/slang/slang-check-conformance.cpp
index 499b409ea..71b87f447 100644
--- a/source/slang/slang-check-conformance.cpp
+++ b/source/slang/slang-check-conformance.cpp
@@ -347,9 +347,16 @@ TypeTag SemanticsVisitor::getTypeTags(Type* type)
if (auto parameterGroupType = as<UniformParameterGroupType>(type))
{
auto elementTags = getTypeTags(parameterGroupType->getElementType());
- elementTags = (TypeTag)((int)elementTags & ~(int)TypeTag::Unsized);
+ elementTags = (TypeTag)(((int)elementTags & ~(int)TypeTag::Unsized) | (int)TypeTag::Opaque);
return elementTags;
}
+ else if (
+ as<UntypedBufferResourceType>(type) || as<ResourceType>(type) ||
+ as<SamplerStateType>(type) || as<HLSLStructuredBufferTypeBase>(type) ||
+ as<DynamicResourceType>(type))
+ {
+ return TypeTag::Opaque;
+ }
else if (auto declRefType = as<DeclRefType>(type))
{
if (auto aggTypeDecl = as<AggTypeDecl>(declRefType->getDeclRef()))
diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp
index 04d5b7a75..2a5b5a4e3 100644
--- a/source/slang/slang-check-decl.cpp
+++ b/source/slang/slang-check-decl.cpp
@@ -2392,6 +2392,18 @@ void SemanticsDeclBodyVisitor::checkVarDeclCommon(VarDeclBase* varDecl)
{
getSink()->diagnose(varDecl, Diagnostics::varCannotBeUnsized);
}
+
+ bool isOpaque = (((int)varTypeTags & (int)TypeTag::Opaque) != 0);
+ if (isOpaque && isGlobalDecl(varDecl) && !varDecl->hasModifier<ConstModifier>() &&
+ varDecl->hasModifier<HLSLStaticModifier>())
+ {
+ // Opaque type global variable must be const.
+ getSink()->diagnose(varDecl, Diagnostics::globalVarCannotHaveOpaqueType);
+ if (varDecl->initExpr)
+ getSink()->diagnose(varDecl, Diagnostics::doYouMeanStaticConst);
+ else
+ getSink()->diagnose(varDecl, Diagnostics::doYouMeanUniform);
+ }
}
if (auto elementType = getConstantBufferElementType(varDecl->getType()))
diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h
index eacc345a7..7eddc16a9 100644
--- a/source/slang/slang-diagnostic-defs.h
+++ b/source/slang/slang-diagnostic-defs.h
@@ -738,6 +738,10 @@ DIAGNOSTIC(
cannotSpecializeGeneric,
"cannot specialize generic '$0' with the provided arguments.")
+DIAGNOSTIC(30076, Error, globalVarCannotHaveOpaqueType, "global variable cannot have opaque type.")
+DIAGNOSTIC(-1, Note, doYouMeanStaticConst, "do you intend to define a `static const` instead?")
+DIAGNOSTIC(-1, Note, doYouMeanUniform, "do you intend to define a `uniform` parameter instead?")
+
DIAGNOSTIC(
30100,
Error,
diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp
index b17bc03f3..176cd0558 100644
--- a/source/slang/slang-emit.cpp
+++ b/source/slang/slang-emit.cpp
@@ -52,6 +52,7 @@
#include "slang-ir-insts.h"
#include "slang-ir-layout.h"
#include "slang-ir-legalize-array-return-type.h"
+#include "slang-ir-legalize-global-values.h"
#include "slang-ir-legalize-image-subscript.h"
#include "slang-ir-legalize-mesh-outputs.h"
#include "slang-ir-legalize-uniform-buffer-load.h"
@@ -1073,6 +1074,8 @@ Result linkAndOptimizeIR(
// We don't need the legalize pass for C/C++ based types
if (options.shouldLegalizeExistentialAndResourceTypes)
{
+ inlineGlobalConstantsForLegalization(irModule);
+
// The Slang language allows interfaces to be used like
// ordinary types (including placing them in constant
// buffers and entry-point parameter lists), but then
diff --git a/source/slang/slang-ir-legalize-global-values.cpp b/source/slang/slang-ir-legalize-global-values.cpp
index 92a1edb2b..63dbed3bd 100644
--- a/source/slang/slang-ir-legalize-global-values.cpp
+++ b/source/slang/slang-ir-legalize-global-values.cpp
@@ -223,6 +223,8 @@ IRInst* GlobalInstInliningContextGeneric::maybeInlineGlobalValue(
}
if (as<IRType>(inst))
return inst;
+ if (!wrapReferences)
+ return inst;
// If we encounter a global value that shouldn't be inlined, e.g. a const literal,
// we should insert a GlobalValueRef() inst to wrap around it, so all the dependent
@@ -244,4 +246,47 @@ IRInst* GlobalInstInliningContextGeneric::maybeInlineGlobalValue(
return inlineInst(builder, cloneEnv, inst);
}
+struct GlobalInstLegalizationInliningContext : public GlobalInstInliningContextGeneric
+{
+ static bool isSimpleConstantType(IRType* type)
+ {
+ for (;;)
+ {
+ if (!type)
+ return true;
+ if (as<IRBasicType>(type))
+ return true;
+ if (as<IRVectorType>(type))
+ return true;
+ if (as<IRMatrixType>(type))
+ return true;
+ if (auto arrayType = as<IRArrayTypeBase>(type))
+ {
+ type = arrayType->getElementType();
+ continue;
+ }
+ return false;
+ }
+ }
+ bool isLegalGlobalInstForTarget(IRInst* inst) override
+ {
+ auto type = inst->getDataType();
+ return isSimpleConstantType(type);
+ }
+
+ bool isInlinableGlobalInstForTarget(IRInst* /* inst */) override { return false; }
+
+ bool shouldBeInlinedForTarget(IRInst* /* user */) override { return false; }
+
+ IRInst* getOutsideASM(IRInst* beforeInst) override { return beforeInst; }
+};
+
+void inlineGlobalConstantsForLegalization(IRModule* module)
+{
+ GlobalInstLegalizationInliningContext context;
+
+ context.wrapReferences = false;
+ context.inlineGlobalValuesAndRemoveIfUnused(module);
+}
+
} // namespace Slang
diff --git a/source/slang/slang-ir-legalize-global-values.h b/source/slang/slang-ir-legalize-global-values.h
index c563b02c9..87b9fccc2 100644
--- a/source/slang/slang-ir-legalize-global-values.h
+++ b/source/slang/slang-ir-legalize-global-values.h
@@ -12,6 +12,7 @@ struct IRModule;
struct GlobalInstInliningContextGeneric
{
Dictionary<IRInst*, bool> m_mapGlobalInstToShouldInline;
+ bool wrapReferences = true;
// Target-specific control over how inlining happens
virtual bool isLegalGlobalInstForTarget(IRInst* inst) = 0;
@@ -43,4 +44,11 @@ struct GlobalInstInliningContextGeneric
IRInst* inst,
IRCloneEnv& cloneEnv);
};
+
+// For global constant values that are resource typed or struct containing resource types,
+// we need to inline their uses to concrete function bodies so they can be legalized during
+// resource legalization.
+void inlineGlobalConstantsForLegalization(IRModule* module);
+
+
} // namespace Slang
diff --git a/source/slang/slang-ir-legalize-types.cpp b/source/slang/slang-ir-legalize-types.cpp
index ae95ca8da..8eb70596f 100644
--- a/source/slang/slang-ir-legalize-types.cpp
+++ b/source/slang/slang-ir-legalize-types.cpp
@@ -572,7 +572,7 @@ private:
// We expect that the parent is actually an IR function.
//
// TODO: What about the case where we have an `IRGlobalVar`
- // of a type that needs legalization, and teh variable has
+ // of a type that needs legalization, and the variable has
// an initializer? For now, I believe that case is disallowed
// in the legalization for global variables.
//