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

@@ -76,6 +76,21 @@ 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]);
}
}
}
S conv_S(S_std140 val) {
return S(val.before, f16mat4x2(val.m_0, val.m_1, val.m_2, val.m_3), val.pad, val.pad_1, val.pad_2, val.pad_3, val.pad_4, val.pad_5, val.pad_6, val.pad_7, val.pad_8, val.pad_9, val.pad_10, val.after, val.pad_11, val.pad_12, val.pad_13, val.pad_14, val.pad_15, val.pad_16, val.pad_17, val.pad_18, val.pad_19, val.pad_20, val.pad_21, val.pad_22, val.pad_23, val.pad_24, val.pad_25);
}
@@ -95,8 +110,9 @@ f16mat4x2 load_u_inner_2_m() {
}
void f() {
s.inner = conv_arr4_S(u.inner);
s.inner[1] = conv_S(u.inner[2u]);
assign_and_preserve_padding_s(conv_arr4_S(u.inner));
uint tint_symbol_1[1] = uint[1](1u);
assign_and_preserve_padding_1_s_X(tint_symbol_1, conv_S(u.inner[2u]));
s.inner[3].m = load_u_inner_2_m();
s.inner[1].m[0] = u.inner[0u].m_1.yx;
}

View File

@@ -22,9 +22,21 @@ struct S {
/* 0x0044 */ tint_array<int8_t, 60> tint_pad_1;
};
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] = half2((*(tint_symbol_1))[0].m[1]).yx;
return;

View File

