summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSruthik P <spatibandlla@nvidia.com>2025-05-13 11:09:23 +0530
committerGitHub <noreply@github.com>2025-05-13 05:39:23 +0000
commit6c262bc5c9a364cd8c6a4026dbb9f8034c500f11 (patch)
treeacc5a5faa3d32aa59ff0277bf62e2e1e2de09716
parent2fb95f99c3efbe54f92f6338ab8c6970f1ec35ee (diff)
Support Array Sizes using Generic arguments to be initialized via {} (#6720)
* Add support for Array Sizes using Generic arguments to be initialized via {} Fixes one subissue of #6138 This change adds support for initializing Arrays with Generic size arguments via {} and adds a test to verify it. The change checks for an array whose size parameter is a GenericParamIntVal and since the size of such an array will be known at link time, is not considered as a case of the size not being known statically. * Add support for Array Sizes using Generic arguments to be initialized via {} Fixes one subissue of #6138. Fixes the issue #6958. This change adds support for initializing Arrays with Generic size arguments via {} and adds a test to verify it. Support is added by means of adding a new AST Expr node that lowers down to the IR MakeArrayFromElement and the emission of a diagnostic is replaced with the creation of this new AST Expr node. * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
-rw-r--r--source/slang/slang-ast-expr.h6
-rw-r--r--source/slang/slang-check-conversion.cpp18
-rw-r--r--source/slang/slang-check-expr.cpp5
-rw-r--r--source/slang/slang-check-impl.h1
-rw-r--r--source/slang/slang-lower-to-ir.cpp11
-rw-r--r--tests/initializer-list/generic-array-init.slang28
6 files changed, 57 insertions, 12 deletions
diff --git a/source/slang/slang-ast-expr.h b/source/slang/slang-ast-expr.h
index 5d8caefa9..346ed56f1 100644
--- a/source/slang/slang-ast-expr.h
+++ b/source/slang/slang-ast-expr.h
@@ -139,6 +139,12 @@ class StringLiteralExpr : public LiteralExpr
FIDDLE() String value;
};
+FIDDLE()
+class MakeArrayFromElementExpr : public Expr
+{
+ FIDDLE(...)
+};
+
// An initializer list, e.g. `{ 1, 2, 3 }`
FIDDLE()
class InitializerListExpr : public Expr
diff --git a/source/slang/slang-check-conversion.cpp b/source/slang/slang-check-conversion.cpp
index 1c8846e66..a2cb0f91b 100644
--- a/source/slang/slang-check-conversion.cpp
+++ b/source/slang/slang-check-conversion.cpp
@@ -613,19 +613,13 @@ bool SemanticsVisitor::_readAggregateValueFromInitializerList(
}
else
{
- // We don't know the element count statically,
- // so what are we supposed to be doing?
- //
- if (outToExpr)
- {
- getSink()->diagnose(
- fromInitializerListExpr,
- Diagnostics::cannotUseInitializerListForArrayOfUnknownSize,
- toElementCount);
- }
- return false;
- }
+ auto toMakeArrayFromElementExpr = m_astBuilder->create<MakeArrayFromElementExpr>();
+ toMakeArrayFromElementExpr->loc = fromInitializerListExpr->loc;
+ toMakeArrayFromElementExpr->type = QualType(toType);
+ *outToExpr = toMakeArrayFromElementExpr;
+ return true;
+ }
for (UInt ee = 0; ee < elementCount; ++ee)
{
Expr* coercedArg = nullptr;
diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp
index 87f29d367..48f32952b 100644
--- a/source/slang/slang-check-expr.cpp
+++ b/source/slang/slang-check-expr.cpp
@@ -5181,6 +5181,11 @@ Expr* SemanticsExprVisitor::visitMemberExpr(MemberExpr* expr)
}
}
+Expr* SemanticsExprVisitor::visitMakeArrayFromElementExpr(MakeArrayFromElementExpr* expr)
+{
+ return expr;
+}
+
Expr* SemanticsExprVisitor::visitInitializerListExpr(InitializerListExpr* expr)
{
// If we are assigned a type, expr has already been legalized
diff --git a/source/slang/slang-check-impl.h b/source/slang/slang-check-impl.h
index 1e93d8381..a910a3722 100644
--- a/source/slang/slang-check-impl.h
+++ b/source/slang/slang-check-impl.h
@@ -2954,6 +2954,7 @@ public:
Expr* visitMemberExpr(MemberExpr* expr);
Expr* visitInitializerListExpr(InitializerListExpr* expr);
+ Expr* visitMakeArrayFromElementExpr(MakeArrayFromElementExpr* expr);
Expr* visitThisExpr(ThisExpr* expr);
Expr* visitThisTypeExpr(ThisTypeExpr* expr);
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp
index 90780882d..1006a3489 100644
--- a/source/slang/slang-lower-to-ir.cpp
+++ b/source/slang/slang-lower-to-ir.cpp
@@ -4825,6 +4825,17 @@ struct ExprLoweringVisitorBase : public ExprVisitor<Derived, LoweredValInfo>
}
}
+ LoweredValInfo visitMakeArrayFromElementExpr(MakeArrayFromElementExpr* expr)
+ {
+ auto irType = lowerType(context, expr->type);
+ auto irDefaultElement = getSimpleVal(
+ context,
+ getDefaultVal(as<ArrayExpressionType>(expr->type)->getElementType()));
+
+ return LoweredValInfo::simple(
+ getBuilder()->emitMakeArrayFromElement(irType, irDefaultElement));
+ }
+
LoweredValInfo visitInitializerListExpr(InitializerListExpr* expr)
{
// Allocate a temporary of the given type
diff --git a/tests/initializer-list/generic-array-init.slang b/tests/initializer-list/generic-array-init.slang
new file mode 100644
index 000000000..4257e2972
--- /dev/null
+++ b/tests/initializer-list/generic-array-init.slang
@@ -0,0 +1,28 @@
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUFFER):-shaderobj -vk
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUFFER):-shaderobj -mtl
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUFFER):-cpu -compute -entry computeMain
+
+//TEST_INPUT:ubuffer(data=[9 9 9 9], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+void test<let TSize : uint>()
+{
+ // Test array initialization with generic size
+ int array[TSize] = {};
+
+ // BUFFER: 0
+ outputBuffer[0] = array[0];
+ // BUFFER-NEXT: 0
+ outputBuffer[1] = array[1];
+ // BUFFER-NEXT: 0
+ outputBuffer[2] = array[2];
+ // BUFFER-NEXT: 3
+ outputBuffer[3] = array.getCount();
+}
+
+[shader("compute")]
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ test<3>();
+}