diff --git a/clang-tools-extra/pseudo/benchmarks/Benchmark.cpp b/clang-tools-extra/pseudo/benchmarks/Benchmark.cpp --- a/clang-tools-extra/pseudo/benchmarks/Benchmark.cpp +++ b/clang-tools-extra/pseudo/benchmarks/Benchmark.cpp @@ -10,12 +10,12 @@ // important pieces of the pseudoparser (grammar compliation, LR table build // etc). // -// Note: make sure we build it in Relase mode. +// Note: make sure to build the benchmark in Release mode. // // Usage: // tools/clang/tools/extra/pseudo/benchmarks/ClangPseudoBenchmark \ -// --grammar=/path/to/cxx.bnf --source=/patch/to/source-to-parse.cpp \ -// --benchmark_filter=runParseOverall +// --grammar=../clang-tools-extra/pseudo/lib/cxx.bnf \ +// --source=../clang/lib/Sema/SemaDecl.cpp // //===----------------------------------------------------------------------===// @@ -35,16 +35,17 @@ #include using llvm::cl::desc; -using llvm::cl::init; using llvm::cl::opt; +using llvm::cl::Required; static opt GrammarFile("grammar", desc("Parse and check a BNF grammar file."), - init("")); -static opt Source("source", desc("Source file")); + Required); +static opt Source("source", desc("Source file"), Required); namespace clang { namespace pseudo { +namespace bench { namespace { const std::string *GrammarText = nullptr; @@ -68,75 +69,90 @@ G = Grammar::parseBNF(*GrammarText, Diags).release(); } -static void runParseBNFGrammar(benchmark::State &State) { +static void parseBNF(benchmark::State &State) { std::vector Diags; for (auto _ : State) Grammar::parseBNF(*GrammarText, Diags); } -BENCHMARK(runParseBNFGrammar); +BENCHMARK(parseBNF); -static void runBuildLR(benchmark::State &State) { +static void buildSLR(benchmark::State &State) { for (auto _ : State) - clang::pseudo::LRTable::buildSLR(*G); + LRTable::buildSLR(*G); } -BENCHMARK(runBuildLR); +BENCHMARK(buildSLR); -TokenStream parseableTokenStream() { +TokenStream lexAndPreprocess() { clang::LangOptions LangOpts = genericLangOpts(); - TokenStream RawStream = clang::pseudo::lex(*SourceText, LangOpts); + TokenStream RawStream = pseudo::lex(*SourceText, LangOpts); auto DirectiveStructure = DirectiveTree::parse(RawStream); - clang::pseudo::chooseConditionalBranches(DirectiveStructure, RawStream); + chooseConditionalBranches(DirectiveStructure, RawStream); TokenStream Cook = cook(DirectiveStructure.stripDirectives(RawStream), LangOpts); - return clang::pseudo::stripComments(Cook); + return stripComments(Cook); } -static void runPreprocessTokens(benchmark::State &State) { +static void lex(benchmark::State &State) { + clang::LangOptions LangOpts = genericLangOpts(); for (auto _ : State) - parseableTokenStream(); + clang::pseudo::lex(*SourceText, LangOpts); State.SetBytesProcessed(static_cast(State.iterations()) * SourceText->size()); } -BENCHMARK(runPreprocessTokens); +BENCHMARK(lex); -static void runGLRParse(benchmark::State &State) { +static void preprocess(benchmark::State &State) { clang::LangOptions LangOpts = genericLangOpts(); + TokenStream RawStream = clang::pseudo::lex(*SourceText, LangOpts); + for (auto _ : State) { + auto DirectiveStructure = DirectiveTree::parse(RawStream); + chooseConditionalBranches(DirectiveStructure, RawStream); + stripComments( + cook(DirectiveStructure.stripDirectives(RawStream), LangOpts)); + } + State.SetBytesProcessed(static_cast(State.iterations()) * + SourceText->size()); +} +BENCHMARK(preprocess); + +static void glrParse(benchmark::State &State) { LRTable Table = clang::pseudo::LRTable::buildSLR(*G); - TokenStream ParseableStream = parseableTokenStream(); SymbolID StartSymbol = *G->findNonterminal("translation-unit"); + TokenStream Stream = lexAndPreprocess(); for (auto _ : State) { pseudo::ForestArena Forest; pseudo::GSS GSS; - glrParse(ParseableStream, ParseParams{*G, Table, Forest, GSS}, StartSymbol); + pseudo::glrParse(Stream, ParseParams{*G, Table, Forest, GSS}, StartSymbol); } State.SetBytesProcessed(static_cast(State.iterations()) * SourceText->size()); } -BENCHMARK(runGLRParse); +BENCHMARK(glrParse); -static void runParseOverall(benchmark::State &State) { - clang::LangOptions LangOpts = genericLangOpts(); +static void full(benchmark::State &State) { LRTable Table = clang::pseudo::LRTable::buildSLR(*G); SymbolID StartSymbol = *G->findNonterminal("translation-unit"); for (auto _ : State) { + TokenStream Stream = lexAndPreprocess(); pseudo::ForestArena Forest; pseudo::GSS GSS; - glrParse(parseableTokenStream(), ParseParams{*G, Table, Forest, GSS}, - StartSymbol); + pseudo::glrParse(lexAndPreprocess(), ParseParams{*G, Table, Forest, GSS}, + StartSymbol); } State.SetBytesProcessed(static_cast(State.iterations()) * SourceText->size()); } -BENCHMARK(runParseOverall); +BENCHMARK(full); } // namespace +} // namespace bench } // namespace pseudo } // namespace clang int main(int argc, char *argv[]) { benchmark::Initialize(&argc, argv); llvm::cl::ParseCommandLineOptions(argc, argv); - clang::pseudo::setupGrammarAndSource(); + clang::pseudo::bench::setupGrammarAndSource(); benchmark::RunSpecifiedBenchmarks(); return 0; }