Skip to content

Commit 7df6e47

Browse files
jamienicoljimblandy
authored andcommitted
[naga wgsl-in] Do not eagerly concretize local const declarations of abstract types
Instead allow the const to be converted each time it is const evaluated as part of another expression. This allows an abstract const to be used as a different type depending on the context. As a result, abstract types may now find their way in to the IR, which we don't want. This occurs because the compact pass treats named expressions as used, mostly so that our snapshot tests are more useful, and therefore does not remove them. To prevent this, we avoid adding abstract-typed local consts to the named expressions list. This will have no functional effect on any shaders produced by the backends, but some unused local const declarations will no longer be present.
1 parent cc1e26a commit 7df6e47

14 files changed

+65
-132
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ By @Vecvec in [#6905](https://github.com/gfx-rs/wgpu/pull/6905), [#7086](https:/
198198
- Error if structs have two fields with the same name. By @SparkyPotato in [#7088](https://github.com/gfx-rs/wgpu/pull/7088).
199199
- Forward '--keep-coordinate-space' flag to GLSL backend in naga-cli. By @cloone8 in [#7206](https://github.com/gfx-rs/wgpu/pull/7206).
200200
- Allow template lists to have a trailing comma. By @KentSlaney in [#7142](https://github.com/gfx-rs/wgpu/pull/7142).
201-
- Allow WGSL const declarations to have abstract types. By @jamienicol in [#7055](https://github.com/gfx-rs/wgpu/pull/7055).
201+
- Allow WGSL const declarations to have abstract types. By @jamienicol in [#7055](https://github.com/gfx-rs/wgpu/pull/7055) and [#7222](https://github.com/gfx-rs/wgpu/pull/7222).
202202
203203
#### General
204204

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,21 +1579,25 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
15791579
c.ty.map(|ast| self.resolve_ast_type(ast, &mut ectx.as_const()))
15801580
.transpose()?;
15811581

1582-
let (_ty, init) = self.type_and_init(
1582+
let (ty, init) = self.type_and_init(
15831583
c.name,
15841584
Some(c.init),
15851585
explicit_ty,
1586-
AbstractRule::Concretize,
1586+
AbstractRule::Allow,
15871587
&mut ectx.as_const(),
15881588
)?;
15891589
let init = init.expect("Local const must have init");
15901590

15911591
block.extend(emitter.finish(&ctx.function.expressions));
15921592
ctx.local_table
15931593
.insert(c.handle, Declared::Const(Typed::Plain(init)));
1594-
ctx.named_expressions
1595-
.insert(init, (c.name.name.to_string(), c.name.span));
1596-
1594+
// Only add constants of non-abstract types to the named expressions
1595+
// to prevent abstract types ending up in the IR.
1596+
let is_abstract = ctx.module.types[ty].inner.is_abstract(&ctx.module.types);
1597+
if !is_abstract {
1598+
ctx.named_expressions
1599+
.insert(init, (c.name.name.to_string(), c.name.span));
1600+
}
15971601
return Ok(());
15981602
}
15991603
},

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ const one = 1;
2929
fn return_const_f32_const_ai() -> f32 {
3030
return one;
3131
}
32+
33+
fn return_vec2f32_const_ai() -> vec2<f32> {
34+
const vec_one = vec2(1);
35+
return vec_one;
36+
}

naga/tests/in/wgsl/const_assert.wgsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ fn foo() {
1313
const z = x + y - 2;
1414
const_assert z > 0; // valid in functions.
1515
const_assert(z > 0);
16+
17+
const_assert z == 1i;
18+
const_assert z > 0u;
19+
const_assert z < 2.0f;
1620
}

naga/tests/out/glsl/abstract-types-return.main.Compute.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ float return_const_f32_const_ai() {
3434
return 1.0;
3535
}
3636

37+
vec2 return_vec2f32_const_ai() {
38+
return vec2(1.0);
39+
}
40+
3741
void main() {
3842
return;
3943
}

naga/tests/out/hlsl/abstract-types-return.hlsl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ float return_const_f32_const_ai()
4040
return 1.0;
4141
}
4242

43+
float2 return_vec2f32_const_ai()
44+
{
45+
return (1.0).xx;
46+
}
47+
4348
[numthreads(1, 1, 1)]
4449
void main()
4550
{

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616
arguments: [],
1717
result: None,
1818
local_variables: [],
19-
expressions: [
20-
Literal(I32(1)),
21-
],
22-
named_expressions: {
23-
0: "z",
24-
},
19+
expressions: [],
20+
named_expressions: {},
2521
body: [
2622
Return(
2723
value: None,

naga/tests/out/ir/const_assert.ron

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616
arguments: [],
1717
result: None,
1818
local_variables: [],
19-
expressions: [
20-
Literal(I32(1)),
21-
],
22-
named_expressions: {
23-
0: "z",
24-
},
19+
expressions: [],
20+
named_expressions: {},
2521
body: [
2622
Return(
2723
value: None,

naga/tests/out/ir/local-const.compact.ron

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@
2121
width: 4,
2222
)),
2323
),
24-
(
25-
name: None,
26-
inner: Vector(
27-
size: Tri,
28-
scalar: (
29-
kind: Sint,
30-
width: 4,
31-
),
32-
),
33-
),
3424
],
3525
special_types: (
3626
ray_desc: None,
@@ -69,63 +59,26 @@
6959
result: None,
7060
local_variables: [],
7161
expressions: [
72-
Literal(I32(4)),
7362
Literal(I32(4)),
7463
Literal(U32(4)),
7564
Literal(F32(4.0)),
76-
Compose(
77-
ty: 3,
78-
components: [
79-
0,
80-
0,
81-
0,
82-
],
83-
),
84-
Literal(F32(2.0)),
85-
Literal(I32(4)),
8665
Constant(0),
8766
Constant(1),
8867
Constant(2),
89-
Literal(I32(4)),
90-
Literal(I32(4)),
91-
Literal(I32(4)),
92-
Compose(
93-
ty: 3,
94-
components: [
95-
10,
96-
11,
97-
12,
98-
],
99-
),
100-
Literal(F32(2.0)),
10168
],
10269
named_expressions: {
103-
0: "a",
104-
1: "b",
105-
2: "c",
106-
3: "d",
107-
4: "e",
108-
5: "f",
109-
6: "ag",
110-
7: "bg",
111-
8: "cg",
112-
9: "dg",
113-
13: "eg",
114-
14: "fg",
70+
0: "b",
71+
1: "c",
72+
2: "d",
73+
3: "bg",
74+
4: "cg",
75+
5: "dg",
11576
},
11677
body: [
117-
Emit((
118-
start: 4,
119-
end: 5,
120-
)),
12178
Emit((
12279
start: 0,
12380
end: 0,
12481
)),
125-
Emit((
126-
start: 13,
127-
end: 14,
128-
)),
12982
Return(
13083
value: None,
13184
),

naga/tests/out/ir/local-const.ron

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@
2121
width: 4,
2222
)),
2323
),
24-
(
25-
name: None,
26-
inner: Vector(
27-
size: Tri,
28-
scalar: (
29-
kind: Sint,
30-
width: 4,
31-
),
32-
),
33-
),
3424
],
3525
special_types: (
3626
ray_desc: None,
@@ -69,63 +59,26 @@
6959
result: None,
7060
local_variables: [],
7161
expressions: [
72-
Literal(I32(4)),
7362
Literal(I32(4)),
7463
Literal(U32(4)),
7564
Literal(F32(4.0)),
76-
Compose(
77-
ty: 3,
78-
components: [
79-
0,
80-
0,
81-
0,
82-
],
83-
),
84-
Literal(F32(2.0)),
85-
Literal(I32(4)),
8665
Constant(0),
8766
Constant(1),
8867
Constant(2),
89-
Literal(I32(4)),
90-
Literal(I32(4)),
91-
Literal(I32(4)),
92-
Compose(
93-
ty: 3,
94-
components: [
95-
10,
96-
11,
97-
12,
98-
],
99-
),
100-
Literal(F32(2.0)),
10168
],
10269
named_expressions: {
103-
0: "a",
104-
1: "b",
105-
2: "c",
106-
3: "d",
107-
4: "e",
108-
5: "f",
109-
6: "ag",
110-
7: "bg",
111-
8: "cg",
112-
9: "dg",
113-
13: "eg",
114-
14: "fg",
70+
0: "b",
71+
1: "c",
72+
2: "d",
73+
3: "bg",
74+
4: "cg",
75+
5: "dg",
11576
},
11677
body: [
117-
Emit((
118-
start: 4,
119-
end: 5,
120-
)),
12178
Emit((
12279
start: 0,
12380
end: 0,
12481
)),
125-
Emit((
126-
start: 13,
127-
end: 14,
128-
)),
12982
Return(
13083
value: None,
13184
),

0 commit comments

Comments
 (0)