Skip to content

Commit 16a783f

Browse files
committed
cheri: reduce usage of AddressSpace::ZERO
1 parent 2ad5afb commit 16a783f

File tree

14 files changed

+49
-35
lines changed

14 files changed

+49
-35
lines changed

.cirrus.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ x_check_task:
6262
pull_master_script: git fetch origin master
6363
tidy_script: CC="clang" CXX="clang++" ./x test tidy
6464
check_script: CC="clang" CXX="clang++" ./x check
65+
tidy_script: CC="clang" CXX="clang++" ./x test
6566

6667
# -- End PR tasks

compiler/rustc_codegen_gcc/src/type_.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,6 @@ impl<'gcc, 'tcx> BaseTypeCodegenMethods for CodegenCx<'gcc, 'tcx> {
244244
}
245245
}
246246

247-
fn type_ptr(&self) -> Type<'gcc> {
248-
self.type_ptr_to(self.type_void())
249-
}
250247

251248
fn type_ptr_ext(&self, address_space: AddressSpace) -> Type<'gcc> {
252249
self.type_ptr_to_ext(self.type_void(), address_space)

compiler/rustc_codegen_gcc/src/type_of.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,8 @@ impl<'gcc, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
378378
let FnAbiGcc { return_type, arguments_type, is_c_variadic, .. } = fn_abi.gcc_type(self);
379379
self.context.new_function_pointer_type(None, return_type, &arguments_type, is_c_variadic)
380380
}
381+
382+
fn type_ptr(&self) -> Type<'gcc> {
383+
self.type_ptr_to(self.type_void())
384+
}
381385
}

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) unsafe fn codegen(
2828
tws => bug!("Unsupported target word size for int: {}", tws),
2929
};
3030
let i8 = cx.type_i8();
31-
let i8p = cx.type_ptr();
31+
let i8p = cx.type_ptr_ext(tcx.data_layout.default_address_space);
3232

