Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/rustc_codegen_nvvm/src/back.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,12 @@ pub extern "C" fn demangle_callback(
/// Compile a single module (in an nvvm context this means getting the llvm bitcode out of it)
pub(crate) unsafe fn codegen(
cgcx: &CodegenContext<NvvmCodegenBackend>,
dcx: DiagCtxtHandle<'_>,
module: ModuleCodegen<LlvmMod>,
config: &ModuleConfig,
) -> Result<CompiledModule, FatalError> {
let dcx = cgcx.create_dcx();
let dcx = dcx.handle();

// For NVVM, all the codegen we need to do is turn the llvm modules
// into llvm bitcode and write them to a tempdir. nvvm expects llvm
// bitcode as the modules to be added to the program. Then as the last step
Expand Down
13 changes: 1 addition & 12 deletions crates/rustc_codegen_nvvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,6 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
}
}

fn dynamic_alloca(&mut self, size: &'ll Value, align: Align) -> &'ll Value {
trace!("Dynamic Alloca `{:?}`", size);
unsafe {
let alloca =
llvm::LLVMBuildArrayAlloca(self.llbuilder, self.cx().type_i8(), size, UNNAMED);
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
// Cast to default addrspace if necessary
llvm::LLVMBuildPointerCast(self.llbuilder, alloca, self.cx().type_ptr(), UNNAMED)
}
}

fn load(&mut self, ty: &'ll Type, ptr: &'ll Value, align: Align) -> &'ll Value {
trace!("Load {ty:?} {:?}", ptr);
let ptr = self.pointercast(ptr, self.cx.type_ptr_to(ty));
Expand Down Expand Up @@ -901,7 +890,7 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
if kind == llvm::TypeKind::Pointer {
let element = self.element_type(ty);
let addrspace = llvm::LLVMGetPointerAddressSpace(ty);
let new_ty = self.type_ptr_to_ext(element, AddressSpace::DATA);
let new_ty = self.type_ptr_to_ext(element, AddressSpace::ZERO);
if addrspace != 0 {
trace!("injecting addrspace cast for `{:?}` to `{:?}`", ty, new_ty);
val = llvm::LLVMBuildAddrSpaceCast(self.llbuilder, val, new_ty, UNNAMED);
Expand Down
13 changes: 9 additions & 4 deletions crates/rustc_codegen_nvvm/src/const_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
let sc = self.const_bytes(s.as_bytes());
let sym = self.generate_local_symbol_name("str");
let g = self
.define_global(&sym[..], self.val_ty(sc), AddressSpace::DATA)
.define_global(&sym[..], self.val_ty(sc), AddressSpace::ZERO)
.unwrap_or_else(|| {
bug!("symbol `{}` is already defined", sym);
});
Expand Down Expand Up @@ -185,7 +185,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
format!("alloc_{hash:032x}").as_bytes(),
);
}
(value, AddressSpace::DATA)
(value, AddressSpace::ZERO)
}
}
GlobalAlloc::Function { instance, .. } => (
Expand All @@ -204,7 +204,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
.unwrap_memory();
let init = const_alloc_to_llvm(self, alloc, /*static*/ false);
let value = self.static_addr_of(init, alloc.inner().align, None);
(value, AddressSpace::DATA)
(value, AddressSpace::ZERO)
}
GlobalAlloc::Static(def_id) => {
assert!(self.tcx.is_static(def_id));
Expand All @@ -214,6 +214,11 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
unsafe { llvm::LLVMGetPointerAddressSpace(self.val_ty(val)) };
(val, AddressSpace(addrspace))
}
GlobalAlloc::TypeId { .. } => {
// Drop the provenance, the offset contains the bytes of the hash
let llval = self.const_usize(offset.bytes());
return unsafe { llvm::LLVMConstIntToPtr(llval, llty) };
}
};
let llval = unsafe {
llvm::LLVMConstInBoundsGEP2(
Expand All @@ -228,7 +233,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
if !matches!(layout.primitive(), Pointer(_)) {
unsafe { llvm::LLVMConstPtrToInt(llval, llty) }
} else {
if base_addr_space != AddressSpace::DATA {
if base_addr_space != AddressSpace::ZERO {
unsafe {
let element = llvm::LLVMGetElementType(llty);
llty = self.type_ptr_to_ext(element, base_addr_space);
Expand Down
4 changes: 2 additions & 2 deletions crates/rustc_codegen_nvvm/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub(crate) fn const_alloc_to_llvm<'ll>(
}
let mut llvals = Vec::with_capacity(alloc.provenance().ptrs().len() + 1);
let dl = cx.data_layout();
let pointer_size = dl.pointer_size.bytes() as usize;
let pointer_size = dl.pointer_size().bytes() as usize;

// Note: this function may call `inspect_with_uninit_and_ptr_outside_interpreter`,
// so `range` must be within the bounds of `alloc` and not contain or overlap a relocation.
Expand Down Expand Up @@ -253,7 +253,7 @@ impl<'ll> CodegenCx<'ll, '_> {
// TODO(RDambrosio016): replace this with latest rustc's handling when we use llvm 13
let name = self.generate_local_symbol_name(kind.unwrap_or("private"));
let gv = self
.define_global(&name[..], self.val_ty(cv), AddressSpace::DATA)
.define_global(&name[..], self.val_ty(cv), AddressSpace::ZERO)
.unwrap_or_else(|| bug!("symbol `{}` is already defined", name));
llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
llvm::LLVMSetInitializer(gv, cv);
Expand Down
3 changes: 2 additions & 1 deletion crates/rustc_codegen_nvvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
local_gen_sym_counter: Cell::new(0),
nvptx_data_layout: TargetDataLayout::parse_from_llvm_datalayout_string(
&target::target().data_layout,
AddressSpace::ZERO,
)
.unwrap_or_else(|err| tcx.sess.dcx().emit_fatal(err)),
nvptx_target: target::target(),
Expand Down Expand Up @@ -292,7 +293,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
}
}
} else {
AddressSpace::DATA
AddressSpace::ZERO
}
}

Expand Down
12 changes: 6 additions & 6 deletions crates/rustc_codegen_nvvm/src/debug_info/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
None => {
// This is a thin pointer. Create a regular pointer type and give it the correct name.
assert_eq!(
(data_layout.pointer_size, data_layout.pointer_align.abi),
(data_layout.pointer_size(), data_layout.pointer_align().abi),
cx.size_and_align_of(ptr_type),
"ptr_type={ptr_type}, pointee_type={pointee_type}",
);
Expand All @@ -176,8 +176,8 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
llvm::LLVMRustDIBuilderCreatePointerType(
DIB(cx),
pointee_type_di_node,
data_layout.pointer_size.bits(),
data_layout.pointer_align.abi.bits() as u32,
data_layout.pointer_size().bits(),
data_layout.pointer_align().abi.bits() as u32,
CString::new(ptr_type_debuginfo_name).unwrap().as_ptr(),
)
};
Expand Down Expand Up @@ -335,8 +335,8 @@ fn build_subroutine_type_di_node<'ll, 'tcx>(
let (size, align) = match fn_ty.kind() {
ty::FnDef(..) => (0, 1),
ty::FnPtr(..) => (
cx.tcx.data_layout.pointer_size.bits(),
cx.tcx.data_layout.pointer_align.abi.bits() as u32,
cx.tcx.data_layout.pointer_size().bits(),
cx.tcx.data_layout.pointer_align().abi.bits() as u32,
),
_ => unreachable!(),
};
Expand Down Expand Up @@ -527,7 +527,7 @@ fn recursion_marker_type_di_node<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIType {
DIB(cx),
name.as_c_char_ptr(),
name.len(),
cx.tcx.data_layout.pointer_size.bits(),
cx.tcx.data_layout.pointer_size().bits(),
dwarf_const::DW_ATE_unsigned,
)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rustc_codegen_nvvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
// For rusty ABIs, small aggregates are actually passed
// as `RegKind::Integer` (see `FnAbi::adjust_for_abi`),
// so we re-use that same threshold here.
layout.size <= self.data_layout().pointer_size * 2
layout.size <= self.data_layout().pointer_size() * 2
}
};

