Skip to content

Commit 943152d

Browse files
committed
Workaround for typed pointers
1 parent 2c921b9 commit 943152d

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

crates/rustc_codegen_nvvm/src/builder.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,23 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
303303
if changed {
304304
v = transmute_llval(self.llbuilder, self.cx, v, new_ty);
305305
}
306+
// Get the return type.
307+
let sig = llvm::LLVMGetElementType(self.val_ty(self.llfn()));
308+
let return_ty = llvm::LLVMGetReturnType(sig);
309+
// Check if new_ty & return_ty are different pointers.
310+
// FIXME: get rid of this nonsense once we are past LLVM 7 and don't have
311+
// to suffer from typed pointers.
312+
if return_ty != new_ty
313+
&& llvm::LLVMRustGetTypeKind(return_ty) == llvm::TypeKind::Pointer
314+
&& llvm::LLVMRustGetTypeKind(new_ty) == llvm::TypeKind::Pointer
315+
{
316+
v = llvm::LLVMBuildBitCast(
317+
self.llbuilder,
318+
v,
319+
return_ty,
320+
c"return pointer adjust".as_ptr(),
321+
);
322+
}
306323
llvm::LLVMBuildRet(self.llbuilder, v);
307324
}
308325
}
@@ -923,9 +940,17 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
923940
}
924941

925942
/* Comparisons */
926-
fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
943+
fn icmp(&mut self, op: IntPredicate, mut lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
927944
trace!("Icmp lhs: `{:?}`, rhs: `{:?}`", lhs, rhs);
928-
945+
// FIXME(FractalFir): Once again, a bunch of nosense to make the LLVM typed pointers happy.
946+
// Get rid of this as soon as we move past typed pointers.
947+
let lhs_ty = self.val_ty(lhs);
948+
let rhs_ty = self.val_ty(rhs);
949+
if lhs_ty != rhs_ty {
950+
lhs = unsafe {
951+
llvm::LLVMBuildBitCast(self.llbuilder, lhs, rhs_ty, c"icmp_cast".as_ptr())
952+
};
953+
}
929954
unsafe {
930955
let op = llvm::IntPredicate::from_generic(op);
931956
llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, unnamed())

crates/rustc_codegen_nvvm/src/ty.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,14 @@ fn struct_llfields<'a, 'tcx>(
611611
assert_eq!(offset.align_to(padding_align) + padding, target_offset);
612612
result.push(cx.type_padding_filler(padding, padding_align));
613613
}
614+
match field.ty.kind() {
615+
// This is a workaround for recursive types.
616+
rustc_middle::ty::TyKind::FnPtr(_, _)
617+
| rustc_middle::ty::TyKind::RawPtr(_, _)
618+
| rustc_middle::ty::TyKind::Ref(_, _, _) => result.push(cx.type_i8p()),
619+
_ => result.push(field.llvm_type(cx)),
620+
}
614621

615-
result.push(field.llvm_type(cx));
616622
offset = target_offset + field.size;
617623
prev_effective_align = effective_field_align;
618624
}

0 commit comments

Comments
 (0)