From 95fcf65c38d52ed458a3b11622ea8b55a3864c24 Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Mon, 18 Sep 2023 23:45:44 -0400 Subject: 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 --- tests/autodiff/reverse-while-loop-3.slang | 66 +++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/autodiff/reverse-while-loop-3.slang (limited to 'tests/autodiff/reverse-while-loop-3.slang') 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 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 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 + } +} -- cgit v1.2.3