Skip to content

Commit e6179da

Browse files
committed
clippy fixes + added an assertion to ensure typed pointers are *NOT* used
1 parent eed781a commit e6179da

File tree

4 files changed

+37
-69
lines changed

4 files changed

+37
-69
lines changed

crates/rustc_codegen_nvvm/src/abi.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use libc::c_uint;
44
use rustc_abi::BackendRepr::Scalar;
55
use rustc_abi::CanonAbi;
66
use rustc_abi::Size;
7-
use rustc_abi::{HasDataLayout, Primitive, Reg, RegKind};
7+
use rustc_abi::{Primitive, Reg, RegKind};
88
use rustc_codegen_ssa::mir::operand::OperandRef;
99
use rustc_codegen_ssa::mir::operand::OperandValue;
1010
use rustc_codegen_ssa::mir::place::{PlaceRef, PlaceValue};
@@ -305,7 +305,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
305305
PassMode::Cast { cast, .. } => cast.llvm_type(cx),
306306
PassMode::Indirect { .. } => {
307307
idx += 1;
308-
llargument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
308+
llargument_tys.push(cx.type_i8p());
309309
cx.type_void()
310310
}
311311
};
@@ -353,7 +353,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
353353
attrs: _,
354354
meta_attrs: None,
355355
on_stack: _,
356-
} => cx.type_ptr_to(arg.memory_ty(cx)),
356+
} => cx.type_i8p(),
357357
};
358358
let (new, changed) = get_transformed_type(cx, llarg_ty);
359359
if changed {
@@ -564,7 +564,6 @@ impl<'tcx> AbiBuilderMethods for Builder<'_, '_, 'tcx> {
564564
}
565565

566566
pub(crate) trait ArgAbiExt<'ll, 'tcx> {
567-
fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
568567
fn store(
569568
&self,
570569
bx: &mut Builder<'_, 'll, 'tcx>,
@@ -580,12 +579,6 @@ pub(crate) trait ArgAbiExt<'ll, 'tcx> {
580579
}
581580

582581
impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
583-
/// Gets the LLVM type for a place of the original Rust type of
584-
/// this argument/return, i.e., the result of `type_of::type_of`.
585-
fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
586-
self.layout.llvm_type(cx)
587-
}
588-
589582
/// Stores a direct/indirect value described by this ArgAbi into a
590583
/// place for the original Rust type of this argument/return.
591584
/// Can be used for both storing formal arguments into Rust variables

crates/rustc_codegen_nvvm/src/builder.rs

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ptr;
44

55
use libc::{c_char, c_uint};
66
use rustc_abi as abi;
7-
use rustc_abi::{AddressSpace, Align, HasDataLayout, Size, TargetDataLayout, WrappingRange};
7+
use rustc_abi::{Align, HasDataLayout, Size, TargetDataLayout, WrappingRange};
88
use rustc_codegen_ssa::MemFlags;
99
use rustc_codegen_ssa::common::{AtomicRmwBinOp, IntPredicate, RealPredicate, TypeKind};
1010
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
@@ -720,7 +720,7 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
720720
flags: MemFlags,
721721
) -> &'ll Value {
722722
assert_eq!(self.cx.type_kind(self.cx.val_ty(ptr)), TypeKind::Pointer);
723-
let ptr = self.check_store(val, ptr);
723+
724724
let address_space = unsafe { llvm::LLVMGetPointerAddressSpace(self.val_ty(ptr)) };
725725
let store_pointer_ty = unsafe { llvm::LLVMPointerType(self.val_ty(val), address_space) };
726726

@@ -784,7 +784,7 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
784784
UNNAMED,
785785
);
786786
self.pointercast(
787-
ptr,
787+
res,
788788
self.type_i8p_ext(rustc_abi::AddressSpace(address_space)),
789789
)
790790
}
@@ -809,7 +809,7 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
809809
UNNAMED,
810810
);
811811
self.pointercast(
812-
ptr,
812+
res,
813813
self.type_i8p_ext(rustc_abi::AddressSpace(address_space)),
814814
)
815815
}
@@ -917,20 +917,20 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
917917