Expand Down
40 changes: 15 additions & 25 deletions crates/rustc_codegen_nvvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
use rustc_codegen_ssa::{
CodegenResults, CompiledModule, ModuleCodegen, TargetConfig,
back::{
lto::{LtoModuleCodegen, SerializedModule, ThinModule},
lto::{SerializedModule, ThinModule},
write::{CodegenContext, FatLtoInput, ModuleConfig, OngoingCodegen},
},
traits::{CodegenBackend, ExtraBackendMethods, WriteBackendMethods},
Expand All @@ -83,6 +83,7 @@ use rustc_session::{
use tracing::debug;

use std::ffi::CString;
use std::path::PathBuf;

// codegen dylib entrypoint
#[unsafe(no_mangle)]
Expand Down Expand Up @@ -275,19 +276,25 @@ impl WriteBackendMethods for NvvmCodegenBackend {
todo!();
}

fn run_fat_lto(
_: &CodegenContext<Self>,
_: Vec<FatLtoInput<Self>>,
_: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
) -> Result<LtoModuleCodegen<Self>, FatalError> {
fn run_and_optimize_fat_lto(
_cgcx: &CodegenContext<Self>,
_exported_symbols_for_lto: &[String],
_each_linked_rlib_for_lto: &[PathBuf],
_modules: Vec<FatLtoInput<Self>>,
_diff_fncs: Vec<AutoDiffItem>,
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
todo!()
}

fn run_thin_lto(
cgcx: &CodegenContext<Self>,
// FIXME: Limit LTO exports to these symbols
_exported_symbols_for_lto: &[String],
// FIXME: handle these? but only relevant for non-thin LTO?
_each_linked_rlib_for_lto: &[PathBuf],
modules: Vec<(String, Self::ThinBuffer)>,
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
) -> Result<(Vec<ThinModule<Self>>, Vec<WorkProduct>), FatalError> {
lto::run_thin(cgcx, modules, cached_modules)
}

Expand Down Expand Up @@ -317,11 +324,10 @@ impl WriteBackendMethods for NvvmCodegenBackend {

fn codegen(
cgcx: &CodegenContext<Self>,
diag_handler: DiagCtxtHandle<'_>,
module: ModuleCodegen<Self::Module>,
config: &ModuleConfig,
) -> Result<CompiledModule, FatalError> {
unsafe { back::codegen(cgcx, diag_handler, module, config) }
unsafe { back::codegen(cgcx, module, config) }
}

fn prepare_thin(
Expand All @@ -346,22 +352,6 @@ impl WriteBackendMethods for NvvmCodegenBackend {
)
}
}

fn optimize_fat(
_cgcx: &CodegenContext<Self>,
_llmod: &mut ModuleCodegen<Self::Module>,
) -> Result<(), FatalError> {
todo!()
}

fn autodiff(
_cgcx: &CodegenContext<Self>,
_module: &ModuleCodegen<Self::Module>,
_diff_fncs: Vec<AutoDiffItem>,
_config: &ModuleConfig,
) -> Result<(), FatalError> {
todo!()
}
}

impl ExtraBackendMethods for NvvmCodegenBackend {
Expand Down
8 changes: 4 additions & 4 deletions crates/rustc_codegen_nvvm/src/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use rustc_codegen_ssa::{
ModuleCodegen,
back::{
lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared},
lto::{SerializedModule, ThinModule, ThinShared},
write::CodegenContext,
},
traits::{ModuleBufferMethods, ThinBufferMethods},
Expand Down Expand Up @@ -106,7 +106,7 @@ pub(crate) fn run_thin(
_cgcx: &CodegenContext<NvvmCodegenBackend>,
modules: Vec<(String, ThinBuffer)>,
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
) -> Result<(Vec<LtoModuleCodegen<NvvmCodegenBackend>>, Vec<WorkProduct>), FatalError> {
) -> Result<(Vec<ThinModule<NvvmCodegenBackend>>, Vec<WorkProduct>), FatalError> {
debug!("Running thin LTO");
let mut thin_buffers = Vec::with_capacity(modules.len());
let mut module_names = Vec::with_capacity(modules.len() + cached_modules.len());
Expand Down Expand Up @@ -142,10 +142,10 @@ pub(crate) fn run_thin(

let mut opt_jobs = vec![];
for (module_index, _) in shared.module_names.iter().enumerate() {
opt_jobs.push(LtoModuleCodegen::Thin(ThinModule {
opt_jobs.push(ThinModule {
shared: shared.clone(),
idx: module_index,
}));
});
}

Ok((opt_jobs, vec![]))
Expand Down
16 changes: 8 additions & 8 deletions crates/rustc_codegen_nvvm/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'ll> CodegenCx<'ll, '_> {
pub(crate) fn voidp(&self) -> &'ll Type {
// llvm uses i8* for void ptrs, void* is invalid
let i8_ty = self.type_i8();
self.type_ptr_to_ext(i8_ty, AddressSpace::DATA)
self.type_ptr_to_ext(i8_ty, AddressSpace::ZERO)
}

pub(crate) fn type_named_struct(&self, name: &str) -> &'ll Type {
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<'ll> CodegenCx<'ll, '_> {
}

pub(crate) fn type_i8p(&self) -> &'ll Type {
self.type_i8p_ext(AddressSpace::DATA)
self.type_i8p_ext(AddressSpace::ZERO)
}

pub(crate) fn type_i8p_ext(&self, address_space: AddressSpace) -> &'ll Type {
Expand All @@ -116,7 +116,7 @@ impl<'ll> CodegenCx<'ll, '_> {
"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"
);

unsafe { llvm::LLVMPointerType(ty, AddressSpace::DATA.0) }
unsafe { llvm::LLVMPointerType(ty, AddressSpace::ZERO.0) }
}

