Add transform to strip entry points from a module

Remove the Generator::GenerateEntryPoint() APIs as they were mostly
unimplemented and not used by anything except the Tint sample app,
which now uses the new transform.

Change-Id: I1ccb303d6c3aa15e622c193d33b753e22bf39a95
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/49160
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: James Price <jrprice@google.com>
This commit is contained in:
James Price
2021-04-28 15:33:03 +00:00
committed by Commit Bot service account
parent f5f311e264
commit 0949bdf68f
20 changed files with 577 additions and 258 deletions

View File

@@ -0,0 +1,105 @@
// 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/single_entry_point.h"
#include <unordered_set>
#include <utility>
#include "src/program_builder.h"
#include "src/sem/function.h"
#include "src/sem/variable.h"
TINT_INSTANTIATE_TYPEINFO(tint::transform::SingleEntryPoint::Config);
namespace tint {
namespace transform {
SingleEntryPoint::SingleEntryPoint() = default;
SingleEntryPoint::~SingleEntryPoint() = default;
Output SingleEntryPoint::Run(const Program* in, const DataMap& data) {
ProgramBuilder out;
auto* cfg = data.Get<Config>();
if (cfg == nullptr) {
out.Diagnostics().add_error("missing transform data for SingleEntryPoint");
return Output(Program(std::move(out)));
}
// Find the target entry point.
ast::Function* entry_point = nullptr;
for (auto* f : in->AST().Functions()) {
if (!f->IsEntryPoint()) {
continue;
}
if (in->Symbols().NameFor(f->symbol()) == cfg->entry_point_name) {
entry_point = f;
break;
}
}
if (entry_point == nullptr) {
out.Diagnostics().add_error("entry point '" + cfg->entry_point_name +
"' not found");
return Output(Program(std::move(out)));
}
CloneContext ctx(&out, in);
auto* sem = in->Sem().Get(entry_point);
// Build set of referenced module-scope variables for faster lookups later.
std::unordered_set<const ast::Variable*> referenced_vars;
for (auto* var : sem->ReferencedModuleVariables()) {
referenced_vars.emplace(var->Declaration());
}
// Clone any module-scope variables, types, and functions that are statically
// referenced by the target entry point.
for (auto* decl : in->AST().GlobalDeclarations()) {
if (auto* ty = decl->As<sem::Type>()) {
// TODO(jrprice): Strip unused types.
out.AST().AddConstructedType(ctx.Clone(ty));
} else if (auto* var = decl->As<ast::Variable>()) {
if (var->is_const() || referenced_vars.count(var)) {
out.AST().AddGlobalVariable(ctx.Clone(var));
}
} else if (auto* func = decl->As<ast::Function>()) {
if (in->Sem().Get(func)->HasAncestorEntryPoint(entry_point->symbol())) {
out.AST().AddFunction(ctx.Clone(func));
}
} else {
TINT_UNREACHABLE(out.Diagnostics())
<< "unhandled global declaration: " << decl->TypeInfo().name;
return Output(Program(std::move(out)));
}
}
// Clone the entry point.
out.AST().AddFunction(ctx.Clone(entry_point));
return Output(Program(std::move(out)));
}
SingleEntryPoint::Config::Config(std::string entry_point)
: entry_point_name(entry_point) {}
SingleEntryPoint::Config::Config(const Config&) = default;
SingleEntryPoint::Config::~Config() = default;
SingleEntryPoint::Config& SingleEntryPoint::Config::operator=(const Config&) =
default;
} // namespace transform
} // namespace tint

View File

@@ -0,0 +1,67 @@
// 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_SINGLE_ENTRY_POINT_H_
#define SRC_TRANSFORM_SINGLE_ENTRY_POINT_H_
#include <string>
#include "src/transform/transform.h"
namespace tint {
namespace transform {
/// Strip all but one entry point a module.
///
/// All module-scope variables, types, and functions that are not used by the
/// target entry point will also be removed.
class SingleEntryPoint : public Transform {
public:
/// Configuration options for the transform
struct Config : public Castable<Config, Data> {
/// Constructor
/// @param entry_point the name of the entry point to keep
explicit Config(std::string entry_point = "");
/// Copy constructor
Config(const Config&);
/// Destructor
~Config() override;
/// Assignment operator
/// @returns this Config
Config& operator=(const Config&);
/// The name of the entry point to keep.
std::string entry_point_name;
};
/// Constructor
SingleEntryPoint();
/// Destructor
~SingleEntryPoint() 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_SINGLE_ENTRY_POINT_H_

View File

@@ -0,0 +1,385 @@
// 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/single_entry_point.h"
#include <utility>
#include "src/transform/test_helper.h"
namespace tint {
namespace transform {
namespace {
using SingleEntryPointTest = TransformTest;
TEST_F(SingleEntryPointTest, Error_MissingTransformData) {
auto* src = "";
auto* expect = "error: missing transform data for SingleEntryPoint";
auto got = Run<SingleEntryPoint>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(SingleEntryPointTest, Error_NoEntryPoints) {
auto* src = "";
auto* expect = "error: entry point 'main' not found";
DataMap data;
data.Add<SingleEntryPoint::Config>("main");
auto got = Run<SingleEntryPoint>(src, data);
EXPECT_EQ(expect, str(got));
}
TEST_F(SingleEntryPointTest, Error_InvalidEntryPoint) {
auto* src = R"(
[[stage(vertex)]]
fn main() -> [[builtin(position)]] vec4<f32> {
return vec4<f32>();
}
)";
auto* expect = "error: entry point '_' not found";
SingleEntryPoint::Config cfg("_");
DataMap data;
data.Add<SingleEntryPoint::Config>(cfg);
auto got = Run<SingleEntryPoint>(src, data);
EXPECT_EQ(expect, str(got));
}
TEST_F(SingleEntryPointTest, Error_NotAnEntryPoint) {
auto* src = R"(
fn foo() {}
[[stage(fragment)]]
fn main() {}
)";
auto* expect = "error: entry point 'foo' not found";
SingleEntryPoint::Config cfg("foo");
DataMap data;
data.Add<SingleEntryPoint::Config>(cfg);
auto got = Run<SingleEntryPoint>(src, data);
EXPECT_EQ(expect, str(got));
}
TEST_F(SingleEntryPointTest, SingleEntryPoint) {
auto* src = R"(
[[stage(compute)]]
fn main() {
}
)";
SingleEntryPoint::Config cfg("main");
DataMap data;
data.Add<SingleEntryPoint::Config>(cfg);
auto got = Run<SingleEntryPoint>(src, data);
EXPECT_EQ(src, str(got));
}
TEST_F(SingleEntryPointTest, MultipleEntryPoints) {
auto* src = R"(
[[stage(vertex)]]
fn vert_main() {
}
[[stage(fragment)]]
fn frag_main() {
}
[[stage(compute)]]
fn comp_main1() {
}
[[stage(compute)]]
fn comp_main2() {
}
)";
auto* expect = R"(
[[stage(compute)]]
fn comp_main1() {
}
)";
SingleEntryPoint::Config cfg("comp_main1");
DataMap data;
data.Add<SingleEntryPoint::Config>(cfg);
auto got = Run<SingleEntryPoint>(src, data);
EXPECT_EQ(expect, str(got));
}
TEST_F(SingleEntryPointTest, GlobalVariables) {
auto* src = R"(
var<private> a : f32;
var<private> b : f32;
var<private> c : f32;
var<private> d : f32;
[[stage(vertex)]]
fn vert_main() {
a = 0.0;
}
[[stage(fragment)]]
fn frag_main() {
b = 0.0;
}
[[stage(compute)]]
fn comp_main1() {
c = 0.0;
}
[[stage(compute)]]
fn comp_main2() {
d = 0.0;
}
)";
auto* expect = R"(
var<private> c : f32;
[[stage(compute)]]
fn comp_main1() {
c = 0.0;
}
)";
SingleEntryPoint::Config cfg("comp_main1");
DataMap data;
data.Add<SingleEntryPoint::Config>(cfg);
auto got = Run<SingleEntryPoint>(src, data);
EXPECT_EQ(expect, str(got));
}
TEST_F(SingleEntryPointTest, GlobalConstants) {
auto* src = R"(
let a : f32 = 1.0;
let b : f32 = 1.0;
let c : f32 = 1.0;
let d : f32 = 1.0;
[[stage(vertex)]]
fn vert_main() {
let local_a : f32 = a;
}
[[stage(fragment)]]
fn frag_main() {
let local_b : f32 = b;
}
[[stage(compute)]]
fn comp_main1() {
let local_c : f32 = c;
}
[[stage(compute)]]
fn comp_main2() {
let local_d : f32 = d;
}
)";
auto* expect = R"(
let a : f32 = 1.0;
let b : f32 = 1.0;
let c : f32 = 1.0;
let d : f32 = 1.0;
[[stage(compute)]]
fn comp_main1() {
let local_c : f32 = c;
}
)";
SingleEntryPoint::Config cfg("comp_main1");
DataMap data;
data.Add<SingleEntryPoint::Config>(cfg);
auto got = Run<SingleEntryPoint>(src, data);
EXPECT_EQ(expect, str(got));
}
TEST_F(SingleEntryPointTest, CalledFunctions) {
auto* src = R"(
fn inner1() {
}
fn inner2() {
}
fn inner_shared() {
}
fn outer1() {
inner1();
inner_shared();
}
fn outer2() {
inner2();
inner_shared();
}
[[stage(compute)]]
fn comp_main1() {
outer1();
}
[[stage(compute)]]
fn comp_main2() {
outer2();
}
)";
auto* expect = R"(
fn inner1() {
}
fn inner_shared() {
}
fn outer1() {
inner1();
inner_shared();
}
[[stage(compute)]]
fn comp_main1() {
outer1();
}
)";
SingleEntryPoint::Config cfg("comp_main1");
DataMap data;
data.Add<SingleEntryPoint::Config>(cfg);
auto got = Run<SingleEntryPoint>(src, data);
EXPECT_EQ(expect, str(got));
}
TEST_F(SingleEntryPointTest, GlobalsReferencedByCalledFunctions) {
auto* src = R"(
var<private> inner1_var : f32;
var<private> inner2_var : f32;
var<private> inner_shared_var : f32;
var<private> outer1_var : f32;
var<private> outer2_var : f32;
fn inner1() {
inner1_var = 0.0;
}
fn inner2() {
inner2_var = 0.0;
}
fn inner_shared() {
inner_shared_var = 0.0;
}
fn outer1() {
inner1();
inner_shared();
outer1_var = 0.0;
}
fn outer2() {
inner2();
inner_shared();
outer2_var = 0.0;
}
[[stage(compute)]]
fn comp_main1() {
outer1();
}
[[stage(compute)]]
fn comp_main2() {
outer2();
}
)";
auto* expect = R"(
var<private> inner1_var : f32;
var<private> inner_shared_var : f32;
var<private> outer1_var : f32;
fn inner1() {
inner1_var = 0.0;
}
fn inner_shared() {
inner_shared_var = 0.0;
}
fn outer1() {
inner1();
inner_shared();
outer1_var = 0.0;
}
[[stage(compute)]]
fn comp_main1() {
outer1();
}
)";
SingleEntryPoint::Config cfg("comp_main1");
DataMap data;
data.Add<SingleEntryPoint::Config>(cfg);
auto got = Run<SingleEntryPoint>(src, data);
EXPECT_EQ(expect, str(got));
}
} // namespace
} // namespace transform
} // namespace tint