Repository navigation
Conversation
The mxm and mxmT_masked CPU kernels append entries to LIL rows directly without updating the 'values' counter, and the Coo -> Lil conversion has the same flaw. Downstream consumers size their output from the counter (e.g. Lil -> Coo in Matrix::read), so a stale counter leads to out-of-bounds writes and segfaults when reading back the result. Maintain the counter in all three places, following the pattern already used by the kron, eadd, emult and transpose kernels. Add regression tests that read the mxm/mxmT results back through MemView.
TVector::set_fill_value invalidated every storage format, so the next read rebuilt an empty representation and silently discarded all explicit entries. Only dense representations materialize the fill value; sparse ones (Coo, Dok) store explicit entries and are unaffected. Invalidate just the dense formats, converting the data to COO first when no sparse representation is valid.
TMatrix::set_fill_value invalidated every storage format, so the next read rebuilt a representation from scratch and silently lost all explicit entries. All matrix formats are sparse and never materialize the fill value in storage (it is applied on read), so no representation needs to be invalidated. Regression test: matrix.set_fill_value_preserves_data verifies entries survive the call and round trips through Lil/Dok/Coo/Csr.
gsvgit
approved these changes
Nov 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Short description
Fixes two silent data-corruption bugs in the C++ core. Both were found while
building the Python test suite for the graph-algorithms work (PR #282), which
exercised code paths the existing C++ tests never covered.
1. Out-of-bounds write (heap corruption / segfault) after
mxm,mxmT_masked, or COO→LIL conversion.CpuLil<T>tracks the total number of stored entries in avaluescounter.The kernels
Algo_mxm_cpuandAlgo_mxmT_masked_cpuand thecpu_coo_to_lilconverter append entries to LIL rows without updating thiscounter. The Lil→Coo and Lil→Csr conversions size their output buffers from
the counter (
cpu_coo_resize(lil->values, ...),cpu_csr_resize(n_rows, lil->values, ...)), so a stale (too small) countermakes them write past the end of the allocation.
Repro:
exec_mxm(R, A, B, ...)followed by any read-back ofR(
R->read(...),to_lists()from Python, ...) crashes with a segfault.2.
set_fill_valuesilently discards all explicit entries.Both
TVector<T>::set_fill_valueandTMatrix<T>::set_fill_valuecalledm_storage.invalidate(), which marks every storage format invalid. The nextread rebuilds a representation from scratch — starting empty — so all data is
lost:
Only dense representations actually materialize the fill value; sparse ones
(Lil/Dok/Coo/Csr) store explicit entries and are unaffected by it, so
invalidating them loses data for no reason.
List of changes
src/cpu/cpu_mxm.hpp,src/cpu/cpu_mxmT_masked.hpp,src/cpu/cpu_format_coo.hpp— keep the LILvaluescounter in sync withthe actual row contents (increment per appended entry, assign before
return), the same pattern already used by the kron, eadd, emult, transpose,
and vxm kernels. Side effect:
kron(computes the counter arithmetically)and
transpose(copies it) now also produce correct counters when fedaffected inputs.
src/core/tvector.hpp,src/core/tdecoration.hpp—TVector::set_fill_valuenow invalidates only the dense representations(
CpuDense, plusAccDensein OpenCL builds), converting the data to COOfirst when no sparse representation is valid. Adds per-format
invalidate(F)toTDecorationStorage.src/core/tmatrix.hpp—TMatrix::set_fill_valueno longer invalidates anyrepresentation: all matrix formats are sparse and the fill value is applied
on read, so nothing can be lost.
tests/test_mxm.cpp,tests/test_mxmT.cpp,tests/test_vector.cpp,tests/test_matrix.cpp— four regression tests (see Testing strategy).Type of changes
Changes aspect
.githubscripts addedTesting strategy
Four regression tests, each failing (crash or wrong value) before the fix and
passing after:
mxm.read_after_exec— strict nnz check after reading back anmxmresult(segfaults pre-fix).
mxmT_masked.read_after_exec— entry-wise check, tolerant of explicitzeros kept by the OpenCL kernel.
vector.set_fill_value_preserves_data— entries surviveset_fill_valueand a round trip through the dense representation.matrix.set_fill_value_preserves_data— entries surviveset_fill_valueand round trips through Lil/Dok/Coo/Csr.Full C++ test suite verified locally on three backends: CPU (no OpenCL),
Intel iGPU (OpenCL), and PoCL (CPU OpenCL) — all green.
Background
The stale-counter bug means any code that multiplies matrices on the CPU
backend and then reads the result back was already crashing or corrupting
memory; the
set_fill_valuebug means any code that changed the fill value ofa populated vector/matrix silently lost its data.
Addition context
This PR is a prerequisite for #282 (Python graph algorithms): that PR's tests
exercise
mxmread-back and fill-value semantics, so they fail without thesefixes.