From aa919e2786c29fbd3ce441337a89a4b42df797b4 Mon Sep 17 00:00:00 2001 From: tignear Date: Thu, 29 Oct 2026 00:14:54 +0900 Subject: [PATCH] perf(vpi): skip unused value format metadata Suppressed reads and callback deliveries still resolved signal values and metadata. Return without that work for vpiSuppressVal, retaining the owning thread check before accessing caller storage. Only narrow integer output needs signedness for sign extension; skip that lookup for scalar, string, vector and wider integer formats. Validation: signed integer/Z regression, all 68 VPI unit/API tests, all 10 ignored cocotb integration tests, all-target clippy with warnings denied, and workspace formatting. Change-Id: I78c2f11a5a286f8d1b38ef7ed5b6aa85aab930d7 --- crates/celox-vpi/src/lib.rs | 43 +++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/crates/celox-vpi/src/lib.rs b/crates/celox-vpi/src/lib.rs index 58899198424..e436f821580 100644 --- a/crates/celox-vpi/src/lib.rs +++ b/crates/celox-vpi/src/lib.rs @@ -1537,6 +1537,11 @@ unsafe fn write_snapshot_value( if value.is_null() || reference.is_null() { return; } + // Safety: checked non-null above; the callback owns its value record. + let value = unsafe { &mut *value }; + if value.format == VPI_SUPPRESS_VAL { + return; + } // Safety: the caller has selected this host and keeps the callback's // object handle live through dispatch. let handle = unsafe { &mut *reference }; @@ -1545,16 +1550,16 @@ unsafe fn write_snapshot_value( }; let metadata = borrow_cell(&host.runtime, |runtime| { let runtime = runtime.as_ref()?.state(); - runtime - .reflection() - .signal(id.root) - .and_then(|signal| Some((id.width(runtime)?, signal.signed))) + let width = id.width(runtime)?; + let signed = value.format == VPI_INT_VAL + && width > 0 + && width < 32 + && runtime.reflection().signal(id.root)?.signed; + Some((width, signed)) }); let Some((width, signed)) = metadata else { return; }; - // Safety: checked non-null above; the caller owns writable value storage. - let value = unsafe { &mut *value }; encode_snapshot_value(handle, value, snapshot, width, signed); } @@ -1574,12 +1579,16 @@ pub unsafe extern "C" fn vpi_get_value(reference: VpiHandle, value: *mut VpiValu let Some(host) = selected_host() else { return; }; + // Safety: selection above rejects foreign-thread calls before accessing + // caller storage, which is non-null and writable on the owning thread. + let value = unsafe { &mut *value }; + if value.format == VPI_SUPPRESS_VAL { + return; + } // Safety: the caller supplies a live handle; selection above rejects // foreign-thread/unbound calls before dereferencing it. let handle = unsafe { &mut *reference }; if let HandleKind::Constant(integer) = handle.kind { - // Safety: checked non-null above; caller owns the `VpiValue` allocation. - let value = unsafe { &mut *value }; if value.format == VPI_INT_VAL { value.value.integer = integer; } @@ -1591,14 +1600,16 @@ pub unsafe extern "C" fn vpi_get_value(reference: VpiHandle, value: *mut VpiValu let Some((snapshot, width)) = value_snapshot_at(&host, id) else { return; }; - let signed = borrow_cell(&host.runtime, |runtime| { - runtime - .as_ref() - .and_then(|runtime| runtime.state().reflection().signal(id.root)) - .is_some_and(|signal| signal.signed) - }); - // Safety: checked non-null above; caller owns the `VpiValue` allocation. - encode_snapshot_value(handle, unsafe { &mut *value }, &snapshot, width, signed); + let signed = value.format == VPI_INT_VAL + && width > 0 + && width < 32 + && borrow_cell(&host.runtime, |runtime| { + runtime + .as_ref() + .and_then(|runtime| runtime.state().reflection().signal(id.root)) + .is_some_and(|signal| signal.signed) + }); + encode_snapshot_value(handle, value, &snapshot, width, signed); } fn decode_digit(digit: u8, radix_bits: u32) -> Option<(u8, u8)> {