[Clang][CodeGen] Improve support for NPM codegen
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index e55d242..e8322a3 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1319,6 +1319,22 @@
   TargetMachine *TMPointer = TM.get();
   PassBuilder PB(TMPointer, PTOptions, std::nullopt, &PIC,
                  CI.getVirtualFileSystemPtr());
+
+  StandardInstrumentations SI(TheModule->getContext(),
+                              CodeGenOpts.DebugPassManager,
+                              CodeGenOpts.VerifyEach);
+  SI.registerCallbacks(PIC, &MAM);
+
+  TargetLibraryInfoImpl TLII(TheModule->getTargetTriple());
+  FAM.registerPass([&] { return TargetLibraryAnalysis(TLII); });
+  MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
+  MAM.registerPass([&] {
+    const llvm::TargetOptions &Options = TM->Options;
+    return RuntimeLibraryAnalysis(TargetTriple, Options.ExceptionModel,
+                                  Options.FloatABIType, Options.EABIVersion,
+                                  Options.MCOptions.ABIName, Options.VecLib);
+  });
+
   PB.registerModuleAnalyses(MAM);
   PB.registerCGSCCAnalyses(CGAM);
   PB.registerFunctionAnalyses(FAM);
@@ -1326,17 +1342,26 @@
   PB.registerMachineFunctionAnalyses(MFAM);
   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, &MFAM);
 
-  MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
-
-  Error BuildPipelineError =
-      TM->buildCodeGenPipeline(MPM, MAM, *OS, DwoOS ? &DwoOS->os() : nullptr,
-                               CGFT, Opt, MMI.getContext(), &PIC);
-  if (BuildPipelineError) {
+  if (Error BuildPipelineError = TM->buildCodeGenPipeline(
+          MPM, MAM, *OS, DwoOS ? &DwoOS->os() : nullptr, CGFT, Opt,
+          MMI.getContext(), &PIC)) {
     Diags.Report(diag::err_fe_unable_to_interface_with_target);
     return;
   }
 
+  if (PrintPipelinePasses) {
+    std::string PipelineStr;
+    raw_string_ostream OutS(PipelineStr);
+    MPM.printPipeline(OutS, [&PIC](StringRef ClassName) {
+      auto PassName = PIC.getPassNameForClassName(ClassName);
+      return PassName.empty() ? ClassName : PassName;
+    });
+    outs() << PipelineStr << '\n';
+    return;
+  }
+
   TimeCodegenPasses([&] { MPM.run(*TheModule, MAM); });
+  return;
 }
 
 void EmitAssemblyHelper::TimeCodegenPasses(
diff --git a/clang/test/CodeGen/print-pipeline-passes-codegen.c b/clang/test/CodeGen/print-pipeline-passes-codegen.c
new file mode 100644
index 0000000..d13a3ed
--- /dev/null
+++ b/clang/test/CodeGen/print-pipeline-passes-codegen.c
@@ -0,0 +1,16 @@
+// Test that -print-pipeline-passes also prints the codegen pipeline.
+
+// REQUIRES: amdgpu-registered-target
+
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -target-cpu gfx900 \
+// RUN:   -fenable-new-pm-codegen=force-on -emit-obj -o /dev/null \
+// RUN:   -mllvm -print-pipeline-passes -O0 %s 2>&1 | FileCheck %s
+
+// Don't try to check all passes, just a few codegen-specific ones (in order) to
+// make sure the machine pipeline is actually printed.
+// CHECK: require<MachineModuleAnalysis>
+// CHECK-SAME: amdgpu-isel
+// CHECK-SAME: prolog-epilog
+// CHECK-SAME: amdgpu-asm-printer
+
+void Foo(void) {}