Skip to content

Commit

Permalink
Fixes #342
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat-Lafon committed Oct 23, 2024
1 parent 2a9c5bb commit f506b7e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
25 changes: 19 additions & 6 deletions bril-rs/brillvm/src/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,28 +1294,29 @@ pub fn create_module_from_program<'a>(
}
});

(llvm_func, instrs, block, heap)
(llvm_func, instrs, block, heap, return_type)
},
)
.collect(); // Important to collect, can't be done lazily because we need all functions to be loaded in before a call instruction of a function is processed.

// Now actually build each function
funcs
.into_iter()
.for_each(|(llvm_func, instrs, mut block, heap)| {
.for_each(|(llvm_func, instrs, mut block, heap, return_type)| {
let mut last_instr = None;

// Maps labels to llvm blocks for jumps
let mut block_map = HashMap::new();

// If their are actually instructions, proceed
if !instrs.is_empty() {
builder.position_at_end(block);

// Maps labels to llvm blocks for jumps
let mut block_map = HashMap::new();
instrs.iter().for_each(|i| match i {
bril_rs::Code::Label { label, .. } => {
let new_block = block_map_get(context, llvm_func, &mut block_map, label);

// Check if wee need to insert a jump since all llvm blocks must be terminated
// Check if we need to insert a jump since all llvm blocks must be terminated
if !is_terminating_instr(&last_instr) {
builder
.build_unconditional_branch(block_map_get(
Expand Down Expand Up @@ -1354,7 +1355,19 @@ pub fn create_module_from_program<'a>(

// Make sure every function is terminated with a return if not already
if !is_terminating_instr(&last_instr) {
builder.build_return(None).unwrap();
if return_type.is_none() {
builder.build_return(None).unwrap();
} else {
// This block did not have a terminating instruction
// Returning void is ill-typed for this function
// This code should be unreachable in well-formed Bril
// Let's just arbitrarily jump to avoid needing to
// instantiate a valid return value.
assert!(!block_map.is_empty());
builder
.build_unconditional_branch(*block_map.values().next().unwrap())
.unwrap();
}
}
});

Expand Down
10 changes: 10 additions & 0 deletions test/interp/core/dead_block.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@abs(x: int): int {
ret x;
.label435:
}

@main() {
x : int = const 42;
y : int = call @abs x;
print y;
}
1 change: 1 addition & 0 deletions test/interp/core/dead_block.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42

0 comments on commit f506b7e

Please sign in to comment.