Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
jit.c File Reference
#include "serene/jit/jit.h"
#include <llvm-c/Core.h>
#include <llvm-c/Error.h>
#include <llvm-c/LLJIT.h>
#include <llvm-c/Support.h>
#include <llvm-c/Target.h>
#include <stdio.h>
#include "serene/rt/context.h"
#include "serene/rt/engine.h"
#include "serene/rt/mm/interface.h"
#include "serene/utils.h"
#include <string.h>
Include dependency graph for jit.c:

Go to the source code of this file.

Functions

static int handle_error (LLVMErrorRef err)
 
srn_jit_tsrn_jit_make (srn_mm_t *mm)
 
int srn_jit_make_and_initialize (srn_jit_t *result)
 
int srn_jit_shutdown (srn_jit_t *jit)
 
srn_error_tsrn_jit_add_module (srn_context_t *ctx, srn_jit_module_t *module)
 Add a module to the jit compiler.
 

Function Documentation

◆ handle_error()

static int handle_error ( LLVMErrorRef err)
inlinestatic

Definition at line 36 of file jit.c.

36 {
37 // TODO(lxsameer): Use the runtime diagnostic system here.
38 char *err_msg = LLVMGetErrorMessage(err);
39 UNUSED(fprintf(stderr, "Error: %s\n", err_msg));
40 LLVMDisposeErrorMessage(err_msg);
41 return 1;
42}
#define UNUSED(x)
Definition utils.h:43
Here is the caller graph for this function:

◆ srn_jit_add_module()

srn_error_t * srn_jit_add_module ( srn_context_t * ctx,
srn_jit_module_t * module )

Add a module to the jit compiler.

Definition at line 83 of file jit.c.

83 {
84 PANIC_IF_NULL(ctx);
85 PANIC_IF_NULL(module);
86
87 LLVMOrcLLJITRef j = ctx->engine->jit->llvm_jit;
88
89 LLVMOrcJITDylibRef jd = LLVMOrcLLJITGetMainJITDylib(j);
90 LLVMErrorRef err = LLVMOrcLLJITAddLLVMIRModule(j, jd, module->tsm);
91 srn_error_t *srn_err = nullptr;
92
93 if (err != nullptr) {
94 // If adding the ThreadSafeModule fails then we need to clean it up
95 // ourselves. If adding it succeeds the JIT will manage the memory.
96 // LLVMOrcDisposeThreadSafeModule(module);
97
98 // LLVM owns the string message, but we want to own it
99 char *jit_err_msg = LLVMGetErrorMessage(err);
100 char *msg = srn_copy_bytes(ctx, jit_err_msg, strlen(jit_err_msg), alignof(char));
101 srn_err = srn_errors_make(ctx, FAILED_TO_ADD_MODULE, msg);
102 LLVMDisposeErrorMessage(jit_err_msg);
103 }
104 return srn_err;
105}
srn_error_t * srn_errors_make(const srn_context_t *ctx, srn_error_tag_t tag, const char *msg)
Definition errors.c:28
@ FAILED_TO_ADD_MODULE
Failed to add a module to the jit engine.
Definition errors.h:49
srn_engine_t * engine
Long term state of the compiler.
Definition context.h:49
LLVMOrcThreadSafeModuleRef tsm
Definition jit.h:29
#define PANIC_IF_NULL(ptr)
Definition utils.h:64
char * srn_copy_bytes(srn_context_t *ctx, const char *src, size_t len, size_t alignment)
Definition utils.c:42
Here is the call graph for this function:

◆ srn_jit_make()

srn_jit_t * srn_jit_make ( srn_mm_t * mm)

Definition at line 44 of file jit.c.

44 {
45 PANIC_IF_NULL(mm);
47 PANIC_IF_NULL(jit);
48 jit->builder = nullptr;
49 jit->llvm_jit = nullptr;
50 return jit;
51}
#define srn_mm_immortal_allocate(mm, T)
Definition interface.h:169
LLVMOrcLLJITRef llvm_jit
Definition jit.h:36
LLVMOrcLLJITBuilderRef builder
Definition jit.h:37
Here is the caller graph for this function:

◆ srn_jit_make_and_initialize()

int srn_jit_make_and_initialize ( srn_jit_t * result)

Definition at line 53 of file jit.c.

53 {
54 PANIC_IF_NULL(result);
55
56 int error = 0;
57 // TODO(lxsameer): Check for the target platform and setup the codegen
58 // and printer accordingly. Only applies to when we want to
59 // link the jit library into the target binary.
60
61 // Initialize native target codegen and asm printer.
62 LLVMInitializeNativeTarget();
63 LLVMInitializeNativeAsmPrinter();
64
65 LLVMErrorRef err = nullptr;
66 err = LLVMOrcCreateLLJIT(&result->llvm_jit, result->builder);
67 if (err) {
68 error = handle_error(err);
69 LLVMShutdown();
70 return error;
71 }
72 return 0;
73}
static int handle_error(LLVMErrorRef err)
Definition jit.c:36
Here is the call graph for this function:

◆ srn_jit_shutdown()

int srn_jit_shutdown ( srn_jit_t * jit)

Definition at line 75 of file jit.c.

75 {
76 LLVMErrorRef err = LLVMOrcDisposeLLJIT(jit->llvm_jit);
77 if (err) {
78 return handle_error(err);
79 }
80 return 0;
81}
Here is the call graph for this function:
Here is the caller graph for this function: