tint: Add PreservePadding transform

This is used to ensure that assignments to host-visible memory do not
modify padding bytes in structures and arrays. We decompose
assignments of whole structure and array types into member-wise or
element-wise copies, using helper functions.

This is used in all backends except HLSL, which already decomposes
memory accesses.

Bug: tint:1571
Change-Id: Id6de2f917fb80151cc654a7e1c8413ae956f0d61
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/112720
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: James Price <jrprice@google.com>
This commit is contained in:
James Price
2022-12-06 18:32:19 +00:00
committed by Dawn LUCI CQ
parent f2b8d2b1ab
commit 8753796aac
90 changed files with 5411 additions and 1972 deletions

View File

@@ -44,9 +44,25 @@ layout(binding = 1, std430) buffer u_block_ssbo {
S inner[4];
} s;
void assign_and_preserve_padding_1_s_X(uint dest[1], S value) {
s.inner[dest[0]].before = value.before;
s.inner[dest[0]].m = value.m;
s.inner[dest[0]].after = value.after;
}
void assign_and_preserve_padding_s(S value[4]) {
{
for(uint i = 0u; (i < 4u); i = (i + 1u)) {
uint tint_symbol[1] = uint[1](i);
assign_and_preserve_padding_1_s_X(tint_symbol, value[i]);
}
}
}
void f() {
s.inner = u.inner;
s.inner[1] = u.inner[2];
assign_and_preserve_padding_s(u.inner);
uint tint_symbol_1[1] = uint[1](1u);
assign_and_preserve_padding_1_s_X(tint_symbol_1, u.inner[2]);
s.inner[3].m = u.inner[2].m;
s.inner[1].m[0] = u.inner[0].m[1].zxy;
}

View File

