From 24c9611a8296395dfad23c6ae136fab585dff735 Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Tue, 18 Mar 2025 03:28:59 -0700 Subject: Fix `IRVar` hoisting when its already in the right block. (#6626) Co-authored-by: Ellie Hermaszewska --- source/slang/slang-ir-util.cpp | 4 +- tests/autodiff/force-inline-differentiable.slang | 47 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/autodiff/force-inline-differentiable.slang diff --git a/source/slang/slang-ir-util.cpp b/source/slang/slang-ir-util.cpp index 4aef7e7ba..58bb7aaf2 100644 --- a/source/slang/slang-ir-util.cpp +++ b/source/slang/slang-ir-util.cpp @@ -2162,7 +2162,9 @@ void legalizeDefUse(IRGlobalValueWithCode* func) { // If inst is an var, this is easy, we just move it to the // common dominator. - var->insertBefore(commonDominator->getTerminator()); + if (var->getParent() != commonDominator) + var->insertBefore(commonDominator->getTerminator()); + if (shouldInitializeVar) { IRBuilder builder(func); diff --git a/tests/autodiff/force-inline-differentiable.slang b/tests/autodiff/force-inline-differentiable.slang new file mode 100644 index 000000000..5d22a525f --- /dev/null +++ b/tests/autodiff/force-inline-differentiable.slang @@ -0,0 +1,47 @@ +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK): -slang -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +// A general test for differentiable, force-inlined functions. +// +// This test was added to expose a bug in legalizeDefUse() where an IRVar can be be hoisted to +// after its uses (when it is already in the same block, but gets reinserted), and cause +// a validation bug. +// + +__generic +[ForceInline, PreferRecompute, Differentiable] +vector column(matrix m, int i) { + vector result; + [ForceUnroll] + for (int j = 0; j < R; j++) { + result[j] = m[j][i]; + } + return result; +} + +struct Data { + float4x4 m; + float foo() { + return length(column(m, 0).xyz); + } +}; + +[numthreads(1, 1, 1)] +void computeMain(uint3 id : SV_DispatchThreadID) +{ + Data data; + // Initialize matrix with some values + data.m = float4x4( + 3.0, 0.0, 0.0, 0.0, + 0.0, 5.0, 0.0, 0.0, + 0.0, 0.0, 7.0, 0.0, + 0.0, 0.0, 0.0, 11.0); + + // Calculate and store result + outputBuffer[0] = data.foo(); + + // CHECK: type: float + // CHECK: 3.0 +} \ No newline at end of file -- cgit v1.2.3