Skip to content

Commit 6a3b08b

Browse files
committed
Spill large statics from constant to global memory
This isn't fully correct, as ideally we keep track of what we have put into constant memory and when it is filled up spill instdead of only spilling when a static is big. But, this is materially better than what is there (which is a runtime error). An argument can be made to just _always_ use global memory and we don't have to worry about getting the packing right. Fixes #208. See also the debugging and discussion in #216
1 parent e98fec3 commit 6a3b08b

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

crates/rustc_codegen_nvvm/src/context.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_errors::DiagMessage;
2222
use rustc_hash::FxHashMap;
2323
use rustc_middle::dep_graph::DepContext;
2424
use rustc_middle::ty::layout::{
25-
FnAbiError, FnAbiOf, FnAbiRequest, HasTyCtxt, HasTypingEnv, LayoutError,
25+
FnAbiError, FnAbiOf, FnAbiRequest, HasTyCtxt, HasTypingEnv, LayoutError, LayoutOf,
2626
};
2727
use rustc_middle::ty::layout::{FnAbiOfHelpers, LayoutOfHelpers};
2828
use rustc_middle::ty::{Ty, TypeVisitableExt};
@@ -40,6 +40,10 @@ use rustc_target::callconv::FnAbi;
4040
use rustc_target::spec::{HasTargetSpec, Target};
4141
use tracing::{debug, trace};
4242

43+
/// "There is a total of 64 KB constant memory on a device."
44+
/// <https://docs.nvidia.com/cuda/archive/12.8.1/pdf/CUDA_C_Best_Practices_Guide.pdf>
45+
const CONSTANT_MEMORY_SIZE_LIMIT_BYTES: u64 = 64 * 1024;
46+
4347
pub(crate) struct CodegenCx<'ll, 'tcx> {
4448
pub tcx: TyCtxt<'tcx>,
4549

@@ -267,7 +271,18 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
267271
}
268272

269273
if !is_mutable && self.type_is_freeze(ty) {
270-
AddressSpace(4)
274+
let layout = self.layout_of(ty);
275+
if layout.size.bytes() > CONSTANT_MEMORY_SIZE_LIMIT_BYTES {
276+
self.tcx.sess.dcx().warn(format!(
277+
"static `{}` exceeds the constant-memory limit; placing in global memory (performance may be reduced)",
278+
instance
279+
));
280+
// Global memory
281+
AddressSpace(1)
282+
} else {
283+
// Constant memory
284+
AddressSpace(4)
285+
}
271286
} else {
272287
AddressSpace::DATA
273288
}

0 commit comments

Comments
 (0)