Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions docs/abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -1439,10 +1439,19 @@ The gate discriminates by layout: the same calls succeed on AoS, pinned
two-sided.

**Alignment, stated honestly** (matching §11's own statement): the base
pointer is `u8`-aligned (`Arc<[u8]>`), and every region base and per-facet
block offset is a multiple of 64 for any `n_rows` (128, 384 and the block
factors against `n` all carry the factor; pinned by test). The kernels use
unaligned loads either way. The carvings' own contract is untouched: every
pointer is `u8`-aligned (`Arc<[u8]>`). The three region bases (`0`, `128n`,
`384n`) are multiples of 64 for any `n_rows`. The per-facet BLOCK offsets are
not: classid and hi32 blocks start at `f*4n` (plus the region base), all
multiples of 64 exactly when `n` is a multiple of 16; lo64 blocks start at
`128n + f*8n`, all multiples of 64 exactly when `n` is a multiple of 8. At
`n = 257`, block 1 starts at byte 1028, and `1028 % 64 == 4`. Nothing is
padded. No kernel and no Java read requires alignment: the kernels use
unaligned loads (and give identical mask words at base offsets 1, 3 and 7),
and Java reads with `*_UNALIGNED` layouts. Pinned by
`facet_major_alignment_holds_exactly_when_n_is_a_multiple_of_16` and
`strided_eq_mask_is_independent_of_the_base_alignment`. An earlier text of
this paragraph claimed "any `n_rows`" for the block offsets; that was false.
The carvings' own contract is untouched: every
`CascadeShape` group is ≤ 4 bytes — half the JEP 401 flattening budget —
and `512 = 8 × 64` keeps the row stride cache-line-quantised (both pinned
in `rowstore.rs`, the substrate half of what R4/R10 measured from the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static void main(String[] args) {
suites.put("ColumnarStoreTest", ColumnarStoreTest::run);
suites.put("MaskingOpCompletionTest", MaskingOpCompletionTest::run);
suites.put("GroupSumTest", GroupSumTest::run);
suites.put("DescriptorParityTest", DescriptorParityTest::run);

if (!NativeRuntime.isAvailable()) {
// ApiSurfaceTest and DoctrineFenceTest need no native library — the API's shape is a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package com.adaworldapi.lancegraph;

import com.adaworldapi.lancegraph.internal.ffm.Engine;
import com.adaworldapi.lancegraph.internal.ffm.Layouts;
import java.lang.foreign.MemoryLayout.PathElement;

/**
* Layout parity from the Java side: the lane descriptors Java reads through Panama agree with the
* geometry documented in docs/abi.md §11/§18 for BOTH store layouts, the Java struct offsets agree
* with the literals Rust pins ({@code abi::tests::field_offsets_are_pinned_to_literals}), and every
* (row, facet) read through the public accessors is equal across the layouts AND equal to an
* independent Java transcription of the documented generator.
*
* <p>The expected geometry here is computed from the documentation, never from {@code Layouts} or
* from any offset the production code derives. The generator transcription is a TEST ORACLE only;
* nothing in {@code src/main} gains a compute path from this file.
*
* <p>DISABLE (verified red-then-green): change the facet-major lo64 expected offset in
* {@link #expected} (e.g. {@code 128 * n + f * 8 * n} to {@code 128 * n + f * 8 * n + 1}) and the
* descriptor check goes red for every facet-major lo64 lane, while row-major stays green. Change a
* literal in {@link #pinStructOffsets} and the struct-offset check goes red.
*
* <p>Alignment is deliberately NOT asserted: block offsets are 64-aligned only when n is a
* multiple of 16 (docs/abi.md §18), and no Java read requires it.
*/
public final class DescriptorParityTest {

private DescriptorParityTest() {}

private static final int FACETS = 32;
private static final int LANES = 1 + 3 * FACETS;

public static void main(String[] args) {
System.out.println("DescriptorParityTest");
Checks c = new Checks("DescriptorParityTest");
run(c);
System.exit(c.report());
}

/** SplitMix64, transcribed from docs/abi.md §11 (no warm-up draws). */
private static final class SplitMix64 {
private long state;

SplitMix64(long seed) {
this.state = seed;
}

long next() {
state += 0x9E3779B97F4A7C15L;
long z = state;
z = (z ^ (z >>> 30)) * 0xBF58476D1CE4E5B9L;
z = (z ^ (z >>> 27)) * 0x94D049BB133111EBL;
return z ^ (z >>> 31);
}
}

/** Documented geometry of one lane: offset from the raw lane, length, element bytes, stride. */
private record Geometry(long offset, long len, int elemBytes, int stride, int kind) {}

private static Geometry expected(boolean columnar, long n, int lane) {
if (lane == 0) {
return new Geometry(0, 512 * n, 1, 1, 1);
}
int field = (lane - 1) / FACETS;
long f = (lane - 1) % FACETS;
if (!columnar) {
return switch (field) {
case 0 -> new Geometry(f * 16, n, 4, 512, 5);
case 1 -> new Geometry(f * 16 + 4, n, 8, 512, 7);
default -> new Geometry(f * 16 + 12, n, 4, 512, 5);
};
}
return switch (field) {
case 0 -> new Geometry(f * 4 * n, n, 4, 4, 5);
case 1 -> new Geometry(128 * n + f * 8 * n, n, 8, 8, 7);
default -> new Geometry(384 * n + f * 4 * n, n, 4, 4, 5);
};
}

public static void run(Checks c) {
pinStructOffsets(c);

for (boolean columnar : new boolean[] {false, true}) {
String name = columnar ? "facet-major" : "row-major";
for (long n : new long[] {16, 257, 4096}) {
c.section(name + " descriptors at n=" + n);
try (RowStore s = columnar ? RowStore.openColumnar(n, 0x1A7EL)
: RowStore.open(n, 0x1A7EL)) {
checkDescriptors(c, s, columnar, n, name);
}
}
}

accessorsAgree(c);
}

private static void pinStructOffsets(Checks c) {
c.section("Java struct offsets equal the literals Rust pins");
c.eq("LgjLaneDesc size", 56L, Layouts.LANE_DESC.byteSize());
String[] lane = {"addr", "len_elems", "byte_len", "owner", "epoch", "elem_kind",
"elem_bytes", "stride_bytes", "flags"};
long[] laneOff = {0, 8, 16, 24, 32, 40, 44, 48, 52};
for (int i = 0; i < lane.length; i++) {
c.eq("LgjLaneDesc." + lane[i], laneOff[i],
Layouts.LANE_DESC.byteOffset(PathElement.groupElement(lane[i])));
}
c.eq("LgjResourceInfo size", 32L, Layouts.RESOURCE_INFO.byteSize());
String[] info = {"kind", "lane_count", "n_rows", "epoch", "parent"};
long[] infoOff = {0, 4, 8, 16, 24};
for (int i = 0; i < info.length; i++) {
c.eq("LgjResourceInfo." + info[i], infoOff[i],
Layouts.RESOURCE_INFO.byteOffset(PathElement.groupElement(info[i])));
}
c.eq("LgjOpDesc size", 24L, Layouts.OP_DESC.byteSize());
String[] op = {"op", "lane_id", "operand", "combine", "_reserved"};
long[] opOff = {0, 4, 8, 16, 20};
for (int i = 0; i < op.length; i++) {
c.eq("LgjOpDesc." + op[i], opOff[i],
Layouts.OP_DESC.byteOffset(PathElement.groupElement(op[i])));
}
}

private static void checkDescriptors(Checks c, RowStore s, boolean columnar, long n,
String name) {
long handle = s.handle();
Engine.LaneWindow raw = Engine.describeLane(handle, 0);
long base = raw.segment().address();
int bad = 0;
long covered = 0;
boolean[] seenByte = new boolean[(int) (512 * n)];
boolean disjoint = true;
for (int lane = 0; lane < LANES; lane++) {
Engine.LaneWindow w = Engine.describeLane(handle, lane);
Geometry g = expected(columnar, n, lane);
long byteLen = (g.len() - 1) * g.stride() + g.elemBytes();
boolean contiguous = g.stride() == g.elemBytes();
boolean ok = w.segment().address() - base == g.offset()
&& w.lengthElements() == g.len()
&& w.elemBytes() == g.elemBytes()
&& w.strideBytes() == g.stride()
&& w.elemKind() == g.kind()
&& w.byteLength() == byteLen
&& w.segment().byteSize() == byteLen
&& w.isContiguous() == contiguous
&& (w.flags() & Layouts.FLAG_READABLE) != 0
&& !w.isWritable();
if (!ok) {
bad++;
System.out.println(" lane " + lane + " " + w + " expected " + g);
}
if (lane != 0) {
for (long row = 0; row < g.len(); row++) {
int at = (int) (g.offset() + row * g.stride());
for (int b = 0; b < g.elemBytes(); b++) {
if (seenByte[at + b]) {
disjoint = false;
}
seenByte[at + b] = true;
covered++;
}
}
}
}
c.eq(name + " n=" + n + ": lanes whose descriptor differs from the documented geometry",
0L, bad);
c.that(name + " n=" + n + ": the 96 facet lanes are pairwise disjoint", disjoint);
c.eq(name + " n=" + n + ": and cover exactly 512n bytes", 512 * n, covered);
}

private static void accessorsAgree(Checks c) {
c.section("every (row, facet) agrees across layouts and with the generator oracle, n=257");
final int n = 257;
final long seed = 0xF00DCAFEL;
// Oracle: classid = (a >>> 33) & 0xF; payload = le64(b) ++ le32(a & 0xFFFFFFFF).
int[] classid = new int[n * FACETS];
long[] lo64 = new long[n * FACETS];
int[] hi32 = new int[n * FACETS];
SplitMix64 rng = new SplitMix64(seed);
for (int i = 0; i < n * FACETS; i++) {
long a = rng.next();
long b = rng.next();
classid[i] = (int) ((a >>> 33) & 0xF);
lo64[i] = b;
hi32[i] = (int) a;
}
try (RowStore aos = RowStore.open(n, seed);
RowStore col = RowStore.openColumnar(n, seed)) {
long compared = 0;
long crossMismatch = 0;
long oracleMismatchAos = 0;
long oracleMismatchCol = 0;
for (int row = 0; row < n; row++) {
for (int f = 0; f < FACETS; f++) {
FacetId facet = FacetId.of(f);
int i = row * FACETS + f;
int ca = aos.classidAt(row, facet);
int cc = col.classidAt(row, facet);
long la = aos.payloadLow64At(row, facet);
long lc = col.payloadLow64At(row, facet);
int ha = aos.payloadHi32At(row, facet);
int hc = col.payloadHi32At(row, facet);
if (ca != cc || la != lc || ha != hc) {
crossMismatch++;
}
if (ca != classid[i] || la != lo64[i] || ha != hi32[i]) {
oracleMismatchAos++;
}
if (cc != classid[i] || lc != lo64[i] || hc != hi32[i]) {
oracleMismatchCol++;
}
compared++;
}
}
c.eq("(row, facet) pairs compared", (long) n * FACETS, compared);
c.eq("row-major vs facet-major mismatches", 0L, crossMismatch);
c.eq("row-major vs the Java generator oracle mismatches", 0L, oracleMismatchAos);
c.eq("facet-major vs the Java generator oracle mismatches", 0L, oracleMismatchCol);
// Anti-vacuity: the oracle is not constant, and the layouts' bytes really differ.
c.that("oracle classids span the full 0..16 range",
java.util.Arrays.stream(classid).distinct().count() == 16);
c.that("oracle payloads are not constant", lo64[0] != lo64[1] && hi32[0] != hi32[1]);
c.that("the two stores really are arranged differently (lane 1 base-relative offset"
+ " of facet 1 differs)",
Engine.describeLane(col.handle(), 2).segment().address()
- Engine.describeLane(col.handle(), 0).segment().address()
!= Engine.describeLane(aos.handle(), 2).segment().address()
- Engine.describeLane(aos.handle(), 0).segment().address());
}
}
}
29 changes: 29 additions & 0 deletions native/lgj-abi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,35 @@ mod tests {
assert_eq!(size_of::<LgjAbiManifest>(), 128);
}

/// Every field offset of the three wire structs, pinned to literal
/// numbers. The Java `Layouts` test (`DescriptorParityTest`) pins the SAME
/// literals, so a reorder on either side is red on one side.
#[test]
fn field_offsets_are_pinned_to_literals() {
use core::mem::offset_of;
assert_eq!(offset_of!(LgjLaneDesc, addr), 0);
assert_eq!(offset_of!(LgjLaneDesc, len_elems), 8);
assert_eq!(offset_of!(LgjLaneDesc, byte_len), 16);
assert_eq!(offset_of!(LgjLaneDesc, owner), 24);
assert_eq!(offset_of!(LgjLaneDesc, epoch), 32);
assert_eq!(offset_of!(LgjLaneDesc, elem_kind), 40);
assert_eq!(offset_of!(LgjLaneDesc, elem_bytes), 44);
assert_eq!(offset_of!(LgjLaneDesc, stride_bytes), 48);
assert_eq!(offset_of!(LgjLaneDesc, flags), 52);

assert_eq!(offset_of!(LgjResourceInfo, kind), 0);
assert_eq!(offset_of!(LgjResourceInfo, lane_count), 4);
assert_eq!(offset_of!(LgjResourceInfo, n_rows), 8);
assert_eq!(offset_of!(LgjResourceInfo, epoch), 16);
assert_eq!(offset_of!(LgjResourceInfo, parent), 24);

assert_eq!(offset_of!(LgjOpDesc, op), 0);
assert_eq!(offset_of!(LgjOpDesc, lane_id), 4);
assert_eq!(offset_of!(LgjOpDesc, operand), 8);
assert_eq!(offset_of!(LgjOpDesc, combine), 16);
assert_eq!(offset_of!(LgjOpDesc, _reserved), 20);
}

#[test]
fn magic_reads_as_lgj_abi_nul_big_endian() {
assert_eq!(&LGJ_MAGIC.to_be_bytes(), b"LGJ_ABI\0");
Expand Down
3 changes: 3 additions & 0 deletions native/lgj-abi/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2882,6 +2882,9 @@ mod tests {
/// against the two-crossing path it replaces, the scalar oracle, the
/// fk-keyed form against a hand-walked join, and every refusal.
mod group_sum;
/// Layout parity: every lane descriptor of BOTH row-store layouts, as
/// served through `lgj_lane_describe`, against the documented geometry.
mod layout_parity;
/// The lowering differential: this crate's `plan_lower` (a flat op list)
/// against `lance-graph-quack`'s `lower` (a Boolean tree), both producing
/// a `mask_risc::Program`. Two implementations of one law; until this
Expand Down
Loading
Loading