Skip to content

Commit ce73bab

Browse files
authored
Merge pull request #726 from rust-lang/fix/const_undef
Fix const_undef
2 parents 15d23bd + 1926f60 commit ce73bab

File tree

3 files changed

+9
-50
lines changed

3 files changed

+9
-50
lines changed

src/builder.rs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
538538
}
539539

540540
fn ret(&mut self, mut value: RValue<'gcc>) {
541-
if self.structs_as_pointer.borrow().contains(&value) {
542-
// NOTE: hack to workaround a limitation of the rustc API: see comment on
543-
// CodegenCx.structs_as_pointer
544-
value = value.dereference(self.location).to_rvalue();
545-
}
546541
let expected_return_type = self.current_func().get_return_type();
547542
if !expected_return_type.is_compatible_with(value.get_type()) {
548543
// NOTE: due to opaque pointers now being used, we need to cast here.
@@ -1119,13 +1114,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
11191114
// TODO(antoyo)
11201115
}
11211116

1122-
fn store(&mut self, mut val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
1123-
if self.structs_as_pointer.borrow().contains(&val) {
1124-
// NOTE: hack to workaround a limitation of the rustc API: see comment on
1125-
// CodegenCx.structs_as_pointer
1126-
val = val.dereference(self.location).to_rvalue();
1127-
}
1128-
1117+
fn store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
11291118
self.store_with_flags(val, ptr, align, MemFlags::empty())
11301119
}
11311120

@@ -1508,16 +1497,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
15081497
element.get_address(self.location)
15091498
} else if value_type.dyncast_vector().is_some() {
15101499
panic!();
1511-
} else if let Some(pointer_type) = value_type.get_pointee() {
1512-
if let Some(struct_type) = pointer_type.is_struct() {
1513-
// NOTE: hack to workaround a limitation of the rustc API: see comment on
1514-
// CodegenCx.structs_as_pointer
1515-
aggregate_value
1516-
.dereference_field(self.location, struct_type.get_field(idx as i32))
1517-
.to_rvalue()
1518-
} else {
1519-
panic!("Unexpected type {:?}", value_type);
1520-
}
15211500
} else if let Some(struct_type) = value_type.is_struct() {
15221501
aggregate_value
15231502
.access_field(self.location, struct_type.get_field(idx as i32))
@@ -1537,21 +1516,18 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
15371516
assert_eq!(idx as usize as u64, idx);
15381517
let value_type = aggregate_value.get_type();
15391518

1519+
let new_val = self.current_func().new_local(None, value_type, "aggregate_value");
1520+
self.block.add_assignment(None, new_val, aggregate_value);
1521+
15401522
let lvalue = if value_type.dyncast_array().is_some() {
15411523
let index = self
15421524
.context
15431525
.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1544-
self.context.new_array_access(self.location, aggregate_value, index)
1526+
self.context.new_array_access(self.location, new_val, index)
15451527
} else if value_type.dyncast_vector().is_some() {
15461528
panic!();
1547-
} else if let Some(pointer_type) = value_type.get_pointee() {
1548-
if let Some(struct_type) = pointer_type.is_struct() {
1549-
// NOTE: hack to workaround a limitation of the rustc API: see comment on
1550-
// CodegenCx.structs_as_pointer
1551-
aggregate_value.dereference_field(self.location, struct_type.get_field(idx as i32))
1552-
} else {
1553-
panic!("Unexpected type {:?}", value_type);
1554-
}
1529+
} else if let Some(struct_type) = value_type.is_struct() {
1530+
new_val.access_field(None, struct_type.get_field(idx as i32))
15551531
} else {
15561532
panic!("Unexpected type {:?}", value_type);
15571533
};
@@ -1568,7 +1544,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
15681544

15691545
self.llbb().add_assignment(self.location, lvalue, value);
15701546

1571-
aggregate_value
1547+
new_val.to_rvalue()
15721548
}
15731549

15741550
fn set_personality_fn(&mut self, _personality: Function<'gcc>) {

src/common.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,7 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
117117

118118
fn const_undef(&self, typ: Type<'gcc>) -> RValue<'gcc> {
119119
let local = self.current_func.borrow().expect("func").new_local(None, typ, "undefined");
120-
if typ.is_struct().is_some() {
121-
// NOTE: hack to workaround a limitation of the rustc API: see comment on
122-
// CodegenCx.structs_as_pointer
123-
let pointer = local.get_address(None);
124-
self.structs_as_pointer.borrow_mut().insert(pointer);
125-
pointer
126-
} else {
127-
local.to_rvalue()
128-
}
120+
local.to_rvalue()
129121
}
130122

131123
fn const_poison(&self, typ: Type<'gcc>) -> RValue<'gcc> {

src/context.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,6 @@ pub struct CodegenCx<'gcc, 'tcx> {
124124

125125
pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
126126

127-
/// NOTE: a hack is used because the rustc API is not suitable to libgccjit and as such,
128-
/// `const_undef()` returns struct as pointer so that they can later be assigned a value (in
129-
/// e.g. Builder::insert_value).
130-
/// As such, this set remembers which of these pointers were returned by this function so that
131-
/// they can be dereferenced later.
132-
/// FIXME(antoyo): fix the rustc API to avoid having this hack.
133-
pub structs_as_pointer: RefCell<FxHashSet<RValue<'gcc>>>,
134-
135127
#[cfg(feature = "master")]
136128
pub cleanup_blocks: RefCell<FxHashSet<Block<'gcc>>>,
137129
/// The alignment of a u128/i128 type.
@@ -304,7 +296,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
304296
#[cfg(feature = "master")]
305297
rust_try_fn: Cell::new(None),
306298
pointee_infos: Default::default(),
307-
structs_as_pointer: Default::default(),
308299
#[cfg(feature = "master")]
309300
cleanup_blocks: Default::default(),
310301
};

0 commit comments

Comments
 (0)