From 3d0f5ee55788dca324641ae9268ee37dc4d7d9d5 Mon Sep 17 00:00:00 2001 From: "Harsh Aggarwal (NVIDIA)" Date: Wed, 10 Sep 2025 17:31:36 +0530 Subject: CUDA: Fix compiler crash with unsized array field - nonuniformres-as-… (#8380) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …function-parameter.slang #8315 Root Cause: CUDA compilation crashed with `assert failure: !seenFinalUnsizedArrayField` because unsized arrays like `RWStructuredBuffer globalBuffer[]` were not the final field in generated parameter structs, violating the layout constraint in slang-ir-layout.cpp. Fix: Extended `collectGlobalUniformParameters` to automatically reorder struct fields for CUDA targets - regular fields first, unsized arrays last. Other targets preserve original order. Impact: - Enables CUDA support for nonuniform resource indexing as function parameters - Zero impact on existing GLSL/HLSL/SPIRV targets - Automatic handling - no manual parameter reordering required Files: slang-emit.cpp, slang-ir-collect-global-uniforms.cpp/.h, test file --------- Co-authored-by: slangbot Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Ellie Hermaszewska --- source/slang/slang-emit.cpp | 2 +- source/slang/slang-ir-collect-global-uniforms.cpp | 49 ++++++++++++++++++++++- source/slang/slang-ir-collect-global-uniforms.h | 5 ++- 3 files changed, 52 insertions(+), 4 deletions(-) (limited to 'source/slang') diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index f5b818c5d..31e6d17be 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -802,7 +802,7 @@ Result linkAndOptimizeIR( // can assume that all ordinary/uniform data is strictly // passed using constant buffers. // - collectGlobalUniformParameters(irModule, outLinkedIR.globalScopeVarLayout); + collectGlobalUniformParameters(irModule, outLinkedIR.globalScopeVarLayout, target); #if 0 dumpIRIfEnabled(codeGenContext, irModule, "GLOBAL UNIFORMS COLLECTED"); #endif diff --git a/source/slang/slang-ir-collect-global-uniforms.cpp b/source/slang/slang-ir-collect-global-uniforms.cpp index fb2a2233c..aad0de44f 100644 --- a/source/slang/slang-ir-collect-global-uniforms.cpp +++ b/source/slang/slang-ir-collect-global-uniforms.cpp @@ -49,6 +49,7 @@ struct CollectGlobalUniformParametersContext // IRModule* module; IRVarLayout* globalScopeVarLayout; + CodeGenTarget target = CodeGenTarget::Unknown; IRGlobalParam* _getGlobalParamFromLayoutFieldKey(IRInst* key) { @@ -174,7 +175,47 @@ struct CollectGlobalUniformParametersContext // parameters that were present in the layout information (they are // represented as the fields of the global-scope `struct` layout). // - for (auto fieldLayoutAttr : globalParamsStructTypeLayout->getFieldLayoutAttrs()) + // For CUDA targets, we need to ensure unsized arrays come last to satisfy + // the layout constraint in slang-ir-layout.cpp + auto fieldAttrs = globalParamsStructTypeLayout->getFieldLayoutAttrs(); + + // Create ordered field list - for CUDA, put unsized arrays last + List orderedFields; + + if (target == CodeGenTarget::CUDASource) + { + // For CUDA: separate regular and unsized array fields + List regularFields; + List unsizedArrayFields; + + for (auto fieldLayoutAttr : fieldAttrs) + { + auto globalParam = + _getGlobalParamFromLayoutFieldKey(fieldLayoutAttr->getFieldKey()); + if (globalParam && as(globalParam->getDataType())) + { + unsizedArrayFields.add(fieldLayoutAttr); + } + else + { + regularFields.add(fieldLayoutAttr); + } + } + + // Add regular fields first, then unsized arrays + for (auto field : regularFields) + orderedFields.add(field); + for (auto field : unsizedArrayFields) + orderedFields.add(field); + } + else + { + // For other targets: preserve original order + for (auto field : fieldAttrs) + orderedFields.add(field); + } + + for (auto fieldLayoutAttr : orderedFields) { // We expect the IR layout pass to have encoded field per-field // layout so that the "key" for the field is the corresponding @@ -339,11 +380,15 @@ struct CollectGlobalUniformParametersContext } }; -void collectGlobalUniformParameters(IRModule* module, IRVarLayout* globalScopeVarLayout) +void collectGlobalUniformParameters( + IRModule* module, + IRVarLayout* globalScopeVarLayout, + CodeGenTarget target) { CollectGlobalUniformParametersContext context; context.module = module; context.globalScopeVarLayout = globalScopeVarLayout; + context.target = target; context.processModule(); } diff --git a/source/slang/slang-ir-collect-global-uniforms.h b/source/slang/slang-ir-collect-global-uniforms.h index 76f56f074..5f9393a9b 100644 --- a/source/slang/slang-ir-collect-global-uniforms.h +++ b/source/slang/slang-ir-collect-global-uniforms.h @@ -11,6 +11,9 @@ struct IRVarLayout; /// Collect global-scope shader parameters that use uniform/ordinary /// storage into a single `struct` (possibly wrapped in a constant buffer). /// -void collectGlobalUniformParameters(IRModule* module, IRVarLayout* globalScopeVarLayout); +void collectGlobalUniformParameters( + IRModule* module, + IRVarLayout* globalScopeVarLayout, + CodeGenTarget target = CodeGenTarget::Unknown); } // namespace Slang -- cgit v1.2.3