diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -487,6 +487,11 @@ } } +static bool actionRequiresCodeGen(BackendAction Action) { + return Action != Backend_EmitNothing && Action != Backend_EmitBC && + Action != Backend_EmitLL; +} + static bool initTargetOptions(DiagnosticsEngine &Diags, llvm::TargetOptions &Options, const CodeGenOptions &CodeGenOpts, @@ -977,9 +982,7 @@ setCommandLineOpts(CodeGenOpts); - bool UsesCodeGen = (Action != Backend_EmitNothing && - Action != Backend_EmitBC && - Action != Backend_EmitLL); + bool UsesCodeGen = actionRequiresCodeGen(Action); CreateTargetMachine(UsesCodeGen); if (UsesCodeGen && !TM) @@ -1006,6 +1009,10 @@ CreatePasses(PerModulePasses, PerFunctionPasses); + if (!UsesCodeGen && CodeGenOpts.VerifyModule) + // Add it here so that it runs prior to the BC/LL emission pass + PerModulePasses.add(createVerifierPass()); + legacy::PassManager CodeGenPasses; CodeGenPasses.add( createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); @@ -1425,6 +1432,11 @@ MPM.addPass(ModuleMemProfilerPass()); } } + + if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule) + // Add it here so that it runs prior to the BC/LL emission pass + MPM.addPass(VerifierPass()); + switch (Action) { case Backend_EmitBC: if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) { @@ -1514,8 +1526,7 @@ TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr); setCommandLineOpts(CodeGenOpts); - bool RequiresCodeGen = (Action != Backend_EmitNothing && - Action != Backend_EmitBC && Action != Backend_EmitLL); + bool RequiresCodeGen = actionRequiresCodeGen(Action); CreateTargetMachine(RequiresCodeGen); if (RequiresCodeGen && !TM)