Skip to content

Commit

Permalink
cc: Refactor mlir pipeline to be used in any workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
xlauko committed Jan 24, 2024
1 parent 387fef0 commit 96e7e12
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
4 changes: 4 additions & 0 deletions include/vast/Frontend/Consumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion include/vast/Frontend/Pipelines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 21 additions & 10 deletions lib/vast/Frontend/Consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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;
Expand Down
16 changes: 4 additions & 12 deletions lib/vast/Frontend/Pipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,17 @@ 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
) {
// TODO: add support for custom conversion paths
// 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;
}

Expand All @@ -89,7 +81,7 @@ namespace vast::cc {
co_yield step();
}

if (target_dialect == dialect) {
if (trg == dialect) {
break;
}
}
Expand All @@ -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
Expand Down

0 comments on commit 96e7e12

Please sign in to comment.