Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Commit

Permalink
Use Expression::ZeroValue in SPIR-V and WGSL front ends.
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy authored and jimblandy committed May 11, 2023
1 parent cfb9a13 commit ac9e9bb
Show file tree
Hide file tree
Showing 32 changed files with 2,301 additions and 2,474 deletions.
9 changes: 6 additions & 3 deletions src/front/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,14 +1324,17 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
Op::NoLine => inst.expect(1)?,
Op::Undef => {
inst.expect(3)?;
let (type_id, id, handle) =
self.parse_null_constant(inst, ctx.type_arena, ctx.const_arena)?;
let type_id = self.next()?;
let id = self.next()?;
let type_lookup = self.lookup_type.lookup(type_id)?;
let ty = type_lookup.handle;

self.lookup_expression.insert(
id,
LookupExpression {
handle: ctx
.expressions
.append(crate::Expression::Constant(handle), span),
.append(crate::Expression::ZeroValue(ty), span),
type_id,
block_id,
},
Expand Down
7 changes: 1 addition & 6 deletions src/front/wgsl/lower/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
_ => return Err(Error::TypeNotInferrable(ty_span)),
};

return match ctx.create_zero_value_constant(ty) {
Some(constant) => {
Ok(ctx.interrupt_emitter(crate::Expression::Constant(constant), span))
}
None => Err(Error::TypeNotConstructible(ty_span)),
};
return Ok(ctx.interrupt_emitter(crate::Expression::ZeroValue(ty), span));
}

// Scalar constructor & conversion (scalar -> scalar)
Expand Down
77 changes: 0 additions & 77 deletions src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,83 +342,6 @@ impl<'a> ExpressionContext<'a, '_, '_> {
}
}

/// Creates a zero value constant of type `ty`
///
/// Returns `None` if the given `ty` is not a constructible type
fn create_zero_value_constant(
&mut self,
ty: Handle<crate::Type>,
) -> Option<Handle<crate::Constant>> {
let inner = match self.module.types[ty].inner {
crate::TypeInner::Scalar { kind, width } => {
let value = match kind {
crate::ScalarKind::Sint => crate::ScalarValue::Sint(0),
crate::ScalarKind::Uint => crate::ScalarValue::Uint(0),
crate::ScalarKind::Float => crate::ScalarValue::Float(0.),
crate::ScalarKind::Bool => crate::ScalarValue::Bool(false),
};
crate::ConstantInner::Scalar { width, value }
}
crate::TypeInner::Vector { size, kind, width } => {
let scalar_ty = self.ensure_type_exists(crate::TypeInner::Scalar { width, kind });
let component = self.create_zero_value_constant(scalar_ty)?;
crate::ConstantInner::Composite {
ty,
components: (0..size as u8).map(|_| component).collect(),
}
}
crate::TypeInner::Matrix {
columns,
rows,
width,
} => {
let vec_ty = self.ensure_type_exists(crate::TypeInner::Vector {
width,
kind: crate::ScalarKind::Float,
size: rows,
});
let component = self.create_zero_value_constant(vec_ty)?;
crate::ConstantInner::Composite {
ty,
components: (0..columns as u8).map(|_| component).collect(),
}
}
crate::TypeInner::Array {
base,
size: crate::ArraySize::Constant(size),
..
} => {
let size = self.module.constants[size].to_array_length()?;
let component = self.create_zero_value_constant(base)?;
crate::ConstantInner::Composite {
ty,
components: (0..size).map(|_| component).collect(),
}
}
crate::TypeInner::Struct { ref members, .. } => {
let members = members.clone();
crate::ConstantInner::Composite {
ty,
components: members
.iter()
.map(|member| self.create_zero_value_constant(member.ty))
.collect::<Option<_>>()?,
}
}
_ => return None,
};

let constant = self.module.constants.fetch_or_append(
crate::Constant {
name: None,
specialization: None,
inner,
},
Span::UNDEFINED,
);
Some(constant)
}

