summaryrefslogtreecommitdiffstats
path: root/tests/autodiff/reverse-while-loop-3.slang
blob: 0a27437e97ef13c81ddad0b010442ccf514f89f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -compute -shaderobj -output-using-type
//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-vk -compute -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-cpu -compute -output-using-type -shaderobj

//TEST_INPUT:ubuffer(data=[1], 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)
// 

typedef DifferentialPair<float> dpfloat;
typedef float.Differential dfloat;

struct P
{
    bool terminated;
    bool isTerminated() { return terminated; }
    bool isHit() { return !terminated; }
};

struct Data
{
    __init(float dataIn)
    {
        this.t = dataIn;
    }
    float t;
};

void updateData(Data data)
{
    data.t = data.t * data.t;
}

[BackwardDifferentiable]
float test_simple_while(float y, uint n)
{
    Data d = Data(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;
        //BUF: 0
    }
}