https://github.com/koparasy updated 
https://github.com/llvm/llvm-project/pull/243679

>From 95bb0c3163c6ac971a80dccdcdee1e1058bf1396 Mon Sep 17 00:00:00 2001
From: Konstantinos Parasyris <[email protected]>
Date: Tue, 17 Nov 2026 17:00:45 -0800
Subject: [PATCH 1/2] [CIR] Rerun the CIR pipeline on ClangIR input and add
 -emit-cir=pristine

The CIR-to-CIR passes are now safe to rerun, so ClangIR input runs the
full pipeline instead of skipping it. Add the -cc1 option
-emit-cir=pristine, which emits CIRGen's output before any ClangIR pass,
as agreed in the RFC:
https://discourse.llvm.org/t/rfc-clangir-making-cir-pipeline-boundaries-first-class-driver-artifacts/90998
---
 clang/include/clang/Options/Options.td        |  5 ++
 clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 53 +++++++++++--------
 clang/lib/Frontend/CompilerInvocation.cpp     | 15 +++++-
 .../test/CIR/Input/cir-input-lower-error.cir  | 10 ++--
 clang/test/CIR/Input/cir-input-lower.cpp      |  6 +--
 clang/test/CIR/Input/emit-cir-pristine.cpp    | 47 ++++++++++++++++
 6 files changed, 105 insertions(+), 31 deletions(-)
 create mode 100644 clang/test/CIR/Input/emit-cir-pristine.cpp

diff --git a/clang/include/clang/Options/Options.td 
b/clang/include/clang/Options/Options.td
index 9156640ff67c79..4f7c9f3ea87818 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -3569,6 +3569,11 @@ defm clangir : BoolFOption<"clangir",
   BothFlags<[], [ClangOption, CC1Option], "">>;
 def emit_cir : Flag<["-"], "emit-cir">, Visibility<[ClangOption, CC1Option]>,
   Group<Action_Group>, HelpText<"Build ASTs and then lower to ClangIR">;
+def emit_cir_EQ : Joined<["-"], "emit-cir=">, Visibility<[CC1Option]>,
+  Group<Action_Group>, Values<"pristine">,
+  HelpText<"Build ASTs and then lower to ClangIR, stopping at the named "
+           "point (experimental). 'pristine' is CIRGen's output before any "
+           "ClangIR pass">;
 def emit_cir_bc : Flag<["-"], "emit-cir-bc">,
   Visibility<[CC1Option]>, Group<Action_Group>,
   HelpText<"Build ASTs and then emit ClangIR bytecode">;
diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp 
b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
index 3f7078a77b8698..c5c2e36f76ea0f 100644
--- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
+++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
@@ -104,6 +104,23 @@ static void embedSYCLDeviceBinary(CompilerInstance &CI, 
llvm::Module &M) {
   }
 }
 
+// Runs the CIR-to-CIR pipeline on \p MlirModule unless -clangir-disable-passes
+// is given.
+static mlir::LogicalResult runCIRPipeline(const FrontendOptions &FEOpts,
+                                          const CodeGenOptions &CGO,
+                                          mlir::ModuleOp MlirModule,
+                                          mlir::MLIRContext &MlirCtx) {
+  if (FEOpts.ClangIRDisablePasses)
+    return mlir::success();
+  const bool EnableLibOpt =
+      FEOpts.ClangIRLibOptEnabled && (CGO.OptimizationLevel > 0);
+  return runCIRToCIRPasses(
+      MlirModule, MlirCtx, !FEOpts.ClangIRDisableCIRVerifier,
+      FEOpts.ClangIREnableIdiomRecognizer, CGO.OptimizationLevel > 0,
+      EnableLibOpt, FEOpts.ClangIRLibOptOptions,
+      FEOpts.ClangIRCallConvLowering);
+}
+
 // Emits the output requested by \p Action for \p MlirModule, a CIR module that
 // has been through the CIR-to-CIR pipeline. Language facts are read from the
 // module's #cir.lowering_options and target facts from \p CI, so this