fn format_typeinner(&self, inner: &crate::TypeInner) -> String {
inner.to_wgsl(&self.module.types, &self.module.constants)
}
Expand Down
1 change: 1 addition & 0 deletions src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ impl crate::Expression {
pub const fn needs_pre_emit(&self) -> bool {
match *self {
Self::Constant(_)
| Self::ZeroValue(_)
| Self::FunctionArgument(_)
| Self::GlobalVariable(_)
| Self::LocalVariable(_) => true,
Expand Down
2 changes: 1 addition & 1 deletion tests/out/glsl/access.foo_frag.Fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void main() {
_group_0_binding_0_fs._matrix = mat4x3(vec3(0.0), vec3(1.0), vec3(2.0), vec3(3.0));
_group_0_binding_0_fs.arr = uvec2[2](uvec2(0u), uvec2(1u));
_group_0_binding_0_fs.data[1].value = 1;
_group_0_binding_2_fs = ivec2(0, 0);
_group_0_binding_2_fs = ivec2(0);
_fs2p_location0 = vec4(0.0);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/out/glsl/access.foo_vert.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ void test_matrix_within_array_within_struct_accesses() {
int _e58 = idx_1;
int _e60 = idx_1;
float unnamed_14 = _group_0_binding_3_vs.am[0][_e58][_e60];
t_1 = MatCx2InArray(mat4x2[2](mat4x2(vec2(0.0, 0.0), vec2(0.0, 0.0), vec2(0.0, 0.0), vec2(0.0, 0.0)), mat4x2(vec2(0.0, 0.0), vec2(0.0, 0.0), vec2(0.0, 0.0), vec2(0.0, 0.0))));
t_1 = MatCx2InArray(mat4x2[2](mat4x2(0.0), mat4x2(0.0)));
int _e66 = idx_1;
idx_1 = (_e66 + 1);
t_1.am = mat4x2[2](mat4x2(vec2(0.0, 0.0), vec2(0.0, 0.0), vec2(0.0, 0.0), vec2(0.0, 0.0)), mat4x2(vec2(0.0, 0.0), vec2(0.0, 0.0), vec2(0.0, 0.0), vec2(0.0, 0.0)));
t_1.am = mat4x2[2](mat4x2(0.0), mat4x2(0.0));
t_1.am[0] = mat4x2(vec2(8.0), vec2(7.0), vec2(6.0), vec2(5.0));
t_1.am[0][0] = vec2(9.0);
int _e93 = idx_1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ void test_textureStore_3d(ivec3 coords_14, vec4 value_4) {

void main() {
vec4 _e2 = test_textureLoad_1d(0, 0);
vec4 _e5 = test_textureLoad_2d(ivec2(0, 0), 0);
vec4 _e9 = test_textureLoad_2d_array_u(ivec2(0, 0), 0u, 0);
vec4 _e13 = test_textureLoad_2d_array_s(ivec2(0, 0), 0, 0);
vec4 _e16 = test_textureLoad_3d(ivec3(0, 0, 0), 0);
vec4 _e19 = test_textureLoad_multisampled_2d(ivec2(0, 0), 0);
test_textureStore_1d(0, vec4(0.0, 0.0, 0.0, 0.0));
test_textureStore_2d(ivec2(0, 0), vec4(0.0, 0.0, 0.0, 0.0));
test_textureStore_2d_array_u(ivec2(0, 0), 0u, vec4(0.0, 0.0, 0.0, 0.0));
test_textureStore_2d_array_s(ivec2(0, 0), 0, vec4(0.0, 0.0, 0.0, 0.0));
test_textureStore_3d(ivec3(0, 0, 0), vec4(0.0, 0.0, 0.0, 0.0));
vec4 _e5 = test_textureLoad_2d(ivec2(0), 0);
vec4 _e9 = test_textureLoad_2d_array_u(ivec2(0), 0u, 0);
vec4 _e13 = test_textureLoad_2d_array_s(ivec2(0), 0, 0);
vec4 _e16 = test_textureLoad_3d(ivec3(0), 0);
vec4 _e19 = test_textureLoad_multisampled_2d(ivec2(0), 0);
test_textureStore_1d(0, vec4(0.0));
test_textureStore_2d(ivec2(0), vec4(0.0));
test_textureStore_2d_array_u(ivec2(0), 0u, vec4(0.0));
test_textureStore_2d_array_s(ivec2(0), 0, vec4(0.0));
test_textureStore_3d(ivec3(0), vec4(0.0));
_fs2p_location0 = vec4(0.0, 0.0, 0.0, 0.0);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ void test_textureStore_3d(ivec3 coords_14, vec4 value_4) {

void main() {
vec4 _e2 = test_textureLoad_1d(0, 0);
vec4 _e5 = test_textureLoad_2d(ivec2(0, 0), 0);
vec4 _e9 = test_textureLoad_2d_array_u(ivec2(0, 0), 0u, 0);
vec4 _e13 = test_textureLoad_2d_array_s(ivec2(0, 0), 0, 0);
vec4 _e16 = test_textureLoad_3d(ivec3(0, 0, 0), 0);
vec4 _e19 = test_textureLoad_multisampled_2d(ivec2(0, 0), 0);
test_textureStore_1d(0, vec4(0.0, 0.0, 0.0, 0.0));
test_textureStore_2d(ivec2(0, 0), vec4(0.0, 0.0, 0.0, 0.0));
test_textureStore_2d_array_u(ivec2(0, 0), 0u, vec4(0.0, 0.0, 0.0, 0.0));
test_textureStore_2d_array_s(ivec2(0, 0), 0, vec4(0.0, 0.0, 0.0, 0.0));
test_textureStore_3d(ivec3(0, 0, 0), vec4(0.0, 0.0, 0.0, 0.0));
vec4 _e5 = test_textureLoad_2d(ivec2(0), 0);
vec4 _e9 = test_textureLoad_2d_array_u(ivec2(0), 0u, 0);
vec4 _e13 = test_textureLoad_2d_array_s(ivec2(0), 0, 0);
vec4 _e16 = test_textureLoad_3d(ivec3(0), 0);
vec4 _e19 = test_textureLoad_multisampled_2d(ivec2(0), 0);
test_textureStore_1d(0, vec4(0.0));
test_textureStore_2d(ivec2(0), vec4(0.0));
test_textureStore_2d_array_u(ivec2(0), 0u, vec4(0.0));
test_textureStore_2d_array_s(ivec2(0), 0, vec4(0.0));
test_textureStore_3d(ivec3(0), vec4(0.0));
_fs2p_location0 = vec4(0.0, 0.0, 0.0, 0.0);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/out/glsl/globals.main.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ void test_msl_packed_vec3_() {
vec3 unnamed = data.v3_;
vec2 unnamed_1 = data.v3_.zx;
test_msl_packed_vec3_as_arg(data.v3_);
vec3 unnamed_2 = (data.v3_ * mat3x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)));
vec3 unnamed_3 = (mat3x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)) * data.v3_);
vec3 unnamed_2 = (data.v3_ * mat3x3(0.0));
vec3 unnamed_3 = (mat3x3(0.0) * data.v3_);
vec3 unnamed_4 = (data.v3_ * 2.0);
vec3 unnamed_5 = (2.0 * data.v3_);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/out/glsl/math-functions.main.Fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void main() {
vec4 d = radians(v);
vec4 e = clamp(v, vec4(0.0), vec4(1.0));
vec4 g = refract(v, v, 1.0);
int const_dot = ( + ivec2(0, 0).x * ivec2(0, 0).x + ivec2(0, 0).y * ivec2(0, 0).y);
int const_dot = ( + ivec2(0).x * ivec2(0).x + ivec2(0).y * ivec2(0).y);
uint first_leading_bit_abs = uint(findMSB(uint(abs(int(0u)))));
int flb_a = findMSB(-1);
ivec2 flb_b = findMSB(ivec2(-1));
Expand Down
24 changes: 12 additions & 12 deletions tests/out/glsl/operators.main.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ float constructors() {
int unnamed_4 = int(0);
uint unnamed_5 = uint(0u);
float unnamed_6 = float(0.0);
uvec2 unnamed_7 = uvec2(uvec2(0u, 0u));
mat2x3 unnamed_8 = mat2x3(mat2x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)));
uvec2 unnamed_9 = uvec2(0u, 0u);
mat2x3 unnamed_10 = mat2x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0));
uvec2 unnamed_7 = uvec2(uvec2(0u));
mat2x3 unnamed_8 = mat2x3(mat2x3(0.0));
uvec2 unnamed_9 = uvec2(0u);
mat2x3 unnamed_10 = mat2x3(0.0);
float _e71 = foo.a.x;
return _e71;
}
Expand Down Expand Up @@ -140,13 +140,13 @@ void arithmetic() {
uvec2 unnamed_78 = (uvec2(2u) % uvec2(1u));
vec2 unnamed_79 = (vec2(2.0) - vec2(1.0) * trunc(vec2(2.0) / vec2(1.0)));
vec2 unnamed_80 = (vec2(2.0) - vec2(1.0) * trunc(vec2(2.0) / vec2(1.0)));
mat3x3 unnamed_81 = (mat3x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)) + mat3x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)));
mat3x3 unnamed_82 = (mat3x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)) - mat3x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)));
mat3x3 unnamed_83 = (mat3x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)) * 1.0);
mat3x3 unnamed_84 = (2.0 * mat3x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)));
vec3 unnamed_85 = (mat4x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)) * vec4(1.0));
vec4 unnamed_86 = (vec3(2.0) * mat4x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)));
mat3x3 unnamed_87 = (mat4x3(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0)) * mat3x4(vec4(0.0, 0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0)));
mat3x3 unnamed_81 = (mat3x3(0.0) + mat3x3(0.0));
mat3x3 unnamed_82 = (mat3x3(0.0) - mat3x3(0.0));
mat3x3 unnamed_83 = (mat3x3(0.0) * 1.0);
mat3x3 unnamed_84 = (2.0 * mat3x3(0.0));
vec3 unnamed_85 = (mat4x3(0.0) * vec4(1.0));
vec4 unnamed_86 = (vec3(2.0) * mat4x3(0.0));
mat3x3 unnamed_87 = (mat4x3(0.0) * mat3x4(0.0));
}

