Skip to content

Commit 02b377d

Browse files
authored
[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: #121078
1 parent 333f2c3 commit 02b377d

File tree

9 files changed

+47
-12
lines changed

9 files changed

+47
-12
lines changed

llvm/docs/CommandGuide/llc.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ End-user Options
4343

4444
Print a summary of command line options.
4545

46+
.. option:: -M
47+
48+
Pass target-specific InstPrinter options.
49+
Refer to the ``-M`` option of :manpage:`llvm-objdump(1)`.
50+
4651
.. option:: -o <filename>
4752

4853
Use ``<filename>`` as the output filename. See the summary above for more

llvm/include/llvm/MC/MCTargetOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ class MCTargetOptions {
105105
/// integrated assembler.
106106
std::vector<std::string> IASSearchPaths;
107107

108+
// InstPrinter options.
109+
std::vector<std::string> InstPrinterOptions;
110+
108111
// Whether to emit compact-unwind for non-canonical personality
109112
// functions on Darwins.
110113
bool EmitCompactUnwindNonCanonical : 1;

llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/MC/MCAsmInfo.h"
2222
#include "llvm/MC/MCCodeEmitter.h"
2323
#include "llvm/MC/MCContext.h"
24+
#include "llvm/MC/MCInstPrinter.h"
2425
#include "llvm/MC/MCInstrInfo.h"
2526
#include "llvm/MC/MCObjectWriter.h"
2627
#include "llvm/MC/MCRegisterInfo.h"
@@ -132,8 +133,10 @@ bool CodeGenTargetMachineImpl::addAsmPrinter(PassManagerBase &PM,
132133
MCContext &Context) {
133134
Expected<std::unique_ptr<MCStreamer>> MCStreamerOrErr =
134135
createMCStreamer(Out, DwoOut, FileType, Context);
135-
if (auto Err = MCStreamerOrErr.takeError())
136+
if (!MCStreamerOrErr) {
137+
Context.reportError(SMLoc(), toString(MCStreamerOrErr.takeError()));
136138
return true;
139+
}
137140

138141
// Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
139142
FunctionPass *Printer =
@@ -163,6 +166,9 @@ CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out,
163166
getTargetTriple(),
164167
Options.MCOptions.OutputAsmVariant.value_or(MAI.getAssemblerDialect()),
165168
MAI, MII, MRI);
169+
for (StringRef Opt : Options.MCOptions.InstPrinterOptions)
170+
if (!InstPrinter->applyTargetSpecificCLOption(Opt))
171+
return createStringError("invalid InstPrinter option '" + Opt + "'");
166172

167173
// Create a code emitter if asked to show the encoding.
168174
std::unique_ptr<MCCodeEmitter> MCE;

llvm/test/CodeGen/RISCV/compress-opt-select.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
22
; RUN: llc -mtriple=riscv32 -target-abi ilp32d -mattr=+c,+f,+d \
3-
; RUN: -riscv-no-aliases < %s \
3+
; RUN: -M no-aliases < %s \
44
; RUN: | FileCheck -check-prefix=RV32IFDC %s
55
; RUN: llc -mtriple=riscv32 -target-abi ilp32d -mattr=-c,+f,+d \
6-
; RUN: -riscv-no-aliases < %s \
6+
; RUN: -M no-aliases < %s \
77
; RUN: | FileCheck -check-prefix=RV32IFD %s
88

99
; constant is small and fit in 6 bit (compress imm)

llvm/test/CodeGen/RISCV/hwasan-check-memaccess.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
22
; RUN: llc -mtriple=riscv64 < %s | FileCheck %s
33
; RUN: llc -mtriple=riscv64 --relocation-model=pic < %s | FileCheck %s
4-
; RUN: llc -mtriple=riscv64 -mattr=+c --riscv-no-aliases < %s \
4+
; RUN: llc -mtriple=riscv64 -mattr=+c -M no-aliases < %s \
55
; RUN: | FileCheck %s --check-prefix=COMPRESS
66

77
define ptr @f2(ptr %x0, ptr %x1) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
; REQUIRES: x86-registered-target
2+
; RUN: not llc -mtriple=x86_64 < %s -M invalid 2>&1 | FileCheck %s --implicit-check-not=error:
3+
4+
; CHECK: error: invalid InstPrinter option 'invalid'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# RUN: export LSAN_OPTIONS=detect_leaks=0
22
# RUN: not llvm-mc -M invalid /dev/null 2>&1 | FileCheck %s
33

4-
# CHECK: error: invalid disassembler option 'invalid'
4+
# CHECK: error: invalid InstPrinter option 'invalid'

llvm/tools/llc/llc.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ static codegen::RegisterCodeGenFlags CGF;
6969
static cl::opt<std::string>
7070
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
7171

72+
static cl::list<std::string>
73+
InstPrinterOptions("M", cl::desc("InstPrinter options"));
74+
7275
static cl::opt<std::string>
7376
InputLanguage("x", cl::desc("Input language ('ir' or 'mir')"));
7477

@@ -498,6 +501,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
498501
Options.MCOptions.AsmVerbose = AsmVerbose;
499502
Options.MCOptions.PreserveAsmComments = PreserveComments;
500503
Options.MCOptions.IASSearchPaths = IncludeDirs;
504+
Options.MCOptions.InstPrinterOptions = InstPrinterOptions;
501505
Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
502506
if (DwarfDirectory.getPosition()) {
503507
Options.MCOptions.MCUseDwarfDirectory =
@@ -674,6 +678,17 @@ static int compileModule(char **argv, LLVMContext &Context) {
674678
MachineModuleInfoWrapperPass *MMIWP =
675679
new MachineModuleInfoWrapperPass(Target.get());
676680

681+
// Set a temporary diagnostic handler. This is used before
682+
// MachineModuleInfoWrapperPass::doInitialization for features like -M.
683+
bool HasMCErrors = false;
684+
MCContext &MCCtx = MMIWP->getMMI().getContext();
685+
MCCtx.setDiagnosticHandler([&](const SMDiagnostic &SMD, bool IsInlineAsm,
686+
const SourceMgr &SrcMgr,
687+
std::vector<const MDNode *> &LocInfos) {
688+
WithColor::error(errs(), argv0) << SMD.getMessage() << '\n';
689+
HasMCErrors = true;
690+
});
691+
677692
// Construct a custom pass pipeline that starts after instruction
678693
// selection.
679694
if (!getRunPassNames().empty()) {
@@ -708,7 +723,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
708723
} else if (Target->addPassesToEmitFile(
709724
PM, *OS, DwoOut ? &DwoOut->os() : nullptr,
710725
codegen::getFileType(), NoVerify, MMIWP)) {
711-
reportError("target does not support generation of this file type");
726+
if (!HasMCErrors)
727+
reportError("target does not support generation of this file type");
712728
}
713729

714730
const_cast<TargetLoweringObjectFile *>(Target->getObjFileLowering())
@@ -736,7 +752,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
736752

737753
PM.run(*M);
738754

739-
if (Context.getDiagHandlerPtr()->HasErrors)
755+
if (Context.getDiagHandlerPtr()->HasErrors || HasMCErrors)
740756
return 1;
741757

742758
// Compare the two outputs and make sure they're the same

llvm/tools/llvm-mc/llvm-mc.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ static cl::opt<std::string> InputFilename(cl::Positional,
4949
cl::desc("<input file>"),
5050
cl::init("-"), cl::cat(MCCategory));
5151

52-
static cl::list<std::string>
53-
DisassemblerOptions("M", cl::desc("Disassembler options"),
54-
cl::cat(MCCategory));
52+
static cl::list<std::string> InstPrinterOptions("M",
53+
cl::desc("InstPrinter options"),
54+
cl::cat(MCCategory));
5555

5656
static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
5757
cl::value_desc("filename"),
@@ -369,6 +369,7 @@ int main(int argc, char **argv) {
369369
MCOptions.ShowMCInst = ShowInst;
370370
MCOptions.AsmVerbose = true;
371371
MCOptions.MCUseDwarfDirectory = MCTargetOptions::EnableDwarfDirectory;
372+
MCOptions.InstPrinterOptions = InstPrinterOptions;
372373

373374
setDwarfDebugFlags(argc, argv);
374375
setDwarfDebugProducer();
@@ -531,9 +532,9 @@ int main(int argc, char **argv) {
531532
return 1;
532533
}
533534

534-
for (StringRef Opt : DisassemblerOptions)
535+
for (StringRef Opt : InstPrinterOptions)
535536
if (!IP->applyTargetSpecificCLOption(Opt)) {
536-
WithColor::error() << "invalid disassembler option '" << Opt << "'\n";
537+
WithColor::error() << "invalid InstPrinter option '" << Opt << "'\n";
537538
return 1;
538539
}
539540

0 commit comments

Comments
 (0)