summaryrefslogtreecommitdiffstats
path: root/tests/ir/loop-inversion.slang
blob: 7611c4062a7f12ffd06767ed138ec34c3bf8e446 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//TEST():SIMPLE(filecheck=CHECK):-entry computeMain -stage compute -line-directive-mode none -target hlsl -loop-inversion
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=OUT):-shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=OUT):-dx12 -use-dxil -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=OUT):-cpu -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=OUT):-vk -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=OUT):-cpu -shaderobj -output-using-type

// Check that all the backends cope with the slightly unusual IR the loop inversion generated

// OUT: 180

// For all the below functions, verify that the body (adding to j and
// incrementing i) comes before any break. This verifies that the `break` has
// been moved to the end of the loop.

//TEST_INPUT:ubuffer(data=[0], stride=4):out,name=outputBuffer
RWStructuredBuffer<int> outputBuffer;

// A standard loop
// CHECK-LABEL: int a_{{.*}}()
// CHECK-NOT: break;
// CHECK: int {{.*}} = j_{{.*}} + [[i:i_[0-9]+]]
// CHECK: [[i]] + int(1);
// CHECK: if(
// CHECK: break;
// CHECK: return
int a()
{
    int j = 0;
    for(int i = 0; i < 10; ++i)
        j += i;
    return j;
}

// A vanilla while loop
// CHECK-LABEL: int b_{{.*}}()
// CHECK-NOT: break;
// CHECK: int {{.*}} = j_{{.*}} + [[i:i_[0-9]+]]
// CHECK: [[i]] + int(1);
// CHECK: if(
// CHECK: break;
// CHECK: return
int b()
{
    int j = 0;
    int i = 0;
    while(i < 10)
    {
        j += i;
        i++;
    }
    return j;
}

// A while loop with a break on the false branch
// CHECK-LABEL: int c_{{.*}}()
// CHECK-NOT: break;
// CHECK: int {{.*}} = j_{{.*}} + [[i:i_[0-9]+]]
// CHECK: [[i]] + int(1);
// CHECK: if(
// CHECK: break;
// CHECK: return
int c()
{
    int j = 0;
    int i = 0;
    do
    {
        if(i < 10)
            {}
        else
            break;
        j += i;
        i++;
    } while(true);
    return j;
}

// A while loop with a break on the true branch
// CHECK-LABEL: int d_{{.*}}()
// CHECK-NOT: break;
// CHECK: int {{.*}} = j_{{.*}} + [[i:i_[0-9]+]]
// CHECK: [[i]] + int(1);
// CHECK: if(
// CHECK: break;
// CHECK: return
int d()
{
    int j = 0;
    int i = 0;
    do
    {
        if(i >= 10)
            break;
        else
            {}
        j += i;
        i++;
    } while(true);
    return j;
}

[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
    outputBuffer[dispatchThreadID.x] = a() + b() + c() + d();
}