@@ -294,26 +294,54 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
294294
295295 // Check if this single static is too large for constant memory
296296 if size_bytes > CONSTANT_MEMORY_SIZE_LIMIT_BYTES {
297- self . tcx . sess . dcx ( ) . warn ( format ! (
298- "static `{instance}` is {size_bytes} bytes, exceeds the constant memory limit of {} bytes; placing in global memory (performance may be reduced)" ,
299- CONSTANT_MEMORY_SIZE_LIMIT_BYTES
300- ) ) ;
297+ let def_id = instance. def_id ( ) ;
298+ let span = self . tcx . def_span ( def_id) ;
299+ let mut diag = self . tcx . sess . dcx ( ) . struct_span_warn (
300+ span,
301+ format ! (
302+ "static `{instance}` is {size_bytes} bytes, exceeds the constant memory limit of {} bytes" ,
303+ CONSTANT_MEMORY_SIZE_LIMIT_BYTES
304+ ) ,
305+ ) ;
306+ diag. span_label ( span, "static exceeds constant memory limit" ) ;
307+ diag. note ( "placing in global memory (performance may be reduced)" ) ;
308+ diag. help ( "use `#[cuda_std::address_space(global)]` to explicitly place this static in global memory" ) ;
309+ diag. emit ( ) ;
301310 return AddressSpace ( 1 ) ;
302311 }
303312
304313 // Check if adding this static would exceed the cumulative limit
305314 if new_usage > CONSTANT_MEMORY_SIZE_LIMIT_BYTES {
306- self . fatal ( format ! (
307- "cannot place static `{instance}` ({size_bytes} bytes) in constant memory: \
308- cumulative constant memory usage would be {new_usage} bytes, exceeding the {} byte limit. \
309- Current usage: {current_usage} bytes. \
310- \n \
311- = help: use `#[cuda_std::address_space(global)]` on less frequently accessed statics\n \
312- = help: reducing static data size\n \
313- = help: disabling automatic constant memory placement by calling `.use_constant_memory_space(false)` \
314- on your `CudaBuilder` in build.rs",
315- CONSTANT_MEMORY_SIZE_LIMIT_BYTES
315+ let def_id = instance. def_id ( ) ;
316+ let span = self . tcx . def_span ( def_id) ;
317+ let mut diag = self . tcx . sess . dcx ( ) . struct_span_err (
318+ span,
319+ format ! (
320+ "cannot place static `{instance}` ({size_bytes} bytes) in constant memory: \
321+ cumulative constant memory usage would be {new_usage} bytes, exceeding the {} byte limit",
322+ CONSTANT_MEMORY_SIZE_LIMIT_BYTES
323+ ) ,
324+ ) ;
325+ diag. span_label (
326+ span,
327+ format ! (
328+ "this static would cause total usage to exceed {} bytes" ,
329+ CONSTANT_MEMORY_SIZE_LIMIT_BYTES
330+ ) ,
331+ ) ;
332+ diag. note ( format ! (
333+ "current constant memory usage: {current_usage} bytes"
316334 ) ) ;
335+ diag. note ( format ! ( "static size: {size_bytes} bytes" ) ) ;
336+ diag. note ( format ! ( "would result in: {new_usage} bytes total" ) ) ;
337+
338+ diag. help ( "move this or other statics to global memory using `#[cuda_std::address_space(global)]`" ) ;
339+ diag. help ( "reduce the total size of static data" ) ;
340+ diag. help ( "disable automatic constant memory placement by setting `.use_constant_memory_space(false)` on `CudaBuilder` in build.rs" ) ;
341+
342+ diag. emit ( ) ;
343+ self . tcx . sess . dcx ( ) . abort_if_errors ( ) ;
344+ unreachable ! ( )
317345 }
318346
319347 // If successfully placed in constant memory: update cumulative usage
@@ -323,11 +351,27 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
323351 if new_usage > CONSTANT_MEMORY_WARNING_THRESHOLD_BYTES
324352 && current_usage <= CONSTANT_MEMORY_WARNING_THRESHOLD_BYTES
325353 {
326- self . tcx . sess . dcx ( ) . warn ( format ! (
327- "constant memory usage is approaching the limit: {new_usage} / {} bytes ({:.1}% used)" ,
328- CONSTANT_MEMORY_SIZE_LIMIT_BYTES ,
329- ( new_usage as f64 / CONSTANT_MEMORY_SIZE_LIMIT_BYTES as f64 ) * 100.0
354+ let def_id = instance. def_id ( ) ;
355+ let span = self . tcx . def_span ( def_id) ;
356+ let usage_percent =
357+ ( new_usage as f64 / CONSTANT_MEMORY_SIZE_LIMIT_BYTES as f64 ) * 100.0 ;
358+ let mut diag = self . tcx . sess . dcx ( ) . struct_span_warn (
359+ span,
360+ format ! (
361+ "constant memory usage is approaching the limit: {new_usage} / {} bytes ({usage_percent:.1}% used)" ,
362+ CONSTANT_MEMORY_SIZE_LIMIT_BYTES
363+ ) ,
364+ ) ;
365+ diag. span_label (
366+ span,
367+ "this placement brought you over 80% of constant memory capacity" ,
368+ ) ;
369+ diag. note ( format ! (
370+ "only {} bytes of constant memory remain" ,
371+ CONSTANT_MEMORY_SIZE_LIMIT_BYTES - new_usage
330372 ) ) ;
373+ diag. help ( "to prevent constant memory overflow, consider moving some statics to global memory using `#[cuda_std::address_space(global)]`" ) ;
374+ diag. emit ( ) ;
331375 }
332376
333377 trace ! (
0 commit comments