Skip to content

Commit

Permalink
HLSL-IR: fix frexp polyfill
Browse files Browse the repository at this point in the history
The polyfill needed to maintain the sign of the fractional part, not
the exponent.

Bug: b/375407462
Change-Id: I140aaaba18602919deb7657d3aefea3bbcca7614
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/212735
Auto-Submit: Antonio Maiorano <[email protected]>
Reviewed-by: James Price <[email protected]>
Commit-Queue: James Price <[email protected]>
  • Loading branch information
amaiorano authored and Dawn LUCI CQ committed Oct 28, 2024
1 parent 60e913d commit 9eb59d0
Show file tree
Hide file tree
Showing 22 changed files with 55 additions and 79 deletions.
10 changes: 5 additions & 5 deletions src/tint/lang/hlsl/writer/raise/builtin_polyfill.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1776,14 +1776,14 @@ struct State {
auto* exp_out = b.Var(ty.ptr<function>(arg_ty));
// HLSL frexp writes exponent part to second out param, and returns the fraction
// (mantissa) part.
auto* call_result = b.Call<hlsl::ir::BuiltinCall>(arg_ty, hlsl::BuiltinFn::kFrexp, arg,
b.Load(exp_out));
// The returned exponent is always positive, but for WGSL, we want it to keep the sign
core::ir::Instruction* fract = b.Call<hlsl::ir::BuiltinCall>(
arg_ty, hlsl::BuiltinFn::kFrexp, arg, b.Load(exp_out));
// The returned fraction is always positive, but for WGSL, we want it to keep the sign
// of the input value.
auto* arg_sign = BuildSign(arg);
b.Store(exp_out, b.Multiply(arg_ty, arg_sign, b.Load(exp_out)));
fract = b.Multiply(arg_ty, arg_sign, fract);
// Replace the call with new result struct
b.ConstructWithResult(call->DetachResult(), call_result,
b.ConstructWithResult(call->DetachResult(), fract,
b.Convert(arg_i32_ty, b.Load(exp_out)));
});
call->Destroy();
Expand Down
24 changes: 10 additions & 14 deletions src/tint/lang/hlsl/writer/raise/builtin_polyfill_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7379,13 +7379,11 @@ __frexp_result_f32 = struct @align(4) {
%6:f32 = hlsl.frexp %3, %5
%7:i32 = hlsl.sign %3
%8:f32 = convert %7
%9:f32 = load %4
%10:f32 = mul %8, %9
store %4, %10
%11:f32 = load %4
%12:i32 = convert %11
%13:__frexp_result_f32 = construct %6, %12
%a:__frexp_result_f32 = let %13
%9:f32 = mul %8, %6
%10:f32 = load %4
%11:i32 = convert %10
%12:__frexp_result_f32 = construct %9, %11
%a:__frexp_result_f32 = let %12
ret
}
}
Expand Down Expand Up @@ -7437,13 +7435,11 @@ __frexp_result_vec3_f32 = struct @align(16) {
%6:vec3<f32> = hlsl.frexp %3, %5
%7:vec3<i32> = hlsl.sign %3
%8:vec3<f32> = convert %7
%9:vec3<f32> = load %4
%10:vec3<f32> = mul %8, %9
store %4, %10
%11:vec3<f32> = load %4
%12:vec3<i32> = convert %11
%13:__frexp_result_vec3_f32 = construct %6, %12
%a:__frexp_result_vec3_f32 = let %13
%9:vec3<f32> = mul %8, %6
%10:vec3<f32> = load %4
%11:vec3<i32> = convert %10
%12:__frexp_result_vec3_f32 = construct %9, %11
%a:__frexp_result_vec3_f32 = let %12
ret
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ void main() {
frexp_result_f32 res = {0.625f, int(1)};
float v = 0.0f;
float v_1 = frexp(runtime_in, v);
float v_2 = float(sign(runtime_in));
v = (v_2 * v);
frexp_result_f32 v_3 = {v_1, int(v)};
float v_2 = (float(sign(runtime_in)) * v_1);
frexp_result_f32 v_3 = {v_2, int(v)};
res = v_3;
frexp_result_f32 v_4 = {0.625f, int(1)};
res = v_4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ void main() {
frexp_result_f32 res = {0.625f, int(1)};
float v = 0.0f;
float v_1 = frexp(runtime_in, v);
float v_2 = float(sign(runtime_in));
v = (v_2 * v);
frexp_result_f32 v_3 = {v_1, int(v)};
float v_2 = (float(sign(runtime_in)) * v_1);
frexp_result_f32 v_3 = {v_2, int(v)};
res = v_3;
frexp_result_f32 v_4 = {0.625f, int(1)};
res = v_4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ void main() {
float tint_symbol = 1.25f;
float v = 0.0f;
float v_1 = frexp(tint_symbol, v);
float v_2 = float(sign(tint_symbol));
v = (v_2 * v);
frexp_result_f32 res = {v_1, int(v)};
float v_2 = (float(sign(tint_symbol)) * v_1);
frexp_result_f32 res = {v_2, int(v)};
float fract = res.fract;
int exp = res.exp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ void main() {
float tint_symbol = 1.25f;
float v = 0.0f;
float v_1 = frexp(tint_symbol, v);
float v_2 = float(sign(tint_symbol));
v = (v_2 * v);
frexp_result_f32 res = {v_1, int(v)};
float v_2 = (float(sign(tint_symbol)) * v_1);
frexp_result_f32 res = {v_2, int(v)};
float fract = res.fract;
int exp = res.exp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ void main() {
frexp_result_vec2_f32 res = {float2(0.625f, 0.9375f), int2(int(1), int(2))};
float2 v = (0.0f).xx;
float2 v_1 = frexp(runtime_in, v);
float2 v_2 = float2(sign(runtime_in));
v = (v_2 * v);
frexp_result_vec2_f32 v_3 = {v_1, int2(v)};
float2 v_2 = (float2(sign(runtime_in)) * v_1);
frexp_result_vec2_f32 v_3 = {v_2, int2(v)};
res = v_3;
frexp_result_vec2_f32 v_4 = {float2(0.625f, 0.9375f), int2(int(1), int(2))};
res = v_4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ void main() {
frexp_result_vec2_f32 res = {float2(0.625f, 0.9375f), int2(int(1), int(2))};
float2 v = (0.0f).xx;
float2 v_1 = frexp(runtime_in, v);
float2 v_2 = float2(sign(runtime_in));
v = (v_2 * v);
frexp_result_vec2_f32 v_3 = {v_1, int2(v)};
float2 v_2 = (float2(sign(runtime_in)) * v_1);
frexp_result_vec2_f32 v_3 = {v_2, int2(v)};
res = v_3;
frexp_result_vec2_f32 v_4 = {float2(0.625f, 0.9375f), int2(int(1), int(2))};
res = v_4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ void main() {
float2 tint_symbol = float2(1.25f, 3.75f);
float2 v = (0.0f).xx;
float2 v_1 = frexp(tint_symbol, v);
float2 v_2 = float2(sign(tint_symbol));
v = (v_2 * v);
frexp_result_vec2_f32 res = {v_1, int2(v)};
float2 v_2 = (float2(sign(tint_symbol)) * v_1);
frexp_result_vec2_f32 res = {v_2, int2(v)};
float2 fract = res.fract;
int2 exp = res.exp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ void main() {
float2 tint_symbol = float2(1.25f, 3.75f);
float2 v = (0.0f).xx;
float2 v_1 = frexp(tint_symbol, v);
float2 v_2 = float2(sign(tint_symbol));
v = (v_2 * v);
frexp_result_vec2_f32 res = {v_1, int2(v)};
float2 v_2 = (float2(sign(tint_symbol)) * v_1);
frexp_result_vec2_f32 res = {v_2, int2(v)};
float2 fract = res.fract;
int2 exp = res.exp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_3dd21e() {
vector<float16_t, 4> v = arg_0;
vector<float16_t, 4> v_1 = (float16_t(0.0h)).xxxx;
vector<float16_t, 4> v_2 = frexp(v, v_1);
vector<float16_t, 4> v_3 = vector<float16_t, 4>(sign(v));
v_1 = (v_3 * v_1);
frexp_result_vec4_f16 res = {v_2, int4(v_1)};
vector<float16_t, 4> v_3 = (vector<float16_t, 4>(sign(v)) * v_2);
frexp_result_vec4_f16 res = {v_3, int4(v_1)};
}

void fragment_main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_4b2200() {
float v = arg_0;
float v_1 = 0.0f;
float v_2 = frexp(v, v_1);
float v_3 = float(sign(v));
v_1 = (v_3 * v_1);
frexp_result_f32 res = {v_2, int(v_1)};
float v_3 = (float(sign(v)) * v_2);
frexp_result_f32 res = {v_3, int(v_1)};
}

void fragment_main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_4b2200() {
float v = arg_0;
float v_1 = 0.0f;
float v_2 = frexp(v, v_1);
float v_3 = float(sign(v));
v_1 = (v_3 * v_1);
frexp_result_f32 res = {v_2, int(v_1)};
float v_3 = (float(sign(v)) * v_2);
frexp_result_f32 res = {v_3, int(v_1)};
}

void fragment_main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_5257dd() {
float16_t v = arg_0;
float16_t v_1 = float16_t(0.0h);
float16_t v_2 = frexp(v, v_1);
float16_t v_3 = float16_t(sign(v));
v_1 = (v_3 * v_1);
frexp_result_f16 res = {v_2, int(v_1)};
float16_t v_3 = (float16_t(sign(v)) * v_2);
frexp_result_f16 res = {v_3, int(v_1)};
}

void fragment_main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_5f47bf() {
vector<float16_t, 2> v = arg_0;
vector<float16_t, 2> v_1 = (float16_t(0.0h)).xx;
vector<float16_t, 2> v_2 = frexp(v, v_1);
vector<float16_t, 2> v_3 = vector<float16_t, 2>(sign(v));
v_1 = (v_3 * v_1);
frexp_result_vec2_f16 res = {v_2, int2(v_1)};
vector<float16_t, 2> v_3 = (vector<float16_t, 2>(sign(v)) * v_2);
frexp_result_vec2_f16 res = {v_3, int2(v_1)};
}

void fragment_main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_77af93() {
float4 v = arg_0;
float4 v_1 = (0.0f).xxxx;
float4 v_2 = frexp(v, v_1);
float4 v_3 = float4(sign(v));
v_1 = (v_3 * v_1);
frexp_result_vec4_f32 res = {v_2, int4(v_1)};
float4 v_3 = (float4(sign(v)) * v_2);
frexp_result_vec4_f32 res = {v_3, int4(v_1)};
}

void fragment_main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_77af93() {
float4 v = arg_0;
float4 v_1 = (0.0f).xxxx;
float4 v_2 = frexp(v, v_1);
float4 v_3 = float4(sign(v));
v_1 = (v_3 * v_1);
frexp_result_vec4_f32 res = {v_2, int4(v_1)};
float4 v_3 = (float4(sign(v)) * v_2);
frexp_result_vec4_f32 res = {v_3, int4(v_1)};
}

void fragment_main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_979800() {
float3 v = arg_0;
float3 v_1 = (0.0f).xxx;
float3 v_2 = frexp(v, v_1);
float3 v_3 = float3(sign(v));
v_1 = (v_3 * v_1);
frexp_result_vec3_f32 res = {v_2, int3(v_1)};
float3 v_3 = (float3(sign(v)) * v_2);
frexp_result_vec3_f32 res = {v_3, int3(v_1)};
}

void fragment_main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_979800() {
float3 v = arg_0;
float3 v_1 = (0.0f).xxx;
float3 v_2 = frexp(v, v_1);
float3 v_3 = float3(sign(v));
v_1 = (v_3 * v_1);
frexp_result_vec3_f32 res = {v_2, int3(v_1)};
float3 v_3 = (float3(sign(v)) * v_2);
frexp_result_vec3_f32 res = {v_3, int3(v_1)};
}

void fragment_main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_ae4a66() {
vector<float16_t, 3> v = arg_0;
vector<float16_t, 3> v_1 = (float16_t(0.0h)).xxx;
vector<float16_t, 3> v_2 = frexp(v, v_1);
vector<float16_t, 3> v_3 = vector<float16_t, 3>(sign(v));
v_1 = (v_3 * v_1);
frexp_result_vec3_f16 res = {v_2, int3(v_1)};
vector<float16_t, 3> v_3 = (vector<float16_t, 3>(sign(v)) * v_2);
frexp_result_vec3_f16 res = {v_3, int3(v_1)};
}

void fragment_main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_eb2421() {
float2 v = arg_0;
float2 v_1 = (0.0f).xx;
float2 v_2 = frexp(v, v_1);
float2 v_3 = float2(sign(v));
v_1 = (v_3 * v_1);
frexp_result_vec2_f32 res = {v_2, int2(v_1)};
float2 v_3 = (float2(sign(v)) * v_2);
frexp_result_vec2_f32 res = {v_3, int2(v_1)};
}

void fragment_main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ void frexp_eb2421() {
float2 v = arg_0;
float2 v_1 = (0.0f).xx;
float2 v_2 = frexp(v, v_1);
float2 v_3 = float2(sign(v));
v_1 = (v_3 * v_1);
frexp_result_vec2_f32 res = {v_2, int2(v_1)};
float2 v_3 = (float2(sign(v)) * v_2);
frexp_result_vec2_f32 res = {v_3, int2(v_1)};
}

void fragment_main() {
Expand Down

0 comments on commit 9eb59d0

Please sign in to comment.