github ↗
docs menu

Module Ops.Upat

Patterns.

A pattern describes nodes by their operation, type, argument and sources, and names the nodes it matches. A name used twice must match the same node.

#type node := t
#type t

The type for patterns.

#val v : ?op:Op.Set.t -> ?dtype:Dtype.t list -> ?src:t list -> ?perm:t list -> ?each:t -> ?allow_any_len:bool -> ?arg:arg -> ?name:string -> ?early_reject:Op.t list -> unit -> t

v () matches the nodes of an operation in op, a type in dtype and argument arg, each when given. Sources are matched in order by src, in any order by perm, or each by each; src and perm require exactly as many sources unless allow_any_len. name names the node matched. early_reject is operations the node's sources must include for the pattern to be tried, by default the operations the source patterns require.

Arguments compare as numbers do: `Int 0, `Float 0. and `Bool false are equal, and so are two NaNs.

Raises Invalid_argument if more than one of src, perm and each is given.

#val op : ?dtype:Dtype.t list -> ?src:t list -> ?perm:t list -> ?each:t -> ?allow_any_len:bool -> ?arg:arg -> ?name:string -> ?early_reject:Op.t list -> Op.t -> t

op o is v ~op:(Op.Set.of_list [o]) ().

#val wild : t

wild is v (), which matches any node.

#val var : ?dtype:Dtype.t list -> string -> t

var ~dtype name matches any node of a type in dtype, named name.

#val cvar : ?dtype:Dtype.t list -> ?arg:Dtype.const -> string -> t

cvar ~dtype ~arg name matches a constant, named name.

#val const : ?dtype:Dtype.t list -> Dtype.const -> t

const ~dtype c matches the constant c.

#val any : t list -> t

any ps matches what any of ps matches.

#val named : string -> t -> t

named name p is p naming the node it matches name.

#val or_casted : ?name:string -> t -> t

or_casted p matches p or a cast of it.

#val or_bitcasted : ?name:string -> t -> t

or_bitcasted p matches p or a bitcast of it.

#val or_after : ?name:string -> t -> t

or_after p matches p or p ordered after anything.

#val f : ?dtype:Dtype.t list -> ?name:string -> ?arg:arg -> ?allow_any_len:bool -> t -> Op.t -> t

f p o matches the node o of the one source p matches: f p Cast matches a cast of p to any type.

#val sink : ?name:string -> t list -> t
#val index : ?name:string -> ?allow_any_len:bool -> t -> t list -> t
#val load : ?name:string -> ?allow_any_len:bool -> t -> t list -> t
#val store : ?name:string -> ?allow_any_len:bool -> t -> t list -> t
#val reduce : ?name:string -> ?op:Op.t -> ?allow_any_len:bool -> t -> t list -> t
#val broadcast : ?name:string -> t -> t

broadcast p matches a stack whose sources each match p.

#val after : ?name:string -> ?allow_any_len:bool -> t -> t list -> t
#val end_ : ?name:string -> ?allow_any_len:bool -> t -> t list -> t
#val backedge : ?name:string -> t -> loop:t -> cond:t -> t
#val bitcast : ?dtype:Dtype.t -> t -> t

bitcast ~dtype p matches a bitcast of p, to dtype if given.

#val dtype : t -> Dtype.t

dtype p is the first type p requires, or Dtype.t.Void.

Commutative operations match their operands in either order, and the literals of O match constants of any type (cvar). cast p dt is p if p requires exactly dt. // and % match Op.t.Div and Op.t.Mod.

include Elementwise with type t := t
#val alu : t -> Op.t -> t list -> t

alu x op rest is the operation op on x :: rest as given.

#val cast : t -> Dtype.t -> t

cast x dt is x converted to dt, or x if it is of type dt.

Arithmetic

add, mul, lt, eq, maximum, bitwise_and, bitwise_or, bitwise_xor, shl and shr are Op.t.Add, Op.t.Mul, Op.t.Cmplt, Op.t.Cmpeq, Op.t.Max, Op.t.And, Op.t.Or, Op.t.Xor, Op.t.Shl and Op.t.Shr of their promoted operands. reciprocal, trunc, sqrt, exp2 and log2 are Op.t.Recip, Op.t.Trunc, Op.t.Sqrt, Op.t.Exp2 and Op.t.Log2 of their operand. The others are built from these.

#val add : t -> t -> t
#val mul : t -> t -> t
#val lt : t -> t -> t
#val eq : t -> t -> t
#val maximum : t -> t -> t
#val bitwise_and : t -> t -> t
#val bitwise_or : t -> t -> t
#val bitwise_xor : t -> t -> t
#val shl : t -> t -> t
#val shr : t -> t -> t
#val reciprocal : t -> t
#val trunc : t -> t
#val sqrt : t -> t
#val exp2 : t -> t
#val log2 : t -> t
#val sub : t -> t -> t

sub x y is x + neg y.

#val neg : t -> t

neg x is logical_not x for booleans and x * -1 otherwise.

#val div : ?rounding:[ `Trunc | `Floor ] -> t -> t -> t

div ~rounding x y divides integers by Op.t.Truncdiv or Op.t.Div when rounding; otherwise it multiplies x, as a float, by y's reciprocal, then rounds as asked.

#val mod_ : t -> t -> t

mod_ x y is the remainder of div ~rounding:`Floor x y.