@@ -1,7 +1,7 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 113
; Bound: 151
; Schema: 0
OpCapability Shader
OpCapability Float16
@@ -28,13 +28,20 @@
OpMemberName %S 1 "m"
OpMemberName %S 2 "after"
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 %conv_S "conv_S"
OpName %val "val"
OpName %conv_arr4_S "conv_arr4_S"
OpName %val_0 "val"
OpName %arr "arr"
OpName %i "i"
OpName %var_for_index "var_for_index"
OpName %i_0 "i"
OpName %var_for_index_1 "var_for_index_1"
OpName %load_u_inner_2_m "load_u_inner_2_m"
OpName %f "f"
OpDecorate %u_block_std140 Block
@@ -59,6 +66,7 @@
OpDecorate %_arr_S_uint_4 ArrayStride 128
OpDecorate %s DescriptorSet 0
OpDecorate %s Binding 1
OpDecorate %_arr_uint_uint_1 ArrayStride 4
%int = OpTypeInt 32 1
%half = OpTypeFloat 16
%v2half = OpTypeVector %half 2
@@ -75,117 +83,171 @@
%u_block = OpTypeStruct %_arr_S_uint_4
%_ptr_StorageBuffer_u_block = OpTypePointer StorageBuffer %u_block
%s = OpVariable %_ptr_StorageBuffer_u_block StorageBuffer
%17 = OpTypeFunction %S %S_std140
%29 = OpTypeFunction %_arr_S_uint_4 %_arr_S_std140_uint_4
%_ptr_Function__arr_S_uint_4 = OpTypePointer Function %_arr_S_uint_4
%35 = OpConstantNull %_arr_S_uint_4
%_ptr_Function_uint = OpTypePointer Function %uint
%38 = OpConstantNull %uint
%bool = OpTypeBool
%_ptr_Function__arr_S_std140_uint_4 = OpTypePointer Function %_arr_S_std140_uint_4
%51 = OpConstantNull %_arr_S_std140_uint_4
%_ptr_Function_S = OpTypePointer Function %S
%_ptr_Function_S_std140 = OpTypePointer Function %S_std140
%void = OpTypeVoid
%uint_1 = OpConstant %uint 1
%64 = OpTypeFunction %mat4v2half
%_arr_uint_uint_1 = OpTypeArray %uint %uint_1
%17 = OpTypeFunction %void %_arr_uint_uint_1 %S
%uint_0 = OpConstant %uint 0
%26 = OpConstantNull %int
%_ptr_StorageBuffer_int = OpTypePointer StorageBuffer %int
%_ptr_StorageBuffer_mat4v2half = OpTypePointer StorageBuffer %mat4v2half
%uint_2 = OpConstant %uint 2
%39 = OpTypeFunction %void %_arr_S_uint_4
%43 = OpConstantNull %uint
%_ptr_Function_uint = OpTypePointer Function %uint
%bool = OpTypeBool
%_ptr_Function__arr_S_uint_4 = OpTypePointer Function %_arr_S_uint_4
%58 = OpConstantNull %_arr_S_uint_4
%_ptr_Function_S = OpTypePointer Function %S
%68 = OpTypeFunction %S %S_std140
%80 = OpTypeFunction %_arr_S_uint_4 %_arr_S_std140_uint_4
%_ptr_Function__arr_S_std140_uint_4 = OpTypePointer Function %_arr_S_std140_uint_4
%97 = OpConstantNull %_arr_S_std140_uint_4
%_ptr_Function_S_std140 = OpTypePointer Function %S_std140
%108 = OpTypeFunction %mat4v2half
%_ptr_Uniform_S_std140 = OpTypePointer Uniform %S_std140
%_ptr_Uniform_v2half = OpTypePointer Uniform %v2half
%uint_3 = OpConstant %uint 3
%void = OpTypeVoid
%87 = OpTypeFunction %void
%_ptr_StorageBuffer__arr_S_uint_4 = OpTypePointer StorageBuffer %_arr_S_uint_4
%129 = OpTypeFunction %void
%_ptr_Uniform__arr_S_std140_uint_4 = OpTypePointer Uniform %_arr_S_std140_uint_4
%int_1 = OpConstant %int 1
%_ptr_StorageBuffer_S = OpTypePointer StorageBuffer %S
%138 = OpConstantComposite %_arr_uint_uint_1 %uint_1
%int_3 = OpConstant %int 3
%_ptr_StorageBuffer_mat4v2half = OpTypePointer StorageBuffer %mat4v2half
%107 = OpConstantNull %int
%int_1 = OpConstant %int 1
%_ptr_StorageBuffer_v2half = OpTypePointer StorageBuffer %v2half
%conv_S = OpFunction %S None %17
%val = OpFunctionParameter %S_std140
%20 = OpLabel
%21 = OpCompositeExtract %int %val 0
%22 = OpCompositeExtract %v2half %val 1
%23 = OpCompositeExtract %v2half %val 2
%24 = OpCompositeExtract %v2half %val 3
%25 = OpCompositeExtract %v2half %val 4
%26 = OpCompositeConstruct %mat4v2half %22 %23 %24 %25
%27 = OpCompositeExtract %int %val 5
%28 = OpCompositeConstruct %S %21 %26 %27
OpReturnValue %28
OpFunctionEnd
%conv_arr4_S = OpFunction %_arr_S_uint_4 None %29
%val_0 = OpFunctionParameter %_arr_S_std140_uint_4
%32 = OpLabel
%arr = OpVariable %_ptr_Function__arr_S_uint_4 Function %35
%i = OpVariable %_ptr_Function_uint Function %38
%var_for_index = OpVariable %_ptr_Function__arr_S_std140_uint_4 Function %51
OpBranch %39
%39 = OpLabel
OpLoopMerge %40 %41 None
OpBranch %42
%42 = OpLabel
%44 = OpLoad %uint %i
%45 = OpULessThan %bool %44 %uint_4
%43 = OpLogicalNot %bool %45
OpSelectionMerge %47 None
OpBranchConditional %43 %48 %47
%48 = OpLabel
OpBranch %40
%47 = OpLabel
OpStore %var_for_index %val_0
%52 = OpLoad %uint %i
%54 = OpAccessChain %_ptr_Function_S %arr %52
%56 = OpLoad %uint %i
%58 = OpAccessChain %_ptr_Function_S_std140 %var_for_index %56
%59 = OpLoad %S_std140 %58
%55 = OpFunctionCall %S %conv_S %59
OpStore %54 %55
OpBranch %41
%41 = OpLabel
%60 = OpLoad %uint %i
%62 = OpIAdd %uint %60 %uint_1
OpStore %i %62
OpBranch %39
%40 = OpLabel
%63 = OpLoad %_arr_S_uint_4 %arr
OpReturnValue %63
OpFunctionEnd
%load_u_inner_2_m = OpFunction %mat4v2half None %64
%66 = OpLabel
%71 = OpAccessChain %_ptr_Uniform_S_std140 %u %uint_0 %uint_2
%74 = OpAccessChain %_ptr_Uniform_v2half %71 %uint_1
%75 = OpLoad %v2half %74
%77 = OpAccessChain %_ptr_Uniform_v2half %71 %uint_2
%78 = OpLoad %v2half %77
%81 = OpAccessChain %_ptr_Uniform_v2half %71 %uint_3
%82 = OpLoad %v2half %81
%84 = OpAccessChain %_ptr_Uniform_v2half %71 %uint_4
%85 = OpLoad %v2half %84
%86 = OpCompositeConstruct %mat4v2half %75 %78 %82 %85
OpReturnValue %86
OpFunctionEnd
%f = OpFunction %void None %87
%90 = OpLabel
%92 = OpAccessChain %_ptr_StorageBuffer__arr_S_uint_4 %s %uint_0
%95 = OpAccessChain %_ptr_Uniform__arr_S_std140_uint_4 %u %uint_0
%96 = OpLoad %_arr_S_std140_uint_4 %95
%93 = OpFunctionCall %_arr_S_uint_4 %conv_arr4_S %96
OpStore %92 %93
%99 = OpAccessChain %_ptr_StorageBuffer_S %s %uint_0 %int_1
%101 = OpAccessChain %_ptr_Uniform_S_std140 %u %uint_0 %uint_2
%102 = OpLoad %S_std140 %101
%100 = OpFunctionCall %S %conv_S %102
OpStore %99 %100
%105 = OpAccessChain %_ptr_StorageBuffer_mat4v2half %s %uint_0 %int_3 %uint_1
%106 = OpFunctionCall %mat4v2half %load_u_inner_2_m
OpStore %105 %106
%109 = OpAccessChain %_ptr_StorageBuffer_v2half %s %uint_0 %int_1 %uint_1 %107
%110 = OpAccessChain %_ptr_Uniform_v2half %u %uint_0 %38 %uint_2
%111 = OpLoad %v2half %110
%112 = OpVectorShuffle %v2half %111 %111 1 0
OpStore %109 %112
%assign_and_preserve_padding_1_s_X = OpFunction %void None %17
%dest = OpFunctionParameter %_arr_uint_uint_1
%value = OpFunctionParameter %S
%24 = OpLabel
%27 = OpCompositeExtract %uint %dest 0
%29 = OpAccessChain %_ptr_StorageBuffer_int %s %uint_0 %27 %uint_0
%30 = OpCompositeExtract %int %value 0
OpStore %29 %30
%31 = OpCompositeExtract %uint %dest 0
%33 = OpAccessChain %_ptr_StorageBuffer_mat4v2half %s %uint_0 %31 %uint_1
%34 = OpCompositeExtract %mat4v2half %value 1
OpStore %33 %34
%35 = OpCompositeExtract %uint %dest 0
%37 = OpAccessChain %_ptr_StorageBuffer_int %s %uint_0 %35 %uint_2
%38 = OpCompositeExtract %int %value 2
OpStore %37 %38
OpReturn
OpFunctionEnd
%assign_and_preserve_padding_s = OpFunction %void None %39
%value_0 = OpFunctionParameter %_arr_S_uint_4
%42 = OpLabel
%i = OpVariable %_ptr_Function_uint Function %43
%var_for_index = OpVariable %_ptr_Function__arr_S_uint_4 Function %58
OpStore %i %43
OpBranch %46
%46 = OpLabel
OpLoopMerge %47 %48 None
OpBranch %49
%49 = OpLabel
%51 = OpLoad %uint %i
%52 = OpULessThan %bool %51 %uint_4
%50 = OpLogicalNot %bool %52
OpSelectionMerge %54 None
OpBranchConditional %50 %55 %54
%55 = OpLabel
OpBranch %47
%54 = OpLabel
OpStore %var_for_index %value_0
%60 = OpLoad %uint %i
%61 = OpCompositeConstruct %_arr_uint_uint_1 %60
%62 = OpLoad %uint %i
%64 = OpAccessChain %_ptr_Function_S %var_for_index %62
%65 = OpLoad %S %64
%59 = OpFunctionCall %void %assign_and_preserve_padding_1_s_X %61 %65
OpBranch %48
%48 = OpLabel
%66 = OpLoad %uint %i
%67 = OpIAdd %uint %66 %uint_1
OpStore %i %67
OpBranch %46
%47 = OpLabel
OpReturn
OpFunctionEnd
%conv_S = OpFunction %S None %68
%val = OpFunctionParameter %S_std140
%71 = OpLabel
%72 = OpCompositeExtract %int %val 0
%73 = OpCompositeExtract %v2half %val 1
%74 = OpCompositeExtract %v2half %val 2
%75 = OpCompositeExtract %v2half %val 3
%76 = OpCompositeExtract %v2half %val 4
%77 = OpCompositeConstruct %mat4v2half %73 %74 %75 %76
%78 = OpCompositeExtract %int %val 5
%79 = OpCompositeConstruct %S %72 %77 %78
OpReturnValue %79
OpFunctionEnd
%conv_arr4_S = OpFunction %_arr_S_uint_4 None %80
%val_0 = OpFunctionParameter %_arr_S_std140_uint_4
%83 = OpLabel
%arr = OpVariable %_ptr_Function__arr_S_uint_4 Function %58
%i_0 = OpVariable %_ptr_Function_uint Function %43
%var_for_index_1 = OpVariable %_ptr_Function__arr_S_std140_uint_4 Function %97
OpBranch %86
%86 = OpLabel
OpLoopMerge %87 %88 None
OpBranch %89
%89 = OpLabel
%91 = OpLoad %uint %i_0
%92 = OpULessThan %bool %91 %uint_4
%90 = OpLogicalNot %bool %92
OpSelectionMerge %93 None
OpBranchConditional %90 %94 %93
%94 = OpLabel
OpBranch %87
%93 = OpLabel
OpStore %var_for_index_1 %val_0
%98 = OpLoad %uint %i_0
%99 = OpAccessChain %_ptr_Function_S %arr %98
%101 = OpLoad %uint %i_0
%103 = OpAccessChain %_ptr_Function_S_std140 %var_for_index_1 %101
%104 = OpLoad %S_std140 %103
%100 = OpFunctionCall %S %conv_S %104
OpStore %99 %100
OpBranch %88
%88 = OpLabel
%105 = OpLoad %uint %i_0
%106 = OpIAdd %uint %105 %uint_1
OpStore %i_0 %106
OpBranch %86
%87 = OpLabel
%107 = OpLoad %_arr_S_uint_4 %arr
OpReturnValue %107
OpFunctionEnd
%load_u_inner_2_m = OpFunction %mat4v2half None %108
%110 = OpLabel
%113 = OpAccessChain %_ptr_Uniform_S_std140 %u %uint_0 %uint_2
%116 = OpAccessChain %_ptr_Uniform_v2half %113 %uint_1
%117 = OpLoad %v2half %116
%119 = OpAccessChain %_ptr_Uniform_v2half %113 %uint_2
%120 = OpLoad %v2half %119
%123 = OpAccessChain %_ptr_Uniform_v2half %113 %uint_3
%124 = OpLoad %v2half %123
%126 = OpAccessChain %_ptr_Uniform_v2half %113 %uint_4
%127 = OpLoad %v2half %126
%128 = OpCompositeConstruct %mat4v2half %117 %120 %124 %127
OpReturnValue %128
OpFunctionEnd
%f = OpFunction %void None %129
%131 = OpLabel
%135 = OpAccessChain %_ptr_Uniform__arr_S_std140_uint_4 %u %uint_0
%136 = OpLoad %_arr_S_std140_uint_4 %135
%133 = OpFunctionCall %_arr_S_uint_4 %conv_arr4_S %136
%132 = OpFunctionCall %void %assign_and_preserve_padding_s %133
%140 = OpAccessChain %_ptr_Uniform_S_std140 %u %uint_0 %uint_2
%141 = OpLoad %S_std140 %140
%139 = OpFunctionCall %S %conv_S %141
%137 = OpFunctionCall %void %assign_and_preserve_padding_1_s_X %138 %139
%143 = OpAccessChain %_ptr_StorageBuffer_mat4v2half %s %uint_0 %int_3 %uint_1
%144 = OpFunctionCall %mat4v2half %load_u_inner_2_m
OpStore %143 %144
%147 = OpAccessChain %_ptr_StorageBuffer_v2half %s %uint_0 %int_1 %uint_1 %26
%148 = OpAccessChain %_ptr_Uniform_v2half %u %uint_0 %43 %uint_2
%149 = OpLoad %v2half %148
%150 = OpVectorShuffle %v2half %149 %149 1 0
OpStore %147 %150
OpReturn
OpFunctionEnd