sammccall created this revision.
sammccall added a reviewer: hokein.
Herald added a project: All.
sammccall requested review of this revision.
Herald added subscribers: cfe-commits, alextsao1999.
Herald added a project: clang-tools-extra.

- 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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125312

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

Index: clang-tools-extra/pseudo/benchmarks/Benchmark.cpp
===================================================================
--- clang-tools-extra/pseudo/benchmarks/Benchmark.cpp
+++ 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,72 +69,87 @@
   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();
+  TokenStream Stream = lexAndPreprocess();
   for (auto _ : State) {
     pseudo::ForestArena Forest;
     pseudo::GSS GSS;
-    glrParse(ParseableStream, ParseParams{*G, Table, Forest, GSS});
+    pseudo::glrParse(Stream, ParseParams{*G, Table, Forest, GSS});
   }
   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);
   for (auto _ : State) {
+    TokenStream Stream = lexAndPreprocess();
     pseudo::ForestArena Forest;
     pseudo::GSS GSS;
-    glrParse(parseableTokenStream(), ParseParams{*G, Table, Forest, GSS});
+    pseudo::glrParse(lexAndPreprocess(), ParseParams{*G, Table, Forest, GSS});
   }
   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;
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to