@@ -320,23 +337,12 @@ class CIRGenConsumer : public clang::ASTConsumer {
     mlir::ModuleOp MlirModule = Gen->getModule();
     mlir::MLIRContext &MlirCtx = Gen->getMLIRContext();
 
-    if (!FEOptions.ClangIRDisablePasses) {
-      std::string LibOptOptions = FEOptions.ClangIRLibOptOptions;
-
-      // Setup and run CIR pipeline.
-      const bool EnableLibOpt =
-          FEOptions.ClangIRLibOptEnabled && (CGO.OptimizationLevel > 0);
-      if (runCIRToCIRPasses(
-              MlirModule, MlirCtx, !FEOptions.ClangIRDisableCIRVerifier,
-              FEOptions.ClangIREnableIdiomRecognizer, CGO.OptimizationLevel > 
0,
-              EnableLibOpt, LibOptOptions, FEOptions.ClangIRCallConvLowering)
-              .failed()) {
-        // Pass-side errors already routed through ClangIRDiagnosticHandler.
-        // Skip the generic catch-all if a specific diagnostic was emitted.
-        if (!CI.getDiagnostics().hasErrorOccurred())
-          CI.getDiagnostics().Report(diag::err_cir_to_cir_transform_failed);
-        return;
-      }
+    if (runCIRPipeline(FEOptions, CGO, MlirModule, MlirCtx).failed()) {
+      // Pass-side errors already routed through ClangIRDiagnosticHandler.
+      // Skip the generic catch-all if a specific diagnostic was emitted.
+      if (!CI.getDiagnostics().hasErrorOccurred())
+        CI.getDiagnostics().Report(diag::err_cir_to_cir_transform_failed);
+      return;
     }
 
     // CIRGen-only step: map the emitted LLVM functions back to their source
@@ -450,10 +456,15 @@ void CIRGenAction::ExecuteAction() {
     return;
   }
 
-  // The CIR-to-CIR pipeline is not run: CIR printed by -emit-cir has already
-  // been through it, and its lowering passes are not idempotent.
-  // TODO: Decide from a pipeline-stage attribute on the module which passes
-  // still have to run, once -emit-cir can stop before ABI lowering.
+  // The CIR-to-CIR passes are safe to rerun, so the whole pipeline runs
+  // whether or not the input has already been through it.
+  if (runCIRPipeline(CI.getFrontendOpts(), CI.getCodeGenOpts(), *Module,
+                     MLIRContext)
+          .failed()) {
+    if (!Diags.hasErrorOccurred())
+      Diags.Report(diag::err_cir_to_cir_transform_failed);
+    return;
+  }
   BackendDiagnosticConsumer DiagConsumer(Diags, CI.getCodeGenOpts());
   DiagConsumer.setSourceManager(&SM);
   emitCIRModuleOutput(Action, CI, *Module, *Ctx, LinkModules, DiagConsumer,
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index d0d99f6335dad5..60c7f5e0ae0696 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -2836,6 +2836,7 @@ static const auto &getFrontendActionTable() {
       {frontend::EmitAssembly, OPT_S},
       {frontend::EmitBC, OPT_emit_llvm_bc},
       {frontend::EmitCIR, OPT_emit_cir},
+      {frontend::EmitCIR, OPT_emit_cir_EQ},
       {frontend::EmitCIRBC, OPT_emit_cir_bc},
       {frontend::EmitHTML, OPT_emit_html},
       {frontend::EmitLLVM, OPT_emit_llvm},
@@ -3092,6 +3093,18 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, 
ArgList &Args,
       }
     }
 
+    if (Opt == OPT_emit_cir_EQ) {
+      std::optional<bool> DisablePasses =
+          llvm::StringSwitch<std::optional<bool>>(A->getValue())
+              .Case("pristine", true)
+              .Default(std::nullopt);
+      if (DisablePasses)
+        Opts.ClangIRDisablePasses = *DisablePasses;
+      else
+        Diags.Report(diag::err_drv_invalid_value)
+            << A->getAsString(Args) << A->getValue();
+    }
+
     if (ProgramAction == frontend::FixIt && Opt == OPT_fixit_EQ)
       Opts.FixItSuffix = A->getValue();
 
@@ -3200,7 +3213,7 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, 
ArgList &Args,
   if (Opts.ProgramAction != frontend::GenerateModule && Opts.IsSystemModule)
     Diags.Report(diag::err_drv_argument_only_allowed_with) << "-fsystem-module"
                                                            << "-emit-module";
-  if (Args.hasArg(OPT_emit_cir, OPT_emit_cir_bc))
+  if (Args.hasArg(OPT_emit_cir, OPT_emit_cir_EQ, OPT_emit_cir_bc))
     Opts.UseClangIRPipeline = true;
 
 #if CLANG_ENABLE_CIR
diff --git a/clang/test/CIR/Input/cir-input-lower-error.cir 
b/clang/test/CIR/Input/cir-input-lower-error.cir
index 2f135bdd0f9a39..7ed58a8f8c37e5 100644
--- a/clang/test/CIR/Input/cir-input-lower-error.cir
+++ b/clang/test/CIR/Input/cir-input-lower-error.cir
@@ -1,14 +1,14 @@
 // Errors from lowering ClangIR input to LLVM IR are diagnosed, not crashed on.
-// The ctor region of @gs is only removed by LoweringPrepare, which is not run
-// on ClangIR input.
+// The ctor region of @gs is only removed by LoweringPrepare, which
+// -clangir-disable-passes skips.
 
-// RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - 
2>&1 \
+// RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu 
-clangir-disable-passes -emit-llvm %s -o - 2>&1 \
 // RUN:   | FileCheck %s --implicit-check-not=error:
-// RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-obj %s -o 
/dev/null 2>&1 \
+// RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu 
-clangir-disable-passes -emit-obj %s -o /dev/null 2>&1 \
 // RUN:   | FileCheck %s --implicit-check-not=error:
 
 // No partial output is left behind.
-// RUN: rm -f %t.o && not %clang_cc1 -triple x86_64-unknown-linux-gnu 
-emit-obj %s -o %t.o 2>/dev/null
+// RUN: rm -f %t.o && not %clang_cc1 -triple x86_64-unknown-linux-gnu 
-clangir-disable-passes -emit-obj %s -o %t.o 2>/dev/null
 // RUN: not ls %t.o
 
 // CHECK: cir-input-lower-error.cir:[[@LINE+6]]:3: error: GlobalOp ctor and 
dtor regions should be removed in LoweringPrepare
diff --git a/clang/test/CIR/Input/cir-input-lower.cpp 
b/clang/test/CIR/Input/cir-input-lower.cpp
index 4d1d25573d349a..7d60f347999695 100644
--- a/clang/test/CIR/Input/cir-input-lower.cpp
+++ b/clang/test/CIR/Input/cir-input-lower.cpp
@@ -1,8 +1,6 @@
 // Lowering ClangIR input to LLVM IR, assembly, bitcode or an object gives the
-// same result as compiling the source directly. The CIR-to-CIR pipeline is not
-// run again on input printed by -emit-cir, whose lowering passes are not
-// idempotent; the code below exercises the calling convention, global
-// constructor and TLS wrapper lowering.
+// same result as compiling the source directly. The code below exercises the
+// calling convention, global constructor and TLS wrapper lowering.
 
 // REQUIRES: x86-registered-target
 
diff --git a/clang/test/CIR/Input/emit-cir-pristine.cpp 
b/clang/test/CIR/Input/emit-cir-pristine.cpp
new file mode 100644
index 00000000000000..842224e4ee3688
--- /dev/null
+++ b/clang/test/CIR/Input/emit-cir-pristine.cpp
@@ -0,0 +1,47 @@
+// -emit-cir=pristine prints CIRGen's output before any ClangIR pass runs.
+// Compiling that output resumes the pipeline and gives the same result as
+// compiling the source directly.
+
+// REQUIRES: x86-registered-target
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir 
-emit-cir=pristine %s -o %t.cir
+// RUN: FileCheck %s --input-file=%t.cir
+
+// CHECK: cir.global external @gs = #cir.zero : !rec_S ctor {
+// CHECK: tls_refs = <"_ZTW2tl", "_ZTH2tl"> @tl
+// CHECK: cir.func {{.*}} @_Z4addvDv2_fS_(%arg0: !cir.vector<2 x !cir.float> 
{{.*}} requires_call_conv_lowering
+
+// It is the same as -clangir-disable-passes, and implies -fclangir.
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir 
-clangir-disable-passes -emit-cir %s -o %t.disable.cir
+// RUN: diff %t.cir %t.disable.cir
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir=pristine %s -o 
%t.implied.cir
+// RUN: diff %t.cir %t.implied.cir
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o 
%t.direct.ll
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %t.cir -o %t.ll
+// RUN: diff %t.direct.ll %t.ll
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-obj %s -o 
%t.direct.o
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-obj %t.cir -o %t.o
+// RUN: cmp %t.direct.o %t.o
+
+// RUN: not %clang_cc1 -fclangir -emit-cir=bogus %s -o /dev/null 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=BAD
+// BAD: error: invalid value 'bogus' in '-emit-cir=bogus'
+
+// The keyword is a -cc1 option only.
+// RUN: not %clang -fclangir -emit-cir=pristine -c %s -o /dev/null 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=DRIVER
+// DRIVER: error: unknown argument '-emit-cir=pristine'
+
+typedef float v2f __attribute__((vector_size(8)));
+v2f addv(v2f a, v2f b) { return a + b; }
+
+struct S {
+  S();
+  int x;
+};
+S gs;
+
+thread_local int tl = 3;
+int get() { return tl; }

>From fbb10a7b32b3ac57fa7bf8a7b94b870726f7ac4e Mon Sep 17 00:00:00 2001
From: Konstantinos Parasyris <[email protected]>
Date: Wed, 18 Nov 2026 13:08:53 -0800
Subject: [PATCH 2/2] Add documentation and clarification

---
 clang/docs/CIR/EmitPoints.md                  | 15 +++++++++++++++
 clang/docs/CIR/index.md                       |  1 +
 clang/lib/CIR/FrontendAction/CIRGenAction.cpp |  4 ++--
 3 files changed, 18 insertions(+), 2 deletions(-)
 create mode 100644 clang/docs/CIR/EmitPoints.md

diff --git a/clang/docs/CIR/EmitPoints.md b/clang/docs/CIR/EmitPoints.md
new file mode 100644
index 00000000000000..cf12f82fd54ca3
--- /dev/null
+++ b/clang/docs/CIR/EmitPoints.md
@@ -0,0 +1,15 @@
+# ClangIR Emit Points
+
+`-cc1 -emit-cir` prints CIR after the CIR-to-CIR pipeline.
+`-emit-cir=<keyword>` stops at an earlier point. A keyword names a condition
+of the emitted CIR, not a pass.
+
+| Keyword | Condition |
+|---|---|
+| `pristine` | CIRGen's output; no ClangIR pass has run. |
+
+## ClangIR input
+
+`-cc1` accepts CIR as input (`.cir` or `-x cir`) and runs the full CIR-to-CIR
+pipeline on it, whichever point it was emitted at. The passes are safe to
+rerun. `-clangir-disable-passes` skips the pipeline.
diff --git a/clang/docs/CIR/index.md b/clang/docs/CIR/index.md
index dba6fb70564e38..d57265ebd35b5a 100644
--- a/clang/docs/CIR/index.md
+++ b/clang/docs/CIR/index.md
@@ -39,6 +39,7 @@ pipeline viewer is convenient for following along.
 ABILowering
 CleanupAndEHDesign
 CodeDuplication
+EmitPoints
 ```
 
 ## RFCs
diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp 
b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
index c5c2e36f76ea0f..38cf51a38eb93f 100644
--- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
+++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
@@ -456,8 +456,8 @@ void CIRGenAction::ExecuteAction() {
     return;
   }
 
-  // The CIR-to-CIR passes are safe to rerun, so the whole pipeline runs
-  // whether or not the input has already been through it.
+  // ClangIR input may already have been through the pipeline in an earlier
+  // invocation (e.g. -emit-cir). Its passes are safe to rerun, so run it 
whole.
   if (runCIRPipeline(CI.getFrontendOpts(), CI.getCodeGenOpts(), *Module,
                      MLIRContext)
           .failed()) {

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to