summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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
-rw-r--r--tests/bugs/gh-4874-2.slang41
-rw-r--r--tests/bugs/gh-4874.slang50
-rw-r--r--tests/diagnostics/variable-redeclaration.slang.expected6
11 files changed, 179 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.
//
diff --git a/tests/bugs/gh-4874-2.slang b/tests/bugs/gh-4874-2.slang
new file mode 100644
index 000000000..7f6944f7a
--- /dev/null
+++ b/tests/bugs/gh-4874-2.slang
@@ -0,0 +1,41 @@
+// Same to gh-4874.slang, but defining the global resource typed variables
+// without the "const" modifier. This should lead to a compile time error.
+//
+//TEST:SIMPLE(filecheck=CHECK): -target spirv
+RWStructuredBuffer<uint> outputBuffer;
+
+RWStructuredBuffer<uint> inputBuffer;
+
+// CHECK: ([[# @LINE+1]]): error 30076:
+static RWStructuredBuffer<uint> gBuffer = inputBuffer;
+
+struct Stuff
+{
+ __init(RWStructuredBuffer<uint> a1, RWStructuredBuffer<uint> b2)
+ {
+ a = a1;
+ b = b2;
+ }
+
+ RWStructuredBuffer<uint> a;
+ RWStructuredBuffer<uint> b;
+}
+
+// CHECK: ([[# @LINE+1]]): error 30076:
+static Stuff gStuff = Stuff( inputBuffer, inputBuffer );
+
+uint test(uint x)
+{
+ return gBuffer[x]
+ + gStuff.a[x + 1] * 16
+ + gStuff.b[x + 2] * 256;
+}
+
+[numthreads(4, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ let tid = dispatchThreadID.x;
+ let inVal = tid;
+ let outVal = test(inVal);
+ outputBuffer[tid] = outVal;
+} \ No newline at end of file
diff --git a/tests/bugs/gh-4874.slang b/tests/bugs/gh-4874.slang
new file mode 100644
index 000000000..403f6fc50
--- /dev/null
+++ b/tests/bugs/gh-4874.slang
@@ -0,0 +1,50 @@
+// type-legalize-global-with-init.slang
+//
+// Confirm that type legalization can handle a global constant
+// with a resource type or a type that recursively contains
+// resources.
+//
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj -vk
+//
+//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<uint> outputBuffer;
+
+//TEST_INPUT:ubuffer(data=[1 2 3 4 5 6 7 8], stride=4):name=inputBuffer
+RWStructuredBuffer<uint> inputBuffer;
+
+static const RWStructuredBuffer<uint> gBuffer = inputBuffer;
+
+struct Stuff
+{
+ __init(RWStructuredBuffer<uint> a1, RWStructuredBuffer<uint> b2)
+ {
+ a = a1;
+ b = b2;
+ }
+
+ RWStructuredBuffer<uint> a;
+ RWStructuredBuffer<uint> b;
+}
+
+static const Stuff gStuff = Stuff( inputBuffer, inputBuffer );
+
+uint test(uint x)
+{
+ return gBuffer[x]
+ + gStuff.a[x + 1] * 16
+ + gStuff.b[x + 2] * 256;
+}
+
+[numthreads(4, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ let tid = dispatchThreadID.x;
+ let inVal = tid;
+ let outVal = test(inVal);
+ outputBuffer[tid] = outVal;
+ // CHECK: 321
+ // CHECK: 432
+ // CHECK: 543
+ // CHECK: 654
+} \ No newline at end of file
diff --git a/tests/diagnostics/variable-redeclaration.slang.expected b/tests/diagnostics/variable-redeclaration.slang.expected
index 944037b1c..1998c13c8 100644
--- a/tests/diagnostics/variable-redeclaration.slang.expected
+++ b/tests/diagnostics/variable-redeclaration.slang.expected
@@ -18,6 +18,12 @@ tests/diagnostics/variable-redeclaration.slang(51): error 30200: declaration of
tests/diagnostics/variable-redeclaration.slang(50): note: see previous declaration of 'size'
int size,
^~~~
+tests/diagnostics/variable-redeclaration.slang(14): error 30076: global variable cannot have opaque type.
+static Texture2D gA;
+ ^~
+tests/diagnostics/variable-redeclaration.slang(14): note: do you intend to define a `uniform` parameter instead?
+static Texture2D gA;
+ ^~
tests/diagnostics/variable-redeclaration.slang(21): error 30200: declaration of 'y' conflicts with existing declaration
int y = x;
^