@@ -23,9 +23,21 @@ struct S {
/* 0x0084 */ tint_array<int8_t, 60> tint_pad_2;
};
void assign_and_preserve_padding_1(device S* const dest, S value) {
(*(dest)).before = value.before;
(*(dest)).m = value.m;
(*(dest)).after = value.after;
}
void assign_and_preserve_padding(device tint_array<S, 4>* const dest, tint_array<S, 4> value) {
for(uint i = 0u; (i < 4u); i = (i + 1u)) {
assign_and_preserve_padding_1(&((*(dest))[i]), value[i]);
}
}
kernel void f(device tint_array<S, 4>* tint_symbol [[buffer(1)]], const constant tint_array<S, 4>* tint_symbol_1 [[buffer(0)]]) {
*(tint_symbol) = *(tint_symbol_1);
(*(tint_symbol))[1] = (*(tint_symbol_1))[2];
assign_and_preserve_padding(tint_symbol, *(tint_symbol_1));
assign_and_preserve_padding_1(&((*(tint_symbol))[1]), (*(tint_symbol_1))[2]);
(*(tint_symbol))[3].m = (*(tint_symbol_1))[2].m;
(*(tint_symbol))[1].m[0] = float3((*(tint_symbol_1))[0].m[1]).zxy;
return;

View File

@@ -1,7 +1,7 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 45
; Bound: 90
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
@@ -15,6 +15,13 @@
OpMemberName %S 2 "after"
OpName %u "u"
OpName %s "s"
OpName %assign_and_preserve_padding_1_s_X "assign_and_preserve_padding_1_s_X"
OpName %dest "dest"
OpName %value "value"
OpName %assign_and_preserve_padding_s "assign_and_preserve_padding_s"
OpName %value_0 "value"
OpName %i "i"
OpName %var_for_index "var_for_index"
OpName %f "f"
OpDecorate %u_block Block
OpMemberDecorate %u_block 0 Offset 0
@@ -29,6 +36,7 @@
OpDecorate %u Binding 0
OpDecorate %s DescriptorSet 0
OpDecorate %s Binding 1
OpDecorate %_arr_uint_uint_1 ArrayStride 4
%int = OpTypeInt 32 1
%float = OpTypeFloat 32
%v3float = OpTypeVector %float 3
@@ -43,39 +51,100 @@
%_ptr_StorageBuffer_u_block = OpTypePointer StorageBuffer %u_block
%s = OpVariable %_ptr_StorageBuffer_u_block StorageBuffer
%void = OpTypeVoid
%14 = OpTypeFunction %void
%uint_1 = OpConstant %uint 1
%_arr_uint_uint_1 = OpTypeArray %uint %uint_1
%14 = OpTypeFunction %void %_arr_uint_uint_1 %S
%uint_0 = OpConstant %uint 0
%_ptr_StorageBuffer__arr_S_uint_4 = OpTypePointer StorageBuffer %_arr_S_uint_4
%23 = OpConstantNull %int
%_ptr_StorageBuffer_int = OpTypePointer StorageBuffer %int
%_ptr_StorageBuffer_mat4v3float = OpTypePointer StorageBuffer %mat4v3float
%uint_2 = OpConstant %uint 2
%36 = OpTypeFunction %void %_arr_S_uint_4
%40 = OpConstantNull %uint
%_ptr_Function_uint = OpTypePointer Function %uint
%bool = OpTypeBool
%_ptr_Function__arr_S_uint_4 = OpTypePointer Function %_arr_S_uint_4
%55 = OpConstantNull %_arr_S_uint_4
%_ptr_Function_S = OpTypePointer Function %S
%65 = OpTypeFunction %void
%_ptr_Uniform__arr_S_uint_4 = OpTypePointer Uniform %_arr_S_uint_4
%int_1 = OpConstant %int 1
%_ptr_StorageBuffer_S = OpTypePointer StorageBuffer %S
%73 = OpConstantComposite %_arr_uint_uint_1 %uint_1
%int_2 = OpConstant %int 2
%_ptr_Uniform_S = OpTypePointer Uniform %S
%int_3 = OpConstant %int 3
%uint_1 = OpConstant %uint 1
%_ptr_StorageBuffer_mat4v3float = OpTypePointer StorageBuffer %mat4v3float
%_ptr_Uniform_mat4v3float = OpTypePointer Uniform %mat4v3float
%38 = OpConstantNull %int
%int_1 = OpConstant %int 1
%_ptr_StorageBuffer_v3float = OpTypePointer StorageBuffer %v3float
%_ptr_Uniform_v3float = OpTypePointer Uniform %v3float
%f = OpFunction %void None %14
%17 = OpLabel
%20 = OpAccessChain %_ptr_StorageBuffer__arr_S_uint_4 %s %uint_0
%22 = OpAccessChain %_ptr_Uniform__arr_S_uint_4 %u %uint_0
%23 = OpLoad %_arr_S_uint_4 %22
OpStore %20 %23
%26 = OpAccessChain %_ptr_StorageBuffer_S %s %uint_0 %int_1
%29 = OpAccessChain %_ptr_Uniform_S %u %uint_0 %int_2
%30 = OpLoad %S %29
OpStore %26 %30
%34 = OpAccessChain %_ptr_StorageBuffer_mat4v3float %s %uint_0 %int_3 %uint_1
%36 = OpAccessChain %_ptr_Uniform_mat4v3float %u %uint_0 %int_2 %uint_1
%37 = OpLoad %mat4v3float %36
OpStore %34 %37
%40 = OpAccessChain %_ptr_StorageBuffer_v3float %s %uint_0 %int_1 %uint_1 %38
%42 = OpAccessChain %_ptr_Uniform_v3float %u %uint_0 %38 %uint_1 %int_1
%43 = OpLoad %v3float %42
%44 = OpVectorShuffle %v3float %43 %43 2 0 1
OpStore %40 %44
%assign_and_preserve_padding_1_s_X = OpFunction %void None %14
%dest = OpFunctionParameter %_arr_uint_uint_1
%value = OpFunctionParameter %S
%21 = OpLabel
%24 = OpCompositeExtract %uint %dest 0
%26 = OpAccessChain %_ptr_StorageBuffer_int %s %uint_0 %24 %uint_0
%27 = OpCompositeExtract %int %value 0
OpStore %26 %27
%28 = OpCompositeExtract %uint %dest 0
%30 = OpAccessChain %_ptr_StorageBuffer_mat4v3float %s %uint_0 %28 %uint_1
%31 = OpCompositeExtract %mat4v3float %value 1
OpStore %30 %31
%32 = OpCompositeExtract %uint %dest 0
%34 = OpAccessChain %_ptr_StorageBuffer_int %s %uint_0 %32 %uint_2
%35 = OpCompositeExtract %int %value 2
OpStore %34 %35
OpReturn
OpFunctionEnd
%assign_and_preserve_padding_s = OpFunction %void None %36
%value_0 = OpFunctionParameter %_arr_S_uint_4
%39 = OpLabel
%i = OpVariable %_ptr_Function_uint Function %40
%var_for_index = OpVariable %_ptr_Function__arr_S_uint_4 Function %55
OpStore %i %40
OpBranch %43
%43 = OpLabel
OpLoopMerge %44 %45 None
OpBranch %46
%46 = OpLabel
%48 = OpLoad %uint %i
%49 = OpULessThan %bool %48 %uint_4
%47 = OpLogicalNot %bool %49
OpSelectionMerge %51 None
OpBranchConditional %47 %52 %51
%52 = OpLabel
OpBranch %44
%51 = OpLabel
OpStore %var_for_index %value_0
%57 = OpLoad %uint %i
%58 = OpCompositeConstruct %_arr_uint_uint_1 %57
%59 = OpLoad %uint %i
%61 = OpAccessChain %_ptr_Function_S %var_for_index %59
%62 = OpLoad %S %61
%56 = OpFunctionCall %void %assign_and_preserve_padding_1_s_X %58 %62
OpBranch %45
%45 = OpLabel
%63 = OpLoad %uint %i
%64 = OpIAdd %uint %63 %uint_1
OpStore %i %64
OpBranch %43
%44 = OpLabel
OpReturn
OpFunctionEnd
%f = OpFunction %void None %65
%67 = OpLabel
%70 = OpAccessChain %_ptr_Uniform__arr_S_uint_4 %u %uint_0
%71 = OpLoad %_arr_S_uint_4 %70
%68 = OpFunctionCall %void %assign_and_preserve_padding_s %71
%76 = OpAccessChain %_ptr_Uniform_S %u %uint_0 %int_2
%77 = OpLoad %S %76
%72 = OpFunctionCall %void %assign_and_preserve_padding_1_s_X %73 %77
%79 = OpAccessChain %_ptr_StorageBuffer_mat4v3float %s %uint_0 %int_3 %uint_1
%81 = OpAccessChain %_ptr_Uniform_mat4v3float %u %uint_0 %int_2 %uint_1
%82 = OpLoad %mat4v3float %81
OpStore %79 %82
%85 = OpAccessChain %_ptr_StorageBuffer_v3float %s %uint_0 %int_1 %uint_1 %23
%87 = OpAccessChain %_ptr_Uniform_v3float %u %uint_0 %23 %uint_1 %int_1
%88 = OpLoad %v3float %87
%89 = OpVectorShuffle %v3float %88 %88 2 0 1
OpStore %85 %89
OpReturn
OpFunctionEnd