fix(emit): PHI predecessor for and/or operand that emits sub-CFG (issue 0078)

A string `==`/`!=` used as an operand of a short-circuit `and`/`or` emitted
invalid LLVM (`PHI node entries do not match predecessors!`). String compares
expand into their own memcmp sub-CFG during LLVM emission, so the operand
finishes in a later basic block (`str.merge`) than the one the IR block
started in. `fixupPhiNodes` wired the short-circuit merge PHI's incoming edge
to `block_map[ir_block]` (the block the IR block started as), recording a
stale predecessor (`%entry`/`%and.rhs.0`).

Fix: record the builder's actual insertion block after emitting each IR
block's instructions (`term_block_map`, via `LLVMGetInsertBlock`) and use it
as the PHI predecessor. General — corrects the incoming block for any operand
that emitted intermediate basic blocks (string `==`, value `match`, …), not
just string `==`.

Regression: examples/0045-basic-string-eq-short-circuit.sx (string `==` on
both sides of `and` and of `or`, plus a match-value + enum-payload `==` shape).
Fails (LLVM abort) pre-fix, passes after.
This commit is contained in:
agra
2026-06-04 02:00:13 +03:00
parent 295d95d51a
commit 7c1b90519f
6 changed files with 211 additions and 1 deletions

View File

@@ -124,6 +124,13 @@ pub const LLVMEmitter = struct {
// Maps (func_idx, block_idx) → LLVM BasicBlock
block_map: std.AutoHashMap(u64, c.LLVMBasicBlockRef),
// For each IR block, the LLVM block its terminator was actually emitted
// into. Usually equals `block_map[block]`, but an instruction can expand
// into its own sub-CFG (string `==`'s memcmp blocks, a value `match`'s
// arm blocks) and leave the builder in a later block, so the terminator —
// and therefore the PHI predecessor edge — lands there instead. Keyed the
// same way as `block_map`.
term_block_map: std.AutoHashMap(u64, c.LLVMBasicBlockRef),
// Cached LLVM types
cached_i1: c.LLVMTypeRef,
@@ -278,6 +285,7 @@ pub const LLVMEmitter = struct {
.func_map = std.AutoHashMap(u32, c.LLVMValueRef).init(alloc),
.global_map = std.AutoHashMap(u32, c.LLVMValueRef).init(alloc),
.block_map = std.AutoHashMap(u64, c.LLVMBasicBlockRef).init(alloc),
.term_block_map = std.AutoHashMap(u64, c.LLVMBasicBlockRef).init(alloc),
.pending_phis = std.ArrayList(PendingPhi).empty,
.cached_i1 = c.LLVMInt1TypeInContext(ctx),
.cached_i8 = c.LLVMInt8TypeInContext(ctx),
@@ -311,6 +319,7 @@ pub const LLVMEmitter = struct {
self.jni_slots.deinit();
self.global_map.deinit();
self.block_map.deinit();
self.term_block_map.deinit();
self.di_files.deinit();
var fsc_it = self.frame_str_cache.keyIterator();
while (fsc_it.next()) |k| self.alloc.free(k.*);
@@ -1270,6 +1279,7 @@ pub const LLVMEmitter = struct {
// Clear pending phis for this function
self.pending_phis.clearRetainingCapacity();
self.term_block_map.clearRetainingCapacity();
// Emit instructions for each block — use first_ref to sync ref numbering
for (func.blocks.items, 0..) |block, bi| {
@@ -1285,6 +1295,12 @@ pub const LLVMEmitter = struct {
_ = inst_i;
self.emitInst(&instruction, func_idx);
}
// The terminator may have landed in a later LLVM block than `bb`
// if an instruction in this IR block expanded into its own sub-CFG.
// Record where the builder actually is so PHI predecessors point at
// the block that holds the branch, not the block we started in.
self.term_block_map.put(block_key, c.LLVMGetInsertBlock(self.builder)) catch unreachable;
}
// Fixup PHI nodes: scan all blocks for branches that pass args
@@ -1300,7 +1316,10 @@ pub const LLVMEmitter = struct {
for (func.blocks.items, 0..) |block, bi| {
const src_key = makeBlockKey(func_idx, @intCast(bi));
const src_bb = self.block_map.get(src_key) orelse continue;
// Predecessor is the block the terminator was emitted into, which
// differs from `block_map[bi]` when an instruction expanded the
// block into a sub-CFG (string `==`, value `match`, …).
const src_bb = self.term_block_map.get(src_key) orelse continue;
for (block.insts.items) |instruction| {
switch (instruction.op) {