918918
fn bitcast(&mut self, mut val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
919919
trace!("Bitcast `{:?}` to ty `{:?}`", val, dest_ty);
920-
unsafe {
921-
let ty = self.val_ty(val);
922-
let kind = llvm::LLVMRustGetTypeKind(ty);
923-
if kind == llvm::TypeKind::Pointer {
924-
let element = self.element_type(ty);
925-
let addrspace = llvm::LLVMGetPointerAddressSpace(ty);
926-
let new_ty = self.type_ptr_to_ext(element, AddressSpace::ZERO);
927-
if addrspace != 0 {
928-
trace!("injecting addrspace cast for `{:?}` to `{:?}`", ty, new_ty);
929-
val = llvm::LLVMBuildAddrSpaceCast(self.llbuilder, val, new_ty, UNNAMED);
930-
}
920+
921+
let ty = self.val_ty(val);
922+
let kind = unsafe { llvm::LLVMRustGetTypeKind(ty) };
923+
924+
if kind == llvm::TypeKind::Pointer {
925+
let element = self.element_type(ty);
926+
let addrspace = unsafe { llvm::LLVMGetPointerAddressSpace(ty) };
927+
let new_ty = unsafe { llvm::LLVMPointerType(element, 0) };
928+
if addrspace != 0 {
929+
trace!("injecting addrspace cast for `{:?}` to `{:?}`", ty, new_ty);
930+
val = unsafe { llvm::LLVMBuildAddrSpaceCast(self.llbuilder, val, new_ty, UNNAMED) };
931931
}
932-
llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED)
933932
}
933+
unsafe { llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED) }
934934
}
935935

936936
fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
@@ -1198,7 +1198,7 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11981198
let res = self.atomic_op(
11991199
dst,
12001200
tuple,
1201-
|builder, dst, ty| {
1201+
|builder, dst, _| {
12021202
let address_space =
12031203
unsafe { llvm::LLVMGetPointerAddressSpace(builder.val_ty(dst)) };
12041204
let dst_ty = unsafe { llvm::LLVMPointerType(builder.val_ty(cmp), address_space) };
@@ -1216,7 +1216,7 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
12161216
)
12171217
}
12181218
},
1219-
|builder, dst, ty| {
1219+
|builder, dst, _| {
12201220
let dst = builder.pointercast(dst, unsafe {
12211221
llvm::LLVMPointerType(
12221222
builder.val_ty(cmp),
@@ -1698,20 +1698,6 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
16981698

16991699
fn noundef_metadata(&mut self, _load: &'ll Value) {}
17001700

1701-
fn check_store(&mut self, val: &'ll Value, ptr: &'ll Value) -> &'ll Value {
1702-
let dest_ptr_ty = self.cx.val_ty(ptr);
1703-
let stored_ty = self.cx.val_ty(val);
1704-
let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
1705-
1706-
assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
1707-
1708-
if dest_ptr_ty == stored_ptr_ty {
1709-
ptr
1710-
} else {
1711-
self.bitcast(ptr, stored_ptr_ty)
1712-
}
1713-
}
1714-
17151701
fn check_call<'b>(
17161702
&mut self,
17171703
typ: &str,

crates/rustc_codegen_nvvm/src/const_ty.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::llvm::{self, Bool, False, True, Type, Value};
2-
use crate::{consts::const_alloc_to_llvm, context::CodegenCx, ty::LayoutLlvmExt};
2+
use crate::{consts::const_alloc_to_llvm, context::CodegenCx};
33
use libc::c_uint;
44
use rustc_abi as abi;
55
use rustc_abi::Primitive::Pointer;
@@ -11,7 +11,6 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1111
use rustc_hashes::Hash128;
1212
use rustc_middle::bug;
1313
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
14-
use rustc_middle::ty::layout::LayoutOf;
1514
use tracing::trace;
1615

1716
impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
@@ -99,7 +98,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
9998
g
10099
});
101100
let len = s.len();
102-
let ty = self.type_ptr_to(self.layout_of(self.tcx.types.str_).llvm_type(self));
101+
let ty = self.type_i8p();
103102
let cs = unsafe { llvm::LLVMConstPointerCast(val, ty) };
104103
(cs, self.const_usize(len as u64))
105104
}

crates/rustc_codegen_nvvm/src/ty.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ impl Type {
5555

5656
impl<'ll> CodegenCx<'ll, '_> {
5757
pub(crate) fn voidp(&self) -> &'ll Type {
58-
// llvm uses i8* for void ptrs, void* is invalid
59-
let i8_ty = self.type_i8();
60-
self.type_ptr_to_ext(i8_ty, AddressSpace::ZERO)
58+
self.type_i8p()
6159
}
6260

6361
pub(crate) fn type_named_struct(&self, name: &str) -> &'ll Type {
@@ -116,11 +114,15 @@ impl<'ll> CodegenCx<'ll, '_> {
116114
"don't call ptr_to on function types, use ptr_to_llvm_type on FnAbi instead or explicitly specify an address space if it makes sense"
117115
);
118116

119-
unsafe { self.type_ptr_to_ext(ty, AddressSpace::ZERO) }
117+
self.type_ptr_to_ext(ty, AddressSpace::ZERO)
120118
}
121119
#[track_caller]
122120
pub(crate) fn type_ptr_to_ext(&self, ty: &'ll Type, address_space: AddressSpace) -> &'ll Type {
123-
//assert_eq!(ty,self.type_ix(8),"rustc_codegen_nvvm uses opaque pointers - specifying pointer type other than `i8` is not valid!");
121+
assert_eq!(
122+
ty,
123+
self.type_ix(8),
124+
"rustc_codegen_nvvm uses opaque pointers - specifying pointer type other than `i8` is not valid!"
125+
);
124126
unsafe { llvm::LLVMPointerType(self.type_ix(8), address_space.0) }
125127
}
126128

@@ -134,11 +136,6 @@ impl<'ll> CodegenCx<'ll, '_> {
134136
}
135137
}
136138

