🔧 Fix LLVM basic block naming collision (ChatGPT5)

- Add function name prefix to basic block labels to avoid cross-function conflicts
- blocks.rs: create_basic_blocks now takes fn_label parameter
- Format: 'Main_join_2_bb23' instead of just 'bb23'
- Add conservative fallback for missing terminators (jump to next or entry)
- This fixes 'Basic Block does not have terminator' verification error

Analysis insights:
- MIR output was correct (all blocks had terminators)
- Problem was LLVM-side block name collision between functions
- Classic case of 'Rust complexity' - simple C++ style fix works best
- Sometimes the simplest solution is the right one\!
This commit is contained in:
Selfhosting Dev
2025-09-12 04:54:09 +09:00
parent 187edfcaaf
commit 23fea9258f
3 changed files with 49 additions and 13 deletions

View File

@ -11,18 +11,19 @@ pub(in super::super) fn create_basic_blocks<'ctx>(
codegen: &CodegenContext<'ctx>,
llvm_func: FunctionValue<'ctx>,
func: &MirFunction,
fn_label: &str,
) -> (HashMap<BasicBlockId, BasicBlock<'ctx>>, BasicBlock<'ctx>) {
let mut bb_map: HashMap<BasicBlockId, BasicBlock> = HashMap::new();
let entry_first = func.entry_block;
let entry_bb = codegen
.context
.append_basic_block(llvm_func, &format!("bb{}", entry_first.as_u32()));
.append_basic_block(llvm_func, &format!("{}_bb{}", fn_label, entry_first.as_u32()));
bb_map.insert(entry_first, entry_bb);
for bid in func.block_ids() {
if bid == entry_first {
continue;
}
let name = format!("bb{}", bid.as_u32());
let name = format!("{}_bb{}", fn_label, bid.as_u32());
let bb = codegen.context.append_basic_block(llvm_func, &name);
bb_map.insert(bid, bb);
}