Skip to content

Commit

Permalink
don't treat let declarations as const declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Sep 29, 2023
1 parent e77c831 commit 06add21
Show file tree
Hide file tree
Showing 22 changed files with 779 additions and 781 deletions.
22 changes: 17 additions & 5 deletions src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,16 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {

fn const_access(&self, handle: Handle<crate::Expression>) -> Option<u32> {
match self.expr_type {
ExpressionContextType::Runtime(ref ctx) => self
.module
.to_ctx()
.eval_expr_to_u32_from(handle, ctx.naga_expressions)
.ok(),
ExpressionContextType::Runtime(ref ctx) => {
if !ctx.expression_constness.is_const(handle) {
return None;
}

self.module
.to_ctx()
.eval_expr_to_u32_from(handle, ctx.naga_expressions)
.ok()
}
ExpressionContextType::Constant => self.module.to_ctx().eval_expr_to_u32(handle).ok(),
}
}
Expand Down Expand Up @@ -404,6 +409,12 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
) -> Result<crate::SwizzleComponent, Error<'source>> {
match self.expr_type {
ExpressionContextType::Runtime(ref rctx) => {
if !rctx.expression_constness.is_const(expr) {
return Err(Error::ExpectedConstExprConcreteIntegerScalar(
component_span,
));
}

let index = self
.module
.to_ctx()
Expand Down Expand Up @@ -1073,6 +1084,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
emitter.start(ctx.naga_expressions);

let value = self.expression(l.init, ctx.as_expression(block, &mut emitter))?;
ctx.expression_constness.force_non_const(value);

let explicit_ty =
l.ty.map(|ty| self.resolve_ast_type(ty, ctx.as_global()))
Expand Down
5 changes: 5 additions & 0 deletions src/proc/constant_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ impl ExpressionConstnessTracker {
}
}

/// Forces the the expression to not be const
pub fn force_non_const(&mut self, value: Handle<Expression>) {
self.inner.remove(value.index());
}

fn insert(&mut self, value: Handle<Expression>) {
self.inner.insert(value.index());
}
Expand Down
2 changes: 1 addition & 1 deletion src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ impl GlobalCtx<'_> {
self.eval_expr_to_literal_from(handle, self.const_expressions)
}

