@@ -69,6 +69,9 @@ static codegen::RegisterCodeGenFlags CGF;
6969static cl::opt<std::string>
7070InputFilename (cl::Positional, cl::desc(" <input bitcode>" ), cl::init(" -" ));
7171
72+ static cl::list<std::string>
73+ InstPrinterOptions (" M" , cl::desc(" InstPrinter options" ));
74+
7275static cl::opt<std::string>
7376InputLanguage (" x" , cl::desc(" Input language ('ir' or 'mir')" ));
7477
@@ -512,6 +515,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
512515 Options.MCOptions .AsmVerbose = AsmVerbose;
513516 Options.MCOptions .PreserveAsmComments = PreserveComments;
514517 Options.MCOptions .IASSearchPaths = IncludeDirs;
518+ Options.MCOptions .InstPrinterOptions = InstPrinterOptions;
515519 Options.MCOptions .SplitDwarfFile = SplitDwarfFile;
516520 if (DwarfDirectory.getPosition ()) {
517521 Options.MCOptions .MCUseDwarfDirectory =
@@ -688,6 +692,17 @@ static int compileModule(char **argv, LLVMContext &Context) {
688692 MachineModuleInfoWrapperPass *MMIWP =
689693 new MachineModuleInfoWrapperPass (Target.get ());
690694
695+ // Set a temporary diagnostic handler. This is used before
696+ // MachineModuleInfoWrapperPass::doInitialization for features like -M.
697+ bool HasMCErrors = false ;
698+ MCContext &MCCtx = MMIWP->getMMI ().getContext ();
699+ MCCtx.setDiagnosticHandler ([&](const SMDiagnostic &SMD, bool IsInlineAsm,
700+ const SourceMgr &SrcMgr,
701+ std::vector<const MDNode *> &LocInfos) {
702+ WithColor::error (errs (), argv0) << SMD.getMessage () << ' \n ' ;
703+ HasMCErrors = true ;
704+ });
705+
691706 // Construct a custom pass pipeline that starts after instruction
692707 // selection.
693708 if (!getRunPassNames ().empty ()) {
@@ -722,7 +737,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
722737 } else if (Target->addPassesToEmitFile (
723738 PM, *OS, DwoOut ? &DwoOut->os () : nullptr ,
724739 codegen::getFileType (), NoVerify, MMIWP)) {
725- reportError (" target does not support generation of this file type" );
740+ if (!HasMCErrors)
741+ reportError (" target does not support generation of this file type" );
726742 }
727743
728744 const_cast <TargetLoweringObjectFile *>(Target->getObjFileLowering ())
@@ -750,7 +766,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
750766
751767 PM.run (*M);
752768
753- if (Context.getDiagHandlerPtr ()->HasErrors )
769+ if (Context.getDiagHandlerPtr ()->HasErrors || HasMCErrors )
754770 return 1 ;
755771
756772 // Compare the two outputs and make sure they're the same
0 commit comments