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

@@ -526,6 +526,9 @@ std::string AlignDeco(uint32_t value) {
// Test different types used as a struct member
TEST_P(ComputeLayoutMemoryBufferTests, StructMember) {
// TODO(crbug.com/dawn/1606): find out why these tests fail on Windows for OpenGL.
DAWN_TEST_UNSUPPORTED_IF(IsOpenGLES() && IsWindows());
// Sentinel value markers codes used to check that the start and end of
// structures are correctly aligned. Each of these codes are distinct and
// are not likely to be confused with data.
@@ -694,6 +697,9 @@ fn main() {
// Test different types that used directly as buffer type
TEST_P(ComputeLayoutMemoryBufferTests, NonStructMember) {
// TODO(crbug.com/dawn/1606): find out why these tests fail on Windows for OpenGL.
DAWN_TEST_UNSUPPORTED_IF(IsOpenGLES() && IsWindows());
auto params = GetParam();
Field& field = params.mField;

View File

@@ -529,6 +529,8 @@ libtint_source_set("libtint_core_all_src") {
"transform/packed_vec3.h",
"transform/pad_structs.cc",
"transform/pad_structs.h",
"transform/preserve_padding.cc",
"transform/preserve_padding.h",
"transform/promote_initializers_to_let.cc",
"transform/promote_initializers_to_let.h",
"transform/promote_side_effects_to_decl.cc",
@@ -1244,6 +1246,7 @@ if (tint_build_unittests) {
"transform/num_workgroups_from_uniform_test.cc",
"transform/packed_vec3_test.cc",
"transform/pad_structs_test.cc",
"transform/preserve_padding_test.cc",
"transform/promote_initializers_to_let_test.cc",
"transform/promote_side_effects_to_decl_test.cc",
"transform/remove_continue_in_switch_test.cc",

View File

@@ -453,6 +453,8 @@ list(APPEND TINT_LIB_SRCS
transform/packed_vec3.h
transform/pad_structs.cc
transform/pad_structs.h
transform/preserve_padding.cc
transform/preserve_padding.h
transform/promote_initializers_to_let.cc
transform/promote_initializers_to_let.h
transform/promote_side_effects_to_decl.cc
@@ -1212,6 +1214,7 @@ if(TINT_BUILD_TESTS)
transform/num_workgroups_from_uniform_test.cc
transform/packed_vec3_test.cc
transform/pad_structs_test.cc
transform/preserve_padding_test.cc
transform/promote_initializers_to_let_test.cc
transform/promote_side_effects_to_decl_test.cc
transform/remove_continue_in_switch_test.cc

View File

@@ -0,0 +1,226 @@
// Copyright 2022 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/transform/preserve_padding.h"
#include <unordered_set>
#include <utility>
#include "src/tint/program_builder.h"
#include "src/tint/sem/reference.h"
#include "src/tint/sem/struct.h"
#include "src/tint/utils/map.h"
#include "src/tint/utils/vector.h"
TINT_INSTANTIATE_TYPEINFO(tint::transform::PreservePadding);
using namespace tint::number_suffixes; // NOLINT
namespace tint::transform {
PreservePadding::PreservePadding() = default;
PreservePadding::~PreservePadding() = default;
/// The PIMPL state for the PreservePadding transform
struct PreservePadding::State {
/// Constructor
/// @param src the source Program
explicit State(const Program* src) : ctx{&b, src, /* auto_clone_symbols */ true} {}
/// The main function for the transform.
/// @returns the ApplyResult
ApplyResult Run() {
// Gather a list of assignments that need to be transformed.
std::unordered_set<const ast::AssignmentStatement*> assignments_to_transform;
for (auto* node : ctx.src->ASTNodes().Objects()) {
Switch(
node, //
[&](const ast::AssignmentStatement* assign) {
auto* ty = sem.Get(assign->lhs)->Type();
if (assign->lhs->Is<ast::PhonyExpression>()) {
// Ignore phony assignment.
return;
}
if (ty->As<sem::Reference>()->AddressSpace() != ast::AddressSpace::kStorage) {
// We only care about assignments that write to variables in the storage
// address space, as nothing else is host-visible.
return;
}
if (HasPadding(ty->UnwrapRef())) {
// The assigned type has padding bytes, so we need to decompose the writes.
assignments_to_transform.insert(assign);
}
},
[&](const ast::Enable* enable) {
// Check if the full pointer parameters extension is already enabled.
if (enable->extension ==
ast::Extension::kChromiumExperimentalFullPtrParameters) {
ext_enabled = true;
}
});
}
if (assignments_to_transform.empty()) {
return SkipTransform;
}
// Replace all assignments that include padding with decomposed versions.
ctx.ReplaceAll([&](const ast::AssignmentStatement* assign) -> const ast::Statement* {
if (!assignments_to_transform.count(assign)) {
return nullptr;
}
auto* ty = sem.Get(assign->lhs)->Type()->UnwrapRef();
return MakeAssignment(ty, ctx.Clone(assign->lhs), ctx.Clone(assign->rhs));
});
ctx.Clone();
return Program(std::move(b));
}
/// Create a statement that will perform the assignment `lhs = rhs`, creating and using helper
/// functions to decompose the assignment into element-wise copies if needed.
/// @param ty the type of the assignment
/// @param lhs the lhs expression (in the destination program)
/// @param rhs the rhs expression (in the destination program)
/// @returns the statement that performs the assignment
const ast::Statement* MakeAssignment(const sem::Type* ty,
const ast::Expression* lhs,
const ast::Expression* rhs) {
if (!HasPadding(ty)) {
// No padding - use a regular assignment.
return b.Assign(lhs, rhs);
}
// Call (and create if necessary) a helper function that assigns a composite using the
// statements in `body`. The helper will have the form:
// fn assign_helper_T(dest : ptr<storage, T, read_write>, value : T) {
// <body>
// }
// It will be called by passing a pointer to the original LHS:
// assign_helper_T(&lhs, rhs);
//
// Since this requires passing pointers to the storage address space, this will also enable
// the chromium_experimental_full_ptr_parameters extension.
constexpr const char* kDestParamName = "dest";
constexpr const char* kValueParamName = "value";
auto call_helper = [&](auto&& body) {
EnableExtension();
auto helper = helpers.GetOrCreate(ty, [&]() {
auto helper_name = b.Symbols().New("assign_and_preserve_padding");
utils::Vector<const ast::Parameter*, 2> params = {
b.Param(kDestParamName,
b.ty.pointer(CreateASTTypeFor(ctx, ty), ast::AddressSpace::kStorage,
ast::Access::kReadWrite)),
b.Param(kValueParamName, CreateASTTypeFor(ctx, ty)),
};
b.Func(helper_name, params, b.ty.void_(), body());
return helper_name;
});
return b.CallStmt(b.Call(helper, b.AddressOf(lhs), rhs));
};
return Switch(
ty, //
[&](const sem::Array* arr) {
// Call a helper function that uses a loop to assigns each element separately.
return call_helper([&]() {
utils::Vector<const ast::Statement*, 8> body;
auto* idx = b.Var("i", b.Expr(0_u));
body.Push(
b.For(b.Decl(idx), b.LessThan(idx, u32(arr->ConstantCount().value())),
b.Assign(idx, b.Add(idx, 1_u)),
b.Block(MakeAssignment(arr->ElemType(),
b.IndexAccessor(b.Deref(kDestParamName), idx),
b.IndexAccessor(kValueParamName, idx)))));
return body;
});
},
[&](const sem::Struct* str) {
// Call a helper function that assigns each member separately.
return call_helper([&]() {
utils::Vector<const ast::Statement*, 8> body;
for (auto member : str->Members()) {
auto name = sym.NameFor(member->Declaration()->symbol);
body.Push(MakeAssignment(member->Type(),
b.MemberAccessor(b.Deref(kDestParamName), name),
b.MemberAccessor(kValueParamName, name)));
}
return body;
});
},
[&](Default) {
TINT_ICE(Transform, b.Diagnostics()) << "unhandled type with padding";
return nullptr;
});
}
/// Checks if a type contains padding bytes.
/// @param ty the type to check
/// @returns true if `ty` (or any of its contained types) have padding bytes
bool HasPadding(const sem::Type* ty) {
return Switch(
ty, //
[&](const sem::Array* arr) {
auto* elem_ty = arr->ElemType();
if (elem_ty->Size() % elem_ty->Align() > 0) {
return true;
}
return HasPadding(elem_ty);
},
[&](const sem::Struct* str) {
uint32_t current_offset = 0;
for (auto* member : str->Members()) {
if (member->Offset() > current_offset) {
return true;
}
if (HasPadding(member->Type())) {
return true;
}
current_offset += member->Type()->Size();
}
return (current_offset < str->Size());
},
[&](Default) { return false; });
}
/// Enable the full pointer parameters extension, if we have not already done so.
void EnableExtension() {
if (!ext_enabled) {
b.Enable(ast::Extension::kChromiumExperimentalFullPtrParameters);
ext_enabled = true;
}
}
private:
/// The program builder
ProgramBuilder b;
/// The clone context
CloneContext ctx;
/// Alias to the semantic info in ctx.src
const sem::Info& sem = ctx.src->Sem();
/// Alias to the symbols in ctx.src
const SymbolTable& sym = ctx.src->Symbols();
/// Flag to track whether we have already enabled the full pointer parameters extension.
bool ext_enabled = false;
/// Map of semantic types to their assignment helper functions.
utils::Hashmap<const sem::Type*, Symbol, 8> helpers;
};
Transform::ApplyResult PreservePadding::Apply(const Program* program,
const DataMap&,
DataMap&) const {
return State(program).Run();
}
} // namespace tint::transform

View File

@@ -0,0 +1,47 @@
// Copyright 2022 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_TINT_TRANSFORM_PRESERVE_PADDING_H_
#define SRC_TINT_TRANSFORM_PRESERVE_PADDING_H_
#include "src/tint/transform/transform.h"
namespace tint::transform {
/// Decompose assignments of whole structure and array types to preserve padding bytes.
///
/// WGSL states that memory operations on structures and arrays will not access padding bytes. To
/// avoid overwriting padding bytes when writing to buffers, this transform decomposes those
/// assignments into element-wise assignments via helper functions.
///
/// @note Assumes that the DirectVariableTransform will be run afterwards for backends that need it.
class PreservePadding final : public Castable<PreservePadding, Transform> {
public:
/// Constructor
PreservePadding();
/// Destructor
~PreservePadding() override;
/// @copydoc Transform::Apply
ApplyResult Apply(const Program* program,
const DataMap& inputs,
DataMap& outputs) const override;
private:
struct State;
};
} // namespace tint::transform
#endif // SRC_TINT_TRANSFORM_PRESERVE_PADDING_H_

View File

@@ -0,0 +1,677 @@
// Copyright 2022 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/transform/preserve_padding.h"
#include <utility>
#include "src/tint/transform/test_helper.h"
namespace tint::transform {
namespace {
using PreservePaddingTest = TransformTest;
TEST_F(PreservePaddingTest, ShouldRun_EmptyModule) {
auto* src = R"()";
EXPECT_FALSE(ShouldRun<PreservePadding>(src));
}
TEST_F(PreservePaddingTest, ShouldRun_NonStructVec3) {
auto* src = R"(
@group(0) @binding(0) var<storage, read_write> v : vec3<u32>;
@compute @workgroup_size(1)
fn foo() {
v = vec3<u32>();
}
)";
EXPECT_FALSE(ShouldRun<PreservePadding>(src));
}
TEST_F(PreservePaddingTest, ShouldRun_StructWithoutPadding) {
auto* src = R"(
struct S {
a : u32,
b : u32,
c : u32,
d : u32,
e : vec3<u32>,
f : u32,
}
@group(0) @binding(0) var<storage, read_write> v : S;
@compute @workgroup_size(1)
fn foo() {
v = S();
}
)";
EXPECT_FALSE(ShouldRun<PreservePadding>(src));
}
TEST_F(PreservePaddingTest, ShouldRun_ArrayWithoutPadding) {
auto* src = R"(
@group(0) @binding(0) var<storage, read_write> v : array<vec4<u32>, 4>;
@compute @workgroup_size(1)
fn foo() {
v = array<vec4<u32>, 4>();
}
)";
EXPECT_FALSE(ShouldRun<PreservePadding>(src));
}
TEST_F(PreservePaddingTest, EmptyModule) {
auto* src = R"()";
auto* expect = src;
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, StructTrailingPadding) {
auto* src = R"(
struct S {
a : u32,
b : u32,
c : u32,
d : u32,
e : vec3<u32>,
}
@group(0) @binding(0) var<storage, read_write> v : S;
@compute @workgroup_size(1)
fn foo() {
v = S();
}
)";
auto* expect = R"(
enable chromium_experimental_full_ptr_parameters;
struct S {
a : u32,
b : u32,
c : u32,
d : u32,
e : vec3<u32>,
}
@group(0) @binding(0) var<storage, read_write> v : S;
fn assign_and_preserve_padding(dest : ptr<storage, S, read_write>, value : S) {
(*(dest)).a = value.a;
(*(dest)).b = value.b;
(*(dest)).c = value.c;
(*(dest)).d = value.d;
(*(dest)).e = value.e;
}
@compute @workgroup_size(1)
fn foo() {
assign_and_preserve_padding(&(v), S());
}
)";
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, StructInternalPadding) {
auto* src = R"(
struct S {
a : u32,
b : vec4<u32>,
}
@group(0) @binding(0) var<storage, read_write> v : S;
@compute @workgroup_size(1)
fn foo() {
v = S();
}
)";
auto* expect = R"(
enable chromium_experimental_full_ptr_parameters;
struct S {
a : u32,
b : vec4<u32>,
}
@group(0) @binding(0) var<storage, read_write> v : S;
fn assign_and_preserve_padding(dest : ptr<storage, S, read_write>, value : S) {
(*(dest)).a = value.a;
(*(dest)).b = value.b;
}
@compute @workgroup_size(1)
fn foo() {
assign_and_preserve_padding(&(v), S());
}
)";
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, StructExplicitSize_TrailingPadding) {
auto* src = R"(
struct S {
@size(16) a : u32,
}
@group(0) @binding(0) var<storage, read_write> v : S;
@compute @workgroup_size(1)
fn foo() {
v = S();
}
)";
auto* expect = R"(
enable chromium_experimental_full_ptr_parameters;
struct S {
@size(16)
a : u32,
}
@group(0) @binding(0) var<storage, read_write> v : S;
fn assign_and_preserve_padding(dest : ptr<storage, S, read_write>, value : S) {
(*(dest)).a = value.a;
}
@compute @workgroup_size(1)
fn foo() {
assign_and_preserve_padding(&(v), S());
}
)";
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, StructExplicitSize_InternalPadding) {
auto* src = R"(
struct S {
@size(16) a : u32,
b : u32,
}
@group(0) @binding(0) var<storage, read_write> v : S;
@compute @workgroup_size(1)
fn foo() {
v = S();
}
)";
auto* expect = R"(
enable chromium_experimental_full_ptr_parameters;
struct S {
@size(16)
a : u32,
b : u32,
}
@group(0) @binding(0) var<storage, read_write> v : S;
fn assign_and_preserve_padding(dest : ptr<storage, S, read_write>, value : S) {
(*(dest)).a = value.a;
(*(dest)).b = value.b;
}
@compute @workgroup_size(1)
fn foo() {
assign_and_preserve_padding(&(v), S());
}
)";
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, NestedStructs) {
auto* src = R"(
struct S1 {
a1 : u32,
b1 : vec3<u32>,
c1 : u32,
}
struct S2 {
a2 : u32,
b2 : S1,
c2 : S1,
}
struct S3 {
a3 : S1,
b3 : S2,
c3 : S2,
}
@group(0) @binding(0) var<storage, read_write> v : S3;
@compute @workgroup_size(1)
fn foo() {
v = S3();
}
)";
auto* expect = R"(
enable chromium_experimental_full_ptr_parameters;
struct S1 {
a1 : u32,
b1 : vec3<u32>,
c1 : u32,
}
struct S2 {
a2 : u32,
b2 : S1,
c2 : S1,
}
struct S3 {
a3 : S1,
b3 : S2,
c3 : S2,
}
@group(0) @binding(0) var<storage, read_write> v : S3;
fn assign_and_preserve_padding_1(dest : ptr<storage, S1, read_write>, value : S1) {
(*(dest)).a1 = value.a1;
(*(dest)).b1 = value.b1;
(*(dest)).c1 = value.c1;
}
fn assign_and_preserve_padding_2(dest : ptr<storage, S2, read_write>, value : S2) {
(*(dest)).a2 = value.a2;
assign_and_preserve_padding_1(&((*(dest)).b2), value.b2);
assign_and_preserve_padding_1(&((*(dest)).c2), value.c2);
}
fn assign_and_preserve_padding(dest : ptr<storage, S3, read_write>, value : S3) {
assign_and_preserve_padding_1(&((*(dest)).a3), value.a3);
assign_and_preserve_padding_2(&((*(dest)).b3), value.b3);
assign_and_preserve_padding_2(&((*(dest)).c3), value.c3);
}
@compute @workgroup_size(1)
fn foo() {
assign_and_preserve_padding(&(v), S3());
}
)";
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, ArrayOfVec3) {
auto* src = R"(
@group(0) @binding(0) var<storage, read_write> v : array<vec3<u32>, 4>;
@compute @workgroup_size(1)
fn foo() {
v = array<vec3<u32>, 4>();
}
)";
auto* expect = R"(
enable chromium_experimental_full_ptr_parameters;
@group(0) @binding(0) var<storage, read_write> v : array<vec3<u32>, 4>;
fn assign_and_preserve_padding(dest : ptr<storage, array<vec3<u32>, 4u>, read_write>, value : array<vec3<u32>, 4u>) {
for(var i = 0u; (i < 4u); i = (i + 1u)) {
(*(dest))[i] = value[i];
}
}
@compute @workgroup_size(1)
fn foo() {
assign_and_preserve_padding(&(v), array<vec3<u32>, 4>());
}
)";
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, ArrayOfArray) {
auto* src = R"(
type Array = array<array<vec3<u32>, 4>, 3>;
@group(0) @binding(0) var<storage, read_write> v : Array;
@compute @workgroup_size(1)
fn foo() {
v = Array();
}
)";
auto* expect = R"(
enable chromium_experimental_full_ptr_parameters;
type Array = array<array<vec3<u32>, 4>, 3>;
@group(0) @binding(0) var<storage, read_write> v : Array;
fn assign_and_preserve_padding_1(dest : ptr<storage, array<vec3<u32>, 4u>, read_write>, value : array<vec3<u32>, 4u>) {
for(var i = 0u; (i < 4u); i = (i + 1u)) {
(*(dest))[i] = value[i];
}
}
fn assign_and_preserve_padding(dest : ptr<storage, array<array<vec3<u32>, 4u>, 3u>, read_write>, value : array<array<vec3<u32>, 4u>, 3u>) {
for(var i = 0u; (i < 3u); i = (i + 1u)) {
assign_and_preserve_padding_1(&((*(dest))[i]), value[i]);
}
}
@compute @workgroup_size(1)
fn foo() {
assign_and_preserve_padding(&(v), Array());
}
)";
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, ArrayOfStructOfArray) {
auto* src = R"(
struct S {
a : u32,
b : array<vec3<u32>, 4>,
}
@group(0) @binding(0) var<storage, read_write> v : array<S, 3>;
@compute @workgroup_size(1)
fn foo() {
v = array<S, 3>();
}
)";
auto* expect = R"(
enable chromium_experimental_full_ptr_parameters;
struct S {
a : u32,
b : array<vec3<u32>, 4>,
}
@group(0) @binding(0) var<storage, read_write> v : array<S, 3>;
fn assign_and_preserve_padding_2(dest : ptr<storage, array<vec3<u32>, 4u>, read_write>, value : array<vec3<u32>, 4u>) {
for(var i = 0u; (i < 4u); i = (i + 1u)) {
(*(dest))[i] = value[i];
}
}
fn assign_and_preserve_padding_1(dest : ptr<storage, S, read_write>, value : S) {
(*(dest)).a = value.a;
assign_and_preserve_padding_2(&((*(dest)).b), value.b);
}
fn assign_and_preserve_padding(dest : ptr<storage, array<S, 3u>, read_write>, value : array<S, 3u>) {
for(var i = 0u; (i < 3u); i = (i + 1u)) {
assign_and_preserve_padding_1(&((*(dest))[i]), value[i]);
}
}
@compute @workgroup_size(1)
fn foo() {
assign_and_preserve_padding(&(v), array<S, 3>());
}
)";
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, NoModify_Vec3) {
auto* src = R"(
@group(0) @binding(0) var<storage, read_write> v : vec3<u32>;
@compute @workgroup_size(1)
fn foo() {
v = vec3<u32>();
}
)";
auto* expect = src;
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, AvoidDuplicateEnables) {
auto* src = R"(
enable chromium_experimental_full_ptr_parameters;
struct S {
@size(16) a : u32,
}
@group(0) @binding(0) var<storage, read_write> v : S;
@compute @workgroup_size(1)
fn foo() {
v = S();
}
)";
auto* expect = R"(
enable chromium_experimental_full_ptr_parameters;
struct S {
@size(16)
a : u32,
}
@group(0) @binding(0) var<storage, read_write> v : S;
fn assign_and_preserve_padding(dest : ptr<storage, S, read_write>, value : S) {
(*(dest)).a = value.a;
}
@compute @workgroup_size(1)
fn foo() {
assign_and_preserve_padding(&(v), S());
}
)";
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, NoModify_Mat3x3) {
auto* src = R"(
@group(0) @binding(0) var<storage, read_write> v : mat3x3<f32>;
@compute @workgroup_size(1)
fn foo() {
v = mat3x3<f32>();
}
)";
auto* expect = src;
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, NoModify_StructNoPadding) {
auto* src = R"(
struct S {
a : u32,
b : u32,
c : u32,
d : u32,
e : vec4<u32>,
}
@group(0) @binding(0) var<storage, read_write> v : S;
@compute @workgroup_size(1)
fn foo() {
v = S();
}
)";
auto* expect = src;
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, NoModify_ArrayNoPadding) {
auto* src = R"(
@group(0) @binding(0) var<storage, read_write> v : array<vec4<u32>, 4>;
@compute @workgroup_size(1)
fn foo() {
v = array<vec4<u32>, 4>();
}
)";
auto* expect = src;
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, NoModify_ArrayOfStructNoPadding) {
auto* src = R"(
struct S {
a : u32,
b : u32,
c : u32,
d : u32,
e : vec4<u32>,
}
@group(0) @binding(0) var<storage, read_write> v : array<S, 4>;
@compute @workgroup_size(1)
fn foo() {
v = array<S, 4>();
}
)";
auto* expect = src;
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, NoModify_Workgroup) {
auto* src = R"(
struct S {
a : u32,
b : vec3<u32>,
}
var<workgroup> v : S;
@compute @workgroup_size(1)
fn foo() {
v = S();
}
)";
auto* expect = src;
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, NoModify_Private) {
auto* src = R"(
struct S {
a : u32,
b : vec3<u32>,
}
var<private> v : S;
@compute @workgroup_size(1)
fn foo() {
v = S();
}
)";
auto* expect = src;
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(PreservePaddingTest, NoModify_Function) {
auto* src = R"(
struct S {
a : u32,
b : vec3<u32>,
}
@compute @workgroup_size(1)
fn foo() {
var<function> v : S;
v = S();
}
)";
auto* expect = src;
auto got = Run<PreservePadding>(src);
EXPECT_EQ(expect, str(got));
}
} // namespace
} // namespace tint::transform

