dawn-cmake/src/clone_context.cc

70 lines
2.0 KiB
C++
Raw Normal View History

// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/clone_context.h"
SymbolTable: Change behavior of anonymous symbols SymbolTable::New() used to build and return a symbol without a registered name. When you asked for the name of the symbol it would return tint_symbol_N, where N is the numerical identifier for the symbol. This approach was a major tripping hazzard for transforms that liked to fetch the source program name, and register it in the new program (in this situation, you should always use `CloneContext::Clone(Symbol)`). Without special casing for unnamed symbols, you could end up promoting the unnamed symbol to a named symbol, and then colliding against a new unnamed symbol. This is exactly what happened in tint:711. Instead, with this change: * The concept of unnamed symbols has been removed. All symbols now have a name. * The signature of `SymbolTable::New()` has been changed to take a name parameter (which defaults to 'tint_symbol'). This can be used to create a new, unique named symbol (possibly with a suffix), which will not collide with any existing symbols. Note these symbols may still collide if `SymbolTable::Register()` is called with the same name. All Transforms that currently use `SymbolTable::Register()` will be fixed in another change. * The CloneContext has been updated to use `SymbolTable::New()` instead of `Register()`. This means that any symbols defined before a clone will not collide. * `CloneContext::CloneSymbols()` has been added which allows a transform to pre-clone all the symbols from the source program. This can be used to avoid the authored identifiers being suffixed with a number, in the case a transform calls New() before the symbol is cloned. * `Symbol::to_str()` has been changed to return `$<id>` instead of `tint_symbol_N`. This is to avoid any confusion between the actual name and the symbol ID. Bug: tint:711 Bug: tint:712 Change-Id: I526e4b49b7027545613859de487e6a275686107a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/47631 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: Antonio Maiorano <amaiorano@google.com>
2021-04-13 20:07:57 +00:00
#include <string>
#include "src/program_builder.h"
#include "src/utils/get_or_create.h"
TINT_INSTANTIATE_TYPEINFO(tint::Cloneable);
namespace tint {
CloneContext::ListTransforms::ListTransforms() = default;
CloneContext::ListTransforms::~ListTransforms() = default;
CloneContext::CloneContext(ProgramBuilder* to, Program const* from)
: dst(to), src(from) {}
CloneContext::~CloneContext() = default;
Symbol CloneContext::Clone(Symbol s) {
return utils::GetOrCreate(cloned_symbols_, s, [&]() -> Symbol {
if (symbol_transform_) {
return symbol_transform_(s);
}
SymbolTable: Change behavior of anonymous symbols SymbolTable::New() used to build and return a symbol without a registered name. When you asked for the name of the symbol it would return tint_symbol_N, where N is the numerical identifier for the symbol. This approach was a major tripping hazzard for transforms that liked to fetch the source program name, and register it in the new program (in this situation, you should always use `CloneContext::Clone(Symbol)`). Without special casing for unnamed symbols, you could end up promoting the unnamed symbol to a named symbol, and then colliding against a new unnamed symbol. This is exactly what happened in tint:711. Instead, with this change: * The concept of unnamed symbols has been removed. All symbols now have a name. * The signature of `SymbolTable::New()` has been changed to take a name parameter (which defaults to 'tint_symbol'). This can be used to create a new, unique named symbol (possibly with a suffix), which will not collide with any existing symbols. Note these symbols may still collide if `SymbolTable::Register()` is called with the same name. All Transforms that currently use `SymbolTable::Register()` will be fixed in another change. * The CloneContext has been updated to use `SymbolTable::New()` instead of `Register()`. This means that any symbols defined before a clone will not collide. * `CloneContext::CloneSymbols()` has been added which allows a transform to pre-clone all the symbols from the source program. This can be used to avoid the authored identifiers being suffixed with a number, in the case a transform calls New() before the symbol is cloned. * `Symbol::to_str()` has been changed to return `$<id>` instead of `tint_symbol_N`. This is to avoid any confusion between the actual name and the symbol ID. Bug: tint:711 Bug: tint:712 Change-Id: I526e4b49b7027545613859de487e6a275686107a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/47631 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: Antonio Maiorano <amaiorano@google.com>
2021-04-13 20:07:57 +00:00
return dst->Symbols().New(src->Symbols().NameFor(s));
});
}
SymbolTable: Change behavior of anonymous symbols SymbolTable::New() used to build and return a symbol without a registered name. When you asked for the name of the symbol it would return tint_symbol_N, where N is the numerical identifier for the symbol. This approach was a major tripping hazzard for transforms that liked to fetch the source program name, and register it in the new program (in this situation, you should always use `CloneContext::Clone(Symbol)`). Without special casing for unnamed symbols, you could end up promoting the unnamed symbol to a named symbol, and then colliding against a new unnamed symbol. This is exactly what happened in tint:711. Instead, with this change: * The concept of unnamed symbols has been removed. All symbols now have a name. * The signature of `SymbolTable::New()` has been changed to take a name parameter (which defaults to 'tint_symbol'). This can be used to create a new, unique named symbol (possibly with a suffix), which will not collide with any existing symbols. Note these symbols may still collide if `SymbolTable::Register()` is called with the same name. All Transforms that currently use `SymbolTable::Register()` will be fixed in another change. * The CloneContext has been updated to use `SymbolTable::New()` instead of `Register()`. This means that any symbols defined before a clone will not collide. * `CloneContext::CloneSymbols()` has been added which allows a transform to pre-clone all the symbols from the source program. This can be used to avoid the authored identifiers being suffixed with a number, in the case a transform calls New() before the symbol is cloned. * `Symbol::to_str()` has been changed to return `$<id>` instead of `tint_symbol_N`. This is to avoid any confusion between the actual name and the symbol ID. Bug: tint:711 Bug: tint:712 Change-Id: I526e4b49b7027545613859de487e6a275686107a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/47631 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: Antonio Maiorano <amaiorano@google.com>
2021-04-13 20:07:57 +00:00
CloneContext& CloneContext::CloneSymbols() {
src->Symbols().Foreach([&](Symbol s, const std::string&) { Clone(s); });
return *this;
}
void CloneContext::Clone() {
dst->AST().Copy(this, &src->AST());
}
ast::FunctionList CloneContext::Clone(const ast::FunctionList& v) {
ast::FunctionList out;
out.reserve(v.size());
for (ast::Function* el : v) {
out.Add(Clone(el));
}
return out;
}
diag::List& CloneContext::Diagnostics() const {
return dst->Diagnostics();
}
CloneContext::CloneableTransform::CloneableTransform() = default;
CloneContext::CloneableTransform::CloneableTransform(
const CloneableTransform&) = default;
CloneContext::CloneableTransform::~CloneableTransform() = default;
} // namespace tint