• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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/transform.h"
16 
17 #include <algorithm>
18 #include <string>
19 
20 #include "src/program_builder.h"
21 #include "src/sem/atomic_type.h"
22 #include "src/sem/block_statement.h"
23 #include "src/sem/depth_multisampled_texture_type.h"
24 #include "src/sem/for_loop_statement.h"
25 #include "src/sem/reference_type.h"
26 #include "src/sem/sampler_type.h"
27 
28 TINT_INSTANTIATE_TYPEINFO(tint::transform::Transform);
29 TINT_INSTANTIATE_TYPEINFO(tint::transform::Data);
30 
31 namespace tint {
32 namespace transform {
33 
34 Data::Data() = default;
35 Data::Data(const Data&) = default;
36 Data::~Data() = default;
37 Data& Data::operator=(const Data&) = default;
38 
39 DataMap::DataMap() = default;
40 DataMap::DataMap(DataMap&&) = default;
41 DataMap::~DataMap() = default;
42 DataMap& DataMap::operator=(DataMap&&) = default;
43 
44 Output::Output() = default;
Output(Program && p)45 Output::Output(Program&& p) : program(std::move(p)) {}
46 Transform::Transform() = default;
47 Transform::~Transform() = default;
48 
Run(const Program * program,const DataMap & data)49 Output Transform::Run(const Program* program, const DataMap& data /* = {} */) {
50   ProgramBuilder builder;
51   CloneContext ctx(&builder, program);
52   Output output;
53   Run(ctx, data, output.data);
54   builder.SetTransformApplied(this);
55   output.program = Program(std::move(builder));
56   return output;
57 }
58 
Run(CloneContext & ctx,const DataMap &,DataMap &)59 void Transform::Run(CloneContext& ctx, const DataMap&, DataMap&) {
60   TINT_UNIMPLEMENTED(Transform, ctx.dst->Diagnostics())
61       << "Transform::Run() unimplemented for " << TypeInfo().name;
62 }
63 
Requires(CloneContext & ctx,std::initializer_list<const::tint::TypeInfo * > deps)64 bool Transform::Requires(CloneContext& ctx,
65                          std::initializer_list<const ::tint::TypeInfo*> deps) {
66   for (auto* dep : deps) {
67     if (!ctx.src->HasTransformApplied(dep)) {
68       ctx.dst->Diagnostics().add_error(
69           diag::System::Transform, std::string(TypeInfo().name) +
70                                        " depends on " + std::string(dep->name) +
71                                        " but the dependency was not run");
72       return false;
73     }
74   }
75   return true;
76 }
77 
RemoveStatement(CloneContext & ctx,const ast::Statement * stmt)78 void Transform::RemoveStatement(CloneContext& ctx, const ast::Statement* stmt) {
79   auto* sem = ctx.src->Sem().Get(stmt);
80   if (auto* block = tint::As<sem::BlockStatement>(sem->Parent())) {
81     ctx.Remove(block->Declaration()->statements, stmt);
82     return;
83   }
84   if (tint::Is<sem::ForLoopStatement>(sem->Parent())) {
85     ctx.Replace(stmt, static_cast<ast::Expression*>(nullptr));
86     return;
87   }
88   TINT_ICE(Transform, ctx.dst->Diagnostics())
89       << "unable to remove statement from parent of type "
90       << sem->TypeInfo().name;
91 }
92 
CreateASTTypeFor(CloneContext & ctx,const sem::Type * ty)93 const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx,
94                                              const sem::Type* ty) {
95   if (ty->Is<sem::Void>()) {
96     return ctx.dst->create<ast::Void>();
97   }
98   if (ty->Is<sem::I32>()) {
99     return ctx.dst->create<ast::I32>();
100   }
101   if (ty->Is<sem::U32>()) {
102     return ctx.dst->create<ast::U32>();
103   }
104   if (ty->Is<sem::F32>()) {
105     return ctx.dst->create<ast::F32>();
106   }
107   if (ty->Is<sem::Bool>()) {
108     return ctx.dst->create<ast::Bool>();
109   }
110   if (auto* m = ty->As<sem::Matrix>()) {
111     auto* el = CreateASTTypeFor(ctx, m->type());
112     return ctx.dst->create<ast::Matrix>(el, m->rows(), m->columns());
113   }
114   if (auto* v = ty->As<sem::Vector>()) {
115     auto* el = CreateASTTypeFor(ctx, v->type());
116     return ctx.dst->create<ast::Vector>(el, v->Width());
117   }
118   if (auto* a = ty->As<sem::Array>()) {
119     auto* el = CreateASTTypeFor(ctx, a->ElemType());
120     ast::DecorationList decos;
121     if (!a->IsStrideImplicit()) {
122       decos.emplace_back(ctx.dst->create<ast::StrideDecoration>(a->Stride()));
123     }
124     if (a->IsRuntimeSized()) {
125       return ctx.dst->ty.array(el, nullptr, std::move(decos));
126     } else {
127       return ctx.dst->ty.array(el, a->Count(), std::move(decos));
128     }
129   }
130   if (auto* s = ty->As<sem::Struct>()) {
131     return ctx.dst->create<ast::TypeName>(ctx.Clone(s->Declaration()->name));
132   }
133   if (auto* s = ty->As<sem::Reference>()) {
134     return CreateASTTypeFor(ctx, s->StoreType());
135   }
136   if (auto* a = ty->As<sem::Atomic>()) {
137     return ctx.dst->create<ast::Atomic>(CreateASTTypeFor(ctx, a->Type()));
138   }
139   if (auto* t = ty->As<sem::DepthTexture>()) {
140     return ctx.dst->create<ast::DepthTexture>(t->dim());
141   }
142   if (auto* t = ty->As<sem::DepthMultisampledTexture>()) {
143     return ctx.dst->create<ast::DepthMultisampledTexture>(t->dim());
144   }
145   if (auto* t = ty->As<sem::MultisampledTexture>()) {
146     return ctx.dst->create<ast::MultisampledTexture>(
147         t->dim(), CreateASTTypeFor(ctx, t->type()));
148   }
149   if (auto* t = ty->As<sem::SampledTexture>()) {
150     return ctx.dst->create<ast::SampledTexture>(
151         t->dim(), CreateASTTypeFor(ctx, t->type()));
152   }
153   if (auto* t = ty->As<sem::StorageTexture>()) {
154     return ctx.dst->create<ast::StorageTexture>(
155         t->dim(), t->image_format(), CreateASTTypeFor(ctx, t->type()),
156         t->access());
157   }
158   if (auto* s = ty->As<sem::Sampler>()) {
159     return ctx.dst->create<ast::Sampler>(s->kind());
160   }
161   TINT_UNREACHABLE(Transform, ctx.dst->Diagnostics())
162       << "Unhandled type: " << ty->TypeInfo().name;
163   return nullptr;
164 }
165 
166 }  // namespace transform
167 }  // namespace tint
168