Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[msl-out] Insert padding initialization for global constants #1988

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ pub struct Writer<W> {
put_expression_stack_pointers: FastHashSet<*const ()>,
#[cfg(test)]
put_block_stack_pointers: FastHashSet<*const ()>,
/// Set of (struct type, struct field index) denoting which fields require
/// padding inserted **before** them (i.e. between fields at index - 1 and index)
struct_member_pads: FastHashSet<(Handle<crate::Type>, u32)>,
}

Expand Down Expand Up @@ -3082,6 +3084,10 @@ impl<W: Write> Writer<W> {
};
write!(self.out, "constant {} {} = {{", ty_name, name,)?;
for (i, &sub_handle) in components.iter().enumerate() {
// insert padding initialization, if needed
if self.struct_member_pads.contains(&(ty, i as u32)) {
write!(self.out, ", {{}}")?;
}
let separator = if i != 0 { ", " } else { "" };
let coco = ConstantContext {
handle: sub_handle,
Expand Down
8 changes: 8 additions & 0 deletions tests/in/access.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// This snapshot tests accessing various containers, dereferencing pointers.

struct GlobalConst {
a: u32,
b: vec3<u32>,
c: i32,
}
// tests msl padding insertion for global constants
var<private> global_const: GlobalConst = GlobalConst(0u, vec3<u32>(0u, 0u, 0u), 0);

struct AlignedWrapper {
@align(8) value: i32
}
Expand Down
9 changes: 7 additions & 2 deletions tests/out/glsl/access.assign_through_ptr.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ precision highp int;

layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

struct GlobalConst {
uint a;
uvec3 b;
int c;
};
struct AlignedWrapper {
int value;
};
Expand All @@ -15,8 +20,8 @@ shared uint val;


float read_from_private(inout float foo_1) {
float _e4 = foo_1;
return _e4;
float _e5 = foo_1;
return _e5;
}

float test_arr_as_arg(float a[5][10]) {
Expand Down
41 changes: 23 additions & 18 deletions tests/out/glsl/access.atomics.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ precision highp int;

layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

struct GlobalConst {
uint a;
uvec3 b;
int c;
};
struct AlignedWrapper {
int value;
};
Expand All @@ -21,8 +26,8 @@ layout(std430) buffer Bar_block_0Compute {


float read_from_private(inout float foo_1) {
float _e4 = foo_1;
return _e4;
float _e5 = foo_1;
return _e5;
}

float test_arr_as_arg(float a[5][10]) {
Expand All @@ -37,22 +42,22 @@ void assign_through_ptr_fn(inout uint p) {
void main() {
int tmp = 0;
int value = _group_0_binding_0_cs.atom;
int _e8 = atomicAdd(_group_0_binding_0_cs.atom, 5);
tmp = _e8;
int _e11 = atomicAdd(_group_0_binding_0_cs.atom, -5);
tmp = _e11;
int _e14 = atomicAnd(_group_0_binding_0_cs.atom, 5);
tmp = _e14;
int _e17 = atomicOr(_group_0_binding_0_cs.atom, 5);
tmp = _e17;
int _e20 = atomicXor(_group_0_binding_0_cs.atom, 5);
tmp = _e20;
int _e23 = atomicMin(_group_0_binding_0_cs.atom, 5);
tmp = _e23;
int _e26 = atomicMax(_group_0_binding_0_cs.atom, 5);
tmp = _e26;
int _e29 = atomicExchange(_group_0_binding_0_cs.atom, 5);
tmp = _e29;
int _e9 = atomicAdd(_group_0_binding_0_cs.atom, 5);
tmp = _e9;
int _e12 = atomicAdd(_group_0_binding_0_cs.atom, -5);
tmp = _e12;
int _e15 = atomicAnd(_group_0_binding_0_cs.atom, 5);
tmp = _e15;
int _e18 = atomicOr(_group_0_binding_0_cs.atom, 5);
tmp = _e18;
int _e21 = atomicXor(_group_0_binding_0_cs.atom, 5);
tmp = _e21;
int _e24 = atomicMin(_group_0_binding_0_cs.atom, 5);
tmp = _e24;
int _e27 = atomicMax(_group_0_binding_0_cs.atom, 5);
tmp = _e27;
int _e30 = atomicExchange(_group_0_binding_0_cs.atom, 5);
tmp = _e30;
_group_0_binding_0_cs.atom = value;
return;
}
Expand Down
11 changes: 8 additions & 3 deletions tests/out/glsl/access.foo_frag.Fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
precision highp float;
precision highp int;

struct GlobalConst {
uint a;
uvec3 b;
int c;
};
struct AlignedWrapper {
int value;
};
Expand All @@ -17,13 +22,13 @@ layout(std430) buffer Bar_block_0Fragment {
AlignedWrapper data[];
} _group_0_binding_0_fs;

layout(std430) buffer type_9_block_1Fragment { ivec2 _group_0_binding_2_fs; };
layout(std430) buffer type_11_block_1Fragment { ivec2 _group_0_binding_2_fs; };

layout(location = 0) out vec4 _fs2p_location0;

float read_from_private(inout float foo_1) {
float _e4 = foo_1;
return _e4;
float _e5 = foo_1;
return _e5;
}

float test_arr_as_arg(float a[5][10]) {
Expand Down
59 changes: 32 additions & 27 deletions tests/out/glsl/access.foo_vert.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
precision highp float;
precision highp int;

struct GlobalConst {
uint a;
uvec3 b;
int c;
};
struct AlignedWrapper {
int value;
};
Expand All @@ -19,47 +24,47 @@ layout(std430) buffer Bar_block_0Vertex {

uniform Baz_block_1Vertex { Baz _group_0_binding_1_vs; };

layout(std430) buffer type_9_block_2Vertex { ivec2 _group_0_binding_2_vs; };
layout(std430) buffer type_11_block_2Vertex { ivec2 _group_0_binding_2_vs; };


void test_matrix_within_struct_accesses() {
int idx = 1;
Baz t = Baz(mat3x2(0.0));
int _e5 = idx;
idx = (_e5 - 1);
int _e6 = idx;
idx = (_e6 - 1);
mat3x2 unnamed = _group_0_binding_1_vs.m;
vec2 unnamed_1 = _group_0_binding_1_vs.m[0];
int _e15 = idx;
vec2 unnamed_2 = _group_0_binding_1_vs.m[_e15];
int _e16 = idx;
vec2 unnamed_2 = _group_0_binding_1_vs.m[_e16];
float unnamed_3 = _group_0_binding_1_vs.m[0][1];
int _e27 = idx;
float unnamed_4 = _group_0_binding_1_vs.m[0][_e27];
int _e31 = idx;
float unnamed_5 = _group_0_binding_1_vs.m[_e31][1];
int _e37 = idx;
int _e39 = idx;
float unnamed_6 = _group_0_binding_1_vs.m[_e37][_e39];
int _e28 = idx;
float unnamed_4 = _group_0_binding_1_vs.m[0][_e28];
int _e32 = idx;
float unnamed_5 = _group_0_binding_1_vs.m[_e32][1];
int _e38 = idx;
int _e40 = idx;
float unnamed_6 = _group_0_binding_1_vs.m[_e38][_e40];
t = Baz(mat3x2(vec2(1.0), vec2(2.0), vec2(3.0)));
int _e51 = idx;
idx = (_e51 + 1);
int _e52 = idx;
idx = (_e52 + 1);
t.m = mat3x2(vec2(6.0), vec2(5.0), vec2(4.0));
t.m[0] = vec2(9.0);
int _e68 = idx;
t.m[_e68] = vec2(90.0);
int _e69 = idx;
t.m[_e69] = vec2(90.0);
t.m[0][1] = 10.0;
int _e81 = idx;
t.m[0][_e81] = 20.0;
int _e85 = idx;
t.m[_e85][1] = 30.0;
int _e91 = idx;
int _e93 = idx;
t.m[_e91][_e93] = 40.0;
int _e82 = idx;
t.m[0][_e82] = 20.0;
int _e86 = idx;
t.m[_e86][1] = 30.0;
int _e92 = idx;
int _e94 = idx;
t.m[_e92][_e94] = 40.0;
return;
}

float read_from_private(inout float foo_1) {
float _e4 = foo_1;
return _e4;
float _e5 = foo_1;
return _e5;
}

float test_arr_as_arg(float a[5][10]) {
Expand All @@ -83,11 +88,11 @@ void main() {
float b = _group_0_binding_0_vs._matrix[3][0];
int a_1 = _group_0_binding_0_vs.data[(uint(_group_0_binding_0_vs.data.length()) - 2u)].value;
ivec2 c_1 = _group_0_binding_2_vs;
float _e30 = read_from_private(foo);
float _e31 = read_from_private(foo);
c = int[5](a_1, int(b), 3, 4, 5);
c[(vi + 1u)] = 42;
int value = c[vi];
float _e44 = test_arr_as_arg(float[5][10](float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
float _e45 = test_arr_as_arg(float[5][10](float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), float[10](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
gl_Position = vec4((_matrix * vec4(ivec4(value))), 2.0);
gl_Position.yz = vec2(-gl_Position.y, gl_Position.z * 2.0 - gl_Position.w);
return;
Expand Down
Loading