Serene Runtime 1.0.0-dev
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 <string.h>
#include "serene/rt/context.h"
#include "serene/rt/engine.h"
#include "serene/rt/mm/interface.h"
#include "serene/utils.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_init (srn_jit_t *result)
 Creates a jit engine and sets the given result to it.
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()

int handle_error ( LLVMErrorRef err)
inlinestatic

Definition at line 35 of file jit.c.

35 {
36 // TODO(lxsameer): Use the runtime diagnostic system here.
37 char *err_msg = LLVMGetErrorMessage(err);
38 UNUSED(fprintf(stderr, "Error: %s\n", err_msg));
39 LLVMDisposeErrorMessage(err_msg);
40 return 1;
41}
#define UNUSED(x)
Definition utils.h:45
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 88 of file jit.c.

88 {
89 PANIC_IF_NULL(ctx);
90 PANIC_IF_NULL(module);
91
92 LLVMOrcLLJITRef j = ctx->engine->jit->llvm_jit;
93
94 LLVMOrcJITDylibRef jd = LLVMOrcLLJITGetMainJITDylib(j);
95 LLVMErrorRef err = LLVMOrcLLJITAddLLVMIRModule(j, jd, module->tsm);
96 srn_error_t *srn_err = nullptr;
97
98 if (err != nullptr) {
99 // If adding the ThreadSafeModule fails then we need to clean it up
100 // ourselves. If adding it succeeds the JIT will manage the memory.
101 // LLVMOrcDisposeThreadSafeModule(module);
102
103 // LLVM owns the string message, but we want to own it. The copy has to
104 // include the terminator; srn_copy_bytes copies exactly the byte count
105 // it is given.
106 char *jit_err_msg = LLVMGetErrorMessage(err);
107 char *msg = srn_copy_bytes(ctx, jit_err_msg, strlen(jit_err_msg) + 1, alignof(char));
108 srn_err = srn_errors_make(ctx, FAILED_TO_ADD_MODULE, msg);
109 LLVMDisposeErrorMessage(jit_err_msg);
110 }
111 return srn_err;
112}
srn_error_t * srn_errors_make(const srn_context_t *ctx, srn_error_tag_t tag, const char *msg)
Build an error in ctx's memory, tagged tag and carrying msg.
Definition errors.c:31
@ FAILED_TO_ADD_MODULE
Definition errors.h:80
srn_engine_t * engine
Long term state of the compiler.
Definition context.h:49
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
LLVMOrcThreadSafeModuleRef tsm
Definition jit.h:29
#define PANIC_IF_NULL(ptr)
Definition utils.h:66
char * srn_copy_bytes(const srn_context_t *ctx, const char *src, size_t len, size_t alignment)
Definition utils.c:56
Here is the call graph for this function:

◆ srn_jit_init()

int srn_jit_init ( srn_jit_t * result)

Creates a jit engine and sets the given result to it.

Definition at line 52 of file jit.c.

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

◆ srn_jit_make()

srn_jit_t * srn_jit_make ( srn_mm_t * mm)

Definition at line 43 of file jit.c.

43 {
44 PANIC_IF_NULL(mm);
46 PANIC_IF_NULL(jit);
47 jit->builder = nullptr;
48 jit->llvm_jit = nullptr;
49 return jit;
50}
#define srn_mm_immortal_allocate(mm, T)
Definition interface.h:183
Here is the caller graph for this function:

◆ srn_jit_shutdown()

int srn_jit_shutdown ( srn_jit_t * jit)

Definition at line 74 of file jit.c.

74 {
75 PANIC_IF_NULL(jit);
76 // A jit that was made but never initialized has no LLJIT instance to
77 // dispose.
78 if (jit->llvm_jit == nullptr) {
79 return 0;
80 }
81 LLVMErrorRef err = LLVMOrcDisposeLLJIT(jit->llvm_jit);
82 if (err) {
83 return handle_error(err);
84 }
85 return 0;
86}
Here is the call graph for this function:
Here is the caller graph for this function: