From ce7c2381366dc86dceda68209997c0b48de77ff8 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Tue, 19 Mar 2024 13:44:30 +0300 Subject: [PATCH 01/16] [CIR] A skeleton for the structured cfg pass --- clang/include/clang/CIR/Dialect/Passes.h | 1 + clang/include/clang/CIR/Dialect/Passes.td | 14 ++++++ clang/lib/CIR/CodeGen/CIRPasses.cpp | 1 + .../lib/CIR/Dialect/Transforms/CMakeLists.txt | 1 + .../CIR/Dialect/Transforms/StructuredCFG.cpp | 50 +++++++++++++++++++ 5 files changed, 67 insertions(+) create mode 100644 clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp diff --git a/clang/include/clang/CIR/Dialect/Passes.h b/clang/include/clang/CIR/Dialect/Passes.h index a685ab8ce3fa..99e4317b97c6 100644 --- a/clang/include/clang/CIR/Dialect/Passes.h +++ b/clang/include/clang/CIR/Dialect/Passes.h @@ -34,6 +34,7 @@ std::unique_ptr createIdiomRecognizerPass(); std::unique_ptr createIdiomRecognizerPass(clang::ASTContext *astCtx); std::unique_ptr createLibOptPass(); std::unique_ptr createLibOptPass(clang::ASTContext *astCtx); +std::unique_ptr createStructuredCFGPass(); //===----------------------------------------------------------------------===// // Registration diff --git a/clang/include/clang/CIR/Dialect/Passes.td b/clang/include/clang/CIR/Dialect/Passes.td index affc28b85003..203c194b64e5 100644 --- a/clang/include/clang/CIR/Dialect/Passes.td +++ b/clang/include/clang/CIR/Dialect/Passes.td @@ -75,6 +75,20 @@ def LoweringPrepare : Pass<"cir-lowering-prepare"> { let dependentDialects = ["cir::CIRDialect"]; } +def StructuredCFG : Pass<"cir-structured-cfg"> { + let summary = "Produces structured cfg"; + let description = [{ + This pass transforms CIR and inline all the nested regions. Thus, + the next post condtions are met after the pass applied: + - there are not any nested region in a function body + - all the blocks in a function belong to the parent region + In other words, this pass removes such CIR operations like IfOp, LoopOp, + ScopeOp and etc. and produce flat CIR. + }]; + let constructor = "mlir::createStructuredCFGPass()"; + let dependentDialects = ["cir::CIRDialect"]; +} + def IdiomRecognizer : Pass<"cir-idiom-recognizer"> { let summary = "Raise calls to C/C++ libraries to CIR operations"; let description = [{ diff --git a/clang/lib/CIR/CodeGen/CIRPasses.cpp b/clang/lib/CIR/CodeGen/CIRPasses.cpp index e214df3f555b..c1901c4c71cc 100644 --- a/clang/lib/CIR/CodeGen/CIRPasses.cpp +++ b/clang/lib/CIR/CodeGen/CIRPasses.cpp @@ -55,6 +55,7 @@ mlir::LogicalResult runCIRToCIRPasses( } pm.addPass(mlir::createLoweringPreparePass(&astCtx)); + //pm.addPass(mlir::createStructuredCFGPass()); // FIXME: once CIRCodenAction fixes emission other than CIR we // need to run this right before dialect emission. diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt index a1ff2fc7d119..97e7f400b830 100644 --- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt +++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt @@ -6,6 +6,7 @@ add_clang_library(MLIRCIRTransforms IdiomRecognizer.cpp LibOpt.cpp StdHelpers.cpp + StructuredCFG.cpp DEPENDS MLIRCIRPassIncGen diff --git a/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp b/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp new file mode 100644 index 000000000000..233bdf16bd95 --- /dev/null +++ b/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp @@ -0,0 +1,50 @@ +#include "PassDetail.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" +#include "mlir/IR/PatternMatch.h" +#include "mlir/Support/LogicalResult.h" +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" +#include "mlir/Transforms/DialectConversion.h" +#include "clang/CIR/Dialect/IR/CIRDialect.h" +#include "clang/CIR/Dialect/Passes.h" + + +using namespace mlir; +using namespace mlir::cir; + +namespace { + +struct StructuredCFGPass : public StructuredCFGBase { + + StructuredCFGPass() = default; + void runOnOperation() override; +}; + +void populateStructuredCFGPatterns(RewritePatternSet &patterns) { + //TODO: add patterns here +} + +void StructuredCFGPass::runOnOperation() { + RewritePatternSet patterns(&getContext()); + populateStructuredCFGPatterns(patterns); + + // Collect operations to apply patterns. + SmallVector ops; + getOperation()->walk([&](Operation *op) { + //TODO: push back operations here + }); + + // Apply patterns. + if (applyOpPatternsAndFold(ops, std::move(patterns)).failed()) + signalPassFailure(); +} + +} // namespace + + +namespace mlir { + +std::unique_ptr createStructuredCFGPass() { + return std::make_unique(); +} + +} From 9d4c51df4c00dc1a3b5cc3fb1b5cab7e414ad1e1 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Tue, 19 Mar 2024 13:54:49 +0300 Subject: [PATCH 02/16] adds the pass right after the lowering prepare --- clang/lib/CIR/CodeGen/CIRPasses.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRPasses.cpp b/clang/lib/CIR/CodeGen/CIRPasses.cpp index c1901c4c71cc..efebfbce6837 100644 --- a/clang/lib/CIR/CodeGen/CIRPasses.cpp +++ b/clang/lib/CIR/CodeGen/CIRPasses.cpp @@ -55,7 +55,7 @@ mlir::LogicalResult runCIRToCIRPasses( } pm.addPass(mlir::createLoweringPreparePass(&astCtx)); - //pm.addPass(mlir::createStructuredCFGPass()); + pm.addPass(mlir::createStructuredCFGPass()); // FIXME: once CIRCodenAction fixes emission other than CIR we // need to run this right before dialect emission. From 92b55868a59b80774dd16c3eef8376e0082e224e Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 20 Mar 2024 14:11:36 +0300 Subject: [PATCH 03/16] Refactoring And Unifrom flat cir passes --- clang/include/clang/CIR/CIRToCIRPasses.h | 4 ++++ clang/include/clang/CIR/Dialect/Passes.h | 2 ++ clang/include/clang/CIR/Passes.h | 4 ++++ clang/include/clang/CIRFrontendAction/CIRGenAction.h | 1 + clang/lib/CIR/CodeGen/CIRPasses.cpp | 11 +++++++++-- clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp | 7 +++++++ clang/lib/CIR/Dialect/Transforms/CMakeLists.txt | 1 + clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 11 +++++++++++ clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt | 1 + clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 11 ++++++++--- clang/tools/cir-opt/cir-opt.cpp | 7 +++++-- 11 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp diff --git a/clang/include/clang/CIR/CIRToCIRPasses.h b/clang/include/clang/CIR/CIRToCIRPasses.h index 162846c75184..99b5b7c676ef 100644 --- a/clang/include/clang/CIR/CIRToCIRPasses.h +++ b/clang/include/clang/CIR/CIRToCIRPasses.h @@ -34,6 +34,10 @@ mlir::LogicalResult runCIRToCIRPasses( llvm::StringRef lifetimeOpts, bool enableIdiomRecognizer, llvm::StringRef idiomRecognizerOpts, bool enableLibOpt, llvm::StringRef libOptOpts, std::string &passOptParsingFailure); + +mlir::LogicalResult runCIRToFlatCIRPasses( + mlir::ModuleOp &theModule, mlir::MLIRContext *mlirCtx); + } // namespace cir #endif // CLANG_CIR_CIRTOCIRPASSES_H_ diff --git a/clang/include/clang/CIR/Dialect/Passes.h b/clang/include/clang/CIR/Dialect/Passes.h index 99e4317b97c6..9e9495fc4126 100644 --- a/clang/include/clang/CIR/Dialect/Passes.h +++ b/clang/include/clang/CIR/Dialect/Passes.h @@ -36,6 +36,8 @@ std::unique_ptr createLibOptPass(); std::unique_ptr createLibOptPass(clang::ASTContext *astCtx); std::unique_ptr createStructuredCFGPass(); +void populateCIRFlatteningPasses(mlir::OpPassManager &pm); + //===----------------------------------------------------------------------===// // Registration //===----------------------------------------------------------------------===// diff --git a/clang/include/clang/CIR/Passes.h b/clang/include/clang/CIR/Passes.h index 293af0412e6d..5497ad4c14cf 100644 --- a/clang/include/clang/CIR/Passes.h +++ b/clang/include/clang/CIR/Passes.h @@ -28,6 +28,10 @@ std::unique_ptr createConvertCIRToMLIRPass(); namespace direct { /// Create a pass that fully lowers CIR to the LLVMIR dialect. std::unique_ptr createConvertCIRToLLVMPass(); + +/// Create a pass pipeline that fully lowers CIR to the LLVMIR dialect. +void createCIRToLLVMPipeline(mlir::OpPassManager &pm); + } // namespace direct } // end namespace cir diff --git a/clang/include/clang/CIRFrontendAction/CIRGenAction.h b/clang/include/clang/CIRFrontendAction/CIRGenAction.h index d61c90573ade..0a60f1da726d 100644 --- a/clang/include/clang/CIRFrontendAction/CIRGenAction.h +++ b/clang/include/clang/CIRFrontendAction/CIRGenAction.h @@ -32,6 +32,7 @@ class CIRGenAction : public clang::ASTFrontendAction { enum class OutputType { EmitAssembly, EmitCIR, + EmitFlatCIR, EmitLLVM, EmitMLIR, EmitObj, diff --git a/clang/lib/CIR/CodeGen/CIRPasses.cpp b/clang/lib/CIR/CodeGen/CIRPasses.cpp index efebfbce6837..f579f4e2c611 100644 --- a/clang/lib/CIR/CodeGen/CIRPasses.cpp +++ b/clang/lib/CIR/CodeGen/CIRPasses.cpp @@ -23,7 +23,7 @@ mlir::LogicalResult runCIRToCIRPasses( clang::ASTContext &astCtx, bool enableVerifier, bool enableLifetime, llvm::StringRef lifetimeOpts, bool enableIdiomRecognizer, llvm::StringRef idiomRecognizerOpts, bool enableLibOpt, - llvm::StringRef libOptOpts, std::string &passOptParsingFailure) { + llvm::StringRef libOptOpts, std::string &passOptParsingFailure) { mlir::PassManager pm(mlirCtx); pm.addPass(mlir::createMergeCleanupsPass()); @@ -55,7 +55,6 @@ mlir::LogicalResult runCIRToCIRPasses( } pm.addPass(mlir::createLoweringPreparePass(&astCtx)); - pm.addPass(mlir::createStructuredCFGPass()); // FIXME: once CIRCodenAction fixes emission other than CIR we // need to run this right before dialect emission. @@ -64,4 +63,12 @@ mlir::LogicalResult runCIRToCIRPasses( (void)mlir::applyPassManagerCLOptions(pm); return pm.run(theModule); } + +mlir::LogicalResult runCIRToFlatCIRPasses( + mlir::ModuleOp &theModule, mlir::MLIRContext *mlirCtx) { + mlir::PassManager pm(mlirCtx); + mlir::populateCIRFlatteningPasses(pm); + pm.addPass(mlir::createStructuredCFGPass()); + return pm.run(theModule); +} } // namespace cir diff --git a/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp b/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp new file mode 100644 index 000000000000..ba3bc59c50dd --- /dev/null +++ b/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp @@ -0,0 +1,7 @@ +#include "clang/CIR/Dialect/Passes.h" +#include "mlir/Pass/PassManager.h" + + +void mlir::populateCIRFlatteningPasses(mlir::OpPassManager &pm) { + pm.addPass(mlir::createStructuredCFGPass()); +} \ No newline at end of file diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt index 97e7f400b830..b450fab9c61f 100644 --- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt +++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt @@ -1,4 +1,5 @@ add_clang_library(MLIRCIRTransforms + CIRFlattening.cpp LifetimeCheck.cpp LoweringPrepare.cpp MergeCleanups.cpp diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index 657f954d27e7..3f9b9b34a328 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -244,6 +244,15 @@ class CIRGenConsumer : public clang::ASTConsumer { mlirMod->print(*outputStream, flags); } break; + case CIRGenAction::OutputType::EmitFlatCIR: { + auto flatCIRModule = runCIRToFlatCIRPasses(mlirMod, mlirCtx.get()); + + // FIXME: we cannot roundtrip prettyForm=true right now. + mlir::OpPrintingFlags flags; + flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); + mlirMod->print(*outputStream, flags); + } + break; case CIRGenAction::OutputType::EmitMLIR: { auto loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get()); assert(outputStream && "Why are we here without an output stream?"); @@ -353,6 +362,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::EmitFlatCIR: + return ci.createDefaultOutputFile(false, inFile, "cir"); case CIRGenAction::OutputType::EmitMLIR: return ci.createDefaultOutputFile(false, inFile, "mlir"); case CIRGenAction::OutputType::EmitLLVM: diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt index 7cf80d0b0a0e..edabbaabec13 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt +++ b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt @@ -29,6 +29,7 @@ add_clang_library(clangCIRLoweringDirectToLLVM ${dialect_libs} MLIRCIR MLIRAnalysis + MLIRCIRTransforms MLIRIR MLIRParser MLIRSideEffectInterfaces diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index e249c59c964f..b30deae018d1 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -50,6 +50,7 @@ #include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/Dialect/IR/CIROpsEnums.h" #include "clang/CIR/Dialect/IR/CIRTypes.h" +#include "clang/CIR/Dialect/Passes.h" #include "clang/CIR/Passes.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/ArrayRef.h" @@ -916,7 +917,7 @@ struct ConvertCIRToLLVMPass } void runOnOperation() final; - virtual StringRef getArgument() const override { return "cir-to-llvm"; } + virtual StringRef getArgument() const override { return "cir-to-llvm-internal"; } }; class CIRCallLowering : public mlir::OpConversionPattern { @@ -2968,6 +2969,11 @@ std::unique_ptr createConvertCIRToLLVMPass() { return std::make_unique(); } +void createCIRToLLVMPipeline(mlir::OpPassManager &pm) { + populateCIRFlatteningPasses(pm); + pm.addPass(createConvertCIRToLLVMPass()); +} + extern void registerCIRDialectTranslation(mlir::MLIRContext &context); std::unique_ptr @@ -2975,8 +2981,7 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx, bool disableVerifier) { mlir::MLIRContext *mlirCtx = theModule.getContext(); mlir::PassManager pm(mlirCtx); - - pm.addPass(createConvertCIRToLLVMPass()); + createCIRToLLVMPipeline(pm); // This is necessary to have line tables emitted and basic // debugger working. In the future we will add proper debug information diff --git a/clang/tools/cir-opt/cir-opt.cpp b/clang/tools/cir-opt/cir-opt.cpp index deee67afa8a4..150db6d4c468 100644 --- a/clang/tools/cir-opt/cir-opt.cpp +++ b/clang/tools/cir-opt/cir-opt.cpp @@ -21,6 +21,7 @@ #include "mlir/Dialect/OpenMP/OpenMPDialect.h" #include "mlir/InitAllPasses.h" #include "mlir/Pass/PassRegistry.h" +#include "mlir/Pass/PassManager.h" #include "mlir/Tools/mlir-opt/MlirOptMain.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/Dialect/Passes.h" @@ -45,8 +46,10 @@ int main(int argc, char **argv) { return cir::createConvertCIRToMLIRPass(); }); - ::mlir::registerPass([]() -> std::unique_ptr<::mlir::Pass> { - return cir::direct::createConvertCIRToLLVMPass(); + mlir::PassPipelineRegistration pipeline( + "cir-to-llvm", "", + [](mlir::OpPassManager &pm) { + cir::direct::createCIRToLLVMPipeline(pm); }); ::mlir::registerPass([]() -> std::unique_ptr<::mlir::Pass> { From 35652725162a5e54aff554482256c885c3148e79 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 20 Mar 2024 15:54:17 +0300 Subject: [PATCH 04/16] emit-flat-cir option --- clang/include/clang/CIRFrontendAction/CIRGenAction.h | 7 +++++++ clang/include/clang/Driver/Options.td | 2 ++ clang/include/clang/Driver/Types.def | 1 + clang/include/clang/Frontend/FrontendOptions.h | 3 +++ clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 8 ++++++-- clang/lib/Driver/Driver.cpp | 6 ++++-- clang/lib/Driver/ToolChains/Clang.cpp | 5 ++++- clang/lib/Frontend/CompilerInvocation.cpp | 6 +++++- clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp | 4 +++- 9 files changed, 35 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/CIRFrontendAction/CIRGenAction.h b/clang/include/clang/CIRFrontendAction/CIRGenAction.h index 0a60f1da726d..9f9be76ab53d 100644 --- a/clang/include/clang/CIRFrontendAction/CIRGenAction.h +++ b/clang/include/clang/CIRFrontendAction/CIRGenAction.h @@ -78,6 +78,13 @@ class EmitCIRAction : public CIRGenAction { EmitCIRAction(mlir::MLIRContext *mlirCtx = nullptr); }; +class EmitFlatCIRAction : public CIRGenAction { + virtual void anchor(); + +public: + EmitFlatCIRAction(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 5f3120105940..50796f5fc138 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2926,6 +2926,8 @@ defm clangir_direct_lowering : BoolFOption<"clangir-direct-lowering", def emit_cir : Flag<["-"], "emit-cir">, Visibility<[ClangOption, CC1Option]>, Group, HelpText<"Build ASTs and then lower to ClangIR, emit the .cir file">; +def emit_flat_cir : Flag<["-"], "emit-flat-cir">, Visibility<[ClangOption, CC1Option]>, + Group, HelpText<"Build ASTs and then lower to ClangIR with no nested regions, then emit the .cir file">; def emit_mlir : Flag<["-"], "emit-mlir">, Visibility<[CC1Option]>, Group, HelpText<"Build ASTs and then lower through ClangIR to MLIR, emit the .milr file">; /// ClangIR-specific options - END diff --git a/clang/include/clang/Driver/Types.def b/clang/include/clang/Driver/Types.def index 9d59f85adbd6..8fd83d14caac 100644 --- a/clang/include/clang/Driver/Types.def +++ b/clang/include/clang/Driver/Types.def @@ -91,6 +91,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("flat-cir", FLAT_CIR, 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 857446e6dc0c..07b1c7c27d4b 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -67,6 +67,9 @@ enum ActionKind { /// Emit a .cir file EmitCIR, + /// Emit a .cir file with flat ClangIR + EmitFlatCIR, + /// Generate CIR, bud don't emit anything. EmitCIROnly, diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index 3f9b9b34a328..2cdde7877deb 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -251,8 +251,8 @@ class CIRGenConsumer : public clang::ASTConsumer { mlir::OpPrintingFlags flags; flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); mlirMod->print(*outputStream, flags); - } - break; + break; + } case CIRGenAction::OutputType::EmitMLIR: { auto loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get()); assert(outputStream && "Why are we here without an output stream?"); @@ -457,6 +457,10 @@ void EmitCIRAction::anchor() {} EmitCIRAction::EmitCIRAction(mlir::MLIRContext *_MLIRContext) : CIRGenAction(OutputType::EmitCIR, _MLIRContext) {} +void EmitFlatCIRAction::anchor() {} +EmitFlatCIRAction::EmitFlatCIRAction(mlir::MLIRContext *_MLIRContext) + : CIRGenAction(OutputType::EmitFlatCIR, _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 6b4cf85daf58..315dec059e45 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -361,6 +361,7 @@ phases::ID Driver::getFinalPhase(const DerivedArgList &DAL, (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_flat_cir)) || (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) { FinalPhase = phases::Compile; @@ -4785,9 +4786,10 @@ 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)) { + if (Args.hasArg(options::OPT_emit_cir)) return C.MakeAction(Input, types::TY_CIR); - } + if (Args.hasArg(options::OPT_emit_flat_cir)) + return C.MakeAction(Input, types::TY_FLAT_CIR); 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 9018735e324c..168045191f05 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -4910,7 +4910,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } if (Args.hasArg(options::OPT_fclangir_enable) || - Args.hasArg(options::OPT_emit_cir)) + Args.hasArg(options::OPT_emit_cir) || + Args.hasArg(options::OPT_emit_flat_cir)) CmdArgs.push_back("-fclangir-enable"); if (Args.hasArg(options::OPT_fclangir_direct_lowering)) @@ -5052,6 +5053,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-emit-llvm"); } else if (JA.getType() == types::TY_CIR) { CmdArgs.push_back("-emit-cir"); + } else if (JA.getType() == types::TY_FLAT_CIR) { + CmdArgs.push_back("-emit-flat-cir"); } 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 29cd10d2f100..366838d6ce49 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2552,6 +2552,7 @@ static const auto &getFrontendActionTable() { {frontend::EmitAssembly, OPT_S}, {frontend::EmitBC, OPT_emit_llvm_bc}, {frontend::EmitCIR, OPT_emit_cir}, + {frontend::EmitFlatCIR, OPT_emit_flat_cir}, {frontend::EmitCIROnly, OPT_emit_cir_only}, {frontend::EmitMLIR, OPT_emit_mlir}, {frontend::EmitHTML, OPT_emit_html}, @@ -2907,7 +2908,9 @@ 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_enable) || Args.hasArg(OPT_emit_cir)) + if (Args.hasArg(OPT_fclangir_enable) + || Args.hasArg(OPT_emit_cir) + || Args.hasArg(OPT_emit_flat_cir)) Opts.UseClangIRPipeline = true; if (Args.hasArg(OPT_fclangir_direct_lowering)) @@ -4383,6 +4386,7 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) { case frontend::EmitAssembly: case frontend::EmitBC: case frontend::EmitCIR: + case frontend::EmitFlatCIR: case frontend::EmitCIROnly: case frontend::EmitMLIR: case frontend::EmitHTML: diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp index d5fd228ee88b..82312f4e24f9 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -53,7 +53,7 @@ CreateFrontendBaseAction(CompilerInstance &CI) { auto UseCIR = CI.getFrontendOpts().UseClangIRPipeline; auto Act = CI.getFrontendOpts().ProgramAction; - auto EmitsCIR = Act == EmitCIR || Act == EmitCIROnly; + auto EmitsCIR = Act == EmitCIR || Act == EmitFlatCIR || Act == EmitCIROnly; if (!UseCIR && EmitsCIR) llvm::report_fatal_error( @@ -81,10 +81,12 @@ CreateFrontendBaseAction(CompilerInstance &CI) { case EmitBC: return std::make_unique(); #if CLANG_ENABLE_CIR case EmitCIR: return std::make_unique<::cir::EmitCIRAction>(); + case EmitFlatCIR: return std::make_unique<::cir::EmitFlatCIRAction>(); case EmitCIROnly: return std::make_unique<::cir::EmitCIROnlyAction>(); case EmitMLIR: return std::make_unique<::cir::EmitMLIRAction>(); #else case EmitCIR: + case EmitFlatCIR: case EmitCIROnly: llvm_unreachable("CIR suppport not built into clang"); #endif From 058d28df390a2fdf17250261b2d7acde01c1b427 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 20 Mar 2024 16:11:15 +0300 Subject: [PATCH 05/16] kind of test fixing --- clang/test/CIR/mlirprint.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/CIR/mlirprint.c b/clang/test/CIR/mlirprint.c index 7d98959a78f6..dd2d5d0ae473 100644 --- a/clang/test/CIR/mlirprint.c +++ b/clang/test/CIR/mlirprint.c @@ -14,7 +14,7 @@ int foo(void) { // CIR: cir.func @foo() -> !s32i // CIR: IR Dump After DropAST (cir-drop-ast) // CIR: cir.func @foo() -> !s32i -// LLVM: IR Dump After cir::direct::ConvertCIRToLLVMPass (cir-to-llvm) +// LLVM: IR Dump After cir::direct::ConvertCIRToLLVMPass (cir-to-llvm-internal) // LLVM: llvm.func @foo() -> i32 // LLVM: IR Dump After // LLVM: define i32 @foo() From 659424907261a1877bd4c392aac3bfc25e89b260 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 20 Mar 2024 16:49:45 +0300 Subject: [PATCH 06/16] silly test --- clang/test/CIR/mlirprint.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/clang/test/CIR/mlirprint.c b/clang/test/CIR/mlirprint.c index dd2d5d0ae473..ff80ebd72f32 100644 --- a/clang/test/CIR/mlirprint.c +++ b/clang/test/CIR/mlirprint.c @@ -1,4 +1,5 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -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-enable -emit-flat-cir -mmlir --mlir-print-ir-after-all %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=FLATCIR // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -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-enable -emit-cir -mmlir --mlir-print-ir-after=cir-drop-ast %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CIRPASS @@ -12,8 +13,16 @@ int foo(void) { // CIR: cir.func @foo() -> !s32i // CIR: IR Dump After LoweringPrepare (cir-lowering-prepare) // CIR: cir.func @foo() -> !s32i +// CIR-NOT: IR Dump After StructuredCFG // CIR: IR Dump After DropAST (cir-drop-ast) // CIR: cir.func @foo() -> !s32i +// FLATCIR: IR Dump After MergeCleanups (cir-merge-cleanups) +// FLATCIR: cir.func @foo() -> !s32i +// FLATCIR: IR Dump After LoweringPrepare (cir-lowering-prepare) +// FLATCIR: cir.func @foo() -> !s32i +// FLATCIR: IR Dump After StructuredCFG +// FLATCIR: IR Dump After DropAST (cir-drop-ast) +// FLATCIR: cir.func @foo() -> !s32i // LLVM: IR Dump After cir::direct::ConvertCIRToLLVMPass (cir-to-llvm-internal) // LLVM: llvm.func @foo() -> i32 // LLVM: IR Dump After From d2c66400890335fa9a7fbb266fb5829a1cb046f5 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 20 Mar 2024 16:56:17 +0300 Subject: [PATCH 07/16] refactoring --- clang/include/clang/CIR/CIRToCIRPasses.h | 6 ++---- clang/lib/CIR/CodeGen/CIRPasses.cpp | 12 ++++-------- clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp | 1 - clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 13 +++---------- 4 files changed, 9 insertions(+), 23 deletions(-) diff --git a/clang/include/clang/CIR/CIRToCIRPasses.h b/clang/include/clang/CIR/CIRToCIRPasses.h index 99b5b7c676ef..e3bdb0babbaf 100644 --- a/clang/include/clang/CIR/CIRToCIRPasses.h +++ b/clang/include/clang/CIR/CIRToCIRPasses.h @@ -33,10 +33,8 @@ mlir::LogicalResult runCIRToCIRPasses( clang::ASTContext &astCtx, bool enableVerifier, bool enableLifetime, llvm::StringRef lifetimeOpts, bool enableIdiomRecognizer, llvm::StringRef idiomRecognizerOpts, bool enableLibOpt, - llvm::StringRef libOptOpts, std::string &passOptParsingFailure); - -mlir::LogicalResult runCIRToFlatCIRPasses( - mlir::ModuleOp &theModule, mlir::MLIRContext *mlirCtx); + llvm::StringRef libOptOpts, std::string &passOptParsingFailure, + bool flattenCIR); } // namespace cir diff --git a/clang/lib/CIR/CodeGen/CIRPasses.cpp b/clang/lib/CIR/CodeGen/CIRPasses.cpp index f579f4e2c611..ec85220183bf 100644 --- a/clang/lib/CIR/CodeGen/CIRPasses.cpp +++ b/clang/lib/CIR/CodeGen/CIRPasses.cpp @@ -23,7 +23,8 @@ mlir::LogicalResult runCIRToCIRPasses( clang::ASTContext &astCtx, bool enableVerifier, bool enableLifetime, llvm::StringRef lifetimeOpts, bool enableIdiomRecognizer, llvm::StringRef idiomRecognizerOpts, bool enableLibOpt, - llvm::StringRef libOptOpts, std::string &passOptParsingFailure) { + llvm::StringRef libOptOpts, std::string &passOptParsingFailure, + bool flattenCIR) { mlir::PassManager pm(mlirCtx); pm.addPass(mlir::createMergeCleanupsPass()); @@ -55,6 +56,8 @@ mlir::LogicalResult runCIRToCIRPasses( } pm.addPass(mlir::createLoweringPreparePass(&astCtx)); + if (flattenCIR) + mlir::populateCIRFlatteningPasses(pm); // FIXME: once CIRCodenAction fixes emission other than CIR we // need to run this right before dialect emission. @@ -64,11 +67,4 @@ mlir::LogicalResult runCIRToCIRPasses( return pm.run(theModule); } -mlir::LogicalResult runCIRToFlatCIRPasses( - mlir::ModuleOp &theModule, mlir::MLIRContext *mlirCtx) { - mlir::PassManager pm(mlirCtx); - mlir::populateCIRFlatteningPasses(pm); - pm.addPass(mlir::createStructuredCFGPass()); - return pm.run(theModule); -} } // namespace cir diff --git a/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp b/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp index 233bdf16bd95..c59cf4bc2c6c 100644 --- a/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp +++ b/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp @@ -7,7 +7,6 @@ #include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/Dialect/Passes.h" - using namespace mlir; using namespace mlir::cir; diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index 2cdde7877deb..f3b32e5d8a60 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -185,7 +185,8 @@ class CIRGenConsumer : public clang::ASTConsumer { mlirMod, mlirCtx.get(), C, !feOptions.ClangIRDisableCIRVerifier, feOptions.ClangIRLifetimeCheck, lifetimeOpts, feOptions.ClangIRIdiomRecognizer, idiomRecognizerOpts, - feOptions.ClangIRLibOpt, libOptOpts, passOptParsingFailure) + feOptions.ClangIRLibOpt, libOptOpts, passOptParsingFailure, + action==CIRGenAction::OutputType::EmitFlatCIR) .failed()) { if (!passOptParsingFailure.empty()) diagnosticsEngine.Report(diag::err_drv_cir_pass_opt_parsing) @@ -233,6 +234,7 @@ class CIRGenConsumer : public clang::ASTConsumer { switch (action) { case CIRGenAction::OutputType::EmitCIR: + case CIRGenAction::OutputType::EmitFlatCIR: if (outputStream && mlirMod) { // Emit remaining defaulted C++ methods if (!feOptions.ClangIRDisableEmitCXXDefault) @@ -244,15 +246,6 @@ class CIRGenConsumer : public clang::ASTConsumer { mlirMod->print(*outputStream, flags); } break; - case CIRGenAction::OutputType::EmitFlatCIR: { - auto flatCIRModule = runCIRToFlatCIRPasses(mlirMod, mlirCtx.get()); - - // FIXME: we cannot roundtrip prettyForm=true right now. - mlir::OpPrintingFlags flags; - flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false); - mlirMod->print(*outputStream, flags); - break; - } case CIRGenAction::OutputType::EmitMLIR: { auto loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get()); assert(outputStream && "Why are we here without an output stream?"); From d97f9a1d8652c018b3ab936958af1ee052b4367c Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 20 Mar 2024 17:08:43 +0300 Subject: [PATCH 08/16] refactoring --- clang/include/clang/CIR/Dialect/Passes.td | 4 ++-- clang/include/clang/CIR/Passes.h | 4 ++-- clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 4 ++-- clang/tools/cir-opt/cir-opt.cpp | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/Passes.td b/clang/include/clang/CIR/Dialect/Passes.td index 203c194b64e5..1a144795f3ee 100644 --- a/clang/include/clang/CIR/Dialect/Passes.td +++ b/clang/include/clang/CIR/Dialect/Passes.td @@ -80,10 +80,10 @@ def StructuredCFG : Pass<"cir-structured-cfg"> { let description = [{ This pass transforms CIR and inline all the nested regions. Thus, the next post condtions are met after the pass applied: - - there are not any nested region in a function body + - there is not any nested region in a function body - all the blocks in a function belong to the parent region In other words, this pass removes such CIR operations like IfOp, LoopOp, - ScopeOp and etc. and produce flat CIR. + ScopeOp and etc. and produces a flat CIR. }]; let constructor = "mlir::createStructuredCFGPass()"; let dependentDialects = ["cir::CIRDialect"]; diff --git a/clang/include/clang/CIR/Passes.h b/clang/include/clang/CIR/Passes.h index 5497ad4c14cf..6b1d2fdc75c4 100644 --- a/clang/include/clang/CIR/Passes.h +++ b/clang/include/clang/CIR/Passes.h @@ -29,8 +29,8 @@ namespace direct { /// Create a pass that fully lowers CIR to the LLVMIR dialect. std::unique_ptr createConvertCIRToLLVMPass(); -/// Create a pass pipeline that fully lowers CIR to the LLVMIR dialect. -void createCIRToLLVMPipeline(mlir::OpPassManager &pm); +/// Adds passes that fully lower CIR to the LLVMIR dialect. +void populateCIRToLLVMPasses(mlir::OpPassManager &pm); } // namespace direct } // end namespace cir diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index b30deae018d1..c81969eb512a 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -2969,7 +2969,7 @@ std::unique_ptr createConvertCIRToLLVMPass() { return std::make_unique(); } -void createCIRToLLVMPipeline(mlir::OpPassManager &pm) { +void populateCIRToLLVMPasses(mlir::OpPassManager &pm) { populateCIRFlatteningPasses(pm); pm.addPass(createConvertCIRToLLVMPass()); } @@ -2981,7 +2981,7 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx, bool disableVerifier) { mlir::MLIRContext *mlirCtx = theModule.getContext(); mlir::PassManager pm(mlirCtx); - createCIRToLLVMPipeline(pm); + populateCIRToLLVMPasses(pm); // This is necessary to have line tables emitted and basic // debugger working. In the future we will add proper debug information diff --git a/clang/tools/cir-opt/cir-opt.cpp b/clang/tools/cir-opt/cir-opt.cpp index 150db6d4c468..87e4d7a52c3c 100644 --- a/clang/tools/cir-opt/cir-opt.cpp +++ b/clang/tools/cir-opt/cir-opt.cpp @@ -49,7 +49,7 @@ int main(int argc, char **argv) { mlir::PassPipelineRegistration pipeline( "cir-to-llvm", "", [](mlir::OpPassManager &pm) { - cir::direct::createCIRToLLVMPipeline(pm); + cir::direct::populateCIRToLLVMPasses(pm); }); ::mlir::registerPass([]() -> std::unique_ptr<::mlir::Pass> { From 3fa07d7ff20c75e5007cbe11b7bad1761b07b2b1 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 20 Mar 2024 17:34:39 +0300 Subject: [PATCH 09/16] test updated --- clang/test/CIR/mlirprint.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/clang/test/CIR/mlirprint.c b/clang/test/CIR/mlirprint.c index ff80ebd72f32..304883cd2f24 100644 --- a/clang/test/CIR/mlirprint.c +++ b/clang/test/CIR/mlirprint.c @@ -2,6 +2,7 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-flat-cir -mmlir --mlir-print-ir-after-all %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=FLATCIR // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -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-enable -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-enable -emit-flat-cir -mmlir --mlir-print-ir-before=cir-structured-cfg %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CFGPASS int foo(void) { int i = 3; @@ -13,7 +14,7 @@ int foo(void) { // CIR: cir.func @foo() -> !s32i // CIR: IR Dump After LoweringPrepare (cir-lowering-prepare) // CIR: cir.func @foo() -> !s32i -// CIR-NOT: IR Dump After StructuredCFG +// CIR-NOT: IR Dump After StructuredCFG (cir-structured-cfg) // CIR: IR Dump After DropAST (cir-drop-ast) // CIR: cir.func @foo() -> !s32i // FLATCIR: IR Dump After MergeCleanups (cir-merge-cleanups) @@ -30,3 +31,5 @@ int foo(void) { // CIRPASS-NOT: IR Dump After MergeCleanups // CIRPASS: IR Dump After DropAST + +// CFGPASS: IR Dump Before StructuredCFG From 8bd1e7bc4621b6f4dbc4318b98381334a434d814 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 20 Mar 2024 17:37:32 +0300 Subject: [PATCH 10/16] clang-format ... --- clang/include/clang/CIR/CIRToCIRPasses.h | 15 ++++++++------- clang/lib/CIR/CodeGen/CIRPasses.cpp | 15 ++++++++------- .../CIR/Dialect/Transforms/CIRFlattening.cpp | 5 ++--- .../CIR/Dialect/Transforms/StructuredCFG.cpp | 17 ++++++++--------- clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 2 +- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 6 ++++-- clang/lib/Frontend/CompilerInvocation.cpp | 5 ++--- .../FrontendTool/ExecuteCompilerInvocation.cpp | 3 ++- clang/tools/cir-opt/cir-opt.cpp | 7 +++---- 9 files changed, 38 insertions(+), 37 deletions(-) diff --git a/clang/include/clang/CIR/CIRToCIRPasses.h b/clang/include/clang/CIR/CIRToCIRPasses.h index e3bdb0babbaf..ed089cd966f4 100644 --- a/clang/include/clang/CIR/CIRToCIRPasses.h +++ b/clang/include/clang/CIR/CIRToCIRPasses.h @@ -28,13 +28,14 @@ class ModuleOp; namespace cir { // Run set of cleanup/prepare/etc passes CIR <-> CIR. -mlir::LogicalResult runCIRToCIRPasses( - mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx, - clang::ASTContext &astCtx, bool enableVerifier, bool enableLifetime, - llvm::StringRef lifetimeOpts, bool enableIdiomRecognizer, - llvm::StringRef idiomRecognizerOpts, bool enableLibOpt, - llvm::StringRef libOptOpts, std::string &passOptParsingFailure, - bool flattenCIR); +mlir::LogicalResult +runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx, + clang::ASTContext &astCtx, bool enableVerifier, + bool enableLifetime, llvm::StringRef lifetimeOpts, + bool enableIdiomRecognizer, + llvm::StringRef idiomRecognizerOpts, bool enableLibOpt, + llvm::StringRef libOptOpts, + std::string &passOptParsingFailure, bool flattenCIR); } // namespace cir diff --git a/clang/lib/CIR/CodeGen/CIRPasses.cpp b/clang/lib/CIR/CodeGen/CIRPasses.cpp index ec85220183bf..380ff3b677f4 100644 --- a/clang/lib/CIR/CodeGen/CIRPasses.cpp +++ b/clang/lib/CIR/CodeGen/CIRPasses.cpp @@ -18,13 +18,14 @@ #include "mlir/Pass/PassManager.h" namespace cir { -mlir::LogicalResult runCIRToCIRPasses( - mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx, - clang::ASTContext &astCtx, bool enableVerifier, bool enableLifetime, - llvm::StringRef lifetimeOpts, bool enableIdiomRecognizer, - llvm::StringRef idiomRecognizerOpts, bool enableLibOpt, - llvm::StringRef libOptOpts, std::string &passOptParsingFailure, - bool flattenCIR) { +mlir::LogicalResult +runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx, + clang::ASTContext &astCtx, bool enableVerifier, + bool enableLifetime, llvm::StringRef lifetimeOpts, + bool enableIdiomRecognizer, + llvm::StringRef idiomRecognizerOpts, bool enableLibOpt, + llvm::StringRef libOptOpts, + std::string &passOptParsingFailure, bool flattenCIR) { mlir::PassManager pm(mlirCtx); pm.addPass(mlir::createMergeCleanupsPass()); diff --git a/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp b/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp index ba3bc59c50dd..38b3586f29af 100644 --- a/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp +++ b/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp @@ -1,7 +1,6 @@ -#include "clang/CIR/Dialect/Passes.h" #include "mlir/Pass/PassManager.h" - +#include "clang/CIR/Dialect/Passes.h" void mlir::populateCIRFlatteningPasses(mlir::OpPassManager &pm) { - pm.addPass(mlir::createStructuredCFGPass()); + pm.addPass(mlir::createStructuredCFGPass()); } \ No newline at end of file diff --git a/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp b/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp index c59cf4bc2c6c..c7d957b01dcf 100644 --- a/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp +++ b/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp @@ -2,8 +2,8 @@ #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/IR/PatternMatch.h" #include "mlir/Support/LogicalResult.h" -#include "mlir/Transforms/GreedyPatternRewriteDriver.h" #include "mlir/Transforms/DialectConversion.h" +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/Dialect/Passes.h" @@ -13,25 +13,25 @@ using namespace mlir::cir; namespace { struct StructuredCFGPass : public StructuredCFGBase { - + StructuredCFGPass() = default; void runOnOperation() override; }; -void populateStructuredCFGPatterns(RewritePatternSet &patterns) { - //TODO: add patterns here +void populateStructuredCFGPatterns(RewritePatternSet &patterns) { + // TODO: add patterns here } void StructuredCFGPass::runOnOperation() { RewritePatternSet patterns(&getContext()); populateStructuredCFGPatterns(patterns); - // Collect operations to apply patterns. + // Collect operations to apply patterns. SmallVector ops; getOperation()->walk([&](Operation *op) { - //TODO: push back operations here + // TODO: push back operations here }); - + // Apply patterns. if (applyOpPatternsAndFold(ops, std::move(patterns)).failed()) signalPassFailure(); @@ -39,11 +39,10 @@ void StructuredCFGPass::runOnOperation() { } // namespace - namespace mlir { std::unique_ptr createStructuredCFGPass() { return std::make_unique(); } -} +} // namespace mlir diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index f3b32e5d8a60..f49637b28e5e 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -186,7 +186,7 @@ class CIRGenConsumer : public clang::ASTConsumer { feOptions.ClangIRLifetimeCheck, lifetimeOpts, feOptions.ClangIRIdiomRecognizer, idiomRecognizerOpts, feOptions.ClangIRLibOpt, libOptOpts, passOptParsingFailure, - action==CIRGenAction::OutputType::EmitFlatCIR) + action == CIRGenAction::OutputType::EmitFlatCIR) .failed()) { if (!passOptParsingFailure.empty()) diagnosticsEngine.Report(diag::err_drv_cir_pass_opt_parsing) diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index c81969eb512a..170eb5afc6c6 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -917,7 +917,9 @@ struct ConvertCIRToLLVMPass } void runOnOperation() final; - virtual StringRef getArgument() const override { return "cir-to-llvm-internal"; } + virtual StringRef getArgument() const override { + return "cir-to-llvm-internal"; + } }; class CIRCallLowering : public mlir::OpConversionPattern { @@ -2981,7 +2983,7 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx, bool disableVerifier) { mlir::MLIRContext *mlirCtx = theModule.getContext(); mlir::PassManager pm(mlirCtx); - populateCIRToLLVMPasses(pm); + populateCIRToLLVMPasses(pm); // This is necessary to have line tables emitted and basic // debugger working. In the future we will add proper debug information diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 366838d6ce49..6f375308ba2c 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2908,9 +2908,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_enable) - || Args.hasArg(OPT_emit_cir) - || Args.hasArg(OPT_emit_flat_cir)) + if (Args.hasArg(OPT_fclangir_enable) || Args.hasArg(OPT_emit_cir) || + Args.hasArg(OPT_emit_flat_cir)) 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 82312f4e24f9..4adefac8759c 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -81,7 +81,8 @@ CreateFrontendBaseAction(CompilerInstance &CI) { case EmitBC: return std::make_unique(); #if CLANG_ENABLE_CIR case EmitCIR: return std::make_unique<::cir::EmitCIRAction>(); - case EmitFlatCIR: return std::make_unique<::cir::EmitFlatCIRAction>(); + case EmitFlatCIR: + return std::make_unique<::cir::EmitFlatCIRAction>(); case EmitCIROnly: return std::make_unique<::cir::EmitCIROnlyAction>(); case EmitMLIR: return std::make_unique<::cir::EmitMLIRAction>(); #else diff --git a/clang/tools/cir-opt/cir-opt.cpp b/clang/tools/cir-opt/cir-opt.cpp index 87e4d7a52c3c..8e2044ecdc9e 100644 --- a/clang/tools/cir-opt/cir-opt.cpp +++ b/clang/tools/cir-opt/cir-opt.cpp @@ -20,8 +20,8 @@ #include "mlir/Dialect/MemRef/IR/MemRef.h" #include "mlir/Dialect/OpenMP/OpenMPDialect.h" #include "mlir/InitAllPasses.h" -#include "mlir/Pass/PassRegistry.h" #include "mlir/Pass/PassManager.h" +#include "mlir/Pass/PassRegistry.h" #include "mlir/Tools/mlir-opt/MlirOptMain.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/Dialect/Passes.h" @@ -47,10 +47,9 @@ int main(int argc, char **argv) { }); mlir::PassPipelineRegistration pipeline( - "cir-to-llvm", "", - [](mlir::OpPassManager &pm) { + "cir-to-llvm", "", [](mlir::OpPassManager &pm) { cir::direct::populateCIRToLLVMPasses(pm); - }); + }); ::mlir::registerPass([]() -> std::unique_ptr<::mlir::Pass> { return mlir::createReconcileUnrealizedCastsPass(); From 5d4c149a02c5aa03de607471e622da6a00af5bcf Mon Sep 17 00:00:00 2001 From: gitoleg Date: Thu, 21 Mar 2024 12:00:11 +0300 Subject: [PATCH 11/16] minor fix --- clang/test/CIR/mlirprint.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/test/CIR/mlirprint.c b/clang/test/CIR/mlirprint.c index 304883cd2f24..5b7605141d22 100644 --- a/clang/test/CIR/mlirprint.c +++ b/clang/test/CIR/mlirprint.c @@ -14,14 +14,14 @@ int foo(void) { // CIR: cir.func @foo() -> !s32i // CIR: IR Dump After LoweringPrepare (cir-lowering-prepare) // CIR: cir.func @foo() -> !s32i -// CIR-NOT: IR Dump After StructuredCFG (cir-structured-cfg) +// CIR-NOT: IR Dump After StructuredCFG // CIR: IR Dump After DropAST (cir-drop-ast) // CIR: cir.func @foo() -> !s32i // FLATCIR: IR Dump After MergeCleanups (cir-merge-cleanups) // FLATCIR: cir.func @foo() -> !s32i // FLATCIR: IR Dump After LoweringPrepare (cir-lowering-prepare) // FLATCIR: cir.func @foo() -> !s32i -// FLATCIR: IR Dump After StructuredCFG +// FLATCIR: IR Dump After StructuredCFG (cir-structured-cfg) // FLATCIR: IR Dump After DropAST (cir-drop-ast) // FLATCIR: cir.func @foo() -> !s32i // LLVM: IR Dump After cir::direct::ConvertCIRToLLVMPass (cir-to-llvm-internal) @@ -32,4 +32,4 @@ int foo(void) { // CIRPASS-NOT: IR Dump After MergeCleanups // CIRPASS: IR Dump After DropAST -// CFGPASS: IR Dump Before StructuredCFG +// CFGPASS: IR Dump Before StructuredCFG (cir-structured-cfg) From 1714c8abb81dd6dc0d1affb352e3a03287732c2f Mon Sep 17 00:00:00 2001 From: gitoleg Date: Fri, 22 Mar 2024 15:24:33 +0300 Subject: [PATCH 12/16] flat cir -> cir flat --- .../clang/CIRFrontendAction/CIRGenAction.h | 6 +++--- clang/include/clang/Driver/Options.td | 4 ++-- clang/include/clang/Driver/Types.def | 2 +- clang/include/clang/Frontend/FrontendOptions.h | 2 +- clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 12 ++++++------ clang/lib/Driver/Driver.cpp | 6 +++--- clang/lib/Driver/ToolChains/Clang.cpp | 4 ++-- clang/lib/Frontend/CompilerInvocation.cpp | 6 +++--- .../FrontendTool/ExecuteCompilerInvocation.cpp | 8 ++++---- clang/test/CIR/mlirprint.c | 18 +++++++++--------- 10 files changed, 34 insertions(+), 34 deletions(-) diff --git a/clang/include/clang/CIRFrontendAction/CIRGenAction.h b/clang/include/clang/CIRFrontendAction/CIRGenAction.h index 9f9be76ab53d..74d5e5e32611 100644 --- a/clang/include/clang/CIRFrontendAction/CIRGenAction.h +++ b/clang/include/clang/CIRFrontendAction/CIRGenAction.h @@ -32,7 +32,7 @@ class CIRGenAction : public clang::ASTFrontendAction { enum class OutputType { EmitAssembly, EmitCIR, - EmitFlatCIR, + EmitCIRFlat, EmitLLVM, EmitMLIR, EmitObj, @@ -78,11 +78,11 @@ class EmitCIRAction : public CIRGenAction { EmitCIRAction(mlir::MLIRContext *mlirCtx = nullptr); }; -class EmitFlatCIRAction : public CIRGenAction { +class EmitCIRFlatAction : public CIRGenAction { virtual void anchor(); public: - EmitFlatCIRAction(mlir::MLIRContext *mlirCtx = nullptr); + EmitCIRFlatAction(mlir::MLIRContext *mlirCtx = nullptr); }; class EmitCIROnlyAction : public CIRGenAction { diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 50796f5fc138..f7f260fcb7cc 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2926,8 +2926,8 @@ defm clangir_direct_lowering : BoolFOption<"clangir-direct-lowering", def emit_cir : Flag<["-"], "emit-cir">, Visibility<[ClangOption, CC1Option]>, Group, HelpText<"Build ASTs and then lower to ClangIR, emit the .cir file">; -def emit_flat_cir : Flag<["-"], "emit-flat-cir">, Visibility<[ClangOption, CC1Option]>, - Group, HelpText<"Build ASTs and then lower to ClangIR with no nested regions, then emit the .cir file">; +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">; /// ClangIR-specific options - END diff --git a/clang/include/clang/Driver/Types.def b/clang/include/clang/Driver/Types.def index 8fd83d14caac..9d76e949d4ac 100644 --- a/clang/include/clang/Driver/Types.def +++ b/clang/include/clang/Driver/Types.def @@ -91,7 +91,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("flat-cir", FLAT_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 07b1c7c27d4b..20a4053010d7 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -68,7 +68,7 @@ enum ActionKind { EmitCIR, /// Emit a .cir file with flat ClangIR - EmitFlatCIR, + EmitCIRFlat, /// Generate CIR, bud don't emit anything. EmitCIROnly, diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index f49637b28e5e..bc4f68655883 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -186,7 +186,7 @@ class CIRGenConsumer : public clang::ASTConsumer { feOptions.ClangIRLifetimeCheck, lifetimeOpts, feOptions.ClangIRIdiomRecognizer, idiomRecognizerOpts, feOptions.ClangIRLibOpt, libOptOpts, passOptParsingFailure, - action == CIRGenAction::OutputType::EmitFlatCIR) + action == CIRGenAction::OutputType::EmitCIRFlat) .failed()) { if (!passOptParsingFailure.empty()) diagnosticsEngine.Report(diag::err_drv_cir_pass_opt_parsing) @@ -234,7 +234,7 @@ class CIRGenConsumer : public clang::ASTConsumer { switch (action) { case CIRGenAction::OutputType::EmitCIR: - case CIRGenAction::OutputType::EmitFlatCIR: + case CIRGenAction::OutputType::EmitCIRFlat: if (outputStream && mlirMod) { // Emit remaining defaulted C++ methods if (!feOptions.ClangIRDisableEmitCXXDefault) @@ -355,7 +355,7 @@ getOutputStream(CompilerInstance &ci, StringRef inFile, return ci.createDefaultOutputFile(false, inFile, "s"); case CIRGenAction::OutputType::EmitCIR: return ci.createDefaultOutputFile(false, inFile, "cir"); - case CIRGenAction::OutputType::EmitFlatCIR: + case CIRGenAction::OutputType::EmitCIRFlat: return ci.createDefaultOutputFile(false, inFile, "cir"); case CIRGenAction::OutputType::EmitMLIR: return ci.createDefaultOutputFile(false, inFile, "mlir"); @@ -450,9 +450,9 @@ void EmitCIRAction::anchor() {} EmitCIRAction::EmitCIRAction(mlir::MLIRContext *_MLIRContext) : CIRGenAction(OutputType::EmitCIR, _MLIRContext) {} -void EmitFlatCIRAction::anchor() {} -EmitFlatCIRAction::EmitFlatCIRAction(mlir::MLIRContext *_MLIRContext) - : CIRGenAction(OutputType::EmitFlatCIR, _MLIRContext) {} +void EmitCIRFlatAction::anchor() {} +EmitCIRFlatAction::EmitCIRFlatAction(mlir::MLIRContext *_MLIRContext) + : CIRGenAction(OutputType::EmitCIRFlat, _MLIRContext) {} void EmitCIROnlyAction::anchor() {} EmitCIROnlyAction::EmitCIROnlyAction(mlir::MLIRContext *_MLIRContext) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 315dec059e45..30c58c61e8f1 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -361,7 +361,7 @@ phases::ID Driver::getFinalPhase(const DerivedArgList &DAL, (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_flat_cir)) || + (PhaseArg = DAL.getLastArg(options::OPT_emit_cir_flat)) || (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) { FinalPhase = phases::Compile; @@ -4788,8 +4788,8 @@ Action *Driver::ConstructPhaseAction( 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_flat_cir)) - return C.MakeAction(Input, types::TY_FLAT_CIR); + if (Args.hasArg(options::OPT_emit_cir_flat)) + return C.MakeAction(Input, types::TY_CIR_FLAT); 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 168045191f05..0b2f97aaa052 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -4911,7 +4911,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, if (Args.hasArg(options::OPT_fclangir_enable) || Args.hasArg(options::OPT_emit_cir) || - Args.hasArg(options::OPT_emit_flat_cir)) + Args.hasArg(options::OPT_emit_cir_flat)) CmdArgs.push_back("-fclangir-enable"); if (Args.hasArg(options::OPT_fclangir_direct_lowering)) @@ -5053,7 +5053,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-emit-llvm"); } else if (JA.getType() == types::TY_CIR) { CmdArgs.push_back("-emit-cir"); - } else if (JA.getType() == types::TY_FLAT_CIR) { + } else if (JA.getType() == types::TY_CIR_FLAT) { CmdArgs.push_back("-emit-flat-cir"); } else if (JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC) { diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 6f375308ba2c..1a36d669fec7 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2552,7 +2552,7 @@ static const auto &getFrontendActionTable() { {frontend::EmitAssembly, OPT_S}, {frontend::EmitBC, OPT_emit_llvm_bc}, {frontend::EmitCIR, OPT_emit_cir}, - {frontend::EmitFlatCIR, OPT_emit_flat_cir}, + {frontend::EmitCIRFlat, OPT_emit_cir_flat}, {frontend::EmitCIROnly, OPT_emit_cir_only}, {frontend::EmitMLIR, OPT_emit_mlir}, {frontend::EmitHTML, OPT_emit_html}, @@ -2909,7 +2909,7 @@ 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_enable) || Args.hasArg(OPT_emit_cir) || - Args.hasArg(OPT_emit_flat_cir)) + Args.hasArg(OPT_emit_cir_flat)) Opts.UseClangIRPipeline = true; if (Args.hasArg(OPT_fclangir_direct_lowering)) @@ -4385,7 +4385,7 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) { case frontend::EmitAssembly: case frontend::EmitBC: case frontend::EmitCIR: - case frontend::EmitFlatCIR: + 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 4adefac8759c..8afb32f7d6eb 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -53,7 +53,7 @@ CreateFrontendBaseAction(CompilerInstance &CI) { auto UseCIR = CI.getFrontendOpts().UseClangIRPipeline; auto Act = CI.getFrontendOpts().ProgramAction; - auto EmitsCIR = Act == EmitCIR || Act == EmitFlatCIR || Act == EmitCIROnly; + auto EmitsCIR = Act == EmitCIR || Act == EmitCIRFlat || Act == EmitCIROnly; if (!UseCIR && EmitsCIR) llvm::report_fatal_error( @@ -81,13 +81,13 @@ CreateFrontendBaseAction(CompilerInstance &CI) { case EmitBC: return std::make_unique(); #if CLANG_ENABLE_CIR case EmitCIR: return std::make_unique<::cir::EmitCIRAction>(); - case EmitFlatCIR: - return std::make_unique<::cir::EmitFlatCIRAction>(); + case EmitCIRFlat: + return std::make_unique<::cir::EmitCIRFlatAction>(); case EmitCIROnly: return std::make_unique<::cir::EmitCIROnlyAction>(); case EmitMLIR: return std::make_unique<::cir::EmitMLIRAction>(); #else case EmitCIR: - case EmitFlatCIR: + case EmitCIRFlat: case EmitCIROnly: llvm_unreachable("CIR suppport not built into clang"); #endif diff --git a/clang/test/CIR/mlirprint.c b/clang/test/CIR/mlirprint.c index 5b7605141d22..163f244a74f9 100644 --- a/clang/test/CIR/mlirprint.c +++ b/clang/test/CIR/mlirprint.c @@ -1,8 +1,8 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -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-enable -emit-flat-cir -mmlir --mlir-print-ir-after-all %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=FLATCIR +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -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 -fclangir-enable -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-enable -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-enable -emit-flat-cir -mmlir --mlir-print-ir-before=cir-structured-cfg %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CFGPASS +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-cir-flat -mmlir --mlir-print-ir-before=cir-structured-cfg %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CFGPASS int foo(void) { int i = 3; @@ -17,13 +17,13 @@ int foo(void) { // CIR-NOT: IR Dump After StructuredCFG // CIR: IR Dump After DropAST (cir-drop-ast) // CIR: cir.func @foo() -> !s32i -// FLATCIR: IR Dump After MergeCleanups (cir-merge-cleanups) -// FLATCIR: cir.func @foo() -> !s32i -// FLATCIR: IR Dump After LoweringPrepare (cir-lowering-prepare) -// FLATCIR: cir.func @foo() -> !s32i -// FLATCIR: IR Dump After StructuredCFG (cir-structured-cfg) -// FLATCIR: IR Dump After DropAST (cir-drop-ast) -// FLATCIR: cir.func @foo() -> !s32i +// CIRFLAT: IR Dump After MergeCleanups (cir-merge-cleanups) +// CIRFLAT: cir.func @foo() -> !s32i +// CIRFLAT: IR Dump After LoweringPrepare (cir-lowering-prepare) +// CIRFLAT: cir.func @foo() -> !s32i +// CIRFLAT: IR Dump After StructuredCFG (cir-structured-cfg) +// CIRFLAT: IR Dump After DropAST (cir-drop-ast) +// CIRFLAT: cir.func @foo() -> !s32i // LLVM: IR Dump After cir::direct::ConvertCIRToLLVMPass (cir-to-llvm-internal) // LLVM: llvm.func @foo() -> i32 // LLVM: IR Dump After From 399f8bdfd2d33a42ae6b2d5f91ef737ebfb7b089 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Fri, 22 Mar 2024 16:48:37 +0300 Subject: [PATCH 13/16] StructuredCFG -> FlattenCFG --- clang/include/clang/CIR/Dialect/Passes.h | 2 +- clang/include/clang/CIR/Dialect/Passes.td | 6 +++--- clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp | 2 +- clang/lib/CIR/Dialect/Transforms/CMakeLists.txt | 2 +- .../{StructuredCFG.cpp => FlattenCFG.cpp} | 14 +++++++------- clang/test/CIR/mlirprint.c | 8 ++++---- 6 files changed, 17 insertions(+), 17 deletions(-) rename clang/lib/CIR/Dialect/Transforms/{StructuredCFG.cpp => FlattenCFG.cpp} (71%) diff --git a/clang/include/clang/CIR/Dialect/Passes.h b/clang/include/clang/CIR/Dialect/Passes.h index 9e9495fc4126..ba9d8aa1ff62 100644 --- a/clang/include/clang/CIR/Dialect/Passes.h +++ b/clang/include/clang/CIR/Dialect/Passes.h @@ -34,7 +34,7 @@ std::unique_ptr createIdiomRecognizerPass(); std::unique_ptr createIdiomRecognizerPass(clang::ASTContext *astCtx); std::unique_ptr createLibOptPass(); std::unique_ptr createLibOptPass(clang::ASTContext *astCtx); -std::unique_ptr createStructuredCFGPass(); +std::unique_ptr createFlattenCFGPass(); void populateCIRFlatteningPasses(mlir::OpPassManager &pm); diff --git a/clang/include/clang/CIR/Dialect/Passes.td b/clang/include/clang/CIR/Dialect/Passes.td index 1a144795f3ee..e63b97469980 100644 --- a/clang/include/clang/CIR/Dialect/Passes.td +++ b/clang/include/clang/CIR/Dialect/Passes.td @@ -75,8 +75,8 @@ def LoweringPrepare : Pass<"cir-lowering-prepare"> { let dependentDialects = ["cir::CIRDialect"]; } -def StructuredCFG : Pass<"cir-structured-cfg"> { - let summary = "Produces structured cfg"; +def FlattenCFG : Pass<"cir-flatten-cfg"> { + let summary = "Produces flatten cfg"; let description = [{ This pass transforms CIR and inline all the nested regions. Thus, the next post condtions are met after the pass applied: @@ -85,7 +85,7 @@ def StructuredCFG : Pass<"cir-structured-cfg"> { In other words, this pass removes such CIR operations like IfOp, LoopOp, ScopeOp and etc. and produces a flat CIR. }]; - let constructor = "mlir::createStructuredCFGPass()"; + let constructor = "mlir::createFlattenCFGPass()"; let dependentDialects = ["cir::CIRDialect"]; } diff --git a/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp b/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp index 38b3586f29af..096803d398ed 100644 --- a/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp +++ b/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp @@ -2,5 +2,5 @@ #include "clang/CIR/Dialect/Passes.h" void mlir::populateCIRFlatteningPasses(mlir::OpPassManager &pm) { - pm.addPass(mlir::createStructuredCFGPass()); + pm.addPass(mlir::createFlattenCFGPass()); } \ No newline at end of file diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt index b450fab9c61f..e407db8b7a65 100644 --- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt +++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt @@ -7,7 +7,7 @@ add_clang_library(MLIRCIRTransforms IdiomRecognizer.cpp LibOpt.cpp StdHelpers.cpp - StructuredCFG.cpp + FlattenCFG.cpp DEPENDS MLIRCIRPassIncGen diff --git a/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp similarity index 71% rename from clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp rename to clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp index c7d957b01dcf..2f493ab24260 100644 --- a/clang/lib/CIR/Dialect/Transforms/StructuredCFG.cpp +++ b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp @@ -12,19 +12,19 @@ using namespace mlir::cir; namespace { -struct StructuredCFGPass : public StructuredCFGBase { +struct FlattenCFGPass : public FlattenCFGBase { - StructuredCFGPass() = default; + FlattenCFGPass() = default; void runOnOperation() override; }; -void populateStructuredCFGPatterns(RewritePatternSet &patterns) { +void populateFlattenCFGPatterns(RewritePatternSet &patterns) { // TODO: add patterns here } -void StructuredCFGPass::runOnOperation() { +void FlattenCFGPass::runOnOperation() { RewritePatternSet patterns(&getContext()); - populateStructuredCFGPatterns(patterns); + populateFlattenCFGPatterns(patterns); // Collect operations to apply patterns. SmallVector ops; @@ -41,8 +41,8 @@ void StructuredCFGPass::runOnOperation() { namespace mlir { -std::unique_ptr createStructuredCFGPass() { - return std::make_unique(); +std::unique_ptr createFlattenCFGPass() { + return std::make_unique(); } } // namespace mlir diff --git a/clang/test/CIR/mlirprint.c b/clang/test/CIR/mlirprint.c index 163f244a74f9..2456711b1ebc 100644 --- a/clang/test/CIR/mlirprint.c +++ b/clang/test/CIR/mlirprint.c @@ -2,7 +2,7 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -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 -fclangir-enable -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-enable -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-enable -emit-cir-flat -mmlir --mlir-print-ir-before=cir-structured-cfg %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CFGPASS +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-cir-flat -mmlir --mlir-print-ir-before=cir-flatten-cfg %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=CFGPASS int foo(void) { int i = 3; @@ -14,14 +14,14 @@ int foo(void) { // CIR: cir.func @foo() -> !s32i // CIR: IR Dump After LoweringPrepare (cir-lowering-prepare) // CIR: cir.func @foo() -> !s32i -// CIR-NOT: IR Dump After StructuredCFG +// CIR-NOT: IR Dump After FlattenCFG // CIR: IR Dump After DropAST (cir-drop-ast) // CIR: cir.func @foo() -> !s32i // CIRFLAT: IR Dump After MergeCleanups (cir-merge-cleanups) // CIRFLAT: cir.func @foo() -> !s32i // CIRFLAT: IR Dump After LoweringPrepare (cir-lowering-prepare) // CIRFLAT: cir.func @foo() -> !s32i -// CIRFLAT: IR Dump After StructuredCFG (cir-structured-cfg) +// CIRFLAT: IR Dump After FlattenCFG (cir-flatten-cfg) // CIRFLAT: IR Dump After DropAST (cir-drop-ast) // CIRFLAT: cir.func @foo() -> !s32i // LLVM: IR Dump After cir::direct::ConvertCIRToLLVMPass (cir-to-llvm-internal) @@ -32,4 +32,4 @@ int foo(void) { // CIRPASS-NOT: IR Dump After MergeCleanups // CIRPASS: IR Dump After DropAST -// CFGPASS: IR Dump Before StructuredCFG (cir-structured-cfg) +// CFGPASS: IR Dump Before FlattenCFG (cir-flatten-cfg) From f32440486ff7281e1c4cfc2f10605bf437944cc9 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Fri, 22 Mar 2024 17:26:39 +0300 Subject: [PATCH 14/16] minor fixes --- clang/include/clang/CIR/Dialect/Passes.h | 2 +- clang/lib/CIR/CodeGen/CIRPasses.cpp | 12 +++++++++++- clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp | 6 ------ clang/lib/CIR/Dialect/Transforms/CMakeLists.txt | 1 - clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp | 12 ++++++++++++ clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 2 +- 6 files changed, 25 insertions(+), 10 deletions(-) delete mode 100644 clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp diff --git a/clang/include/clang/CIR/Dialect/Passes.h b/clang/include/clang/CIR/Dialect/Passes.h index ba9d8aa1ff62..2f713240944f 100644 --- a/clang/include/clang/CIR/Dialect/Passes.h +++ b/clang/include/clang/CIR/Dialect/Passes.h @@ -36,7 +36,7 @@ std::unique_ptr createLibOptPass(); std::unique_ptr createLibOptPass(clang::ASTContext *astCtx); std::unique_ptr createFlattenCFGPass(); -void populateCIRFlatteningPasses(mlir::OpPassManager &pm); +void populateCIRPreLoweringPasses(mlir::OpPassManager &pm); //===----------------------------------------------------------------------===// // Registration diff --git a/clang/lib/CIR/CodeGen/CIRPasses.cpp b/clang/lib/CIR/CodeGen/CIRPasses.cpp index 380ff3b677f4..2ff6d5b4ce1a 100644 --- a/clang/lib/CIR/CodeGen/CIRPasses.cpp +++ b/clang/lib/CIR/CodeGen/CIRPasses.cpp @@ -58,7 +58,7 @@ runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx, pm.addPass(mlir::createLoweringPreparePass(&astCtx)); if (flattenCIR) - mlir::populateCIRFlatteningPasses(pm); + mlir::populateCIRPreLoweringPasses(pm); // FIXME: once CIRCodenAction fixes emission other than CIR we // need to run this right before dialect emission. @@ -69,3 +69,13 @@ runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx, } } // namespace cir + + +namespace mlir { + +void populateCIRPreLoweringPasses(OpPassManager &pm) { + pm.addPass(createFlattenCFGPass()); + // add other passes here +} + +} // namespace mlir { \ No newline at end of file diff --git a/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp b/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp deleted file mode 100644 index 096803d398ed..000000000000 --- a/clang/lib/CIR/Dialect/Transforms/CIRFlattening.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "mlir/Pass/PassManager.h" -#include "clang/CIR/Dialect/Passes.h" - -void mlir::populateCIRFlatteningPasses(mlir::OpPassManager &pm) { - pm.addPass(mlir::createFlattenCFGPass()); -} \ No newline at end of file diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt index e407db8b7a65..2a8a4dfeae92 100644 --- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt +++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt @@ -1,5 +1,4 @@ add_clang_library(MLIRCIRTransforms - CIRFlattening.cpp LifetimeCheck.cpp LoweringPrepare.cpp MergeCleanups.cpp diff --git a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp index 2f493ab24260..477a85df6c26 100644 --- a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp +++ b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp @@ -1,3 +1,15 @@ +//====- FlattenCFG.cpp - Flatten CIR CFG ----------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements pass that inlines CIR operations regions into the parent +// function region. +// +//===----------------------------------------------------------------------===// #include "PassDetail.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/IR/PatternMatch.h" diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 170eb5afc6c6..5164d4d95586 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -2972,7 +2972,7 @@ std::unique_ptr createConvertCIRToLLVMPass() { } void populateCIRToLLVMPasses(mlir::OpPassManager &pm) { - populateCIRFlatteningPasses(pm); + populateCIRPreLoweringPasses(pm); pm.addPass(createConvertCIRToLLVMPass()); } From daec97f1de2e58b2f419ff956fffd73fae719816 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Fri, 22 Mar 2024 17:34:36 +0300 Subject: [PATCH 15/16] minor bug fix --- clang/lib/CIR/CodeGen/CIRPasses.cpp | 1 - clang/lib/Driver/ToolChains/Clang.cpp | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRPasses.cpp b/clang/lib/CIR/CodeGen/CIRPasses.cpp index 2ff6d5b4ce1a..89560084b79e 100644 --- a/clang/lib/CIR/CodeGen/CIRPasses.cpp +++ b/clang/lib/CIR/CodeGen/CIRPasses.cpp @@ -70,7 +70,6 @@ runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx, } // namespace cir - namespace mlir { void populateCIRPreLoweringPasses(OpPassManager &pm) { diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 0b2f97aaa052..4e16b1fbe45a 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5054,7 +5054,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } else if (JA.getType() == types::TY_CIR) { CmdArgs.push_back("-emit-cir"); } else if (JA.getType() == types::TY_CIR_FLAT) { - CmdArgs.push_back("-emit-flat-cir"); + CmdArgs.push_back("-emit-cir-flat"); } 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 From 5a876d9ea5c577be6c03d4524ed8ce4ebed41aa6 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Fri, 22 Mar 2024 17:35:48 +0300 Subject: [PATCH 16/16] clang-format ... --- clang/lib/CIR/CodeGen/CIRPasses.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRPasses.cpp b/clang/lib/CIR/CodeGen/CIRPasses.cpp index 89560084b79e..63d936efdb9e 100644 --- a/clang/lib/CIR/CodeGen/CIRPasses.cpp +++ b/clang/lib/CIR/CodeGen/CIRPasses.cpp @@ -77,4 +77,4 @@ void populateCIRPreLoweringPasses(OpPassManager &pm) { // add other passes here } -} // namespace mlir { \ No newline at end of file +} // namespace mlir \ No newline at end of file