Add utilities for printing Dawn enums and bitmasks

This is a header-only utility so it can be easily included and
used anyhere. Include it in DawnTest.h to automatically pick up
definitions to print test parameters.

Also, work around bot limitations for very-long test names.

Bug: none
Change-Id: I940263ab0a4cc415b06fa04749694f16ff08335c
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/51841
Auto-Submit: Austin Eng <enga@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
This commit is contained in:
Austin Eng 2021-06-01 18:49:12 +00:00 committed by Dawn LUCI CQ
parent f56dab0d72
commit a9e39e11a8
6 changed files with 154 additions and 23 deletions

View File

@ -697,6 +697,11 @@ class MultiGeneratorFromDawnJSON(Generator):
FileRender('webgpu_cpp.h', 'src/include/dawn/webgpu_cpp.h', FileRender('webgpu_cpp.h', 'src/include/dawn/webgpu_cpp.h',
[base_params, api_params])) [base_params, api_params]))
renders.append(
FileRender('webgpu_cpp_print.h',
'src/include/dawn/webgpu_cpp_print.h',
[base_params, api_params]))
if 'dawn_proc' in targets: if 'dawn_proc' in targets:
renders.append( renders.append(
FileRender('dawn_proc.c', 'src/dawn/dawn_proc.c', FileRender('dawn_proc.c', 'src/dawn/dawn_proc.c',

View File

@ -0,0 +1,90 @@
//* Copyright 2021 The Dawn 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.
#ifndef WEBGPU_CPP_PRINT_H_
#define WEBGPU_CPP_PRINT_H_
#include "dawn/webgpu_cpp.h"
#include <iomanip>
#include <ios>
#include <ostream>
#include <type_traits>
namespace wgpu {
{% for type in by_category["enum"] %}
template <typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, {{as_cppType(type.name)}} value) {
switch (value) {
{% for value in type.values %}
case {{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}:
o << "{{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}";
break;
{% endfor %}
default:
o << "{{as_cppType(type.name)}}::" << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<{{as_cppType(type.name)}}>::type>(value);
}
return o;
}
{% endfor %}
{% for type in by_category["bitmask"] %}
template <typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& o, {{as_cppType(type.name)}} value) {
o << "{{as_cppType(type.name)}}::";
if (!static_cast<bool>(value)) {
{% for value in type.values if value.value == 0 %}
// 0 is often explicitly declared as None.
o << "{{as_cppEnum(value.name)}}";
{% else %}
o << std::showbase << std::hex << std::setfill('0') << std::setw(4) << 0;
{% endfor %}
return o;
}
bool moreThanOneBit = !HasZeroOrOneBits(value);
if (moreThanOneBit) {
o << "(";
}
bool first = true;
{% for value in type.values if value.value != 0 %}
if (value & {{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}) {
if (!first) {
o << "|";
}
first = false;
o << "{{as_cppEnum(value.name)}}";
value &= ~{{as_cppType(type.name)}}::{{as_cppEnum(value.name)}};
}
{% endfor %}
if (static_cast<bool>(value)) {
if (!first) {
o << "|";
}
o << std::showbase << std::hex << std::setfill('0') << std::setw(4) << static_cast<typename std::underlying_type<{{as_cppType(type.name)}}>::type>(value);
}
if (moreThanOneBit) {
o << ")";
}
return o;
}
{% endfor %}
} // namespace wgpu
#endif // WEBGPU_CPP_PRINT_H_

View File

@ -61,11 +61,10 @@
// Implementation for DAWN_PP_FOR_EACH. // Implementation for DAWN_PP_FOR_EACH.
// Creates a call to DAWN_PP_FOR_EACH_X where X is 1, 2, ..., etc. // Creates a call to DAWN_PP_FOR_EACH_X where X is 1, 2, ..., etc.
#define DAWN_PP_FOR_EACH_(N, func, x, ...) \ #define DAWN_PP_FOR_EACH_(N, func, ...) DAWN_PP_CONCATENATE(DAWN_PP_FOR_EACH_, N)(func, __VA_ARGS__)
DAWN_PP_CONCATENATE(DAWN_PP_FOR_EACH_, N)(func, x, __VA_ARGS__)
// DAWN_PP_FOR_EACH: Apply |func| to each argument in |x| and __VA_ARGS__ // DAWN_PP_FOR_EACH: Apply |func| to each argument in |x| and __VA_ARGS__
#define DAWN_PP_FOR_EACH(func, x, ...) \ #define DAWN_PP_FOR_EACH(func, ...) \
DAWN_PP_FOR_EACH_(DAWN_PP_FOR_EACH_NARG(x, __VA_ARGS__), func, x, __VA_ARGS__) DAWN_PP_FOR_EACH_(DAWN_PP_FOR_EACH_NARG(__VA_ARGS__), func, __VA_ARGS__)
#endif // COMMON_PREPROCESSOR_H_ #endif // COMMON_PREPROCESSOR_H_

View File

@ -51,7 +51,10 @@ source_set("dawn_headers") {
dawn_json_generator("dawncpp_headers_gen") { dawn_json_generator("dawncpp_headers_gen") {
target = "dawncpp_headers" target = "dawncpp_headers"
outputs = [ "src/include/dawn/webgpu_cpp.h" ] outputs = [
"src/include/dawn/webgpu_cpp.h",
"src/include/dawn/webgpu_cpp_print.h",
]
} }
source_set("dawncpp_headers") { source_set("dawncpp_headers") {

View File

@ -167,35 +167,56 @@ AdapterTestParam::AdapterTestParam(const BackendTestConfig& config,
} }
std::ostream& operator<<(std::ostream& os, const AdapterTestParam& param) { std::ostream& operator<<(std::ostream& os, const AdapterTestParam& param) {
// Sanitize the adapter name for GoogleTest os << ParamName(param.adapterProperties.backendType) << " "
std::string sanitizedName = << param.adapterProperties.adapterName;
std::regex_replace(param.adapterProperties.adapterName, std::regex("[^a-zA-Z0-9]+"), "_");
// Strip trailing underscores, if any.
if (sanitizedName.back() == '_') {
sanitizedName.back() = '\0';
}
os << ParamName(param.adapterProperties.backendType) << "_" << sanitizedName.c_str();
// In a Windows Remote Desktop session there are two adapters named "Microsoft Basic Render // In a Windows Remote Desktop session there are two adapters named "Microsoft Basic Render
// Driver" with different adapter types. We must differentiate them to avoid any tests using the // Driver" with different adapter types. We must differentiate them to avoid any tests using the
// same name. // same name.
if (param.adapterProperties.deviceID == 0x008C) { if (param.adapterProperties.deviceID == 0x008C) {
std::string adapterType = AdapterTypeName(param.adapterProperties.adapterType); std::string adapterType = AdapterTypeName(param.adapterProperties.adapterType);
std::replace(adapterType.begin(), adapterType.end(), ' ', '_'); os << " " << adapterType;
os << "_" << adapterType;
} }
for (const char* forceEnabledWorkaround : param.forceEnabledWorkarounds) { for (const char* forceEnabledWorkaround : param.forceEnabledWorkarounds) {
os << "__e_" << forceEnabledWorkaround; os << "; e:" << forceEnabledWorkaround;
} }
for (const char* forceDisabledWorkaround : param.forceDisabledWorkarounds) { for (const char* forceDisabledWorkaround : param.forceDisabledWorkarounds) {
os << "__d_" << forceDisabledWorkaround; os << "; d:" << forceDisabledWorkaround;
} }
return os; return os;
} }
DawnTestBase::PrintToStringParamName::PrintToStringParamName(const char* test) : mTest(test) {
}
std::string DawnTestBase::PrintToStringParamName::SanitizeParamName(std::string paramName,
size_t index) const {
// Sanitize the adapter name for GoogleTest
std::string sanitizedName = std::regex_replace(paramName, std::regex("[^a-zA-Z0-9]+"), "_");
// Strip trailing underscores, if any.
while (sanitizedName.back() == '_') {
sanitizedName.resize(sanitizedName.length() - 1);
}
// We don't know the the test name at this point, but the format usually looks like
// this.
std::string prefix = mTest + ".TheTestNameUsuallyGoesHere/";
std::string testFormat = prefix + sanitizedName;
if (testFormat.length() > 220) {
// The bots don't support test names longer than 256. Shorten the name and append a unique
// index if we're close. The failure log will still print the full param name.
std::string suffix = std::string("__") + std::to_string(index);
size_t targetLength = sanitizedName.length();
targetLength -= testFormat.length() - 220;
targetLength -= suffix.length();
sanitizedName.resize(targetLength);
sanitizedName = sanitizedName + suffix;
}
return sanitizedName;
}
// Implementation of DawnTestEnvironment // Implementation of DawnTestEnvironment
void InitDawnEnd2EndTestEnvironment(int argc, char** argv) { void InitDawnEnd2EndTestEnvironment(int argc, char** argv) {

View File

@ -19,6 +19,7 @@
#include "common/Preprocessor.h" #include "common/Preprocessor.h"
#include "dawn/dawn_proc_table.h" #include "dawn/dawn_proc_table.h"
#include "dawn/webgpu_cpp.h" #include "dawn/webgpu_cpp.h"
#include "dawn/webgpu_cpp_print.h"
#include "dawn_native/DawnNative.h" #include "dawn_native/DawnNative.h"
#include "tests/ParamGenerator.h" #include "tests/ParamGenerator.h"
#include "tests/ToggleParser.h" #include "tests/ToggleParser.h"
@ -301,6 +302,18 @@ class DawnTestBase {
virtual std::unique_ptr<dawn_platform::Platform> CreateTestPlatform(); virtual std::unique_ptr<dawn_platform::Platform> CreateTestPlatform();
struct PrintToStringParamName {
PrintToStringParamName(const char* test);
std::string SanitizeParamName(std::string paramName, size_t index) const;
template <class ParamType>
std::string operator()(const ::testing::TestParamInfo<ParamType>& info) const {
return SanitizeParamName(::testing::PrintToStringParamName()(info), info.index);
}
std::string mTest;
};
protected: protected:
wgpu::Device device; wgpu::Device device;
wgpu::Queue queue; wgpu::Queue queue;
@ -510,7 +523,7 @@ using DawnTest = DawnTestWithParams<>;
, testName, \ , testName, \
testing::ValuesIn(::detail::GetAvailableAdapterTestParamsForBackends( \ testing::ValuesIn(::detail::GetAvailableAdapterTestParamsForBackends( \
testName##params, sizeof(testName##params) / sizeof(testName##params[0]))), \ testName##params, sizeof(testName##params) / sizeof(testName##params[0]))), \
testing::PrintToStringParamName()); \ DawnTestBase::PrintToStringParamName(#testName)); \
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(testName) GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(testName)
// Instantiate the test once for each backend provided in the first param list. // Instantiate the test once for each backend provided in the first param list.
@ -526,13 +539,13 @@ using DawnTest = DawnTestWithParams<>;
#define DAWN_INSTANTIATE_TEST_P(testName, ...) \ #define DAWN_INSTANTIATE_TEST_P(testName, ...) \
INSTANTIATE_TEST_SUITE_P( \ INSTANTIATE_TEST_SUITE_P( \
, testName, ::testing::ValuesIn(MakeParamGenerator<testName::ParamType>(__VA_ARGS__)), \ , testName, ::testing::ValuesIn(MakeParamGenerator<testName::ParamType>(__VA_ARGS__)), \
testing::PrintToStringParamName()); \ DawnTestBase::PrintToStringParamName(#testName)); \
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(testName) GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(testName)
// Implementation for DAWN_TEST_PARAM_STRUCT to declare/print struct fields. // Implementation for DAWN_TEST_PARAM_STRUCT to declare/print struct fields.
#define DAWN_TEST_PARAM_STRUCT_DECL_STRUCT_FIELD(Type) Type DAWN_PP_CONCATENATE(m, Type); #define DAWN_TEST_PARAM_STRUCT_DECL_STRUCT_FIELD(Type) Type DAWN_PP_CONCATENATE(m, Type);
#define DAWN_TEST_PARAM_STRUCT_PRINT_STRUCT_FIELD(Type) \ #define DAWN_TEST_PARAM_STRUCT_PRINT_STRUCT_FIELD(Type) \
o << "__" << #Type << "_" << param.DAWN_PP_CONCATENATE(m, Type); o << "; " << #Type << "=" << param.DAWN_PP_CONCATENATE(m, Type);
// Usage: DAWN_TEST_PARAM_STRUCT(Foo, TypeA, TypeB, ...) // Usage: DAWN_TEST_PARAM_STRUCT(Foo, TypeA, TypeB, ...)
// Generate a test param struct called Foo which extends AdapterTestParam and generated // Generate a test param struct called Foo which extends AdapterTestParam and generated
@ -564,7 +577,7 @@ using DawnTest = DawnTestWithParams<>;
}; \ }; \
std::ostream& operator<<(std::ostream& o, const StructName& param) { \ std::ostream& operator<<(std::ostream& o, const StructName& param) { \
o << static_cast<const AdapterTestParam&>(param); \ o << static_cast<const AdapterTestParam&>(param); \
o << "_" << static_cast<const DAWN_PP_CONCATENATE(_Dawn_, StructName)&>(param); \ o << "; " << static_cast<const DAWN_PP_CONCATENATE(_Dawn_, StructName)&>(param); \
return o; \ return o; \
} }