Fix JIT exceptions

pull/6/head
A. R. Shajii 2021-10-27 19:02:36 -04:00
parent 8003220455
commit 94965760ba
5 changed files with 31 additions and 14 deletions

View File

@ -1,5 +1,6 @@
#include "engine.h"
#include "codon/runtime/lib.h"
#include "codon/sir/llvm/memory_manager.h"
#include "codon/sir/llvm/optimize.h"
@ -129,7 +130,11 @@ void JIT::run(const ir::Func *input, const std::vector<ir::Var *> &newGlobals) {
llvm::cantFail(engine->addModule({std::move(pair.second), std::move(pair.first)}));
auto func = llvm::cantFail(engine->lookup(name));
auto *repl = (InputFunc *)func.getAddress();
(*repl)();
try {
(*repl)();
} catch (const seq_jit_error &) {
// nothing to do
}
// llvm::cantFail(rt->remove());
}

View File

@ -127,7 +127,7 @@ static void seq_delete_exc(_Unwind_Exception *expToDelete) {
if (!expToDelete || expToDelete->exception_class != ourBaseExceptionClass)
return;
auto *exc = (OurException *)((char *)expToDelete + ourBaseFromUnwindOffset);
if (seq_debug) {
if (seq_flags & SEQ_FLAG_DEBUG) {
exc->bt.free();
}
seq_free(exc);
@ -148,7 +148,7 @@ SEQ_FUNC void *seq_alloc_exc(int type, void *obj) {
e->obj = obj;
e->unwindException.exception_class = ourBaseExceptionClass;
e->unwindException.exception_cleanup = seq_delete_unwind_exc;
if (seq_debug) {
if (seq_flags & SEQ_FLAG_DEBUG) {
e->bt.frames = nullptr;
e->bt.count = 0;
if (!state)
@ -208,7 +208,7 @@ SEQ_FUNC void seq_terminate(void *exc) {
}
fprintf(stderr, "\n");
if (seq_debug) {
if (seq_flags & SEQ_FLAG_DEBUG) {
auto *bt = &base->bt;
if (bt->count > 0) {
fprintf(stderr, "\n\033[1mBacktrace:\033[0m\n");
@ -220,7 +220,11 @@ SEQ_FUNC void seq_terminate(void *exc) {
}
}
abort();
if (seq_flags & SEQ_FLAG_JIT) {
throw seq_jit_error();
} else {
abort();
}
}
SEQ_FUNC void seq_throw(void *exc) {

View File

@ -45,16 +45,16 @@ static void register_thread(kmp_int32 *global_tid, kmp_int32 *bound_tid) {
void seq_exc_init();
int seq_debug;
int seq_flags;
SEQ_FUNC void seq_init(int d) {
SEQ_FUNC void seq_init(int flags) {
GC_INIT();
GC_set_warn_proc(GC_ignore_warn_proc);
GC_allow_register_threads();
// equivalent to: #pragma omp parallel { register_thread }
__kmpc_fork_call(&dummy_loc, 0, (kmpc_micro)register_thread);
seq_exc_init();
seq_debug = d;
seq_flags = flags;
}
SEQ_FUNC bool seq_is_macos() {

View File

@ -1,12 +1,16 @@
#ifndef CODON_RUNTIME_LIB_H
#define CODON_RUNTIME_LIB_H
#pragma once
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#include <string>
#include <unwind.h>
#define SEQ_FLAG_DEBUG (1 << 0)
#define SEQ_FLAG_JIT (1 << 1)
#define SEQ_FUNC extern "C"
typedef int64_t seq_int_t;
@ -21,9 +25,9 @@ struct seq_str_t {
char *str;
};
extern int seq_debug;
extern int seq_flags;
SEQ_FUNC void seq_init(int debug);
SEQ_FUNC void seq_init(int flags);
SEQ_FUNC bool seq_is_macos();
SEQ_FUNC seq_int_t seq_pid();
@ -76,4 +80,7 @@ SEQ_FUNC void *seq_rlock_new();
SEQ_FUNC bool seq_rlock_acquire(void *lock, bool block, double timeout);
SEQ_FUNC void seq_rlock_release(void *lock);
#endif /* CODON_RUNTIME_LIB_H */
class seq_jit_error : public std::runtime_error {
public:
explicit seq_jit_error(const std::string &what = "") : std::runtime_error(what) {}
};

View File

@ -625,7 +625,8 @@ void LLVMVisitor::visit(const Module *x) {
llvm::Value *argStorage = getVar(x->getArgVar());
seqassert(argStorage, "argument storage missing");
B->CreateStore(arr, argStorage);
B->CreateCall(initFunc, B->getInt32(db.debug ? 1 : 0));
const int flags = (db.debug ? SEQ_FLAG_DEBUG : 0) | (db.jit ? SEQ_FLAG_JIT : 0);
B->CreateCall(initFunc, B->getInt32(flags));
// Put the entire program in a new function
{