Skip to content

Commit

Permalink
HLSL-IR: Fix fmod polyfill
Browse files Browse the repository at this point in the history
Order of operations was wrong.

Bug: 375406903
Change-Id: I0a62b122294431200ab313fd996306efb55d6618
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/212834
Auto-Submit: Antonio Maiorano <[email protected]>
Commit-Queue: James Price <[email protected]>
Reviewed-by: James Price <[email protected]>
  • Loading branch information
amaiorano authored and Dawn LUCI CQ committed Oct 29, 2024
1 parent 3a6d8b6 commit 2710aff
Show file tree
Hide file tree
Showing 38 changed files with 53 additions and 52 deletions.
8 changes: 4 additions & 4 deletions src/tint/lang/hlsl/writer/binary_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void foo() {
float v_1 = right;
float v_2 = (v / v_1);
float v_3 = floor(v_2);
float val = ((v - (((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3))) * v_1);
float val = (v - ((((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3)) * v_1));
}
)");
Expand Down Expand Up @@ -266,7 +266,7 @@ void foo() {
float16_t v_1 = right;
float16_t v_2 = (v / v_1);
float16_t v_3 = floor(v_2);
float16_t val = ((v - (((v_2 < float16_t(0.0h))) ? (ceil(v_2)) : (v_3))) * v_1);
float16_t val = (v - ((((v_2 < float16_t(0.0h))) ? (ceil(v_2)) : (v_3)) * v_1));
}
)");
Expand Down Expand Up @@ -296,7 +296,7 @@ void foo() {
float3 v_1 = right;
float3 v_2 = (v / v_1);
float3 v_3 = floor(v_2);
float3 val = ((v - (((v_2 < (0.0f).xxx)) ? (ceil(v_2)) : (v_3))) * v_1);
float3 val = (v - ((((v_2 < (0.0f).xxx)) ? (ceil(v_2)) : (v_3)) * v_1));
}
)");
Expand Down Expand Up @@ -326,7 +326,7 @@ void foo() {
vector<float16_t, 3> v_1 = right;
vector<float16_t, 3> v_2 = (v / v_1);
vector<float16_t, 3> v_3 = floor(v_2);
vector<float16_t, 3> val = ((v - (((v_2 < (float16_t(0.0h)).xxx)) ? (ceil(v_2)) : (v_3))) * v_1);
vector<float16_t, 3> val = (v - ((((v_2 < (float16_t(0.0h)).xxx)) ? (ceil(v_2)) : (v_3)) * v_1));
}
)");
Expand Down
7 changes: 4 additions & 3 deletions src/tint/lang/hlsl/writer/raise/binary_polyfill.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ struct State {
d->SetValue(div->Result(0));

auto* trunc = b.Call(type, core::BuiltinFn::kTrunc, d);
auto* sub = b.Subtract(type, binary->LHS(), trunc);
auto* mul = b.Multiply(type, sub, binary->RHS());
binary->Result(0)->ReplaceAllUsesWith(mul->Result(0));
auto* mul = b.Multiply(type, trunc, binary->RHS());
auto* sub = b.Subtract(type, binary->LHS(), mul);

binary->Result(0)->ReplaceAllUsesWith(sub->Result(0));
});
binary->Destroy();
}
Expand Down
16 changes: 8 additions & 8 deletions src/tint/lang/hlsl/writer/raise/binary_polyfill_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ TEST_F(HlslWriter_BinaryPolyfillTest, ModF32) {
%4:f32 = div %x, %y
%5:f32 = let %4
%6:f32 = trunc %5
%7:f32 = sub %x, %6
%8:f32 = mul %7, %y
%7:f32 = mul %6, %y
%8:f32 = sub %x, %7
%a:f32 = let %8
ret
}
Expand Down Expand Up @@ -106,8 +106,8 @@ TEST_F(HlslWriter_BinaryPolyfillTest, ModF16) {
%4:f16 = div %x, %y
%5:f16 = let %4
%6:f16 = trunc %5
%7:f16 = sub %x, %6
%8:f16 = mul %7, %y
%7:f16 = mul %6, %y
%8:f16 = sub %x, %7
%a:f16 = let %8
ret
}
Expand Down Expand Up @@ -145,8 +145,8 @@ TEST_F(HlslWriter_BinaryPolyfillTest, ModF32Vec3) {
%4:vec3<f32> = div %x, %y
%5:vec3<f32> = let %4
%6:vec3<f32> = trunc %5
%7:vec3<f32> = sub %x, %6
%8:vec3<f32> = mul %7, %y
%7:vec3<f32> = mul %6, %y
%8:vec3<f32> = sub %x, %7
%a:vec3<f32> = let %8
ret
}
Expand Down Expand Up @@ -184,8 +184,8 @@ TEST_F(HlslWriter_BinaryPolyfillTest, ModF16Vec3) {
%4:vec3<f16> = div %x, %y
%5:vec3<f16> = let %4
%6:vec3<f16> = trunc %5
%7:vec3<f16> = sub %x, %6
%8:vec3<f16> = mul %7, %y
%7:vec3<f16> = mul %6, %y
%8:vec3<f16> = sub %x, %7
%a:vec3<f16> = let %8
ret
}
Expand Down
2 changes: 1 addition & 1 deletion test/tint/bug/chromium/1434271.wgsl.expected.ir.dxc.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void export_level_inner(uint3 coord) {
uint v_19 = (uint(dst_offset) * 4u);
float v_20 = (sum / 4.0f);
float v_21 = floor(v_20);
buf_out.Store((0u + v_19), asuint(((sum - (((v_20 < 0.0f)) ? (ceil(v_20)) : (v_21))) * 4.0f)));
buf_out.Store((0u + v_19), asuint((sum - ((((v_20 < 0.0f)) ? (ceil(v_20)) : (v_21)) * 4.0f))));
float4 v_22 = float4(a, (a * b), ((a / b) + c), sum);
float4 probabilities = (v_22 + max(sum, 0.0f));
tex_out[int2(coord.xy)] = probabilities;
Expand Down
2 changes: 1 addition & 1 deletion test/tint/bug/tint/948.wgsl.expected.ir.dxc.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void main_1() {
float x_184 = animationData.z;
float v_6 = ((x_181 * x_184) / 1.0f);
float v_7 = floor(v_6);
mt = (((x_181 * x_184) - (((v_6 < 0.0f)) ? (ceil(v_6)) : (v_7))) * 1.0f);
mt = ((x_181 * x_184) - ((((v_6 < 0.0f)) ? (ceil(v_6)) : (v_7)) * 1.0f));
f = 0.0f;
{
while(true) {
Expand Down
2 changes: 1 addition & 1 deletion test/tint/bug/tint/948.wgsl.expected.ir.fxc.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void main_1() {
float x_184 = animationData.z;
float v_6 = ((x_181 * x_184) / 1.0f);
float v_7 = floor(v_6);
mt = (((x_181 * x_184) - (((v_6 < 0.0f)) ? (ceil(v_6)) : (v_7))) * 1.0f);
mt = ((x_181 * x_184) - ((((v_6 < 0.0f)) ? (ceil(v_6)) : (v_7)) * 1.0f));
f = 0.0f;
{
while(true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float16_t b = float16_t(2.0h);
float16_t v = (a / b);
float16_t v_1 = floor(v);
float16_t r = ((a - (((v < float16_t(0.0h))) ? (ceil(v)) : (v_1))) * b);
float16_t r = (a - ((((v < float16_t(0.0h))) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float b = 2.0f;
float v = (a / b);
float v_1 = floor(v);
float r = ((a - (((v < 0.0f)) ? (ceil(v)) : (v_1))) * b);
float r = (a - ((((v < 0.0f)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float b = 2.0f;
float v = (a / b);
float v_1 = floor(v);
float r = ((a - (((v < 0.0f)) ? (ceil(v)) : (v_1))) * b);
float r = (a - ((((v < 0.0f)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
vector<float16_t, 3> b = vector<float16_t, 3>(float16_t(1.0h), float16_t(2.0h), float16_t(3.0h));
vector<float16_t, 3> v = (a / b);
vector<float16_t, 3> v_1 = floor(v);
vector<float16_t, 3> r = ((a - (((v < (float16_t(0.0h)).xxx)) ? (ceil(v)) : (v_1))) * b);
vector<float16_t, 3> r = (a - ((((v < (float16_t(0.0h)).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float3 b = float3(1.0f, 2.0f, 3.0f);
float3 v = (a / b);
float3 v_1 = floor(v);
float3 r = ((a - (((v < (0.0f).xxx)) ? (ceil(v)) : (v_1))) * b);
float3 r = (a - ((((v < (0.0f).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float3 b = float3(1.0f, 2.0f, 3.0f);
float3 v = (a / b);
float3 v_1 = floor(v);
float3 r = ((a - (((v < (0.0f).xxx)) ? (ceil(v)) : (v_1))) * b);
float3 r = (a - ((((v < (0.0f).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float16_t b = float16_t(4.0h);
vector<float16_t, 3> v = (a / b);
vector<float16_t, 3> v_1 = floor(v);
vector<float16_t, 3> r = ((a - (((v < (float16_t(0.0h)).xxx)) ? (ceil(v)) : (v_1))) * b);
vector<float16_t, 3> r = (a - ((((v < (float16_t(0.0h)).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float b = 4.0f;
float3 v = (a / b);
float3 v_1 = floor(v);
float3 r = ((a - (((v < (0.0f).xxx)) ? (ceil(v)) : (v_1))) * b);
float3 r = (a - ((((v < (0.0f).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float b = 4.0f;
float3 v = (a / b);
float3 v_1 = floor(v);
float3 r = ((a - (((v < (0.0f).xxx)) ? (ceil(v)) : (v_1))) * b);
float3 r = (a - ((((v < (0.0f).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
vector<float16_t, 3> b = vector<float16_t, 3>(float16_t(4.0h), float16_t(5.0h), float16_t(6.0h));
vector<float16_t, 3> v = (a / b);
vector<float16_t, 3> v_1 = floor(v);
vector<float16_t, 3> r = ((a - (((v < (float16_t(0.0h)).xxx)) ? (ceil(v)) : (v_1))) * b);
vector<float16_t, 3> r = (a - ((((v < (float16_t(0.0h)).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float3 b = float3(4.0f, 5.0f, 6.0f);
float3 v = (a / b);
float3 v_1 = floor(v);
float3 r = ((a - (((v < (0.0f).xxx)) ? (ceil(v)) : (v_1))) * b);
float3 r = (a - ((((v < (0.0f).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float3 b = float3(4.0f, 5.0f, 6.0f);
float3 v = (a / b);
float3 v_1 = floor(v);
float3 r = ((a - (((v < (0.0f).xxx)) ? (ceil(v)) : (v_1))) * b);
float3 r = (a - ((((v < (0.0f).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float16_t b = float16_t(0.0h);
float16_t v = (a / b);
float16_t v_1 = floor(v);
float16_t r = ((a - (((v < float16_t(0.0h))) ? (ceil(v)) : (v_1))) * b);
float16_t r = (a - ((((v < float16_t(0.0h))) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float b = 0.0f;
float v = (a / b);
float v_1 = floor(v);
float r = ((a - (((v < 0.0f)) ? (ceil(v)) : (v_1))) * b);
float r = (a - ((((v < 0.0f)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float b = 0.0f;
float v = (a / b);
float v_1 = floor(v);
float r = ((a - (((v < 0.0f)) ? (ceil(v)) : (v_1))) * b);
float r = (a - ((((v < 0.0f)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
vector<float16_t, 3> b = vector<float16_t, 3>(float16_t(0.0h), float16_t(5.0h), float16_t(0.0h));
vector<float16_t, 3> v = (a / b);
vector<float16_t, 3> v_1 = floor(v);
vector<float16_t, 3> r = ((a - (((v < (float16_t(0.0h)).xxx)) ? (ceil(v)) : (v_1))) * b);
vector<float16_t, 3> r = (a - ((((v < (float16_t(0.0h)).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float3 b = float3(0.0f, 5.0f, 0.0f);
float3 v = (a / b);
float3 v_1 = floor(v);
float3 r = ((a - (((v < (0.0f).xxx)) ? (ceil(v)) : (v_1))) * b);
float3 r = (a - ((((v < (0.0f).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ void f() {
float3 b = float3(0.0f, 5.0f, 0.0f);
float3 v = (a / b);
float3 v_1 = floor(v);
float3 r = ((a - (((v < (0.0f).xxx)) ? (ceil(v)) : (v_1))) * b);
float3 r = (a - ((((v < (0.0f).xxx)) ? (ceil(v)) : (v_1)) * b));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
float16_t v_1 = (b + b);
float16_t v_2 = (v / v_1);
float16_t v_3 = floor(v_2);
float16_t r = ((v - (((v_2 < float16_t(0.0h))) ? (ceil(v_2)) : (v_3))) * v_1);
float16_t r = (v - ((((v_2 < float16_t(0.0h))) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
float v_1 = (b + b);
float v_2 = (v / v_1);
float v_3 = floor(v_2);
float r = ((v - (((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3))) * v_1);
float r = (v - ((((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
float v_1 = (b + b);
float v_2 = (v / v_1);
float v_3 = floor(v_2);
float r = ((v - (((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3))) * v_1);
float r = (v - ((((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
vector<float16_t, 3> v_1 = (b + b);
vector<float16_t, 3> v_2 = (v / v_1);
vector<float16_t, 3> v_3 = floor(v_2);
vector<float16_t, 3> r = ((v - (((v_2 < (float16_t(0.0h)).xxx)) ? (ceil(v_2)) : (v_3))) * v_1);
vector<float16_t, 3> r = (v - ((((v_2 < (float16_t(0.0h)).xxx)) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
float3 v_1 = (b + b);
float3 v_2 = (v / v_1);
float3 v_3 = floor(v_2);
float3 r = ((v - (((v_2 < (0.0f).xxx)) ? (ceil(v_2)) : (v_3))) * v_1);
float3 r = (v - ((((v_2 < (0.0f).xxx)) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
float3 v_1 = (b + b);
float3 v_2 = (v / v_1);
float3 v_3 = floor(v_2);
float3 r = ((v - (((v_2 < (0.0f).xxx)) ? (ceil(v_2)) : (v_3))) * v_1);
float3 r = (v - ((((v_2 < (0.0f).xxx)) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
float16_t v_1 = b;
float16_t v_2 = (v / v_1);
float16_t v_3 = floor(v_2);
float16_t r = ((v - (((v_2 < float16_t(0.0h))) ? (ceil(v_2)) : (v_3))) * v_1);
float16_t r = (v - ((((v_2 < float16_t(0.0h))) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
float v_1 = b;
float v_2 = (v / v_1);
float v_3 = floor(v_2);
float r = ((v - (((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3))) * v_1);
float r = (v - ((((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
float v_1 = b;
float v_2 = (v / v_1);
float v_3 = floor(v_2);
float r = ((v - (((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3))) * v_1);
float r = (v - ((((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
vector<float16_t, 3> v_1 = b;
vector<float16_t, 3> v_2 = (v / v_1);
vector<float16_t, 3> v_3 = floor(v_2);
vector<float16_t, 3> r = ((v - (((v_2 < (float16_t(0.0h)).xxx)) ? (ceil(v_2)) : (v_3))) * v_1);
vector<float16_t, 3> r = (v - ((((v_2 < (float16_t(0.0h)).xxx)) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
float3 v_1 = b;
float3 v_2 = (v / v_1);
float3 v_3 = floor(v_2);
float3 r = ((v - (((v_2 < (0.0f).xxx)) ? (ceil(v_2)) : (v_3))) * v_1);
float3 r = (v - ((((v_2 < (0.0f).xxx)) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ void f() {
float3 v_1 = b;
float3 v_2 = (v / v_1);
float3 v_3 = floor(v_2);
float3 r = ((v - (((v_2 < (0.0f).xxx)) ? (ceil(v_2)) : (v_3))) * v_1);
float3 r = (v - ((((v_2 < (0.0f).xxx)) ? (ceil(v_2)) : (v_3)) * v_1));
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ void foo(int maybe_zero) {
float v_1 = b;
float v_2 = (v_1 / 0.0f);
float v_3 = floor(v_2);
b = ((v_1 - (((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3))) * 0.0f);
b = (v_1 - ((((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3)) * 0.0f));
float v_4 = float(maybe_zero);
b = (b / v_4);
float v_5 = float(maybe_zero);
float v_6 = b;
float v_7 = (v_6 / v_5);
float v_8 = floor(v_7);
b = ((v_6 - (((v_7 < 0.0f)) ? (ceil(v_7)) : (v_8))) * v_5);
b = (v_6 - ((((v_7 < 0.0f)) ? (ceil(v_7)) : (v_8)) * v_5));
}

[numthreads(1, 1, 1)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ void foo(int maybe_zero) {
float v_1 = b;
float v_2 = (v_1 / 0.0f);
float v_3 = floor(v_2);
b = ((v_1 - (((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3))) * 0.0f);
b = (v_1 - ((((v_2 < 0.0f)) ? (ceil(v_2)) : (v_3)) * 0.0f));
float v_4 = float(maybe_zero);
b = (b / v_4);
float v_5 = float(maybe_zero);
float v_6 = b;
float v_7 = (v_6 / v_5);
float v_8 = floor(v_7);
b = ((v_6 - (((v_7 < 0.0f)) ? (ceil(v_7)) : (v_8))) * v_5);
b = (v_6 - ((((v_7 < 0.0f)) ? (ceil(v_7)) : (v_8)) * v_5));
}

[numthreads(1, 1, 1)]
Expand Down

0 comments on commit 2710aff

Please sign in to comment.