Replace some of the Result types with llvm::Optional

This commit is contained in:
Sameer Rahmani 2021-09-19 10:27:13 +01:00
parent 3c19cace5f
commit 00869e6c8d
7 changed files with 38 additions and 25 deletions

View File

@ -125,9 +125,13 @@ int dumpAsObject(Namespace &ns) {
// TODO: Move the compilation process to the Namespace class
auto maybeModule = ns.compileToLLVM();
// TODO: Fix this call to raise the wrapped error instead
auto module = std::move(
maybeModule.getValueOrFail("Faild to generato LLVM IR for namespace"));
auto &ctx = ns.getContext();
if (!maybeModule) {
// TODO: Rais and error: "Faild to generato LLVM IR for namespace"
return -1;
}
auto module = std::move(maybeModule.getValue());
auto &ctx = ns.getContext();
// TODO: We need to set the triple data layout and everything to that sort in
// one place. We want them for the JIT as well and also we're kinda
@ -350,7 +354,11 @@ int main(int argc, char *argv[]) {
case Action::RunJIT: {
auto maybeJIT = JIT::make(*ns.get());
auto jit = std::move(maybeJIT.getValueOrFail("Couldn't creat the JIT!"));
if (!maybeJIT) {
// TODO: panic in here: "Couldn't creat the JIT!"
return -1;
}
auto jit = std::move(maybeJIT.getValue());
if (jit->invoke("main")) {
llvm::errs() << "Faild to invoke the 'main' function.\n";

View File

@ -2,8 +2,3 @@
(fn () 4))
(def main1 (fn (v y n) 3))
ht
(- 3 4 (sh - r e 3bea -32) ((((())))))

View File

@ -44,7 +44,7 @@
namespace serene {
class JIT;
using MaybeJIT = Result<std::unique_ptr<JIT>, serene::errors::Error>;
using MaybeJIT = llvm::Optional<std::unique_ptr<JIT>>;
/// A simple object cache following Lang's LLJITWithObjectCache example and
/// MLIR's SimpelObjectCache.

View File

@ -53,11 +53,9 @@ class Expression;
using Node = std::shared_ptr<Expression>;
using Ast = std::vector<Node>;
} // namespace exprs
// TODO: replace the temporary `bool` by errors::Error
using MaybeModule = Result<std::unique_ptr<llvm::Module>, bool>;
// TODO: replace the temporary `bool` by errors::Error
using MaybeModuleOp = Result<mlir::OwningOpRef<mlir::ModuleOp>, bool>;
using MaybeModule = llvm::Optional<std::unique_ptr<llvm::Module>>;
using MaybeModuleOp = llvm::Optional<mlir::OwningOpRef<mlir::ModuleOp>>;
/// Serene's namespaces are the unit of compilation. Any code that needs to be
/// compiled has to be in a namespace. The official way to create a new

View File

@ -193,8 +193,11 @@ MaybeJIT JIT::make(Namespace &ns,
auto maybeModule = jitEngine->ns.compileToLLVM();
auto llvmModule =
std::move(maybeModule.getValueOrFail("Compilation Failed!"));
if (!maybeModule.hasValue()) {
return llvm::None;
}
auto llvmModule = std::move(maybeModule.getValue());
packFunctionArguments(llvmModule.get());
auto dataLayout = llvmModule->getDataLayout();
@ -282,7 +285,7 @@ MaybeJIT JIT::make(Namespace &ns,
cantFail(llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
dataLayout.getGlobalPrefix())));
return MaybeJIT::success(std::move(jitEngine));
return MaybeJIT(std::move(jitEngine));
};
llvm::Expected<void (*)(void **)> JIT::lookup(llvm::StringRef name) const {

View File

@ -60,6 +60,7 @@ mlir::LogicalResult Namespace::setTree(exprs::Ast &t) {
if (initialized) {
return mlir::failure();
}
this->tree = std::move(t);
this->initialized = true;
return mlir::success();
@ -85,10 +86,10 @@ MaybeModuleOp Namespace::generate() {
if (mlir::failed(runPasses(module))) {
// TODO: Report a proper error
module.emitError("Failure in passes!");
return MaybeModuleOp::error(true);
return llvm::None;
}
return MaybeModuleOp::success(module);
return MaybeModuleOp(module);
}
mlir::LogicalResult Namespace::runPasses(mlir::ModuleOp &m) {
@ -112,15 +113,15 @@ MaybeModule Namespace::compileToLLVM() {
if (!maybeModule) {
NAMESPACE_LOG("IR generation failed for '" << name << "'");
return MaybeModule::error(true);
return llvm::None;
}
if (ctx.getTargetPhase() >= CompilationPhase::IR) {
mlir::ModuleOp module = maybeModule.getValue().get();
return MaybeModule::success(::serene::slir::compileToLLVMIR(ctx, module));
return MaybeModule(::serene::slir::compileToLLVMIR(ctx, module));
}
return MaybeModule::error(true);
return llvm::None;
};
Namespace::~Namespace(){};

View File

@ -28,6 +28,7 @@
#include "serene/namespace.h"
#include "serene/reader/location.h"
#include "serene/reader/reader.h"
#include "serene/reader/semantics.h"
#include "serene/utils.h"
#include <llvm/Support/FormatVariadic.h>
@ -102,11 +103,18 @@ NSPtr SourceMgr::readNamespace(SereneContext &ctx, std::string name,
return nullptr;
}
// Perform the semantic analytics
auto afterAst = reader::analyze(ctx, maybeAst.getValue());
if (!afterAst) {
// TODO: Do we need raise an error here too?
return nullptr;
}
// Create the NS and set the AST
auto ns =
makeNamespace(ctx, name, llvm::Optional(llvm::StringRef(importedFile)));
if (mlir::failed(ns->setTree(maybeAst.getValue()))) {
if (mlir::failed(ns->setTree(afterAst.getValue()))) {
SMGR_LOG("Couldn't set the AST for namespace: " + name)
return nullptr;
}
@ -117,8 +125,8 @@ NSPtr SourceMgr::readNamespace(SereneContext &ctx, std::string name,
unsigned SourceMgr::AddNewSourceBuffer(std::unique_ptr<llvm::MemoryBuffer> f,
reader::LocationRange includeLoc) {
SrcBuffer nb;
nb.buffer = std::move(f);
nb.importLoc = includeLoc;
nb.buffer = std::move(f);
nb.importLoc = includeLoc;
buffers.push_back(std::move(nb));
return buffers.size();
};