Skip to content

[llc] Add -M for InstPrinter options #121078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/docs/CommandGuide/llc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/MC/MCTargetOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/RISCV/compress-opt-select.ll
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/RISCV/hwasan-check-memaccess.ll
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions llvm/test/tools/llc/instprinter-options.ll
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion llvm/test/tools/llvm-mc/disassembler-options.test
Original file line number Diff line number Diff line change
@@ -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'
20 changes: 18 additions & 2 deletions llvm/tools/llc/llc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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')"));

Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions llvm/tools/llvm-mc/llvm-mc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down