@@ -208,20 +208,27 @@ CodeGenTargetMachineImpl::createMCStreamer(raw_pwrite_stream &Out,
208208 return std::move (AsmStreamer);
209209}
210210
211+ std::unique_ptr<MachineModuleInfo>
212+ CodeGenTargetMachineImpl::createMachineModuleInfo () const override {
213+ return std::make_unique<MachineModuleInfo>(this );
214+ }
215+
211216bool CodeGenTargetMachineImpl::addPassesToEmitFile (
212217 PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
213- CodeGenFileType FileType, bool DisableVerify,
214- MachineModuleInfoWrapperPass *MMIWP) {
215- // Add common CodeGen passes.
216- if (!MMIWP)
217- MMIWP = new MachineModuleInfoWrapperPass (this );
218+ CodeGenFileType FileType, MachineModuleInfo *MMI, bool DisableVerify) {
219+ // Check if MMI is nullptr, or if MMI pointer is valid but the target
220+ // machine of the MMI is not the same as this target machine
221+ if (!MMI || &MMI->getTarget () != this )
222+ return true ;
223+ // Add the wrapper pass for MMI
224+ auto *MMIWP = new MachineModuleInfoWrapperPass (MMI);
218225 TargetPassConfig *PassConfig =
219226 addPassesToGenerateCode (*this , PM, DisableVerify, *MMIWP);
220227 if (!PassConfig)
221228 return true ;
222229
223230 if (TargetPassConfig::willCompleteCodeGenPipeline ()) {
224- if (addAsmPrinter (PM, Out, DwoOut, FileType, MMIWP-> getMMI (). getContext ()))
231+ if (addAsmPrinter (PM, Out, DwoOut, FileType, MMI-> getContext ()))
225232 return true ;
226233 } else {
227234 // MIR printing is redundant with -filetype=null.
@@ -239,19 +246,23 @@ bool CodeGenTargetMachineImpl::addPassesToEmitFile(
239246// / used to build custom MCStreamer.
240247// /
241248bool CodeGenTargetMachineImpl::addPassesToEmitMC (PassManagerBase &PM,
242- MCContext *&Ctx,
243249 raw_pwrite_stream &Out,
250+ MachineModuleInfo *MMI,
244251 bool DisableVerify) {
252+ // Check if MMI is nullptr, or if MMI pointer is valid but the target
253+ // machine of the MMI is not the same as this target machine
254+ if (!MMI || &MMI->getTarget () != this )
255+ return true ;
245256 // Add common CodeGen passes.
246- MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass (this );
257+ MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass (MMI );
247258 TargetPassConfig *PassConfig =
248259 addPassesToGenerateCode (*this , PM, DisableVerify, *MMIWP);
249260 if (!PassConfig)
250261 return true ;
251262 assert (TargetPassConfig::willCompleteCodeGenPipeline () &&
252263 " Cannot emit MC with limited codegen pipeline" );
253264
254- Ctx = &MMIWP-> getMMI (). getContext ();
265+ MCContext & Ctx = MMI-> getContext ();
255266 // libunwind is unable to load compact unwind dynamically, so we must generate
256267 // DWARF unwind info for the JIT.
257268 Options.MCOptions .EmitDwarfUnwind = EmitDwarfUnwindType::Always;
@@ -261,7 +272,7 @@ bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
261272 const MCSubtargetInfo &STI = *getMCSubtargetInfo ();
262273 const MCRegisterInfo &MRI = *getMCRegisterInfo ();
263274 std::unique_ptr<MCCodeEmitter> MCE (
264- getTarget ().createMCCodeEmitter (*getMCInstrInfo (), * Ctx));
275+ getTarget ().createMCCodeEmitter (*getMCInstrInfo (), Ctx));
265276 if (!MCE)
266277 return true ;
267278 MCAsmBackend *MAB =
@@ -271,7 +282,7 @@ bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
271282
272283 const Triple &T = getTargetTriple ();
273284 std::unique_ptr<MCStreamer> AsmStreamer (getTarget ().createMCObjectStreamer (
274- T, * Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter (Out),
285+ T, Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter (Out),
275286 std::move (MCE), STI));
276287
277288 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
0 commit comments