[llvm] [llc] Add -M for InstPrinter options (PR #121077)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 24 17:16:33 PST 2024
https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/121077
For many targets, llvm-objdump and llvm-mc
(https://reviews.llvm.org/D103004) support -M no-aliases (e.g.
`RISCVInstPrinter::applyTargetSpecificCLOption`).
This patch implements -M for llc.
While here, rename "DisassemblerOptions" in llvm-mc to the more
appropriate "InstPrinterOptions". For llvm-mc --assemble, there is no
disassembler involved.
>From e0d465530291c242cf86a4c6423389f33cc910c1 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Tue, 24 Dec 2024 17:16:23 -0800
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.5-bogner
---
llvm/docs/CommandGuide/llc.rst | 5 +++++
llvm/include/llvm/MC/MCTargetOptions.h | 3 +++
llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp | 9 ++++++++-
.../test/CodeGen/RISCV/compress-opt-select.ll | 4 ++--
.../CodeGen/RISCV/hwasan-check-memaccess.ll | 2 +-
llvm/test/tools/llc/instprinter-options.ll | 4 ++++
.../tools/llvm-mc/disassembler-options.test | 2 +-
llvm/tools/llc/llc.cpp | 20 +++++++++++++++++--
llvm/tools/llvm-mc/llvm-mc.cpp | 11 +++++-----
9 files changed, 48 insertions(+), 12 deletions(-)
create mode 100644 llvm/test/tools/llc/instprinter-options.ll
diff --git a/llvm/docs/CommandGuide/llc.rst b/llvm/docs/CommandGuide/llc.rst
index 7f28ab3a82f7c7..900649f59c4017 100644
--- a/llvm/docs/CommandGuide/llc.rst
+++ b/llvm/docs/CommandGuide/llc.rst
@@ -43,6 +43,11 @@ End-user Options
Print a summary of command line options.
+.. option:: -M
+
+ Pass target-specific InstPrinter options.
+ Refer to the ``-M`` option of :manpage:`llvm-objdump(1)`.
+
.. option:: -o <filename>
Use ``<filename>`` as the output filename. See the summary above for more
diff --git a/llvm/include/llvm/MC/MCTargetOptions.h b/llvm/include/llvm/MC/MCTargetOptions.h
index 7b0d81faf73d2d..fd554bc0ba7c63 100644
--- a/llvm/include/llvm/MC/MCTargetOptions.h
+++ b/llvm/include/llvm/MC/MCTargetOptions.h
@@ -105,6 +105,9 @@ class MCTargetOptions {
/// integrated assembler.
std::vector<std::string> IASSearchPaths;
+ // InstPrinter options.
+ std::vector<std::string> InstPrinterOptions;
+
// Whether to emit compact-unwind for non-canonical personality
// functions on Darwins.
bool EmitCompactUnwindNonCanonical : 1;
diff --git a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
index 5757ca1b3adf89..b54fc8d73d3b46 100644
--- a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
+++ b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
@@ -21,6 +21,7 @@
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCRegisterInfo.h"
@@ -132,8 +133,10 @@ bool CodeGenTargetMachineImpl::addAsmPrinter(PassManagerBase &PM,
MCContext &Context) {
Expected<std::unique_ptr<MCStreamer>> MCStreamerOrErr =
createMCStreamer(Out, DwoOut, FileType, Context);
- if (auto Err = MCStreamerOrErr.takeError())
+ if (auto Err = MCStreamerOrErr.takeError()) {
+ Context.reportError(SMLoc(), toString(std::move(Err)));
return true;
+ }
// Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
FunctionPass *Printer =
@@ -163,6 +166,10 @@ CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out,
getTargetTriple(),
Options.MCOptions.OutputAsmVariant.value_or(MAI.getAssemblerDialect()),
MAI, MII, MRI);
+ for (StringRef Opt : Options.MCOptions.InstPrinterOptions)
+ if (!InstPrinter->applyTargetSpecificCLOption(Opt))
+ return createStringError(inconvertibleErrorCode(),
+ "invalid InstPrinter option '" + Opt + "'");
// Create a code emitter if asked to show the encoding.
std::unique_ptr<MCCodeEmitter> MCE;
diff --git a/llvm/test/CodeGen/RISCV/compress-opt-select.ll b/llvm/test/CodeGen/RISCV/compress-opt-select.ll
index 733c84ac236133..c0234f40cff56b 100644
--- a/llvm/test/CodeGen/RISCV/compress-opt-select.ll
+++ b/llvm/test/CodeGen/RISCV/compress-opt-select.ll
@@ -1,9 +1,9 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc -mtriple=riscv32 -target-abi ilp32d -mattr=+c,+f,+d \
-; RUN: -riscv-no-aliases < %s \
+; RUN: -M no-aliases < %s \
; RUN: | FileCheck -check-prefix=RV32IFDC %s
; RUN: llc -mtriple=riscv32 -target-abi ilp32d -mattr=-c,+f,+d \
-; RUN: -riscv-no-aliases < %s \
+; RUN: -M no-aliases < %s \
; RUN: | FileCheck -check-prefix=RV32IFD %s
; constant is small and fit in 6 bit (compress imm)
diff --git a/llvm/test/CodeGen/RISCV/hwasan-check-memaccess.ll b/llvm/test/CodeGen/RISCV/hwasan-check-memaccess.ll
index c4e5dcedf5ede0..cf780f4f7f76c0 100644
--- a/llvm/test/CodeGen/RISCV/hwasan-check-memaccess.ll
+++ b/llvm/test/CodeGen/RISCV/hwasan-check-memaccess.ll
@@ -1,7 +1,7 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mtriple=riscv64 < %s | FileCheck %s
; RUN: llc -mtriple=riscv64 --relocation-model=pic < %s | FileCheck %s
-; RUN: llc -mtriple=riscv64 -mattr=+c --riscv-no-aliases < %s \
+; RUN: llc -mtriple=riscv64 -mattr=+c -M no-aliases < %s \
; RUN: | FileCheck %s --check-prefix=COMPRESS
define ptr @f2(ptr %x0, ptr %x1) {
diff --git a/llvm/test/tools/llc/instprinter-options.ll b/llvm/test/tools/llc/instprinter-options.ll
new file mode 100644
index 00000000000000..dd47bb46decce7
--- /dev/null
+++ b/llvm/test/tools/llc/instprinter-options.ll
@@ -0,0 +1,4 @@
+; REQUIRES: x86-registered-target
+; RUN: not llc -mtriple=x86_64 < %s -M invalid 2>&1 | FileCheck %s --implicit-check-not=error:
+
+; CHECK: error: invalid InstPrinter option 'invalid'
diff --git a/llvm/test/tools/llvm-mc/disassembler-options.test b/llvm/test/tools/llvm-mc/disassembler-options.test
index a7fd8b1b9f8d3f..e53bfce9f47ab1 100644
--- a/llvm/test/tools/llvm-mc/disassembler-options.test
+++ b/llvm/test/tools/llvm-mc/disassembler-options.test
@@ -1,4 +1,4 @@
# RUN: export LSAN_OPTIONS=detect_leaks=0
# RUN: not llvm-mc -M invalid /dev/null 2>&1 | FileCheck %s
-# CHECK: error: invalid disassembler option 'invalid'
+# CHECK: error: invalid InstPrinter option 'invalid'
diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp
index 3694ff79b54364..89d2751dcc6044 100644
--- a/llvm/tools/llc/llc.cpp
+++ b/llvm/tools/llc/llc.cpp
@@ -69,6 +69,9 @@ static codegen::RegisterCodeGenFlags CGF;
static cl::opt<std::string>
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
+static cl::list<std::string>
+ InstPrinterOptions("M", cl::desc("InstPrinter options"));
+
static cl::opt<std::string>
InputLanguage("x", cl::desc("Input language ('ir' or 'mir')"));
@@ -512,6 +515,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
Options.MCOptions.AsmVerbose = AsmVerbose;
Options.MCOptions.PreserveAsmComments = PreserveComments;
Options.MCOptions.IASSearchPaths = IncludeDirs;
+ Options.MCOptions.InstPrinterOptions = InstPrinterOptions;
Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
if (DwarfDirectory.getPosition()) {
Options.MCOptions.MCUseDwarfDirectory =
@@ -688,6 +692,17 @@ static int compileModule(char **argv, LLVMContext &Context) {
MachineModuleInfoWrapperPass *MMIWP =
new MachineModuleInfoWrapperPass(Target.get());
+ // Set a temporary diagnostic handler. This is used before
+ // MachineModuleInfoWrapperPass::doInitialization for features like -M.
+ bool HasMCErrors = false;
+ MCContext &MCCtx = MMIWP->getMMI().getContext();
+ MCCtx.setDiagnosticHandler([&](const SMDiagnostic &SMD, bool IsInlineAsm,
+ const SourceMgr &SrcMgr,
+ std::vector<const MDNode *> &LocInfos) {
+ WithColor::error(errs(), argv0) << SMD.getMessage() << '\n';
+ HasMCErrors = true;
+ });
+
// Construct a custom pass pipeline that starts after instruction
// selection.
if (!getRunPassNames().empty()) {
@@ -722,7 +737,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
} else if (Target->addPassesToEmitFile(
PM, *OS, DwoOut ? &DwoOut->os() : nullptr,
codegen::getFileType(), NoVerify, MMIWP)) {
- reportError("target does not support generation of this file type");
+ if (!HasMCErrors)
+ reportError("target does not support generation of this file type");
}
const_cast<TargetLoweringObjectFile *>(Target->getObjFileLowering())
@@ -750,7 +766,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
PM.run(*M);
- if (Context.getDiagHandlerPtr()->HasErrors)
+ if (Context.getDiagHandlerPtr()->HasErrors || HasMCErrors)
return 1;
// Compare the two outputs and make sure they're the same
diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp
index 70f92d09aded7e..db2c6eeb22519a 100644
--- a/llvm/tools/llvm-mc/llvm-mc.cpp
+++ b/llvm/tools/llvm-mc/llvm-mc.cpp
@@ -49,9 +49,9 @@ static cl::opt<std::string> InputFilename(cl::Positional,
cl::desc("<input file>"),
cl::init("-"), cl::cat(MCCategory));
-static cl::list<std::string>
- DisassemblerOptions("M", cl::desc("Disassembler options"),
- cl::cat(MCCategory));
+static cl::list<std::string> InstPrinterOptions("M",
+ cl::desc("InstPrinter options"),
+ cl::cat(MCCategory));
static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
cl::value_desc("filename"),
@@ -369,6 +369,7 @@ int main(int argc, char **argv) {
MCOptions.ShowMCInst = ShowInst;
MCOptions.AsmVerbose = true;
MCOptions.MCUseDwarfDirectory = MCTargetOptions::EnableDwarfDirectory;
+ MCOptions.InstPrinterOptions = InstPrinterOptions;
setDwarfDebugFlags(argc, argv);
setDwarfDebugProducer();
@@ -531,9 +532,9 @@ int main(int argc, char **argv) {
return 1;
}
- for (StringRef Opt : DisassemblerOptions)
+ for (StringRef Opt : InstPrinterOptions)
if (!IP->applyTargetSpecificCLOption(Opt)) {
- WithColor::error() << "invalid disassembler option '" << Opt << "'\n";
+ WithColor::error() << "invalid InstPrinter option '" << Opt << "'\n";
return 1;
}
More information about the llvm-commits
mailing list