Skip to content

Fix read_volatile intrinsic #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
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 @@ -1154,18 +1154,7 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {

impl<'ll> StaticBuilderMethods for Builder<'_, 'll, '_> {
fn get_static(&mut self, def_id: DefId) -> &'ll Value {
unsafe {
let mut g = self.cx.get_static(def_id);
let llty = self.val_ty(g);
let addrspace = AddressSpace(llvm::LLVMGetPointerAddressSpace(llty));
if addrspace != AddressSpace::DATA {
trace!("Remapping global address space of global {:?}", g);
let llty = llvm::LLVMGetElementType(llty);
let ty = self.type_ptr_to_ext(llty, AddressSpace::DATA);
g = llvm::LLVMBuildAddrSpaceCast(self.llbuilder, g, ty, unnamed());
}
g
}
self.cx.get_static(def_id)
}
}

Expand Down
17 changes: 15 additions & 2 deletions crates/rustc_codegen_nvvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_errors::DiagMessage;
use rustc_hash::FxHashMap;
use rustc_middle::dep_graph::DepContext;
use rustc_middle::ty::layout::{
FnAbiError, FnAbiOf, FnAbiRequest, HasTyCtxt, HasTypingEnv, LayoutError,
FnAbiError, FnAbiOf, FnAbiRequest, HasTyCtxt, HasTypingEnv, LayoutError, LayoutOf,
};
use rustc_middle::ty::layout::{FnAbiOfHelpers, LayoutOfHelpers};
use rustc_middle::ty::{Ty, TypeVisitableExt};
Expand All @@ -40,6 +40,10 @@ use rustc_target::callconv::FnAbi;
use rustc_target::spec::{HasTargetSpec, Target};
use tracing::{debug, trace};

/// "There is a total of 64 KB constant memory on a device."
/// <https://docs.nvidia.com/cuda/archive/12.8.1/pdf/CUDA_C_Best_Practices_Guide.pdf>
const CONSTANT_MEMORY_SIZE_LIMIT_BYTES: u64 = 64 * 1024;

pub(crate) struct CodegenCx<'ll, 'tcx> {
pub tcx: TyCtxt<'tcx>,

Expand Down Expand Up @@ -267,7 +271,16 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
}

if !is_mutable && self.type_is_freeze(ty) {
AddressSpace(4)
let layout = self.layout_of(ty);
if layout.size.bytes() > CONSTANT_MEMORY_SIZE_LIMIT_BYTES {
self.tcx.sess.dcx().warn(format!(
"static `{}` exceeds the constant-memory limit; placing in global memory (performance may be reduced)",
instance
));
AddressSpace(1)
} else {
AddressSpace(4)
}
} else {
AddressSpace::DATA
}
Expand Down
37 changes: 28 additions & 9 deletions crates/rustc_codegen_nvvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,22 +295,41 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
}
}
sym::volatile_load | sym::unaligned_volatile_load => {
let tp_ty = fn_args.type_at(0);
let mut ptr = args[0].immediate();
if let PassMode::Cast { cast: ty, .. } = &fn_abi.ret.mode {
ptr = self.pointercast(ptr, self.type_ptr_to(ty.llvm_type(self)));
}
let load = self.volatile_load(self.type_i1(), ptr);
// The `*const T` or `*mut T` operand.
let ptr_operand = &args[0];
let src_ptr_llval = ptr_operand.immediate();

// Determine the type T (the pointee type) and its LLVM representation.
// `ptr_operand.layout.ty` is the Rust type `*const T` (or `*mut T`). We
// need the layout of `T`.
let pointee_rust_ty =
ptr_operand
.layout
.ty
.builtin_deref(true)
.unwrap_or_else(|| {
span_bug!(span, "volatile_load input pointer is not a pointer type")
});
let layout_of_pointee = self.layout_of(pointee_rust_ty);
let llvm_ty_of_pointee = layout_of_pointee.llvm_type(self);

// Call volatile_load with the correct LLVM type of T. The
// `volatile_load` does a pointercast so we do not need to do it here.
let loaded_llval = self.volatile_load(llvm_ty_of_pointee, src_ptr_llval);

// Set alignment for the LLVM load instruction based on the alignment of
// `T`.
let align = if name == sym::unaligned_volatile_load {
1
} else {
self.align_of(tp_ty).bytes() as u32
layout_of_pointee.align.abi.bytes() as u32
};
unsafe {
llvm::LLVMSetAlignment(load, align);
llvm::LLVMSetAlignment(loaded_llval, align);
}

if !result.layout.is_zst() {
self.store_to_place(load, result.val);
self.store_to_place(loaded_llval, result.val);
}
return Ok(());
}
Expand Down
Loading