Skip to content

Commit 981980a

Browse files
committed
New ME_DSL_JIT_COMPILER envvar to select compiler backend for JIT
1 parent 257f92a commit 981980a

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

README_DEVELOPERS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ The public/runtime-stable DSL JIT controls remain documented in [README.md](READ
124124
- `ME_SIMD_MATH_BACKEND=auto|sleef|accelerate|scalar`: Force the SIMD math backend selection used by `src/functions-simd.c` for benchmarking and debugging. Default: `auto` (prefers SLEEF when available, otherwise falls back to Accelerate on macOS when enabled, then the existing scalar fallback).
125125
- The SIMD math benchmarks print backend-aware columns. For `accelerate` and `scalar`, do not interpret the `ME_SIMD_ULP_1` / `ME_SIMD_ULP_3_5` labels as distinct math implementations.
126126
- `ME_DSL_WHILE_MAX_ITERS=<n>`: Override the runtime safety cap for DSL `while` loops.
127+
- `ME_DSL_JIT_COMPILER=tcc|cc`: Force the DSL JIT compiler backend at compile time. When unset, the parsed DSL compiler selection is used.
127128
- `ME_DSL_JIT_MATH_BRIDGE=0|1`: Enable or disable runtime math-bridge lowering globally. Default: `1`.
128129
- `ME_DSL_JIT_SCALAR_MATH_BRIDGE=0|1`: Enable scalar math-bridge lowering for the `cc` backend. Default: `0`.
129130
- `ME_DSL_JIT_VEC_MATH=0|1`: Enable vector math lowering where available. Default: `1`.

src/dsl_compile_support.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ static bool dsl_source_has_fp_pragma(const char *source) {
103103
return false;
104104
}
105105

106+
static bool dsl_env_jit_compiler_override(me_dsl_compiler *out_compiler) {
107+
const char *env = getenv("ME_DSL_JIT_COMPILER");
108+
109+
if (!out_compiler || !env || env[0] == '\0') {
110+
return false;
111+
}
112+
if (strcmp(env, "tcc") == 0) {
113+
*out_compiler = ME_DSL_COMPILER_LIBTCC;
114+
return true;
115+
}
116+
if (strcmp(env, "cc") == 0) {
117+
*out_compiler = ME_DSL_COMPILER_CC;
118+
return true;
119+
}
120+
return false;
121+
}
122+
106123
static void dsl_compiled_block_free(me_dsl_compiled_block *block);
107124

108125
void dsl_compiled_expr_free(me_dsl_compiled_expr *expr) {
@@ -266,6 +283,7 @@ me_dsl_compiled_program *dsl_compiled_program_alloc(const me_dsl_program *parsed
266283
program->fp_mode = dsl_default_fp_mode_from_env();
267284
}
268285
program->compiler = parsed->compiler;
286+
(void)dsl_env_jit_compiler_override(&program->compiler);
269287
program->compile_ndims = compile_ndims;
270288
dsl_var_table_init(&program->vars);
271289
program->idx_ndim = -1;

tests/test_dsl_jit_ir.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <stdlib.h>
77
#include <string.h>
88

9+
#include "../src/dsl_compile_internal.h"
910
#include "../src/dsl_jit_ir.h"
1011
#include "../src/dsl_parser.h"
1112
#include "minctest.h"
@@ -437,6 +438,71 @@ static int test_parser_pragmas(void) {
437438
return 0;
438439
}
439440

441+
static int test_env_jit_compiler_override(void) {
442+
printf("\n=== JIT IR Test 6b: env compiler override ===\n");
443+
444+
const char *src =
445+
"# me:compiler=tcc\n"
446+
"def kernel(x):\n"
447+
" return x\n";
448+
me_dsl_error error;
449+
me_dsl_program *parsed = me_dsl_parse(src, &error);
450+
me_dsl_compiled_program *compiled = NULL;
451+
452+
if (!parsed) {
453+
printf(" FAILED: parse error for env compiler override source\n");
454+
return 1;
455+
}
456+
457+
if (setenv("ME_DSL_JIT_COMPILER", "cc", 1) != 0) {
458+
printf(" FAILED: could not set ME_DSL_JIT_COMPILER=cc\n");
459+
me_dsl_program_free(parsed);
460+
return 1;
461+
}
462+
compiled = dsl_compiled_program_alloc(parsed, src, 1, NULL, 0);
463+
if (!compiled) {
464+
printf(" FAILED: could not allocate compiled program for cc override\n");
465+
unsetenv("ME_DSL_JIT_COMPILER");
466+
me_dsl_program_free(parsed);
467+
return 1;
468+
}
469+
if (compiled->compiler != ME_DSL_COMPILER_CC) {
470+
printf(" FAILED: env compiler override did not force cc backend\n");
471+
dsl_compiled_program_free(compiled);
472+
unsetenv("ME_DSL_JIT_COMPILER");
473+
me_dsl_program_free(parsed);
474+
return 1;
475+
}
476+
dsl_compiled_program_free(compiled);
477+
478+
if (setenv("ME_DSL_JIT_COMPILER", "tcc", 1) != 0) {
479+
printf(" FAILED: could not set ME_DSL_JIT_COMPILER=tcc\n");
480+
unsetenv("ME_DSL_JIT_COMPILER");
481+
me_dsl_program_free(parsed);
482+
return 1;
483+
}
484+
compiled = dsl_compiled_program_alloc(parsed, src, 1, NULL, 0);
485+
if (!compiled) {
486+
printf(" FAILED: could not allocate compiled program for tcc override\n");
487+
unsetenv("ME_DSL_JIT_COMPILER");
488+
me_dsl_program_free(parsed);
489+
return 1;
490+
}
491+
if (compiled->compiler != ME_DSL_COMPILER_LIBTCC) {
492+
printf(" FAILED: env compiler override did not force tcc backend\n");
493+
dsl_compiled_program_free(compiled);
494+
unsetenv("ME_DSL_JIT_COMPILER");
495+
me_dsl_program_free(parsed);
496+
return 1;
497+
}
498+
499+
dsl_compiled_program_free(compiled);
500+
unsetenv("ME_DSL_JIT_COMPILER");
501+
me_dsl_program_free(parsed);
502+
printf(" PASSED\n");
503+
return 0;
504+
}
505+
440506
static int test_ir_fingerprint_includes_fp_mode(void) {
441507
printf("\n=== JIT IR Test 6: fingerprint includes fp mode ===\n");
442508

@@ -593,6 +659,7 @@ int main(void) {
593659
fail |= test_ir_rejects_unsupported_statements();
594660
fail |= test_ir_fingerprint_is_deterministic();
595661
fail |= test_parser_pragmas();
662+
fail |= test_env_jit_compiler_override();
596663
fail |= test_ir_fingerprint_includes_fp_mode();
597664
fail |= test_ir_accepts_while_loops();
598665
fail |= test_ir_accepts_reassignment_dtype_coercion();

0 commit comments

Comments
 (0)