pub(crate) fn eval_expr_to_literal_from(
fn eval_expr_to_literal_from(
&self,
handle: crate::Handle<crate::Expression>,
arena: &crate::Arena<crate::Expression>,
Expand Down
8 changes: 2 additions & 6 deletions tests/in/const-exprs.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ fn main() {

// Swizzle the value of nested Compose expressions.
fn swizzle_of_compose() {
let a = vec2(1, 2);
let b = vec2(3, 4);
out = vec4(a, b).wzyx; // should assign vec4(4, 3, 2, 1);
out = vec4(vec2(1, 2), vec2(3, 4)).wzyx; // should assign vec4(4, 3, 2, 1);
}

// Index the value of nested Compose expressions.
fn index_of_compose() {
let a = vec2(1, 2);
let b = vec2(3, 4);
out2 += vec4(a, b)[1]; // should assign 2
out2 += vec4(vec2(1, 2), vec2(3, 4))[1]; // should assign 2
}

// Index the value of Compose expressions nested three deep
Expand Down
2 changes: 1 addition & 1 deletion tests/out/analysis/access.info.ron
Original file line number Diff line number Diff line change
Expand Up @@ -2774,7 +2774,7 @@
non_uniform_result: None,
requirements: (""),
),
ref_count: 0,
ref_count: 1,
assignable_global: None,
ty: Value(Scalar(
kind: Uint,
Expand Down
2 changes: 1 addition & 1 deletion tests/out/glsl/access.foo_vert.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void main() {
test_matrix_within_array_within_struct_accesses();
mat4x3 _matrix = _group_0_binding_0_vs._matrix;
uvec2 arr_1[2] = _group_0_binding_0_vs.arr;
float b = _group_0_binding_0_vs._matrix[3][0];
float b = _group_0_binding_0_vs._matrix[3u][0];
int a_1 = _group_0_binding_0_vs.data[(uint(_group_0_binding_0_vs.data.length()) - 2u)].value;
ivec2 c = _group_0_binding_2_vs;
float _e33 = read_from_private(foo);
Expand Down
4 changes: 0 additions & 4 deletions tests/out/glsl/const-exprs.main.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ layout(std430) buffer type_1_block_1Compute { int _group_0_binding_1_cs; };


void swizzle_of_compose() {
ivec2 a = ivec2(1, 2);
ivec2 b = ivec2(3, 4);
_group_0_binding_0_cs = ivec4(4, 3, 2, 1);
return;
}

void index_of_compose() {
ivec2 a_1 = ivec2(1, 2);
ivec2 b_1 = ivec2(3, 4);
int _e8 = _group_0_binding_1_cs;
_group_0_binding_1_cs = (_e8 + 2);
return;
Expand Down
136 changes: 68 additions & 68 deletions tests/out/glsl/image.texture_sample.Fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,74 +18,74 @@ void main() {
vec4 a = vec4(0.0);
vec2 tc = vec2(0.5);
vec3 tc3_ = vec3(0.5);
vec4 _e8 = texture(_group_0_binding_0_fs, vec2(0.5, 0.0));
vec4 _e9 = a;
a = (_e9 + _e8);
vec4 _e13 = texture(_group_0_binding_1_fs, vec2(tc));
vec4 _e14 = a;
a = (_e14 + _e13);
vec4 _e18 = textureOffset(_group_0_binding_1_fs, vec2(tc), ivec2(3, 1));
vec4 _e19 = a;
a = (_e19 + _e18);
vec4 _e23 = textureLod(_group_0_binding_1_fs, vec2(tc), 2.3);
vec4 _e24 = a;
a = (_e24 + _e23);
vec4 _e28 = textureLodOffset(_group_0_binding_1_fs, vec2(tc), 2.3, ivec2(3, 1));
vec4 _e29 = a;
a = (_e29 + _e28);
vec4 _e34 = textureOffset(_group_0_binding_1_fs, vec2(tc), ivec2(3, 1), 2.0);
vec4 _e35 = a;
a = (_e35 + _e34);
vec4 _e40 = texture(_group_0_binding_4_fs, vec3(tc, 0u));
vec4 _e41 = a;
a = (_e41 + _e40);
vec4 _e46 = textureOffset(_group_0_binding_4_fs, vec3(tc, 0u), ivec2(3, 1));
vec4 _e47 = a;
a = (_e47 + _e46);
vec4 _e52 = textureLod(_group_0_binding_4_fs, vec3(tc, 0u), 2.3);
vec4 _e53 = a;
a = (_e53 + _e52);
vec4 _e58 = textureLodOffset(_group_0_binding_4_fs, vec3(tc, 0u), 2.3, ivec2(3, 1));
vec4 _e59 = a;
a = (_e59 + _e58);
vec4 _e65 = textureOffset(_group_0_binding_4_fs, vec3(tc, 0u), ivec2(3, 1), 2.0);
vec4 _e66 = a;
a = (_e66 + _e65);
vec4 _e71 = texture(_group_0_binding_4_fs, vec3(tc, 0));
vec4 _e72 = a;
a = (_e72 + _e71);
vec4 _e77 = textureOffset(_group_0_binding_4_fs, vec3(tc, 0), ivec2(3, 1));
vec4 _e78 = a;
a = (_e78 + _e77);
vec4 _e83 = textureLod(_group_0_binding_4_fs, vec3(tc, 0), 2.3);
vec4 _e84 = a;
a = (_e84 + _e83);
vec4 _e89 = textureLodOffset(_group_0_binding_4_fs, vec3(tc, 0), 2.3, ivec2(3, 1));
vec4 _e90 = a;
a = (_e90 + _e89);
vec4 _e96 = textureOffset(_group_0_binding_4_fs, vec3(tc, 0), ivec2(3, 1), 2.0);
vec4 _e97 = a;
a = (_e97 + _e96);
vec4 _e102 = texture(_group_0_binding_6_fs, vec4(tc3_, 0u));
vec4 _e103 = a;
a = (_e103 + _e102);
vec4 _e108 = textureLod(_group_0_binding_6_fs, vec4(tc3_, 0u), 2.3);
vec4 _e109 = a;
a = (_e109 + _e108);
vec4 _e115 = texture(_group_0_binding_6_fs, vec4(tc3_, 0u), 2.0);
vec4 _e116 = a;
a = (_e116 + _e115);
vec4 _e121 = texture(_group_0_binding_6_fs, vec4(tc3_, 0));
vec4 _e122 = a;
a = (_e122 + _e121);
vec4 _e127 = textureLod(_group_0_binding_6_fs, vec4(tc3_, 0), 2.3);
vec4 _e128 = a;
a = (_e128 + _e127);
vec4 _e134 = texture(_group_0_binding_6_fs, vec4(tc3_, 0), 2.0);
vec4 _e135 = a;
a = (_e135 + _e134);
vec4 _e137 = a;
_fs2p_location0 = _e137;
vec4 _e9 = texture(_group_0_binding_0_fs, vec2(tc.x, 0.0));
vec4 _e10 = a;
a = (_e10 + _e9);
vec4 _e14 = texture(_group_0_binding_1_fs, vec2(tc));
vec4 _e15 = a;
a = (_e15 + _e14);
vec4 _e19 = textureOffset(_group_0_binding_1_fs, vec2(tc), ivec2(3, 1));
vec4 _e20 = a;
a = (_e20 + _e19);
vec4 _e24 = textureLod(_group_0_binding_1_fs, vec2(tc), 2.3);
vec4 _e25 = a;
a = (_e25 + _e24);
vec4 _e29 = textureLodOffset(_group_0_binding_1_fs, vec2(tc), 2.3, ivec2(3, 1));
vec4 _e30 = a;
a = (_e30 + _e29);
vec4 _e35 = textureOffset(_group_0_binding_1_fs, vec2(tc), ivec2(3, 1), 2.0);
vec4 _e36 = a;
a = (_e36 + _e35);
vec4 _e41 = texture(_group_0_binding_4_fs, vec3(tc, 0u));
vec4 _e42 = a;
a = (_e42 + _e41);
vec4 _e47 = textureOffset(_group_0_binding_4_fs, vec3(tc, 0u), ivec2(3, 1));
vec4 _e48 = a;
a = (_e48 + _e47);
vec4 _e53 = textureLod(_group_0_binding_4_fs, vec3(tc, 0u), 2.3);
vec4 _e54 = a;
a = (_e54 + _e53);
vec4 _e59 = textureLodOffset(_group_0_binding_4_fs, vec3(tc, 0u), 2.3, ivec2(3, 1));
vec4 _e60 = a;
a = (_e60 + _e59);
vec4 _e66 = textureOffset(_group_0_binding_4_fs, vec3(tc, 0u), ivec2(3, 1), 2.0);
vec4 _e67 = a;
a = (_e67 + _e66);
vec4 _e72 = texture(_group_0_binding_4_fs, vec3(tc, 0));
vec4 _e73 = a;
a = (_e73 + _e72);
vec4 _e78 = textureOffset(_group_0_binding_4_fs, vec3(tc, 0), ivec2(3, 1));
vec4 _e79 = a;
a = (_e79 + _e78);
vec4 _e84 = textureLod(_group_0_binding_4_fs, vec3(tc, 0), 2.3);
vec4 _e85 = a;
a = (_e85 + _e84);
vec4 _e90 = textureLodOffset(_group_0_binding_4_fs, vec3(tc, 0), 2.3, ivec2(3, 1));
vec4 _e91 = a;
a = (_e91 + _e90);
vec4 _e97 = textureOffset(_group_0_binding_4_fs, vec3(tc, 0), ivec2(3, 1), 2.0);
vec4 _e98 = a;
a = (_e98 + _e97);
vec4 _e103 = texture(_group_0_binding_6_fs, vec4(tc3_, 0u));
vec4 _e104 = a;
a = (_e104 + _e103);
vec4 _e109 = textureLod(_group_0_binding_6_fs, vec4(tc3_, 0u), 2.3);
vec4 _e110 = a;
a = (_e110 + _e109);
vec4 _e116 = texture(_group_0_binding_6_fs, vec4(tc3_, 0u), 2.0);
vec4 _e117 = a;
a = (_e117 + _e116);
vec4 _e122 = texture(_group_0_binding_6_fs, vec4(tc3_, 0));
vec4 _e123 = a;
a = (_e123 + _e122);
vec4 _e128 = textureLod(_group_0_binding_6_fs, vec4(tc3_, 0), 2.3);
vec4 _e129 = a;
a = (_e129 + _e128);
vec4 _e135 = texture(_group_0_binding_6_fs, vec4(tc3_, 0), 2.0);
vec4 _e136 = a;
a = (_e136 + _e135);
vec4 _e138 = a;
_fs2p_location0 = _e138;
return;
}

2 changes: 1 addition & 1 deletion tests/out/hlsl/access.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ float4 foo_vert(uint vi : SV_VertexID) : SV_Position
test_matrix_within_array_within_struct_accesses();
float4x3 _matrix = float4x3(asfloat(bar.Load3(0+0)), asfloat(bar.Load3(0+16)), asfloat(bar.Load3(0+32)), asfloat(bar.Load3(0+48)));
uint2 arr_1[2] = Constructarray2_uint2_(asuint(bar.Load2(144+0)), asuint(bar.Load2(144+8)));
float b = asfloat(bar.Load(0+48+0));
float b = asfloat(bar.Load(0+3u*16+0));
int a_1 = asint(bar.Load(0+(((NagaBufferLengthRW(bar) - 160) / 8) - 2u)*8+160));
int2 c = asint(qux.Load2(0));
const float _e33 = read_from_private(foo);
Expand Down
4 changes: 0 additions & 4 deletions tests/out/hlsl/const-exprs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ RWByteAddressBuffer out2_ : register(u1);

void swizzle_of_compose()
{
int2 a = int2(1, 2);
int2 b = int2(3, 4);
out_.Store4(0, asuint(int4(4, 3, 2, 1)));
return;
}

void index_of_compose()
{
int2 a_1 = int2(1, 2);
int2 b_1 = int2(3, 4);
int _expr8 = asint(out2_.Load(0));
out2_.Store(0, asuint((_expr8 + 2)));
return;
Expand Down
Loading

0 comments on commit 06add21

Please sign in to comment.