137-
pub(crate) fn type_pointee_for_align(&self, align: Align) -> &'ll Type {
138-
let ity = Integer::approximate_align(self, align);
139-
self.type_from_integer(ity)
140-
}
141-
142139
/// Return a LLVM type that has at most the required alignment,
143140
/// and exactly the required size, as a best-effort padding array.
144141
pub(crate) fn type_padding_filler(&self, size: Size, align: Align) -> &'ll Type {
@@ -341,12 +338,8 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyAndLayout<'tcx> {
341338
}
342339

343340
let llty = match *self.ty.kind() {
344-
ty::Ref(_, ty, _) | ty::RawPtr(ty, _) => {
345-
cx.type_ptr_to(cx.layout_of(ty).llvm_type(cx))
346-
}
347-
ty::Adt(def, _) if def.is_box() => {
348-
cx.type_ptr_to(cx.layout_of(self.ty.expect_boxed_ty()).llvm_type(cx))
349-
}
341+
ty::Ref(_, _, _) | ty::RawPtr(_, _) => cx.type_i8p(),
342+
ty::Adt(def, _) if def.is_box() => cx.type_i8p(),
350343
ty::FnPtr(sig, hdr) => {
351344
cx.fn_ptr_backend_type(cx.fn_abi_of_fn_ptr(sig.with(hdr), ty::List::empty()))
352345
}
@@ -427,17 +420,14 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyAndLayout<'tcx> {
427420
Float(f) => cx.type_from_float(f),
428421
Pointer(address_space) => {
429422
// If we know the alignment, pick something better than i8.
430-
let (pointee, address_space) = if let Some(PointeeInfo {
431-
safe: Some(_),
432-
align,
433-
..
434-
}) = self.pointee_info_at(cx, Size::ZERO)
423+
let address_space = if let Some(PointeeInfo { safe: Some(_), .. }) =
424+
self.pointee_info_at(cx, Size::ZERO)
435425
{
436-
(cx.type_pointee_for_align(align), address_space)
426+
address_space
437427
} else {
438-
(cx.type_i8(), AddressSpace::ZERO)
428+
AddressSpace::ZERO
439429
};
440-
cx.type_ptr_to_ext(pointee, address_space)
430+
cx.type_i8p_ext(address_space)
441431
}
442432
}
443433
}

0 commit comments

Comments
 (0)