summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com>2025-03-18 03:28:59 -0700
committerGitHub <noreply@github.com>2025-03-18 18:28:59 +0800
commit24c9611a8296395dfad23c6ae136fab585dff735 (patch)
tree16fbac1e87fb4818d6b261de2ff3e5973800cd11
parent613cca56f6da1e659e4ca8c48efc59802412fd4e (diff)
Fix `IRVar` hoisting when its already in the right block. (#6626)
Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
-rw-r--r--source/slang/slang-ir-util.cpp4
-rw-r--r--tests/autodiff/force-inline-differentiable.slang47
2 files changed, 50 insertions, 1 deletions
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<float> 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<T : __BuiltinFloatingPointType, let R : int, let C : int>
+[ForceInline, PreferRecompute, Differentiable]
+vector<T, R> column(matrix<T, R, C> m, int i) {
+ vector<T, R> 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