/* ** JIT context management for the extensible SQL parser. ** ** This file implements the JITContext lifecycle: creation, destruction, ** and snapshot integration using LLVM's OrcJIT (LLJIT) infrastructure. ** The actual LLVM IR generation lives in jit_codegen.c. ** ** OrcJIT replaces the deprecated MCJIT engine and provides: ** - Thread-safe contexts for concurrent compilation ** - Lazy compilation support (not yet used) ** - Better resource management and error handling ** ** When compiled without LLVM (LIME_NO_JIT defined), all functions ** degrade to stubs that return appropriate error codes or no-ops. */ #include "jit_context.h" #include "snapshot.h" #include #include #include #ifndef LIME_NO_JIT #include #include #include #include #include #include "jit_llvm_compat.h" /* ------------------------------------------------------------------ */ /* JIT context internal structure */ /* ------------------------------------------------------------------ */ struct JITContext { LLVMOrcLLJITRef lljit; /* OrcJIT LLJIT instance */ LLVMContextRef llvm_ctx; /* Bare LLVM context (for IR gen) */ LLVMOrcThreadSafeContextRef ts_ctx; /* Thread-safe wrapper of llvm_ctx */ void *parse_sequence_fn; /* Monolithic jit_parse_sequence fn */ void *find_shift_action_fn; /* Per-token jit_find_shift_action fn */ uint32_t nstates; /* Number of states */ uint32_t *state_hit_counts; /* Per-state hit counting */ uint32_t nstate_functions; /* Number of per-state functions */ JITStats stats; /* Compilation statistics */ }; /* One-time LLVM initialization flag */ static int llvm_initialized = 0; static JITStatus ensure_llvm_initialized(void) { if (llvm_initialized) return JIT_OK; LLVMInitializeNativeTarget(); LLVMInitializeNativeAsmPrinter(); LLVMInitializeNativeAsmParser(); llvm_initialized = 1; return JIT_OK; } /* Helper to convert LLVMErrorRef to a status code, disposing the error */ static JITStatus consume_llvm_error(LLVMErrorRef err, JITStatus fallback) { if (err == LLVMErrorSuccess) return JIT_OK; char *msg = LLVMGetErrorMessage(err); (void)msg; /* Could log this in debug builds */ LLVMDisposeErrorMessage(msg); return fallback; } /* ------------------------------------------------------------------ */ /* Public API (LLVM-enabled build) */ /* ------------------------------------------------------------------ */ JITStatus jit_create(JITContext **ctx_out) { if (ctx_out == NULL) return JIT_ERR_INVALID_ARG; *ctx_out = NULL; JITStatus st = ensure_llvm_initialized(); if (st != JIT_OK) return st; JITContext *ctx = calloc(1, sizeof(JITContext)); if (ctx == NULL) return JIT_ERR_INIT_FAILED; /* Create a ThreadSafeContext and fetch its internal LLVMContext. ** The compat shim papers over the LLVM 14 (ts_ctx-first) vs ** LLVM 15+ (externally-created context) API split; in both cases ** ts_ctx owns llvm_ctx, so only ts_ctx is disposed at teardown. */ if (!lime_jit_create_ts_ctx(&ctx->ts_ctx, &ctx->llvm_ctx)) { free(ctx); return JIT_ERR_INIT_FAILED; } /* Build the LLJIT instance */ LLVMOrcLLJITBuilderRef builder = LLVMOrcCreateLLJITBuilder(); LLVMErrorRef err = LLVMOrcCreateLLJIT(&ctx->lljit, builder); if (err != LLVMErrorSuccess) { consume_llvm_error(err, JIT_ERR_INIT_FAILED); LLVMOrcDisposeThreadSafeContext(ctx->ts_ctx); free(ctx); return JIT_ERR_INIT_FAILED; } ctx->stats.available = true; *ctx_out = ctx; return JIT_OK; } void jit_destroy(JITContext *ctx) { if (ctx == NULL) return; /* parse_sequence_fn is a function pointer inside JIT memory - don't free it */ free(ctx->state_hit_counts); /* Dispose the LLJIT instance (owns all JIT'd code and modules) */ if (ctx->lljit != NULL) { LLVMErrorRef err = LLVMOrcDisposeLLJIT(ctx->lljit); if (err != LLVMErrorSuccess) { consume_llvm_error(err, JIT_ERR_COMPILE_FAILED); } } /* Dispose the thread-safe context after LLJIT (it may reference it) */ if (ctx->ts_ctx != NULL) { LLVMOrcDisposeThreadSafeContext(ctx->ts_ctx); } free(ctx); } JITStats jit_get_stats(const JITContext *ctx) { if (ctx == NULL) { JITStats empty = { 0 }; return empty; } return ctx->stats; } bool jit_is_available(void) { return ensure_llvm_initialized() == JIT_OK; } /* Defined in jit_codegen.c -- OrcJIT-compatible entry point that ** generates IR into an externally-provided module rather than ** relying on the JITContext's internal module pointer. */ extern JITStatus jit_codegen_generate_into(LLVMContextRef llvm_ctx, LLVMModuleRef module, const ParserSnapshot *snap, JITStats *stats_out); JITStatus jit_compile_snapshot(JITContext *ctx, const ParserSnapshot *snap) { if (ctx == NULL || snap == NULL) return JIT_ERR_INVALID_ARG; if (snap->yy_action == NULL || snap->nstate == 0) return JIT_ERR_INVALID_ARG; /* Use the bare LLVM context (stored alongside ts_ctx) for IR generation */ LLVMContextRef llvm_ctx = ctx->llvm_ctx; /* Create a module in the thread-safe context */ LLVMModuleRef module = LLVMModuleCreateWithNameInContext("lime_jit", llvm_ctx); if (module == NULL) return JIT_ERR_CODEGEN_FAILED; /* Generate LLVM IR for all states using the codegen module. ** jit_codegen_generate needs access to ctx fields, so we temporarily ** stash the module pointer. The codegen uses ctx->llvm_ctx and ctx->module ** through its own copy of the JITContext struct layout. We use the ** OrcJIT-aware codegen entry point instead. */ JITStatus st = jit_codegen_generate_into(llvm_ctx, module, snap, &ctx->stats); if (st != JIT_OK) { LLVMDisposeModule(module); return st; } /* Verify the module */ char *error = NULL; if (LLVMVerifyModule(module, LLVMReturnStatusAction, &error)) { LLVMDisposeMessage(error); LLVMDisposeModule(module); return JIT_ERR_CODEGEN_FAILED; } LLVMDisposeMessage(error); /* Run optimization passes (compat shim picks PassBuilder on ** LLVM 16+ or legacy PassManagerBuilder on LLVM 14-15). ** Codegen sets stats.skip_opts when the grammar is large enough ** that LLVM's default pipeline does not terminate in ** reasonable bounds (see jit_codegen.c rationale). In that ** case we skip the pipeline and let LLVM's mandatory codegen ** lower the IR -- the IR is structured (every switch arm is a ** constant-return) so no opts are needed for correctness. */ if (!ctx->stats.skip_opts) { LIME_JIT_RUN_O2_PASSES(module); } /* Wrap module in a thread-safe module for OrcJIT submission */ LLVMOrcThreadSafeModuleRef ts_mod = LLVMOrcCreateNewThreadSafeModule(module, ctx->ts_ctx); /* Note: ts_mod now owns module; do not dispose module separately */ /* Add the module to the LLJIT main JITDylib */ LLVMOrcJITDylibRef jd = LLVMOrcLLJITGetMainJITDylib(ctx->lljit); LLVMErrorRef err = LLVMOrcLLJITAddLLVMIRModule(ctx->lljit, jd, ts_mod); if (err != LLVMErrorSuccess) { /* ts_mod is consumed even on error by some LLVM versions, ** but we attempt cleanup just in case */ return consume_llvm_error(err, JIT_ERR_COMPILE_FAILED); } /* Look up the monolithic parse function via OrcJIT. LimeJitAddress ** resolves to LLVMOrcExecutorAddress on LLVM 15+ and to ** LLVMOrcJITTargetAddress on LLVM 14 -- both are uint64_t. ** This lookup is conditional: codegen skips emitting ** jit_parse_sequence on very large grammars (see ctx->stats.skip_opts ** rationale), in which case the lookup would fail. The runtime ** engine never depends on it -- only the bench-only ** jit_parse_batch() API does. */ if (!ctx->stats.skip_opts) { LimeJitAddress addr = 0; err = LLVMOrcLLJITLookup(ctx->lljit, &addr, "jit_parse_sequence"); if (err != LLVMErrorSuccess || addr == 0) { if (err != LLVMErrorSuccess) { consume_llvm_error(err, JIT_ERR_LOOKUP_FAILED); } return JIT_ERR_LOOKUP_FAILED; } ctx->parse_sequence_fn = (void *)(uintptr_t)addr; } else { ctx->parse_sequence_fn = NULL; } ctx->nstates = snap->nstate; ctx->stats.states_compiled = snap->nstate; ctx->stats.states_total = snap->nstate; /* Look up the per-token find_shift_action function the runtime ** push parser uses when a snapshot has JIT code attached. This ** is what makes lime_jit_compile() actually accelerate ** parse_token (rather than only the synthetic batch path). */ LimeJitAddress fa_addr = 0; err = LLVMOrcLLJITLookup(ctx->lljit, &fa_addr, "jit_find_shift_action"); if (err == LLVMErrorSuccess && fa_addr != 0) { ctx->find_shift_action_fn = (void *)(uintptr_t)fa_addr; } else if (err != LLVMErrorSuccess) { /* The lookup failure isn't fatal -- the batch path still ** works, the runtime engine just won't use the JIT. Drain ** the error so the LLVM error chain doesn't leak. */ consume_llvm_error(err, JIT_OK); ctx->find_shift_action_fn = NULL; } /* Allocate per-state hit counters for warmup tracking */ ctx->state_hit_counts = calloc(snap->nstate, sizeof(uint32_t)); return JIT_OK; } JITShiftActionFn jit_get_shift_action(const JITContext *ctx, uint32_t state_id) { /* No longer used in monolithic JIT approach - per-state functions not generated */ (void)ctx; (void)state_id; return NULL; } JITFindShiftActionFn jit_get_find_shift_action(const JITContext *ctx) { if (ctx == NULL) return NULL; /* Cast through uintptr_t to silence -Wpedantic about ** object-vs-function pointers. */ union { void *ptr; JITFindShiftActionFn fn; } cast; cast.ptr = ctx->find_shift_action_fn; return cast.fn; } JITStatus jit_warmup(JITContext *ctx, const uint32_t *hot_states, uint32_t n) { if (ctx == NULL) return JIT_ERR_INVALID_ARG; if (hot_states == NULL || n == 0) return JIT_OK; /* Record hot state hits for future per-state compilation. ** Currently the monolithic function covers all states, so warmup ** just records which states are hot for potential future use ** (e.g., tiered compilation where only hot states get extra ** optimization). */ if (ctx->state_hit_counts == NULL || ctx->nstates == 0) { return JIT_OK; /* No snapshot compiled yet */ } for (uint32_t i = 0; i < n; i++) { if (hot_states[i] < ctx->nstates) { ctx->state_hit_counts[hot_states[i]]++; } } return JIT_OK; } #else /* LIME_NO_JIT -- stub implementations */ struct JITContext { JITStats stats; /* Mirror the real struct's slot so jit_attach_to_snapshot's ** ctx->find_shift_action_fn reference compiles in LIME_NO_JIT ** builds (the actual code path returns early from jit_create ** with JIT_ERR_NO_LLVM and never dereferences ctx). */ void *find_shift_action_fn; }; JITStatus jit_create(JITContext **ctx_out) { if (ctx_out == NULL) return JIT_ERR_INVALID_ARG; *ctx_out = NULL; return JIT_ERR_NO_LLVM; } void jit_destroy(JITContext *ctx) { free(ctx); } JITStatus jit_compile_snapshot(JITContext *ctx, const ParserSnapshot *snap) { (void)ctx; (void)snap; return JIT_ERR_NO_LLVM; } JITShiftActionFn jit_get_shift_action(const JITContext *ctx, uint32_t state_id) { (void)ctx; (void)state_id; return NULL; } JITFindShiftActionFn jit_get_find_shift_action(const JITContext *ctx) { (void)ctx; return NULL; } JITStats jit_get_stats(const JITContext *ctx) { (void)ctx; JITStats empty = { 0 }; return empty; } bool jit_is_available(void) { return false; } JITStatus jit_warmup(JITContext *ctx, const uint32_t *hot_states, uint32_t n) { (void)ctx; (void)hot_states; (void)n; return JIT_ERR_NO_LLVM; } #endif /* LIME_NO_JIT */ /* ------------------------------------------------------------------ */ /* Status string (always available) */ /* ------------------------------------------------------------------ */ const char *jit_status_string(JITStatus status) { switch (status) { case JIT_OK: return "OK"; case JIT_ERR_NO_LLVM: return "LLVM not available"; case JIT_ERR_INIT_FAILED: return "LLVM initialization failed"; case JIT_ERR_CODEGEN_FAILED: return "code generation failed"; case JIT_ERR_COMPILE_FAILED: return "JIT compilation failed"; case JIT_ERR_LOOKUP_FAILED: return "symbol lookup failed"; case JIT_ERR_INVALID_ARG: return "invalid argument"; case JIT_ERR_ALREADY_COMPILED: return "already compiled"; } return "unknown JIT error"; } /* ------------------------------------------------------------------ */ /* Snapshot integration helpers */ /* ------------------------------------------------------------------ */ JITStatus jit_attach_to_snapshot(ParserSnapshot *snap) { if (snap == NULL) return JIT_ERR_INVALID_ARG; if (snap->jit_ctx != NULL) return JIT_ERR_ALREADY_COMPILED; JITContext *ctx = NULL; JITStatus st = jit_create(&ctx); if (st != JIT_OK) return st; st = jit_compile_snapshot(ctx, snap); if (st != JIT_OK) { jit_destroy(ctx); return st; } snap->jit_ctx = ctx; /* Cache the per-token JIT entry point on the snapshot so the ** runtime's hot path can read it with a single load and skip ** the indirection through ctx->find_shift_action_fn on every ** token. Safe even when find_shift_action_fn is NULL (large- ** grammar codegen path); find_shift_action will fall through ** to the table interpreter. */ snap->jit_find_shift_fn = ctx->find_shift_action_fn; return JIT_OK; } void jit_detach_from_snapshot(ParserSnapshot *snap) { if (snap == NULL) return; if (snap->jit_ctx != NULL) { jit_destroy((JITContext *)snap->jit_ctx); snap->jit_ctx = NULL; snap->jit_find_shift_fn = NULL; } } /* ** Table-driven fallback for find_shift_action. ** ** This mirrors the logic in lempar.c's yy_find_shift_action() but ** operates on the snapshot's dynamically-allocated arrays instead ** of static globals. */ static uint16_t table_find_shift_action(const ParserSnapshot *snap, uint16_t stateno, uint16_t iLookAhead) { if (snap->yy_shift_ofst == NULL) return 0; int32_t ofst = snap->yy_shift_ofst[stateno]; uint32_t idx = (uint32_t)(ofst + (int32_t)iLookAhead); if (idx < snap->lookahead_count && snap->yy_lookahead[idx] == iLookAhead) { return snap->yy_action[idx]; } return snap->yy_default[stateno]; } uint16_t jit_find_shift_action(const ParserSnapshot *snap, uint16_t stateno, uint16_t iLookAhead) { if (snap == NULL) return 0; /* JIT path disabled for per-token lookups (too much overhead). ** Use jit_parse_batch() instead for batch processing. */ /* Fall back to table-driven lookup */ return table_find_shift_action(snap, stateno, iLookAhead); } void jit_parse_batch(const ParserSnapshot *snap, uint16_t *tokens, uint32_t count, uint16_t *state_inout) { if (snap == NULL || tokens == NULL || count == 0 || state_inout == NULL) { return; } #ifndef LIME_NO_JIT /* Try JIT batch path */ if (snap->jit_ctx != NULL) { JITContext *ctx = (JITContext *)snap->jit_ctx; if (ctx->parse_sequence_fn != NULL) { /* Call monolithic JIT function: void (*)(uint16_t*, uint32_t, uint16_t*) */ typedef void (*JITParseSequenceFn)(uint16_t *, uint32_t, uint16_t *); JITParseSequenceFn jit_fn = (JITParseSequenceFn)ctx->parse_sequence_fn; jit_fn(tokens, count, state_inout); return; } } #endif /* Fallback: interpreted loop */ uint16_t state = *state_inout; for (uint32_t i = 0; i < count; i++) { uint16_t action = table_find_shift_action(snap, state, tokens[i]); /* Update state based on action (simplified parse logic) */ if (action < 1000) { state = action % snap->nstate; } } *state_inout = state; } /* ------------------------------------------------------------------ */ /* Public lime_jit_* wrappers (declared in parser.h) */ /* ------------------------------------------------------------------ */ bool lime_jit_available(void) { return jit_is_available(); } int lime_jit_compile(ParserSnapshot *snap) { /* ** Architecture-conditional gate. On x86_64 with the unrolled- ** switch interpreter codegen, the C compiler-generated code ** typically *outperforms* the JIT on small grammars (Intel's ** branch predictor walks the static switch better than the ** indirect-call sequence the JIT produces). Measured on ** docs/BENCHMARK_RESULTS.md: -8 to -12 % on JSON / arith ** grammars. On aarch64 the relationship reverses: JIT wins. ** ** Honour an explicit user request via the LIME_JIT env var: ** LIME_JIT=1 -- force JIT regardless of architecture ** LIME_JIT=0 -- skip JIT regardless of architecture ** (unset) -- arch-conditional default below ** ** Fast-path skip: if architecture is x86_64 *and* grammar is ** below the size threshold where JIT codegen becomes ** worthwhile, skip the JIT compile and let the table-driven ** path run. The table-driven path on x86_64 is faster anyway. ** The threshold matches the IR_SIZE_THRESHOLD in ** src/jit_codegen.c::generate_find_shift_action_compact, since ** the compact codegen is the path that genuinely beats the ** interpreter on x86_64. */ const char *env = getenv("LIME_JIT"); if (env != NULL) { if (env[0] == '0') return 1; /* user-forced skip */ /* env[0] == '1' (or anything else): fall through, JIT */ } else { #if defined(__x86_64__) || defined(_M_X64) if (snap != NULL) { uint64_t ir_size_estimate = (uint64_t)snap->nstate * (uint64_t)snap->nterminal; if (ir_size_estimate < 500000) { /* On x86_64, small-grammar JIT loses to the ** interpreter; skip and let the table path run. */ return 1; } } #endif } JITStatus st = jit_attach_to_snapshot(snap); return (st == JIT_OK || st == JIT_ERR_ALREADY_COMPILED) ? 0 : (int)st; }