• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/remove_phonies.h"
16 
17 #include <memory>
18 #include <unordered_map>
19 #include <utility>
20 #include <vector>
21 
22 #include "src/ast/traverse_expressions.h"
23 #include "src/program_builder.h"
24 #include "src/sem/block_statement.h"
25 #include "src/sem/function.h"
26 #include "src/sem/statement.h"
27 #include "src/sem/variable.h"
28 #include "src/utils/map.h"
29 #include "src/utils/scoped_assignment.h"
30 
31 TINT_INSTANTIATE_TYPEINFO(tint::transform::RemovePhonies);
32 
33 namespace tint {
34 namespace transform {
35 namespace {
36 
37 struct SinkSignature {
38   std::vector<const sem::Type*> types;
39 
operator ==tint::transform::__anonb664b07f0111::SinkSignature40   bool operator==(const SinkSignature& other) const {
41     if (types.size() != other.types.size()) {
42       return false;
43     }
44     for (size_t i = 0; i < types.size(); i++) {
45       if (types[i] != other.types[i]) {
46         return false;
47       }
48     }
49     return true;
50   }
51 
52   struct Hasher {
53     /// @param sig the CallTargetSignature to hash
54     /// @return the hash value
operator ()tint::transform::__anonb664b07f0111::SinkSignature::Hasher55     std::size_t operator()(const SinkSignature& sig) const {
56       size_t hash = tint::utils::Hash(sig.types.size());
57       for (auto* ty : sig.types) {
58         tint::utils::HashCombine(&hash, ty);
59       }
60       return hash;
61     }
62   };
63 };
64 
65 }  // namespace
66 
67 RemovePhonies::RemovePhonies() = default;
68 
69 RemovePhonies::~RemovePhonies() = default;
70 
Run(CloneContext & ctx,const DataMap &,DataMap &)71 void RemovePhonies::Run(CloneContext& ctx, const DataMap&, DataMap&) {
72   auto& sem = ctx.src->Sem();
73 
74   std::unordered_map<SinkSignature, Symbol, SinkSignature::Hasher> sinks;
75 
76   for (auto* node : ctx.src->ASTNodes().Objects()) {
77     if (auto* stmt = node->As<ast::AssignmentStatement>()) {
78       if (stmt->lhs->Is<ast::PhonyExpression>()) {
79         std::vector<const ast::Expression*> side_effects;
80         if (!ast::TraverseExpressions(
81                 stmt->rhs, ctx.dst->Diagnostics(),
82                 [&](const ast::CallExpression* call) {
83                   // ast::CallExpression may map to a function or intrinsic call
84                   // (both may have side-effects), or a type constructor or
85                   // type conversion (both do not have side effects).
86                   if (sem.Get(call)
87                           ->Target()
88                           ->IsAnyOf<sem::Function, sem::Intrinsic>()) {
89                     side_effects.push_back(call);
90                     return ast::TraverseAction::Skip;
91                   }
92                   return ast::TraverseAction::Descend;
93                 })) {
94           return;
95         }
96 
97         if (side_effects.empty()) {
98           // Phony assignment with no side effects.
99           // Just remove it.
100           RemoveStatement(ctx, stmt);
101           continue;
102         }
103 
104         if (side_effects.size() == 1) {
105           if (auto* call = side_effects[0]->As<ast::CallExpression>()) {
106             // Phony assignment with single call side effect.
107             // Replace phony assignment with call.
108             ctx.Replace(
109                 stmt, [&, call] { return ctx.dst->CallStmt(ctx.Clone(call)); });
110             continue;
111           }
112         }
113 
114         // Phony assignment with multiple side effects.
115         // Generate a call to a dummy function with the side effects as
116         // arguments.
117         ctx.Replace(stmt, [&, side_effects] {
118           SinkSignature sig;
119           for (auto* arg : side_effects) {
120             sig.types.push_back(sem.Get(arg)->Type()->UnwrapRef());
121           }
122           auto sink = utils::GetOrCreate(sinks, sig, [&] {
123             auto name = ctx.dst->Symbols().New("phony_sink");
124             ast::VariableList params;
125             for (auto* ty : sig.types) {
126               auto* ast_ty = CreateASTTypeFor(ctx, ty);
127               params.push_back(
128                   ctx.dst->Param("p" + std::to_string(params.size()), ast_ty));
129             }
130             ctx.dst->Func(name, params, ctx.dst->ty.void_(), {});
131             return name;
132           });
133           ast::ExpressionList args;
134           for (auto* arg : side_effects) {
135             args.push_back(ctx.Clone(arg));
136           }
137           return ctx.dst->CallStmt(ctx.dst->Call(sink, args));
138         });
139       }
140     }
141   }
142 
143   ctx.Clone();
144 }
145 
146 }  // namespace transform
147 }  // namespace tint
148