Skip to content

Commit 1509512

Browse files
authored
[naga] Handle bitshifts for abstract lhs operands during const evaluation (#6865)
1 parent a84b6ca commit 1509512

File tree

5 files changed

+144
-101
lines changed

5 files changed

+144
-101
lines changed

naga/src/proc/constant_evaluator.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,20 @@ impl<'a> ConstantEvaluator<'a> {
18811881
BinaryOperator::Modulo => a % b,
18821882
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs),
18831883
}),
1884+
(Literal::AbstractInt(a), Literal::U32(b)) => {
1885+
Literal::AbstractInt(match op {
1886+
BinaryOperator::ShiftLeft => {
1887+
if (if a.is_negative() { !a } else { a }).leading_zeros() <= b {
1888+
return Err(ConstantEvaluatorError::Overflow(
1889+
"<<".to_string(),
1890+
));
1891+
}
1892+
a.checked_shl(b).unwrap_or(0)
1893+
}
1894+
BinaryOperator::ShiftRight => a.checked_shr(b).unwrap_or(0),
1895+
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs),
1896+
})
1897+
}
18841898
(Literal::AbstractInt(a), Literal::AbstractInt(b)) => {
18851899
Literal::AbstractInt(match op {
18861900
BinaryOperator::Add => a.checked_add(b).ok_or_else(|| {

naga/tests/in/abstract-types-operators.wgsl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ const bitflip_uai: u32 = ~0xffffffff & (0x100000000 - 1);
2424
const least_i32: i32 = -2147483648;
2525
const least_f32: f32 = -3.40282347e+38;
2626

27+
const shl_iaiai: i32 = 1 << 2;
28+
const shl_iai_u: i32 = 1 << 2u;
29+
const shl_uaiai: u32 = 1 << 2;
30+
const shl_uai_u: u32 = 1 << 2u;
31+
32+
const shr_iaiai: i32 = 1 >> 2;
33+
const shr_iai_u: i32 = 1 >> 2u;
34+
const shr_uaiai: u32 = 1 >> 2;
35+
const shr_uai_u: u32 = 1 >> 2u;
36+
2737
fn runtime_values() {
2838
var f: f32 = 42;
2939
var i: i32 = 43;

naga/tests/out/msl/abstract-types-operators.msl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ constant uint bitflip_u_u = 0u;
2828
constant uint bitflip_uai = 0u;
2929
constant int least_i32_ = -2147483648;
3030
constant float least_f32_ = -340282350000000000000000000000000000000.0;
31+
constant int shl_iaiai = 4;
32+
constant int shl_iai_u = 4;
33+
constant uint shl_uaiai = 4u;
34+
constant uint shl_uai_u = 4u;
35+
constant int shr_iaiai = 0;
36+
constant int shr_iai_u = 0;
37+
constant uint shr_uaiai = 0u;
38+
constant uint shr_uai_u = 0u;
3139
constant int wgpu_4492_ = -2147483648;
3240
constant int wgpu_4492_2_ = -2147483648;
3341

Lines changed: 104 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; SPIR-V
22
; Version: 1.1
33
; Generator: rspirv
4-
; Bound: 100
4+
; Bound: 103
55
OpCapability Shader
66
OpCapability Linkage
77
%1 = OpExtInstImport "GLSL.std.450"
@@ -19,109 +19,112 @@ OpDecorate %6 ArrayStride 4
1919
%11 = OpConstant %5 0
2020
%12 = OpConstant %4 -2147483648
2121
%13 = OpConstant %3 -3.4028235e38
22-
%15 = OpTypePointer Workgroup %6
23-
%14 = OpVariable %15 Workgroup
24-
%18 = OpTypeFunction %2
25-
%19 = OpConstant %3 42.0
26-
%20 = OpConstant %4 43
27-
%21 = OpConstant %5 44
28-
%22 = OpConstant %3 1.0
29-
%23 = OpConstant %3 2.0
30-
%24 = OpConstant %4 1
31-
%25 = OpConstant %4 2
32-
%26 = OpConstant %5 1
33-
%27 = OpConstant %5 2
34-
%29 = OpTypePointer Function %3
35-
%31 = OpTypePointer Function %4
36-
%33 = OpTypePointer Function %5
37-
%37 = OpConstantNull %3
38-
%41 = OpConstantNull %3
39-
%43 = OpConstantNull %3
40-
%45 = OpConstantNull %3
41-
%47 = OpConstantNull %3
42-
%50 = OpConstantNull %4
43-
%52 = OpConstantNull %4
44-
%54 = OpConstantNull %4
45-
%57 = OpConstantNull %5
46-
%59 = OpConstantNull %5
47-
%61 = OpConstantNull %5
48-
%90 = OpConstant %3 5.0
49-
%91 = OpConstant %3 7.0
50-
%97 = OpTypePointer Workgroup %5
51-
%17 = OpFunction %2 None %18
52-
%16 = OpLabel
53-
%60 = OpVariable %33 Function %61
54-
%55 = OpVariable %33 Function %10
55-
%49 = OpVariable %31 Function %50
56-
%44 = OpVariable %29 Function %45
57-
%39 = OpVariable %29 Function %8
58-
%35 = OpVariable %29 Function %8
59-
%30 = OpVariable %31 Function %20
60-
%58 = OpVariable %33 Function %59
61-
%53 = OpVariable %31 Function %54
62-
%48 = OpVariable %31 Function %9
63-
%42 = OpVariable %29 Function %43
64-
%38 = OpVariable %29 Function %8
65-
%34 = OpVariable %29 Function %8
66-
%28 = OpVariable %29 Function %19
67-
%56 = OpVariable %33 Function %57
68-
%51 = OpVariable %31 Function %52
69-
%46 = OpVariable %29 Function %47
70-
%40 = OpVariable %29 Function %41
71-
%36 = OpVariable %29 Function %37
72-
%32 = OpVariable %33 Function %21
73-
OpBranch %62
74-
%62 = OpLabel
75-
%63 = OpLoad %3 %28
76-
%64 = OpFAdd %3 %22 %63
77-
OpStore %36 %64
78-
%65 = OpLoad %3 %28
79-
%66 = OpFAdd %3 %22 %65
80-
OpStore %40 %66
81-
%67 = OpLoad %3 %28
82-
%68 = OpFAdd %3 %67 %23
83-
OpStore %42 %68
84-
%69 = OpLoad %3 %28
85-
%70 = OpFAdd %3 %69 %23
86-
OpStore %44 %70
87-
%71 = OpLoad %3 %28
88-
%72 = OpLoad %3 %28
89-
%73 = OpFAdd %3 %71 %72
90-
OpStore %46 %73
91-
%74 = OpLoad %4 %30
92-
%75 = OpIAdd %4 %24 %74
93-
OpStore %49 %75
94-
%76 = OpLoad %4 %30
95-
%77 = OpIAdd %4 %76 %25
96-
OpStore %51 %77
97-
%78 = OpLoad %4 %30
98-
%79 = OpLoad %4 %30
99-
%80 = OpIAdd %4 %78 %79
100-
OpStore %53 %80
101-
%81 = OpLoad %5 %32
102-
%82 = OpIAdd %5 %26 %81
103-
OpStore %56 %82
104-
%83 = OpLoad %5 %32
105-
%84 = OpIAdd %5 %83 %27
106-
OpStore %58 %84
107-
%85 = OpLoad %5 %32
108-
%86 = OpLoad %5 %32
109-
%87 = OpIAdd %5 %85 %86
110-
OpStore %60 %87
22+
%14 = OpConstant %4 4
23+
%15 = OpConstant %5 4
24+
%16 = OpConstant %4 0
25+
%18 = OpTypePointer Workgroup %6
26+
%17 = OpVariable %18 Workgroup
27+
%21 = OpTypeFunction %2
28+
%22 = OpConstant %3 42.0
29+
%23 = OpConstant %4 43
30+
%24 = OpConstant %5 44
31+
%25 = OpConstant %3 1.0
32+
%26 = OpConstant %3 2.0
33+
%27 = OpConstant %4 1
34+
%28 = OpConstant %4 2
35+
%29 = OpConstant %5 1
36+
%30 = OpConstant %5 2
37+
%32 = OpTypePointer Function %3
38+
%34 = OpTypePointer Function %4
39+
%36 = OpTypePointer Function %5
40+
%40 = OpConstantNull %3
41+
%44 = OpConstantNull %3
42+
%46 = OpConstantNull %3
43+
%48 = OpConstantNull %3
44+
%50 = OpConstantNull %3
45+
%53 = OpConstantNull %4
46+
%55 = OpConstantNull %4
47+
%57 = OpConstantNull %4
48+
%60 = OpConstantNull %5
49+
%62 = OpConstantNull %5
50+
%64 = OpConstantNull %5
51+
%93 = OpConstant %3 5.0
52+
%94 = OpConstant %3 7.0
53+
%100 = OpTypePointer Workgroup %5
54+
%20 = OpFunction %2 None %21
55+
%19 = OpLabel
56+
%63 = OpVariable %36 Function %64
57+
%58 = OpVariable %36 Function %10
58+
%52 = OpVariable %34 Function %53
59+
%47 = OpVariable %32 Function %48
60+
%42 = OpVariable %32 Function %8
61+
%38 = OpVariable %32 Function %8
62+
%33 = OpVariable %34 Function %23
63+
%61 = OpVariable %36 Function %62
64+
%56 = OpVariable %34 Function %57
65+
%51 = OpVariable %34 Function %9
66+
%45 = OpVariable %32 Function %46
67+
%41 = OpVariable %32 Function %8
68+
%37 = OpVariable %32 Function %8
69+
%31 = OpVariable %32 Function %22
70+
%59 = OpVariable %36 Function %60
71+
%54 = OpVariable %34 Function %55
72+
%49 = OpVariable %32 Function %50
73+
%43 = OpVariable %32 Function %44
74+
%39 = OpVariable %32 Function %40
75+
%35 = OpVariable %36 Function %24
76+
OpBranch %65
77+
%65 = OpLabel
78+
%66 = OpLoad %3 %31
79+
%67 = OpFAdd %3 %25 %66
80+
OpStore %39 %67
81+
%68 = OpLoad %3 %31
82+
%69 = OpFAdd %3 %25 %68
83+
OpStore %43 %69
84+
%70 = OpLoad %3 %31
85+
%71 = OpFAdd %3 %70 %26
86+
OpStore %45 %71
87+
%72 = OpLoad %3 %31
88+
%73 = OpFAdd %3 %72 %26
89+
OpStore %47 %73
90+
%74 = OpLoad %3 %31
91+
%75 = OpLoad %3 %31
92+
%76 = OpFAdd %3 %74 %75
93+
OpStore %49 %76
94+
%77 = OpLoad %4 %33
95+
%78 = OpIAdd %4 %27 %77
96+
OpStore %52 %78
97+
%79 = OpLoad %4 %33
98+
%80 = OpIAdd %4 %79 %28
99+
OpStore %54 %80
100+
%81 = OpLoad %4 %33
101+
%82 = OpLoad %4 %33
102+
%83 = OpIAdd %4 %81 %82
103+
OpStore %56 %83
104+
%84 = OpLoad %5 %35
105+
%85 = OpIAdd %5 %29 %84
106+
OpStore %59 %85
107+
%86 = OpLoad %5 %35
108+
%87 = OpIAdd %5 %86 %30
109+
OpStore %61 %87
110+
%88 = OpLoad %5 %35
111+
%89 = OpLoad %5 %35
112+
%90 = OpIAdd %5 %88 %89
113+
OpStore %63 %90
111114
OpReturn
112115
OpFunctionEnd
113-
%89 = OpFunction %2 None %18
114-
%88 = OpLabel
115-
OpBranch %92
116-
%92 = OpLabel
117-
OpReturn
118-
OpFunctionEnd
119-
%94 = OpFunction %2 None %18
120-
%93 = OpLabel
116+
%92 = OpFunction %2 None %21
117+
%91 = OpLabel
121118
OpBranch %95
122119
%95 = OpLabel
123-
%96 = OpISub %4 %24 %24
124-
%98 = OpAccessChain %97 %14 %96
125-
%99 = OpLoad %5 %98
120+
OpReturn
121+
OpFunctionEnd
122+
%97 = OpFunction %2 None %21
123+
%96 = OpLabel
124+
OpBranch %98
125+
%98 = OpLabel
126+
%99 = OpISub %4 %27 %27
127+
%101 = OpAccessChain %100 %17 %99
128+
%102 = OpLoad %5 %101
126129
OpReturn
127130
OpFunctionEnd

naga/tests/out/wgsl/abstract-types-operators.wgsl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ const bitflip_u_u: u32 = 0u;
1919
const bitflip_uai: u32 = 0u;
2020
const least_i32_: i32 = i32(-2147483648);
2121
const least_f32_: f32 = -340282350000000000000000000000000000000f;
22+
const shl_iaiai: i32 = 4i;
23+
const shl_iai_u: i32 = 4i;
24+
const shl_uaiai: u32 = 4u;
25+
const shl_uai_u: u32 = 4u;
26+
const shr_iaiai: i32 = 0i;
27+
const shr_iai_u: i32 = 0i;
28+
const shr_uaiai: u32 = 0u;
29+
const shr_uai_u: u32 = 0u;
2230
const wgpu_4492_: i32 = i32(-2147483648);
2331
const wgpu_4492_2_: i32 = i32(-2147483648);
2432

0 commit comments

Comments
 (0)