[llvm] 142aa1b - [Support] Move Target/CPU Printing out of CommandLine
Archibald Elliott via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 20 01:56:36 PST 2022
Author: Archibald Elliott
Date: 2022-12-20T09:56:14Z
New Revision: 142aa1bdd1dd1db9a7fecf9d157228019c794c94
URL: https://github.com/llvm/llvm-project/commit/142aa1bdd1dd1db9a7fecf9d157228019c794c94
DIFF: https://github.com/llvm/llvm-project/commit/142aa1bdd1dd1db9a7fecf9d157228019c794c94.diff
LOG: [Support] Move Target/CPU Printing out of CommandLine
This change is rather more invasive than intended. The main intention
here is to make CommandLine.cpp not rely on llvm/Support/Host.h. Right
now, this reliance is only in 3 superficial places:
- Choosing how to expand response files (in two places)
- Printing the default triple and current CPU in `--version` output.
The built in version system has a method for adding "extra version
printers", commonly used by several tools (such as llc) to report the
registered targets in the built version of LLVM. It was reasonably easy
to move the logic for printing the default triple and current CPU into
a similar function, and register it with any relevant binaries.
The incompatible change here is that now, even if
LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO is defined, most binaries
will no longer print out the default target triple and cpu when provided
with `--version`, for instance llvm-as and llvm-dis. This breakage is
intended, but the changes in this patch keep printing the default target
and detected in `llc` and `opt` as these were remarked as important
binaries in the LLVM install.
The change to expanding response files may also be controversial, but I
believe that these macros should correspond exactly to the host triple
introspection used before.
Differential Revision: https://reviews.llvm.org/D137837
Added:
Modified:
llvm/include/llvm/Support/Host.h
llvm/lib/MC/TargetRegistry.cpp
llvm/lib/Support/CommandLine.cpp
llvm/lib/Support/Host.cpp
llvm/test/tools/llvm-libtool-darwin/ignored-options.test
llvm/tools/llc/llc.cpp
llvm/tools/llvm-exegesis/llvm-exegesis.cpp
llvm/tools/llvm-mca/llvm-mca.cpp
llvm/tools/opt/opt.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/Host.h b/llvm/include/llvm/Support/Host.h
index dcebebdca6cb7..d1318b101b618 100644
--- a/llvm/include/llvm/Support/Host.h
+++ b/llvm/include/llvm/Support/Host.h
@@ -19,6 +19,7 @@ namespace llvm {
class MallocAllocator;
class StringRef;
template <typename ValueTy, typename AllocatorTy> class StringMap;
+class raw_ostream;
namespace sys {
@@ -54,6 +55,10 @@ namespace sys {
/// \return - True on success.
bool getHostCPUFeatures(StringMap<bool, MallocAllocator> &Features);
+ /// This is a function compatible with cl::AddExtraVersionPrinter, which adds
+ /// info about the current target triple and detected CPU.
+ void printDefaultTargetAndDetectedCPU(raw_ostream &OS);
+
namespace detail {
/// Helper functions to extract HostCPUName from /proc/cpuinfo on linux.
StringRef getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent);
diff --git a/llvm/lib/MC/TargetRegistry.cpp b/llvm/lib/MC/TargetRegistry.cpp
index 57444fd23784e..b54853a6e0d74 100644
--- a/llvm/lib/MC/TargetRegistry.cpp
+++ b/llvm/lib/MC/TargetRegistry.cpp
@@ -123,6 +123,7 @@ void TargetRegistry::printRegisteredTargetsForVersion(raw_ostream &OS) {
}
array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
+ OS << "\n";
OS << " Registered Targets:\n";
for (const auto &Target : Targets) {
OS << " " << Target.first;
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index 24fe04955d282..66632504d6fb6 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -27,7 +27,6 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/Triple.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Config/config.h"
#include "llvm/Support/ConvertUTF.h"
@@ -35,7 +34,6 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
-#include "llvm/Support/Host.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
@@ -1358,9 +1356,11 @@ Error ExpansionContext::expandResponseFiles(
bool cl::expandResponseFiles(int Argc, const char *const *Argv,
const char *EnvVar, StringSaver &Saver,
SmallVectorImpl<const char *> &NewArgv) {
- auto Tokenize = Triple(sys::getProcessTriple()).isOSWindows()
- ? cl::TokenizeWindowsCommandLine
- : cl::TokenizeGNUCommandLine;
+#ifdef _WIN32
+ auto Tokenize = cl::TokenizeWindowsCommandLine;
+#else
+ auto Tokenize = cl::TokenizeGNUCommandLine;
+#endif
// The environment variable specifies initial options.
if (EnvVar)
if (std::optional<std::string> EnvValue = sys::Process::GetEnv(EnvVar))
@@ -1504,9 +1504,12 @@ bool CommandLineParser::ParseCommandLineOptions(int argc,
// Expand response files.
SmallVector<const char *, 20> newArgv(argv, argv + argc);
BumpPtrAllocator A;
- ExpansionContext ECtx(A, Triple(sys::getProcessTriple()).isOSWindows()
- ? cl::TokenizeWindowsCommandLine
- : cl::TokenizeGNUCommandLine);
+#ifdef _WIN32
+ auto Tokenize = cl::TokenizeWindowsCommandLine;
+#else
+ auto Tokenize = cl::TokenizeGNUCommandLine;
+#endif
+ ExpansionContext ECtx(A, Tokenize);
if (Error Err = ECtx.expandResponseFiles(newArgv)) {
*Errs << toString(std::move(Err)) << '\n';
return false;
@@ -2535,7 +2538,7 @@ class HelpPrinterWrapper {
namespace {
class VersionPrinter {
public:
- void print() {
+ void print(std::vector<VersionPrinterTy> ExtraPrinters = {}) {
raw_ostream &OS = outs();
#ifdef PACKAGE_VENDOR
OS << PACKAGE_VENDOR << " ";
@@ -2551,15 +2554,14 @@ class VersionPrinter {
#ifndef NDEBUG
OS << " with assertions";
#endif
-#if LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
- std::string CPU = std::string(sys::getHostCPUName());
- if (CPU == "generic")
- CPU = "(unknown)";
- OS << ".\n"
- << " Default target: " << sys::getDefaultTargetTriple() << '\n'
- << " Host CPU: " << CPU;
-#endif
- OS << '\n';
+ OS << ".\n";
+
+ // Iterate over any registered extra printers and call them to add further
+ // information.
+ if (!ExtraPrinters.empty()) {
+ for (const auto &I : ExtraPrinters)
+ I(outs());
+ }
}
void operator=(bool OptionWasSpecified);
};
@@ -2686,15 +2688,7 @@ void VersionPrinter::operator=(bool OptionWasSpecified) {
CommonOptions->OverrideVersionPrinter(outs());
exit(0);
}
- print();
-
- // Iterate over any registered extra printers and call them to add further
- // information.
- if (!CommonOptions->ExtraVersionPrinters.empty()) {
- outs() << '\n';
- for (const auto &I : CommonOptions->ExtraVersionPrinters)
- I(outs());
- }
+ print(CommonOptions->ExtraVersionPrinters);
exit(0);
}
@@ -2749,7 +2743,7 @@ void cl::PrintHelpMessage(bool Hidden, bool Categorized) {
/// Utility function for printing version number.
void cl::PrintVersionMessage() {
- CommonOptions->VersionPrinterInstance.print();
+ CommonOptions->VersionPrinterInstance.print(CommonOptions->ExtraVersionPrinters);
}
void cl::SetVersionPrinter(VersionPrinterTy func) {
diff --git a/llvm/lib/Support/Host.cpp b/llvm/lib/Support/Host.cpp
index 2bd29489ed816..f2ecf0aca33fc 100644
--- a/llvm/lib/Support/Host.cpp
+++ b/llvm/lib/Support/Host.cpp
@@ -1855,3 +1855,13 @@ std::string sys::getProcessTriple() {
return PT.str();
}
+
+void sys::printDefaultTargetAndDetectedCPU(raw_ostream &OS) {
+#if LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
+ std::string CPU = std::string(sys::getHostCPUName());
+ if (CPU == "generic")
+ CPU = "(unknown)";
+ OS << " Default target: " << sys::getDefaultTargetTriple() << '\n'
+ << " Host CPU: " << CPU << '\n';
+#endif
+}
diff --git a/llvm/test/tools/llvm-libtool-darwin/ignored-options.test b/llvm/test/tools/llvm-libtool-darwin/ignored-options.test
index 3b5386eb1fb52..ce2f1af3525ca 100644
--- a/llvm/test/tools/llvm-libtool-darwin/ignored-options.test
+++ b/llvm/test/tools/llvm-libtool-darwin/ignored-options.test
@@ -3,6 +3,6 @@
# RUN: llvm-libtool-darwin -V -syslibroot foo | FileCheck %s
# RUN: llvm-libtool-darwin -h | FileCheck --check-prefix=HELP %s
-# CHECK: Default target:
+# CHECK: LLVM version
# HELP-NOT: syslibroot
diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp
index 4f8c7b9d9dbbb..c871d55314886 100644
--- a/llvm/tools/llc/llc.cpp
+++ b/llvm/tools/llc/llc.cpp
@@ -374,6 +374,8 @@ int main(int argc, char **argv) {
// Initialize debugging passes.
initializeScavengerTestPass(*Registry);
+ // Register the Target and CPU printer for --version.
+ cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU);
// Register the target printer for --version.
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
diff --git a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
index d13abd185d4f3..04aac6aa5a70d 100644
--- a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
+++ b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
@@ -582,6 +582,9 @@ int main(int Argc, char **Argv) {
InitializeAllTargets();
InitializeAllTargetMCs();
+ // Register the Target and CPU printer for --version.
+ cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU);
+
// Enable printing of available targets when flag --version is specified.
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp
index 2a27feaa5cc11..73c341891ab7b 100644
--- a/llvm/tools/llvm-mca/llvm-mca.cpp
+++ b/llvm/tools/llvm-mca/llvm-mca.cpp
@@ -322,6 +322,9 @@ int main(int argc, char **argv) {
InitializeAllAsmParsers();
InitializeAllTargetMCAs();
+ // Register the Target and CPU printer for --version.
+ cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU);
+
// Enable printing of available targets when flag --version is specified.
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp
index 63fbf3ccca2ac..b0b13fd52430a 100644
--- a/llvm/tools/opt/opt.cpp
+++ b/llvm/tools/opt/opt.cpp
@@ -469,6 +469,9 @@ int main(int argc, char **argv) {
PluginList.emplace_back(Plugin.get());
});
+ // Register the Target and CPU printer for --version.
+ cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU);
+
cl::ParseCommandLineOptions(argc, argv,
"llvm .bc -> .bc modular optimizer and analysis printer\n");
More information about the llvm-commits
mailing list