Skip to content

Commit 2ad95b2

Browse files
jimblandyteoxoy
authored andcommitted
[naga wgsl-in] Allow override expressions as local var initializers.
Allow `LocalVariable::init` to be an override expression. Note that this is unrelated to WGSL compliance. The WGSL front end already accepts any sort of expression as an initializer for `LocalVariable`s, but initialization by an override expression was handled in the same way as initialization by a runtime expression, via an explicit `Store` statement. This commit merely lets us skip the `Store` when the initializer is an override expression, producing slightly cleaner output in some cases.
1 parent 7df0aa6 commit 2ad95b2

File tree

11 files changed

+72
-92
lines changed

11 files changed

+72
-92
lines changed

naga/src/back/pipeline_constants.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ fn process_function(
304304

305305
filter_emits_in_block(&mut function.body, &function.expressions);
306306

307+
// Update local expression initializers.
308+
for (_, local) in function.local_variables.iter_mut() {
309+
if let &mut Some(ref mut init) = &mut local.init {
310+
*init = adjusted_local_expressions[init.index()];
311+
}
312+
}
313+
307314
// We've changed the keys of `function.named_expression`, so we have to
308315
// rebuild it from scratch.
309316
let named_expressions = mem::take(&mut function.named_expressions);

naga/src/front/wgsl/lower/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
13171317
// expression, so its value depends on the
13181318
// state at the point of initialization.
13191319
if is_inside_loop
1320-
|| !ctx.local_expression_kind_tracker.is_const(init)
1320+
|| !ctx.local_expression_kind_tracker.is_const_or_override(init)
13211321
{
13221322
(None, Some(init))
13231323
} else {

naga/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ pub struct LocalVariable {
998998
///
999999
/// This handle refers to this `LocalVariable`'s function's
10001000
/// [`expressions`] arena, but it is required to be an evaluated
1001-
/// constant expression.
1001+
/// override expression.
10021002
///
10031003
/// [`expressions`]: Function::expressions
10041004
pub init: Option<Handle<Expression>>,

naga/src/valid/function.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pub enum LocalVariableError {
5454
InvalidType(Handle<crate::Type>),
5555
#[error("Initializer doesn't match the variable type")]
5656
InitializerType,
57-
#[error("Initializer is not const")]
58-
NonConstInitializer,
57+
#[error("Initializer is not a const or override expression")]
58+
NonConstOrOverrideInitializer,
5959
}
6060

6161
#[derive(Clone, Debug, thiserror::Error)]
@@ -945,8 +945,8 @@ impl super::Validator {
945945
return Err(LocalVariableError::InitializerType);
946946
}
947947

948-
if !local_expr_kind.is_const(init) {
949-
return Err(LocalVariableError::NonConstInitializer);
948+
if !local_expr_kind.is_const_or_override(init) {
949+
return Err(LocalVariableError::NonConstOrOverrideInitializer);
950950
}
951951
}
952952

naga/tests/out/analysis/overrides.info.ron

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@
5151
width: 4,
5252
))),
5353
),
54-
(
55-
uniformity: (
56-
non_uniform_result: Some(4),
57-
requirements: (""),
58-
),
59-
ref_count: 1,
60-
assignable_global: None,
61-
ty: Value(Pointer(
62-
base: 2,
63-
space: Function,
64-
)),
65-
),
6654
(
6755
uniformity: (
6856
non_uniform_result: None,
@@ -83,7 +71,7 @@
8371
),
8472
(
8573
uniformity: (
86-
non_uniform_result: Some(7),
74+
non_uniform_result: Some(6),
8775
requirements: (""),
8876
),
8977
ref_count: 1,
@@ -95,7 +83,7 @@
9583
),
9684
(
9785
uniformity: (
98-
non_uniform_result: Some(8),
86+
non_uniform_result: Some(7),
9987
requirements: (""),
10088
),
10189
ref_count: 1,
@@ -107,7 +95,7 @@
10795
),
10896
(
10997
uniformity: (
110-
non_uniform_result: Some(8),
98+
non_uniform_result: Some(7),
11199
requirements: (""),
112100
),
113101
ref_count: 1,
@@ -128,7 +116,7 @@
128116
),
129117
(
130118
uniformity: (
131-
non_uniform_result: Some(8),
119+
non_uniform_result: Some(7),
132120
requirements: (""),
133121
),
134122
ref_count: 1,
@@ -140,7 +128,7 @@
140128
),
141129
(
142130
uniformity: (
143-
non_uniform_result: Some(12),
131+
non_uniform_result: Some(11),
144132
requirements: (""),
145133
),
146134
ref_count: 1,

naga/tests/out/glsl/overrides.main.Compute.glsl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ float gain_x_10_ = 11.0;
1717

1818

1919
void main() {
20-
float t = 0.0;
20+
float t = 23.0;
2121
bool x = false;
2222
float gain_x_100_ = 0.0;
23-
t = 23.0;
2423
x = true;
25-
float _e10 = gain_x_10_;
26-
gain_x_100_ = (_e10 * 10.0);
24+
float _e9 = gain_x_10_;
25+
gain_x_100_ = (_e9 * 10.0);
2726
return;
2827
}
2928

naga/tests/out/hlsl/overrides.hlsl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ static float gain_x_10_ = 11.0;
1111
[numthreads(1, 1, 1)]
1212
void main()
1313
{
14-
float t = (float)0;
14+
float t = 23.0;
1515
bool x = (bool)0;
1616
float gain_x_100_ = (float)0;
1717

18-
t = 23.0;
1918
x = true;
20-
float _expr10 = gain_x_10_;
21-
gain_x_100_ = (_expr10 * 10.0);
19+
float _expr9 = gain_x_10_;
20+
gain_x_100_ = (_expr9 * 10.0);
2221
return;
2322
}

naga/tests/out/ir/overrides.compact.ron

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
(
110110
name: Some("t"),
111111
ty: 2,
112-
init: None,
112+
init: Some(3),
113113
),
114114
(
115115
name: Some("x"),
@@ -130,56 +130,51 @@
130130
left: 1,
131131
right: 2,
132132
),
133-
LocalVariable(1),
134133
Override(1),
135134
Unary(
136135
op: LogicalNot,
137-
expr: 5,
136+
expr: 4,
138137
),
139138
LocalVariable(2),
140139
GlobalVariable(1),
141140
Load(
142-
pointer: 8,
141+
pointer: 7,
143142
),
144143
Literal(F32(10.0)),
145144
Binary(
146145
op: Multiply,
147-
left: 9,
148-
right: 10,
146+
left: 8,
147+
right: 9,
149148
),
150149
LocalVariable(3),
151150
],
152151
named_expressions: {
153-
6: "a",
152+
5: "a",
154153
},
155154
body: [
156155
Emit((
157156
start: 2,
158157
end: 3,
159158
)),
160-
Store(
161-
pointer: 4,
162-
value: 3,
163-
),
164159
Emit((
165-
start: 5,
166-
end: 6,
160+
start: 4,
161+
end: 5,
167162
)),
168163
Store(
169-
pointer: 7,
170-
value: 6,
164+
pointer: 6,
165+
value: 5,
171166
),
172167
Emit((
173-
start: 8,
174-
end: 9,
168+
start: 7,
169+
end: 8,
175170
)),
176171
Emit((
177-
start: 10,
178-
end: 11,
172+
start: 9,
173+
end: 10,
179174
)),
180175
Store(
181-
pointer: 12,
182-
value: 11,
176+
pointer: 11,
177+
value: 10,
183178
),
184179
Return(
185180
value: None,

naga/tests/out/ir/overrides.ron

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
(
110110
name: Some("t"),
111111
ty: 2,
112-
init: None,
112+
init: Some(3),
113113
),
114114
(
115115
name: Some("x"),
@@ -130,56 +130,51 @@
130130
left: 1,
131131
right: 2,
132132
),
133-
LocalVariable(1),
134133
Override(1),
135134
Unary(
136135
op: LogicalNot,
137-
expr: 5,
136+
expr: 4,
138137
),
139138
LocalVariable(2),
140139
GlobalVariable(1),
141140
Load(
142-
pointer: 8,
141+
pointer: 7,
143142
),
144143
Literal(F32(10.0)),
145144
Binary(
146145
op: Multiply,
147-
left: 9,
148-
right: 10,
146+
left: 8,
147+
right: 9,
149148
),
150149
LocalVariable(3),
151150
],
152151
named_expressions: {
153-
6: "a",
152+
5: "a",
154153
},
155154
body: [
156155
Emit((
157156
start: 2,
158157
end: 3,
159158
)),
160-
Store(
161-
pointer: 4,
162-
value: 3,
163-
),
164159
Emit((
165-
start: 5,
166-
end: 6,
160+
start: 4,
161+
end: 5,
167162
)),
168163
Store(
169-
pointer: 7,
170-
value: 6,
164+
pointer: 6,
165+
value: 5,
171166
),
172167
Emit((
173-
start: 8,
174-
end: 9,
168+
start: 7,
169+
end: 8,
175170
)),
176171
Emit((
177-
start: 10,
178-
end: 11,
172+
start: 9,
173+
end: 10,
179174
)),
180175
Store(
181-
pointer: 12,
182-
value: 11,
176+
pointer: 11,
177+
value: 10,
183178
),
184179
Return(
185180
value: None,

naga/tests/out/msl/overrides.msl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ constant float inferred_f32_ = 2.718;
1515
kernel void main_(
1616
) {
1717
float gain_x_10_ = 11.0;
18-
float t = {};
18+
float t = 23.0;
1919
bool x = {};
2020
float gain_x_100_ = {};
21-
t = 23.0;
2221
x = true;
23-
float _e10 = gain_x_10_;
24-
gain_x_100_ = _e10 * 10.0;
22+
float _e9 = gain_x_10_;
23+
gain_x_100_ = _e9 * 10.0;
2524
return;
2625
}

0 commit comments

Comments
 (0)