Skip to content

Commit 10c04c9

Browse files
jamienicolVecvec
authored andcommitted
[naga hlsl-out] Emit most-negative integer value literals as 2nd-most-negative value minus one
As we did for MSL in gfx-rs#7437. eg `-2147483648` becomes `-2147483647 - 1`. Neither FXC nor DXC currently have any issues parsing the most negative value literals. However, we have been informed this is not guaranteed to always be the case, so are making this change as a precaution.
1 parent f2f1a84 commit 10c04c9

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

naga/src/back/hlsl/writer.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,11 +2620,23 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
26202620
crate::Literal::F32(value) => write!(self.out, "{value:?}")?,
26212621
crate::Literal::F16(value) => write!(self.out, "{value:?}h")?,
26222622
crate::Literal::U32(value) => write!(self.out, "{value}u")?,
2623+
// `-2147483648` is parsed by some compilers as unary negation of
2624+
// positive 2147483648, which is too large for an int, causing
2625+
// issues for some compilers. Neither DXC nor FXC appear to have
2626+
// this problem, but this is not specified and could change. We
2627+
// therefore use `-2147483647 - 1` as a precaution.
2628+
crate::Literal::I32(value) if value == i32::MIN => {
2629+
write!(self.out, "int({} - 1)", value + 1)?
2630+
}
26232631
// HLSL has no suffix for explicit i32 literals, but not using any suffix
26242632
// makes the type ambiguous which prevents overload resolution from
26252633
// working. So we explicitly use the int() constructor syntax.
26262634
crate::Literal::I32(value) => write!(self.out, "int({value})")?,
26272635
crate::Literal::U64(value) => write!(self.out, "{value}uL")?,
2636+
// I64 version of the minimum I32 value issue described above.
2637+
crate::Literal::I64(value) if value == i64::MIN => {
2638+
write!(self.out, "({}L - 1L)", value + 1)?;
2639+
}
26282640
crate::Literal::I64(value) => write!(self.out, "{value}L")?,
26292641
crate::Literal::Bool(value) => write!(self.out, "{value}")?,
26302642
crate::Literal::AbstractInt(_) | crate::Literal::AbstractFloat(_) => {

naga/tests/out/hlsl/wgsl-conversion-float-to-int.hlsl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ void test_const_eval()
1515
int64_t max_f16_to_i64_ = 65504L;
1616
uint64_t min_f16_to_u64_ = 0uL;
1717
uint64_t max_f16_to_u64_ = 65504uL;
18-
int min_f32_to_i32_ = int(-2147483648);
18+
int min_f32_to_i32_ = int(-2147483647 - 1);
1919
int max_f32_to_i32_ = int(2147483520);
2020
uint min_f32_to_u32_ = 0u;
2121
uint max_f32_to_u32_ = 4294967040u;
22-
int64_t min_f32_to_i64_ = -9223372036854775808L;
22+
int64_t min_f32_to_i64_ = (-9223372036854775807L - 1L);
2323
int64_t max_f32_to_i64_ = 9223371487098961920L;
2424
uint64_t min_f32_to_u64_ = 0uL;
2525
uint64_t max_f32_to_u64_ = 18446742974197923840uL;
26-
int64_t min_f64_to_i64_ = -9223372036854775808L;
26+
int64_t min_f64_to_i64_ = (-9223372036854775807L - 1L);
2727
int64_t max_f64_to_i64_ = 9223372036854774784L;
2828
uint64_t min_f64_to_u64_ = 0uL;
2929
uint64_t max_f64_to_u64_ = 18446744073709549568uL;
30-
int min_abstract_float_to_i32_ = int(-2147483648);
30+
int min_abstract_float_to_i32_ = int(-2147483647 - 1);
3131
int max_abstract_float_to_i32_ = int(2147483647);
3232
uint min_abstract_float_to_u32_ = 0u;
3333
uint max_abstract_float_to_u32_ = 4294967295u;
34-
int64_t min_abstract_float_to_i64_ = -9223372036854775808L;
34+
int64_t min_abstract_float_to_i64_ = (-9223372036854775807L - 1L);
3535
int64_t max_abstract_float_to_i64_ = 9223372036854774784L;
3636
uint64_t min_abstract_float_to_u64_ = 0uL;
3737
uint64_t max_abstract_float_to_u64_ = 18446744073709549568uL;

naga/tests/out/hlsl/wgsl-int64.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ int64_t int64_function(int64_t x)
100100
int64_t _e74 = val;
101101
val = (_e74 + _e71.w);
102102
int64_t _e77 = val;
103-
val = (_e77 + -9223372036854775808L);
103+
val = (_e77 + (-9223372036854775807L - 1L));
104104
int64_t _e83 = input_uniform.val_i64_;
105105
int64_t _e86 = input_storage.Load<int64_t>(128);
106106
output.Store(128, (_e83 + _e86));

naga/tests/out/hlsl/wgsl-operators.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void arithmetic()
197197
float4 mul_vector1_ = mul(ZeroValuefloat4x3(), (2.0).xxx);
198198
float3x3 mul_ = mul(ZeroValuefloat3x4(), ZeroValuefloat4x3());
199199
int _e175 = prevent_const_eval;
200-
wgpu_7437_ = asint(asuint(_e175) + asuint(int(-2147483648)));
200+
wgpu_7437_ = asint(asuint(_e175) + asuint(int(-2147483647 - 1)));
201201
return;
202202
}
203203

0 commit comments

Comments
 (0)