blob: 3e034112b514ee54e9d1de5f0f6246e75c9974fe [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"
dan sinclair78f80672022-09-22 22:28:21 +000027#include "src/tint/sem/variable.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000028
29TINT_INSTANTIATE_TYPEINFO(tint::transform::Transform);
30TINT_INSTANTIATE_TYPEINFO(tint::transform::Data);
31
dan sinclairb5599d32022-04-07 16:55:14 +000032namespace tint::transform {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000033
34Data::Data() = default;
35Data::Data(const Data&) = default;
36Data::~Data() = default;
37Data& Data::operator=(const Data&) = default;
38
39DataMap::DataMap() = default;
40DataMap::DataMap(DataMap&&) = default;
41DataMap::~DataMap() = default;
42DataMap& DataMap::operator=(DataMap&&) = default;
43
44Output::Output() = default;
45Output::Output(Program&& p) : program(std::move(p)) {}
46Transform::Transform() = default;
47Transform::~Transform() = default;
48
dan sinclair41e4d9a2022-05-01 14:40:55 +000049Output Transform::Run(const Program* program, const DataMap& data /* = {} */) const {
50 ProgramBuilder builder;
51 CloneContext ctx(&builder, program);
52 Output output;
53 Run(ctx, data, output.data);
54 output.program = Program(std::move(builder));
55 return output;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000056}
57
58void Transform::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000059 TINT_UNIMPLEMENTED(Transform, ctx.dst->Diagnostics())
60 << "Transform::Run() unimplemented for " << TypeInfo().name;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000061}
62
63bool Transform::ShouldRun(const Program*, const DataMap&) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000064 return true;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000065}
66
67void Transform::RemoveStatement(CloneContext& ctx, const ast::Statement* stmt) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000068 auto* sem = ctx.src->Sem().Get(stmt);
69 if (auto* block = tint::As<sem::BlockStatement>(sem->Parent())) {
70 ctx.Remove(block->Declaration()->statements, stmt);
71 return;
72 }
73 if (tint::Is<sem::ForLoopStatement>(sem->Parent())) {
74 ctx.Replace(stmt, static_cast<ast::Expression*>(nullptr));
75 return;
76 }
77 TINT_ICE(Transform, ctx.dst->Diagnostics())
78 << "unable to remove statement from parent of type " << sem->TypeInfo().name;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000079}
80
dan sinclair41e4d9a2022-05-01 14:40:55 +000081const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, const sem::Type* ty) {
82 if (ty->Is<sem::Void>()) {
83 return ctx.dst->create<ast::Void>();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000084 }
dan sinclair41e4d9a2022-05-01 14:40:55 +000085 if (ty->Is<sem::I32>()) {
86 return ctx.dst->create<ast::I32>();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000087 }
dan sinclair41e4d9a2022-05-01 14:40:55 +000088 if (ty->Is<sem::U32>()) {
89 return ctx.dst->create<ast::U32>();
90 }
Zhaoming Jiang62bfd312022-05-13 12:01:11 +000091 if (ty->Is<sem::F16>()) {
92 return ctx.dst->create<ast::F16>();
93 }
dan sinclair41e4d9a2022-05-01 14:40:55 +000094 if (ty->Is<sem::F32>()) {
95 return ctx.dst->create<ast::F32>();
96 }
97 if (ty->Is<sem::Bool>()) {
98 return ctx.dst->create<ast::Bool>();
99 }
100 if (auto* m = ty->As<sem::Matrix>()) {
101 auto* el = CreateASTTypeFor(ctx, m->type());
102 return ctx.dst->create<ast::Matrix>(el, m->rows(), m->columns());
103 }
104 if (auto* v = ty->As<sem::Vector>()) {
105 auto* el = CreateASTTypeFor(ctx, v->type());
106 return ctx.dst->create<ast::Vector>(el, v->Width());
107 }
108 if (auto* a = ty->As<sem::Array>()) {
109 auto* el = CreateASTTypeFor(ctx, a->ElemType());
Ben Clayton783b1692022-08-02 17:03:35 +0000110 utils::Vector<const ast::Attribute*, 1> attrs;
dan sinclair41e4d9a2022-05-01 14:40:55 +0000111 if (!a->IsStrideImplicit()) {
Ben Clayton783b1692022-08-02 17:03:35 +0000112 attrs.Push(ctx.dst->create<ast::StrideAttribute>(a->Stride()));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000113 }
114 if (a->IsRuntimeSized()) {
115 return ctx.dst->ty.array(el, nullptr, std::move(attrs));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000116 }
Ben Clayton22c48502022-10-31 17:26:10 +0000117 if (auto* override = std::get_if<sem::NamedOverrideArrayCount>(&a->Count())) {
dan sinclair78f80672022-09-22 22:28:21 +0000118 auto* count = ctx.Clone(override->variable->Declaration());
119 return ctx.dst->ty.array(el, count, std::move(attrs));
120 }
Ben Clayton22c48502022-10-31 17:26:10 +0000121 if (auto* override = std::get_if<sem::UnnamedOverrideArrayCount>(&a->Count())) {
122 auto* count = ctx.Clone(override->expr->Declaration());
123 return ctx.dst->ty.array(el, count, std::move(attrs));
124 }
dan sinclair78f80672022-09-22 22:28:21 +0000125 if (auto count = a->ConstantCount()) {
126 return ctx.dst->ty.array(el, u32(count.value()), std::move(attrs));
127 }
128 TINT_ICE(Transform, ctx.dst->Diagnostics()) << sem::Array::kErrExpectedConstantCount;
129 return ctx.dst->ty.array(el, u32(1), std::move(attrs));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000130 }
131 if (auto* s = ty->As<sem::Struct>()) {
132 return ctx.dst->create<ast::TypeName>(ctx.Clone(s->Declaration()->name));
133 }
134 if (auto* s = ty->As<sem::Reference>()) {
135 return CreateASTTypeFor(ctx, s->StoreType());
136 }
137 if (auto* a = ty->As<sem::Atomic>()) {
138 return ctx.dst->create<ast::Atomic>(CreateASTTypeFor(ctx, a->Type()));
139 }
140 if (auto* t = ty->As<sem::DepthTexture>()) {
141 return ctx.dst->create<ast::DepthTexture>(t->dim());
142 }
143 if (auto* t = ty->As<sem::DepthMultisampledTexture>()) {
144 return ctx.dst->create<ast::DepthMultisampledTexture>(t->dim());
145 }
146 if (ty->Is<sem::ExternalTexture>()) {
147 return ctx.dst->create<ast::ExternalTexture>();
148 }
149 if (auto* t = ty->As<sem::MultisampledTexture>()) {
150 return ctx.dst->create<ast::MultisampledTexture>(t->dim(),
151 CreateASTTypeFor(ctx, t->type()));
152 }
153 if (auto* t = ty->As<sem::SampledTexture>()) {
154 return ctx.dst->create<ast::SampledTexture>(t->dim(), CreateASTTypeFor(ctx, t->type()));
155 }
156 if (auto* t = ty->As<sem::StorageTexture>()) {
157 return ctx.dst->create<ast::StorageTexture>(t->dim(), t->texel_format(),
158 CreateASTTypeFor(ctx, t->type()), t->access());
159 }
160 if (auto* s = ty->As<sem::Sampler>()) {
161 return ctx.dst->create<ast::Sampler>(s->kind());
162 }
163 TINT_UNREACHABLE(Transform, ctx.dst->Diagnostics())
164 << "Unhandled type: " << ty->TypeInfo().name;
165 return nullptr;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000166}
167
dan sinclairb5599d32022-04-07 16:55:14 +0000168} // namespace tint::transform