View File

@@ -59,6 +59,7 @@
#include "src/tint/transform/expand_compound_assignment.h"
#include "src/tint/transform/manager.h"
#include "src/tint/transform/pad_structs.h"
#include "src/tint/transform/preserve_padding.h"
#include "src/tint/transform/promote_initializers_to_let.h"
#include "src/tint/transform/promote_side_effects_to_decl.h"
#include "src/tint/transform/remove_phonies.h"
@@ -210,6 +211,9 @@ SanitizedResult Sanitize(const Program* in,
manager.Add<transform::Renamer>();
data.Add<transform::Renamer::Config>(transform::Renamer::Target::kGlslKeywords,
/* preserve_unicode */ false);
manager.Add<transform::PreservePadding>(); // Must come before DirectVariableAccess
manager.Add<transform::Unshadow>(); // Must come before DirectVariableAccess
manager.Add<transform::DirectVariableAccess>();

View File

@@ -67,6 +67,7 @@
#include "src/tint/transform/manager.h"
#include "src/tint/transform/module_scope_var_to_entry_point_param.h"
#include "src/tint/transform/packed_vec3.h"
#include "src/tint/transform/preserve_padding.h"
#include "src/tint/transform/promote_initializers_to_let.h"
#include "src/tint/transform/promote_side_effects_to_decl.h"
#include "src/tint/transform/remove_phonies.h"
@@ -219,6 +220,8 @@ SanitizedResult Sanitize(const Program* in, const Options& options) {
}
manager.Add<transform::MultiplanarExternalTexture>();
manager.Add<transform::PreservePadding>();
manager.Add<transform::Unshadow>();
if (!options.disable_workgroup_init) {

View File

@@ -28,6 +28,7 @@
#include "src/tint/transform/for_loop_to_loop.h"
#include "src/tint/transform/manager.h"
#include "src/tint/transform/merge_return.h"
#include "src/tint/transform/preserve_padding.h"
#include "src/tint/transform/promote_side_effects_to_decl.h"
#include "src/tint/transform/remove_phonies.h"
#include "src/tint/transform/remove_unreachable_statements.h"
@@ -78,6 +79,8 @@ SanitizedResult Sanitize(const Program* in, const Options& options) {
}
manager.Add<transform::MultiplanarExternalTexture>();
manager.Add<transform::PreservePadding>(); // Must come before DirectVariableAccess
manager.Add<transform::Unshadow>(); // Must come before DirectVariableAccess
bool disable_workgroup_init_in_sanitizer =
options.disable_workgroup_init || options.use_zero_initialize_workgroup_memory_extension;