#val fmod : t -> t -> t

fmod x y is the remainder of div ~rounding:`Trunc x y.

#val floor : t -> t

floor x is trunc x, less one where that is above x.

#val pow : t -> t -> t

pow x y is Op.t.Pow of x and y: promoted to a common type, but for an integer x and a weak integer y, which keeps its value at no width (Op.t.Pow's exponent is a number).

Raises Invalid_argument if the promoted type is not a float and y is a negative integer constant.

#val gt : t -> t -> t

gt x y is lt y x.

#val le : t -> t -> t

le x y is logical_not (gt x y).

#val ge : t -> t -> t

ge x y is logical_not (lt x y).

#val ne : t -> t -> t

ne x y is logical_not (eq x y).

#val logical_not : t -> t

logical_not x is eq (cast x Bool) false.

#val bitwise_not : t -> t

bitwise_not x is logical_not x for booleans, and x xor all ones otherwise.

#val where : t -> t -> t -> t

where c x y is x where c holds and y elsewhere.

#val minimum : t -> t -> t

minimum x y is neg (maximum (neg x) (neg y)) for floats; for integers, the same through an order-reversing xor.

#module O : sig ... end

Operators, for local opening: O.(x + int 4 * y). Each is the function of the same meaning: + is add, // is div ~rounding:`Floor, % is mod_, <> is ne, land, lor, lxor and lnot are the bitwise operations, lsl and lsr the shifts.

The operators are also at the top, so that Upat.(var "x" + cvar "c") reads as the pattern it builds.

include module type of O
#val int : int -> t

int n is the weak literal n.

#val float : float -> t

float x is the weak literal x.

#val bool : bool -> t

bool b is the literal b.

#val (+) : t -> t -> t
#val (-) : t -> t -> t
#val (*) : t -> t -> t
#val (/) : t -> t -> t
#val (//) : t -> t -> t
#val (%) : t -> t -> t
#val (~-) : t -> t
#val (<) : t -> t -> t
#val (>) : t -> t -> t
#val (<=) : t -> t -> t
#val (>=) : t -> t -> t
#val (<>) : t -> t -> t
#val (land) : t -> t -> t
#val (lor) : t -> t -> t
#val (lxor) : t -> t -> t
#val lnot : t -> t
#val (lsl) : t -> t -> t
#val (lsr) : t -> t -> t
#val match_ : t -> node -> (string * node) list list

match_ p u is the naming of each way p matches u, in order: the alternatives of any in turn, the orderings of a commutative operation's operands in the order of their permutations, the first operand varying slowest, and each source's namings before the next source's; [] if p does not match. Ways that differ only within a part of p that names nothing give one naming.