blob: e3d0ea94754a24f5934cf5459f02b0446edb6679 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// 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/tint/transform/transform.h"
16
17#include <algorithm>
18#include <string>
19
20#include "src/tint/program_builder.h"
Ben Clayton01004b72022-04-28 18:49:04 +000021#include "src/tint/sem/atomic.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000022#include "src/tint/sem/block_statement.h"
Ben Clayton01004b72022-04-28 18:49:04 +000023#include "src/tint/sem/depth_multisampled_texture.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000024#include "src/tint/sem/for_loop_statement.h"
Ben Clayton01004b72022-04-28 18:49:04 +000025#include "src/tint/sem/reference.h"
26#include "src/tint/sem/sampler.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
28TINT_INSTANTIATE_TYPEINFO(tint::transform::Transform);
29TINT_INSTANTIATE_TYPEINFO(tint::transform::Data);
30
dan sinclairb5599d32022-04-07 16:55:14 +000031namespace tint::transform {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000032
33Data::Data() = default;
34Data::Data(const Data&) = default;
35Data::~Data() = default;
36Data& Data::operator=(const Data&) = default;
37
38DataMap::DataMap() = default;
39DataMap::DataMap(DataMap&&) = default;
40DataMap::~DataMap() = default;
41DataMap& DataMap::operator=(DataMap&&) = default;
42
43Output::Output() = default;
44Output::Output(Program&& p) : program(std::move(p)) {}
45Transform::Transform() = default;
46Transform::~Transform() = default;
47
dan sinclair41e4d9a2022-05-01 14:40:55 +000048Output Transform::Run(const Program* program, const DataMap& data /* = {} */) const {
49 ProgramBuilder builder;
50 CloneContext ctx(&builder, program);
51 Output output;
52 Run(ctx, data, output.data);
53 output.program = Program(std::move(builder));
54 return output;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000055}
56
57void Transform::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000058 TINT_UNIMPLEMENTED(Transform, ctx.dst->Diagnostics())
59 << "Transform::Run() unimplemented for " << TypeInfo().name;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000060}
61
62bool Transform::ShouldRun(const Program*, const DataMap&) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000063 return true;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000064}
65
66void Transform::RemoveStatement(CloneContext& ctx, const ast::Statement* stmt) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000067 auto* sem = ctx.src->Sem().Get(stmt);
68 if (auto* block = tint::As<sem::BlockStatement>(sem->Parent())) {
69 ctx.Remove(block->Declaration()->statements, stmt);
70 return;
71 }
72 if (tint::Is<sem::ForLoopStatement>(sem->Parent())) {
73 ctx.Replace(stmt, static_cast<ast::Expression*>(nullptr));
74 return;
75 }
76 TINT_ICE(Transform, ctx.dst->Diagnostics())
77 << "unable to remove statement from parent of type " << sem->TypeInfo().name;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000078}
79
dan sinclair41e4d9a2022-05-01 14:40:55 +000080const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, const sem::Type* ty) {
81 if (ty->Is<sem::Void>()) {
82 return ctx.dst->create<ast::Void>();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000083 }
dan sinclair41e4d9a2022-05-01 14:40:55 +000084 if (ty->Is<sem::I32>()) {
85 return ctx.dst->create<ast::I32>();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000086 }
dan sinclair41e4d9a2022-05-01 14:40:55 +000087 if (ty->Is<sem::U32>()) {
88 return ctx.dst->create<ast::U32>();
89 }
90 if (ty->Is<sem::F32>()) {
91 return ctx.dst->create<ast::F32>();
92 }
93 if (ty->Is<sem::Bool>()) {
94 return ctx.dst->create<ast::Bool>();
95 }
96 if (auto* m = ty->As<sem::Matrix>()) {
97 auto* el = CreateASTTypeFor(ctx, m->type());
98 return ctx.dst->create<ast::Matrix>(el, m->rows(), m->columns());
99 }
100 if (auto* v = ty->As<sem::Vector>()) {
101 auto* el = CreateASTTypeFor(ctx, v->type());
102 return ctx.dst->create<ast::Vector>(el, v->Width());
103 }
104 if (auto* a = ty->As<sem::Array>()) {
105 auto* el = CreateASTTypeFor(ctx, a->ElemType());
106 ast::AttributeList attrs;
107 if (!a->IsStrideImplicit()) {
108 attrs.emplace_back(ctx.dst->create<ast::StrideAttribute>(a->Stride()));
109 }
110 if (a->IsRuntimeSized()) {
111 return ctx.dst->ty.array(el, nullptr, std::move(attrs));
112 } else {
Ben Clayton0ce9ab02022-05-05 20:23:40 +0000113 return ctx.dst->ty.array(el, u32(a->Count()), std::move(attrs));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000114 }
115 }
116 if (auto* s = ty->As<sem::Struct>()) {
117 return ctx.dst->create<ast::TypeName>(ctx.Clone(s->Declaration()->name));
118 }
119 if (auto* s = ty->As<sem::Reference>()) {
120 return CreateASTTypeFor(ctx, s->StoreType());
121 }
122 if (auto* a = ty->As<sem::Atomic>()) {
123 return ctx.dst->create<ast::Atomic>(CreateASTTypeFor(ctx, a->Type()));
124 }
125 if (auto* t = ty->As<sem::DepthTexture>()) {
126 return ctx.dst->create<ast::DepthTexture>(t->dim());
127 }
128 if (auto* t = ty->As<sem::DepthMultisampledTexture>()) {
129 return ctx.dst->create<ast::DepthMultisampledTexture>(t->dim());
130 }
131 if (ty->Is<sem::ExternalTexture>()) {
132 return ctx.dst->create<ast::ExternalTexture>();
133 }
134 if (auto* t = ty->As<sem::MultisampledTexture>()) {
135 return ctx.dst->create<ast::MultisampledTexture>(t->dim(),
136 CreateASTTypeFor(ctx, t->type()));
137 }
138 if (auto* t = ty->As<sem::SampledTexture>()) {
139 return ctx.dst->create<ast::SampledTexture>(t->dim(), CreateASTTypeFor(ctx, t->type()));
140 }
141 if (auto* t = ty->As<sem::StorageTexture>()) {
142 return ctx.dst->create<ast::StorageTexture>(t->dim(), t->texel_format(),
143 CreateASTTypeFor(ctx, t->type()), t->access());
144 }
145 if (auto* s = ty->As<sem::Sampler>()) {
146 return ctx.dst->create<ast::Sampler>(s->kind());
147 }
148 TINT_UNREACHABLE(Transform, ctx.dst->Diagnostics())
149 << "Unhandled type: " << ty->TypeInfo().name;
150 return nullptr;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000151}
152
dan sinclairb5599d32022-04-07 16:55:14 +0000153} // namespace tint::transform