void bit() {
Expand Down Expand Up @@ -245,7 +245,7 @@ void assignment() {
a_1 = (_e32 + 1);
int _e35 = a_1;
a_1 = (_e35 - 1);
vec0_ = ivec3(0, 0, 0);
vec0_ = ivec3(0);
int _e42 = vec0_.y;
vec0_.y = (_e42 + 1);
int _e47 = vec0_.y;
Expand Down
26 changes: 4 additions & 22 deletions tests/out/hlsl/access.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,6 @@ GlobalConst ConstructGlobalConst(uint arg0, uint3 arg1, int arg2) {
return ret;
}

typedef float4x2 ret_Constructarray2_float4x2_[2];
ret_Constructarray2_float4x2_ Constructarray2_float4x2_(float4x2 arg0, float4x2 arg1) {
float4x2 ret[2] = { arg0, arg1 };
return ret;
}

typedef float ret_Constructarray10_float_[10];
ret_Constructarray10_float_ Constructarray10_float_(float arg0, float arg1, float arg2, float arg3, float arg4, float arg5, float arg6, float arg7, float arg8, float arg9) {
float ret[10] = { arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9 };
return ret;
}

typedef float ret_Constructarray5_array10_float__[5][10];
ret_Constructarray5_array10_float__ Constructarray5_array10_float__(float arg0[10], float arg1[10], float arg2[10], float arg3[10], float arg4[10]) {
float ret[5][10] = { arg0, arg1, arg2, arg3, arg4 };
return ret;
}

static GlobalConst global_const = ConstructGlobalConst(0u, uint3(0u, 0u, 0u), 0);
RWByteAddressBuffer bar : register(u0);
cbuffer baz : register(b1) { Baz baz; }
Expand Down Expand Up @@ -201,10 +183,10 @@ void test_matrix_within_array_within_struct_accesses()
int _expr58 = idx_1;
int _expr60 = idx_1;
float unnamed_14 = __get_col_of_mat4x2(nested_mat_cx2_.am[0], _expr58)[_expr60];
t_1 = ConstructMatCx2InArray(Constructarray2_float4x2_(float4x2(float2(0.0, 0.0), float2(0.0, 0.0), float2(0.0, 0.0), float2(0.0, 0.0)), float4x2(float2(0.0, 0.0), float2(0.0, 0.0), float2(0.0, 0.0), float2(0.0, 0.0))));
t_1 = ConstructMatCx2InArray((float4x2[2])0);
int _expr66 = idx_1;
idx_1 = (_expr66 + 1);
t_1.am = (__mat4x2[2])Constructarray2_float4x2_(float4x2(float2(0.0, 0.0), float2(0.0, 0.0), float2(0.0, 0.0), float2(0.0, 0.0)), float4x2(float2(0.0, 0.0), float2(0.0, 0.0), float2(0.0, 0.0), float2(0.0, 0.0)));
t_1.am = (__mat4x2[2])(float4x2[2])0;
t_1.am[0] = (__mat4x2)float4x2((8.0).xx, (7.0).xx, (6.0).xx, (5.0).xx);
t_1.am[0]._0 = (9.0).xx;
int _expr93 = idx_1;
Expand Down Expand Up @@ -287,7 +269,7 @@ float4 foo_vert(uint vi : SV_VertexID) : SV_Position
c2_ = Constructarray5_int_(a_1, int(b), 3, 4, 5);
c2_[(vi + 1u)] = 42;
int value = c2_[vi];
const float _e48 = test_arr_as_arg(Constructarray5_array10_float__(Constructarray10_float_(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), Constructarray10_float_(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), Constructarray10_float_(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), Constructarray10_float_(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), Constructarray10_float_(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
const float _e48 = test_arr_as_arg((float[5][10])0);
return float4(mul(float4((value).xxxx), _matrix), 2.0);
}

Expand All @@ -307,7 +289,7 @@ float4 foo_frag() : SV_Target0
bar.Store2(144+8, asuint(_value2[1]));
}
bar.Store(0+8+160, asuint(1));
qux.Store2(0, asuint(int2(0, 0)));
qux.Store2(0, asuint((int2)0));
return (0.0).xxxx;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/out/hlsl/globals.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ void test_msl_packed_vec3_()
float3 unnamed = data.v3_;
float2 unnamed_1 = data.v3_.zx;
test_msl_packed_vec3_as_arg(data.v3_);
float3 unnamed_2 = mul(float3x3(float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 0.0)), data.v3_);
float3 unnamed_3 = mul(data.v3_, float3x3(float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 0.0)));
float3 unnamed_2 = mul((float3x3)0, data.v3_);
float3 unnamed_3 = mul(data.v3_, (float3x3)0);
float3 unnamed_4 = (data.v3_ * 2.0);
float3 unnamed_5 = (2.0 * data.v3_);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/out/hlsl/math-functions.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void main()
float4 d = radians(v);
float4 e = saturate(v);
float4 g = refract(v, v, 1.0);
int const_dot = dot(int2(0, 0), int2(0, 0));
int const_dot = dot((int2)0, (int2)0);
uint first_leading_bit_abs = firstbithigh(abs(0u));
int flb_a = asint(firstbithigh(-1));
int2 flb_b = asint(firstbithigh((-1).xx));
Expand Down
Loading

0 comments on commit ac9e9bb

Please sign in to comment.