summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com>2023-04-28 16:22:12 -0400
committerGitHub <noreply@github.com>2023-04-28 13:22:12 -0700
commit2492ec59fb52c15d1658ab32f473521b40664168 (patch)
tree7569739846b16bda35577f6c33e37f46f99abfd4 /tests
parentb07f4effda2f87ac9b3229e588121d224fd8cf52 (diff)
Fix handling of `[PreferRecompute]`. (#2855)
Co-authored-by: Yong He <yhe@nvidia.com> Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/autodiff/reverse-loop-diff-only.slang72
-rw-r--r--tests/autodiff/reverse-loop-diff-only.slang.expected.txt6
2 files changed, 78 insertions, 0 deletions
diff --git a/tests/autodiff/reverse-loop-diff-only.slang b/tests/autodiff/reverse-loop-diff-only.slang
new file mode 100644
index 000000000..64db4b814
--- /dev/null
+++ b/tests/autodiff/reverse-loop-diff-only.slang
@@ -0,0 +1,72 @@
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
+//TEST:SIMPLE(filecheck=CHECK): -target hlsl -profile cs_5_0 -entry computeMain -line-directive-mode none
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<float> outputBuffer;
+
+typedef DifferentialPair<float> dpfloat;
+typedef float.Differential dfloat;
+
+[BackwardDifferentiable]
+[PreferRecompute]
+float compute(float x, float y)
+{
+ return x * y;
+}
+
+[BackwardDifferentiable]
+[ForceInline]
+float infinitesimal(float x)
+{
+ return x - detach(x);
+}
+
+// Test that computeLoop compiles to just return 0.
+// CHECK: float computeLoop{{[_0-9]*}}(float y{{[_0-9]*}})
+// CHECK-NOT: for{{.*}}
+// CHECK: return 0
+
+[BackwardDifferentiable]
+[PreferRecompute]
+float computeLoop(float y)
+{
+ float w = 0;
+
+ for (int i = 0; i < 8; i++)
+ {
+ w += compute(i, y);
+ }
+
+ return w - detach(w);
+}
+
+// Since computeLoop is recomputed, test_simple_loop should have nothing to store
+// therefore we check that there is no intermediate context type generated for test_simple_loop.
+
+// CHECK-NOT: struct {{[a-zA-Z0-9_]*}}test_simple_loop{{[a-zA-Z0-9_]*}}
+[BackwardDifferentiable]
+float test_simple_loop(float y)
+{
+ float x = computeLoop(y);
+ return y + x;
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ {
+ dpfloat dpa = dpfloat(1.0, 0.0);
+
+ __bwd_diff(test_simple_loop)(dpa, 1.0f);
+ outputBuffer[0] = dpa.d; // Expect: 29.0
+ }
+
+ {
+ dpfloat dpa = dpfloat(0.4, 0.0);
+
+ __bwd_diff(test_simple_loop)(dpa, 0.5f);
+ outputBuffer[1] = dpa.d; // Expect: 14.5
+ }
+
+ outputBuffer[2] = computeLoop(1.0);
+}
diff --git a/tests/autodiff/reverse-loop-diff-only.slang.expected.txt b/tests/autodiff/reverse-loop-diff-only.slang.expected.txt
new file mode 100644
index 000000000..f3b93a6a8
--- /dev/null
+++ b/tests/autodiff/reverse-loop-diff-only.slang.expected.txt
@@ -0,0 +1,6 @@
+type: float
+29.000000
+14.500000
+0.000000
+0.000000
+0.000000