From d61d6b57dfb0788ebf3449bfde6db288fe4e44a8 Mon Sep 17 00:00:00 2001 From: Gangzheng Tong Date: Tue, 23 Sep 2025 09:39:39 -0700 Subject: Legalize type as well in legalizeOperand (#8483) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes a type mismatch issue. See the generated cuda code ```cuda struct Query_0 { EmptyExample_0 query_0; uint hasNonEmptyAbsorbingBoundary_0; }; struct Query_1 { uint hasNonEmptyAbsorbingBoundary_0; }; struct GlobalParams_0 { Query_0* gQuery_0; RWStructuredBuffer gInput_0; RWStructuredBuffer gOutput_0; }; ... Query_1 _S4 = *globalParams_0->gQuery_0; // ==> type mismatch at call site! ``` **Root Cause:** During the empty type legalization pass in Slang's IR processing, struct types were being optimized. e.g., `Query_0` → `Query_1` with empty type removed), but this created an inconsistency: **Function parameters were updated:** When Query_compute_0 function was legalized, its parameter type was correctly updated from `Query_0` to the optimized `Query_1` **Global parameter types were NOT updated:** The `ParameterBlock` type in globalParams still referenced the old `Query_0` type The PR adds special handling for type operands in the `legalizeInst` function. This triggers the legalization of the `StructType` from the original `legalizeOperand` call site. The leaglized result will be saved in the type-to-legal-type map and be re-used when the same type requires legalization again (e.g. in the `IRFunc` as parameter) Fixes: https://github.com/shader-slang/slang/issues/7905 --- tests/bugs/gh-7905.slang | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/bugs/gh-7905.slang (limited to 'tests') diff --git a/tests/bugs/gh-7905.slang b/tests/bugs/gh-7905.slang new file mode 100644 index 000000000..69dd6d3ba --- /dev/null +++ b/tests/bugs/gh-7905.slang @@ -0,0 +1,63 @@ +//TEST:SIMPLE(filecheck=CHECK): -target cuda -entry runPointEstimator + +// Test for issue #7905: CUDA Backend failure due to type mismatch +// This test ensures that struct types in ParameterBlock operands are properly +// legalized and don't create type mismatches in generated CUDA code. + +#define ZOMBIE_PROBLEM_DIMENSION 3 + +public interface IExample +{ + // computes the distance to the boundary + float compute(vector x); +}; + +public struct Query + where Example : IExample +{ + // private AbsorbingBoundaryGeometricQueries absorbingBoundaryGeometricQueries; + private Example query; + private uint hasNonEmptyAbsorbingBoundary; + + public float compute(vector x) + { + return query.compute(x); + } +}; + +public struct EmptyExample : IExample +{ + // computes the distance to the boundary + public float compute(vector x) + { + internal static const float FLT_MAX = 3.402823466e+38F; + return FLT_MAX; + } +}; + + +typedef EmptyExample ExampleQuery; +typedef Query QueryType; + + +uniform ParameterBlock gQuery; +uniform RWStructuredBuffer gInput; +uniform RWStructuredBuffer gOutput; + + +[shader("compute")] +[numthreads(256, 1, 1)] +void runPointEstimator(uint3 threadId: SV_DispatchThreadID, + uniform uint n) +{ + const uint idx = threadId.x; + + if (idx >= n) { + return; + } + + float res = gQuery.compute(gInput[idx]); + gOutput[idx] = res; +} + +// CHECK-NOT: Query_1 -- cgit v1.2.3