Skip to content

Commit 142aa1b

Browse files
committed
[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
1 parent b76cc30 commit 142aa1b

File tree

9 files changed

+50
-29
lines changed

9 files changed

+50
-29
lines changed

llvm/include/llvm/Support/Host.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace llvm {
1919
class MallocAllocator;
2020
class StringRef;
2121
template <typename ValueTy, typename AllocatorTy> class StringMap;
22+
class raw_ostream;
2223

2324
namespace sys {
2425

@@ -54,6 +55,10 @@ namespace sys {
5455
/// \return - True on success.
5556
bool getHostCPUFeatures(StringMap<bool, MallocAllocator> &Features);
5657

58+
/// This is a function compatible with cl::AddExtraVersionPrinter, which adds
59+
/// info about the current target triple and detected CPU.
60+
void printDefaultTargetAndDetectedCPU(raw_ostream &OS);
61+
5762
namespace detail {
5863
/// Helper functions to extract HostCPUName from /proc/cpuinfo on linux.
5964
StringRef getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent);

llvm/lib/MC/TargetRegistry.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ void TargetRegistry::printRegisteredTargetsForVersion(raw_ostream &OS) {
123123
}
124124
array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
125125

126+
OS << "\n";
126127
OS << " Registered Targets:\n";
127128
for (const auto &Target : Targets) {
128129
OS << " " << Target.first;

llvm/lib/Support/CommandLine.cpp

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@
2727
#include "llvm/ADT/StringExtras.h"
2828
#include "llvm/ADT/StringMap.h"
2929
#include "llvm/ADT/StringRef.h"
30-
#include "llvm/ADT/Triple.h"
3130
#include "llvm/ADT/Twine.h"
3231
#include "llvm/Config/config.h"
3332
#include "llvm/Support/ConvertUTF.h"
3433
#include "llvm/Support/Debug.h"
3534
#include "llvm/Support/Error.h"
3635
#include "llvm/Support/ErrorHandling.h"
3736
#include "llvm/Support/FileSystem.h"
38-
#include "llvm/Support/Host.h"
3937
#include "llvm/Support/ManagedStatic.h"
4038
#include "llvm/Support/MemoryBuffer.h"
4139
#include "llvm/Support/Path.h"
@@ -1358,9 +1356,11 @@ Error ExpansionContext::expandResponseFiles(
13581356
bool cl::expandResponseFiles(int Argc, const char *const *Argv,
13591357
const char *EnvVar, StringSaver &Saver,
13601358
SmallVectorImpl<const char *> &NewArgv) {
1361-
auto Tokenize = Triple(sys::getProcessTriple()).isOSWindows()
1362-
? cl::TokenizeWindowsCommandLine
1363-
: cl::TokenizeGNUCommandLine;
1359+
#ifdef _WIN32
1360+
auto Tokenize = cl::TokenizeWindowsCommandLine;
1361+
#else
1362+
auto Tokenize = cl::TokenizeGNUCommandLine;
1363+
#endif
13641364
// The environment variable specifies initial options.
13651365
if (EnvVar)
13661366
if (std::optional<std::string> EnvValue = sys::Process::GetEnv(EnvVar))
@@ -1504,9 +1504,12 @@ bool CommandLineParser::ParseCommandLineOptions(int argc,
15041504
// Expand response files.
15051505
SmallVector<const char *, 20> newArgv(argv, argv + argc);
15061506
BumpPtrAllocator A;
1507-
ExpansionContext ECtx(A, Triple(sys::getProcessTriple()).isOSWindows()
1508-
? cl::TokenizeWindowsCommandLine
1509-
: cl::TokenizeGNUCommandLine);
1507+
#ifdef _WIN32
1508+
auto Tokenize = cl::TokenizeWindowsCommandLine;
1509+
#else
1510+
auto Tokenize = cl::TokenizeGNUCommandLine;
1511+
#endif
1512+
ExpansionContext ECtx(A, Tokenize);
15101513
if (Error Err = ECtx.expandResponseFiles(newArgv)) {
15111514
*Errs << toString(std::move(Err)) << '\n';
15121515
return false;
@@ -2535,7 +2538,7 @@ class HelpPrinterWrapper {
25352538
namespace {
25362539
class VersionPrinter {
25372540
public:
2538-
void print() {
2541+
void print(std::vector<VersionPrinterTy> ExtraPrinters = {}) {
25392542
raw_ostream &OS = outs();
25402543
#ifdef PACKAGE_VENDOR
25412544
OS << PACKAGE_VENDOR << " ";
@@ -2551,15 +2554,14 @@ class VersionPrinter {
25512554
#ifndef NDEBUG
25522555
OS << " with assertions";
25532556
#endif
2554-
#if LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
2555-
std::string CPU = std::string(sys::getHostCPUName());
2556-
if (CPU == "generic")
2557-
CPU = "(unknown)";
2558-
OS << ".\n"
2559-
<< " Default target: " << sys::getDefaultTargetTriple() << '\n'
2560-
<< " Host CPU: " << CPU;
2561-
#endif
2562-
OS << '\n';
2557+
OS << ".\n";
2558+
2559+
// Iterate over any registered extra printers and call them to add further
2560+
// information.
2561+
if (!ExtraPrinters.empty()) {
2562+
for (const auto &I : ExtraPrinters)
2563+
I(outs());
2564+
}
25632565
}
25642566
void operator=(bool OptionWasSpecified);
25652567
};
@@ -2686,15 +2688,7 @@ void VersionPrinter::operator=(bool OptionWasSpecified) {
26862688
CommonOptions->OverrideVersionPrinter(outs());
26872689
exit(0);
26882690
}
2689-
print();
2690-
2691-
// Iterate over any registered extra printers and call them to add further
2692-
// information.
2693-
if (!CommonOptions->ExtraVersionPrinters.empty()) {
2694-
outs() << '\n';
2695-
for (const auto &I : CommonOptions->ExtraVersionPrinters)
2696-
I(outs());
2697-
}
2691+
print(CommonOptions->ExtraVersionPrinters);
26982692

26992693
exit(0);
27002694
}
@@ -2749,7 +2743,7 @@ void cl::PrintHelpMessage(bool Hidden, bool Categorized) {
27492743

27502744
/// Utility function for printing version number.
27512745
void cl::PrintVersionMessage() {
2752-
CommonOptions->VersionPrinterInstance.print();
2746+
CommonOptions->VersionPrinterInstance.print(CommonOptions->ExtraVersionPrinters);
27532747
}
27542748

27552749
void cl::SetVersionPrinter(VersionPrinterTy func) {

llvm/lib/Support/Host.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,3 +1855,13 @@ std::string sys::getProcessTriple() {
18551855

18561856
return PT.str();
18571857
}
1858+
1859+
void sys::printDefaultTargetAndDetectedCPU(raw_ostream &OS) {
1860+
#if LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
1861+
std::string CPU = std::string(sys::getHostCPUName());
1862+
if (CPU == "generic")
1863+
CPU = "(unknown)";
1864+
OS << " Default target: " << sys::getDefaultTargetTriple() << '\n'
1865+
<< " Host CPU: " << CPU << '\n';
1866+
#endif
1867+
}

llvm/test/tools/llvm-libtool-darwin/ignored-options.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
# RUN: llvm-libtool-darwin -V -syslibroot foo | FileCheck %s
44
# RUN: llvm-libtool-darwin -h | FileCheck --check-prefix=HELP %s
55

6-
# CHECK: Default target:
6+
# CHECK: LLVM version
77

88
# HELP-NOT: syslibroot

llvm/tools/llc/llc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ int main(int argc, char **argv) {
374374
// Initialize debugging passes.
375375
initializeScavengerTestPass(*Registry);
376376

377+
// Register the Target and CPU printer for --version.
378+
cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU);
377379
// Register the target printer for --version.
378380
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
379381

llvm/tools/llvm-exegesis/llvm-exegesis.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,9 @@ int main(int Argc, char **Argv) {
582582
InitializeAllTargets();
583583
InitializeAllTargetMCs();
584584

585+
// Register the Target and CPU printer for --version.
586+
cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU);
587+
585588
// Enable printing of available targets when flag --version is specified.
586589
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
587590

llvm/tools/llvm-mca/llvm-mca.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ int main(int argc, char **argv) {
322322
InitializeAllAsmParsers();
323323
InitializeAllTargetMCAs();
324324

325+
// Register the Target and CPU printer for --version.
326+
cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU);
327+
325328
// Enable printing of available targets when flag --version is specified.
326329
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
327330

llvm/tools/opt/opt.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,9 @@ int main(int argc, char **argv) {
469469
PluginList.emplace_back(Plugin.get());
470470
});
471471

472+
// Register the Target and CPU printer for --version.
473+
cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU);
474+
472475
cl::ParseCommandLineOptions(argc, argv,
473476
"llvm .bc -> .bc modular optimizer and analysis printer\n");
474477

0 commit comments

Comments
 (0)