Add transform to substitute overrides with const expressions.

This CL adds a SubstituteOverride transform which will convert
an `override` into a `const`. The transform is provided a map of
(string, double) which matches what the WebGPU API accepts as
data for overrides.

Bug: tint:1582
Change-Id: I6e6bf51b98ce4d4746f8de55128666c36735e585
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/96760
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
dan sinclair
2022-07-22 16:05:06 +00:00
committed by Dawn LUCI CQ
parent 9ec7893ad4
commit 256f1116b8
156 changed files with 1235 additions and 659 deletions

View File

@@ -46,4 +46,11 @@ const Override* Override::Clone(CloneContext* ctx) const {
return ctx->dst->create<Override>(src, sym, ty, ctor, attrs);
}
std::string Override::Identifier(const SymbolTable& symbols) const {
if (auto* id = ast::GetAttribute<ast::IdAttribute>(attributes)) {
return std::to_string(id->value);
}
return symbols.NameFor(symbol);
}
} // namespace tint::ast

View File

@@ -15,6 +15,8 @@
#ifndef SRC_TINT_AST_OVERRIDE_H_
#define SRC_TINT_AST_OVERRIDE_H_
#include <string>
#include "src/tint/ast/variable.h"
namespace tint::ast {
@@ -60,6 +62,12 @@ class Override final : public Castable<Override, Variable> {
/// @param ctx the clone context
/// @return the newly cloned node
const Override* Clone(CloneContext* ctx) const override;
/// @param symbols the symbol table to retrieve the name from
/// @returns the identifier string for the override. If the override has
/// an ID attribute, the string is the id-stringified. Otherwise, the ID
/// is the symbol.
std::string Identifier(const SymbolTable& symbols) const;
};
} // namespace tint::ast

View File

@@ -0,0 +1,35 @@
// Copyright 2022 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/tint/ast/override.h"
#include "src/tint/ast/test_helper.h"
namespace tint::ast {
namespace {
using OverrideTest = TestHelper;
TEST_F(OverrideTest, Identifier_NoId) {
auto* o = Override("o", nullptr, Expr(f32(1.0)));
EXPECT_EQ(std::string("o"), o->Identifier(Symbols()));
}
TEST_F(OverrideTest, Identifier_WithId) {
auto* o = Override("o", nullptr, Expr(f32(1.0)), {Id(4u)});
EXPECT_EQ(std::string("4"), o->Identifier(Symbols()));
}
} // namespace
} // namespace tint::ast