Skip to content

witness: add optional device backing metadata to RowMajorMatrix#33

Open
hero78119 wants to merge 3 commits intomainfrom
feat/witness_gpu_buffer
Open

witness: add optional device backing metadata to RowMajorMatrix#33
hero78119 wants to merge 3 commits intomainfrom
feat/witness_gpu_buffer

Conversation

@hero78119
Copy link
Copy Markdown
Collaborator

@hero78119 hero78119 commented Apr 13, 2026

PR still WIP

Copilot Reviewer Directive (keep this section)

When Copilot reviews this PR, apply .github/copilot-instructions.md strictly.

  • Perform review only; do not commit/push/propose code changes.
  • Prefer inline comments on changed lines for each actionable finding.
  • If inline comments are unavailable, use [severity] path:line (symbol) format.
  • Prioritize soundness, performance, and architecture risks over style.
  • Output order: findings by severity, then open questions, then brief summary.
  • If PR description is empty or missing key context, report PR metadata: description as a finding.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds optional device-backing metadata to witness::RowMajorMatrix so callers can attach an opaque device storage handle plus a layout tag.

Changes:

  • Add device_backing: Option<…> to RowMajorMatrix and initialize it to None in constructors.
  • Introduce DeviceMatrixLayout and internal DeviceMatrixBacking.
  • Add public APIs to set/clear/query/downcast the device backing.

Comment thread crates/witness/src/lib.rs
Comment on lines +187 to +196
pub fn set_device_backing<D: Any + Send + Sync + 'static>(
&mut self,
storage: D,
layout: DeviceMatrixLayout,
) {
self.device_backing = Some(DeviceMatrixBacking {
storage: Arc::new(storage),
layout,
});
}
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

device_backing can become silently stale: RowMajorMatrix exposes multiple mutable access paths (e.g., DerefMut and the various *_mut iterators/padding methods) but setting a device backing doesn’t establish/maintain any invariant that it matches the current host-side inner.values. If callers set a device backing and then mutate the matrix, downstream consumers may read an out-of-sync device buffer. Consider defining the invariant explicitly and either (a) automatically invalidating device_backing on any host mutation (clear on deref_mut / mut iterators / padding / pad_to_height), or (b) restricting mutable access when a backing is present and forcing callers to clear/update it first.

Copilot uses AI. Check for mistakes.
Comment thread crates/witness/src/lib.rs
Comment on lines +210 to +214
pub fn device_backing_ref<D: Any + 'static>(&self) -> Option<&D> {
self.device_backing
.as_ref()
.and_then(|backing| backing.storage.downcast_ref::<D>())
}
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

device_backing_ref accepts D: Any + 'static, but set_device_backing only permits storing D: Any + Send + Sync + 'static. This mismatch makes it easy to ask for a non-Send + Sync type that can never be present, and is inconsistent with the setter’s contract. Consider mirroring the bounds (D: Any + Send + Sync + 'static) or documenting the intended usage to avoid confusing API calls that always return None.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread crates/witness/src/lib.rs
Comment on lines 43 to +46
is_padded: bool,
padding_strategy: InstancePaddingStrategy,
device_backing: Option<DeviceMatrixBacking>,
}
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RowMajorMatrix derives Clone, and the new device_backing: Option<DeviceMatrixBacking> will be cloned as well (cloning the inner Arc). Since inner is also cloned, the cloned matrix will share the same device backing metadata as the original even though the host-side values are a deep copy, which is very likely to make the backing inconsistent/wrong. Consider implementing a manual Clone for RowMajorMatrix that resets device_backing to None (or otherwise ensures the backing corresponds to the cloned contents).

Copilot uses AI. Check for mistakes.
Comment thread crates/witness/src/lib.rs
Comment on lines +198 to +204
pub fn clear_device_backing(&mut self) {
self.device_backing = None;
}

pub fn has_device_backing(&self) -> bool {
self.device_backing.is_some()
}
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s no invalidation mechanism tying device_backing to the current host-side contents/dimensions: the matrix can be mutated via existing APIs (e.g., DerefMut, iter_mut, pad_to_height, padding_by_strategy, etc.) without clearing or updating device_backing. That can leave stale device metadata attached and risks later code using the wrong device buffer/layout. Consider clearing device_backing in mutating methods (or tracking a version/dirty flag and requiring callers to refresh the backing when dirty).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants