1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/transform/single_entry_point.h"
16
17 #include <unordered_set>
18 #include <utility>
19
20 #include "src/program_builder.h"
21 #include "src/sem/function.h"
22 #include "src/sem/variable.h"
23
24 TINT_INSTANTIATE_TYPEINFO(tint::transform::SingleEntryPoint);
25 TINT_INSTANTIATE_TYPEINFO(tint::transform::SingleEntryPoint::Config);
26
27 namespace tint {
28 namespace transform {
29
30 SingleEntryPoint::SingleEntryPoint() = default;
31
32 SingleEntryPoint::~SingleEntryPoint() = default;
33
Run(CloneContext & ctx,const DataMap & inputs,DataMap &)34 void SingleEntryPoint::Run(CloneContext& ctx, const DataMap& inputs, DataMap&) {
35 auto* cfg = inputs.Get<Config>();
36 if (cfg == nullptr) {
37 ctx.dst->Diagnostics().add_error(
38 diag::System::Transform,
39 "missing transform data for " + std::string(TypeInfo().name));
40
41 return;
42 }
43
44 // Find the target entry point.
45 const ast::Function* entry_point = nullptr;
46 for (auto* f : ctx.src->AST().Functions()) {
47 if (!f->IsEntryPoint()) {
48 continue;
49 }
50 if (ctx.src->Symbols().NameFor(f->symbol) == cfg->entry_point_name) {
51 entry_point = f;
52 break;
53 }
54 }
55 if (entry_point == nullptr) {
56 ctx.dst->Diagnostics().add_error(
57 diag::System::Transform,
58 "entry point '" + cfg->entry_point_name + "' not found");
59 return;
60 }
61
62 auto& sem = ctx.src->Sem();
63
64 // Build set of referenced module-scope variables for faster lookups later.
65 std::unordered_set<const ast::Variable*> referenced_vars;
66 for (auto* var : sem.Get(entry_point)->TransitivelyReferencedGlobals()) {
67 referenced_vars.emplace(var->Declaration());
68 }
69
70 // Clone any module-scope variables, types, and functions that are statically
71 // referenced by the target entry point.
72 for (auto* decl : ctx.src->AST().GlobalDeclarations()) {
73 if (auto* ty = decl->As<ast::TypeDecl>()) {
74 // TODO(jrprice): Strip unused types.
75 ctx.dst->AST().AddTypeDecl(ctx.Clone(ty));
76 } else if (auto* var = decl->As<ast::Variable>()) {
77 if (referenced_vars.count(var)) {
78 if (var->is_const) {
79 if (auto* deco = ast::GetDecoration<ast::OverrideDecoration>(
80 var->decorations)) {
81 // It is an overridable constant
82 if (!deco->has_value) {
83 // If the decoration doesn't have numeric ID specified explicitly
84 // Make their ids explicitly assigned in the decoration so that
85 // they won't be affected by other stripped away constants
86 auto* global = sem.Get(var)->As<sem::GlobalVariable>();
87 const auto* new_deco =
88 ctx.dst->Override(deco->source, global->ConstantId());
89 ctx.Replace(deco, new_deco);
90 }
91 }
92 }
93 ctx.dst->AST().AddGlobalVariable(ctx.Clone(var));
94 }
95 } else if (auto* func = decl->As<ast::Function>()) {
96 if (sem.Get(func)->HasAncestorEntryPoint(entry_point->symbol)) {
97 ctx.dst->AST().AddFunction(ctx.Clone(func));
98 }
99 } else {
100 TINT_UNREACHABLE(Transform, ctx.dst->Diagnostics())
101 << "unhandled global declaration: " << decl->TypeInfo().name;
102 return;
103 }
104 }
105
106 // Clone the entry point.
107 ctx.dst->AST().AddFunction(ctx.Clone(entry_point));
108
109 // Retain the list of applied transforms.
110 // We need to do this manually since we are not going to use the top-level
111 // ctx.Clone() function.
112 ctx.dst->SetTransformApplied(ctx.src->TransformsApplied());
113 }
114
Config(std::string entry_point)115 SingleEntryPoint::Config::Config(std::string entry_point)
116 : entry_point_name(entry_point) {}
117
118 SingleEntryPoint::Config::Config(const Config&) = default;
119 SingleEntryPoint::Config::~Config() = default;
120 SingleEntryPoint::Config& SingleEntryPoint::Config::operator=(const Config&) =
121 default;
122
123 } // namespace transform
124 } // namespace tint
125