Repository navigation
Conversation
Moved couple rules out of NullPropagation and added more comments.
|
QA tests have started for PR 1430. This patch merges cleanly. |
|
The code looks good to me. |
|
See the discussion at #1428. |
|
QA results for PR 1430: |
|
Merging in master & branch-1.0. |
|
Actually only master. There is a conflict in branch-1.0 that I will leave it intact for now. |
Moved couple rules out of NullPropagation and added more comments. Author: Reynold Xin <rxin@apache.org> Closes apache#1430 from rxin/sql-folding-rule and squashes the following commits: 7f9a197 [Reynold Xin] Updated documentation for ConstantFolding. 7f8cf61 [Reynold Xin] [SQL] Cleaned up ConstantFolding slightly.
…ongs (apache#1430) ### What changes were proposed in this pull request? This is the first half of a printer that turns Varka's vector IR into SMT-LIB proofs. It adds the term language the printer writes in, the "word form" it uses for 64-bit values, and two new operators in the proofs' prelude. It builds on apache#1423, merged, which lets a proof ask `dev/varka_prove.sh` for a second prelude; nothing here reads that yet. Nothing in the main source set changes, and no emitted byte moves. **The term tree** (`VarkaSmtTerm`, test side). A term is a constant, a variable (an exact integer, an int or long lane value, or a boolean), an application of an SMT-LIB operator or of one of `java.smt2`'s Java operators (`jlong.add`, `jlong.ushr`, ...), or a named quotient `floor(v / k)`. A term renders to SMT-LIB text and also evaluates over `BigInteger` in the JVM. Each operator's evaluation is written from the JLS, separately from `java.smt2`'s definition with the same name. That way the JVM tests and `java_check.smt2` check the two against Java on their own. A named quotient renders as a declared constant pinned by `0 <= v - k*q < k`, the form the proofs README recommends for cvc5, and evaluates as the floor. The tree enforces the invariants its text rests on where a term is built. A quotient's divisor must be a positive constant: for `k <= 0` the pin has no solution, so every check over it would hold vacuously. A low mask must be a `2^k`, in the word form as in the evaluation, since for any other constant the prelude's `jlong.and.low` is not Java's `&`. Quotients are named `q.<n>`, a prefix a variable's name may not take, so a quotient can never shadow an input. SMT-LIB's `div` and `mod` are Euclidean (the remainder is never negative), and the tree evaluates them that way, so a negative divisor renders and evaluates alike. **The word form** (`VarkaSmtTerm.WordForm`). An earlier spike found that a 64-bit carry network written directly with `java.smt2`'s wrapping operators kept Z3 busy past two minutes, while the same network written over unsigned readings took both solvers a tenth of a second. The word form is that second rendering, done mechanically: - each long is printed as its unsigned reading in `[0, 2^64)`, with an interval worked out from its operands; - an add, subtract, negation, left shift or multiply by a constant whose interval stays in range is printed exactly, and the claim that it does not wrap becomes an obligation for the solver to prove; - a step that can wrap keeps one reduction, a named quotient by `2^64`; - a logical right shift by `k` and a mask by `2^k - 1` share one quotient by `2^k` and its remainder; - `x >> 63` is the sign word, and any other arithmetic shift is the floor of the signed reading; - a signed compare reads the words as signed, an unsigned one reads them unsigned; - an operator with no rule is printed as `(mod t 2^64)` of its prelude form. That is still exact, so the word form never changes what a term means. **The prelude** (`sql/varka/proofs/java.smt2`) gains `jlong.ult`, `jlong.ule`, `jlong.ugt` and `jlong.uge`, which compare two readings modulo `2^64` (the IR's unsigned compares, which the 128-bit decimal comparisons use). It also gains `jlong.and.sign`, which is `m & y` for a sign word `m` in `{0, -1}`. `java_check.smt2` checks each of them against `Long.compareUnsigned` and Java's `&` over the boundary longs. **`smt_print_check.smt2`** (rendered by `VarkaProofFiles`, held by `VarkaProofFilesSuite`). For each long operator the word form has a rule for, at the shift counts and constants its rules branch on, and nested where one rule feeds another, it proves that the word form's reading equals `(mod t 2^64)` of the prelude form for every pair of longs. It also proves that a compare's word form equals the compare, that every no-wrap obligation holds for every input, and that `div` and `mod` equal the term tree's evaluation of them on 110 operand pairs, negative divisors and operands past the long range included. It declares logic `ALL`: under `QF_NIA`, cvc5 returned unknown at the 10 s limit on a mask added to a shift. The second half, which states each IR node's meaning in these terms and checks it against the reference evaluator, is built on a local branch and comes as the next pull request. The plan's section 10.1 records the split and what each half holds. ### Why are the changes needed? Today the kernel side of each refinement proof is a hand transcription of the arithmetic the emitter builds. So a proof can keep holding over code that drops a carry, which is the kind of one-line error found in other engines' 128-bit decimal multiplies. Printing the proof from the IR the compiler lowers removes the transcription. For that, the printer needs a term language whose meaning is checked against Java, and a rendering of 64-bit networks that both pinned solvers can decide. This change provides both and checks them, before any IR node is printed. ### Does this PR introduce _any_ user-facing change? No. The changes are test code, proof files and a development script. ### How was this patch tested? - `VarkaSmtTermSuite` (new): every operator evaluated against Java's over 13 boundary longs and shift counts past the mask, with the range tests and the wrap also past the long range; `div` and `mod` against the Euclidean identity, negative divisors included; the invariants above as refusals; and every law term's word form against its prelude form on 16,800 rows of boundary and random longs, obligations included. The law terms take each branch of the intervals: exact (a multiply of a mask), reduced (a subtract of a shift from a mask), and a shift right of a word below `2^63`. - `VarkaProofFilesSuite` and `VarkaDivisorProofSuite` pass, with the committed files the rendered ones. - `dev/varka_prove.sh --solver both`: every proof holds. On the laptop, `smt_print_check.smt2` (49 checks) takes Z3 0.35 s and cvc5 0.18 s, each check alone holds under both (the slowest Z3's 0.24 s), and `java_check.smt2` (34 checks) takes Z3 0.03 s and cvc5 0.14 s. `--lint` passes. - Planted faults, run and reverted: printing the word form's unsigned compare over signed readings fails `VarkaSmtTermSuite` (486 rows) and five checks of `smt_print_check.smt2` under each solver; defining `jlong.ult` as the signed `<` fails `java_check.smt2` and two checks of `smt_print_check.smt2` under each solver. - `dev/lint-java`, `dev/varka_errorprone.sh`, scalastyle for catalyst, `dev/varka_plan_lint.py --check --offline`, and `dev/varka_regen_generated.sh --all`, which moved no generated file. ### Accepted trade-offs - The law-building helper is duplicated: `VarkaProofFiles.smtPrintCheck` and `VarkaSmtTermSuite` each build the word form of the same law terms, one to render them for the solvers and one to evaluate them in the JVM. Sharing it would add an abstraction for two call sites. - Small cleanups left as they are: the shift kind is an `int` rather than an enum, `scale` is also called with 1, quotients are keyed by their dividend's rendered text, and `subst()` has no caller yet (the second half uses it). ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Opus 5.5)
…e#1477) ### What changes were proposed in this pull request? Varka's decimal kernels are checked by SMT proofs, and the plan is for those proofs to be printed from the same IR the compiler lowers, instead of being written by hand beside it. apache#1430 added the printer's term tree and the word form it uses for 64-bit values. This PR adds the other half: what each IR node means, so the printer has something to print. **`VarkaSmtNodes`** (test code, in catalyst) states each IR node's meaning once, in one exhaustive `switch` over the sealed IR. For a node it gives the lane's outcome as a triple, `(decline, valid, data)`, built from its children's outcomes in `java.smt2`'s operators: - `data` is the lane's value; - `valid` says whether that value is known (false for a null); - `decline` says the kernel refuses the whole batch at this lane: a checked operation that overflows, or a leaf whose load check fails. It propagates from every child except an `IfElse` arm the lane does not take. The section below names one exception this PR leaves open. Leaves are free variables: a column `c<n>` with its validity `v<n>`, and a runtime literal either the literal table's constant or a fresh `l<i>` (int) or `m<i>` (long). `facts` gives the range each variable ranges over. Every 64-bit value is printed in apache#1430's word form, so its no-wrap obligations come with it. A node with no template yet is listed in `REFUSED` with its reason: the calendar nodes, constant and bounded division, `BitCount` (a popcount has no linear integer meaning), the floating nodes (`MathUnary`, `RoundToIntegral` and the conversions to or from a floating lane, since the prelude has no double as a value), and four condition nodes no decimal shape builds. Because the `switch` is exhaustive over the sealed IR, a new IR record does not compile until it gets a template or a refusal: `RoundToIntegral`, added by the rounding functions while this was being written, is the first one it caught. `VarkaSmtSamples` holds a hand set of trees at the boundary words. `sql/varka/proofs/smt_nodes_check.smt2` is the printed text for every sampled tree, and `VarkaProofFiles` registers it so `dev/varka_prove.sh` runs it under both solvers. The proofs README gains its line. ### Why are the changes needed? A hand-written proof can drift from the kernel it is about: the compiler changes, the proof still passes, and it no longer describes what runs. Printing the proof from the IR closes that gap, but only if each node's printed meaning is right. This PR holds every meaning to `VarkaReference`, the reference evaluator the fuzzers already trust, so the proofs printed next rest on checked meanings. ### Does this PR introduce _any_ user-facing change? No. Test code and a proof file only. ### How was this patch tested? `VarkaSmtNodesSuite` (new) has three tests: - every printed outcome, and the word form of every long in it, is `VarkaReference`'s answer, over the IR fuzzer's unsigned and decimal sequences and the hand set at the boundary words; - every IR record has a template that some sample reaches, or a refusal with its reason, never both; - at the coverage rows' trees, the int lane's templates are `refinement.smt2`'s hand-written kernels term for term (`i + 1`, `i - 1`, `year(d) * 2` and the rest), so the printer reproduces what was proven by hand. `smt_nodes_check.smt2` holds the printed text to the same answers under Z3 and cvc5 (`dev/varka_prove.sh --solver both`, every proof holds). The proof-file, SMT-term, divisor-proof, emitted-bytes, coverage and IR-fuzz suites pass on this branch over current master. ### Review, and what is left A `/code-review high` of this branch found one real gap. The printed decline is weaker than the kernel's when a decimal leaf sits under an `IfElse` arm the lane does not take. The emitter never ties a decimal slot check to an arm (`Analysis.armChainsOf`), so the kernel declines a batch with a bad slot in either arm. The template, and the sample oracle that checks it, count only the taken arm's. Nothing in this PR reads the decline as a refinement claim, but 506b's check "a decline only where it raises" will. The fix is about 30 lines: both arms' slot checks go into `choose`, the oracle gets the same rule, and a hand tree covers the case. That would take this PR to 1027 lines, past the 1000-line cap, so it is not in this PR. It has to land before 506b reads a decline. Fixed here: - The calendar records are named one by one instead of through a `Chrono _` catch-all, so a new calendar record does not compile until it has a refusal. - The oracle's `DecimalColumn` bound uses `BigInteger`, since `Math.abs(Long.MIN_VALUE)` is negative. Accepted as they are: - **The decline follows the default options.** With `guardUnderArm` off, or where the emitter leaves a guard unqualified (one over stored values under several arms, or one on a cycle of arm conditions), the kernel declines more batches than the printed outcome says. It never declines fewer. - **No 128-bit decimal values in the samples.** A sample row holds one long per column, which is how `VarkaReference` lays out a `DecimalWord`. Its HIGH word is therefore the sign word, and its slot check never fails in these samples. `VarkaDecimalWordSuite` checks the words past 64 bits against Spark's column reader. - **Few word-form obligations in the file.** Only a handful of the sampled trees produce one: the sign-word masks and a negate. The add, subtract, shift and multiply no-wrap laws are proven on their own in `smt_print_check.smt2` (apache#1430), and the printed trees exercise them only through the rows. - **Terms are copied, not shared.** `pick`, `kleene` and `choose` use a child's terms two or three times, and the text prints them as a tree, so a deep `greatest` or `least` nest prints a long line. Binding each node once with `define-fun` belongs in 506b, where the decimal networks need it. - **The completeness test re-runs the reference sweep** to learn which records were rendered, and the samples' tree walks keep `default` arms. Both are test-time costs and length; `VarkaSmtNodes.template`, which states the meanings, stays exhaustive. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Opus 5.5)
Moved couple rules out of NullPropagation and added more comments.