nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
fusion_rule.hpp
1// core/include/nk/core/fusion_rule.hpp
2//
3// Element-wise fusion rule registry — the GENERIC, domain-free half of VM
4// element-wise fusion. The engine owns a list of FusionRule; the standard
5// library (bundle) registers concrete rules (idiom → ops kernel). The engine
6// and both backends iterate the rules without knowing any specific idiom, so
7// adding a fused idiom later touches only the kernel (ops/) and its rule
8// (bundle/) — never core.
9//
10// Each rule is two closures:
11// * match — pure AST inspection: is this node the idiom? If so, return the
12// operand sub-expressions (in the order `execute` expects). The
13// compiler / TreeWalker evaluate those operands themselves.
14// * execute — runtime fast path: given the evaluated operand Values, fill
15// `out` and return true iff the inputs fit the kernel (real
16// double, conformable, large enough). Return false to fall back
17// to the ordinary per-op path. The fast path is computed
18// bit-identically to that path, so fused == unfused.
19//
20// The `execute` closure is where the type/shape guard and the ops-kernel call
21// live, so core needs no knowledge of kernel signatures (the bundle captures
22// them). This is what makes new idioms purely additive — see the FUSE_EWISE
23// opcode / TreeWalker try-path consumers.
24
25#pragma once
26
27#include <cstddef>
28#include <functional>
29#include <memory_resource>
30#include <optional>
31#include <vector>
32
33namespace nk {
34
35struct ASTNode;
36class Value;
37
38struct FusionRule {
39 const char *name = "";
40
41 // AST match → operand sub-expressions (executor order), or nullopt.
42 std::function<std::optional<std::vector<const ASTNode *>>(const ASTNode *)>
43 match;
44
45 // Evaluated operands → fused result. true = fused (out filled);
46 // false = guard failed, caller must fall back to the per-op path.
47 std::function<bool(const Value *operands, std::size_t nOperands, Value &out,
48 std::pmr::memory_resource *mr)>
49 execute;
50};
51
52} // namespace nk
Core MATLAB-compatible multidimensional array and scalar value type.
Definition value.hpp:61