Skip to content

Commit

Permalink
first demo of MLIR
Browse files Browse the repository at this point in the history
  • Loading branch information
PikachuHy committed Jan 7, 2024
1 parent 6adc4aa commit a2bfd13
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion include/pscm/codegen/codegen.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "pscm/Cell.h"
#include <optional>

namespace pscm {
Cell mlir_codegen_and_run_jit(Cell expr);
std::optional<Cell> mlir_codegen_and_run_jit(Cell expr);
}
9 changes: 8 additions & 1 deletion src/Scheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,14 @@ Cell Scheme::eval(const UString& code) {
ret = Evaluator(*this).eval(ret, current_module_->env());
}
else if (use_mlir_) {
ret = mlir_codegen_and_run_jit(ret);
auto run_ret = mlir_codegen_and_run_jit(ret);
if (run_ret.has_value()) {
ret = run_ret.value();
}
else {
// failback to direct eval
ret = eval(ret);
}
}
else {
ret = eval(ret);
Expand Down
12 changes: 6 additions & 6 deletions src/codegen/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ int create_mlir_add(mlir::MLIRContext& ctx, mlir::OwningOpRef<mlir::ModuleOp>& m
return 0;
}

Cell mlir_codegen_and_run_jit(Cell expr) {
std::optional<Cell> mlir_codegen_and_run_jit(Cell expr) {
mlir::registerAsmPrinterCLOptions();
mlir::registerMLIRContextCLOptions();
mlir::registerPassManagerCLOptions();
Expand All @@ -249,20 +249,20 @@ Cell mlir_codegen_and_run_jit(Cell expr) {
if (auto err = create_mlir_add(context, module, cadr(expr), caddr(expr))) {
llvm::errs() << "create mlir error"
<< "\n";
return Cell::bool_false();
return std::nullopt;
}
}
else {
llvm::errs() << "not supported now"
<< "\n";
return Cell::bool_false();
return std::nullopt;
}

mlir::PassManager pm(module.get()->getName());
if (mlir::failed(mlir::applyPassManagerCLOptions(pm))) {
llvm::errs() << "applyPassManagerCLOptions error"
<< "\n";
return Cell::bool_false();
return std::nullopt;
}
mlir::OpPassManager& optPM = pm.nest<pscm::FuncOp>();
optPM.addPass(mlir::createCanonicalizerPass());
Expand All @@ -274,13 +274,13 @@ Cell mlir_codegen_and_run_jit(Cell expr) {
if (mlir::failed(pm.run(*module))) {
llvm::errs() << "run pass error"
<< "\n";
return Cell::bool_false();
return std::nullopt;
}
// module->dump();
if (auto err = run_jit(*module)) {
llvm::errs() << "run mlir error"
<< "\n";
return Cell::bool_false();
return std::nullopt;
}
return Cell::none();
}
Expand Down

0 comments on commit a2bfd13

Please sign in to comment.