pub(crate) fn type_ptr_to_ext(&self, ty: &'ll Type, address_space: AddressSpace) -> &'ll Type {
Expand Down Expand Up @@ -211,7 +211,7 @@ impl<'ll, 'tcx> BaseTypeCodegenMethods for CodegenCx<'ll, 'tcx> {
}

fn type_ptr(&self) -> Self::Type {
self.type_ptr_ext(AddressSpace::DATA)
self.type_ptr_ext(AddressSpace::ZERO)
}

fn type_ptr_ext(&self, address_space: AddressSpace) -> Self::Type {
Expand Down Expand Up @@ -434,7 +434,7 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyAndLayout<'tcx> {
{
(cx.type_pointee_for_align(align), address_space)
} else {
(cx.type_i8(), AddressSpace::DATA)
(cx.type_i8(), AddressSpace::ZERO)
};
cx.type_ptr_to_ext(pointee, address_space)
}
Expand Down Expand Up @@ -651,11 +651,11 @@ impl<'tcx> CodegenCx<'_, 'tcx> {
}

impl<'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
fn add_type_metadata(&self, _function: Self::Function, _typeid: String) {}
fn add_type_metadata(&self, _function: Self::Function, _typeid: &[u8]) {}

fn set_type_metadata(&self, _function: Self::Function, _typeid: String) {}
fn set_type_metadata(&self, _function: Self::Function, _typeid: &[u8]) {}

fn typeid_metadata(&self, _typeid: String) -> Option<Self::Metadata> {
fn typeid_metadata(&self, _typeid: &[u8]) -> Option<Self::Metadata> {
None
}

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2025-07-07"
channel = "nightly-2025-07-28"
components = ["clippy", "llvm-tools-preview", "rust-src", "rustc-dev", "rustfmt", "rust-analyzer"]
2 changes: 1 addition & 1 deletion tests/compiletests/ui/dis/target_feature_base_cc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $L__tmp1:
.loc 1 38 5
bra.uni $L__tmp2;
$L__tmp2:
.loc 2 2180 9
.loc 2 2198 9
mov.u32 %r1, 1124204544;
st.volatile.global.u32 [%rd2], %r1;
$L__tmp3:
Expand Down
Loading