Add a custom main library for benchmarks. (#3872)

This largely reproduces the upstream one, but has a few advantages:

1) It initializes LLVM which can be important if we use the backends.
2) It parses commandline flags with Abseil allowing the use of Abseil
   flags in benchmarks.
3) It flips the default for tabular display of results which we use
   pretty heavily.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Carbon Infra Bot <carbon-external-infra@google.com>
This commit is contained in:
Chandler Carruth
2024-04-09 20:36:41 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins Richard Smith Carbon Infra Bot
parent 4ed0d95777
commit f5c01ee487
3 changed files with 50 additions and 4 deletions
+31
View File
@@ -0,0 +1,31 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <benchmark/benchmark.h>
#include "absl/flags/parse.h"
#include "common/init_llvm.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
auto main(int orig_argc, char** orig_argv) -> int {
// Do LLVM's initialization first, this will also transform UTF-16 to UTF-8.
Carbon::InitLLVM init_llvm(orig_argc, orig_argv);
// Inject a flag to override the defaults for benchmarks. This can still be
// disabled by user arguments.
llvm::SmallVector<char*> injected_argv_storage(orig_argv,
orig_argv + orig_argc + 1);
char injected_flag[] = "--benchmark_counters_tabular";
injected_argv_storage.insert(injected_argv_storage.begin() + 1,
injected_flag);
char** argv = injected_argv_storage.data();
int argc = injected_argv_storage.size() - 1;
benchmark::Initialize(&argc, argv);
absl::ParseCommandLine(argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}