From f442b310ef36d7965be1ae5cc7593d20219abc2f Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 19 Sep 2026 13:39:16 -0500 Subject: [PATCH] Add comments dicussing requirements for optimize=0 The goal of optimize=0 is both fast compilation and perfect debug info. We would like to do some simple optimizations but the possibility of breaking symbol mappings prevents it. --- src/core/bytecode.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/core/bytecode.c b/src/core/bytecode.c index 40f4fa65..7032b507 100644 --- a/src/core/bytecode.c +++ b/src/core/bytecode.c @@ -198,8 +198,13 @@ static BytecodeBB *janet_bytecode_basic_blocks(JanetFuncDef *def, int32_t *n_blo for (int32_t i = 0; i < words; i++) { #ifdef JANET_MSVC block_count += __popcnt(bitmap[i]); -#else +#elif defined(__GNUC__) block_count += __builtin_popcount(bitmap[i]); +#else + for (int j = 0; j < 32; j++) { + uint32_t mask = ((uint32_t)1 << j); + if (mask & bitmap[i]) block_count++; + } #endif } BytecodeBB *blocks = array_allocate(sizeof(BytecodeBB), block_count); @@ -1437,8 +1442,13 @@ void janet_bytecode_optimize(JanetFuncDef *def, int32_t level) { BytecodeBB *basic_blocks = janet_bytecode_basic_blocks(def, &nblocks); janet_assert(nblocks > 0, "no blocks"); if (level >= 1) { + /* TODO - allow limited value numbering at optimize = 0. + * Constant folding can currently remove named slots which can break debug info */ janet_bytecode_local_value_numbering(def, basic_blocks, nblocks); } + /* This will currently also break debug info, although less severely than + * value numbering. At optimize=0, we should pin all named slots but allow + * constant propogation for anonymous intermediate computations */ janet_bytecode_movopt_full(def, basic_blocks, nblocks); janet_free(basic_blocks); }