From 96e7e12f32513992735c9457297c50588f510dc9 Mon Sep 17 00:00:00 2001 From: xlauko Date: Wed, 24 Jan 2024 17:37:35 +0100 Subject: [PATCH] cc: Refactor mlir pipeline to be used in any workflow. --- include/vast/Frontend/Consumer.hpp | 4 ++++ include/vast/Frontend/Pipelines.hpp | 2 +- lib/vast/Frontend/Consumer.cpp | 31 +++++++++++++++++++---------- lib/vast/Frontend/Pipelines.cpp | 16 ++++----------- 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/include/vast/Frontend/Consumer.hpp b/include/vast/Frontend/Consumer.hpp index 3e4ecbf154..2767cc8027 100644 --- a/include/vast/Frontend/Consumer.hpp +++ b/include/vast/Frontend/Consumer.hpp @@ -98,6 +98,10 @@ namespace vast::cc { target_dialect target, owning_module_ref mod, mcontext_t *mctx ); + void process_mlir_module( + target_dialect target, mlir::ModuleOp mod, mcontext_t *mctx + ); + output_type action; output_stream_ptr output_stream; }; diff --git a/include/vast/Frontend/Pipelines.hpp b/include/vast/Frontend/Pipelines.hpp index 9c4fc14e27..99b1549553 100644 --- a/include/vast/Frontend/Pipelines.hpp +++ b/include/vast/Frontend/Pipelines.hpp @@ -37,7 +37,7 @@ namespace vast::cc { // scheduled pipeline. // std::unique_ptr< pipeline_t > setup_pipeline( - pipeline_source src, output_type trg, + pipeline_source src, target_dialect trg, mcontext_t &mctx, const vast_args &vargs, const pipelines_config &config diff --git a/lib/vast/Frontend/Consumer.cpp b/lib/vast/Frontend/Consumer.cpp index 43754aaeb7..e3795c8abd 100644 --- a/lib/vast/Frontend/Consumer.cpp +++ b/lib/vast/Frontend/Consumer.cpp @@ -149,8 +149,10 @@ namespace vast::cc { backend::Backend_EmitAssembly, std::move(mod), mctx.get() ); case output_type::emit_mlir: { - auto trg = parse_target_dialect(vargs.get_option(opt::emit_mlir).value()); - return emit_mlir_output(trg, std::move(mod), mctx.get()); + if (auto trg = vargs.get_option(opt::emit_mlir)) { + return emit_mlir_output(parse_target_dialect(trg.value()), std::move(mod), mctx.get()); + } + VAST_FATAL("no target dialect specified for MLIR output"); } case output_type::emit_llvm: return emit_backend_output( @@ -170,21 +172,20 @@ namespace vast::cc { ) { llvm::LLVMContext llvm_context; + process_mlir_module(target_dialect::llvm, mlir_module.get(), mctx); + auto mod = target::llvmir::translate(mlir_module.get(), llvm_context); auto dl = cgctx->actx.getTargetInfo().getDataLayoutString(); + clang::EmitBackendOutput( opts.diags, opts.headers, opts.codegen, opts.target, opts.lang, dl, mod.get(), backend_action, &opts.vfs, std::move(output_stream) ); } - void vast_stream_consumer::emit_mlir_output( - target_dialect target, owning_module_ref mod, mcontext_t *mctx + void vast_stream_consumer::process_mlir_module( + target_dialect target, mlir::ModuleOp mod, mcontext_t *mctx ) { - if (!output_stream || !mod) { - return; - } - // Handle source manager properly given that lifetime analysis // might emit warnings and remarks. auto &src_mgr = cgctx->actx.getSourceManager(); @@ -209,12 +210,12 @@ namespace vast::cc { // Setup and execute vast pipeline auto pipeline = setup_pipeline( - pipeline_source::ast, output_type::emit_mlir, *mctx, vargs, + pipeline_source::ast, target, *mctx, vargs, default_pipelines_config() ); VAST_CHECK(pipeline, "failed to setup pipeline"); - auto result = pipeline->run(mod.get()); + auto result = pipeline->run(mod); VAST_CHECK(mlir::succeeded(result), "MLIR pass manager failed when running vast passes"); // Verify the diagnostic handler to make sure that each of the @@ -228,6 +229,16 @@ namespace vast::cc { // if (!vargs.has_option(opt::disable_emit_cxx_default)) { // generator->build_default_methods(); // } + } + + void vast_stream_consumer::emit_mlir_output( + target_dialect target, owning_module_ref mod, mcontext_t *mctx + ) { + if (!output_stream || !mod) { + return; + } + + process_mlir_module(target, mod.get(), mctx); // FIXME: we cannot roundtrip prettyForm=true right now. mlir::OpPrintingFlags flags; diff --git a/lib/vast/Frontend/Pipelines.cpp b/lib/vast/Frontend/Pipelines.cpp index 974d635fa0..799909b1ee 100644 --- a/lib/vast/Frontend/Pipelines.cpp +++ b/lib/vast/Frontend/Pipelines.cpp @@ -62,7 +62,7 @@ namespace vast::cc { gap::generator< pipeline_step_ptr > conversion( pipeline_source src, - output_type trg, + target_dialect trg, const vast_args &vargs, const pipelines_config &config ) { @@ -70,17 +70,9 @@ namespace vast::cc { // deduced from source, target and vargs auto path = default_conversion_path; - auto target_dialect = [&] () -> enum target_dialect { - if (trg == output_type::emit_mlir) { - return parse_target_dialect(vargs.get_option(opt::emit_mlir).value()); - } - // if we user do not specify target dialect, we convert all the way to llvm - return target_dialect::llvm; - } (); - bool simplify = vargs.has_option(opt::simplify); - if (target_dialect == target_dialect::high_level && !simplify) { + if (trg == target_dialect::high_level && !simplify) { co_return; } @@ -89,7 +81,7 @@ namespace vast::cc { co_yield step(); } - if (target_dialect == dialect) { + if (trg == dialect) { break; } } @@ -99,7 +91,7 @@ namespace vast::cc { std::unique_ptr< pipeline_t > setup_pipeline( pipeline_source src, - output_type trg, + target_dialect trg, mcontext_t &mctx, const vast_args &vargs, const pipelines_config &config