dawn-cmake/fuzzers/tint_ast_clone_fuzzer.cc
Ben Clayton ba1a8f8d05 optimization: BlockAllocator: Actually allocate in blocks
Instead of hitting the heap for each and every call to Create()

Significantly improves performance for heavy loads. Slight performance
loss for lighter loads.

A: base.bench
B: new.bench

Test name                             | Δ (A → B)    | % (A → B)
--------------------------------------+--------------+-----------
GenerateSPIRV/"simple_fragment.wgsl"  | 27.021µs     | +6.4%
GenerateMSL/"simple_compute.wgsl"     | 35.592µs     | +6.1%
GenerateMSL/"simple_vertex.wgsl"      | 37.64µs      | +5.5%
GenerateHLSL/"simple_fragment.wgsl"   | 42.145µs     | +5.2%
GenerateGLSL/"simple_fragment.wgsl"   | 31.506µs     | +4.9%
GenerateHLSL/"simple_vertex.wgsl"     | 38.843µs     | +4.7%
GenerateMSL/"simple_fragment.wgsl"    | 29.977µs     | +4.5%
GenerateSPIRV/"simple_vertex.wgsl"    | 19.882µs     | +4.2%
GenerateGLSL/"simple_vertex.wgsl"     | 24.702µs     | +3.7%
GenerateSPIRV/"simple_compute.wgsl"   | 17.652µs     | +3.2%
GenerateHLSL/"simple_compute.wgsl"    | 26.826µs     | +2.7%
GenerateGLSL/"simple_compute.wgsl"    | 11.952µs     | +1.8%
ParseWGSL/"particles.wgsl"            | -104.83µs    | -4.2%
GenerateMSL/"particles.wgsl"          | -1.079243ms  | -9.4%
GenerateSPIRV/"particles.wgsl"        | -1.012483ms  | -9.4%
GenerateGLSL/"particles.wgsl"         | -3.522106ms  | -9.5%
GenerateHLSL/"particles.wgsl"         | -1.849666ms  | -10.6%

Issue: tint:1383
Change-Id: Ib691328538c597c06a75dfba392c99d2afbd5442
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/76961
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
2022-01-22 20:32:38 +00:00

117 lines
4.2 KiB
C++

// 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 <iostream>
#include <string>
#include <unordered_set>
#include "src/reader/wgsl/parser_impl.h"
#include "src/writer/wgsl/generator.h"
#define ASSERT_EQ(A, B) \
do { \
decltype(A) assert_a = (A); \
decltype(B) assert_b = (B); \
if (assert_a != assert_b) { \
std::cerr << "ASSERT_EQ(" #A ", " #B ") failed:\n" \
<< #A << " was: " << assert_a << "\n" \
<< #B << " was: " << assert_b << "\n"; \
__builtin_trap(); \
} \
} while (false)
#define ASSERT_TRUE(A) \
do { \
decltype(A) assert_a = (A); \
if (!assert_a) { \
std::cerr << "ASSERT_TRUE(" #A ") failed:\n" \
<< #A << " was: " << assert_a << "\n"; \
__builtin_trap(); \
} \
} while (false)
[[noreturn]] void TintInternalCompilerErrorReporter(
const tint::diag::List& diagnostics) {
auto printer = tint::diag::Printer::create(stderr, true);
tint::diag::Formatter{}.format(diagnostics, printer.get());
__builtin_trap();
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
std::string str(reinterpret_cast<const char*>(data), size);
tint::SetInternalCompilerErrorReporter(&TintInternalCompilerErrorReporter);
tint::Source::File file("test.wgsl", str);
// Parse the wgsl, create the src program
tint::reader::wgsl::ParserImpl parser(&file);
parser.set_max_errors(1);
if (!parser.Parse()) {
return 0;
}
auto src = parser.program();
if (!src.IsValid()) {
return 0;
}
// Clone the src program to dst
tint::Program dst(src.Clone());
// Expect the printed strings to match
ASSERT_EQ(tint::Program::printer(&src), tint::Program::printer(&dst));
// Check that none of the AST nodes or type pointers in dst are found in src
std::unordered_set<const tint::ast::Node*> src_nodes;
for (auto* src_node : src.ASTNodes().Objects()) {
src_nodes.emplace(src_node);
}
std::unordered_set<const tint::sem::Type*> src_types;
for (auto* src_type : src.Types()) {
src_types.emplace(src_type);
}
for (auto* dst_node : dst.ASTNodes().Objects()) {
ASSERT_EQ(src_nodes.count(dst_node), 0u);
}
for (auto* dst_type : dst.Types()) {
ASSERT_EQ(src_types.count(dst_type), 0u);
}
// Regenerate the wgsl for the src program. We use this instead of the
// original source so that reformatting doesn't impact the final wgsl
// comparison.
std::string src_wgsl;
tint::writer::wgsl::Options wgsl_options;
{
auto result = tint::writer::wgsl::Generate(&src, wgsl_options);
ASSERT_TRUE(result.success);
src_wgsl = result.wgsl;
// Move the src program to a temporary that'll be dropped, so that the src
// program is released before we attempt to print the dst program. This
// guarantee that all the source program nodes and types are destructed and
// freed. ASAN should error if there's any remaining references in dst when
// we try to reconstruct the WGSL.
auto tmp = std::move(src);
}
// Print the dst program, check it matches the original source
auto result = tint::writer::wgsl::Generate(&dst, wgsl_options);
ASSERT_TRUE(result.success);
auto dst_wgsl = result.wgsl;
ASSERT_EQ(src_wgsl, dst_wgsl);
return 0;
}