From 3af1d49fd6f744b29165fc75f2f24c9a3e9d43b1 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Fri, 7 Feb 2025 15:22:38 +0100 Subject: [PATCH 1/5] [CIR] Add option to emit MLIR in LLVM dialect. --- .../clang/CIR/FrontendAction/CIRGenAction.h | 8 +++++ clang/include/clang/CIR/LowerToLLVM.h | 10 +++++-- clang/include/clang/CIR/LowerToMLIR.h | 6 ++++ clang/include/clang/Driver/Options.td | 2 ++ .../include/clang/Frontend/FrontendOptions.h | 3 ++ clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 29 +++++++++++++++---- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 24 +++++++++++---- .../Lowering/ThroughMLIR/LowerCIRToMLIR.cpp | 25 ++++++++++++---- clang/lib/Frontend/CompilerInvocation.cpp | 2 ++ .../ExecuteCompilerInvocation.cpp | 2 ++ 10 files changed, 93 insertions(+), 18 deletions(-) diff --git a/clang/include/clang/CIR/FrontendAction/CIRGenAction.h b/clang/include/clang/CIR/FrontendAction/CIRGenAction.h index 6618fbc54261..92f40231ac19 100644 --- a/clang/include/clang/CIR/FrontendAction/CIRGenAction.h +++ b/clang/include/clang/CIR/FrontendAction/CIRGenAction.h @@ -37,6 +37,7 @@ class CIRGenAction : public clang::ASTFrontendAction { EmitLLVM, EmitBC, EmitMLIR, + EmitMLIRLLVM, EmitObj, None }; @@ -101,6 +102,13 @@ class EmitMLIRAction : public CIRGenAction { EmitMLIRAction(mlir::MLIRContext *mlirCtx = nullptr); }; +class EmitMLIRLLVMAction : public CIRGenAction { + virtual void anchor(); + +public: + EmitMLIRLLVMAction(mlir::MLIRContext *mlirCtx = nullptr); +}; + class EmitLLVMAction : public CIRGenAction { virtual void anchor(); diff --git a/clang/include/clang/CIR/LowerToLLVM.h b/clang/include/clang/CIR/LowerToLLVM.h index 2992163196e7..dfa4e59b5e1c 100644 --- a/clang/include/clang/CIR/LowerToLLVM.h +++ b/clang/include/clang/CIR/LowerToLLVM.h @@ -29,13 +29,19 @@ class ModuleOp; namespace cir { namespace direct { +mlir::ModuleOp lowerDirectlyFromCIRToLLVMDialect(mlir::ModuleOp theModule, + bool disableVerifier = false, + bool disableCCLowering = false, + bool disableDebugInfo = false); + +// Lower directly from pristine CIR to LLVMIR. std::unique_ptr lowerDirectlyFromCIRToLLVMIR( mlir::ModuleOp theModule, llvm::LLVMContext &llvmCtx, bool disableVerifier = false, bool disableCCLowering = false, bool disableDebugInfo = false); -} -// Lower directly from pristine CIR to LLVMIR. +} // namespace direct + std::unique_ptr lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule, std::unique_ptr mlirCtx, diff --git a/clang/include/clang/CIR/LowerToMLIR.h b/clang/include/clang/CIR/LowerToMLIR.h index 567deb7abc7d..1ddced958cee 100644 --- a/clang/include/clang/CIR/LowerToMLIR.h +++ b/clang/include/clang/CIR/LowerToMLIR.h @@ -12,10 +12,16 @@ #ifndef CLANG_CIR_LOWERTOMLIR_H #define CLANG_CIR_LOWERTOMLIR_H +#include "mlir/Transforms/DialectConversion.h" + namespace cir { void populateCIRLoopToSCFConversionPatterns(mlir::RewritePatternSet &patterns, mlir::TypeConverter &converter); + +mlir::ModuleOp +lowerFromCIRToMLIRToLLVMDialect(mlir::ModuleOp theModule, + mlir::MLIRContext *mlirCtx = nullptr); } // namespace cir #endif // CLANG_CIR_LOWERTOMLIR_H_ diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 3bd0fce9ab29..e59eb377b1af 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3121,6 +3121,8 @@ def emit_cir_flat : Flag<["-"], "emit-cir-flat">, Visibility<[ClangOption, CC1Op Group, HelpText<"Similar to -emit-cir but also lowers structured CFG into basic blocks.">; def emit_mlir : Flag<["-"], "emit-mlir">, Visibility<[CC1Option]>, Group, HelpText<"Build ASTs and then lower through ClangIR to MLIR, emit the .milr file">; +def emit_mlir_llvm : Flag<["-"], "emit-mlir-llvm">, Visibility<[CC1Option]>, Group, + HelpText<"Build ASTs and then lower through ClangIR to MLIR LLVM Dialect, emit the .mlir file">; /// ClangIR-specific options - END def flto : Flag<["-"], "flto">, diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index c20744b49136..9eb60621dceb 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -77,6 +77,9 @@ enum ActionKind { /// Emit a .mlir file EmitMLIR, + /// Emit an .mlir file with LLVM dialect + EmitMLIRLLVM, + /// Emit a .ll file. EmitLLVM, diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index 5cb81e89388f..1cc808f02ed9 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -26,6 +26,7 @@ #include "clang/CIR/CIRToCIRPasses.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/LowerToLLVM.h" +#include "clang/CIR/LowerToMLIR.h" #include "clang/CIR/Passes.h" #include "clang/CodeGen/BackendUtil.h" #include "clang/CodeGen/ModuleBuilder.h" @@ -260,6 +261,14 @@ class CIRGenConsumer : public clang::ASTConsumer { } } + auto printMlirModule = [&](mlir::ModuleOp root) { + assert(outputStream && "Why are we here without an output stream?"); + // FIXME: we cannot roundtrip prettyForm=true right now. + mlir::OpPrintingFlags flags; + flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); + root->print(*outputStream, flags); + }; + switch (action) { case CIRGenAction::OutputType::EmitCIR: case CIRGenAction::OutputType::EmitCIRFlat: @@ -274,11 +283,15 @@ class CIRGenConsumer : public clang::ASTConsumer { break; case CIRGenAction::OutputType::EmitMLIR: { auto loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get()); - assert(outputStream && "Why are we here without an output stream?"); - // FIXME: we cannot roundtrip prettyForm=true right now. - mlir::OpPrintingFlags flags; - flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); - loweredMlirModule->print(*outputStream, flags); + printMlirModule(loweredMlirModule); + break; + } + case CIRGenAction::OutputType::EmitMLIRLLVM: { + auto loweredMlirModule = + feOptions.ClangIRDirectLowering + ? direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod) + : lowerFromCIRToMLIRToLLVMDialect(mlirMod); + printMlirModule(loweredMlirModule); break; } case CIRGenAction::OutputType::EmitLLVM: @@ -362,6 +375,8 @@ getOutputStream(CompilerInstance &ci, StringRef inFile, return ci.createDefaultOutputFile(false, inFile, "cir"); case CIRGenAction::OutputType::EmitMLIR: return ci.createDefaultOutputFile(false, inFile, "mlir"); + case CIRGenAction::OutputType::EmitMLIRLLVM: + return ci.createDefaultOutputFile(false, inFile, "mlir"); case CIRGenAction::OutputType::EmitLLVM: return ci.createDefaultOutputFile(false, inFile, "ll"); case CIRGenAction::OutputType::EmitBC: @@ -472,6 +487,10 @@ void EmitMLIRAction::anchor() {} EmitMLIRAction::EmitMLIRAction(mlir::MLIRContext *_MLIRContext) : CIRGenAction(OutputType::EmitMLIR, _MLIRContext) {} +void EmitMLIRLLVMAction::anchor() {} +EmitMLIRLLVMAction::EmitMLIRLLVMAction(mlir::MLIRContext *_MLIRContext) + : CIRGenAction(OutputType::EmitMLIRLLVM, _MLIRContext) {} + void EmitLLVMAction::anchor() {} EmitLLVMAction::EmitLLVMAction(mlir::MLIRContext *_MLIRContext) : CIRGenAction(OutputType::EmitLLVM, _MLIRContext) {} diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 0331122f35d2..b3aad00aadd5 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -4689,11 +4689,11 @@ void populateCIRToLLVMPasses(mlir::OpPassManager &pm, bool useCCLowering) { extern void registerCIRDialectTranslation(mlir::MLIRContext &context); -std::unique_ptr -lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx, - bool disableVerifier, bool disableCCLowering, - bool disableDebugInfo) { - llvm::TimeTraceScope scope("lower from CIR to LLVM directly"); +mlir::ModuleOp lowerDirectlyFromCIRToLLVMDialect(mlir::ModuleOp theModule, + bool disableVerifier, + bool disableCCLowering, + bool disableDebugInfo) { + llvm::TimeTraceScope scope("lower from CIR to LLVM Dialect"); mlir::MLIRContext *mlirCtx = theModule.getContext(); mlir::PassManager pm(mlirCtx); @@ -4722,6 +4722,20 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx, if (theModule.verify().failed()) report_fatal_error("Verification of the final LLVMIR dialect failed!"); + return theModule; +} + +std::unique_ptr +lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx, + bool disableVerifier, bool disableCCLowering, + bool disableDebugInfo) { + llvm::TimeTraceScope scope("lower from CIR to LLVM directly"); + + lowerDirectlyFromCIRToLLVMDialect(theModule, disableVerifier, + disableCCLowering, disableDebugInfo); + + mlir::MLIRContext *mlirCtx = theModule.getContext(); + mlir::registerBuiltinDialectTranslation(*mlirCtx); mlir::registerLLVMDialectTranslation(*mlirCtx); mlir::registerOpenMPDialectTranslation(*mlirCtx); diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp index 8a3e6227b072..75c905299bdb 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp +++ b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp @@ -47,6 +47,7 @@ #include "mlir/Transforms/DialectConversion.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/Dialect/IR/CIRTypes.h" +#include "clang/CIR/LowerToLLVM.h" #include "clang/CIR/LowerToMLIR.h" #include "clang/CIR/LoweringHelpers.h" #include "clang/CIR/Passes.h" @@ -1458,13 +1459,14 @@ void ConvertCIRToMLIRPass::runOnOperation() { signalPassFailure(); } -std::unique_ptr -lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule, - std::unique_ptr mlirCtx, - LLVMContext &llvmCtx) { - llvm::TimeTraceScope scope("Lower from CIR to MLIR To LLVM"); +mlir::ModuleOp lowerFromCIRToMLIRToLLVMDialect(mlir::ModuleOp theModule, + mlir::MLIRContext *mlirCtx) { + llvm::TimeTraceScope scope("Lower from CIR to MLIR To LLVM Dialect"); + if (!mlirCtx) { + mlirCtx = theModule.getContext(); + } - mlir::PassManager pm(mlirCtx.get()); + mlir::PassManager pm(mlirCtx); pm.addPass(createConvertCIRToMLIRPass()); pm.addPass(createConvertMLIRToLLVMPass()); @@ -1478,6 +1480,17 @@ lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule, if (theModule.verify().failed()) report_fatal_error("Verification of the final LLVMIR dialect failed!"); + return theModule; +} + +std::unique_ptr +lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule, + std::unique_ptr mlirCtx, + LLVMContext &llvmCtx) { + llvm::TimeTraceScope scope("Lower from CIR to MLIR To LLVM"); + + lowerFromCIRToMLIRToLLVMDialect(theModule, mlirCtx.get()); + mlir::registerBuiltinDialectTranslation(*mlirCtx); mlir::registerLLVMDialectTranslation(*mlirCtx); mlir::registerOpenMPDialectTranslation(*mlirCtx); diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index a505b5412448..b044eafe5ab9 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2738,6 +2738,7 @@ static const auto &getFrontendActionTable() { {frontend::EmitCIRFlat, OPT_emit_cir_flat}, {frontend::EmitCIROnly, OPT_emit_cir_only}, {frontend::EmitMLIR, OPT_emit_mlir}, + {frontend::EmitMLIRLLVM, OPT_emit_mlir_llvm}, {frontend::EmitHTML, OPT_emit_html}, {frontend::EmitLLVM, OPT_emit_llvm}, {frontend::EmitLLVMOnly, OPT_emit_llvm_only}, @@ -4669,6 +4670,7 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) { case frontend::EmitCIRFlat: case frontend::EmitCIROnly: case frontend::EmitMLIR: + case frontend::EmitMLIRLLVM: case frontend::EmitHTML: case frontend::EmitLLVM: case frontend::EmitLLVMOnly: diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp index 4a54d90a6591..7524a87b09fd 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -99,6 +99,8 @@ CreateFrontendBaseAction(CompilerInstance &CI) { return std::make_unique(); case EmitMLIR: return std::make_unique(); + case EmitMLIRLLVM: + return std::make_unique(); #else case EmitCIR: case EmitCIRFlat: From d51b6250c767714d9b51f998e5b98be67946f08c Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Fri, 14 Feb 2025 15:54:33 +0100 Subject: [PATCH 2/5] amend! [CIR] Add option to emit MLIR in LLVM dialect. [CIR] Add option to emit MLIR in a selected dialect This PR adds `-emit-mlir=` option that emits MLIR in the specified dialect. The `-emit-mlir` option now emits the MLIR in the LLVM dialect when direct lowering to CIR is used (instead of producing an error). --- .../clang/CIR/FrontendAction/CIRGenAction.h | 8 --- clang/include/clang/Driver/Options.td | 13 +++- .../include/clang/Frontend/FrontendOptions.h | 10 ++- clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 65 +++++++++++-------- clang/lib/Frontend/CompilerInvocation.cpp | 10 ++- .../ExecuteCompilerInvocation.cpp | 9 ++- 6 files changed, 67 insertions(+), 48 deletions(-) diff --git a/clang/include/clang/CIR/FrontendAction/CIRGenAction.h b/clang/include/clang/CIR/FrontendAction/CIRGenAction.h index 92f40231ac19..6618fbc54261 100644 --- a/clang/include/clang/CIR/FrontendAction/CIRGenAction.h +++ b/clang/include/clang/CIR/FrontendAction/CIRGenAction.h @@ -37,7 +37,6 @@ class CIRGenAction : public clang::ASTFrontendAction { EmitLLVM, EmitBC, EmitMLIR, - EmitMLIRLLVM, EmitObj, None }; @@ -102,13 +101,6 @@ class EmitMLIRAction : public CIRGenAction { EmitMLIRAction(mlir::MLIRContext *mlirCtx = nullptr); }; -class EmitMLIRLLVMAction : public CIRGenAction { - virtual void anchor(); - -public: - EmitMLIRLLVMAction(mlir::MLIRContext *mlirCtx = nullptr); -}; - class EmitLLVMAction : public CIRGenAction { virtual void anchor(); diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index e59eb377b1af..efe1a30ad904 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3120,9 +3120,16 @@ def emit_cir_only : Flag<["-"], "emit-cir-only">, def emit_cir_flat : Flag<["-"], "emit-cir-flat">, Visibility<[ClangOption, CC1Option]>, Group, HelpText<"Similar to -emit-cir but also lowers structured CFG into basic blocks.">; def emit_mlir : Flag<["-"], "emit-mlir">, Visibility<[CC1Option]>, Group, - HelpText<"Build ASTs and then lower through ClangIR to MLIR, emit the .milr file">; -def emit_mlir_llvm : Flag<["-"], "emit-mlir-llvm">, Visibility<[CC1Option]>, Group, - HelpText<"Build ASTs and then lower through ClangIR to MLIR LLVM Dialect, emit the .mlir file">; + HelpText<"Build ASTs and then lower through ClangIR to MLIR (standard dialects " + "or the LLVM dialect), emit the .mlir file.">; +def emit_mlir_EQ : Joined<["-"], "emit-mlir=">, Visibility<[CC1Option]>, Group, + HelpText<"Build ASTs and then lower through ClangIR to the selected MLIR dialect, emit the .mlir file. " + "Allowed values are `std` for MLIR standard dialects " + "`llvm` for the LLVM dialect and `cir` for the ClangIR dialect.">, + Values<"std,llvm,cir">, + NormalizedValuesScope<"FrontendOptions">, + NormalizedValues<["MLIR_STD", "MLIR_LLVM", "MLIR_CIR"]>, + MarshallingInfoEnum, "MLIR_Default">; /// ClangIR-specific options - END def flto : Flag<["-"], "flto">, diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index 9eb60621dceb..a1fb78dd52ab 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -77,9 +77,6 @@ enum ActionKind { /// Emit a .mlir file EmitMLIR, - /// Emit an .mlir file with LLVM dialect - EmitMLIRLLVM, - /// Emit a .ll file. EmitLLVM, @@ -533,6 +530,13 @@ class FrontendOptions { std::string ClangIRIdiomRecognizerOpts; std::string ClangIRLibOptOpts; + enum { + MLIR_Default, + MLIR_STD, + MLIR_LLVM, + MLIR_CIR + } MLIRTargetDialect = MLIR_Default; + /// The input kind, either specified via -x argument or deduced from the input /// file name. InputKind DashX; diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index 1cc808f02ed9..2f67b64739d6 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -261,17 +261,7 @@ class CIRGenConsumer : public clang::ASTConsumer { } } - auto printMlirModule = [&](mlir::ModuleOp root) { - assert(outputStream && "Why are we here without an output stream?"); - // FIXME: we cannot roundtrip prettyForm=true right now. - mlir::OpPrintingFlags flags; - flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); - root->print(*outputStream, flags); - }; - - switch (action) { - case CIRGenAction::OutputType::EmitCIR: - case CIRGenAction::OutputType::EmitCIRFlat: + auto emitCIR = [&]() { if (outputStream && mlirMod) { // FIXME: we cannot roundtrip prettyForm=true right now. mlir::OpPrintingFlags flags; @@ -280,18 +270,45 @@ class CIRGenConsumer : public clang::ASTConsumer { flags.assumeVerified(); mlirMod->print(*outputStream, flags); } + }; + + switch (action) { + case CIRGenAction::OutputType::EmitCIR: + case CIRGenAction::OutputType::EmitCIRFlat: + emitCIR(); break; case CIRGenAction::OutputType::EmitMLIR: { - auto loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get()); - printMlirModule(loweredMlirModule); - break; - } - case CIRGenAction::OutputType::EmitMLIRLLVM: { - auto loweredMlirModule = - feOptions.ClangIRDirectLowering - ? direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod) - : lowerFromCIRToMLIRToLLVMDialect(mlirMod); - printMlirModule(loweredMlirModule); + mlir::ModuleOp loweredMlirModule; + switch (feOptions.MLIRTargetDialect) { + case clang::FrontendOptions::MLIR_Default: + loweredMlirModule = + feOptions.ClangIRDirectLowering + ? direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod) + : lowerFromCIRToMLIR(mlirMod, mlirCtx.get()); + + break; + case clang::FrontendOptions::MLIR_STD: + // Attempting to emit std with direct lowering is already checked by + // Compiler Invocation + loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get()); + break; + case clang::FrontendOptions::MLIR_LLVM: + loweredMlirModule = + feOptions.ClangIRDirectLowering + ? direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod) + : lowerFromCIRToMLIRToLLVMDialect(mlirMod, mlirCtx.get()); + break; + case clang::FrontendOptions::MLIR_CIR: + emitCIR(); + break; + } + if (loweredMlirModule) { + assert(outputStream && "Why are we here without an output stream?"); + // FIXME: we cannot roundtrip prettyForm=true right now. + mlir::OpPrintingFlags flags; + flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); + loweredMlirModule->print(*outputStream, flags); + } break; } case CIRGenAction::OutputType::EmitLLVM: @@ -375,8 +392,6 @@ getOutputStream(CompilerInstance &ci, StringRef inFile, return ci.createDefaultOutputFile(false, inFile, "cir"); case CIRGenAction::OutputType::EmitMLIR: return ci.createDefaultOutputFile(false, inFile, "mlir"); - case CIRGenAction::OutputType::EmitMLIRLLVM: - return ci.createDefaultOutputFile(false, inFile, "mlir"); case CIRGenAction::OutputType::EmitLLVM: return ci.createDefaultOutputFile(false, inFile, "ll"); case CIRGenAction::OutputType::EmitBC: @@ -487,10 +502,6 @@ void EmitMLIRAction::anchor() {} EmitMLIRAction::EmitMLIRAction(mlir::MLIRContext *_MLIRContext) : CIRGenAction(OutputType::EmitMLIR, _MLIRContext) {} -void EmitMLIRLLVMAction::anchor() {} -EmitMLIRLLVMAction::EmitMLIRLLVMAction(mlir::MLIRContext *_MLIRContext) - : CIRGenAction(OutputType::EmitMLIRLLVM, _MLIRContext) {} - void EmitLLVMAction::anchor() {} EmitLLVMAction::EmitLLVMAction(mlir::MLIRContext *_MLIRContext) : CIRGenAction(OutputType::EmitLLVM, _MLIRContext) {} diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index b044eafe5ab9..6f199b259dcd 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2738,7 +2738,7 @@ static const auto &getFrontendActionTable() { {frontend::EmitCIRFlat, OPT_emit_cir_flat}, {frontend::EmitCIROnly, OPT_emit_cir_only}, {frontend::EmitMLIR, OPT_emit_mlir}, - {frontend::EmitMLIRLLVM, OPT_emit_mlir_llvm}, + {frontend::EmitMLIR, OPT_emit_mlir_EQ}, {frontend::EmitHTML, OPT_emit_html}, {frontend::EmitLLVM, OPT_emit_llvm}, {frontend::EmitLLVMOnly, OPT_emit_llvm_only}, @@ -2850,6 +2850,13 @@ static void GenerateFrontendArgs(const FrontendOptions &Opts, }; } + if (Opts.ProgramAction == frontend::EmitMLIR) { + GenerateProgramAction = [&]() { + if (Opts.MLIRTargetDialect == FrontendOptions::MLIR_Default) + GenerateArg(Consumer, OPT_emit_mlir); + }; + } + if (Opts.ProgramAction == frontend::FixIt && !Opts.FixItSuffix.empty()) { GenerateProgramAction = [&]() { GenerateArg(Consumer, OPT_fixit_EQ, Opts.FixItSuffix); @@ -4670,7 +4677,6 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) { case frontend::EmitCIRFlat: case frontend::EmitCIROnly: case frontend::EmitMLIR: - case frontend::EmitMLIRLLVM: case frontend::EmitHTML: case frontend::EmitLLVM: case frontend::EmitLLVMOnly: diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp index 7524a87b09fd..7f41545d6d14 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -60,9 +60,10 @@ CreateFrontendBaseAction(CompilerInstance &CI) { llvm::report_fatal_error( "-emit-cir and -emit-cir-only only valid when using -fclangir"); - if (CI.getFrontendOpts().ClangIRDirectLowering && Act == EmitMLIR) - llvm::report_fatal_error( - "ClangIR direct lowering is incompatible with -emit-mlir"); + if (Act == EmitMLIR && CI.getFrontendOpts().ClangIRDirectLowering && + CI.getFrontendOpts().MLIRTargetDialect == FrontendOptions::MLIR_STD) + llvm::report_fatal_error("ClangIR direct lowering is incompatible with " + "emitting of MLIR standard dialects"); switch (CI.getFrontendOpts().ProgramAction) { case ASTDeclList: return std::make_unique(); @@ -99,8 +100,6 @@ CreateFrontendBaseAction(CompilerInstance &CI) { return std::make_unique(); case EmitMLIR: return std::make_unique(); - case EmitMLIRLLVM: - return std::make_unique(); #else case EmitCIR: case EmitCIRFlat: From 47de848da7438f031eebf1eb0c7f5b86b0c899c8 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 19 Feb 2025 15:12:03 +0100 Subject: [PATCH 3/5] fixup! [CIR] Add option to emit MLIR in LLVM dialect. --- clang/include/clang/Driver/Options.td | 10 ++-- .../include/clang/Frontend/FrontendOptions.h | 7 +-- clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 52 +++++++------------ clang/test/CIR/emit-mlir.c | 23 ++++++++ 4 files changed, 47 insertions(+), 45 deletions(-) create mode 100644 clang/test/CIR/emit-mlir.c diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index efe1a30ad904..4296279ca76f 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3121,14 +3121,14 @@ def emit_cir_flat : Flag<["-"], "emit-cir-flat">, Visibility<[ClangOption, CC1Op Group, HelpText<"Similar to -emit-cir but also lowers structured CFG into basic blocks.">; def emit_mlir : Flag<["-"], "emit-mlir">, Visibility<[CC1Option]>, Group, HelpText<"Build ASTs and then lower through ClangIR to MLIR (standard dialects " - "or the LLVM dialect), emit the .mlir file.">; + "when `-fno-clangir-direct-lowering` is used or the LLVM dialect when " + "`-fclangir-direct-lowering` is used), emit the .mlir file.">; def emit_mlir_EQ : Joined<["-"], "emit-mlir=">, Visibility<[CC1Option]>, Group, HelpText<"Build ASTs and then lower through ClangIR to the selected MLIR dialect, emit the .mlir file. " - "Allowed values are `std` for MLIR standard dialects " - "`llvm` for the LLVM dialect and `cir` for the ClangIR dialect.">, - Values<"std,llvm,cir">, + "Allowed values are `std` for MLIR standard dialects and `llvm` for the LLVM dialect.">, + Values<"std,llvm">, NormalizedValuesScope<"FrontendOptions">, - NormalizedValues<["MLIR_STD", "MLIR_LLVM", "MLIR_CIR"]>, + NormalizedValues<["MLIR_STD", "MLIR_LLVM"]>, MarshallingInfoEnum, "MLIR_Default">; /// ClangIR-specific options - END diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index a1fb78dd52ab..fa52ae8ed9f0 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -530,12 +530,7 @@ class FrontendOptions { std::string ClangIRIdiomRecognizerOpts; std::string ClangIRLibOptOpts; - enum { - MLIR_Default, - MLIR_STD, - MLIR_LLVM, - MLIR_CIR - } MLIRTargetDialect = MLIR_Default; + enum { MLIR_Default, MLIR_STD, MLIR_LLVM } MLIRTargetDialect = MLIR_Default; /// The input kind, either specified via -x argument or deduced from the input /// file name. diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index 2f67b64739d6..c92cd3ab640f 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -261,7 +261,9 @@ class CIRGenConsumer : public clang::ASTConsumer { } } - auto emitCIR = [&]() { + switch (action) { + case CIRGenAction::OutputType::EmitCIR: + case CIRGenAction::OutputType::EmitCIRFlat: if (outputStream && mlirMod) { // FIXME: we cannot roundtrip prettyForm=true right now. mlir::OpPrintingFlags flags; @@ -270,45 +272,27 @@ class CIRGenConsumer : public clang::ASTConsumer { flags.assumeVerified(); mlirMod->print(*outputStream, flags); } - }; - - switch (action) { - case CIRGenAction::OutputType::EmitCIR: - case CIRGenAction::OutputType::EmitCIRFlat: - emitCIR(); break; case CIRGenAction::OutputType::EmitMLIR: { mlir::ModuleOp loweredMlirModule; - switch (feOptions.MLIRTargetDialect) { - case clang::FrontendOptions::MLIR_Default: - loweredMlirModule = - feOptions.ClangIRDirectLowering - ? direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod) - : lowerFromCIRToMLIR(mlirMod, mlirCtx.get()); - - break; - case clang::FrontendOptions::MLIR_STD: + if (feOptions.ClangIRDirectLowering) { // Attempting to emit std with direct lowering is already checked by // Compiler Invocation - loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get()); - break; - case clang::FrontendOptions::MLIR_LLVM: - loweredMlirModule = - feOptions.ClangIRDirectLowering - ? direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod) - : lowerFromCIRToMLIRToLLVMDialect(mlirMod, mlirCtx.get()); - break; - case clang::FrontendOptions::MLIR_CIR: - emitCIR(); - break; - } - if (loweredMlirModule) { - assert(outputStream && "Why are we here without an output stream?"); - // FIXME: we cannot roundtrip prettyForm=true right now. - mlir::OpPrintingFlags flags; - flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); - loweredMlirModule->print(*outputStream, flags); + loweredMlirModule = direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod); + } else { + if (feOptions.MLIRTargetDialect == clang::FrontendOptions::MLIR_LLVM) + loweredMlirModule = + lowerFromCIRToMLIRToLLVMDialect(mlirMod, mlirCtx.get()); + else // STD and Default cases + loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get()); } + assert(loweredMlirModule && + "MLIR module does not exist, but lowering did not fail?"); + assert(outputStream && "Why are we here without an output stream?"); + // FIXME: we cannot roundtrip prettyForm=true right now. + mlir::OpPrintingFlags flags; + flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); + loweredMlirModule->print(*outputStream, flags); break; } case CIRGenAction::OutputType::EmitLLVM: diff --git a/clang/test/CIR/emit-mlir.c b/clang/test/CIR/emit-mlir.c new file mode 100644 index 000000000000..0e2e8ddf4999 --- /dev/null +++ b/clang/test/CIR/emit-mlir.c @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir %s -o - | FileCheck %s -check-prefix=LLVM +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir %s -o - | FileCheck %s -check-prefix=STD + +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=llvm %s -o - | FileCheck %s -check-prefix=LLVM +// RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=std %s -o - 2>&1 | FileCheck %s -check-prefix=STD_ERR + +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir=llvm %s -o - | FileCheck %s -check-prefix=LLVM +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir=std %s -o - | FileCheck %s -check-prefix=STD + +// RUN: %clang -fclangir -Xclang -emit-mlir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_NO_VALUE +// RUN: %clang -fclangir -Xclang -emit-mlir=llvm %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_LLVM +// RUN: %clang -fno-clangir-direct-lowering -Xclang -emit-mlir=std %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_STD + +int foo(int a, int b) { + return a + b; +} + +// LLVM: llvm.func @foo +// STD: func.func @foo +// STD_ERR: ClangIR direct lowering is incompatible with emitting of MLIR standard dialects +// OPTS_NO_VALUE: "-emit-mlir" +// OPTS_LLVM: "-emit-mlir=llvm" +// OPTS_STD: "-emit-mlir=std" From e3d820f8fea12a895f59201f7bd2958087f136de Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Tue, 25 Feb 2025 20:22:00 +0100 Subject: [PATCH 4/5] fixup! [CIR] Add option to emit MLIR in LLVM dialect. --- clang/include/clang/Driver/Options.td | 14 ++--- .../include/clang/Frontend/FrontendOptions.h | 4 +- clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 63 ++++++++++--------- clang/lib/Driver/Driver.cpp | 6 ++ clang/lib/Driver/ToolChains/Clang.cpp | 12 ++++ clang/lib/Frontend/CompilerInvocation.cpp | 7 ++- .../ExecuteCompilerInvocation.cpp | 2 +- clang/test/CIR/Lowering/ThroughMLIR/binop.cpp | 2 +- clang/test/CIR/Lowering/ThroughMLIR/bit.c | 2 +- clang/test/CIR/Lowering/ThroughMLIR/call.c | 2 +- clang/test/CIR/Lowering/ThroughMLIR/cmp.cpp | 2 +- clang/test/CIR/Lowering/ThroughMLIR/doWhile.c | 2 +- clang/test/CIR/Lowering/ThroughMLIR/for.cpp | 2 +- .../test/CIR/Lowering/ThroughMLIR/global.cpp | 2 +- clang/test/CIR/Lowering/ThroughMLIR/if.c | 2 +- .../test/CIR/Lowering/ThroughMLIR/vectype.cpp | 2 +- clang/test/CIR/Lowering/ThroughMLIR/while.c | 2 +- clang/test/CIR/cc1.c | 2 +- clang/test/CIR/emit-mlir.c | 45 +++++++++---- clang/test/CIR/mlirprint.c | 2 +- 20 files changed, 113 insertions(+), 64 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 4296279ca76f..0d93bd3ab1e8 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3119,17 +3119,17 @@ def emit_cir_only : Flag<["-"], "emit-cir-only">, HelpText<"Build ASTs and convert to CIR, discarding output">; def emit_cir_flat : Flag<["-"], "emit-cir-flat">, Visibility<[ClangOption, CC1Option]>, Group, HelpText<"Similar to -emit-cir but also lowers structured CFG into basic blocks.">; -def emit_mlir : Flag<["-"], "emit-mlir">, Visibility<[CC1Option]>, Group, +def emit_mlir : Flag<["-"], "emit-mlir">, Visibility<[ClangOption]>, Group, HelpText<"Build ASTs and then lower through ClangIR to MLIR (standard dialects " "when `-fno-clangir-direct-lowering` is used or the LLVM dialect when " "`-fclangir-direct-lowering` is used), emit the .mlir file.">; -def emit_mlir_EQ : Joined<["-"], "emit-mlir=">, Visibility<[CC1Option]>, Group, +def emit_mlir_EQ : Joined<["-"], "emit-mlir=">, Visibility<[ClangOption, CC1Option]>, Group, HelpText<"Build ASTs and then lower through ClangIR to the selected MLIR dialect, emit the .mlir file. " - "Allowed values are `std` for MLIR standard dialects and `llvm` for the LLVM dialect.">, - Values<"std,llvm">, - NormalizedValuesScope<"FrontendOptions">, - NormalizedValues<["MLIR_STD", "MLIR_LLVM"]>, - MarshallingInfoEnum, "MLIR_Default">; + "Allowed values are `core` for MLIR standard dialects and `llvm` for the LLVM dialect.">, + Values<"core,llvm,cir,cir-flat">, + NormalizedValuesScope<"frontend">, + NormalizedValues<["MLIR_CORE", "MLIR_LLVM", "MLIR_CIR", "MLIR_CIR_FLAT"]>, + MarshallingInfoEnum, "MLIR_CORE">; /// ClangIR-specific options - END def flto : Flag<["-"], "flto">, diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index fa52ae8ed9f0..f90eb7b072a3 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -160,6 +160,8 @@ enum ActionKind { PrintDependencyDirectivesSourceMinimizerOutput }; +enum MLIRDialectKind { MLIR_CORE, MLIR_LLVM, MLIR_CIR, MLIR_CIR_FLAT }; + } // namespace frontend /// The kind of a file that we've been handed as an input. @@ -530,7 +532,7 @@ class FrontendOptions { std::string ClangIRIdiomRecognizerOpts; std::string ClangIRLibOptOpts; - enum { MLIR_Default, MLIR_STD, MLIR_LLVM } MLIRTargetDialect = MLIR_Default; + frontend::MLIRDialectKind MLIRTargetDialect; /// The input kind, either specified via -x argument or deduced from the input /// file name. diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index c92cd3ab640f..7eb5fa5596e6 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -204,6 +204,9 @@ class CIRGenConsumer : public clang::ASTConsumer { bool enableCCLowering = feOptions.ClangIRCallConvLowering && action != CIRGenAction::OutputType::EmitCIR; + bool flattenCIR = + action == CIRGenAction::OutputType::EmitCIRFlat || + feOptions.MLIRTargetDialect == clang::frontend::MLIR_CIR_FLAT; // Setup and run CIR pipeline. std::string passOptParsingFailure; @@ -212,8 +215,7 @@ class CIRGenConsumer : public clang::ASTConsumer { feOptions.ClangIRLifetimeCheck, lifetimeOpts, feOptions.ClangIRIdiomRecognizer, idiomRecognizerOpts, feOptions.ClangIRLibOpt, libOptOpts, passOptParsingFailure, - codeGenOptions.OptimizationLevel > 0, - action == CIRGenAction::OutputType::EmitCIRFlat, + codeGenOptions.OptimizationLevel > 0, flattenCIR, action == CIRGenAction::OutputType::EmitMLIR, enableCCLowering, feOptions.ClangIREnableMem2Reg) .failed()) { @@ -261,38 +263,43 @@ class CIRGenConsumer : public clang::ASTConsumer { } } + auto emitMLIR = [&](mlir::Operation *mlirMod, bool verify) { + assert(mlirMod && + "MLIR module does not exist, but lowering did not fail?"); + assert(outputStream && "Why are we here without an output stream?"); + // FIXME: we cannot roundtrip prettyForm=true right now. + mlir::OpPrintingFlags flags; + flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); + if (!verify) + flags.assumeVerified(); + mlirMod->print(*outputStream, flags); + }; + switch (action) { case CIRGenAction::OutputType::EmitCIR: case CIRGenAction::OutputType::EmitCIRFlat: - if (outputStream && mlirMod) { - // FIXME: we cannot roundtrip prettyForm=true right now. - mlir::OpPrintingFlags flags; - flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); - if (feOptions.ClangIRDisableCIRVerifier) - flags.assumeVerified(); - mlirMod->print(*outputStream, flags); - } + emitMLIR(mlirMod, feOptions.ClangIRDisableCIRVerifier); break; case CIRGenAction::OutputType::EmitMLIR: { - mlir::ModuleOp loweredMlirModule; - if (feOptions.ClangIRDirectLowering) { - // Attempting to emit std with direct lowering is already checked by - // Compiler Invocation - loweredMlirModule = direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod); - } else { - if (feOptions.MLIRTargetDialect == clang::FrontendOptions::MLIR_LLVM) - loweredMlirModule = - lowerFromCIRToMLIRToLLVMDialect(mlirMod, mlirCtx.get()); - else // STD and Default cases - loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get()); + switch (feOptions.MLIRTargetDialect) { + case clang::frontend::MLIR_CORE: + // case for direct lowering is already checked in compiler invocation + // no need to check here + emitMLIR(lowerFromCIRToMLIR(mlirMod, mlirCtx.get()), false); + break; + case clang::frontend::MLIR_LLVM: { + mlir::ModuleOp loweredMLIRModule = + feOptions.ClangIRDirectLowering + ? direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod) + : lowerFromCIRToMLIRToLLVMDialect(mlirMod, mlirCtx.get()); + emitMLIR(loweredMLIRModule, false); + break; + } + case clang::frontend::MLIR_CIR: + case clang::frontend::MLIR_CIR_FLAT: + emitMLIR(mlirMod, feOptions.ClangIRDisableCIRVerifier); + break; } - assert(loweredMlirModule && - "MLIR module does not exist, but lowering did not fail?"); - assert(outputStream && "Why are we here without an output stream?"); - // FIXME: we cannot roundtrip prettyForm=true right now. - mlir::OpPrintingFlags flags; - flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); - loweredMlirModule->print(*outputStream, flags); break; } case CIRGenAction::OutputType::EmitLLVM: diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 384fd7f4f2a9..19b624904676 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -422,6 +422,8 @@ phases::ID Driver::getFinalPhase(const DerivedArgList &DAL, (PhaseArg = DAL.getLastArg(options::OPT__analyze)) || (PhaseArg = DAL.getLastArg(options::OPT_emit_cir)) || (PhaseArg = DAL.getLastArg(options::OPT_emit_cir_flat)) || + (PhaseArg = DAL.getLastArg(options::OPT_emit_mlir)) || + (PhaseArg = DAL.getLastArg(options::OPT_emit_mlir_EQ)) || (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) { FinalPhase = phases::Compile; @@ -5087,6 +5089,10 @@ Action *Driver::ConstructPhaseAction( return C.MakeAction(Input, types::TY_CIR); if (Args.hasArg(options::OPT_emit_cir_flat)) return C.MakeAction(Input, types::TY_CIR_FLAT); + if (Args.hasArg(options::OPT_emit_mlir)) + return C.MakeAction(Input, types::TY_MLIR); + if (Args.hasArg(options::OPT_emit_mlir_EQ)) + return C.MakeAction(Input, types::TY_MLIR); if (Args.hasArg(options::OPT_module_file_info)) return C.MakeAction(Input, types::TY_ModuleFile); if (Args.hasArg(options::OPT_verify_pch)) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 7961007757a9..fb467a88da81 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5418,6 +5418,18 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-emit-cir"); } else if (JA.getType() == types::TY_CIR_FLAT) { CmdArgs.push_back("-emit-cir-flat"); + } else if (JA.getType() == types::TY_MLIR) { + if (Args.hasArg(options::OPT_emit_mlir)) { + if (Args.hasArg(options::OPT_fno_clangir_direct_lowering)) { + CmdArgs.push_back("-emit-mlir=core"); + } else { + CmdArgs.push_back("-emit-mlir=llvm"); + } + } else { + CmdArgs.push_back(Args.MakeArgString( + Twine("-emit-mlir=") + + Args.getLastArgValue(options::OPT_emit_mlir_EQ))); + } } else if (JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC) { // Emit textual llvm IR for AMDGPU offloading for -emit-llvm -S diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 6f199b259dcd..43c3f44f4ca8 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2852,8 +2852,8 @@ static void GenerateFrontendArgs(const FrontendOptions &Opts, if (Opts.ProgramAction == frontend::EmitMLIR) { GenerateProgramAction = [&]() { - if (Opts.MLIRTargetDialect == FrontendOptions::MLIR_Default) - GenerateArg(Consumer, OPT_emit_mlir); + if (Opts.MLIRTargetDialect == clang::frontend::MLIR_CORE) + GenerateArg(Consumer, OPT_emit_mlir_EQ, "core"); }; } @@ -3118,7 +3118,8 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, Diags.Report(diag::err_drv_argument_only_allowed_with) << "-fsystem-module" << "-emit-module"; if (Args.hasArg(OPT_fclangir) || Args.hasArg(OPT_emit_cir) || - Args.hasArg(OPT_emit_cir_flat)) + Args.hasArg(OPT_emit_cir_flat) || Args.hasArg(OPT_emit_mlir) || + Args.hasArg(OPT_emit_mlir_EQ)) Opts.UseClangIRPipeline = true; if (Args.hasArg(OPT_fclangir_direct_lowering)) diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp index 7f41545d6d14..f9048c466fc7 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -61,7 +61,7 @@ CreateFrontendBaseAction(CompilerInstance &CI) { "-emit-cir and -emit-cir-only only valid when using -fclangir"); if (Act == EmitMLIR && CI.getFrontendOpts().ClangIRDirectLowering && - CI.getFrontendOpts().MLIRTargetDialect == FrontendOptions::MLIR_STD) + CI.getFrontendOpts().MLIRTargetDialect == frontend::MLIR_CORE) llvm::report_fatal_error("ClangIR direct lowering is incompatible with " "emitting of MLIR standard dialects"); diff --git a/clang/test/CIR/Lowering/ThroughMLIR/binop.cpp b/clang/test/CIR/Lowering/ThroughMLIR/binop.cpp index 7f3c8b98cfd1..83ef9c3d1411 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/binop.cpp +++ b/clang/test/CIR/Lowering/ThroughMLIR/binop.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s void testSignedIntBinOps(int a, int b) { diff --git a/clang/test/CIR/Lowering/ThroughMLIR/bit.c b/clang/test/CIR/Lowering/ThroughMLIR/bit.c index 7d21f991215a..b3acc49c5f69 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/bit.c +++ b/clang/test/CIR/Lowering/ThroughMLIR/bit.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s int clz_u16(unsigned short x) { diff --git a/clang/test/CIR/Lowering/ThroughMLIR/call.c b/clang/test/CIR/Lowering/ThroughMLIR/call.c index a325db5f2dd4..3edb7bc83cdc 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/call.c +++ b/clang/test/CIR/Lowering/ThroughMLIR/call.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s void foo(int i) {} diff --git a/clang/test/CIR/Lowering/ThroughMLIR/cmp.cpp b/clang/test/CIR/Lowering/ThroughMLIR/cmp.cpp index 607f8ad5005f..e5b2f0cb109f 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/cmp.cpp +++ b/clang/test/CIR/Lowering/ThroughMLIR/cmp.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s bool testSignedIntCmpOps(int a, int b) { diff --git a/clang/test/CIR/Lowering/ThroughMLIR/doWhile.c b/clang/test/CIR/Lowering/ThroughMLIR/doWhile.c index 5974734740a2..2a4adb1f0091 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/doWhile.c +++ b/clang/test/CIR/Lowering/ThroughMLIR/doWhile.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s int sum() { diff --git a/clang/test/CIR/Lowering/ThroughMLIR/for.cpp b/clang/test/CIR/Lowering/ThroughMLIR/for.cpp index 3ed99718369a..2a6137a0cdc3 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/for.cpp +++ b/clang/test/CIR/Lowering/ThroughMLIR/for.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s int a[101], b[101]; diff --git a/clang/test/CIR/Lowering/ThroughMLIR/global.cpp b/clang/test/CIR/Lowering/ThroughMLIR/global.cpp index d7627139ff6c..a79707746eff 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/global.cpp +++ b/clang/test/CIR/Lowering/ThroughMLIR/global.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s float f[32000]; diff --git a/clang/test/CIR/Lowering/ThroughMLIR/if.c b/clang/test/CIR/Lowering/ThroughMLIR/if.c index dec3f9968d6a..a7d95d3a104a 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/if.c +++ b/clang/test/CIR/Lowering/ThroughMLIR/if.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s void foo() { diff --git a/clang/test/CIR/Lowering/ThroughMLIR/vectype.cpp b/clang/test/CIR/Lowering/ThroughMLIR/vectype.cpp index 81c9fe063260..fb5bdae0b69a 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/vectype.cpp +++ b/clang/test/CIR/Lowering/ThroughMLIR/vectype.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s typedef int vi4 __attribute__((vector_size(16))); diff --git a/clang/test/CIR/Lowering/ThroughMLIR/while.c b/clang/test/CIR/Lowering/ThroughMLIR/while.c index 68454f3bea99..983be1b1d1ea 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/while.c +++ b/clang/test/CIR/Lowering/ThroughMLIR/while.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s void singleWhile() { diff --git a/clang/test/CIR/cc1.c b/clang/test/CIR/cc1.c index 176ea42d44de..5461562e09f6 100644 --- a/clang/test/CIR/cc1.c +++ b/clang/test/CIR/cc1.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s -check-prefix=MLIR // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll // RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM diff --git a/clang/test/CIR/emit-mlir.c b/clang/test/CIR/emit-mlir.c index 0e2e8ddf4999..0fd7b6442727 100644 --- a/clang/test/CIR/emit-mlir.c +++ b/clang/test/CIR/emit-mlir.c @@ -1,23 +1,44 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir %s -o - | FileCheck %s -check-prefix=LLVM -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir %s -o - | FileCheck %s -check-prefix=STD +// RUN: %clang -fclangir -emit-mlir %s -o - | FileCheck %s -check-prefix=LLVM +// RUN: %clang -fno-clangir-direct-lowering -emit-mlir %s -o - | FileCheck %s -check-prefix=CORE // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=llvm %s -o - | FileCheck %s -check-prefix=LLVM -// RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=std %s -o - 2>&1 | FileCheck %s -check-prefix=STD_ERR +// RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=core %s -o - 2>&1 | FileCheck %s -check-prefix=CORE_ERR // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir=llvm %s -o - | FileCheck %s -check-prefix=LLVM -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir=std %s -o - | FileCheck %s -check-prefix=STD +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir=core %s -o - | FileCheck %s -check-prefix=CORE -// RUN: %clang -fclangir -Xclang -emit-mlir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_NO_VALUE -// RUN: %clang -fclangir -Xclang -emit-mlir=llvm %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_LLVM -// RUN: %clang -fno-clangir-direct-lowering -Xclang -emit-mlir=std %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_STD +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=cir %s -o - | FileCheck %s -check-prefix=CIR +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=cir-flat %s -o - | FileCheck %s -check-prefix=CIR_FLAT + +// RUN: %clang -fclangir -emit-mlir %s -o - | FileCheck %s -check-prefix=LLVM +// RUN: %clang -fno-clangir-direct-lowering -emit-mlir %s -o - | FileCheck %s -check-prefix=CORE + +// RUN: %clang -fclangir -emit-mlir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_LLVM +// RUN: %clang -fno-clangir-direct-lowering -emit-mlir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CORE + +// RUN: %clang -fclangir -emit-mlir=llvm %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_LLVM +// RUN: %clang -fno-clangir-direct-lowering -emit-mlir=core %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CORE +// RUN: %clang -fclangir -emit-mlir=cir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CIR +// RUN: %clang -fclangir -emit-mlir=cir-flat %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CIR_FLAT int foo(int a, int b) { - return a + b; + int c; + if (a) { + c = a; + } + c = b; + return c; } // LLVM: llvm.func @foo -// STD: func.func @foo -// STD_ERR: ClangIR direct lowering is incompatible with emitting of MLIR standard dialects -// OPTS_NO_VALUE: "-emit-mlir" +// CORE: func.func @foo +// CIR: cir.func @foo +// CIR: cir.scope +// CIR_FLAT: cir.func @foo +// CIR_FLAT: ^bb1 +// CIR_FLAT-NOT: cir.scope +// CORE_ERR: ClangIR direct lowering is incompatible with emitting of MLIR standard dialects // OPTS_LLVM: "-emit-mlir=llvm" -// OPTS_STD: "-emit-mlir=std" +// OPTS_CORE: "-emit-mlir=core" +// OPTS_CIR: "-emit-mlir=cir" +// OPTS_CIR_FLAT: "-emit-mlir=cir-flat" diff --git a/clang/test/CIR/mlirprint.c b/clang/test/CIR/mlirprint.c index 3514eb895381..a28e4e851992 100644 --- a/clang/test/CIR/mlirprint.c +++ b/clang/test/CIR/mlirprint.c @@ -1,6 +1,6 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -mmlir --mlir-print-ir-after-all %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CIR // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir-flat -mmlir --mlir-print-ir-after-all %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CIRFLAT -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir -mmlir --mlir-print-ir-after-all %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CIRMLIR +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir=core -mmlir --mlir-print-ir-after-all %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CIRMLIR // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm -mmlir --mlir-print-ir-after-all -mllvm -print-after-all %s -o %t.ll 2>&1 | FileCheck %s -check-prefix=CIR -check-prefix=LLVM // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -mmlir --mlir-print-ir-after=cir-drop-ast %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CIRPASS // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir-flat -mmlir --mlir-print-ir-before=cir-flatten-cfg %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CFGPASS From aec099cc9822b1aad1e068f49f5d401f2263f2b7 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Thu, 27 Feb 2025 18:15:06 +0100 Subject: [PATCH 5/5] fixup! [CIR] Add option to emit MLIR in LLVM dialect. --- .../clang/CIR/FrontendAction/CIRGenAction.h | 18 +---------- clang/include/clang/Driver/Options.td | 14 ++++---- clang/include/clang/Driver/Types.def | 2 -- .../include/clang/Frontend/FrontendOptions.h | 6 ---- clang/lib/CIR/CodeGen/CIRPasses.cpp | 4 +-- clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 32 ++++++------------- clang/lib/Driver/Driver.cpp | 9 ++---- clang/lib/Driver/ToolChains/Clang.cpp | 8 ++--- clang/lib/Frontend/CompilerInvocation.cpp | 7 +--- .../ExecuteCompilerInvocation.cpp | 16 ++++------ clang/test/CIR/emit-mlir.c | 20 ++++++------ 11 files changed, 43 insertions(+), 93 deletions(-) diff --git a/clang/include/clang/CIR/FrontendAction/CIRGenAction.h b/clang/include/clang/CIR/FrontendAction/CIRGenAction.h index 6618fbc54261..218829aec063 100644 --- a/clang/include/clang/CIR/FrontendAction/CIRGenAction.h +++ b/clang/include/clang/CIR/FrontendAction/CIRGenAction.h @@ -32,11 +32,9 @@ class CIRGenAction : public clang::ASTFrontendAction { public: enum class OutputType { EmitAssembly, - EmitCIR, - EmitCIRFlat, + EmitMLIR, EmitLLVM, EmitBC, - EmitMLIR, EmitObj, None }; @@ -73,20 +71,6 @@ class CIRGenAction : public clang::ASTFrontendAction { OutputType action; }; -class EmitCIRAction : public CIRGenAction { - virtual void anchor(); - -public: - EmitCIRAction(mlir::MLIRContext *mlirCtx = nullptr); -}; - -class EmitCIRFlatAction : public CIRGenAction { - virtual void anchor(); - -public: - EmitCIRFlatAction(mlir::MLIRContext *mlirCtx = nullptr); -}; - class EmitCIROnlyAction : public CIRGenAction { virtual void anchor(); diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 0d93bd3ab1e8..62016c25fa20 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3113,12 +3113,6 @@ defm clangir_call_conv_lowering : BoolFOption<"clangir-call-conv-lowering", NegFlag, BothFlags<[], [ClangOption, CC1Option], "">>; -def emit_cir : Flag<["-"], "emit-cir">, Visibility<[ClangOption, CC1Option]>, - Group, HelpText<"Build ASTs and then lower to ClangIR, emit the .cir file">; -def emit_cir_only : Flag<["-"], "emit-cir-only">, - HelpText<"Build ASTs and convert to CIR, discarding output">; -def emit_cir_flat : Flag<["-"], "emit-cir-flat">, Visibility<[ClangOption, CC1Option]>, - Group, HelpText<"Similar to -emit-cir but also lowers structured CFG into basic blocks.">; def emit_mlir : Flag<["-"], "emit-mlir">, Visibility<[ClangOption]>, Group, HelpText<"Build ASTs and then lower through ClangIR to MLIR (standard dialects " "when `-fno-clangir-direct-lowering` is used or the LLVM dialect when " @@ -3130,6 +3124,14 @@ def emit_mlir_EQ : Joined<["-"], "emit-mlir=">, Visibility<[ClangOption, CC1Opti NormalizedValuesScope<"frontend">, NormalizedValues<["MLIR_CORE", "MLIR_LLVM", "MLIR_CIR", "MLIR_CIR_FLAT"]>, MarshallingInfoEnum, "MLIR_CORE">; +def emit_cir : Flag<["-"], "emit-cir">, Visibility<[ClangOption, CC1Option]>, + Group, Alias, AliasArgs<["cir"]>, + HelpText<"Build ASTs and then lower to ClangIR, emit the .cir file">; +def emit_cir_only : Flag<["-"], "emit-cir-only">, + HelpText<"Build ASTs and convert to CIR, discarding output">; +def emit_cir_flat : Flag<["-"], "emit-cir-flat">, Visibility<[ClangOption, CC1Option]>, + Group, Alias, AliasArgs<["cir-flat"]>, + HelpText<"Similar to -emit-cir but also lowers structured CFG into basic blocks.">; /// ClangIR-specific options - END def flto : Flag<["-"], "flto">, diff --git a/clang/include/clang/Driver/Types.def b/clang/include/clang/Driver/Types.def index 2c4d227bf07f..6bc0ff0f4af3 100644 --- a/clang/include/clang/Driver/Types.def +++ b/clang/include/clang/Driver/Types.def @@ -99,8 +99,6 @@ TYPE("ir", LLVM_BC, INVALID, "bc", phases TYPE("lto-ir", LTO_IR, INVALID, "s", phases::Compile, phases::Backend, phases::Assemble, phases::Link) 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-flat", CIR_FLAT, INVALID, "cir", phases::Compile, phases::Backend, phases::Assemble, phases::Link) TYPE("mlir", MLIR, INVALID, "mlir", phases::Compile, phases::Backend, phases::Assemble, phases::Link) // Misc. diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index f90eb7b072a3..66f8d250cec4 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -65,12 +65,6 @@ enum ActionKind { /// Translate input source into HTML. EmitHTML, - /// Emit a .cir file - EmitCIR, - - /// Emit a .cir file with flat ClangIR - EmitCIRFlat, - /// Generate CIR, bud don't emit anything. EmitCIROnly, diff --git a/clang/lib/CIR/CodeGen/CIRPasses.cpp b/clang/lib/CIR/CodeGen/CIRPasses.cpp index 04582af2f517..59d2445cb16e 100644 --- a/clang/lib/CIR/CodeGen/CIRPasses.cpp +++ b/clang/lib/CIR/CodeGen/CIRPasses.cpp @@ -28,7 +28,7 @@ mlir::LogicalResult runCIRToCIRPasses( llvm::StringRef lifetimeOpts, bool enableIdiomRecognizer, llvm::StringRef idiomRecognizerOpts, bool enableLibOpt, llvm::StringRef libOptOpts, std::string &passOptParsingFailure, - bool enableCIRSimplify, bool flattenCIR, bool emitMLIR, + bool enableCIRSimplify, bool flattenCIR, bool emitCore, bool enableCallConvLowering, bool enableMem2Reg) { llvm::TimeTraceScope scope("CIR To CIR Passes"); @@ -81,7 +81,7 @@ mlir::LogicalResult runCIRToCIRPasses( if (enableMem2Reg) pm.addPass(mlir::createMem2Reg()); - if (emitMLIR) + if (emitCore) pm.addPass(mlir::createSCFPreparePass()); // FIXME: once CIRCodenAction fixes emission other than CIR we diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index 7eb5fa5596e6..051525ddb068 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -202,12 +202,17 @@ class CIRGenConsumer : public clang::ASTConsumer { if (feOptions.ClangIRLibOpt) libOptOpts = sanitizePassOptions(feOptions.ClangIRLibOptOpts); - bool enableCCLowering = feOptions.ClangIRCallConvLowering && - action != CIRGenAction::OutputType::EmitCIR; + bool enableCCLowering = + feOptions.ClangIRCallConvLowering && + !(action == CIRGenAction::OutputType::EmitMLIR && + feOptions.MLIRTargetDialect == frontend::MLIR_CIR); bool flattenCIR = - action == CIRGenAction::OutputType::EmitCIRFlat || + action == CIRGenAction::OutputType::EmitMLIR && feOptions.MLIRTargetDialect == clang::frontend::MLIR_CIR_FLAT; + bool emitCore = action == CIRGenAction::OutputType::EmitMLIR && + feOptions.MLIRTargetDialect == clang::frontend::MLIR_CORE; + // Setup and run CIR pipeline. std::string passOptParsingFailure; if (runCIRToCIRPasses( @@ -215,9 +220,8 @@ class CIRGenConsumer : public clang::ASTConsumer { feOptions.ClangIRLifetimeCheck, lifetimeOpts, feOptions.ClangIRIdiomRecognizer, idiomRecognizerOpts, feOptions.ClangIRLibOpt, libOptOpts, passOptParsingFailure, - codeGenOptions.OptimizationLevel > 0, flattenCIR, - action == CIRGenAction::OutputType::EmitMLIR, enableCCLowering, - feOptions.ClangIREnableMem2Reg) + codeGenOptions.OptimizationLevel > 0, flattenCIR, emitCore, + enableCCLowering, feOptions.ClangIREnableMem2Reg) .failed()) { if (!passOptParsingFailure.empty()) diagnosticsEngine.Report(diag::err_drv_cir_pass_opt_parsing) @@ -276,10 +280,6 @@ class CIRGenConsumer : public clang::ASTConsumer { }; switch (action) { - case CIRGenAction::OutputType::EmitCIR: - case CIRGenAction::OutputType::EmitCIRFlat: - emitMLIR(mlirMod, feOptions.ClangIRDisableCIRVerifier); - break; case CIRGenAction::OutputType::EmitMLIR: { switch (feOptions.MLIRTargetDialect) { case clang::frontend::MLIR_CORE: @@ -377,10 +377,6 @@ getOutputStream(CompilerInstance &ci, StringRef inFile, switch (action) { case CIRGenAction::OutputType::EmitAssembly: return ci.createDefaultOutputFile(false, inFile, "s"); - case CIRGenAction::OutputType::EmitCIR: - return ci.createDefaultOutputFile(false, inFile, "cir"); - case CIRGenAction::OutputType::EmitCIRFlat: - return ci.createDefaultOutputFile(false, inFile, "cir"); case CIRGenAction::OutputType::EmitMLIR: return ci.createDefaultOutputFile(false, inFile, "mlir"); case CIRGenAction::OutputType::EmitLLVM: @@ -477,14 +473,6 @@ void EmitAssemblyAction::anchor() {} EmitAssemblyAction::EmitAssemblyAction(mlir::MLIRContext *_MLIRContext) : CIRGenAction(OutputType::EmitAssembly, _MLIRContext) {} -void EmitCIRAction::anchor() {} -EmitCIRAction::EmitCIRAction(mlir::MLIRContext *_MLIRContext) - : CIRGenAction(OutputType::EmitCIR, _MLIRContext) {} - -void EmitCIRFlatAction::anchor() {} -EmitCIRFlatAction::EmitCIRFlatAction(mlir::MLIRContext *_MLIRContext) - : CIRGenAction(OutputType::EmitCIRFlat, _MLIRContext) {} - void EmitCIROnlyAction::anchor() {} EmitCIROnlyAction::EmitCIROnlyAction(mlir::MLIRContext *_MLIRContext) : CIRGenAction(OutputType::None, _MLIRContext) {} diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 19b624904676..67957c7f50ba 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -413,15 +413,14 @@ phases::ID Driver::getFinalPhase(const DerivedArgList &DAL, // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler. } else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) || (PhaseArg = DAL.getLastArg(options::OPT_print_supported_cpus)) || - (PhaseArg = DAL.getLastArg(options::OPT_print_enabled_extensions)) || + (PhaseArg = + DAL.getLastArg(options::OPT_print_enabled_extensions)) || (PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) || (PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) || (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) || (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) || (PhaseArg = DAL.getLastArg(options::OPT__migrate)) || (PhaseArg = DAL.getLastArg(options::OPT__analyze)) || - (PhaseArg = DAL.getLastArg(options::OPT_emit_cir)) || - (PhaseArg = DAL.getLastArg(options::OPT_emit_cir_flat)) || (PhaseArg = DAL.getLastArg(options::OPT_emit_mlir)) || (PhaseArg = DAL.getLastArg(options::OPT_emit_mlir_EQ)) || (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) { @@ -5085,10 +5084,6 @@ Action *Driver::ConstructPhaseAction( return C.MakeAction(Input, types::TY_Remap); if (Args.hasArg(options::OPT_emit_ast)) return C.MakeAction(Input, types::TY_AST); - if (Args.hasArg(options::OPT_emit_cir)) - return C.MakeAction(Input, types::TY_CIR); - if (Args.hasArg(options::OPT_emit_cir_flat)) - return C.MakeAction(Input, types::TY_CIR_FLAT); if (Args.hasArg(options::OPT_emit_mlir)) return C.MakeAction(Input, types::TY_MLIR); if (Args.hasArg(options::OPT_emit_mlir_EQ)) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index fb467a88da81..fa578bd09406 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5242,8 +5242,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } if (Args.hasArg(options::OPT_fclangir) || - Args.hasArg(options::OPT_emit_cir) || - Args.hasArg(options::OPT_emit_cir_flat)) + Args.hasArg(options::OPT_emit_mlir) || + Args.hasArg(options::OPT_emit_mlir_EQ)) CmdArgs.push_back("-fclangir"); Args.addOptOutFlag(CmdArgs, options::OPT_fclangir_direct_lowering, @@ -5414,10 +5414,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } else if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR) { CmdArgs.push_back("-emit-llvm"); - } else if (JA.getType() == types::TY_CIR) { - CmdArgs.push_back("-emit-cir"); - } else if (JA.getType() == types::TY_CIR_FLAT) { - CmdArgs.push_back("-emit-cir-flat"); } else if (JA.getType() == types::TY_MLIR) { if (Args.hasArg(options::OPT_emit_mlir)) { if (Args.hasArg(options::OPT_fno_clangir_direct_lowering)) { diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 43c3f44f4ca8..4b2a2e3d76a4 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2734,8 +2734,6 @@ static const auto &getFrontendActionTable() { {frontend::DumpTokens, OPT_dump_tokens}, {frontend::EmitAssembly, OPT_S}, {frontend::EmitBC, OPT_emit_llvm_bc}, - {frontend::EmitCIR, OPT_emit_cir}, - {frontend::EmitCIRFlat, OPT_emit_cir_flat}, {frontend::EmitCIROnly, OPT_emit_cir_only}, {frontend::EmitMLIR, OPT_emit_mlir}, {frontend::EmitMLIR, OPT_emit_mlir_EQ}, @@ -3117,8 +3115,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_fclangir) || Args.hasArg(OPT_emit_cir) || - Args.hasArg(OPT_emit_cir_flat) || Args.hasArg(OPT_emit_mlir) || + if (Args.hasArg(OPT_fclangir) || Args.hasArg(OPT_emit_mlir) || Args.hasArg(OPT_emit_mlir_EQ)) Opts.UseClangIRPipeline = true; @@ -4674,8 +4671,6 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) { case frontend::ASTView: case frontend::EmitAssembly: case frontend::EmitBC: - case frontend::EmitCIR: - case frontend::EmitCIRFlat: case frontend::EmitCIROnly: case frontend::EmitMLIR: case frontend::EmitHTML: diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp index f9048c466fc7..f97007882b14 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -54,11 +54,14 @@ CreateFrontendBaseAction(CompilerInstance &CI) { auto UseCIR = CI.getFrontendOpts().UseClangIRPipeline; auto Act = CI.getFrontendOpts().ProgramAction; auto CIRAnalysisOnly = CI.getFrontendOpts().ClangIRAnalysisOnly; - auto EmitsCIR = Act == EmitCIR || Act == EmitCIRFlat || Act == EmitCIROnly; + auto EmitsCIR = + Act == EmitCIROnly || + (Act == EmitMLIR && + (CI.getFrontendOpts().MLIRTargetDialect == frontend::MLIR_CIR || + CI.getFrontendOpts().MLIRTargetDialect == frontend::MLIR_CIR_FLAT)); if (!UseCIR && EmitsCIR) - llvm::report_fatal_error( - "-emit-cir and -emit-cir-only only valid when using -fclangir"); + llvm::report_fatal_error("emitting mlir only valid when using -fclangir"); if (Act == EmitMLIR && CI.getFrontendOpts().ClangIRDirectLowering && CI.getFrontendOpts().MLIRTargetDialect == frontend::MLIR_CORE) @@ -92,18 +95,13 @@ CreateFrontendBaseAction(CompilerInstance &CI) { return std::make_unique(); } #if CLANG_ENABLE_CIR - case EmitCIR: - return std::make_unique(); - case EmitCIRFlat: - return std::make_unique(); case EmitCIROnly: return std::make_unique(); case EmitMLIR: return std::make_unique(); #else - case EmitCIR: - case EmitCIRFlat: case EmitCIROnly: + case EmitMLIR: llvm_unreachable("CIR suppport not built into clang"); #endif case EmitHTML: return std::make_unique(); diff --git a/clang/test/CIR/emit-mlir.c b/clang/test/CIR/emit-mlir.c index 0fd7b6442727..fc7b04809352 100644 --- a/clang/test/CIR/emit-mlir.c +++ b/clang/test/CIR/emit-mlir.c @@ -1,5 +1,5 @@ -// RUN: %clang -fclangir -emit-mlir %s -o - | FileCheck %s -check-prefix=LLVM -// RUN: %clang -fno-clangir-direct-lowering -emit-mlir %s -o - | FileCheck %s -check-prefix=CORE +// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -emit-mlir %s -o - | FileCheck %s -check-prefix=LLVM +// RUN: %clang -target x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir %s -o - | FileCheck %s -check-prefix=CORE // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=llvm %s -o - | FileCheck %s -check-prefix=LLVM // RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=core %s -o - 2>&1 | FileCheck %s -check-prefix=CORE_ERR @@ -10,16 +10,16 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=cir %s -o - | FileCheck %s -check-prefix=CIR // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=cir-flat %s -o - | FileCheck %s -check-prefix=CIR_FLAT -// RUN: %clang -fclangir -emit-mlir %s -o - | FileCheck %s -check-prefix=LLVM -// RUN: %clang -fno-clangir-direct-lowering -emit-mlir %s -o - | FileCheck %s -check-prefix=CORE +// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -emit-mlir %s -o - | FileCheck %s -check-prefix=LLVM +// RUN: %clang -target x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir %s -o - | FileCheck %s -check-prefix=CORE -// RUN: %clang -fclangir -emit-mlir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_LLVM -// RUN: %clang -fno-clangir-direct-lowering -emit-mlir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CORE +// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -emit-mlir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_LLVM +// RUN: %clang -target x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CORE -// RUN: %clang -fclangir -emit-mlir=llvm %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_LLVM -// RUN: %clang -fno-clangir-direct-lowering -emit-mlir=core %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CORE -// RUN: %clang -fclangir -emit-mlir=cir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CIR -// RUN: %clang -fclangir -emit-mlir=cir-flat %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CIR_FLAT +// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -emit-mlir=llvm %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_LLVM +// RUN: %clang -target x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir=core %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CORE +// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -emit-mlir=cir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CIR +// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -emit-mlir=cir-flat %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_CIR_FLAT int foo(int a, int b) { int c;