[llc] Add -M for InstPrinter options

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.

Pull Request: https://github.com/llvm/llvm-project/pull/121078
This commit is contained in:
Fangrui Song
2025-04-08 19:34:03 -07:00
committed by GitHub
parent 333f2c3341
commit 02b377d8f7
9 changed files with 47 additions and 12 deletions

View File

@@ -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

View File

@@ -105,6 +105,9 @@ public:
/// 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;

View File

@@ -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 (!MCStreamerOrErr) {
Context.reportError(SMLoc(), toString(MCStreamerOrErr.takeError()));
return true;
}
// Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
FunctionPass *Printer =
@@ -163,6 +166,9 @@ 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("invalid InstPrinter option '" + Opt + "'");
// Create a code emitter if asked to show the encoding.
std::unique_ptr<MCCodeEmitter> MCE;

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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'

View File

@@ -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'

View File

@@ -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')"));
@@ -498,6 +501,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 =
@@ -674,6 +678,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()) {
@@ -708,7 +723,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())
@@ -736,7 +752,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

View File

@@ -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;
}