tint/ast: Generate ast::StorageClass from intrinsics.def

Emit unit tests for parsing and printing.
Emit benchmarks for parsing.
Uses intrinsics.def as a single-source-of-truth.
The generators provide a way to optimize the enum parsers.

Change-Id: I1669c123d375f24aca45f3ea4abf04d7892673c7
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/97150
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
This commit is contained in:
Ben Clayton
2022-07-27 16:36:35 +00:00
committed by Dawn LUCI CQ
parent bf8ee35498
commit a123b892a5
22 changed files with 587 additions and 85 deletions

View File

@@ -12,6 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
////////////////////////////////////////////////////////////////////////////////
// File generated by tools/src/cmd/gen
// using the template:
// src/tint/ast/storage_class.cc.tmpl
//
// Do not modify this file directly
////////////////////////////////////////////////////////////////////////////////
#include "src/tint/ast/storage_class.h"
namespace tint::ast {

View File

@@ -0,0 +1,25 @@
{{- /*
--------------------------------------------------------------------------------
Template file for use with tools/src/cmd/gen to generate storage_class.cc
To update the generated file, run:
./tools/run gen
See:
* tools/src/cmd/gen for structures used by this template
* https://golang.org/pkg/text/template/ for documentation on the template syntax
--------------------------------------------------------------------------------
*/ -}}
{{- Import "src/tint/templates/enums.tmpl.inc" -}}
{{- $enum := (Sem.Enum "storage_class") -}}
#include "src/tint/ast/storage_class.h"
namespace tint::ast {
{{ Eval "ParseEnum" $enum}}
{{ Eval "EnumOStream" $enum}}
} // namespace tint::ast

View File

@@ -12,6 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
////////////////////////////////////////////////////////////////////////////////
// File generated by tools/src/cmd/gen
// using the template:
// src/tint/ast/storage_class.h.tmpl
//
// Do not modify this file directly
////////////////////////////////////////////////////////////////////////////////
#ifndef SRC_TINT_AST_STORAGE_CLASS_H_
#define SRC_TINT_AST_STORAGE_CLASS_H_
@@ -22,21 +30,21 @@ namespace tint::ast {
/// Storage class of a given pointer.
enum class StorageClass {
kInvalid,
kNone,
kNone, // Tint-internal enum entry - not parsed
kFunction,
kPrivate,
kWorkgroup,
kUniform,
kStorage,
kHandle,
kIn,
kOut,
kHandle, // Tint-internal enum entry - not parsed
kIn, // Tint-internal enum entry - not parsed
kOut, // Tint-internal enum entry - not parsed
};
/// @param out the std::ostream to write to
/// @param sc the StorageClass
/// @return the std::ostream so calls can be chained
std::ostream& operator<<(std::ostream& out, StorageClass sc);
/// @param value the StorageClass
/// @returns `out` so calls can be chained
std::ostream& operator<<(std::ostream& out, StorageClass value);
/// ParseStorageClass parses a StorageClass from a string.
/// @param str the string to parse

View File

@@ -0,0 +1,36 @@
{{- /*
--------------------------------------------------------------------------------
Template file for use with tools/src/cmd/gen to generate storage_class.h
To update the generated file, run:
./tools/run gen
See:
* tools/src/cmd/gen for structures used by this template
* https://golang.org/pkg/text/template/ for documentation on the template syntax
--------------------------------------------------------------------------------
*/ -}}
{{- Import "src/tint/templates/enums.tmpl.inc" -}}
{{- $enum := (Sem.Enum "storage_class") -}}
#ifndef SRC_TINT_AST_STORAGE_CLASS_H_
#define SRC_TINT_AST_STORAGE_CLASS_H_
#include <ostream>
namespace tint::ast {
/// Storage class of a given pointer.
{{ Eval "DeclareEnum" $enum}}
/// @returns true if the StorageClass is host-shareable
/// @param sc the StorageClass
/// @see https://gpuweb.github.io/gpuweb/wgsl.html#host-shareable
inline bool IsHostShareable(StorageClass sc) {
return sc == ast::StorageClass::kUniform || sc == ast::StorageClass::kStorage;
}
} // namespace tint::ast
#endif // SRC_TINT_AST_STORAGE_CLASS_H_

View File

@@ -0,0 +1,81 @@
// 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.
////////////////////////////////////////////////////////////////////////////////
// File generated by tools/src/cmd/gen
// using the template:
// src/tint/ast/storage_class_bench.cc.tmpl
//
// Do not modify this file directly
////////////////////////////////////////////////////////////////////////////////
#include "src/tint/ast/storage_class.h"
#include <array>
#include "benchmark/benchmark.h"
namespace tint::ast {
namespace {
void StorageClassParser(::benchmark::State& state) {
std::array kStrings{
"fccnctin",
"ucti3",
"functVon",
"function",
"1unction",
"unJtqqon",
"llun77tion",
"ppqqivtHH",
"prcv",
"bivaGe",
"private",
"priviive",
"8WWivate",
"pxxvate",
"wXkgrggup",
"worXVup",
"3orkgroup",
"workgroup",
"workgroEp",
"woTTPkroup",
"ddorkroxxp",
"u44iform",
"unSSfoVVm",
"RniR22m",
"uniform",
"uFfo9m",
"uniorm",
"VOORRHrm",
"straye",
"llntrrr77ge",
"stor4g00",
"storage",
"trooe",
"zzrage",
"siioppa1",
};
for (auto _ : state) {
for (auto& str : kStrings) {
auto result = ParseStorageClass(str);
benchmark::DoNotOptimize(result);
}
}
}
BENCHMARK(StorageClassParser);
} // namespace
} // namespace tint::ast

View File

@@ -0,0 +1,29 @@
{{- /*
--------------------------------------------------------------------------------
Template file for use with tools/src/cmd/gen to generate storage_class_bench.cc
To update the generated file, run:
./tools/run gen
See:
* tools/src/cmd/gen for structures used by this template
* https://golang.org/pkg/text/template/ for documentation on the template syntax
--------------------------------------------------------------------------------
*/ -}}
{{- Import "src/tint/templates/enums.tmpl.inc" -}}
{{- $enum := (Sem.Enum "storage_class") -}}
#include "src/tint/ast/storage_class.h"
#include <array>
#include "benchmark/benchmark.h"
namespace tint::ast {
namespace {
{{ Eval "BenchmarkParseEnum" $enum }}
} // namespace
} // namespace tint::ast

View File

@@ -0,0 +1,94 @@
// 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.
////////////////////////////////////////////////////////////////////////////////
// File generated by tools/src/cmd/gen
// using the template:
// src/tint/ast/storage_class_test.cc.tmpl
//
// Do not modify this file directly
////////////////////////////////////////////////////////////////////////////////
#include "src/tint/ast/storage_class.h"
#include <string>
#include "src/tint/ast/test_helper.h"
#include "src/tint/utils/string.h"
namespace tint::ast {
namespace {
namespace parse_print_tests {
struct Case {
const char* string;
StorageClass value;
};
inline std::ostream& operator<<(std::ostream& out, Case c) {
return out << "'" << std::string(c.string) << "'";
}
static constexpr Case kValidCases[] = {
{"function", StorageClass::kFunction},
{"private", StorageClass::kPrivate},
{"workgroup", StorageClass::kWorkgroup},
{"uniform", StorageClass::kUniform},
{"storage", StorageClass::kStorage},
};
static constexpr Case kInvalidCases[] = {
{"fccnctin", StorageClass::kInvalid},
{"ucti3", StorageClass::kInvalid},
{"functVon", StorageClass::kInvalid},
{"priv1te", StorageClass::kInvalid},
{"pqiJate", StorageClass::kInvalid},
{"privat7ll", StorageClass::kInvalid},
{"workroppqHH", StorageClass::kInvalid},
{"workru", StorageClass::kInvalid},
{"wbkgGoup", StorageClass::kInvalid},
{"unifiivm", StorageClass::kInvalid},
{"8WWiform", StorageClass::kInvalid},
{"uxxform", StorageClass::kInvalid},
{"sXraggg", StorageClass::kInvalid},
{"traXe", StorageClass::kInvalid},
{"stor3ge", StorageClass::kInvalid},
};
using StorageClassParseTest = testing::TestWithParam<Case>;
TEST_P(StorageClassParseTest, Parse) {
const char* string = GetParam().string;
StorageClass expect = GetParam().value;
EXPECT_EQ(expect, ParseStorageClass(string));
}
INSTANTIATE_TEST_SUITE_P(ValidCases, StorageClassParseTest, testing::ValuesIn(kValidCases));
INSTANTIATE_TEST_SUITE_P(InvalidCases, StorageClassParseTest, testing::ValuesIn(kInvalidCases));
using StorageClassPrintTest = testing::TestWithParam<Case>;
TEST_P(StorageClassPrintTest, Print) {
StorageClass value = GetParam().value;
const char* expect = GetParam().string;
EXPECT_EQ(expect, utils::ToString(value));
}
INSTANTIATE_TEST_SUITE_P(ValidCases, StorageClassPrintTest, testing::ValuesIn(kValidCases));
} // namespace parse_print_tests
} // namespace
} // namespace tint::ast

View File

@@ -0,0 +1,30 @@
{{- /*
--------------------------------------------------------------------------------
Template file for use with tools/src/cmd/gen to generate storage_class_test.cc
To update the generated file, run:
./tools/run gen
See:
* tools/src/cmd/gen for structures used by this template
* https://golang.org/pkg/text/template/ for documentation on the template syntax
--------------------------------------------------------------------------------
*/ -}}
{{- Import "src/tint/templates/enums.tmpl.inc" -}}
{{- $enum := (Sem.Enum "storage_class") -}}
#include "src/tint/ast/storage_class.h"
#include <string>
#include "src/tint/ast/test_helper.h"
#include "src/tint/utils/string.h"
namespace tint::ast {
namespace {
{{ Eval "TestParsePrintEnum" $enum}}
} // namespace
} // namespace tint::ast