Documentation
¶
Overview ¶
Package equiv proves, with an SMT solver, that a candidate GPU kernel computes bit for bit what a reference kernel computes, for every input a launch contract allows. Device parity on sampled inputs tests that claim; this package proves it, for a restricted class of kernels.
Class ¶
Elementwise kernels: each element of one output buffer is a fixed expression of the input elements at the same index. Bounds guards, index arithmetic, vector accesses and grid-stride loops are in the class; reductions are not (floating-point reassociation changes results), nor are shared memory, atomics, barriers or several outputs.
Claim ¶
A Contract names In buffers, one Out buffer and the element count n. For each kernel, under its Launch, Check proves for a symbolic thread (all block and thread indices, all loop iterations, all n):
- every load lies in an In buffer and every store in the Out buffer, aligned to the access's full width (no out-of-bounds access);
- every element of the Out buffer is written (coverage);
and, for a pair of symbolic threads, one per kernel, that whenever both store to the same output element they store the same bits. Since no thread reads what any thread writes, these imply equal final memory. Coverage names the writer of element i (block (i/E) mod grid, loop iteration i/(E·grid), every thread of the block tried), so it needs no quantifier; when the guess fails the claim is unknown unless a second query shows no thread writes i.
Verdicts ¶
Proved means the solver answered exactly "unsat" for the negated claim under the stated semantics. Refuted means it produced a model and a concrete run of the kernels (Runner) reproduced the violation from it. Anything else is unknown: a timeout, an instruction outside the supported set, a counterexample replay did not reproduce. A check that the premises are satisfiable guards against vacuous proofs.
Modeled, not proved ¶
- Buffers do not overlap, do not wrap the address space, and are aligned as the Launch says (CUDA allocations are 256-byte aligned).
- An access must be aligned to its full width, vector lanes included; a misaligned one counts as a violation.
- A floating-point operation that produces a NaN writes the NaN 0x7fffffff. This is assumed, not verified; NaN payloads are not modeled, so a+b and b+a are equal.
- Launches are one-dimensional; generic and global addresses agree.
- The front end's semantics of its instruction set (see ptxequiv).
Trusted base ¶
The front end that translates a kernel into an Effect, the term simplifications and SMT-LIB emission in this package, and the solver (z3, run as a subprocess) are trusted. The simplifications fold constants and order the operands of IEEE add, multiply and fma, which commute; z3 alone may time out proving a+b = b+a in binary32. Counterexamples are cross-checked by a concrete interpreter written separately from the translator; proofs are not.
Package equiv imports only the standard library.
This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.
Index ¶
- Constants
- Variables
- func Mentions(t *Term, vs map[*Term]bool) bool
- type Access
- type Arg
- type Builder
- func (b *Builder) AShr(x, y *Term) *Term
- func (b *Builder) Add(x, y *Term) *Term
- func (b *Builder) And(xs ...*Term) *Term
- func (b *Builder) BV(v uint64, w int) *Term
- func (b *Builder) BVAnd(x, y *Term) *Term
- func (b *Builder) BVNot(x *Term) *Term
- func (b *Builder) BVOr(x, y *Term) *Term
- func (b *Builder) BVXor(x, y *Term) *Term
- func (b *Builder) Bool(v bool) *Term
- func (b *Builder) Concat(hi, lo *Term) *Term
- func (b *Builder) Eq(x, y *Term) *Term
- func (b *Builder) Extract(x *Term, hi, lo int) *Term
- func (b *Builder) FAbs(x *Term) *Term
- func (b *Builder) FAdd(rm int, x, y *Term) *Term
- func (b *Builder) FDiv(rm int, x, y *Term) *Term
- func (b *Builder) FEq(x, y *Term) *Term
- func (b *Builder) FFma(rm int, x, y, z *Term) *Term
- func (b *Builder) FFromInt(rm int, x *Term, sign bool, w int) *Term
- func (b *Builder) FIsNaN(x *Term) *Term
- func (b *Builder) FIsNegative(x *Term) *Term
- func (b *Builder) FIsSubnormal(x *Term) *Term
- func (b *Builder) FLeq(x, y *Term) *Term
- func (b *Builder) FLt(x, y *Term) *Term
- func (b *Builder) FMul(rm int, x, y *Term) *Term
- func (b *Builder) FNeg(x *Term) *Term
- func (b *Builder) FSqrt(rm int, x *Term) *Term
- func (b *Builder) FSub(rm int, x, y *Term) *Term
- func (b *Builder) FZero(s Sort, neg bool) *Term
- func (b *Builder) FromBits(x *Term) *Term
- func (b *Builder) Implies(x, y *Term) *Term
- func (b *Builder) Ite(c, x, y *Term) *Term
- func (b *Builder) LShr(x, y *Term) *Term
- func (b *Builder) Mul(x, y *Term) *Term
- func (b *Builder) Neg(x *Term) *Term
- func (b *Builder) Not(x *Term) *Term
- func (b *Builder) Or(xs ...*Term) *Term
- func (b *Builder) Resize(x *Term, w int, sign bool) *Term
- func (b *Builder) Select(mem, off *Term) *Term
- func (b *Builder) Shl(x, y *Term) *Term
- func (b *Builder) SignExt(x *Term, w int) *Term
- func (b *Builder) Sle(x, y *Term) *Term
- func (b *Builder) Slt(x, y *Term) *Term
- func (b *Builder) Sub(x, y *Term) *Term
- func (b *Builder) ToBits(f *Term) *Term
- func (b *Builder) UDiv(x, y *Term) *Term
- func (b *Builder) URem(x, y *Term) *Term
- func (b *Builder) Ule(x, y *Term) *Term
- func (b *Builder) Ult(x, y *Term) *Term
- func (b *Builder) Var(name string, s Sort) *Term
- func (b *Builder) ZeroExt(x *Term, w int) *Term
- type ConcreteThread
- type Contract
- type Effect
- type Event
- type Finding
- type Kernel
- type Kind
- type Launch
- type Obligation
- type Op
- type Query
- type Report
- type Result
- type Role
- type Runner
- type Solver
- type Sort
- type Status
- type Term
- type Thread
- type Verdict
- type Z3
Constants ¶
const ( RNE = iota // round to nearest, ties to even RTZ // toward zero RTN // toward negative infinity RTP // toward positive infinity )
Rounding modes for floating-point operators.
const CanonicalNaN32 = 0x7fffffff
CanonicalNaN32 is the bit pattern a binary32 operation that produces a NaN is modeled to write. See the package documentation.
Variables ¶
Functions ¶
Types ¶
type Access ¶
type Access struct {
Site string // source position and instruction text
Guard *Term // the access happens iff Guard holds
Addr *Term // 64-bit address of lane 0
ElemBytes int // bytes per lane
Lanes int // vector width
Values []*Term
}
An Access is one guarded memory instruction of a thread.
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
A Builder interns terms so that structurally equal terms are shared. It is safe for concurrent use. The zero Builder is not usable; call NewBuilder.
func (*Builder) BV ¶
BV returns the w-bit constant v (truncated to w bits). Widths above 64 are allowed only for v that fit in 64 bits.
func (*Builder) Eq ¶
Eq returns x = y. On floating-point terms it is SMT-LIB equality: NaN equals NaN and +0 differs from -0.
func (*Builder) FAdd ¶
FAdd, FMul and FFma order their commutative operands canonically, so a+b and b+a are one term. IEEE 754 addition and multiplication commute except for NaN payloads, which are not modeled.
func (*Builder) FFromInt ¶
FFromInt converts the bit vector x, read as signed if sign is set, to a floating-point value of w bits with rounding mode rm.
func (*Builder) FIsNegative ¶
func (*Builder) FIsSubnormal ¶
func (*Builder) ToBits ¶
ToBits returns the bits of the floating-point value f. A NaN becomes CanonicalNaN32.
type ConcreteThread ¶
A ConcreteThread is a Thread with concrete values.
type Contract ¶
type Contract struct {
Args []Arg
NMax uint64 // largest element count; at most 1<<31 - 1
NMultiple uint64 // N is a multiple of this; 0 or 1 means no constraint
}
A Contract is the input contract under which two kernels are compared.
Every buffer holds N elements, is aligned as the Launch of each kernel says, does not wrap the address space and does not overlap another buffer. N ranges over [0, NMax] and is a multiple of NMultiple.
type Effect ¶
type Effect struct {
Loads []Access
Stores []Access
// Loop reports that the accesses are those of iteration Thread.Iter
// of the kernel's one loop, and Guard includes "that iteration runs".
Loop bool
// Obligations must hold for every thread of the launch for the
// effect to describe the kernel exactly.
Obligations []Obligation
// Unsupported, if non-empty, says why the effect is not exact. Any
// verdict that depends on it is Unknown.
Unsupported []string
}
An Effect is what one thread does: its guarded global memory accesses, plus obligations the front end needs discharged for the effect to be exact.
type Event ¶
type Event struct {
Store bool
Addr uint64
Bytes int // total bytes
Lanes int
Value []uint64 // per lane
Site string
}
An Event is one memory access of a concrete run.
type Finding ¶
type Finding struct {
Kernel string // "ref", "cand" or "both"
Name string
Verdict Verdict
Detail string // why unknown, or the counterexample
Elapsed time.Duration
}
A Finding is the verdict on one proof obligation.
type Kernel ¶
type Kernel interface {
// Name names the kernel in reports.
Name() string
// Effect returns the effect of thread th, building terms with b.
Effect(b *Builder, th *Thread) (*Effect, error)
}
A Kernel is a kernel under a semantics: it gives the effect of one thread.
type Launch ¶
type Launch struct {
Block int // threads per block
// ElemsPerBlock, if positive, launches ceil(N/ElemsPerBlock) blocks.
// Otherwise Grid, if positive, launches that many blocks, and if Grid
// is zero too, the grid is any size in [1, GridMax].
ElemsPerBlock int
Grid uint64
GridMax uint64
// Params maps each kernel parameter to the index of a contract
// argument, or to -1 for a parameter the contract does not describe;
// such a parameter may hold any value.
Params []int
// Align and Offset give each In and Out buffer's address modulo
// Align: an address is Offset[i] past a multiple of Align. Align is
// 256 (the CUDA allocation alignment) if zero; Offset entries
// default to zero.
Align uint64
Offset map[int]uint64 // contract argument index → byte offset
}
A Launch is how one kernel is launched: a one-dimensional grid of one-dimensional blocks.
type Obligation ¶
An Obligation is a formula the front end needs to be valid.
type Op ¶
type Op uint8
An Op is a Term operator. Bit-vector, Boolean and floating-point operators have their SMT-LIB meaning; see the comments for the others.
const ( OpVar Op = iota + 1 OpConst OpNot OpAnd OpOr OpIte OpEq OpBVAdd OpBVSub OpBVMul OpBVUDiv OpBVURem OpBVAnd OpBVOr OpBVXor OpBVNot OpBVNeg OpBVShl OpBVLShr OpBVAShr OpBVUlt OpBVUle OpBVSlt OpBVSle OpConcat OpExtract // aux[0]=hi, aux[1]=lo OpZeroExt // aux[0]=bits added OpSignExt // aux[0]=bits added OpSelect // byte of a memory at an offset OpFromBits OpToBits // floating point to bits; every NaN becomes the canonical quiet NaN OpFPAdd // aux[0]=rounding mode OpFPSub OpFPMul OpFPDiv OpFPFma OpFPSqrt OpFPNeg OpFPAbs OpFPIsNaN OpFPIsSubnormal OpFPIsNegative OpFPLt OpFPLeq OpFPEq // IEEE equality: false on NaN, +0 equals -0 OpFPFromSBV OpFPFromUBV )
type Query ¶
A Query asks whether Assert is satisfiable. Values names terms whose model values are wanted when it is.
type Report ¶
A Report is the outcome of Check.
func Check ¶
func Check(ctx context.Context, s Solver, c *Contract, ref Kernel, refL *Launch, cand Kernel, candL *Launch) (*Report, error)
Check tries to prove that launching cand as candL computes the same final memory as launching ref as refL, for every input the contract allows. It proves, for each kernel: every load reads inside an In buffer and every store writes inside the Out buffer, naturally aligned; every element of the Out buffer is written; and every store of cand writes the value any store of ref writes to the same element.
Check returns an error only for a malformed contract or launch, or a solver that cannot run; a kernel outside the supported class yields an Unknown verdict.
type Result ¶
type Result struct {
Status Status
Values []uint64 // model values of Query.Values when Status is Sat (Booleans are 0 or 1)
Detail string // solver output when Status is Unknown
}
A Result is a solver's answer to a Query.
type Runner ¶
type Runner interface {
Run(th *ConcreteThread, load func(addr uint64, n int) uint64, maxSteps int) ([]Event, error)
}
A Runner is a Kernel that can also execute one thread on concrete values. Check uses it to replay counterexamples: a Refuted verdict needs both the solver's model and a concrete replay that shows the violation.
type Term ¶
type Term struct {
// contains filtered or unexported fields
}
A Term is an immutable expression built by a Builder. Structurally equal terms from one Builder are the same pointer.
type Thread ¶
type Thread struct {
Prefix string // unique among the threads of one query
CTAID, NCTAID, TID, NTID *Term // 32-bit x components; y and z are 0 (ids) and 1 (counts)
// Iter is the 64-bit iteration of the kernel's loop the effect
// describes. A kernel without a loop ignores it.
Iter *Term
Params []*Term // kernel parameter values, by parameter position
// Load returns the n-byte little-endian value at a 64-bit address.
Load func(addr *Term, n int) *Term
}
A Thread names the values one thread of a launch sees. Kernel.Effect expresses a thread's accesses in terms of them.
type Verdict ¶
type Verdict int
A Verdict is the outcome of a check.
const ( // VerdictUnknown means the check neither proved nor refuted the // claim: the solver gave up, the kernel left the supported class, or // a counterexample could not be confirmed. VerdictUnknown Verdict = iota // Proved means the solver found the negated claim unsatisfiable // under the stated semantics and contract. Proved // Refuted means the solver produced a counterexample. Refuted )