Alrighty. I’ve merged my changes. Because next sync of our internal repo with external will happen tomorrow, to unblock you, use this patch
diff --git a/Mojo/include/Mojo/Compiler/Pipeline.h b/Mojo/include/Mojo/Compiler/Pipeline.h
new file mode 100644
index 00000000000..9f5842d786f
--- /dev/null
+++ b/Mojo/include/Mojo/Compiler/Pipeline.h
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+// Copyright (c) 2026, Modular Inc. All rights reserved.
+//
+// Licensed under the Apache License v2.0 with LLVM Exceptions:
+// https://llvm.org/LICENSE.txt
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//===----------------------------------------------------------------------===//
+
+#ifndef KGEN_COMPILER_OPTPIPELINE_H
+#define KGEN_COMPILER_OPTPIPELINE_H
+
+namespace mlir {
+class PassManager;
+} // namespace mlir
+
+namespace M::KGEN {
+
+class CompilationOptions;
+
+/// The optimization and simplification passes that run right after the
+/// elaborator.
+void buildPostElaborationPipeline(mlir::PassManager &pm,
+ const CompilationOptions &options);
+
+/// Convention lowering, inlining and the optimizations that run at every
+/// level.
+void buildFirstOptPipeline(mlir::PassManager &pm,
+ const CompilationOptions &options);
+
+/// The `-O1` and `-O2` optimization rounds, then loop, closure and async
+/// lowering.
+void buildLateOptPipeline(mlir::PassManager &pm,
+ const CompilationOptions &options);
+
+} // namespace M::KGEN
+
+#endif // KGEN_COMPILER_OPTPIPELINE_H
diff --git a/Mojo/include/Mojo/Compiler/Target/TargetBackend.h b/Mojo/include/Mojo/Compiler/Target/TargetBackend.h
index 92cdce1da6e..4a0a63027df 100644
--- a/Mojo/include/Mojo/Compiler/Target/TargetBackend.h
+++ b/Mojo/include/Mojo/Compiler/Target/TargetBackend.h
@@ -54,6 +54,7 @@ struct object_creator;
namespace mlir {
class Operation;
+class PassManager;
} // namespace mlir
namespace M::KGEN {
@@ -179,6 +180,16 @@ public:
FuncSymbolAttr symbol,
mlir::ModuleOp module) const;
+ /// Appends the target's whole post-elaboration pipeline to `pm` and returns
+ /// true. A target that returns false gets the default pipeline instead. A
+ /// target pipeline replaces the default entirely, so it must include the
+ /// generic optimizations.
+ virtual bool
+ buildPostElaborationPipeline(mlir::PassManager &pm,
+ const CompilationOptions &options) const {
+ return false;
+ }
+
virtual SplitStrategy
splitStrategy(const CompilationOptions &options) const = 0;
/// Inter-procedural codegen disables parallel/per-function llc.
diff --git a/Mojo/lib/Compiler/KGENCompiler.cpp b/Mojo/lib/Compiler/KGENCompiler.cpp
index 95702e1ff62..5d04e73e820 100644
--- a/Mojo/lib/Compiler/KGENCompiler.cpp
+++ b/Mojo/lib/Compiler/KGENCompiler.cpp
@@ -171,6 +171,19 @@ generateInstantiateStub(GeneratorOp func, FuncSymbolAttr symbol,
ReturnOp::create(b, call.getResults());
}
+// Appends the target's post-elaboration pipeline, or the default one when the
+// target has none.
+static void buildPostElaborationPipeline(mlir::PassManager &pm,
+ TargetInfoAttr target,
+ const CompilationOptions &options) {
+ ErrorOr<const TargetBackend *> backendOr =
+ TargetBackendRegistry::get().lookup(target.getTriple());
+ if (!backendOr.isError() &&
+ (*backendOr)->buildPostElaborationPipeline(pm, options))
+ return;
+ buildPostElaborationPipeline(pm, options);
+}
+
/// Find a FuncOp in the module by its kgen.offload.kernelid LLVM metadata.
/// Returns nullptr if no function with the given kernel ID is found.
static FuncOp findFuncByKernelId(ModuleOp module, uint64_t kernelId) {
@@ -346,7 +359,7 @@ static ErrorOr<StringAttr> compileStandaloneKernel(
pm.addPass(createElaborateGenerators(target, elaboratorOptions,
compilationOptions, compileElaboratorAsm,
compileOffloads));
- buildPostElaborationPipeline(pm, compilationOptions);
+ ::buildPostElaborationPipeline(pm, target, compilationOptions);
if (failed(pm.run(*module)))
return Error("failed to run the pass manager");
@@ -756,7 +769,7 @@ static ElaboratorCompileOffloadRetType compileOffloads(
createElaborateGenerators(target, elabOptions, compilationOptions,
compileElaboratorAsm, compileOffloads));
- buildPostElaborationPipeline(pm, compilationOptions);
+ ::buildPostElaborationPipeline(pm, target, compilationOptions);
if (failed(pm.run(*module)))
return Error("failed to run the pass manager for offload functions");
@@ -966,7 +979,7 @@ void KGEN::populateElaborateModulePasses(mlir::PassManager &pm,
const CompilationOptions &options) {
buildElaborateModulePipeline(pm, target, options, compileElaboratorAsm,
compileOffloads);
- buildPostElaborationPipeline(pm, options);
+ ::buildPostElaborationPipeline(pm, target, options);
}
//===----------------------------------------------------------------------===//
diff --git a/Mojo/lib/Compiler/Pipeline/Pipeline.h b/Mojo/lib/Compiler/Pipeline/Pipeline.h
index 64f9a489124..f15c281c042 100644
--- a/Mojo/lib/Compiler/Pipeline/Pipeline.h
+++ b/Mojo/lib/Compiler/Pipeline/Pipeline.h
@@ -14,6 +14,7 @@
#define KGEN_COMPILER_PIPELINE_PIPELINE_H
#include "Mojo/Compiler/KGENCompiler.h"
+#include "Mojo/Compiler/Pipeline.h"
#include "Mojo/ToolCommon/KGENPasses.h"
#include "Support/LLVMForwardDecls.h"
@@ -60,19 +61,6 @@ void buildElaborateModulePipeline(mlir::PassManager &pm, TargetInfoAttr target,
void populateElaborateModulePasses(mlir::PassManager &pm, TargetInfoAttr target,
const CompilationOptions &options);
-//===----------------------------------------------------------------------===//
-// PostElaborationPipeline
-//===----------------------------------------------------------------------===//
-
-/// This populates the post-elaboration optimization and simplification passes.
-/// These passes are intended to run immediately after the elaborator.
-void buildPostElaborationPipeline(mlir::PassManager &pm,
- const CompilationOptions &options);
-void buildFirstOptPipeline(mlir::PassManager &pm,
- const CompilationOptions &options);
-void buildLateOptPipeline(mlir::PassManager &pm,
- const CompilationOptions &options);
-
} // namespace KGEN
} // namespace M
Let me know if you need any other help