Skip to content

Commit 1a0874d

Browse files
authored
Fixes for typed pointers. (#273)
* Fixed 'is_i128', making it check if the type is an intiger, before casting it to check the intiger bit width * Workaround for typed pointers
1 parent 9058b06 commit 1a0874d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,12 @@ fn struct_llfields<'a, 'tcx>(
614614
assert_eq!(offset.align_to(padding_align) + padding, target_offset);
615615
result.push(cx.type_padding_filler(padding, padding_align));
616616
}
617+
match field.ty.kind() {
618+
// This is a workaround for recursive types.
619+
rustc_middle::ty::TyKind::FnPtr(_, _) => result.push(cx.type_i8p()),
620+
_ => result.push(field.llvm_type(cx)),
621+
}
617622

618-
result.push(field.llvm_type(cx));
619623
offset = target_offset + field.size;
620624
prev_effective_align = effective_field_align;
621625
}

0 commit comments

Comments
 (0)