yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
41e7e565e
master
1//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -shaderobj -output-using-type 2//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-vk -compute -shaderobj -output-using-type 3//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-cpu -compute -output-using-type -shaderobj 4//TEST:SIMPLE(filecheck=CHK_REPORT):-target hlsl -stage compute -entry computeMain -report-checkpoint-intermediates 5 6//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], stride=4):out,name=outputBuffer 7RWStructuredBuffer<float> outputBuffer; 8 9typedef DifferentialPair<float> dpfloat; 10typedef float.Differential dfloat; 11 12// A variety of tests to check for loop exit value inference. 13// For all of these loops, we expect our inference pass to be able to 14// infer the loop exit value correctly. 15// 16// Further, if the optimization pass runs successfully, then there should 17// be absolutely no context stored for any of these tests. 18// 19 20// CHK_REPORT: (0): note: no checkpoint contexts to report 21 22[Differentiable] 23float test_simple(float y) 24{ 25 float t = y; 26 27 for (int i = 0; i < 3; i++) 28 { 29 t = t * (i + 1); 30 } 31 32 return t; 33} 34 35[Differentiable] 36float test_strided(float y) 37{ 38 float t = y; 39 40 for (int i = 0; i < 5; i+=2) 41 { 42 t = t * (i + 1); 43 } 44 45 return t; 46} 47 48[Differentiable] 49float test_offset(float y) 50{ 51 float t = y; 52 53 for (int i = 2; i < 5; i+=2) 54 { 55 t = t * (i + 1); 56 } 57 58 return t; 59} 60 61[Differentiable] 62float test_negative_stride(float y) 63{ 64 float t = y; 65 66 for (int i = 7; i >= 1; i-=2) 67 { 68 t = t * (i + 1); 69 } 70 71 return t; 72} 73 74[Differentiable] 75float test_nested(float y) 76{ 77 float t = y; 78 79 for (int i = 0; i < 3; i++) 80 { 81 for (int j = 0; j < 3; j++) 82 { 83 t = t * (i + 4 * j + 1); 84 } 85 } 86 87 return t; 88} 89 90[Differentiable] 91float test_nested_with_offset(float y) 92{ 93 float t = y; 94 95 for (int i = -3; i < 3; i++) 96 { 97 for (int j = -3; j < 3; j++) 98 { 99 t = t * ((abs(i) % 2) + (abs(j) % 2) + 1); 100 } 101 } 102 103 return t; 104} 105 106[Differentiable] 107float test_nested_with_conditions(float y) 108{ 109 float t = y; 110 111 for (int i = 0; i < 3; i++) 112 { 113 if (i % 2 == 0) 114 { 115 for (int j = 0; j < 3; j++) 116 { 117 if (j % 2 == 0) 118 { 119 t = t * (i + 4 * j + 1); 120 } 121 } 122 } 123 } 124 125 return t; 126} 127 128[Differentiable] 129float test_with_continue(float y) 130{ 131 float t = y; 132 133 for (int i = 0; i < 5; i++) 134 { 135 if (i % 2 == 0) 136 { 137 continue; 138 } 139 140 t = t * (i + 1); 141 } 142 143 return t; 144} 145 146[Differentiable] 147float test_nested_with_continue(float y) 148{ 149 float t = y; 150 151 for (int i = 0; i < 3; i++) 152 { 153 if (i % 2 == 0) 154 continue; 155 156 for (int j = 0; j < 3; j++) 157 { 158 if (j % 2 == 0) 159 continue; 160 161 if (j == 0) 162 continue; 163 164 t = t * (i + 4 * j + 1); 165 } 166 } 167 168 return t; 169} 170 171[numthreads(1, 1, 1)] 172void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 173{ 174 outputBuffer[0] = 0.0f; // CHECK: 0.000000 175 176 { 177 dpfloat dpa = dpfloat(0.4, 0.0); 178 179 __bwd_diff(test_simple)(dpa, 1.0f); 180 outputBuffer[1] = dpa.d; // CHECK-NEXT: 6.000000 181 } 182 183 { 184 dpfloat dpa = dpfloat(0.4, 0.0); 185 186 __bwd_diff(test_strided)(dpa, 1.0f); 187 outputBuffer[2] = dpa.d; // CHECK-NEXT: 15.000000 188 } 189 190 { 191 dpfloat dpa = dpfloat(0.4, 0.0); 192 193 __bwd_diff(test_offset)(dpa, 1.0f); 194 outputBuffer[3] = dpa.d; // CHECK-NEXT: 15.000000 195 } 196 197 { 198 dpfloat dpa = dpfloat(0.4, 0.0); 199 200 __bwd_diff(test_negative_stride)(dpa, 1.0f); 201 outputBuffer[4] = dpa.d; // CHECK-NEXT: 384.000000 202 } 203 204 { 205 dpfloat dpa = dpfloat(0.4, 0.0); 206 207 __bwd_diff(test_nested)(dpa, 1.0f); 208 outputBuffer[5] = dpa.d; // CHECK-NEXT: 1247400.000000 209 } 210 211 { 212 dpfloat dpa = dpfloat(0.4, 0.0); 213 214 __bwd_diff(test_nested_with_offset)(dpa, 1.0f); 215 outputBuffer[6] = dpa.d; // CHECK-NEXT: 5159780352.000000 216 } 217 218 { 219 dpfloat dpa = dpfloat(0.4, 0.0); 220 221 __bwd_diff(test_nested_with_conditions)(dpa, 1.0f); 222 outputBuffer[7] = dpa.d; // CHECK-NEXT: 297.000000 223 } 224 225 { 226 dpfloat dpa = dpfloat(0.4, 0.0); 227 228 __bwd_diff(test_with_continue)(dpa, 1.0f); 229 outputBuffer[8] = dpa.d; // CHECK-NEXT: 8.000000 230 } 231 232 { 233 dpfloat dpa = dpfloat(0.4, 0.0); 234 235 __bwd_diff(test_nested_with_continue)(dpa, 1.0f); 236 outputBuffer[9] = dpa.d; // CHECK-NEXT: 6.000000 237 } 238} 239 240//CHK-NOT: note