Skip to content

Commit d8305e6

Browse files
authored
Merge pull request rust-lang#107 from wasmerio/debug
Remove get_entry_basic_block and make get_first_basic_block return None when llvm returns nullptr.
2 parents 75530c0 + 4a46d42 commit d8305e6

File tree

3 files changed

+10
-32
lines changed

3 files changed

+10
-32
lines changed

examples/kaleidoscope/main.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use std::iter::Peekable;
2121
use std::str::Chars;
2222
use std::ops::DerefMut;
2323

24-
use self::inkwell::basic_block::BasicBlock;
2524
use self::inkwell::builder::Builder;
2625
use self::inkwell::context::Context;
2726
use self::inkwell::module::Module;
@@ -861,16 +860,15 @@ impl<'a> Compiler<'a> {
861860
self.fn_value_opt.unwrap()
862861
}
863862

864-
/// Cretes a new stack allocation instruction in the entry block of the function.
865-
fn create_entry_block_alloca(&self, name: &str, entry: Option<&BasicBlock>) -> PointerValue {
863+
/// Creates a new stack allocation instruction in the entry block of the function.
864+
fn create_entry_block_alloca(&self, name: &str) -> PointerValue {
866865
let builder = self.context.create_builder();
867866

868-
let owned_entry = self.fn_value().get_entry_basic_block();
869-
let entry = owned_entry.as_ref().or(entry).unwrap();
867+
let entry = self.fn_value().get_first_basic_block().unwrap();
870868

871869
match entry.get_first_instruction() {
872870
Some(first_instr) => builder.position_before(&first_instr),
873-
None => builder.position_at_end(entry)
871+
None => builder.position_at_end(&entry)
874872
}
875873

876874
builder.build_alloca(self.context.f64_type(), name)
@@ -899,7 +897,7 @@ impl<'a> Compiler<'a> {
899897
None => self.context.f64_type().const_float(0.)
900898
};
901899

902-
let alloca = self.create_entry_block_alloca(var_name, None);
900+
let alloca = self.create_entry_block_alloca(var_name);
903901

904902
self.builder.build_store(alloca, initial_val);
905903

@@ -1040,7 +1038,7 @@ impl<'a> Compiler<'a> {
10401038
Expr::For { ref var_name, ref start, ref end, ref step, ref body } => {
10411039
let parent = self.fn_value();
10421040

1043-
let start_alloca = self.create_entry_block_alloca(var_name, None);
1041+
let start_alloca = self.create_entry_block_alloca(var_name);
10441042
let start = self.compile_expr(start)?;
10451043

10461044
self.builder.build_store(start_alloca, start);
@@ -1132,7 +1130,7 @@ impl<'a> Compiler<'a> {
11321130

11331131
for (i, arg) in function.get_param_iter().enumerate() {
11341132
let arg_name = proto.args[i].as_str();
1135-
let alloca = self.create_entry_block_alloca(arg_name, Some(&entry));
1133+
let alloca = self.create_entry_block_alloca(arg_name);
11361134

11371135
self.builder.build_store(alloca, arg);
11381136

src/values/fn_value.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use llvm_sys::analysis::{LLVMVerifierFailureAction, LLVMVerifyFunction, LLVMViewFunctionCFG, LLVMViewFunctionCFGOnly};
2-
use llvm_sys::core::{LLVMIsAFunction, LLVMIsConstant, LLVMGetLinkage, LLVMGetPreviousFunction, LLVMGetNextFunction, LLVMGetParam, LLVMCountParams, LLVMGetLastParam, LLVMCountBasicBlocks, LLVMGetFirstParam, LLVMGetNextParam, LLVMGetBasicBlocks, LLVMAppendBasicBlock, LLVMDeleteFunction, LLVMGetLastBasicBlock, LLVMGetFirstBasicBlock, LLVMGetEntryBasicBlock, LLVMGetIntrinsicID, LLVMGetFunctionCallConv, LLVMSetFunctionCallConv, LLVMGetGC, LLVMSetGC, LLVMSetLinkage, LLVMSetParamAlignment, LLVMGetParams};
2+
use llvm_sys::core::{LLVMIsAFunction, LLVMIsConstant, LLVMGetLinkage, LLVMGetPreviousFunction, LLVMGetNextFunction, LLVMGetParam, LLVMCountParams, LLVMGetLastParam, LLVMCountBasicBlocks, LLVMGetFirstParam, LLVMGetNextParam, LLVMGetBasicBlocks, LLVMAppendBasicBlock, LLVMDeleteFunction, LLVMGetLastBasicBlock, LLVMGetFirstBasicBlock, LLVMGetIntrinsicID, LLVMGetFunctionCallConv, LLVMSetFunctionCallConv, LLVMGetGC, LLVMSetGC, LLVMSetLinkage, LLVMSetParamAlignment, LLVMGetParams};
33
#[llvm_versions(3.7..=latest)]
44
use llvm_sys::core::{LLVMGetPersonalityFn, LLVMSetPersonalityFn};
55
#[llvm_versions(3.9..=latest)]
@@ -123,22 +123,6 @@ impl FunctionValue {
123123
Some(BasicValueEnum::new(param))
124124
}
125125

126-
// REVIEW: Odd behavior, a function with no BBs returns a ptr
127-
// that isn't actually a basic_block and seems to get corrupted
128-
// Should check filed LLVM bugs - maybe just return None since
129-
// we can catch it with "LLVMIsABasicBlock"
130-
pub fn get_entry_basic_block(&self) -> Option<BasicBlock> {
131-
let bb = unsafe {
132-
LLVMGetEntryBasicBlock(self.as_value_ref())
133-
};
134-
135-
BasicBlock::new(bb)
136-
}
137-
138-
// REVIEW: Odd behavior, a function with no BBs returns a ptr
139-
// that isn't actually a basic_block and seems to get corrupted
140-
// Should check filed LLVM bugs - maybe just return None since
141-
// we can catch it with "LLVMIsABasicBlock"
142126
pub fn get_first_basic_block(&self) -> Option<BasicBlock> {
143127
let bb = unsafe {
144128
LLVMGetFirstBasicBlock(self.as_value_ref())

tests/all/test_basic_block.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ fn test_basic_block_ordering() {
1818

1919
let function = module.add_function("testing", fn_type, None);
2020

21-
// REVIEW: Possibly LLVM bug - gives a basic block ptr that isn't
22-
// actually a basic block instead of returning nullptr. Simplest solution
23-
// may be to just return None if LLVMIsABB doesn't pass
24-
// assert!(function.get_entry_basic_block().is_none());
25-
// assert!(function.get_first_basic_block().is_none());
21+
assert!(function.get_first_basic_block().is_none());
2622

2723
let basic_block = context.append_basic_block(&function, "entry");
2824
let basic_block4 = context.insert_basic_block_after(&basic_block, "block4");
@@ -58,7 +54,7 @@ fn test_basic_block_ordering() {
5854

5955
function.append_basic_block("block6");
6056

61-
let bb1 = function.get_entry_basic_block().unwrap();
57+
let bb1 = function.get_first_basic_block().unwrap();
6258
let bb4 = basic_block5.get_previous_basic_block().unwrap();
6359

6460
assert_eq!(bb1, basic_block3);

0 commit comments

Comments
 (0)