Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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/tint/transform/transform.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <string> |
| 19 | |
| 20 | #include "src/tint/program_builder.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 21 | #include "src/tint/sem/block_statement.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 22 | #include "src/tint/sem/for_loop_statement.h" |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 23 | #include "src/tint/sem/variable.h" |
dan sinclair | d8a0845 | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 24 | #include "src/tint/type/atomic.h" |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 25 | #include "src/tint/type/depth_multisampled_texture.h" |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 26 | #include "src/tint/type/reference.h" |
dan sinclair | 5ee58b6 | 2022-12-08 15:25:18 +0000 | [diff] [blame] | 27 | #include "src/tint/type/sampler.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 28 | |
| 29 | TINT_INSTANTIATE_TYPEINFO(tint::transform::Transform); |
| 30 | TINT_INSTANTIATE_TYPEINFO(tint::transform::Data); |
| 31 | |
dan sinclair | b5599d3 | 2022-04-07 16:55:14 +0000 | [diff] [blame] | 32 | namespace tint::transform { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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; |
| 45 | Output::Output(Program&& p) : program(std::move(p)) {} |
| 46 | Transform::Transform() = default; |
| 47 | Transform::~Transform() = default; |
| 48 | |
Ben Clayton | c6b3814 | 2022-11-03 08:41:19 +0000 | [diff] [blame] | 49 | Output Transform::Run(const Program* src, const DataMap& data /* = {} */) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 50 | Output output; |
Ben Clayton | c6b3814 | 2022-11-03 08:41:19 +0000 | [diff] [blame] | 51 | 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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 59 | return output; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 62 | void Transform::RemoveStatement(CloneContext& ctx, const ast::Statement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 63 | 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 | } |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 68 | if (TINT_LIKELY(tint::Is<sem::ForLoopStatement>(sem->Parent()))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 74 | } |
| 75 | |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 76 | const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, const type::Type* ty) { |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 77 | if (ty->Is<type::Void>()) { |
Ben Clayton | 1906857 | 2023-02-07 21:28:09 +0000 | [diff] [blame] | 78 | return nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 79 | } |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 80 | if (ty->Is<type::I32>()) { |
Ben Clayton | 6e0a515 | 2023-02-09 23:33:24 +0000 | [diff] [blame] | 81 | return ctx.dst->ty.i32(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 82 | } |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 83 | if (ty->Is<type::U32>()) { |
Ben Clayton | 6e0a515 | 2023-02-09 23:33:24 +0000 | [diff] [blame] | 84 | return ctx.dst->ty.u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 85 | } |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 86 | if (ty->Is<type::F16>()) { |
Ben Clayton | 6e0a515 | 2023-02-09 23:33:24 +0000 | [diff] [blame] | 87 | return ctx.dst->ty.f16(); |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 88 | } |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 89 | if (ty->Is<type::F32>()) { |
Ben Clayton | 6e0a515 | 2023-02-09 23:33:24 +0000 | [diff] [blame] | 90 | return ctx.dst->ty.f32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 91 | } |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 92 | if (ty->Is<type::Bool>()) { |
Ben Clayton | 6e0a515 | 2023-02-09 23:33:24 +0000 | [diff] [blame] | 93 | return ctx.dst->ty.bool_(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 94 | } |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 95 | if (auto* m = ty->As<type::Matrix>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | auto* el = CreateASTTypeFor(ctx, m->type()); |
| 97 | return ctx.dst->create<ast::Matrix>(el, m->rows(), m->columns()); |
| 98 | } |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 99 | if (auto* v = ty->As<type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 100 | auto* el = CreateASTTypeFor(ctx, v->type()); |
| 101 | return ctx.dst->create<ast::Vector>(el, v->Width()); |
| 102 | } |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 103 | if (auto* a = ty->As<type::Array>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 104 | auto* el = CreateASTTypeFor(ctx, a->ElemType()); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 105 | utils::Vector<const ast::Attribute*, 1> attrs; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 106 | if (!a->IsStrideImplicit()) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 107 | attrs.Push(ctx.dst->create<ast::StrideAttribute>(a->Stride())); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 108 | } |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 109 | if (a->Count()->Is<type::RuntimeArrayCount>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 110 | return ctx.dst->ty.array(el, nullptr, std::move(attrs)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 111 | } |
dan sinclair | 4b1d79e | 2022-12-01 23:45:18 +0000 | [diff] [blame] | 112 | if (auto* override = a->Count()->As<sem::NamedOverrideArrayCount>()) { |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 113 | auto* count = ctx.Clone(override->variable->Declaration()); |
| 114 | return ctx.dst->ty.array(el, count, std::move(attrs)); |
| 115 | } |
dan sinclair | 4b1d79e | 2022-12-01 23:45:18 +0000 | [diff] [blame] | 116 | if (auto* override = a->Count()->As<sem::UnnamedOverrideArrayCount>()) { |
Ben Clayton | 87bccb7 | 2022-11-21 19:05:24 +0000 | [diff] [blame] | 117 | // If the array count is an unnamed (complex) override expression, then its not safe to |
| 118 | // redeclare this type as we'd end up with two types that would not compare equal. |
| 119 | // See crbug.com/tint/1764. |
| 120 | // Look for a type alias for this array. |
| 121 | for (auto* type_decl : ctx.src->AST().TypeDecls()) { |
| 122 | if (auto* alias = type_decl->As<ast::Alias>()) { |
| 123 | if (ty == ctx.src->Sem().Get(alias)) { |
| 124 | // Alias found. Use the alias name to ensure types compare equal. |
Ben Clayton | b75252b | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 125 | return ctx.dst->ty(ctx.Clone(alias->name->symbol)); |
Ben Clayton | 87bccb7 | 2022-11-21 19:05:24 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | // Array is not aliased. Rebuild the array. |
Ben Clayton | 22c4850 | 2022-10-31 17:26:10 +0000 | [diff] [blame] | 130 | auto* count = ctx.Clone(override->expr->Declaration()); |
| 131 | return ctx.dst->ty.array(el, count, std::move(attrs)); |
| 132 | } |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 133 | auto count = a->ConstantCount(); |
| 134 | if (TINT_UNLIKELY(!count)) { |
| 135 | TINT_ICE(Transform, ctx.dst->Diagnostics()) << type::Array::kErrExpectedConstantCount; |
| 136 | return ctx.dst->ty.array(el, u32(1), std::move(attrs)); |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 137 | } |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 138 | return ctx.dst->ty.array(el, u32(count.value()), std::move(attrs)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 139 | } |
| 140 | if (auto* s = ty->As<sem::Struct>()) { |
Ben Clayton | b75252b | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 141 | return ctx.dst->ty(ctx.Clone(s->Declaration()->name->symbol)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 142 | } |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 143 | if (auto* s = ty->As<type::Reference>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 144 | return CreateASTTypeFor(ctx, s->StoreType()); |
| 145 | } |
dan sinclair | d8a0845 | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 146 | if (auto* a = ty->As<type::Atomic>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 147 | return ctx.dst->create<ast::Atomic>(CreateASTTypeFor(ctx, a->Type())); |
| 148 | } |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 149 | if (auto* t = ty->As<type::DepthTexture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 150 | return ctx.dst->create<ast::DepthTexture>(t->dim()); |
| 151 | } |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 152 | if (auto* t = ty->As<type::DepthMultisampledTexture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 153 | return ctx.dst->create<ast::DepthMultisampledTexture>(t->dim()); |
| 154 | } |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 155 | if (ty->Is<type::ExternalTexture>()) { |
Ben Clayton | 4906b03 | 2023-02-09 23:59:07 +0000 | [diff] [blame^] | 156 | return ctx.dst->ty.external_texture(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 157 | } |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 158 | if (auto* t = ty->As<type::MultisampledTexture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 159 | return ctx.dst->create<ast::MultisampledTexture>(t->dim(), |
| 160 | CreateASTTypeFor(ctx, t->type())); |
| 161 | } |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 162 | if (auto* t = ty->As<type::SampledTexture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 163 | return ctx.dst->create<ast::SampledTexture>(t->dim(), CreateASTTypeFor(ctx, t->type())); |
| 164 | } |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 165 | if (auto* t = ty->As<type::StorageTexture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 166 | return ctx.dst->create<ast::StorageTexture>(t->dim(), t->texel_format(), |
| 167 | CreateASTTypeFor(ctx, t->type()), t->access()); |
| 168 | } |
dan sinclair | 5ee58b6 | 2022-12-08 15:25:18 +0000 | [diff] [blame] | 169 | if (auto* s = ty->As<type::Sampler>()) { |
Ben Clayton | ed3389f | 2023-02-09 23:56:42 +0000 | [diff] [blame] | 170 | return ctx.dst->ty.sampler(s->kind()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 171 | } |
| 172 | TINT_UNREACHABLE(Transform, ctx.dst->Diagnostics()) |
| 173 | << "Unhandled type: " << ty->TypeInfo().name; |
| 174 | return nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 175 | } |
| 176 | |
dan sinclair | b5599d3 | 2022-04-07 16:55:14 +0000 | [diff] [blame] | 177 | } // namespace tint::transform |