summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/fast-jit/jit_compiler.c
blob: 958d0e987b89d3e51d34b55c3a036e3e2f249475 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (C) 2021 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "jit_compiler.h"
#include "jit_ir.h"
#include "jit_codegen.h"
#include "jit_codecache.h"
#include "../interpreter/wasm.h"

typedef struct JitCompilerPass {
    /* Name of the pass */
    const char *name;
    /* The entry of the compiler pass */
    bool (*run)(JitCompContext *cc);
} JitCompilerPass;

/* clang-format off */
static JitCompilerPass compiler_passes[] = {
    { NULL, NULL },
#define REG_PASS(name) { #name, jit_pass_##name }
    REG_PASS(dump),
    REG_PASS(update_cfg),
    REG_PASS(frontend),
    REG_PASS(lower_cg),
    REG_PASS(regalloc),
    REG_PASS(codegen),
    REG_PASS(register_jitted_code)
#undef REG_PASS
};

/* Number of compiler passes */
#define COMPILER_PASS_NUM (sizeof(compiler_passes) / sizeof(compiler_passes[0]))

#if WASM_ENABLE_FAST_JIT_DUMP == 0
static const uint8 compiler_passes_without_dump[] = {
    3, 4, 5, 6, 7, 0
};
#else
static const uint8 compiler_passes_with_dump[] = {
    3, 2, 1, 4, 1, 5, 1, 6, 1, 7, 0
};
#endif

/* The exported global data of JIT compiler */
static JitGlobals jit_globals = {
#if WASM_ENABLE_FAST_JIT_DUMP == 0
    .passes = compiler_passes_without_dump,
#else
    .passes = compiler_passes_with_dump,
#endif
    .return_to_interp_from_jitted = NULL,
#if WASM_ENABLE_LAZY_JIT != 0
    .compile_fast_jit_and_then_call = NULL,
#endif
};
/* clang-format on */

static bool
apply_compiler_passes(JitCompContext *cc)
{
    const uint8 *p = jit_globals.passes;

    for (; *p; p++) {
        /* Set the pass NO */
        cc->cur_pass_no = p - jit_globals.passes;
        bh_assert(*p < COMPILER_PASS_NUM);

        if (!compiler_passes[*p].run(cc) || jit_get_last_error(cc)) {
            LOG_VERBOSE("JIT: compilation failed at pass[%td] = %s\n",
                        p - jit_globals.passes, compiler_passes[*p].name);
            return false;
        }
    }

    return true;
}

bool
jit_compiler_init(const JitCompOptions *options)
{
    uint32 code_cache_size = options->code_cache_size > 0
                                 ? options->code_cache_size
                                 : FAST_JIT_DEFAULT_CODE_CACHE_SIZE;

    LOG_VERBOSE("JIT: compiler init with code cache size: %u\n",
                code_cache_size);

    if (!jit_code_cache_init(code_cache_size))
        return false;

    if (!jit_codegen_init())
        goto fail1;

    return true;

fail1:
    jit_code_cache_destroy();
    return false;
}

void
jit_compiler_destroy()
{
    jit_codegen_destroy();

    jit_code_cache_destroy();
}

JitGlobals *
jit_compiler_get_jit_globals()
{
    return &jit_globals;
}

const char *
jit_compiler_get_pass_name(unsigned i)
{
    return i < COMPILER_PASS_NUM ? compiler_passes[i].name : NULL;
}

bool
jit_compiler_compile(WASMModule *module, uint32 func_idx)
{
    JitCompContext *cc = NULL;
    char *last_error;
    bool ret = false;
    uint32 i = func_idx - module->import_function_count;
    uint32 j = i % WASM_ORC_JIT_BACKEND_THREAD_NUM;

    /* Lock to avoid duplicated compilation by other threads */
    os_mutex_lock(&module->fast_jit_thread_locks[j]);

    if (jit_compiler_is_compiled(module, func_idx)) {
        /* Function has been compiled */
        os_mutex_unlock(&module->fast_jit_thread_locks[j]);
        return true;
    }

    /* Initialize the compilation context */
    if (!(cc = jit_calloc(sizeof(*cc)))) {
        goto fail;
    }

    if (!jit_cc_init(cc, 64)) {
        goto fail;
    }

    cc->cur_wasm_module = module;
    cc->cur_wasm_func = module->functions[i];
    cc->cur_wasm_func_idx = func_idx;
    cc->mem_space_unchanged = (!cc->cur_wasm_func->has_op_memory_grow
                               && !cc->cur_wasm_func->has_op_func_call)
                              || (!module->possible_memory_grow);

    /* Apply compiler passes */
    if (!apply_compiler_passes(cc) || jit_get_last_error(cc)) {
        last_error = jit_get_last_error(cc);

#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
        char *function_name = cc->cur_wasm_func->field_name;
        os_printf("fast jit compilation failed: %s (function_name=%s)\n",
                  last_error ? last_error : "unknown error", function_name);
#else
        os_printf("fast jit compilation failed: %s\n",
                  last_error ? last_error : "unknown error");
#endif

        goto fail;
    }

    ret = true;

fail:
    /* Destroy the compilation context */
    if (cc)
        jit_cc_delete(cc);

    os_mutex_unlock(&module->fast_jit_thread_locks[j]);

    return ret;
}

