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
52 changes: 52 additions & 0 deletions .claude/board/LATEST_STATE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
## 2026-11-12 — two characterisations (tests only; in a draft PR)

**The two lowerings emit the same `Program`** (`lowering_convergence.rs`,
`both_lowerings_emit_the_same_program`). For the 56 plans with at least one AND
(arity 2 to 5, all nine opcodes) and the four terminals this crate lowers to
(keep, count, sum, grouped sum), `plan_lower(ops) == quack::lower(fold_to_tree(ops))`
structurally: 224 of 224. Until now the file compared answers only.

- Disable-verified: dropping the survivor gate, or swapping the AND's operand
order, turns only this test red. Every answer-level test stays green, since
neither change alters an answer.
- Scope: the Quack side receives this file's tree reading of the flat plan.
Plans with no AND and the fk-keyed grouped sum are outside the claim.

**Which exports check a mask's parent** (`provenance.rs`). Twelve exports
against five mask origins, asserted as a table:

- Row count only (9): `lgj_op_eq_u32`, `lgj_op_gt_i32`, `lgj_plan_eval`,
`lgj_reduce_sum_i32`, `lgj_reduce_i32`, `lgj_op_eq_classid`,
`lgj_op_ternary_match`, `lgj_hop` (source and destination). A mask over a
different resource, or over a resource of the other kind, is accepted.
- Same parent (3, plus the four mask-algebra ops): `lgj_reduce_facet_sum`,
`lgj_reduce_facet_sum_resolved`, `lgj_row_layout_probe`.
- `a_selection_can_be_relabelled_onto_another_resource_today` shows the
consequence: a predicate evaluated on B, stored in A's mask and summed over
A's values returns `OK` and a number that answers neither A nor B.
- Experiment, not committed: with the same-parent rule in all nine places,
the native suite (except the two tests that pin the lenient rule), `AllTests`
(617), the four consumer mains (153) and the TinkerPop harness (80) all pass.
`bench/` was not run. No rule was changed.

## 2026-11-12 — ABI minor 13: a plan folded to one scalar, with no mask (D-LGJ-SCALAR-1, in a draft PR)

Status: implemented and measured; in a draft PR on top of the merged P0 fix (#94).

- **Added:** `lgj_plan_scalar` (`docs/abi.md` §21) with `LGJ_FOLD_COUNT` and
`LGJ_FOLD_SUM_I32`. `plan_lower::lower_scalar` reuses `lower_plan`'s ops and
ends in mask-RISC's existing `Count` / `MaskedSumI32` terminals. `lgj_plan_eval`
is unchanged. `View.count()` and `View.sumOf()` use the new symbol at minor
13 or later and keep the mask path against an older library.
- **Measured:** native 205 tests (9 new, including one allocation gate); 6 of 6
disable runs red. Java core 617 checks; consumers 70 / 68 / 3 / 12, unchanged.
Against a minor-12 library built from the #94 commit, `OldAbiCompatTest` gives
the same answers (71, 6257, 111296) in 2 crossings where minor 13 takes 1.
- **Cost:** `sumOf()` 2 crossings to 1; `count()` stays 1. Mask bytes written per
call go from `8 × ceil(rows / 64)` to 0. Native latency: count unchanged within
noise, sum 3–25% faster (`examples/plan_scalar_cost.rs`).
- **Re-pinned:** `LazinessTest` (sumOf 2 to 1) and `GroupSumTest` (the per-group
path 32 to 16 crossings). Both pins moved because the cost moved.
- **Not done:** JMH numbers through the JVM; a `Program`-level comparison with
Quack for these two terminals.

## 2026-10-09 — Gremlin traversals lowered onto the mask hop chain (interop/tinkerpop, A2)

`LanceHopStrategy` is a TinkerPop provider strategy. It rewrites
Expand Down
1 change: 1 addition & 0 deletions .claude/board/STATUS_BOARD.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ same lowering: lance-graph #1257 (SAP CATS, merged).
|---|---|---|
| D-LGJ-FOLD-4 | ABI minor 12 `lgj_plan_group_sum_i32` + `View.sumByGroup` / `sumByGroupVia` + `GroupTotals`: a `GROUP BY … SUM` as ONE program, one crossing, no selection (`docs/abi.md` §20) | **DONE 2026-09-22** — native 191 tests, Java 612 checks (JDK 28), 1 crossing measured beside the 32-crossing path, six disable arms red-then-green |
| D-LGJ-FOLD-5 | Migrate `consumers/bricks` `sumBy()` from the 32-crossing per-group path onto `sumByGroup` (re-pin BricksAuthTest's crossing arithmetic 32 → 1) | **DONE 2026-09-22** — one fused call; **1 crossing at 1,000 and 64,000 rows AND at 1, 16 and 64 groups**. The group-count arm is new and load-bearing: the public `sumBy()` always asks for `Orders.REGIONS` groups, so nothing previously reachable varied the group count and "one crossing whatever the group count" was unfalsifiable; a package-private `sumByGroupCount` exists for that arm alone (invisible to `getMethods()`, so the aggregate-egress guard still audits exactly the public surface). Disable arm A (revert to the per-group loop) measured the old cost formula exactly — **2 / 32 / 128 at 1 / 16 / 64 groups, identical at both row counts, i.e. `2 × groups`** — with parity GREEN throughout, so the two paths agree on the answer and differ only in cost. `Orders.REGIONS` replaces three literal 16s with one named source, a MIRROR of the native generator's classid cardinality (the manifest does not report it) pinned two-sided against the fixture rather than against a copied literal; arms B (20) and C (8) each fire a DIFFERENT half, so neither half alone catches both directions. BricksAuthTest **70/70** (was 62) |
| D-LGJ-SCALAR-1 | ABI minor 13 `lgj_plan_scalar`: count and sum folded straight from the plan with no mask (`docs/abi.md` §21); `lgj_plan_eval` unchanged | **Implemented 2026-11-12; in a draft PR.** Native 205 tests, Java 617 checks, consumers unchanged; `sumOf()` 2 crossings to 1; mask write removed; native latency: count unchanged, sum 3–25% faster |

125 changes: 122 additions & 3 deletions docs/abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ cannot disagree with itself.
The ABI is a **machine membrane**. It is not the product. The product is the Java
semantic API (see `architecture.md`). Therefore:

- It is **small** — currently 30 symbols (minor 12's one addition — the grouped
sum that never builds a selection — is argued in §20; 29 at minor 11, whose three
- It is **small** — currently 31 symbols (minor 13's one addition — a plan
folded to one scalar with no mask — is argued in §21; 30 at minor 12, whose one
addition — the grouped sum that never builds a selection — is argued in §20;
29 at minor 11, whose three
additions are argued in §19, and the same section argues the SEVEN capabilities it deliberately did
NOT spend a symbol on; 26 at minor 10, whose one addition — the
columnar constructor — is argued in §18; 25 at minor 9, whose one addition is argued
Expand Down Expand Up @@ -211,6 +213,9 @@ required — a gate that rejected everything would satisfy a rejection-only test
since minor 2; nothing could consume a mask against the 12-byte facet
register until now. Two new statuses: `UNSUPPORTED_CARVING` (-15) and
`SUM_OVERFLOW` (-16).
- **Minor 13** (2026-11-12) — `lgj_plan_scalar` (§21): the fused plan folded
straight into a count or a sum for a caller that asked for no mask.
`lgj_plan_eval` is unchanged. No new status, no manifest growth.

## 3. Status codes

Expand Down Expand Up @@ -439,7 +444,7 @@ predicates or rows are involved. The unfused per-predicate ops are retained only
so the fused path can be benchmarked *against* something and so parity can be
checked predicate-by-predicate.

## 7. The function surface (30 symbols)
## 7. The function surface (31 symbols)

All symbols are prefixed `lgj_`. All return `i32` status except the manifest
getter. `out_*` parameters are written only on `OK`.
Expand Down Expand Up @@ -556,6 +561,17 @@ is involved. `out_sums[g]` for `g < n_groups`; a key past `n_groups` is
dropped. `n_ops == 0` is legal and means every row. `via_res != 0` reads the
group key THROUGH `group_lane` into `via_res`'s `via_lane` (the fk-keyed form).

### Scalar fold (ABI minor ≥ 13)

```
i32 lgj_plan_scalar(u64 res, const LgjOpDesc* ops, u32 n_ops,
u32 fold, u32 val_lane, i64* out_value) // minor >= 13, §21
```

The plan folded to one count (`fold = 0`) or one `I32` sum (`fold = 1`) in ONE
crossing, with no mask handle. `n_ops == 0` is legal and means every row.
`lgj_plan_eval` remains the symbol for a caller that demands the mask.

### Parity escape hatch

```
Expand Down Expand Up @@ -1803,3 +1819,106 @@ reports *"View.sumByGroup (minor 12) reports an ABI mismatch, not a missing
symbol (threw AbiMismatchException)"*, with every minor-11 feature still
working beside it.

## 21. A plan folded to one scalar, with no mask (ABI minor ≥ 13)

```
i32 lgj_plan_scalar(u64 res, const LgjOpDesc* ops, u32 n_ops,
u32 fold, u32 val_lane, i64* out_value)
```

### 21.1 Why it exists

`lgj_plan_eval` answers two things: it lands the selection in `dst_mask` and
returns its count. A caller that wants only a number still pays for the mask:
one bit per row written into a destination that is read once. A sum pays a
second crossing (`lgj_reduce_sum_i32`) to read that mask back. §20 removed this
for the grouped sum; the plain count and sum were still on the mask path.

`lgj_plan_eval` is **unchanged**. It remains the symbol for a caller that
demands the mask, and it still returns the count with it.

### 21.2 What it does

The plan lowers exactly as `lgj_plan_eval` lowers it: `plan_lower::lower_scalar`
reuses `lower_plan`'s ops unchanged (pinned by
`the_scalar_sum_range_is_the_whole_lane`) and replaces only the terminal.

| `fold` | terminal | `*out_value` |
|---|---|---|
| `LGJ_FOLD_COUNT` (0) | `Terminal::Count` | rows selected; `val_lane` ignored |
| `LGJ_FOLD_SUM_I32` (1) | `Terminal::MaskedSumI32` | `Σ val_lane[i]` over selected rows, `0` if none |

The accumulator is a tile of scratch and the terminal folds each tile into the
running scalar (`Out::None`). No mask handle is named or written.

`n_ops == 0` is legal and means every row, as in §20. A count over every row
(an empty plan, or one whose every combine is OR) is answered from the row
count without a program. A sum over every row uses the whole-lane
`Pred::Range`, `0..n_rows`, the second and last `Range` this crate emits.

### 21.3 Contract

- `*out_value` is written only on `LGJ_OK`.
- Statuses: `NULL_ARGUMENT` for a null `out_value`, or a null `ops` with
`n_ops > 0`; `UNKNOWN_OPCODE` for an unknown `fold`; `INVALID_HANDLE` /
`WRONG_RESOURCE_KIND` for `res`; every plan defect exactly as `lgj_plan_eval`
reports it (pinned against `lgj_plan_eval` itself, defect by defect);
`INVALID_LANE` / `LANE_KIND_MISMATCH` for `val_lane` under a sum;
`LENGTH_OVERFLOW` for a sum past `2^32 - 1` rows. **No new status.**
- Bulk (§6): one pass, `O(rows)`.

### 21.4 The Java spelling

There is none. `View.count()` and `View.sumOf(field)` keep their signatures and
call this symbol when the loaded library reports minor ≥ 13. Against an older
library they keep the mask path, so neither is gated: both answer on every
minor. Run against a minor-12 library built from this repo's history and
against minor 13, `OldAbiCompatTest` reports the same three numbers (count 71,
filtered sum 6257, whole-lane sum 111296), in 2 crossings and 1.

### 21.5 Measured (2026-11-12)

Parity: `scalar_fold.rs` compares the symbol with the mask path, with
mask-risc's row-at-a-time reference on the same program (several tiles), and
with a hand walk over the fixture arrays, across the `{AND, OR}^n` sweep at
1,000 and 65,536 rows.

| | mask path | scalar fold |
|---|---|---|
| crossings, `count()` | 1 | 1 |
| crossings, `sumOf()` | 2 | 1 |
| mask bytes written per call | `8 × ceil(rows / 64)` (8 KiB at 65,536 rows) | 0 |
| native mask resources `count()`/`sumOf()` need | a scratch mask, and an all-rows mask for an unfiltered sum | none |
| Rust allocation per call (3 ops) | 240 B, independent of rows | 240 B, independent of rows |

Native latency, `examples/plan_scalar_cost.rs`, medians of 201, two runs, no JVM
in the numbers:

| rows | plan | count: mask → fold | sum: mask → fold |
|---|---|---|---|
| 1,024 | `class==7 AND value>100` | 389–463 → 328–407 ns | 464–564 → 340–406 ns |
| 65,536 | same | 12.4–16.1 → 12.3–17.0 µs | 14.5–18.8 → 14.1–18.3 µs |
| 65,536 | `value>100` | 4.8–6.3 → 4.7–6.1 µs | 14.3–19.1 → 13.9–17.3 µs |
| 1,048,576 | `class==7 AND value>100` | 386–514 → 467–481 µs | 647–664 → 607–623 µs |
| 1,048,576 | `value>100` | 186–187 → 181–183 µs | 413–423 → 313–337 µs |

The count is not faster within run-to-run noise: the predicate pass dominates
and the mask write is small beside it. The sum is 3–25% faster. What this
symbol removes is the undemanded mask write, the second crossing of a sum and
the two helper mask resources; it is not a large speed-up.

### 21.6 The disable table (red-then-green, or it is not evidence)

| what was disabled | test that went red |
|---|---|
| the all-rows `Range` emptied to `0..0` | `an_empty_plan_and_an_all_or_plan_fold_every_row`, `count_and_sum_match_the_mask_path_and_the_hand_walk`, `the_scalar_sum_range_is_the_whole_lane` |
| the all-rows count answered `0` | `an_empty_plan_and_an_all_or_plan_fold_every_row`, `count_and_sum_match_the_mask_path_and_the_hand_walk` |
| `out_value` written before validation | `every_refusal_is_a_status_and_leaves_out_value_untouched` |
| the `fold` check removed | `every_refusal_is_a_status_and_leaves_out_value_untouched` |
| the sum lowered to a count terminal | `count_and_sum_match_the_mask_path_and_the_hand_walk`, `an_empty_selection_is_zero_not_absent`, `the_scalar_sum_range_is_the_whole_lane` |
| plan validation skipped | `plan_defects_are_reported_as_lgj_plan_eval_reports_them` |

The last row was green on its first run: the executor refuses most defective
plans by itself, with the same status. The test that pins it compares statuses
with `lgj_plan_eval` for a lane index past `u16` and an undefined combine,
which only the shared validation catches.
20 changes: 18 additions & 2 deletions java/src/main/java/com/adaworldapi/lancegraph/NativePattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ void requireOpen(String what) {
}
}

/** Evaluate a plan into the reusable scratch selection and return its popcount. */
/**
* Count the rows a plan selects. Against ABI minor 13 or later this is one scalar fold with
* no selection written; against an older library it evaluates into the reusable scratch
* selection and returns its popcount.
*/
long countOf(List<Predicate> predicates) {
requireOpen("count()");
if (predicates.isEmpty()) {
Expand All @@ -172,15 +176,27 @@ long countOf(List<Predicate> predicates) {
}
synchronized (lock) {
requireOpen("count()");
if (Engine.hasPlanScalar()) {
// Nobody asked for the selection, so none is written (docs/abi.md §21).
return Engine.planCount(handle, plan(predicates));
}
return Engine.evaluateFused(handle, plan(predicates), scratch());
}
}

/** Evaluate a plan, then reduce one lane over the resulting selection. */
/**
* Sum one lane over the rows a plan selects. Against ABI minor 13 or later this is one
* crossing with no selection; against an older library it evaluates the plan into a
* selection, then reduces over it (two crossings).
*/
long sumOf(List<Predicate> predicates, I32Field field) {
requireOpen("sumOf()");
synchronized (lock) {
requireOpen("sumOf()");
if (Engine.hasPlanScalar()) {
// One program, one crossing, no mask; an empty plan is every row (§21).
return Engine.planSumI32(handle, plan(predicates), field.lane().index());
}
long mask;
if (predicates.isEmpty()) {
// Sum over everything: select all rows rather than inventing an always-true plan.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,38 @@ private static final class Minor11 {
private Minor11() {}
}

/**
* ABI minor 13 symbol (docs/abi.md §21): the fused plan folded straight into one scalar,
* with no mask handle involved. Lazy per the minor-2..12 rule.
*/
private static final class Minor13 {
static final MethodHandle PLAN_SCALAR = mh("lgj_plan_scalar",
FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.JAVA_LONG,
ValueLayout.ADDRESS, ValueLayout.JAVA_INT, ValueLayout.JAVA_INT,
ValueLayout.JAVA_INT, ValueLayout.ADDRESS));

private Minor13() {}
}

/**
* Fold the rows the plan selects into one scalar in ONE crossing (docs/abi.md §21, minor
* 13): a count ({@link Layouts#FOLD_COUNT}) or the sum of an {@code I32} lane
* ({@link Layouts#FOLD_SUM_I32}). No destination mask exists; {@code nOps == 0} is legal and
* means every row.
*/
public static long planScalar(long res, MemorySegment ops, int nOps, int fold, int valLane,
MemorySegment outValue) {
crossed();
int st;
try {
st = (int) Minor13.PLAN_SCALAR.invokeExact(res, ops, nOps, fold, valLane, outValue);
} catch (Throwable t) {
throw wrap("lgj_plan_scalar", t);
}
Status.check("lgj_plan_scalar", st);
return outValue.get(ValueLayout.JAVA_LONG, 0);
}

/**
* ABI minor 12 symbol (docs/abi.md §20): the fused plan run straight into a grouped-sum
* terminal — one crossing returns one {@code i64} per group and no selection ever exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,40 @@ public static ReduceOutcome reduceI32(long resource, int laneId, int reduceOp, l
return new ReduceOutcome(value, present);
}

/** The first ABI minor that carries {@code lgj_plan_scalar} (docs/abi.md §21). */
private static final int MINOR_PLAN_SCALAR = 13;

/**
* Whether the loaded library folds a plan straight into a scalar. Against an older library
* the callers below keep the mask path, which every minor since 1 has carried.
*/
public static boolean hasPlanScalar() {
return Abi.manifest().abiMinor() >= MINOR_PLAN_SCALAR;
}

/**
* How many rows {@code plan} selects, in one crossing and with no mask (docs/abi.md §21).
* An empty plan is legal and means every row. Requires ABI minor &gt;= 13.
*/
public static long planCount(long resource, List<PlanOp> plan) {
return planScalar(resource, plan, Layouts.FOLD_COUNT, 0);
}

/**
* The sum of {@code valLane} over the rows {@code plan} selects, in one crossing and with
* no mask (docs/abi.md §21). Zero over an empty selection. Requires ABI minor &gt;= 13.
*/
public static long planSumI32(long resource, List<PlanOp> plan, int valLane) {
return planScalar(resource, plan, Layouts.FOLD_SUM_I32, valLane);
}

private static long planScalar(long resource, List<PlanOp> plan, int fold, int valLane) {
Abi.requireMinor(MINOR_PLAN_SCALAR);
MemorySegment ops = plan.isEmpty() ? MemorySegment.NULL : marshal(plan);
Scratch s = SCRATCH.get();
return Downcalls.planScalar(resource, ops, plan.size(), fold, valLane, s.out);
}

/**
* The grouped sum that never builds a selection (docs/abi.md §20, ABI minor 12):
* {@code GROUP BY groupLane SUM(valLane)} over the rows {@code plan} selects, evaluated as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ private Layouts() {}
/** Union into the accumulator — widening. Deliberately unreachable from {@code View.where}. */
public static final int COMBINE_OR = 1;

/** {@code lgj_plan_scalar} fold (docs/abi.md §21): how many rows the plan selects. */
public static final int FOLD_COUNT = 0;
/** {@code lgj_plan_scalar} fold (docs/abi.md §21): the sum of an {@code I32} lane. */
public static final int FOLD_SUM_I32 = 1;

// ── SIMD backend ids (docs/abi.md §5) ────────────────────────────────────────────────────

public static final int SIMD_SCALAR = 0;
Expand Down
Loading
Loading