3333
if kind == AllocatorKind::Default {
3434
for method in ALLOCATOR_METHODS {

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ pub(crate) mod autodiff;
66
pub(crate) mod gpu_offload;
77

88
use libc::{c_char, c_uint, size_t};
9-
use rustc_abi as abi;
10-
use rustc_abi::{Align, Size, WrappingRange};
9+
use rustc_abi::{self as abi, AddressSpace, Align, Size, WrappingRange};
1110
use rustc_codegen_ssa::MemFlags;
1211
use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, SynchronizationScope, TypeKind};
1312
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
@@ -35,7 +34,8 @@ use crate::attributes;
3534
use crate::common::Funclet;
3635
use crate::context::{CodegenCx, FullCx, GenericCx, SCx};
3736
use crate::llvm::{
38-
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, GEPNoWrapFlags, Metadata, True, PreserveCheriTags
37+
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, GEPNoWrapFlags, Metadata,
38+
PreserveCheriTags, True,
3939
};
4040
use crate::type_::Type;
4141
use crate::type_of::LayoutLlvmExt;
@@ -129,7 +129,12 @@ impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
129129
let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
130130
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
131131
// Cast to default addrspace if necessary
132-
llvm::LLVMBuildPointerCast(self.llbuilder, alloca, self.cx.type_ptr(), UNNAMED)
132+
llvm::LLVMBuildPointerCast(
133+
self.llbuilder,
134+
alloca,
135+
self.cx.type_ptr_ext(AddressSpace::ZERO),
136+
UNNAMED,
137+
)
133138
};
134139
if name != "" {
135140
let name = std::ffi::CString::new(name).unwrap();

compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ffi::CString;
22

33
use llvm::Linkage::*;
4-
use rustc_abi::Align;
4+
use rustc_abi::{AddressSpace, Align};
55
use rustc_codegen_ssa::back::write::CodegenContext;
66
use rustc_codegen_ssa::traits::BaseTypeCodegenMethods;
77

@@ -61,7 +61,7 @@ fn generate_at_one<'ll>(cx: &'ll SimpleCx<'_>) -> &'ll llvm::Value {
6161

6262
pub(crate) fn add_tgt_offload_entry<'ll>(cx: &'ll SimpleCx<'_>) -> &'ll llvm::Type {
6363
let offload_entry_ty = cx.type_named_struct("struct.__tgt_offload_entry");
64-
let tptr = cx.type_ptr();
64+
let tptr = cx.type_ptr_ext(AddressSpace::ZERO);
6565
let ti64 = cx.type_i64();
6666
let ti32 = cx.type_i32();
6767
let ti16 = cx.type_i16();
@@ -85,7 +85,7 @@ pub(crate) fn add_tgt_offload_entry<'ll>(cx: &'ll SimpleCx<'_>) -> &'ll llvm::Ty
8585

8686
fn gen_tgt_kernel_global<'ll>(cx: &'ll SimpleCx<'_>) {
8787
let kernel_arguments_ty = cx.type_named_struct("struct.__tgt_kernel_arguments");
88-
let tptr = cx.type_ptr();
88+
let tptr = cx.type_ptr_ext(AddressSpace::ZERO);
8989
let ti64 = cx.type_i64();
9090
let ti32 = cx.type_i32();
9191
let tarr = cx.type_array(ti32, 3);
@@ -126,7 +126,7 @@ fn gen_tgt_kernel_global<'ll>(cx: &'ll SimpleCx<'_>) {
126126
fn gen_tgt_data_mappers<'ll>(
127127
cx: &'ll SimpleCx<'_>,
128128
) -> (&'ll llvm::Value, &'ll llvm::Value, &'ll llvm::Value, &'ll llvm::Type) {
129-
let tptr = cx.type_ptr();
129+
let tptr = cx.type_ptr_ext(AddressSpace::ZERO);
130130
let ti64 = cx.type_i64();
131131
let ti32 = cx.type_i32();
132132

@@ -233,7 +233,7 @@ fn gen_define_handling<'ll>(
233233
let flags = cx.get_const_i32(0);
234234
let size = cx.get_const_i64(0);
235235
let data = cx.get_const_i64(0);
236-
let aux_addr = cx.const_null(cx.type_ptr());
236+
let aux_addr = cx.const_null(cx.type_ptr_ext(AddressSpace::ZERO));
237237
let elems = vec![reserved, version, kind, flags, region_id, llglobal, size, data, aux_addr];
238238

239239
let initializer = crate::common::named_struct(offload_entry_ty, &elems);
@@ -289,7 +289,7 @@ fn gen_call_handling<'ll>(
289289
o_types: &[&'ll llvm::Value],
290290
) {
291291
// %struct.__tgt_bin_desc = type { i32, ptr, ptr, ptr }
292-
let tptr = cx.type_ptr();
292+
let tptr = cx.type_ptr_ext(AddressSpace::ZERO);
293293
let ti32 = cx.type_i32();
294294
let tgt_bin_desc_ty = vec![ti32, tptr, tptr, tptr];
295295
let tgt_bin_desc = cx.type_named_struct("struct.__tgt_bin_desc");
@@ -321,7 +321,7 @@ fn gen_call_handling<'ll>(
321321

322322
let tgt_bin_desc_alloca = builder.direct_alloca(tgt_bin_desc, Align::EIGHT, "EmptyDesc");
323323

324-
let ty = cx.type_array(cx.type_ptr(), num_args);
324+
let ty = cx.type_array(cx.type_ptr_ext(AddressSpace::ZERO), num_args);
325325
// Baseptr are just the input pointer to the kernel, stored in a local alloca
326326
let a1 = builder.direct_alloca(ty, Align::EIGHT, ".offload_baseptrs");
327327
// Ptrs are the result of a gep into the baseptr, at least for our trivial types.
@@ -352,7 +352,7 @@ fn gen_call_handling<'ll>(
352352
unsafe { llvm::LLVMRustPositionBefore(builder.llbuilder, kernel_call) };
353353
builder.memset(tgt_bin_desc_alloca, cx.get_const_i8(0), cx.get_const_i64(32), Align::EIGHT);
354354

355-
let mapper_fn_ty = cx.type_func(&[cx.type_ptr()], cx.type_void());
355+
let mapper_fn_ty = cx.type_func(&[cx.type_ptr_ext(AddressSpace::ZERO)], cx.type_void());
356356
let register_lib_decl = declare_offload_fn(&cx, "__tgt_register_lib", mapper_fn_ty);
357357
let unregister_lib_decl = declare_offload_fn(&cx, "__tgt_unregister_lib", mapper_fn_ty);
358358
let init_ty = cx.type_func(&[], cx.type_void());
@@ -405,7 +405,7 @@ fn gen_call_handling<'ll>(
405405
num_args: u64,
406406
s_ident_t: &'ll Value,
407407
) {
408-
let nullptr = cx.const_null(cx.type_ptr());
408+
let nullptr = cx.const_null(cx.type_ptr_ext(AddressSpace::ZERO));
409409
let i64_max = cx.get_const_i64(u64::MAX);
410410
let num_args = cx.get_const_i32(num_args);
411411
let args =

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, ConstCodegenMethods};
1+
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, ConstCodegenMethods, LayoutTypeCodegenMethods};
22
use rustc_data_structures::fx::FxHashSet;
33
use rustc_hir::def_id::{DefId, LocalDefId};
44
use rustc_middle::mir;

compiler/rustc_codegen_llvm/src/type_.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::hash::{Hash, Hasher};
33
use std::{fmt, ptr};
44

55
use libc::c_uint;
6-
use rustc_abi::{AddressSpace, Align, Integer, Reg, Size};
6+
use rustc_abi::{AddressSpace, Align, HasDataLayout, Integer, Reg, Size};
77
use rustc_codegen_ssa::common::TypeKind;
88
use rustc_codegen_ssa::traits::*;
99
use rustc_data_structures::small_c_str::SmallCStr;
@@ -207,10 +207,6 @@ impl<'ll, CX: Borrow<SCx<'ll>>> BaseTypeCodegenMethods for GenericCx<'ll, CX> {
207207
unsafe { llvm::LLVMRustGetTypeKind(ty).to_generic() }
208208
}
209209

210-
fn type_ptr(&self) -> &'ll Type {
211-
self.type_ptr_ext(AddressSpace::ZERO)
212-
}
213-
214210
fn type_ptr_ext(&self, address_space: AddressSpace) -> &'ll Type {
215211
unsafe { llvm::LLVMPointerTypeInContext(self.llcx(), address_space.0) }
216212
}
@@ -258,6 +254,9 @@ impl Type {
258254
}
259255

