https://github.com/NotLebedev updated https://github.com/llvm/llvm-project/pull/192211
>From b0e90fdb201d60759150ee000caa55b3fa2b9470 Mon Sep 17 00:00:00 2001 From: Artemiy Galustov <[email protected]> Date: Tue, 14 Apr 2026 18:47:32 +0300 Subject: [PATCH] Add -emit-cir-bc option to cc1 --- .../clang/CIR/FrontendAction/CIRGenAction.h | 8 ++++++++ clang/include/clang/Driver/Types.def | 1 + clang/include/clang/Frontend/FrontendOptions.h | 3 +++ clang/include/clang/Options/Options.td | 2 ++ clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 15 +++++++++++++++ clang/lib/CIR/FrontendAction/CMakeLists.txt | 1 + clang/lib/Driver/Driver.cpp | 1 + clang/lib/Driver/ToolChains/Clang.cpp | 2 ++ clang/lib/Frontend/CompilerInvocation.cpp | 6 +++++- .../FrontendTool/ExecuteCompilerInvocation.cpp | 9 ++++++++- 10 files changed, 46 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/CIR/FrontendAction/CIRGenAction.h b/clang/include/clang/CIR/FrontendAction/CIRGenAction.h index 99495f4718c5f..3b58dc0d77729 100644 --- a/clang/include/clang/CIR/FrontendAction/CIRGenAction.h +++ b/clang/include/clang/CIR/FrontendAction/CIRGenAction.h @@ -27,6 +27,7 @@ class CIRGenAction : public clang::ASTFrontendAction { enum class OutputType { EmitAssembly, EmitCIR, + EmitCIRBC, EmitLLVM, EmitBC, EmitObj, @@ -59,6 +60,13 @@ class EmitCIRAction : public CIRGenAction { EmitCIRAction(mlir::MLIRContext *MLIRCtx = nullptr); }; +class EmitCIRBCAction : public CIRGenAction { + virtual void anchor(); + +public: + EmitCIRBCAction(mlir::MLIRContext *MLIRCtx = nullptr); +}; + class EmitLLVMAction : public CIRGenAction { virtual void anchor(); diff --git a/clang/include/clang/Driver/Types.def b/clang/include/clang/Driver/Types.def index 76944ec656917..e9ba47dfdf08c 100644 --- a/clang/include/clang/Driver/Types.def +++ b/clang/include/clang/Driver/Types.def @@ -100,6 +100,7 @@ TYPE("lto-ir", LTO_IR, INVALID, "s", phases TYPE("lto-bc", LTO_BC, INVALID, "o", phases::Compile, phases::Backend, phases::Assemble, phases::Link) TYPE("cir", CIR, INVALID, "cir", phases::Compile, phases::Backend, phases::Assemble, phases::Link) +TYPE("cir", CIR_BC, INVALID, "cirbc", phases::Compile, phases::Backend, phases::Assemble, phases::Link) // Misc. TYPE("ast", AST, INVALID, "ast", phases::Compile, phases::Backend, phases::Assemble, phases::Link) TYPE("ifs", IFS, INVALID, "ifs", phases::IfsMerge) diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index f7f51bc37c98d..ebb942b466421 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -68,6 +68,9 @@ enum ActionKind { /// Emit a .cir file EmitCIR, + /// Emit a .cirbc file + EmitCIRBC, + /// Emit a .ll file. EmitLLVM, diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index 5d2741e7e30e5..258e7dcd3c875 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -3369,6 +3369,8 @@ 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_bc : Flag<["-"], "emit-cir-bc">, Visibility<[CC1Option]>, + Group<Action_Group>, HelpText<"Build ASTs and then lower to ClangIR. Emit as mlir bytecode">; /// ClangIR-specific options - END def flto_EQ : Joined<["-"], "flto=">, diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index ab273539b1ce2..3eda3d857f357 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "clang/CIR/FrontendAction/CIRGenAction.h" +#include "mlir/Bytecode/BytecodeWriter.h" #include "mlir/IR/MLIRContext.h" #include "mlir/IR/OwningOpRef.h" #include "clang/Basic/DiagnosticFrontend.h" @@ -29,6 +30,7 @@ static BackendAction getBackendActionFromOutputType(CIRGenAction::OutputType Action) { switch (Action) { case CIRGenAction::OutputType::EmitCIR: + case CIRGenAction::OutputType::EmitCIRBC: assert(false && "Unsupported output type for getBackendActionFromOutputType!"); break; // Unreachable, but fall through to report that @@ -137,6 +139,13 @@ class CIRGenConsumer : public clang::ASTConsumer { MlirModule->print(*OutputStream, Flags); } break; + case CIRGenAction::OutputType::EmitCIRBC: + if (OutputStream && MlirModule) { + mlir::FallbackAsmResourceMap fallbackResourceMap; + mlir::BytecodeWriterConfig writerConfig(fallbackResourceMap); + (void)writeBytecodeToFile(MlirModule, *OutputStream, writerConfig); + } + break; case CIRGenAction::OutputType::EmitLLVM: case CIRGenAction::OutputType::EmitBC: case CIRGenAction::OutputType::EmitObj: @@ -205,6 +214,8 @@ getOutputStream(CompilerInstance &CI, StringRef InFile, return CI.createDefaultOutputFile(false, InFile, "s"); case CIRGenAction::OutputType::EmitCIR: return CI.createDefaultOutputFile(false, InFile, "cir"); + case CIRGenAction::OutputType::EmitCIRBC: + return CI.createDefaultOutputFile(false, InFile, "cirbc"); case CIRGenAction::OutputType::EmitLLVM: return CI.createDefaultOutputFile(false, InFile, "ll"); case CIRGenAction::OutputType::EmitBC: @@ -236,6 +247,10 @@ void EmitCIRAction::anchor() {} EmitCIRAction::EmitCIRAction(mlir::MLIRContext *MLIRCtx) : CIRGenAction(OutputType::EmitCIR, MLIRCtx) {} +void EmitCIRBCAction::anchor() {} +EmitCIRBCAction::EmitCIRBCAction(mlir::MLIRContext *MLIRCtx) + : CIRGenAction(OutputType::EmitCIRBC, MLIRCtx) {} + void EmitLLVMAction::anchor() {} EmitLLVMAction::EmitLLVMAction(mlir::MLIRContext *MLIRCtx) : CIRGenAction(OutputType::EmitLLVM, MLIRCtx) {} diff --git a/clang/lib/CIR/FrontendAction/CMakeLists.txt b/clang/lib/CIR/FrontendAction/CMakeLists.txt index 50d6ea7108ce1..42498be396ffd 100644 --- a/clang/lib/CIR/FrontendAction/CMakeLists.txt +++ b/clang/lib/CIR/FrontendAction/CMakeLists.txt @@ -23,4 +23,5 @@ add_clang_library(clangCIRFrontendAction clangCodeGen MLIRCIR MLIRIR + MLIRBytecodeWriter ) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 39d2ad1f1c8c0..2e5160382b2bc 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -364,6 +364,7 @@ phases::ID Driver::getFinalPhase(const DerivedArgList &DAL, (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) || (PhaseArg = DAL.getLastArg(options::OPT__analyze)) || (PhaseArg = DAL.getLastArg(options::OPT_emit_cir)) || + (PhaseArg = DAL.getLastArg(options::OPT_emit_cir_bc)) || (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) { FinalPhase = phases::Compile; diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index f685abe9dad35..4ea98cb6918e1 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5305,6 +5305,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, rewriteKind = RK_Fragile; } else if (JA.getType() == types::TY_CIR) { CmdArgs.push_back("-emit-cir"); + } else if (JA.getType() == types::TY_CIR_BC) { + CmdArgs.push_back("-emit-cir-bc"); } else if (JA.getType() == types::TY_Image && IsAMDSPIRVForHIPDevice) { CmdArgs.push_back("-emit-obj"); } else { diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 748c36efefaed..a647da097e75a 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2793,6 +2793,7 @@ static const auto &getFrontendActionTable() { {frontend::EmitAssembly, OPT_S}, {frontend::EmitBC, OPT_emit_llvm_bc}, {frontend::EmitCIR, OPT_emit_cir}, + {frontend::EmitCIRBC, OPT_emit_cir_bc}, {frontend::EmitHTML, OPT_emit_html}, {frontend::EmitLLVM, OPT_emit_llvm}, {frontend::EmitLLVMOnly, OPT_emit_llvm_only}, @@ -3157,7 +3158,8 @@ 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_fclangir) || Args.hasArg(OPT_emit_cir)) + if (Args.hasArg(OPT_fclangir) || Args.hasArg(OPT_emit_cir) || + Args.hasArg(OPT_emit_cir_bc)) Opts.UseClangIRPipeline = true; #if CLANG_ENABLE_CIR @@ -4695,6 +4697,7 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) { case frontend::EmitAssembly: case frontend::EmitBC: case frontend::EmitCIR: + case frontend::EmitCIRBC: case frontend::EmitHTML: case frontend::EmitLLVM: case frontend::EmitLLVMOnly: @@ -4737,6 +4740,7 @@ static bool isCodeGenAction(frontend::ActionKind Action) { case frontend::EmitAssembly: case frontend::EmitBC: case frontend::EmitCIR: + case frontend::EmitCIRBC: case frontend::EmitHTML: case frontend::EmitLLVM: case frontend::EmitLLVMOnly: diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp index e4622496758ac..2134d2a78382d 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -53,7 +53,7 @@ CreateFrontendBaseAction(CompilerInstance &CI) { unsigned UseCIR = CI.getFrontendOpts().UseClangIRPipeline; frontend::ActionKind Act = CI.getFrontendOpts().ProgramAction; - bool EmitsCIR = Act == EmitCIR; + bool EmitsCIR = Act == EmitCIR || Act == EmitCIRBC; if (!UseCIR && EmitsCIR) llvm::report_fatal_error("-emit-cir and only valid when using -fclangir"); @@ -79,6 +79,13 @@ CreateFrontendBaseAction(CompilerInstance &CI) { return std::make_unique<cir::EmitBCAction>(); #endif return std::make_unique<EmitBCAction>(); + case EmitCIRBC: +#if CLANG_ENABLE_CIR + return std::make_unique<cir::EmitCIRBCAction>(); +#else + CI.getDiagnostics().Report(diag::err_fe_cir_not_built); + return nullptr; +#endif case EmitCIR: #if CLANG_ENABLE_CIR return std::make_unique<cir::EmitCIRAction>(); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
