summaryrefslogtreecommitdiff
path: root/tests/autodiff/reverse-while-loop-3.slang
diff options
context:
space:
mode:
authorSai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com>2023-09-18 23:45:44 -0400
committerGitHub <noreply@github.com>2023-09-18 20:45:44 -0700
commit95fcf65c38d52ed458a3b11622ea8b55a3864c24 (patch)
tree5f07d03893504367466a278d7a1bcf63d4372c57 /tests/autodiff/reverse-while-loop-3.slang
parente884b153f8c29438b818d9930b8342e5ac8f829f (diff)
Fix loop inversion issue that caused ordinary blocks with multiple predecessors (#3211)
* Add test case for while loop * Fix loop inversion issue that caused ordinary blocks with multiple predecessors The original version can leave the CFG in an invalid state with `e4` not being a break block or merge point, but having multiple predecessors. The updated version creates a separate jump block for each break instruction to avoid this issue. * Fixup tests
Diffstat (limited to 'tests/autodiff/reverse-while-loop-3.slang')
-rw-r--r--tests/autodiff/reverse-while-loop-3.slang66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/autodiff/reverse-while-loop-3.slang b/tests/autodiff/reverse-while-loop-3.slang
new file mode 100644
index 000000000..67f030030
--- /dev/null
+++ b/tests/autodiff/reverse-while-loop-3.slang
@@ -0,0 +1,66 @@
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
+//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
+//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type -shaderobj
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<float> outputBuffer;
+
+// This test isn't actually testing the output, but rather that the compiler doesn't crash upon
+// encountering a specific loop pattern. ('Data' is non-differentiable here, so the expected output is 0)s
+//
+
+typedef DifferentialPair<float> dpfloat;
+typedef float.Differential dfloat;
+
+struct P
+{
+ bool terminated;
+ bool isTerminated() { return terminated; }
+ bool isHit() { return !terminated; }
+};
+
+struct Data
+{
+ float t;
+};
+
+void updateData(Data data)
+{
+ data.t = data.t * data.t;
+}
+
+[BackwardDifferentiable]
+float test_simple_while(float y, uint n)
+{
+ Data d = { y };
+ P p;
+ p.terminated = false;
+ int i = n;
+
+ if (p.isTerminated())
+ return d.t;
+
+ [MaxIters(4)]
+ while (!p.isTerminated())
+ {
+ updateData(d);
+ p.terminated = (i-- == 0);
+ if (p.isTerminated())
+ break;
+
+ if (!p.isHit())
+ break;
+ }
+ return d.t;
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ {
+ dpfloat dpa = dpfloat(1.0, 0.0);
+
+ __bwd_diff(test_simple_while)(dpa, 2, 1.0f);
+ outputBuffer[0] = dpa.d; // Expect: 8.0
+ }
+}