feat(llvm): Add optional latch→header connection in LoopForm

- Added NYASH_LOOPFORM_LATCH2HEADER environment variable
- When enabled, latch block jumps back to header (completing the loop)
- When disabled (default), latch remains unreachable (safe mode)
- Preserves header predecessor count stability in default mode

This allows gradual testing of full LoopForm loop structure.
This commit is contained in:
Selfhosting Dev
2025-09-12 16:55:25 +09:00
parent 65497bac04
commit da51f0e51b
2 changed files with 13 additions and 2 deletions

View File

@ -134,9 +134,17 @@ pub fn lower_while_loopform<'ctx, 'b>(
registry.insert(header_bid, (lf.dispatch, tag_phi, payload_phi, lf.latch));
body_to_header.insert(body_bb, header_bid);
// Latch: keep unreachable for now (avoid adding a new predecessor to header)
// Latch: optionally jump back to header (gated), otherwise keep unreachable to avoid header pred増
codegen.builder.position_at_end(lf.latch);
if std::env::var("NYASH_LOOPFORM_LATCH2HEADER").ok().as_deref() == Some("1") {
codegen
.builder
.build_unconditional_branch(header_llbb)
.map_err(|e| e.to_string())
.unwrap();
} else {
let _ = codegen.builder.build_unreachable();
}
// Exit: to original after
codegen.builder.position_at_end(lf.exit);
codegen

View File

@ -500,6 +500,9 @@ impl LLVMCompiler {
}
}
}
// Note: LoopForm latch→header adds a new LLVM pred not represented in MIR.
// Header PHI normalization for this extra pred will be implemented later
// using a LoopForm-aware finalize that does not rely on MIR inputs.
}
}
// Finalize function: ensure every basic block is closed with a terminator.