MCAsmStreamer: Replace the MCInstPrinter * parameter with unique_ptr

... to clarify ownership, aligning with other parameters. Using
`std::unique_ptr` encourages users to manage `createMCInstPrinter` with
a unique_ptr instead of a raw pointer, reducing the risk of memory
leaks.

* llvm-mc: fix a leak and update llvm/test/tools/llvm-mc/disassembler-options.test
* #121078 copied the llvm-mc code to CodeGenTargetMachineImpl and made
  the same mistake. Fixed by 2b8cc651dca0c000ee18ec79bd5de4826156c9d6

Using unique_ptr requires #include MCInstPrinter.h in a few translation
units.

* Delete a createAsmStreamer overload I deprecated in 2024
* SystemZMCTargetDesc.cpp: rename to `createSystemZAsmStreamer` to fix
  an overload conflict.

Pull Request: https://github.com/llvm/llvm-project/pull/135128
This commit is contained in:
Fangrui Song 2025-04-10 21:25:35 -07:00 committed by GitHub
parent ba93fe97c2
commit c04d9d57ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 80 additions and 71 deletions

View File

@ -134,13 +134,14 @@ void dumpFunction(const BinaryFunction &BF) {
std::unique_ptr<MCAsmBackend> MAB(
BC.TheTarget->createMCAsmBackend(*BC.STI, *BC.MRI, MCTargetOptions()));
int AsmPrinterVariant = BC.AsmInfo->getAssemblerDialect();
MCInstPrinter *InstructionPrinter(BC.TheTarget->createMCInstPrinter(
*BC.TheTriple, AsmPrinterVariant, *BC.AsmInfo, *BC.MII, *BC.MRI));
std::unique_ptr<MCInstPrinter> InstructionPrinter(
BC.TheTarget->createMCInstPrinter(*BC.TheTriple, AsmPrinterVariant,
*BC.AsmInfo, *BC.MII, *BC.MRI));
auto FOut = std::make_unique<formatted_raw_ostream>(OS);
FOut->SetUnbuffered();
std::unique_ptr<MCStreamer> AsmStreamer(
createAsmStreamer(*LocalCtx, std::move(FOut), InstructionPrinter,
std::move(MCEInstance.MCE), std::move(MAB)));
std::unique_ptr<MCStreamer> AsmStreamer(createAsmStreamer(
*LocalCtx, std::move(FOut), std::move(InstructionPrinter),
std::move(MCEInstance.MCE), std::move(MAB)));
AsmStreamer->initSections(true, *BC.STI);
std::unique_ptr<TargetMachine> TM(BC.TheTarget->createTargetMachine(
*BC.TheTriple, "", "", TargetOptions(), std::nullopt));

View File

@ -26,6 +26,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/MCObjectFileInfo.h"
#include "llvm/MC/MCObjectWriter.h"
@ -541,8 +542,8 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
// FIXME: There is a bit of code duplication with addPassesToEmitFile.
if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
MCInstPrinter *IP = TheTarget->createMCInstPrinter(
llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI);
std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI));
std::unique_ptr<MCCodeEmitter> CE;
if (Opts.ShowEncoding)
@ -551,7 +552,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
auto FOut = std::make_unique<formatted_raw_ostream>(*Out);
Str.reset(TheTarget->createAsmStreamer(Ctx, std::move(FOut), IP,
Str.reset(TheTarget->createAsmStreamer(Ctx, std::move(FOut), std::move(IP),
std::move(CE), std::move(MAB)));
} else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
Str.reset(createNullStreamer(Ctx));

View File

@ -286,7 +286,6 @@ private:
MCAsmBackend *MAB; // Owned by MCStreamer
std::unique_ptr<MCInstrInfo> MII;
std::unique_ptr<MCSubtargetInfo> MSTI;
MCInstPrinter *MIP; // Owned by AsmPrinter
MCCodeEmitter *MCE; // Owned by MCStreamer
MCStreamer *MS; // Owned by AsmPrinter
std::unique_ptr<TargetMachine> TM;

View File

@ -84,10 +84,11 @@ MCStreamer *createNullStreamer(MCContext &Ctx);
///
/// \param ShowInst - Whether to show the MCInst representation inline with
/// the assembly.
MCStreamer *
createAsmStreamer(MCContext &Ctx, std::unique_ptr<formatted_raw_ostream> OS,
MCInstPrinter *InstPrint, std::unique_ptr<MCCodeEmitter> &&CE,
std::unique_ptr<MCAsmBackend> &&TAB);
MCStreamer *createAsmStreamer(MCContext &Ctx,
std::unique_ptr<formatted_raw_ostream> OS,
std::unique_ptr<MCInstPrinter> InstPrint,
std::unique_ptr<MCCodeEmitter> CE,
std::unique_ptr<MCAsmBackend> TAB);
MCStreamer *createELFStreamer(MCContext &Ctx,
std::unique_ptr<MCAsmBackend> &&TAB,
@ -208,10 +209,10 @@ public:
using AsmTargetStreamerCtorTy =
MCTargetStreamer *(*)(MCStreamer &S, formatted_raw_ostream &OS,
MCInstPrinter *InstPrint);
using AsmStreamerCtorTy =
MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<formatted_raw_ostream> OS,
MCInstPrinter *IP, std::unique_ptr<MCCodeEmitter> CE,
std::unique_ptr<MCAsmBackend> TAB);
using AsmStreamerCtorTy = MCStreamer
*(*)(MCContext & Ctx, std::unique_ptr<formatted_raw_ostream> OS,
std::unique_ptr<MCInstPrinter> IP, std::unique_ptr<MCCodeEmitter> CE,
std::unique_ptr<MCAsmBackend> TAB);
using ObjectTargetStreamerCtorTy =
MCTargetStreamer *(*)(MCStreamer &S, const MCSubtargetInfo &STI);
using MCRelocationInfoCtorTy = MCRelocationInfo *(*)(const Triple &TT,
@ -552,7 +553,7 @@ public:
MCStreamer *createAsmStreamer(MCContext &Ctx,
std::unique_ptr<formatted_raw_ostream> OS,
MCInstPrinter *IP,
std::unique_ptr<MCInstPrinter> IP,
std::unique_ptr<MCCodeEmitter> CE,
std::unique_ptr<MCAsmBackend> TAB) const;

View File

@ -162,16 +162,13 @@ CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out,
switch (FileType) {
case CodeGenFileType::AssemblyFile: {
MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
std::unique_ptr<MCInstPrinter> InstPrinter(getTarget().createMCInstPrinter(
getTargetTriple(),
Options.MCOptions.OutputAsmVariant.value_or(MAI.getAssemblerDialect()),
MAI, MII, MRI);
for (StringRef Opt : Options.MCOptions.InstPrinterOptions) {
if (!InstPrinter->applyTargetSpecificCLOption(Opt)) {
delete InstPrinter;
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;
@ -182,7 +179,8 @@ CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out,
getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions));
auto FOut = std::make_unique<formatted_raw_ostream>(Out);
MCStreamer *S = getTarget().createAsmStreamer(
Context, std::move(FOut), InstPrinter, std::move(MCE), std::move(MAB));
Context, std::move(FOut), std::move(InstPrinter), std::move(MCE),
std::move(MAB));
AsmStreamer.reset(S);
break;
}

View File

@ -14,6 +14,7 @@
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCStreamer.h"
@ -100,10 +101,10 @@ Error DwarfStreamer::init(Triple TheTriple,
switch (OutFileType) {
case DWARFLinker::OutputFileType::Assembly: {
MIP = TheTarget->createMCInstPrinter(TheTriple, MAI->getAssemblerDialect(),
*MAI, *MII, *MRI);
std::unique_ptr<MCInstPrinter> MIP(TheTarget->createMCInstPrinter(
TheTriple, MAI->getAssemblerDialect(), *MAI, *MII, *MRI));
MS = TheTarget->createAsmStreamer(
*MC, std::make_unique<formatted_raw_ostream>(OutFile), MIP,
*MC, std::make_unique<formatted_raw_ostream>(OutFile), std::move(MIP),
std::unique_ptr<MCCodeEmitter>(MCE),
std::unique_ptr<MCAsmBackend>(MAB));
break;

View File

@ -10,6 +10,7 @@
#include "DWARFLinkerCompileUnit.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCTargetOptions.h"
@ -79,10 +80,10 @@ Error DwarfEmitterImpl::init(Triple TheTriple,
switch (OutFileType) {
case DWARFLinker::OutputFileType::Assembly: {
MIP = TheTarget->createMCInstPrinter(TheTriple, MAI->getAssemblerDialect(),
*MAI, *MII, *MRI);
std::unique_ptr<MCInstPrinter> MIP(TheTarget->createMCInstPrinter(
TheTriple, MAI->getAssemblerDialect(), *MAI, *MII, *MRI));
MS = TheTarget->createAsmStreamer(
*MC, std::make_unique<formatted_raw_ostream>(OutFile), MIP,
*MC, std::make_unique<formatted_raw_ostream>(OutFile), std::move(MIP),
std::unique_ptr<MCCodeEmitter>(MCE),
std::unique_ptr<MCAsmBackend>(MAB));
break;

View File

@ -16,6 +16,7 @@
#include "llvm/DWARFLinker/Parallel/DWARFLinker.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCRegisterInfo.h"
@ -110,7 +111,7 @@ private:
MCAsmBackend *MAB; // Owned by MCStreamer
std::unique_ptr<MCInstrInfo> MII;
std::unique_ptr<MCSubtargetInfo> MSTI;
MCInstPrinter *MIP; // Owned by AsmPrinter
std::unique_ptr<MCInstPrinter> MIP; // Owned by AsmPrinter
MCCodeEmitter *MCE; // Owned by MCStreamer
MCStreamer *MS; // Owned by AsmPrinter
std::unique_ptr<TargetMachine> TM;

View File

@ -74,10 +74,11 @@ class MCAsmStreamer final : public MCStreamer {
public:
MCAsmStreamer(MCContext &Context, std::unique_ptr<formatted_raw_ostream> os,
MCInstPrinter *printer, std::unique_ptr<MCCodeEmitter> emitter,
std::unique_ptr<MCInstPrinter> printer,
std::unique_ptr<MCCodeEmitter> emitter,
std::unique_ptr<MCAsmBackend> asmbackend)
: MCStreamer(Context), OSOwner(std::move(os)), OS(*OSOwner),
MAI(Context.getAsmInfo()), InstPrinter(printer),
MAI(Context.getAsmInfo()), InstPrinter(std::move(printer)),
Assembler(std::make_unique<MCAssembler>(
Context, std::move(asmbackend), std::move(emitter),
(asmbackend) ? asmbackend->createObjectWriter(NullStream)
@ -2649,9 +2650,9 @@ void MCAsmStreamer::emitDwarfAdvanceLineAddr(int64_t LineDelta,
MCStreamer *llvm::createAsmStreamer(MCContext &Context,
std::unique_ptr<formatted_raw_ostream> OS,
MCInstPrinter *IP,
std::unique_ptr<MCCodeEmitter> &&CE,
std::unique_ptr<MCAsmBackend> &&MAB) {
return new MCAsmStreamer(Context, std::move(OS), IP, std::move(CE),
std::unique_ptr<MCInstPrinter> IP,
std::unique_ptr<MCCodeEmitter> CE,
std::unique_ptr<MCAsmBackend> MAB) {
return new MCAsmStreamer(Context, std::move(OS), std::move(IP), std::move(CE),
std::move(MAB));
}

View File

@ -12,6 +12,7 @@
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCObjectStreamer.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/Support/raw_ostream.h"
@ -79,19 +80,20 @@ MCStreamer *Target::createMCObjectStreamer(
MCStreamer *Target::createAsmStreamer(MCContext &Ctx,
std::unique_ptr<formatted_raw_ostream> OS,
MCInstPrinter *IP,
std::unique_ptr<MCInstPrinter> IP,
std::unique_ptr<MCCodeEmitter> CE,
std::unique_ptr<MCAsmBackend> TAB) const {
MCInstPrinter *Printer = IP.get();
formatted_raw_ostream &OSRef = *OS;
MCStreamer *S;
if (AsmStreamerCtorFn)
S = AsmStreamerCtorFn(Ctx, std::move(OS), IP, std::move(CE),
S = AsmStreamerCtorFn(Ctx, std::move(OS), std::move(IP), std::move(CE),
std::move(TAB));
else
S = llvm::createAsmStreamer(Ctx, std::move(OS), IP, std::move(CE),
std::move(TAB));
S = llvm::createAsmStreamer(Ctx, std::move(OS), std::move(IP),
std::move(CE), std::move(TAB));
createAsmTargetStreamer(*S, OSRef, IP);
createAsmTargetStreamer(*S, OSRef, Printer);
return S;
}

View File

@ -46,11 +46,11 @@ class SystemZHLASMAsmStreamer final : public MCStreamer {
public:
SystemZHLASMAsmStreamer(MCContext &Context,
std::unique_ptr<formatted_raw_ostream> os,
MCInstPrinter *printer,
std::unique_ptr<MCInstPrinter> printer,
std::unique_ptr<MCCodeEmitter> emitter,
std::unique_ptr<MCAsmBackend> asmbackend)
: MCStreamer(Context), FOSOwner(std::move(os)), FOS(*FOSOwner), OS(Str),
MAI(Context.getAsmInfo()), InstPrinter(printer),
MAI(Context.getAsmInfo()), InstPrinter(std::move(printer)),
Assembler(std::make_unique<MCAssembler>(
Context, std::move(asmbackend), std::move(emitter),
(asmbackend) ? asmbackend->createObjectWriter(NullStream)

View File

@ -192,19 +192,18 @@ static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
return new SystemZTargetGNUStreamer(S, OS);
}
static MCStreamer *createAsmStreamer(MCContext &Ctx,
std::unique_ptr<formatted_raw_ostream> OS,
MCInstPrinter *IP,
std::unique_ptr<MCCodeEmitter> CE,
std::unique_ptr<MCAsmBackend> TAB) {
static MCStreamer *createSystemZAsmStreamer(
MCContext &Ctx, std::unique_ptr<formatted_raw_ostream> OS,
std::unique_ptr<MCInstPrinter> IP, std::unique_ptr<MCCodeEmitter> CE,
std::unique_ptr<MCAsmBackend> TAB) {
auto TT = Ctx.getTargetTriple();
if (TT.isOSzOS() && !GNUAsOnzOSCL)
return new SystemZHLASMAsmStreamer(Ctx, std::move(OS), IP, std::move(CE),
std::move(TAB));
return new SystemZHLASMAsmStreamer(Ctx, std::move(OS), std::move(IP),
std::move(CE), std::move(TAB));
return llvm::createAsmStreamer(Ctx, std::move(OS), IP, std::move(CE),
std::move(TAB));
return llvm::createAsmStreamer(Ctx, std::move(OS), std::move(IP),
std::move(CE), std::move(TAB));
}
static MCTargetStreamer *
@ -254,7 +253,8 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSystemZTargetMC() {
createSystemZMCInstPrinter);
// Register the asm streamer.
TargetRegistry::RegisterAsmStreamer(getTheSystemZTarget(), createAsmStreamer);
TargetRegistry::RegisterAsmStreamer(getTheSystemZTarget(),
createSystemZAsmStreamer);
// Register the asm target streamer.
TargetRegistry::RegisterAsmTargetStreamer(getTheSystemZTarget(),

View File

@ -1,4 +1,3 @@
# RUN: export LSAN_OPTIONS=detect_leaks=0
# RUN: not llvm-mc -M invalid /dev/null 2>&1 | FileCheck %s
# CHECK: error: invalid InstPrinter option 'invalid'

View File

@ -519,10 +519,10 @@ int main(int argc, char **argv) {
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
assert(MCII && "Unable to create instruction info!");
MCInstPrinter *IP = nullptr;
std::unique_ptr<MCInstPrinter> IP;
if (FileType == OFT_AssemblyFile) {
IP = TheTarget->createMCInstPrinter(Triple(TripleName), OutputAsmVariant,
*MAI, *MCII, *MRI);
IP.reset(TheTarget->createMCInstPrinter(
Triple(TripleName), OutputAsmVariant, *MAI, *MCII, *MRI));
if (!IP) {
WithColor::error()
@ -541,6 +541,17 @@ int main(int argc, char **argv) {
// Set the display preference for hex vs. decimal immediates.
IP->setPrintImmHex(PrintImmHex);
switch (Action) {
case AC_MDisassemble:
IP->setUseMarkup(true);
break;
case AC_CDisassemble:
IP->setUseColor(true);
break;
default:
break;
}
// Set up the AsmStreamer.
std::unique_ptr<MCCodeEmitter> CE;
if (ShowEncoding)
@ -549,7 +560,7 @@ int main(int argc, char **argv) {
std::unique_ptr<MCAsmBackend> MAB(
TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
auto FOut = std::make_unique<formatted_raw_ostream>(*OS);
Str.reset(TheTarget->createAsmStreamer(Ctx, std::move(FOut), IP,
Str.reset(TheTarget->createAsmStreamer(Ctx, std::move(FOut), std::move(IP),
std::move(CE), std::move(MAB)));
} else if (FileType == OFT_Null) {
@ -586,13 +597,7 @@ int main(int argc, char **argv) {
*MCII, MCOptions);
break;
case AC_MDisassemble:
IP->setUseMarkup(true);
disassemble = true;
break;
case AC_CDisassemble:
IP->setUseColor(true);
disassemble = true;
break;
case AC_Disassemble:
disassemble = true;
break;

View File

@ -363,13 +363,12 @@ int llvm_ml_main(int Argc, char **Argv, const llvm::ToolContext &) {
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
assert(MCII && "Unable to create instruction info!");
MCInstPrinter *IP = nullptr;
if (FileType == "s") {
const bool OutputATTAsm = InputArgs.hasArg(OPT_output_att_asm);
const unsigned OutputAsmVariant = OutputATTAsm ? 0U // ATT dialect
: 1U; // Intel dialect
IP = TheTarget->createMCInstPrinter(TheTriple, OutputAsmVariant, *MAI,
*MCII, *MRI);
std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
TheTriple, OutputAsmVariant, *MAI, *MCII, *MRI));
if (!IP) {
WithColor::error()
@ -390,7 +389,7 @@ int llvm_ml_main(int Argc, char **Argv, const llvm::ToolContext &) {
std::unique_ptr<MCAsmBackend> MAB(
TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
auto FOut = std::make_unique<formatted_raw_ostream>(*OS);
Str.reset(TheTarget->createAsmStreamer(Ctx, std::move(FOut), IP,
Str.reset(TheTarget->createAsmStreamer(Ctx, std::move(FOut), std::move(IP),
std::move(CE), std::move(MAB)));
} else if (FileType == "null") {