260256
pub(crate) fn ptr_llcx(llcx: &llvm::Context) -> &Type {
257+
// FIXME(xdoardo): This call should use the default address space for the target; there's
258+
// currently no way to retrieve it from here. This function is currently used only when
259+
// targeting MSVC.
261260
unsafe { llvm::LLVMPointerTypeInContext(llcx, AddressSpace::ZERO.0) }
262261
}
263262
}
@@ -295,6 +294,9 @@ impl<'ll, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
295294
fn reg_backend_type(&self, ty: &Reg) -> &'ll Type {
296295
ty.llvm_type(self)
297296
}
297+
fn type_ptr(&self) -> Self::Type {
298+
self.type_ptr_ext(self.data_layout().default_address_space)
299+
}
298300
}
299301

300302
impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,8 +1115,9 @@ fn assume_scalar_range<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
11151115
let range = scalar.valid_range(bx.cx());
11161116
bx.assume_integer_range(imm, backend_ty, range);
11171117
}
1118-
abi::Primitive::Pointer(abi::AddressSpace::ZERO)
1119-
if !scalar.valid_range(bx.cx()).contains(0) =>
1118+
abi::Primitive::Pointer(address_space)
1119+
if address_space == bx.data_layout().default_address_space
1120+
&& !scalar.valid_range(bx.cx()).contains(0) =>
11201121
{
11211122
bx.assume_nonnull(imm);
11221123
}

compiler/rustc_codegen_ssa/src/traits/type_.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub trait BaseTypeCodegenMethods: BackendTypes {
2525
fn type_array(&self, ty: Self::Type, len: u64) -> Self::Type;
2626
fn type_func(&self, args: &[Self::Type], ret: Self::Type) -> Self::Type;
2727
fn type_kind(&self, ty: Self::Type) -> TypeKind;
28-
fn type_ptr(&self) -> Self::Type;
2928
fn type_ptr_ext(&self, address_space: AddressSpace) -> Self::Type;
3029
fn element_type(&self, ty: Self::Type) -> Self::Type;
3130

@@ -149,6 +148,9 @@ pub trait LayoutTypeCodegenMethods<'tcx>: BackendTypes {
149148
|| self.is_backend_immediate(layout)
150149
|| self.is_backend_scalar_pair(layout))
151150
}
151+
152+
153+
fn type_ptr(&self) -> Self::Type;
152154
}
153155

154156
// For backends that support CFI using type membership (i.e., testing whether a given pointer is

0 commit comments

Comments
 (0)