Skip to content

Commit a1c7207

Browse files
committed
use error message api
1 parent b48988d commit a1c7207

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

crates/rustc_codegen_nvvm/src/context.rs

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -294,26 +294,46 @@ 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
316-
));
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(span, format!("this static would cause total usage to exceed {} bytes", CONSTANT_MEMORY_SIZE_LIMIT_BYTES));
326+
diag.note(format!("current constant memory usage: {current_usage} bytes"));
327+
diag.note(format!("static size: {size_bytes} bytes"));
328+
diag.note(format!("would result in: {new_usage} bytes total"));
329+
330+
diag.help("move this or other statics to global memory using `#[cuda_std::address_space(global)]`");
331+
diag.help("reduce the total size of static data");
332+
diag.help("disable automatic constant memory placement by setting `.use_constant_memory_space(false)` on `CudaBuilder` in build.rs");
333+
334+
diag.emit();
335+
self.tcx.sess.dcx().abort_if_errors();
336+
unreachable!()
317337
}
318338

319339
// If successfully placed in constant memory: update cumulative usage
@@ -323,11 +343,20 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
323343
if new_usage > CONSTANT_MEMORY_WARNING_THRESHOLD_BYTES
324344
&& current_usage <= CONSTANT_MEMORY_WARNING_THRESHOLD_BYTES
325345
{
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
330-
));
346+
let def_id = instance.def_id();
347+
let span = self.tcx.def_span(def_id);
348+
let usage_percent = (new_usage as f64 / CONSTANT_MEMORY_SIZE_LIMIT_BYTES as f64) * 100.0;
349+
let mut diag = self.tcx.sess.dcx().struct_span_warn(
350+
span,
351+
format!(
352+
"constant memory usage is approaching the limit: {new_usage} / {} bytes ({usage_percent:.1}% used)",
353+
CONSTANT_MEMORY_SIZE_LIMIT_BYTES
354+
),
355+
);
356+
diag.span_label(span, "this placement brought you over 80% of constant memory capacity");
357+
diag.note(format!("only {} bytes of constant memory remain", CONSTANT_MEMORY_SIZE_LIMIT_BYTES - new_usage));
358+
diag.help("to prevent constant memory overflow, consider moving some statics to global memory using `#[cuda_std::address_space(global)]`");
359+
diag.emit();
331360
}
332361

333362
trace!(

0 commit comments

Comments
 (0)