bool
jit_compiler_compile_all(WASMModule *module)
{
    uint32 i;

    for (i = 0; i < module->function_count; i++) {
        if (!jit_compiler_compile(module, module->import_function_count + i)) {
            return false;
        }
    }

    return true;
}

bool
jit_compiler_is_compiled(const WASMModule *module, uint32 func_idx)
{
    uint32 i = func_idx - module->import_function_count;

    bh_assert(func_idx >= module->import_function_count
              && func_idx
                     < module->import_function_count + module->function_count);

#if WASM_ENABLE_LAZY_JIT == 0
    return module->fast_jit_func_ptrs[i] ? true : false;
#else
    return module->fast_jit_func_ptrs[i]
                   != jit_globals.compile_fast_jit_and_then_call
               ? true
               : false;
#endif
}

#if WASM_ENABLE_LAZY_JIT != 0 && WASM_ENABLE_JIT != 0
bool
jit_compiler_set_call_to_llvm_jit(WASMModule *module, uint32 func_idx)
{
    uint32 i = func_idx - module->import_function_count;
    uint32 j = i % WASM_ORC_JIT_BACKEND_THREAD_NUM;
    WASMType *func_type = module->functions[i]->func_type;
    uint32 k =
        ((uint32)(uintptr_t)func_type >> 3) % WASM_ORC_JIT_BACKEND_THREAD_NUM;
    void *func_ptr = NULL;

    /* Compile code block of call_to_llvm_jit_from_fast_jit of
       this kind of function type if it hasn't been compiled */
    if (!(func_ptr = func_type->call_to_llvm_jit_from_fast_jit)) {
        os_mutex_lock(&module->fast_jit_thread_locks[k]);
        if (!(func_ptr = func_type->call_to_llvm_jit_from_fast_jit)) {
            if (!(func_ptr = func_type->call_to_llvm_jit_from_fast_jit =
                      jit_codegen_compile_call_to_llvm_jit(func_type))) {
                os_mutex_unlock(&module->fast_jit_thread_locks[k]);
                return false;
            }
        }
        os_mutex_unlock(&module->fast_jit_thread_locks[k]);
    }

    /* Switch current fast jit func ptr to the code block */
    os_mutex_lock(&module->fast_jit_thread_locks[j]);
    module->fast_jit_func_ptrs[i] = func_ptr;
    os_mutex_unlock(&module->fast_jit_thread_locks[j]);
    return true;
}

bool
jit_compiler_set_call_to_fast_jit(WASMModule *module, uint32 func_idx)
{
    void *func_ptr = NULL;

    func_ptr = jit_codegen_compile_call_to_fast_jit(module, func_idx);
    if (func_ptr) {
        uint32 i = func_idx - module->import_function_count;
        module->functions[i]->call_to_fast_jit_from_llvm_jit = func_ptr;
        jit_compiler_set_llvm_jit_func_ptr(module, func_idx, func_ptr);
    }

    return func_ptr ? true : false;
}

void
jit_compiler_set_llvm_jit_func_ptr(WASMModule *module, uint32 func_idx,
                                   void *func_ptr)
{
    WASMModuleInstance *instance;
    uint32 i = func_idx - module->import_function_count;

    os_mutex_lock(&module->instance_list_lock);

    module->func_ptrs[i] = func_ptr;

    instance = module->instance_list;
    while (instance) {
        if (instance->e->running_mode == Mode_Multi_Tier_JIT)
            instance->func_ptrs[func_idx] = func_ptr;
        instance = instance->e->next;
    }
    os_mutex_unlock(&module->instance_list_lock);
}
#endif /* end of WASM_ENABLE_LAZY_JIT != 0 && WASM_ENABLE_JIT != 0 */

int
jit_interp_switch_to_jitted(void *exec_env, JitInterpSwitchInfo *info,
                            uint32 func_idx, void *pc)
{
    return jit_codegen_interp_jitted_glue(exec_env, info, func_idx, pc);
}