blob: c37f3b49a7b1676295c631dbb99d27dbdc99ef82 [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
Ben Claytonc6b38142022-11-03 08:41:19 +000049Output Transform::Run(const Program* src, const DataMap& data /* = {} */) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000050 Output output;
Ben Claytonc6b38142022-11-03 08:41:19 +000051 if (auto program = Apply(src, data, output.data)) {
52 output.program = std::move(program.value());
53 } else {
54 ProgramBuilder b;
55 CloneContext ctx{&b, src, /* auto_clone_symbols */ true};
56 ctx.Clone();
57 output.program = Program(std::move(b));
58 }
dan sinclair41e4d9a2022-05-01 14:40:55 +000059 return output;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000060}
61
Ryan Harrisondbc13af2022-02-21 15:19:07 +000062void Transform::RemoveStatement(CloneContext& ctx, const ast::Statement* stmt) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000063 auto* sem = ctx.src->Sem().Get(stmt);
64 if (auto* block = tint::As<sem::BlockStatement>(sem->Parent())) {
65 ctx.Remove(block->Declaration()->statements, stmt);
66 return;
67 }
68 if (tint::Is<sem::ForLoopStatement>(sem->Parent())) {
69 ctx.Replace(stmt, static_cast<ast::Expression*>(nullptr));
70 return;
71 }
72 TINT_ICE(Transform, ctx.dst->Diagnostics())
73 << "unable to remove statement from parent of type " << sem->TypeInfo().name;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000074}
75
dan sinclair41e4d9a2022-05-01 14:40:55 +000076const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, const sem::Type* ty) {
77 if (ty->Is<sem::Void>()) {
78 return ctx.dst->create<ast::Void>();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000079 }
dan sinclair41e4d9a2022-05-01 14:40:55 +000080 if (ty->Is<sem::I32>()) {
81 return ctx.dst->create<ast::I32>();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000082 }
dan sinclair41e4d9a2022-05-01 14:40:55 +000083 if (ty->Is<sem::U32>()) {
84 return ctx.dst->create<ast::U32>();
85 }
Zhaoming Jiang62bfd312022-05-13 12:01:11 +000086 if (ty->Is<sem::F16>()) {
87 return ctx.dst->create<ast::F16>();
88 }
dan sinclair41e4d9a2022-05-01 14:40:55 +000089 if (ty->Is<sem::F32>()) {
90 return ctx.dst->create<ast::F32>();
91 }
92 if (ty->Is<sem::Bool>()) {
93 return ctx.dst->create<ast::Bool>();
94 }
95 if (auto* m = ty->As<sem::Matrix>()) {
96 auto* el = CreateASTTypeFor(ctx, m->type());
97 return ctx.dst->create<ast::Matrix>(el, m->rows(), m->columns());
98 }
99 if (auto* v = ty->As<sem::Vector>()) {
100 auto* el = CreateASTTypeFor(ctx, v->type());
101 return ctx.dst->create<ast::Vector>(el, v->Width());
102 }
103 if (auto* a = ty->As<sem::Array>()) {
104 auto* el = CreateASTTypeFor(ctx, a->ElemType());
Ben Clayton783b1692022-08-02 17:03:35 +0000105 utils::Vector<const ast::Attribute*, 1> attrs;
dan sinclair41e4d9a2022-05-01 14:40:55 +0000106 if (!a->IsStrideImplicit()) {
Ben Clayton783b1692022-08-02 17:03:35 +0000107 attrs.Push(ctx.dst->create<ast::StrideAttribute>(a->Stride()));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000108 }
109 if (a->IsRuntimeSized()) {
110 return ctx.dst->ty.array(el, nullptr, std::move(attrs));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000111 }
Ben Clayton22c48502022-10-31 17:26:10 +0000112 if (auto* override = std::get_if<sem::NamedOverrideArrayCount>(&a->Count())) {
dan sinclair78f80672022-09-22 22:28:21 +0000113 auto* count = ctx.Clone(override->variable->Declaration());
114 return ctx.dst->ty.array(el, count, std::move(attrs));
115 }
Ben Clayton22c48502022-10-31 17:26:10 +0000116 if (auto* override = std::get_if<sem::UnnamedOverrideArrayCount>(&a->Count())) {
117 auto* count = ctx.Clone(override->expr->Declaration());
118 return ctx.dst->ty.array(el, count, std::move(attrs));
119 }
dan sinclair78f80672022-09-22 22:28:21 +0000120 if (auto count = a->ConstantCount()) {
121 return ctx.dst->ty.array(el, u32(count.value()), std::move(attrs));
122 }
123 TINT_ICE(Transform, ctx.dst->Diagnostics()) << sem::Array::kErrExpectedConstantCount;
124 return ctx.dst->ty.array(el, u32(1), std::move(attrs));
dan sinclair41e4d9a2022-05-01 14:40:55 +0000125 }
126 if (auto* s = ty->As<sem::Struct>()) {
127 return ctx.dst->create<ast::TypeName>(ctx.Clone(s->Declaration()->name));
128 }
129 if (auto* s = ty->As<sem::Reference>()) {
130 return CreateASTTypeFor(ctx, s->StoreType());
131 }
132 if (auto* a = ty->As<sem::Atomic>()) {
133 return ctx.dst->create<ast::Atomic>(CreateASTTypeFor(ctx, a->Type()));
134 }
135 if (auto* t = ty->As<sem::DepthTexture>()) {
136 return ctx.dst->create<ast::DepthTexture>(t->dim());
137 }
138 if (auto* t = ty->As<sem::DepthMultisampledTexture>()) {
139 return ctx.dst->create<ast::DepthMultisampledTexture>(t->dim());
140 }
141 if (ty->Is<sem::ExternalTexture>()) {
142 return ctx.dst->create<ast::ExternalTexture>();
143 }
144 if (auto* t = ty->As<sem::MultisampledTexture>()) {
145 return ctx.dst->create<ast::MultisampledTexture>(t->dim(),
146 CreateASTTypeFor(ctx, t->type()));
147 }
148 if (auto* t = ty->As<sem::SampledTexture>()) {
149 return ctx.dst->create<ast::SampledTexture>(t->dim(), CreateASTTypeFor(ctx, t->type()));
150 }
151 if (auto* t = ty->As<sem::StorageTexture>()) {
152 return ctx.dst->create<ast::StorageTexture>(t->dim(), t->texel_format(),
153 CreateASTTypeFor(ctx, t->type()), t->access());
154 }
155 if (auto* s = ty->As<sem::Sampler>()) {
156 return ctx.dst->create<ast::Sampler>(s->kind());
157 }
158 TINT_UNREACHABLE(Transform, ctx.dst->Diagnostics())
159 << "Unhandled type: " << ty->TypeInfo().name;
160 return nullptr;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000161}
162
dan sinclairb5599d32022-04-07 16:55:14 +0000163} // namespace tint::transform