mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-08 13:14:56 +00:00
Add transform::Simplify
Performs basic peephole optimizations on the AST. Use in transform::Hlsl. Required to have the DecomposeStorageAccess transform operate correctly with the output of InlinePointerLets transform, specifically when declaring `let` pointer expressions to storage buffers. Fixed: tint:221 Fixed: tint:492 Fixed: tint:829 Change-Id: I536390921a6492378104e9c3c100d9e761294a27 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51921 Commit-Queue: Ben Clayton <bclayton@chromium.org> Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
committed by
Tint LUCI CQ
parent
7b0c5e880e
commit
ed86bf99b0
@@ -519,6 +519,8 @@ libtint_source_set("libtint_core_all_src") {
|
||||
"transform/manager.h",
|
||||
"transform/renamer.cc",
|
||||
"transform/renamer.h",
|
||||
"transform/simplify.cc",
|
||||
"transform/simplify.h",
|
||||
"transform/single_entry_point.cc",
|
||||
"transform/single_entry_point.h",
|
||||
"transform/transform.cc",
|
||||
|
||||
@@ -282,6 +282,8 @@ set(TINT_LIB_SRCS
|
||||
transform/manager.h
|
||||
transform/renamer.cc
|
||||
transform/renamer.h
|
||||
transform/simplify.cc
|
||||
transform/simplify.h
|
||||
transform/single_entry_point.cc
|
||||
transform/single_entry_point.h
|
||||
transform/transform.cc
|
||||
@@ -810,7 +812,8 @@ if(${TINT_BUILD_TESTS})
|
||||
transform/first_index_offset_test.cc
|
||||
transform/inline_pointer_lets_test.cc
|
||||
transform/renamer_test.cc
|
||||
transform/single_entry_point.cc
|
||||
transform/simplify_test.cc
|
||||
transform/single_entry_point_test.cc
|
||||
transform/test_helper.h
|
||||
transform/var_for_dynamic_index_test.cc
|
||||
transform/vertex_pulling_test.cc
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "src/transform/external_texture_transform.h"
|
||||
#include "src/transform/inline_pointer_lets.h"
|
||||
#include "src/transform/manager.h"
|
||||
#include "src/transform/simplify.h"
|
||||
|
||||
namespace tint {
|
||||
namespace transform {
|
||||
@@ -39,10 +40,16 @@ Hlsl::~Hlsl() = default;
|
||||
Output Hlsl::Run(const Program* in, const DataMap& data) {
|
||||
Manager manager;
|
||||
manager.Add<CanonicalizeEntryPointIO>();
|
||||
manager.Add<InlinePointerLets>();
|
||||
// Simplify cleans up messy `*(&(expr))` expressions from InlinePointerLets.
|
||||
manager.Add<Simplify>();
|
||||
// DecomposeStorageAccess must come after InlinePointerLets as we cannot take
|
||||
// the address of calls to DecomposeStorageAccess::Intrinsic. Must also come
|
||||
// after Simplify, as we need to fold away the address-of and defers of
|
||||
// `*(&(intrinsic_load()))` expressions.
|
||||
manager.Add<DecomposeStorageAccess>();
|
||||
manager.Add<CalculateArrayLength>();
|
||||
manager.Add<ExternalTextureTransform>();
|
||||
manager.Add<InlinePointerLets>();
|
||||
auto out = manager.Run(in, data);
|
||||
if (!out.program.IsValid()) {
|
||||
return out;
|
||||
|
||||
63
src/transform/simplify.cc
Normal file
63
src/transform/simplify.cc
Normal file
@@ -0,0 +1,63 @@
|
||||
// Copyright 2021 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/transform/simplify.h"
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#include "src/program_builder.h"
|
||||
#include "src/sem/block_statement.h"
|
||||
#include "src/sem/function.h"
|
||||
#include "src/sem/statement.h"
|
||||
#include "src/sem/variable.h"
|
||||
#include "src/utils/scoped_assignment.h"
|
||||
|
||||
namespace tint {
|
||||
namespace transform {
|
||||
|
||||
Simplify::Simplify() = default;
|
||||
|
||||
Simplify::~Simplify() = default;
|
||||
|
||||
Output Simplify::Run(const Program* in, const DataMap&) {
|
||||
ProgramBuilder out;
|
||||
CloneContext ctx(&out, in);
|
||||
|
||||
ctx.ReplaceAll([&](ast::Expression* expr) -> ast::Expression* {
|
||||
if (auto* outer = expr->As<ast::UnaryOpExpression>()) {
|
||||
if (auto* inner = outer->expr()->As<ast::UnaryOpExpression>()) {
|
||||
if (outer->op() == ast::UnaryOp::kAddressOf &&
|
||||
inner->op() == ast::UnaryOp::kIndirection) {
|
||||
// &(*(expr)) => expr
|
||||
return ctx.Clone(inner->expr());
|
||||
}
|
||||
if (outer->op() == ast::UnaryOp::kIndirection &&
|
||||
inner->op() == ast::UnaryOp::kAddressOf) {
|
||||
// *(&(expr)) => expr
|
||||
return ctx.Clone(inner->expr());
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
});
|
||||
|
||||
ctx.Clone();
|
||||
|
||||
return Output(Program(std::move(out)));
|
||||
}
|
||||
|
||||
} // namespace transform
|
||||
} // namespace tint
|
||||
49
src/transform/simplify.h
Normal file
49
src/transform/simplify.h
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright 2021 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_TRANSFORM_SIMPLIFY_H_
|
||||
#define SRC_TRANSFORM_SIMPLIFY_H_
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "src/transform/transform.h"
|
||||
|
||||
namespace tint {
|
||||
namespace transform {
|
||||
|
||||
/// Simplify is a peephole optimizer Transform that simplifies ASTs by removing
|
||||
/// unnecessary operations.
|
||||
/// Simplify currently optimizes the following:
|
||||
/// `&(*(expr))` => `expr`
|
||||
/// `*(&(expr))` => `expr`
|
||||
class Simplify : public Transform {
|
||||
public:
|
||||
/// Constructor
|
||||
Simplify();
|
||||
|
||||
/// Destructor
|
||||
~Simplify() override;
|
||||
|
||||
/// Runs the transform on `program`, returning the transformation result.
|
||||
/// @param program the source program to transform
|
||||
/// @param data optional extra transform-specific input data
|
||||
/// @returns the transformation result
|
||||
Output Run(const Program* program, const DataMap& data = {}) override;
|
||||
};
|
||||
|
||||
} // namespace transform
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_TRANSFORM_SIMPLIFY_H_
|
||||
112
src/transform/simplify_test.cc
Normal file
112
src/transform/simplify_test.cc
Normal file
@@ -0,0 +1,112 @@
|
||||
// Copyright 2021 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/transform/simplify.h"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "src/transform/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace transform {
|
||||
namespace {
|
||||
|
||||
using SimplifyTest = TransformTest;
|
||||
|
||||
TEST_F(SimplifyTest, EmptyModule) {
|
||||
auto* src = "";
|
||||
auto* expect = "";
|
||||
|
||||
auto got = Run<Simplify>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
TEST_F(SimplifyTest, AddressOfDeref) {
|
||||
auto* src = R"(
|
||||
fn f() {
|
||||
var v : i32;
|
||||
let p : ptr<function, i32> = &(v);
|
||||
let x : ptr<function, i32> = &(*(p));
|
||||
let y : ptr<function, i32> = &(*(&(*(p))));
|
||||
let z : ptr<function, i32> = &(*(&(*(&(*(&(*(p))))))));
|
||||
}
|
||||
)";
|
||||
|
||||
auto* expect = R"(
|
||||
fn f() {
|
||||
var v : i32;
|
||||
let p : ptr<function, i32> = &(v);
|
||||
let x : ptr<function, i32> = p;
|
||||
let y : ptr<function, i32> = p;
|
||||
let z : ptr<function, i32> = p;
|
||||
}
|
||||
)";
|
||||
|
||||
auto got = Run<Simplify>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
TEST_F(SimplifyTest, DerefAddressOf) {
|
||||
auto* src = R"(
|
||||
fn f() {
|
||||
var v : i32;
|
||||
let x : i32 = *(&(v));
|
||||
let y : i32 = *(&(*(&(v))));
|
||||
let z : i32 = *(&(*(&(*(&(*(&(v))))))));
|
||||
}
|
||||
)";
|
||||
|
||||
auto* expect = R"(
|
||||
fn f() {
|
||||
var v : i32;
|
||||
let x : i32 = v;
|
||||
let y : i32 = v;
|
||||
let z : i32 = v;
|
||||
}
|
||||
)";
|
||||
|
||||
auto got = Run<Simplify>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
TEST_F(SimplifyTest, NoChange) {
|
||||
auto* src = R"(
|
||||
fn f() {
|
||||
var v : i32;
|
||||
let p : ptr<function, i32> = &(v);
|
||||
let x : i32 = *(p);
|
||||
}
|
||||
)";
|
||||
|
||||
auto* expect = R"(
|
||||
fn f() {
|
||||
var v : i32;
|
||||
let p : ptr<function, i32> = &(v);
|
||||
let x : i32 = *(p);
|
||||
}
|
||||
)";
|
||||
|
||||
auto got = Run<Simplify>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace transform
|
||||
} // namespace tint
|
||||
Reference in New Issue
Block a user