[clang-tools-extra] e8e00e3 - [pseudo] benchmark cleanups. NFC

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Tue May 17 11:23:02 PDT 2022


Author: Sam McCall
Date: 2022-05-17T20:22:42+02:00
New Revision: e8e00e342c4fadc8355d1dfb8de86fb0a3dcd5f7

URL: https://github.com/llvm/llvm-project/commit/e8e00e342c4fadc8355d1dfb8de86fb0a3dcd5f7
DIFF: https://github.com/llvm/llvm-project/commit/e8e00e342c4fadc8355d1dfb8de86fb0a3dcd5f7.diff

LOG: [pseudo] benchmark cleanups. NFC

- add missing benchmark for lex/preprocess steps
- name benchmarks after the function they're benchmarking, when appropriate
- remove unergonomic "run" prefixes from benchmark names
- give a useful error message if --grammar or --source are missing
- Use realistic example of how to run, run all benchmarks by default.
  (for someone who doesn't know the commands, this is the most useful action)
- Improve typos/wording in comment
- clean up unused vars
- avoid "parseable stream" name, which isn't a great name & not one I expected
  to escape from ClangPseudoMain

Differential Revision: https://reviews.llvm.org/D125312

Added: 
    

Modified: 
    clang-tools-extra/pseudo/benchmarks/Benchmark.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/pseudo/benchmarks/Benchmark.cpp b/clang-tools-extra/pseudo/benchmarks/Benchmark.cpp
index 5518a51166593..fb028be0c7abd 100644
--- 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 <string>
 
 using llvm::cl::desc;
-using llvm::cl::init;
 using llvm::cl::opt;
+using llvm::cl::Required;
 
 static opt<std::string> GrammarFile("grammar",
                                     desc("Parse and check a BNF grammar file."),
-                                    init(""));
-static opt<std::string> Source("source", desc("Source file"));
+                                    Required);
+static opt<std::string> Source("source", desc("Source file"), Required);
 
 namespace clang {
 namespace pseudo {
+namespace bench {
 namespace {
 
 const std::string *GrammarText = nullptr;
@@ -68,75 +69,90 @@ void setupGrammarAndSource() {
   G = Grammar::parseBNF(*GrammarText, Diags).release();
 }
 
-static void runParseBNFGrammar(benchmark::State &State) {
+static void parseBNF(benchmark::State &State) {
   std::vector<std::string> 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<uint64_t>(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<uint64_t>(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<uint64_t>(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<uint64_t>(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;
 }


        


More information about the cfe-commits mailing list