From 3b3eb3b8f832a6b0f1a1d945bc2ebefea14eee14 Mon Sep 17 00:00:00 2001 From: Chris Gyurgyik Date: Wed, 10 Sep 2025 13:31:58 -0700 Subject: [PATCH 1/7] Fix test. (#207) --- tests/bonsai/error/struct_duplicate_default.expect | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/bonsai/error/struct_duplicate_default.expect b/tests/bonsai/error/struct_duplicate_default.expect index de8c2d161..d3b6bc9ee 100644 --- a/tests/bonsai/error/struct_duplicate_default.expect +++ b/tests/bonsai/error/struct_duplicate_default.expect @@ -1,7 +1,7 @@ ---CODE--- 1 ---STDERR--- -[internal] Error: src/Parser/Parser.cpp:449 +[internal] Error: src/Parser/Parser.cpp:451 --> !defaults.contains(field_name) Duplicate default value for field o of element Ray individually marked: f32x3(2f) and group marked: f32x3(1f) From 341df5c2342bca3fe6f09e50c8790df2b03d4bbd Mon Sep 17 00:00:00 2001 From: Alexander J Root Date: Sun, 30 Aug 2026 00:11:45 -0700 Subject: [PATCH 2/7] Mergefix with PLDI26 Bonsai (#208) * Fix the Linux/GCC build CI only builds on macOS with libc++, which transitively provides several standard headers that libstdc++ does not, and links against a monolithic LLVM dylib that resolves every component. Neither holds on Linux with GCC and a component-wise LLVM install, so the build failed outright. - Add the standard headers that were being picked up transitively: (CPP.cpp), and (Token.h), (Trees.cpp, Layouts.cpp, Defers.cpp), (DCE.cpp, LoopTransforms.cpp). - Request the LLVM components src/CodeGen uses directly: mc, targetparser, codegen, transformutils, instcombine, scalaropts, ipo. * Port the reduction IR nodes from ajr/pldi-benchmarks Brings over the IR-level machinery the paper's associative and idempotent reductions need, without any of the benchmarking scaffolding: - AggOp{avg, count, prod, sum}, the set-level reductions of Fig. 2 that take no lambda. - SetOp::minimum, the paper's set-level `min`. Spelled `minimum` because `min` is already the binary scalar intrinsic and the vector reduction. - Annotation::Aggregate, the reduction augmentations of Section 4 (`with min(id) = idl`), alongside Data/Volume/Interval. - Accumulate::Min/Max, plus LLVM and CUDA emission for them and for the previously unhandled Argmax. - Scan carries an optional reduction op, so a scan can aggregate subtree results with something other than set union. - Infinity becomes Extrema{inf, eps}; reductions need an epsilon for bounds that are not tight. `eps` is now parseable alongside `inf`, and both still print lowercase so they round-trip with the surface syntax. Deliberately not ported from that branch: the commented-out else-if chain in Printer::visit(IfElse), and the machine_epsilon change. Both are unrelated to operators and would churn every golden. * Parse the reduction operators and augmentations - `minimum(metric, set)` and the set aggregations `avg` / `count`. - `sum` and `prod` share a spelling with the vector reductions, so they dispatch on the argument's type in VectorReduce::make rather than getting their own parser entry. - Reduction augmentations in the schedule: `with min(id) = idl`. - `eps[[T]]` now produces a typed Extrema instead of being folded to a float immediate at parse time. Adding `count` as a builtin collided with layout field names, e.g. the `count : u32; group[count] index : u32` in lower/trace.bonsai. A builtin name now only denotes an intrinsic where it is actually applied, as `f(x)` or `f[[T]](x)`. A bare builtin name was already an error, so this widens what parses rather than narrowing it. * Lower minimum, and track reduction augmentations in traversals The Rewriter that walks a Match now also collects, per arm, the reduction augmentations a node stores and any volumes annotated on its children, alongside the bounding volume and scalar intervals it already tracked. build_minimum implements Algorithm 2: value-based pruning comes from fusing `M(x) < best` into the inner filter and letting predicate analysis bound it, with the accumulator's interval recorded as (-inf, best]. An empty-able set yields an option, matching build_argmin. Predicate analysis gains the rules that reductions over scalar fields need: Div, Select, Access, and Intrinsic::max, plus tighter Mul bounds. A geometric operand that varies without a bounding volume now yields an undefined interval instead of an internal error -- "cannot be bounded" is an ordinary outcome that makes the traversal recurse unconditionally. For the same reason, a filter whose upper bound is trivially true no longer emits a guard. count is lowered here too, but as the branch's special case; the next commit replaces it with the general map-then-reduce form. * Generalize count into map followed by reduce The ported count lowering was a special case: a bespoke accumulator that only fired when a node stored a `count()` augmentation. Replace it with the general form the paper describes, where count is a map of every element to 1 followed by an add-reduction with identity 0. - AggOp gains `reduce`, carrying an identity and an associative, commutative binary combiner. `count`, `sum` and `prod` become sugar that expand_aggregate() rewrites into it. - build_map implements Algorithm 1 lines 9-12, and Scan now carries the map applied to subtree elements so a scan under a map stays faithful. - build_reduce implements Algorithm 1 lines 13-22. It recognises which augmentation, if any, computes the reduction over a subtree by matching the combiner against the map feeding it: summing a constant 1 is a `count()`, summing a field f is a `sum(f)`. When a node stores that aggregate the subtree is folded in wholesale; otherwise the scan carries the reduction and recurses. - Updates are emitted as an Accumulate for combiners the backends can apply in place, and otherwise as a store of the inlined combiner, so an arbitrary user-written reduce still lowers. `avg` still errors: it needs a pair reduction over a running sum and count, which the combiner machinery does not build yet. * Complete the set operators of Figure 2 Adds maximum, argmax, any and all, and general reduce, so every operator in the paper's Figure 2 now lowers onto a tree. - SetOp::make is restructured around shared validation (the lambda must accept the set's elements; the result is optional when the set can be empty), replacing the near-duplicate argmin and minimum branches. - minimum/maximum and argmin/argmax are one lowering each, parameterized by which end of the metric they seek: the identity, the accumulate op, the fused "improves on best" comparison, the accumulator's own interval and which subtree bound tightens it all follow from that direction. - build_extremum implements the rest of Algorithm 2: an included subtree is folded in wholesale when the node stores the matching min/max augmentation, and a recursive call first tightens the accumulator with the metric's bound over the subtree. - build_quantifier implements Algorithm 4. Both directions short-circuit once the accumulator reaches the absorbing element of the boolean lattice, and skip a subtree whose predicate is proven always or never true. - reduce(identity, combiner, set) is now surface syntax; `sum` over a set and an explicit reduce produce identical traversals. Two bugs fixed along the way, both about bounds not being found: predicate_analysis ignored a tagged interval on the top-level expression, because it accepted the expression directly instead of going through the interval-map lookup that its subexpressions use; and build_extremum built its volume map without the matching interval map. Together these made `minimum(|p| p.id, points)` tighten the accumulator with a dangling `p.id` instead of the node's stored upper bound. * Derive bounds for every geometric predicate Extends GeomOp with the rest of Figure 1 -- covers, disjoint, equals, touches, within, and the per-dimension ordering predicates lex/ley/lez and ltx/lty/ltz -- and replaces the three hand-written cases in predicate analysis with the paper's two tables. geom_upper_bound is Algorithm 5 and geom_lower_bound Algorithm 6 lines 1-13, each transcribed case by case over the three configurations the paper enumerates: uniform-varying, varying-uniform, and both varying. An undefined result is the algorithms' trailing `_ |-> true` and `_ |-> false`. Metric bounds come from Algorithm 6 lines 15-25, which also fills in distmax; only distmin was handled before. The ordering predicates are the one family the paper leaves out "for brevity". They are monotonic, so the sufficient condition is the same relation between the bounding volumes, and the necessary condition is the negation of the strict counterpart with the operands swapped. Two deliberate deviations, both noted at the rules: - contains(v, u) keeps this repo's contains(V_v, u) upper bound rather than the paper's intersects(V_v, u). It is strictly tighter and equally sound: a b inside a is inside a's volume too. - equals(v, u) uses within(u, V_v), matching the symmetric case, since equality constrains the uniform operand to fit inside the varying operand's volume regardless of argument order. lower/bvh.expect changes because filters on `intersects` now get a sufficient condition. That is the specialization the paper's Figure 5b shows, where `always` becomes `contains`. * Implement the geometric predicates in the standard library stdlib/topology.bonsai defines the topological and ordering relations for the pairs of shapes they are meaningful on. The AABB-against-AABB relations are not optional: predicate analysis derives its pruning conditions in terms of bounding volumes, so it emits calls like covers(u, V) and disjoint(V0, V1) that lower-geometrics has to resolve. The rest are what user queries are written against: AABB against Point, Sphere and Triangle, with box-triangle overlap done by the separating axis test. A ray has empty interior in three dimensions, so it can only contain degenerate geometry; contains(Ray, ...) reports false. That is sound in both roles the rule plays -- as a sufficient condition a false negative only costs pruning, and under negation it is the weakest necessary condition. stdlib/distance.bonsai gains the metrics on bounding volumes that Algorithm 6 lines 15-25 need: every distmin a query uses has to have a distmax counterpart on the volumes, and a closest-point query over a Point tree needs distmax(Point, AABB), which was missing. Verified by lowering one query per predicate against these definitions and checking each derived always/maybe pair against Algorithms 5 and 6. The two trace goldens grow because they import stdlib/distance and print the whole program. * Bound the boolean combinators in predicate analysis Section 6.2 gives rules for and, or and not, but only `&&` was implemented. Predicates are usually written with `&` and `|`, which on booleans mean the same thing, and negation appeared only as something the analysis itself produced (disjoint's upper bound) and never as something it could analyse. - `&`, `|` and `||` join `&&`: all four are monotonically increasing in both arguments, so each bound is that operator over the corresponding bounds. - Negation replaces a hard error. Logical not is monotonically decreasing, so its bounds are the negated bounds swapped; arithmetic negation reverses the order of the bounds the same way. With these, the paper's Section 2 range query `filter(|x| lo <= x.id & x.id <= hi, ...)` lowers to exactly the always/maybe pair the paper shows. * Use stored extrema to skip whole subtrees The min/max augmentation lookup added with build_extremum could never fire. Fusing the value-based condition into a filter is what makes predicate analysis prune on it, but with no filter to fuse into the wrapper was synthesised anyway, and its always-branch -- the `scan` construct standing for a wholly included subtree -- was then dropped because `M(x) < best` has no lower bound. The inclusion case of Section 2 was unreachable. Now the wrapper is only built when there is a real filter to fuse into. Lowering the set as it is keeps the scan, and build_extremum handles it: a node storing this extremum over its subtree settles the contribution exactly and is never visited, and otherwise the subtree is skipped when it cannot beat the running best and tightens the accumulator when it can. `minimum(|p| p.id, points)` over a tree annotated `with min(id) = idl` now reads the root's stored minimum and stops, and without the annotation falls back to the `min_wmax` traversal on page 10. * Add per-operator lowering tests, and fix the C++ runtime on libstdc++ tests/bonsai/lower/setops covers each operator of Figure 2 over one small interval tree, so the goldens show the traversal each lowering produces and nothing else. The range predicate is the paper's Section 2 example, which makes the always and maybe conditions directly comparable to it. Separate augmented variants show a stored count or extremum folding a whole subtree in without visiting it. tests/bonsai/lower/geomops covers each geometric operator, in both operand orders where Algorithms 5 and 6 give different rules, plus a dual-tree query for the both-varying cases and a metrics query for Algorithm 6 lines 15-25. These stop before lower-geometrics, so they check the derived conditions without needing geometry implementations. stdlib-resolution.bonsai goes all the way through against the standard library and so checks the other half: that every operator the analysis emits on a bounding volume has an implementation to resolve to. Also fixes runtime/bonsai_cpp.h, which pulled std::round, std::abs and friends into the global namespace and used bit_cast and memcpy without including the headers that declare them. libc++ supplies them transitively, so this only showed up off macOS -- it broke every correctness/cpp test on Linux. Three of the four now pass; vec still fails on what looks like a vector-ABI difference between the generated object code and the hand-written driver. * Run the set operators end to end against a linear scan correctness/cpp/setops builds a real interval tree and checks count, sum, minimum, maximum, any and all against an unpruned scan over the same data, across ranges that are empty, inside one subtree, spanning several, and covering everything. A traversal that prunes a subtree it should have visited, or double-counts one it folded in from an augmentation, shows up as a mismatch rather than as a golden that merely looks plausible. Getting there needed the parts of the pipeline a reduction over a tree touches, which nothing had exercised before: - A scan over a wholly included subtree can now carry a reduction and a map, so it folds elements into an accumulator instead of unioning them into a set. That was the standing "support scan augmentations" TODO. Scan carries the accumulator it reduces into, and scan functions are shared per (trees, reduction, map). - A leaf's array is iterated elementwise when reducing. Appending an iterable adds all of it at once, which is right for a set and wrong for an accumulator. - The C++ backend gained Accumulate, scalar Allocate, Extrema and Build, and now passes mutating arguments by reference -- without which a recursive traversal updated a copy of the accumulator and the result was always the identity. Also drops tests/bonsai/error/string-literal.expect, which has no corresponding .bonsai and is not registered, and corrects the label list in tests/README.md. * Apply clang-format to the lines this branch changed CI runs clang-format 19 over the diff. * Spell the equals upper bound as the paper does Algorithm 5 line 17 gives the upper bound of equals(v, u) as contains(V_v, u), not within(V_v, u) as I first read it. The two differ only in which operator names the relation, and contains(V_v, u) is exactly the within(u, V_v) that was already being emitted -- so the behaviour was right, but the earlier commit described a departure from the paper that does not exist. Use the paper's spelling. Each single-varying case now keeps the volume in the position its operand held, which is the convention the algorithms follow throughout: within(u, V_v) when the second operand varies, contains(V_v, u) when the first does. The contains(v, u) deviation noted earlier does stand: Section 6.3 states that bound as intersects(V_v, u), where this repo uses the tighter and equally sound contains(V_v, u). * Correct two more Algorithm 5 rules Checked every rule in Algorithms 5 and 6 against the text extracted from the paper rather than against a rendering of the page, which is how the equals rule got misread. Two upper bounds were wrong: - covers(v, u) is covers(V_v, u) on line 9, not intersects(V_v, u). Everything a varying operand covers lies inside its bounding volume, so this prunes strictly more. - disjoint(v, u) is not-within(V_v, u) on line 12. Same relation as the not-contains(u, V_v) being emitted, but spelled with the volume in the position its operand held, which is the convention the algorithms follow and which decides the overload lower-geometrics resolves to. Everything else matches, including all of Algorithm 6 and the metric bounds. The one remaining deviation is contains(v, u), where line 6 gives intersects(V_v, u) and this repo uses the tighter contains(V_v, u); the comment there now notes the paper uses that same tight form for the corresponding covers case. * Keep the fused node bound where no augmentation can replace it Two findings from replaying the pldi-benchmarks suite against this branch. The closest-point benchmark lost a pruning step. Synthesizing the `M(x) < best` filter does two things: it lets predicate analysis prune on the value, and it puts the node's distance bound in front of every match arm, so a leaf popped from the traversal queue is rejected before its primitives are scanned. Skipping that whenever there was no filter to fuse into gave up the second for subtrees that could never have used a stored extremum anyway. Now it is skipped only when the metric names a field an augmentation might cover, which is exactly when the `scan` it consumes is worth keeping. Counting a leaf became a loop. The elements of a leaf are summed one at a time, where asking the leaf how many it holds is a single operation. Summing the same constant over every element is now that constant times the count, which restores the generated code the benchmarks had. This needs the free `count()` overloads on `set` and `range`, ported from pldi-benchmarks; correctness/cpp/setops covers the new path. * Fix the tree declarations in three benchmarks apps/pbrt/trace.bonsai and both rtiow variants declared a leaf holding an array of primitives but never tagged it as the data the tree stores, so lowering built a match arm that neither yields nor recurses and tripped an assertion. Every other tree in the repository carries the tag, e.g. `Leaf(d : array[f32]) with data = d` in apps/queries/range. rtiow additionally declared `tree[[Sphere]]` while its set, its leaf and its layout all hold MaterialSphere. The primitive type is what the tree stores; the bounding volume stays a Sphere, which is what the `with Sphere(center, radius)` annotation is for. All three now compile: pbrt and rtiow/cpu to a closest-hit traversal guarded by intersects and distmin on the node volume, and rtiow/cuda through the CUDA backend. These were reached earlier only after this branch's parser fix let `group[count]` through; before that they failed at parse time, which is why the underlying problem had not surfaced. * Point the README at the PLDI 2026 branches The compiler on main is the general one; the branches carrying the paper's benchmarks and artifact are easy to miss from here. * Re-bless the error goldens whose assert line numbers moved These three goldens record the source line of the internal_assert that fires, and later edits to Expr.cpp and Parser.cpp shifted those asserts without the goldens being updated: Expr.cpp 340 -> 341 (BinOp of non-number or boolean types) Expr.cpp 276 -> 277 (implicit casting of types) Parser.cpp 451 -> 469 (duplicate default value for field) All three asserts sit on a single line, so the reported number does not depend on the compiler. * Clang-format bonsai_benchmark.h and u56.h Both were unformatted, which fails the repo-wide clang-format check. * Fix two C++ interop bugs that made correctness/cpp/vec print garbage The vec golden was blessed on uninitialized memory: it recorded `r: 203935396562725628553461760.000000` for a field the driver sets to 100, and it was missing the "-2 and 5" line that main_vec.cpp prints unconditionally, so it could not match a correct run on any platform. Two separate defects were behind it. printf is variadic, so the default argument promotions apply: a float arrives as a double, and that is what "%f" reads. print_helper pushed the narrow f32 straight through, leaving the upper half of the argument undefined. That is where the garbage came from, and it is why the value differed between the golden, CI and a local run. Emit an fpext to double for any float narrower than one. Separately, vector did not match the type the generated code uses. The backend represents it as LLVM's , which rounds the lane count up to a power of two and which the C ABI passes in a single SSE register (SSE + SSEUP); the runtime declared a packed T[N] instead, which is classified as one register per eightbyte. A three-lane f32 vector was therefore handed over in xmm0+xmm1 and read back out of xmm0 alone, losing the third lane, and a field following a vector inside an element sat at the wrong offset -- Sphere::r at 12 rather than 16. Back the storage with a native vector so the size, alignment and classification all agree. Vectors of bytes keep their exact N-byte layout: there they stand in for a bit-packed field whose offsets the generated layout arithmetic already depends on, and vector_size cannot express a non-power-of-two size anyway. The rand goldens gain the fpext instructions; vec now reports the values the driver actually stores. * Update exported-function golden for the printf double promotion * Rename a ctor parameter to satisfy readability-identifier-naming --- CMakeLists.txt | 13 + README.md | 2 + apps/pbrt/trace.bonsai | 2 +- apps/rtiow/cpu/main.bonsai | 4 +- apps/rtiow/cuda/rtiow.bonsai | 4 +- include/CodeGen/CodeGen_CUDA.h | 3 +- include/CodeGen/CodeGen_LLVM.h | 3 +- include/IR/Expr.h | 67 +- include/IR/IRFwdDecl.h | 3 +- include/IR/Mutator.h | 3 +- include/IR/Operators.h | 26 + include/IR/Printer.h | 7 +- include/IR/Stmt.h | 17 +- include/IR/Type.h | 29 +- include/IR/Visitor.h | 3 +- include/IR/WriteLoc.h | 2 + include/Parser/Token.h | 2 + include/Utils.h | 30 +- runtime/bonsai_benchmark.h | 15 +- runtime/bonsai_cpp.h | 10 + runtime/bonsai_set.h | 13 +- runtime/bonsai_vector.h | 73 +- runtime/u56.h | 17 +- src/CodeGen/CPP.cpp | 78 +- src/CodeGen/CodeGen_CUDA.cpp | 29 +- src/CodeGen/CodeGen_LLVM.cpp | 56 +- src/IR/Analysis.cpp | 10 +- src/IR/Equality.cpp | 79 +- src/IR/Expr.cpp | 256 ++++-- src/IR/Mutator.cpp | 32 +- src/IR/Operators.cpp | 116 +++ src/IR/Printer.cpp | 106 ++- src/IR/Stmt.cpp | 9 +- src/IR/Type.cpp | 18 + src/IR/Visitor.cpp | 12 +- src/IR/WriteLoc.cpp | 23 + src/Lower/Defers.cpp | 1 + src/Lower/Lambdas.cpp | 5 + src/Lower/Layouts.cpp | 1 + src/Lower/LoopTransforms.cpp | 1 + src/Lower/PredicateAnalysis.cpp | 468 ++++++++-- src/Lower/Scans.cpp | 161 +++- src/Lower/Trees.cpp | 867 ++++++++++++++++-- src/Opt/DCE.cpp | 1 + src/Parser/Parser.cpp | 121 ++- src/Utils.cpp | 39 +- stdlib/distance.bonsai | 57 ++ stdlib/topology.bonsai | 343 +++++++ tests/README.md | 2 +- .../backends/llvm/exported-function.expect | 2 +- tests/bonsai/backends/llvm/rand-par.expect | 54 +- tests/bonsai/backends/llvm/rand.expect | 118 ++- tests/bonsai/correctness/cpp/CMakeLists.txt | 1 + tests/bonsai/correctness/cpp/main_setops.cpp | 131 +++ tests/bonsai/correctness/cpp/setops.bonsai | 39 + tests/bonsai/correctness/cpp/setops.expect | 5 + tests/bonsai/correctness/cpp/vec.expect | 5 +- tests/bonsai/error/generic_fib.expect | 2 +- tests/bonsai/error/mismatched_types.expect | 2 +- tests/bonsai/error/string-literal.expect | 7 - .../error/struct_duplicate_default.expect | 2 +- tests/bonsai/lower/CMakeLists.txt | 3 + tests/bonsai/lower/bvh.expect | 8 +- tests/bonsai/lower/geomops/CMakeLists.txt | 16 + tests/bonsai/lower/geomops/contains.bonsai | 26 + tests/bonsai/lower/geomops/contains.expect | 60 ++ tests/bonsai/lower/geomops/covers.bonsai | 26 + tests/bonsai/lower/geomops/covers.expect | 60 ++ tests/bonsai/lower/geomops/disjoint.bonsai | 26 + tests/bonsai/lower/geomops/disjoint.expect | 64 ++ tests/bonsai/lower/geomops/dual-tree.bonsai | 26 + tests/bonsai/lower/geomops/dual-tree.expect | 52 ++ tests/bonsai/lower/geomops/equals.bonsai | 26 + tests/bonsai/lower/geomops/equals.expect | 56 ++ tests/bonsai/lower/geomops/intersects.bonsai | 26 + tests/bonsai/lower/geomops/intersects.expect | 64 ++ tests/bonsai/lower/geomops/metrics.bonsai | 25 + tests/bonsai/lower/geomops/metrics.expect | 61 ++ tests/bonsai/lower/geomops/ordering-le.bonsai | 26 + tests/bonsai/lower/geomops/ordering-le.expect | 89 ++ tests/bonsai/lower/geomops/ordering-lt.bonsai | 26 + tests/bonsai/lower/geomops/ordering-lt.expect | 89 ++ .../lower/geomops/stdlib-resolution.bonsai | 50 + .../lower/geomops/stdlib-resolution.expect | 864 +++++++++++++++++ tests/bonsai/lower/geomops/touches.bonsai | 26 + tests/bonsai/lower/geomops/touches.expect | 56 ++ tests/bonsai/lower/geomops/within.bonsai | 26 + tests/bonsai/lower/geomops/within.expect | 60 ++ tests/bonsai/lower/setops/CMakeLists.txt | 20 + tests/bonsai/lower/setops/all.bonsai | 17 + tests/bonsai/lower/setops/all.expect | 34 + tests/bonsai/lower/setops/any.bonsai | 17 + tests/bonsai/lower/setops/any.expect | 32 + tests/bonsai/lower/setops/argmax.bonsai | 17 + tests/bonsai/lower/setops/argmax.expect | 34 + tests/bonsai/lower/setops/argmin.bonsai | 17 + tests/bonsai/lower/setops/argmin.expect | 34 + .../lower/setops/count-augmented.bonsai | 17 + .../lower/setops/count-augmented.expect | 38 + tests/bonsai/lower/setops/count.bonsai | 17 + tests/bonsai/lower/setops/count.expect | 38 + tests/bonsai/lower/setops/filter.bonsai | 17 + tests/bonsai/lower/setops/filter.expect | 36 + tests/bonsai/lower/setops/map.bonsai | 17 + tests/bonsai/lower/setops/map.expect | 36 + .../lower/setops/maximum-augmented.bonsai | 17 + .../lower/setops/maximum-augmented.expect | 28 + tests/bonsai/lower/setops/maximum.bonsai | 17 + tests/bonsai/lower/setops/maximum.expect | 34 + .../lower/setops/minimum-augmented.bonsai | 17 + .../lower/setops/minimum-augmented.expect | 28 + .../lower/setops/minimum-unfiltered.bonsai | 17 + .../lower/setops/minimum-unfiltered.expect | 31 + tests/bonsai/lower/setops/minimum.bonsai | 17 + tests/bonsai/lower/setops/minimum.expect | 34 + tests/bonsai/lower/setops/product.bonsai | 19 + tests/bonsai/lower/setops/product.expect | 57 ++ tests/bonsai/lower/setops/reduce.bonsai | 17 + tests/bonsai/lower/setops/reduce.expect | 28 + tests/bonsai/lower/setops/sum.bonsai | 17 + tests/bonsai/lower/setops/sum.expect | 28 + tests/bonsai/lower/trace.expect | 46 + tests/bonsai/lower/trace_aosoa.expect | 46 + 123 files changed, 6065 insertions(+), 415 deletions(-) create mode 100644 stdlib/topology.bonsai create mode 100644 tests/bonsai/correctness/cpp/main_setops.cpp create mode 100644 tests/bonsai/correctness/cpp/setops.bonsai create mode 100644 tests/bonsai/correctness/cpp/setops.expect delete mode 100644 tests/bonsai/error/string-literal.expect create mode 100644 tests/bonsai/lower/geomops/CMakeLists.txt create mode 100644 tests/bonsai/lower/geomops/contains.bonsai create mode 100644 tests/bonsai/lower/geomops/contains.expect create mode 100644 tests/bonsai/lower/geomops/covers.bonsai create mode 100644 tests/bonsai/lower/geomops/covers.expect create mode 100644 tests/bonsai/lower/geomops/disjoint.bonsai create mode 100644 tests/bonsai/lower/geomops/disjoint.expect create mode 100644 tests/bonsai/lower/geomops/dual-tree.bonsai create mode 100644 tests/bonsai/lower/geomops/dual-tree.expect create mode 100644 tests/bonsai/lower/geomops/equals.bonsai create mode 100644 tests/bonsai/lower/geomops/equals.expect create mode 100644 tests/bonsai/lower/geomops/intersects.bonsai create mode 100644 tests/bonsai/lower/geomops/intersects.expect create mode 100644 tests/bonsai/lower/geomops/metrics.bonsai create mode 100644 tests/bonsai/lower/geomops/metrics.expect create mode 100644 tests/bonsai/lower/geomops/ordering-le.bonsai create mode 100644 tests/bonsai/lower/geomops/ordering-le.expect create mode 100644 tests/bonsai/lower/geomops/ordering-lt.bonsai create mode 100644 tests/bonsai/lower/geomops/ordering-lt.expect create mode 100644 tests/bonsai/lower/geomops/stdlib-resolution.bonsai create mode 100644 tests/bonsai/lower/geomops/stdlib-resolution.expect create mode 100644 tests/bonsai/lower/geomops/touches.bonsai create mode 100644 tests/bonsai/lower/geomops/touches.expect create mode 100644 tests/bonsai/lower/geomops/within.bonsai create mode 100644 tests/bonsai/lower/geomops/within.expect create mode 100644 tests/bonsai/lower/setops/CMakeLists.txt create mode 100644 tests/bonsai/lower/setops/all.bonsai create mode 100644 tests/bonsai/lower/setops/all.expect create mode 100644 tests/bonsai/lower/setops/any.bonsai create mode 100644 tests/bonsai/lower/setops/any.expect create mode 100644 tests/bonsai/lower/setops/argmax.bonsai create mode 100644 tests/bonsai/lower/setops/argmax.expect create mode 100644 tests/bonsai/lower/setops/argmin.bonsai create mode 100644 tests/bonsai/lower/setops/argmin.expect create mode 100644 tests/bonsai/lower/setops/count-augmented.bonsai create mode 100644 tests/bonsai/lower/setops/count-augmented.expect create mode 100644 tests/bonsai/lower/setops/count.bonsai create mode 100644 tests/bonsai/lower/setops/count.expect create mode 100644 tests/bonsai/lower/setops/filter.bonsai create mode 100644 tests/bonsai/lower/setops/filter.expect create mode 100644 tests/bonsai/lower/setops/map.bonsai create mode 100644 tests/bonsai/lower/setops/map.expect create mode 100644 tests/bonsai/lower/setops/maximum-augmented.bonsai create mode 100644 tests/bonsai/lower/setops/maximum-augmented.expect create mode 100644 tests/bonsai/lower/setops/maximum.bonsai create mode 100644 tests/bonsai/lower/setops/maximum.expect create mode 100644 tests/bonsai/lower/setops/minimum-augmented.bonsai create mode 100644 tests/bonsai/lower/setops/minimum-augmented.expect create mode 100644 tests/bonsai/lower/setops/minimum-unfiltered.bonsai create mode 100644 tests/bonsai/lower/setops/minimum-unfiltered.expect create mode 100644 tests/bonsai/lower/setops/minimum.bonsai create mode 100644 tests/bonsai/lower/setops/minimum.expect create mode 100644 tests/bonsai/lower/setops/product.bonsai create mode 100644 tests/bonsai/lower/setops/product.expect create mode 100644 tests/bonsai/lower/setops/reduce.bonsai create mode 100644 tests/bonsai/lower/setops/reduce.expect create mode 100644 tests/bonsai/lower/setops/sum.bonsai create mode 100644 tests/bonsai/lower/setops/sum.expect diff --git a/CMakeLists.txt b/CMakeLists.txt index a054f74b1..b6f5f2767 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,19 @@ find_package( target passes native + # The components below are used directly by src/CodeGen (TargetRegistry, + # Triple, getDefaultTargetTriple, the AlwaysInliner, and the individual + # passes named in optimize_module). They resolve transitively through the + # monolithic LLVM dylib on macOS, but must be requested explicitly when + # LLVM is built as separate component libraries (e.g. a Linux + # BUILD_SHARED_LIBS install). + mc + targetparser + codegen + transformutils + instcombine + scalaropts + ipo # irbuilder orcjit executionengine # Required for LLJIT diff --git a/README.md b/README.md index 064d6dafd..077d7c2de 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # bonsai +**Click [here for the PLDI 2026 Bonsai branch](https://github.com/rootjalex/bonsai/tree/ajr/pldi-benchmarks) and [here for the PLDI 2026 Scion branch](https://github.com/rootjalex/bonsai/tree/cpg/pldi-artifact).** + DSL for Recursive Geometric Queries ### Setup. diff --git a/apps/pbrt/trace.bonsai b/apps/pbrt/trace.bonsai index 3f37212f5..9478b0f89 100644 --- a/apps/pbrt/trace.bonsai +++ b/apps/pbrt/trace.bonsai @@ -12,7 +12,7 @@ func [[export]] trace_all(n : u32, rays : array[Ray, n]) = map(|r : Ray| trace(r schedule { tree[[Triangle]] TriangleBVH(low high : vec3f) with AABB(low, high) = | Interior(left : TriangleBVH, right : TriangleBVH) - | Leaf(data : array[Triangle]); + | Leaf(data : array[Triangle]) with data = data; triangles : TriangleBVH; diff --git a/apps/rtiow/cpu/main.bonsai b/apps/rtiow/cpu/main.bonsai index 14af5c7ac..a2301970c 100644 --- a/apps/rtiow/cpu/main.bonsai +++ b/apps/rtiow/cpu/main.bonsai @@ -182,9 +182,9 @@ func [[export]] image(c : Camera) { schedule { // TODO: generic over primitive types! - tree[[Sphere]] SphereBVH(center : vec3f, radius : Float) with Sphere(center, radius) = + tree[[MaterialSphere]] SphereBVH(center : vec3f, radius : Float) with Sphere(center, radius) = | Interior(left : SphereBVH, right : SphereBVH, axis : u8) - | Leaf(data : array[MaterialSphere]); + | Leaf(data : array[MaterialSphere]) with data = data; spheres : SphereBVH; diff --git a/apps/rtiow/cuda/rtiow.bonsai b/apps/rtiow/cuda/rtiow.bonsai index e0e0d7f08..eeee23ea3 100644 --- a/apps/rtiow/cuda/rtiow.bonsai +++ b/apps/rtiow/cuda/rtiow.bonsai @@ -177,9 +177,9 @@ func [[export]] image(c : Camera) { } schedule { - tree[[Sphere]] SphereBVH(center : vec3f, radius : Float) with Sphere(center, radius) = + tree[[MaterialSphere]] SphereBVH(center : vec3f, radius : Float) with Sphere(center, radius) = | Interior(left : SphereBVH, right : SphereBVH) - | Leaf(data : array[MaterialSphere]); + | Leaf(data : array[MaterialSphere]) with data = data; spheres : SphereBVH; diff --git a/include/CodeGen/CodeGen_CUDA.h b/include/CodeGen/CodeGen_CUDA.h index c4f9c9826..fd2bab747 100644 --- a/include/CodeGen/CodeGen_CUDA.h +++ b/include/CodeGen/CodeGen_CUDA.h @@ -56,7 +56,7 @@ class CodeGen_CUDA : public ir::Printer { void visit(const ir::FloatImm *) override; void visit(const ir::VecImm *) override; void visit(const ir::StringImm *) override; - void visit(const ir::Infinity *) override; + void visit(const ir::Extrema *) override; void visit(const ir::Deref *) override; RESTRICT_VISITOR(ir::AtomicAdd); // TODO void visit(const ir::Select *) override; @@ -74,6 +74,7 @@ class CodeGen_CUDA : public ir::Printer { RESTRICT_VISITOR(ir::Lambda); RESTRICT_VISITOR(ir::GeomOp); RESTRICT_VISITOR(ir::SetOp); + RESTRICT_VISITOR(ir::AggOp); RESTRICT_VISITOR(ir::Instantiate); // Statements void visit(const ir::CallStmt *) override; diff --git a/include/CodeGen/CodeGen_LLVM.h b/include/CodeGen/CodeGen_LLVM.h index 22a211db4..f1adca892 100644 --- a/include/CodeGen/CodeGen_LLVM.h +++ b/include/CodeGen/CodeGen_LLVM.h @@ -124,7 +124,7 @@ struct CodeGen_LLVM : public ir::Visitor { virtual void visit(const ir::BoolImm *) override; virtual void visit(const ir::VecImm *) override; virtual void visit(const ir::StringImm *) override; - virtual void visit(const ir::Infinity *) override; + virtual void visit(const ir::Extrema *) override; virtual void visit(const ir::Var *) override; virtual void visit(const ir::BinOp *) override; virtual void visit(const ir::UnOp *) override; @@ -143,6 +143,7 @@ struct CodeGen_LLVM : public ir::Visitor { virtual void visit(const ir::Lambda *) override; virtual void visit(const ir::GeomOp *) override; virtual void visit(const ir::SetOp *) override; + virtual void visit(const ir::AggOp *) override; virtual void visit(const ir::Call *) override; virtual void visit(const ir::Instantiate *) override; virtual void visit(const ir::PtrTo *) override; diff --git a/include/IR/Expr.h b/include/IR/Expr.h index 82ba3894a..5ac39d8b6 100644 --- a/include/IR/Expr.h +++ b/include/IR/Expr.h @@ -24,7 +24,7 @@ enum class IRExprEnum { VecImm, StringImm, Var, - Infinity, + Extrema, BinOp, UnOp, Select, @@ -45,6 +45,7 @@ enum class IRExprEnum { Lambda, GeomOp, SetOp, + AggOp, Call, Instantiate, // Pointer operations @@ -162,10 +163,15 @@ struct Var : ExprNode { }; // Maximum value of a type (inf for float) -struct Infinity : ExprNode { - static Expr make(Type tan); +struct Extrema : ExprNode { + enum OpType { + eps, + inf, + }; + OpType op; + static Expr make(Type t, OpType op); - static const IRExprEnum node_type = IRExprEnum::Infinity; + static const IRExprEnum node_type = IRExprEnum::Extrema; }; struct BinOp : ExprNode { @@ -380,13 +386,30 @@ struct Lambda : ExprNode { static const IRExprEnum node_type = IRExprEnum::Lambda; }; +// The geometric operators of Figure 1: the topological predicates of Egenhofer +// and Herring, the per-dimension ordering predicates, and the metrics. +// `lex`/`ltx` and friends are `a <=_x b` and `a <_x b`, one opcode per +// (relation, axis) pair. struct GeomOp : ExprNode { enum OpType { + // Topological predicates. contains, + covers, + disjoint, + equals, + intersects, + touches, + within, + // Ordering predicates, per dimension. + lex, + ley, + lez, + ltx, + lty, + ltz, + // Metrics. distmax, distmin, - intersects, - // TODO: the rest opcount, // sentinel, do not remove! }; @@ -401,15 +424,21 @@ struct GeomOp : ExprNode { static const IRExprEnum node_type = IRExprEnum::GeomOp; }; -// For Argmin/Map/Filter, a: Lambda, b: Set -// For Product, a and b are Sets +// The set operators of Figure 2. For everything but `product`, a is a lambda +// over the set's elements and b is the set; for `product`, a and b are sets. +// The set-level `min` and `max` are spelled `minimum` and `maximum` because +// `min` and `max` already name the binary scalar intrinsics. struct SetOp : ExprNode { enum OpType { + all, + any, + argmax, argmin, filter, map, + maximum, + minimum, product, - // TODO: reduce // TODO: geometric intrinsics for lambda }; @@ -422,6 +451,26 @@ struct SetOp : ExprNode { static const IRExprEnum node_type = IRExprEnum::SetOp; }; +// A reduction over a set. `reduce` is the primitive of Figure 2; the others +// are sugar that expand into a map followed by a reduce (e.g. `count` maps +// every element to 1 and sums with identity 0). +struct AggOp : ExprNode { + enum OpType { avg, count, prod, reduce, sum }; + + OpType op; + + Expr a; // must be a set type + + // Only defined for `reduce`: the identity element, and the associative, + // commutative binary function combining two partial results. + Expr identity, combiner; + + static Expr make(OpType op, Expr a); + static Expr make(Expr identity, Expr combiner, Expr a); + + static const IRExprEnum node_type = IRExprEnum::AggOp; +}; + struct Call : ExprNode { Expr func; std::vector args; diff --git a/include/IR/IRFwdDecl.h b/include/IR/IRFwdDecl.h index 98b01c354..eda9fa3ea 100644 --- a/include/IR/IRFwdDecl.h +++ b/include/IR/IRFwdDecl.h @@ -40,7 +40,7 @@ struct FloatImm; struct BoolImm; struct VecImm; struct StringImm; -struct Infinity; +struct Extrema; struct Var; struct BinOp; struct UnOp; @@ -59,6 +59,7 @@ struct Generator; struct Lambda; struct GeomOp; struct SetOp; +struct AggOp; struct Call; struct Instantiate; struct PtrTo; diff --git a/include/IR/Mutator.h b/include/IR/Mutator.h index 5447a15d2..06e5f90e7 100644 --- a/include/IR/Mutator.h +++ b/include/IR/Mutator.h @@ -47,7 +47,7 @@ struct Mutator { virtual Expr visit(const BoolImm *); virtual Expr visit(const VecImm *); virtual Expr visit(const StringImm *); - virtual Expr visit(const Infinity *); + virtual Expr visit(const Extrema *); virtual Expr visit(const Var *); virtual Expr visit(const BinOp *); virtual Expr visit(const UnOp *); @@ -66,6 +66,7 @@ struct Mutator { virtual Expr visit(const Lambda *); virtual Expr visit(const GeomOp *); virtual Expr visit(const SetOp *); + virtual Expr visit(const AggOp *); virtual Expr visit(const Call *); virtual Expr visit(const Instantiate *); virtual Expr visit(const PtrTo *); diff --git a/include/IR/Operators.h b/include/IR/Operators.h index b7f3bd35e..85b88bba5 100644 --- a/include/IR/Operators.h +++ b/include/IR/Operators.h @@ -36,13 +36,39 @@ Expr distmax(Expr a, Expr b); Expr distmin(Expr a, Expr b); Expr intersects(Expr a, Expr b); Expr contains(Expr a, Expr b); +Expr covers(Expr a, Expr b); +Expr disjoint(Expr a, Expr b); +Expr equals_geom(Expr a, Expr b); +Expr touches(Expr a, Expr b); +Expr within(Expr a, Expr b); +Expr ordering(GeomOp::OpType op, Expr a, Expr b); // Sets Expr filter(Expr predicate, Expr set); Expr argmin(Expr metric, Expr set); +Expr argmax(Expr metric, Expr set); +Expr minimum(Expr metric, Expr set); +Expr maximum(Expr metric, Expr set); +Expr any(Expr predicate, Expr set); +Expr all(Expr predicate, Expr set); Expr map(Expr func, Expr set); Expr product(Expr a, Expr b); +// Aggregates +Expr avg(Expr a); +Expr count(Expr a); +Expr prod(Expr a); +Expr sum(Expr a); +Expr reduce(Expr identity, Expr combiner, Expr a); + +// `|a, b| a b`, the combiner of a reduce built from a binary operator. +Expr binary_lambda(BinOp::OpType op, Type t); + +// Rewrite an aggregation into the map-then-reduce form of Figure 2. `count`, +// for instance, maps every element to 1 and sums with identity 0. Returns the +// expression unchanged when it is already a `reduce`. +Expr expand_aggregate(const AggOp *agg); + Expr abs(Expr a); Expr max(Expr a, Expr b); Expr min(Expr a, Expr b); diff --git a/include/IR/Printer.h b/include/IR/Printer.h index 7c6399285..6c3325545 100644 --- a/include/IR/Printer.h +++ b/include/IR/Printer.h @@ -49,6 +49,8 @@ std::string to_string(const Intrinsic::OpType &op); std::string to_string(const Generator::OpType &op); std::string to_string(const GeomOp::OpType &op); std::string to_string(const SetOp::OpType &op); +std::string to_string(const AggOp::OpType &op); +std::string to_string(const Annotation::Aggregate::OpType &op); std::string from_string_imm(const std::string &value); void print_string_imm(std::ostream &os, const std::string &value); @@ -72,7 +74,7 @@ struct Printer : public Visitor { void print(const Schedule &schedule); void print(const Location &loc); void print(const Type &type); - void print_type_list(const std::vector &types); + virtual void print_type_list(const std::vector &types); void print(const Interface &interface); void print(const Expr &expr); void print_no_parens(const Expr &expr); @@ -114,7 +116,7 @@ struct Printer : public Visitor { void visit(const BoolImm *) override; void visit(const VecImm *) override; void visit(const StringImm *) override; - void visit(const Infinity *) override; + void visit(const Extrema *) override; void visit(const Var *) override; void print(const BinOp::OpType &op); void visit(const BinOp *) override; @@ -136,6 +138,7 @@ struct Printer : public Visitor { void visit(const Lambda *) override; void visit(const GeomOp *) override; void visit(const SetOp *) override; + void visit(const AggOp *) override; void visit(const Call *) override; void visit(const Instantiate *) override; void visit(const PtrTo *) override; diff --git a/include/IR/Stmt.h b/include/IR/Stmt.h index 6502f6ea0..8a4baeb0a 100644 --- a/include/IR/Stmt.h +++ b/include/IR/Stmt.h @@ -191,6 +191,8 @@ struct Accumulate : StmtNode { // value)) Argmax, // TODO: add more. + Min, + Max, }; WriteLoc loc; OpType op; @@ -252,11 +254,22 @@ struct Iterate : StmtNode { static const IRStmtEnum node_type = IRStmtEnum::Iterate; }; -// Recursively traverse this tree for all datums +// Recursively traverse this tree for all datums. +// `op`, when set, is the reduction combining subtree results instead of the +// default set union. `func`, when defined, is a map applied to subtree +// elements before that combination. struct Scan : StmtNode { + std::optional op; + // The accumulator the reduction folds into. Defined exactly when `op` is. + WriteLoc loc; + Expr func; Expr value; - static Stmt make(Expr value); + static Stmt make(std::optional op, WriteLoc loc, Expr func, + Expr value); + static Stmt make(Expr func, Expr value) { + return make({}, WriteLoc(), std::move(func), std::move(value)); + } static const IRStmtEnum node_type = IRStmtEnum::Scan; }; diff --git a/include/IR/Type.h b/include/IR/Type.h index 356f3bf71..0f9c67077 100644 --- a/include/IR/Type.h +++ b/include/IR/Type.h @@ -290,7 +290,6 @@ struct Generic_t : TypeNode { static const IRTypeEnum node_type = IRTypeEnum::Generic_t; }; -// TODO: aggregation struct Annotation { // data = name @@ -314,7 +313,27 @@ struct Annotation { std::string low, high; }; - std::variant type; + // count() = dCount + // min(field) = field_min + // etc. + struct Aggregate { + // TODO: deduplicate with AggOp::OpType... + enum OpType { + avg, + count, + max, + min, + prod, + sum, + }; + OpType op; + std::vector args; // field, empty for count + std::string value; // field name that stores + + static OpType str_to_op(const std::string &str); + }; + + std::variant type; template const T *as() const { @@ -339,7 +358,8 @@ struct BVH_t : TypeNode { bool has_volume() const { for (const auto &annot : annotations) { - if (annot.as()) { + if (annot.as() && + annot.as()->geometry.empty()) { return true; } } @@ -348,7 +368,8 @@ struct BVH_t : TypeNode { const Annotation::Volume *get_volume() const { for (const auto &annot : annotations) { - if (const auto *vol = annot.as()) { + if (const auto *vol = annot.as(); + vol && vol->geometry.empty()) { return vol; } } diff --git a/include/IR/Visitor.h b/include/IR/Visitor.h index cdc3c58b4..889899f93 100644 --- a/include/IR/Visitor.h +++ b/include/IR/Visitor.h @@ -38,7 +38,7 @@ struct Visitor { virtual void visit(const BoolImm *); virtual void visit(const VecImm *); virtual void visit(const StringImm *); - virtual void visit(const Infinity *); + virtual void visit(const Extrema *); virtual void visit(const Var *); virtual void visit(const BinOp *); virtual void visit(const UnOp *); @@ -57,6 +57,7 @@ struct Visitor { virtual void visit(const Lambda *); virtual void visit(const GeomOp *); virtual void visit(const SetOp *); + virtual void visit(const AggOp *); virtual void visit(const Call *); virtual void visit(const Instantiate *); virtual void visit(const PtrTo *); diff --git a/include/IR/WriteLoc.h b/include/IR/WriteLoc.h index aff2857ab..feb853f69 100644 --- a/include/IR/WriteLoc.h +++ b/include/IR/WriteLoc.h @@ -25,6 +25,8 @@ struct WriteLoc { bool defined() const { return !base.empty(); } + ir::Expr to_expr() const; + // These append to `accesses` *AND* mutate type (if set). void add_struct_access(const std::string &field); void add_index_access(const Expr &index); diff --git a/include/Parser/Token.h b/include/Parser/Token.h index e9f12b7a7..70b1815e8 100644 --- a/include/Parser/Token.h +++ b/include/Parser/Token.h @@ -2,6 +2,8 @@ #include "Error.h" +#include +#include #include #include #include diff --git a/include/Utils.h b/include/Utils.h index f931652d2..eff5c2c1d 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -1,5 +1,6 @@ #pragma once +#include "IR/Equality.h" #include "IR/Expr.h" #include "IR/Printer.h" #include "IR/Type.h" @@ -62,7 +63,7 @@ std::optional get_constant_value(const ir::Expr &e, return std::bit_cast(value); } - if (e.is()) { + if (e.is()) { // Conservatively fail until we find use cases for this. return {}; } @@ -103,6 +104,12 @@ ir::Expr make_const(const ir::Type &t, const T &v) { } else if (t.is()) { ir::Expr r = make_const(t.as()->etype, v); return ir::Broadcast::make(t.as()->lanes, std::move(r)); + } else if (const auto *struct_t = t.as()) { + std::vector values; + for (int i = 0, e = struct_t->fields.size(); i < e; ++i) { + values.push_back(make_const(struct_t->fields[i].type, v)); + } + return ir::Build::make(t, values); } else { internal_error << "make_const does not know how to build constant of type: " << t @@ -123,6 +130,9 @@ ir::Expr replace(const std::string &var_name, ir::Expr repl, ir::Expr replace(const std::map &repls, const ir::Expr &orig); +ir::Expr replace(const std::map &repls, + const ir::Expr &orig); + ir::Stmt replace(const std::map &repls, const ir::Stmt &orig); @@ -162,17 +172,23 @@ ir::WriteLoc read_to_writeloc(const ir::Expr &expr); // Whether we can convert an expression into a WriteLoc. bool is_writeloc(const ir::Expr &expr); -inline bool is_geometric_intrinsic(const std::string &name) { - return (name == "contains") || (name == "distmin") || (name == "distmax") || - (name == "intersects"); +inline bool is_geometric_metric(const std::string &name) { + return (name == "distmin") || (name == "distmax"); } +// The topological and ordering predicates of Figure 1. inline bool is_geometric_predicate(const std::string &name) { - return (name == "contains") || (name == "intersects"); + for (uint32_t i = 0; i < ir::GeomOp::opcount; i++) { + const auto op = static_cast(i); + if (name == ir::GeomOp::intrinsic_name(op)) { + return !is_geometric_metric(name); + } + } + return false; } -inline bool is_geometric_metric(const std::string &name) { - return (name == "distmin") || (name == "distmax"); +inline bool is_geometric_intrinsic(const std::string &name) { + return is_geometric_predicate(name) || is_geometric_metric(name); } // Returns a bit mask of size n. diff --git a/runtime/bonsai_benchmark.h b/runtime/bonsai_benchmark.h index 50f4f92b7..816b9cce4 100644 --- a/runtime/bonsai_benchmark.h +++ b/runtime/bonsai_benchmark.h @@ -3,15 +3,16 @@ #include #include #include +#include #include #include -#include -template +template // k is the number of runs, m is the number of low and high runs to drop. -int64_t benchmark_function(Func&& func, int k, int m) { +int64_t benchmark_function(Func &&func, int k, int m) { if (2 * m >= k) { - throw std::invalid_argument("Cannot drop more times than available runs (2 * m >= k)"); + throw std::invalid_argument( + "Cannot drop more times than available runs (2 * m >= k)"); } std::vector times; @@ -22,7 +23,9 @@ int64_t benchmark_function(Func&& func, int k, int m) { func(); // Run benchmarked function auto end = std::chrono::high_resolution_clock::now(); - times.push_back(std::chrono::duration_cast(end - start).count()); + times.push_back( + std::chrono::duration_cast(end - start) + .count()); } std::sort(times.begin(), times.end()); @@ -30,5 +33,5 @@ int64_t benchmark_function(Func&& func, int k, int m) { auto end = times.end() - m; int64_t sum = std::accumulate(begin, end, int64_t{0}); - return sum / std::distance(begin, end); // average of middle runs + return sum / std::distance(begin, end); // average of middle runs } diff --git a/runtime/bonsai_cpp.h b/runtime/bonsai_cpp.h index 439965812..d3d43f2f4 100644 --- a/runtime/bonsai_cpp.h +++ b/runtime/bonsai_cpp.h @@ -7,6 +7,16 @@ #include "u24.h" #include "u56.h" +// For the std:: names pulled into the global namespace below, and the +// bit_cast/memcpy used by reinterpret(). libc++ provides these transitively; +// libstdc++ does not. +#include +#include +#include +#include +#include +#include + template __attribute__((always_inline)) T reinterpret(const U &bits) { static_assert(sizeof(T) == sizeof(U), "Size mismatch in reinterpret"); diff --git a/runtime/bonsai_set.h b/runtime/bonsai_set.h index 109b8cc4a..dec3a2ded 100644 --- a/runtime/bonsai_set.h +++ b/runtime/bonsai_set.h @@ -170,4 +170,15 @@ bool operator==(const set &a, const set &b) { a.for_each([&](const T &x) { std_a.insert(x); }); b.for_each([&](const T &x) { std_b.insert(x); }); return std_a == std_b; -} \ No newline at end of file +} +// How many elements a set or a leaf's range holds. A reduction that sums the +// same constant over every element asks for this instead of iterating. +template +inline uint64_t count(const set &input) { + return input.size(); +} + +template +inline uint64_t count(const range &input) { + return input.count; +} diff --git a/runtime/bonsai_vector.h b/runtime/bonsai_vector.h index c6bdbbbfe..081cf4d60 100644 --- a/runtime/bonsai_vector.h +++ b/runtime/bonsai_vector.h @@ -2,54 +2,101 @@ #include #include #include +#include #include #include +namespace bonsai_detail { + +constexpr size_t round_up_pow2(size_t n) { + size_t p = 1; + while (p < n) { + p *= 2; + } + return p; +} + +// Two different roles share vector, and they want different layouts. +// +// A vector of a multi-byte element is a value. The LLVM backend represents it +// as , which rounds the lane count up to a power of two, and which the C +// ABI hands over in a single SSE register (SSE + SSEUP) rather than one +// register per eightbyte. Reproducing that is what makes such a vector survive +// a call boundary, and what puts a field following it inside an element at the +// offset the generated code reads it from. Only a bare native vector member +// gets that classification: pairing it with a T[lanes] in a union demotes the +// second eightbyte to SSE and splits the value across two registers. +template +struct vector_storage { + static constexpr size_t lanes = round_up_pow2(N); + typedef T native_vector __attribute__((vector_size(sizeof(T) * lanes))); + native_vector lanes_; +}; + +// A vector of bytes is instead an opaque blob standing in for a bit-packed +// field, and the generated layout arithmetic assumes it occupies exactly N +// bytes. Padding those would move every field after them. (A native vector +// could not express it anyway: vector_size must be a power of two.) +template +struct vector_storage { + static constexpr size_t lanes = N; + T lanes_[N]; +}; + +} // namespace bonsai_detail + template struct vector { static_assert(N > 0, "vector: N must be > 0"); static_assert(std::is_trivially_copyable_v, "T must be trivially copyable"); - T data[N] = {}; // Zero-initialize + using storage = bonsai_detail::vector_storage; + static constexpr size_t lanes = storage::lanes; + + storage data; + + T *lane_data() { return reinterpret_cast(&data); } + const T *lane_data() const { return reinterpret_cast(&data); } // Indexing (with bounds checking in debug builds) T &operator[](size_t i) { if (i >= N) throw std::out_of_range("vector[] index out of range"); - return data[i]; + return lane_data()[i]; } const T &operator[](size_t i) const { if (i >= N) throw std::out_of_range("vector[] index out of range"); - return data[i]; + return lane_data()[i]; } - // Comparison operators + // Comparison operators. Only the N addressable lanes participate; any + // padding lane is not part of the value. bool operator==(const vector &other) const { - return std::memcmp(data, other.data, sizeof(data)) == 0; + return std::memcmp(lane_data(), other.lane_data(), sizeof(T) * N) == 0; } bool operator!=(const vector &other) const { return !(*this == other); } + // Default constructor = zero + vector() : data{} {} + // Allow initialization from a single scalar value - explicit vector(const T &value) { + explicit vector(const T &value) : data{} { for (size_t i = 0; i < N; ++i) - data[i] = value; + lane_data()[i] = value; } - // Default constructor = zero - vector() = default; - // Support initializer list if needed - vector(std::initializer_list init) { + vector(std::initializer_list init) : data{} { size_t i = 0; for (T v : init) { if (i < N) - data[i++] = v; + lane_data()[i++] = v; else break; } } -} __attribute__((packed)); +}; diff --git a/runtime/u56.h b/runtime/u56.h index 937ebaf83..9a35187c4 100644 --- a/runtime/u56.h +++ b/runtime/u56.h @@ -22,17 +22,14 @@ struct uint56_t { // Convert to uint64_t operator uint64_t() const { - return (uint64_t(data[6]) << 48) | - (uint64_t(data[5]) << 40) | - (uint64_t(data[4]) << 32) | - (uint64_t(data[3]) << 24) | - (uint64_t(data[2]) << 16) | - (uint64_t(data[1]) << 8) | + return (uint64_t(data[6]) << 48) | (uint64_t(data[5]) << 40) | + (uint64_t(data[4]) << 32) | (uint64_t(data[3]) << 24) | + (uint64_t(data[2]) << 16) | (uint64_t(data[1]) << 8) | (uint64_t(data[0])); } // Assignment from uint64_t - uint56_t& operator=(uint64_t value) { + uint56_t &operator=(uint64_t value) { data[0] = value & 0xFF; data[1] = (value >> 8) & 0xFF; data[2] = (value >> 16) & 0xFF; @@ -44,11 +41,9 @@ struct uint56_t { } // Comparison operators - bool operator==(const uint56_t& other) const { + bool operator==(const uint56_t &other) const { return std::memcmp(data, other.data, 7) == 0; } - bool operator!=(const uint56_t& other) const { - return !(*this == other); - } + bool operator!=(const uint56_t &other) const { return !(*this == other); } } __attribute__((packed)); \ No newline at end of file diff --git a/src/CodeGen/CPP.cpp b/src/CodeGen/CPP.cpp index 4c54b559c..4eb001849 100644 --- a/src/CodeGen/CPP.cpp +++ b/src/CodeGen/CPP.cpp @@ -23,6 +23,7 @@ #include "llvm/Target/TargetOptions.h" #include "llvm/TargetParser/Host.h" +#include #include #include #include @@ -174,10 +175,10 @@ void emit_const_var(std::stringstream &ss, const Expr &expr) { ss << (node->value ? "true" : "false"); } - void visit(const Infinity *node) override { + void visit(const Extrema *node) override { ss << "std::numeric_limits<"; emit_type(ss, node->type); - ss << ">::infinity()"; + ss << (node->op == Extrema::eps ? ">::epsilon()" : ">::infinity()"); } void visit(const VecImm *node) override { @@ -303,7 +304,10 @@ class BonsaiToCpp : ir::Printer { ss << "&"; } else { emit_type(ss, type); - if (!is_return_type && should_be_ref(type)) { + // A mutating argument is written through by the callee, so it has + // to be a reference no matter what it holds. This is how a + // reduction's accumulator reaches its recursive traversal. + if (!is_return_type && (is_mutating || should_be_ref(type))) { ss << "&"; } } @@ -477,12 +481,32 @@ class BonsaiToCpp : ir::Printer { // void visit(const BoolImm *) override; // void visit(const VecImm *) override; // void visit(const StringImm *) override; - // void visit(const Infinity *) override; + // void visit(const Extrema *) override; // void visit(const Var *) override; // void print(const BinOp::OpType &op); // void visit(const BinOp *) override; // void print(const UnOp::OpType &op); // void visit(const UnOp *) override; + void visit(const Extrema *node) override { + ss << "std::numeric_limits<"; + emit_type(ss, node->type); + ss << (node->op == Extrema::eps ? ">::epsilon()" : ">::infinity()"); + } + + void visit(const Build *node) override { + // Aggregate initialisation, e.g. `AABB{low, high}`. A vector builds + // from a braced list of its lanes. + emit_type(ss, node->type); + ss << "{"; + for (size_t i = 0, e = node->values.size(); i < e; i++) { + if (i != 0) { + ss << ", "; + } + print_no_parens(node->values[i]); + } + ss << "}"; + } + void visit(const Select *node) override { ss << "("; print(node->cond); @@ -567,18 +591,54 @@ class BonsaiToCpp : ir::Printer { // void visit(const DoWhile *) override; // void visit(const Sequence *) override; void visit(const Allocate *node) override { - internal_assert(node->loc.base_type.is()) - << "TODO: C++ Allocate lowering: " << Stmt(node); ss << get_indent(); emit_type(ss, node->loc.base_type); ss << " " << node->loc.base; - internal_assert(!node->value.defined()) - << "TODO: C++ Allocate lowering: " << Stmt(node); + // A set is default-constructed and appended to; a reduction's + // accumulator starts at its identity. + if (node->value.defined()) { + internal_assert(!node->loc.base_type.is()) + << "TODO: C++ Allocate lowering: " << Stmt(node); + ss << " = "; + print_no_parens(node->value); + } else { + internal_assert(node->loc.base_type.is()) + << "TODO: C++ Allocate lowering: " << Stmt(node); + } ss << ";\n"; } // void visit(const Free *) override; // void visit(const Store *) override; - // void visit(const Accumulate *) override; + void visit(const Accumulate *node) override { + ss << get_indent(); + print(node->loc); + switch (node->op) { + case Accumulate::Add: + ss << " += "; + break; + case Accumulate::Sub: + ss << " -= "; + break; + case Accumulate::Mul: + ss << " *= "; + break; + case Accumulate::Min: + case Accumulate::Max: { + // No compound assignment for these, so spell out the update. + ss << " = " << (node->op == Accumulate::Min ? "min(" : "max("); + print(node->loc); + ss << ", "; + print_no_parens(node->value); + ss << ");\n"; + return; + } + default: + internal_error << "TODO: C++ codegen for accumulate: " + << Stmt(node); + } + print_no_parens(node->value); + ss << ";\n"; + } // void visit(const Label *) override; // void visit(const RecLoop *) override; void visit(const Match *node) override { diff --git a/src/CodeGen/CodeGen_CUDA.cpp b/src/CodeGen/CodeGen_CUDA.cpp index 6c431b835..e5f0ef9ac 100644 --- a/src/CodeGen/CodeGen_CUDA.cpp +++ b/src/CodeGen/CodeGen_CUDA.cpp @@ -356,7 +356,16 @@ void CodeGen_CUDA::visit(const StringImm *node) { print_string_imm(os, node->value); } -void CodeGen_CUDA::visit(const Infinity *node) { os << "INFINITY"; } +void CodeGen_CUDA::visit(const Extrema *node) { + switch (node->op) { + case Extrema::inf: + os << "INFINITY"; + break; + case Extrema::eps: + os << (node->type.bits() == 64 ? "DBL_EPSILON" : "FLT_EPSILON"); + break; + } +} void CodeGen_CUDA::visit(const Cast *node) { ir::Expr value = node->value; @@ -943,8 +952,8 @@ void CodeGen_CUDA::visit(const Accumulate *node) { case Accumulate::OpType::Mul: os << '*'; break; - case Accumulate::OpType::Argmax: { - case Accumulate::OpType::Argmin: + case Accumulate::OpType::Argmax: + case Accumulate::OpType::Argmin: { // curr arg{min|max}= update; // -> // curr = arg{min|max}(curr, update); @@ -958,6 +967,20 @@ void CodeGen_CUDA::visit(const Accumulate *node) { os << ';' << '\n'; return; } + case Accumulate::OpType::Max: + case Accumulate::OpType::Min: { + // curr {min|max}= update; + // -> + // curr = {min|max}(curr, update); + os << '=' << ' '; + os << (node->op == Accumulate::OpType::Max ? "max" : "min") << '('; + Var::make(current.type, current.base).accept(this); + os << ',' << ' '; + update.accept(this); + os << ')'; + os << ';' << '\n'; + return; + } } os << '=' << ' '; update.accept(this); diff --git a/src/CodeGen/CodeGen_LLVM.cpp b/src/CodeGen/CodeGen_LLVM.cpp index e8fc50002..166e4766a 100644 --- a/src/CodeGen/CodeGen_LLVM.cpp +++ b/src/CodeGen/CodeGen_LLVM.cpp @@ -689,9 +689,17 @@ void CodeGen_LLVM::visit(const StringImm *node) { internal_error << "[unimplemented] StringImm in LLVM: " << Expr(node); } -void CodeGen_LLVM::visit(const Infinity *node) { +void CodeGen_LLVM::visit(const Extrema *node) { llvm::Type *inf_type = codegen_type(node->type); + if (node->op == Extrema::eps) { + internal_assert(node->type.is_float()) + << "Epsilon is only defined for floating point types: " + << node->type; + value = llvm::ConstantFP::get(inf_type, machine_epsilon(node->type)); + return; + } + if (inf_type->isFloatTy()) { value = llvm::ConstantFP::get( inf_type, llvm::APFloat::getInf(llvm::APFloat::IEEEsingle())); @@ -710,7 +718,7 @@ void CodeGen_LLVM::visit(const Infinity *node) { value = llvm::ConstantInt::get(inf_type, max_val); } else { - internal_error << "Infinity codegen not yet supported for type: " + internal_error << "Extrema codegen not yet supported for type: " << node->type; } } @@ -1068,6 +1076,13 @@ void CodeGen_LLVM::print_helper(const ir::Expr &node, llvm::Value *f = builder->CreateGlobalStringPtr("false"); expr = builder->CreateSelect(expr, t, f); } + if (t.is() && expr->getType() != f64_t) { + // printf is variadic, so the default argument promotions apply: any + // float narrower than a double arrives as a double, and that is what + // the "%f" specifier reads. Passing the narrow value straight through + // leaves the upper half of the argument undefined and prints garbage. + expr = builder->CreateFPExt(expr, f64_t); + } args.push_back(expr); } @@ -1591,6 +1606,10 @@ void CodeGen_LLVM::visit(const SetOp *node) { internal_error << "TODO: implement SetOp code generation: " << Expr(node); } +void CodeGen_LLVM::visit(const AggOp *node) { + internal_error << "TODO: implement AggOp code generation: " << Expr(node); +} + void CodeGen_LLVM::visit(const Call *node) { llvm::Function *func = codegen_func_ptr(node->func); const size_t n_args = node->args.size(); @@ -2461,6 +2480,39 @@ void CodeGen_LLVM::visit(const Accumulate *node) { acc = builder->CreateSelect(cmp, current, update); break; } + case Accumulate::Argmax: { + // acc = select(curr.first > update.first, curr, update) + llvm::Value *curr_key = + builder->CreateExtractValue(current, 0); // curr.first + llvm::Value *new_key = + builder->CreateExtractValue(update, 0); // update.first + + internal_assert(curr_key->getType()->isFloatingPointTy()); + llvm::Value *cmp = + builder->CreateFCmpOGT(curr_key, new_key); // curr_key > new_key + + // Select the full struct based on which key is larger + acc = builder->CreateSelect(cmp, current, update); + break; + } + case Accumulate::Min: + case Accumulate::Max: { + const bool is_min = node->op == Accumulate::Min; + const Type &t = node->value.type(); + llvm::Value *cmp = nullptr; + if (t.is_float()) { + cmp = is_min ? builder->CreateFCmpOLT(current, update) + : builder->CreateFCmpOGT(current, update); + } else if (t.is_int()) { + cmp = is_min ? builder->CreateICmpSLT(current, update) + : builder->CreateICmpSGT(current, update); + } else { + cmp = is_min ? builder->CreateICmpULT(current, update) + : builder->CreateICmpUGT(current, update); + } + acc = builder->CreateSelect(cmp, current, update); + break; + } default: { internal_error << "TODO: implement codegen for accumulate: " << Stmt(node); diff --git a/src/IR/Analysis.cpp b/src/IR/Analysis.cpp index 40e40d04d..7dbe69d66 100644 --- a/src/IR/Analysis.cpp +++ b/src/IR/Analysis.cpp @@ -78,7 +78,13 @@ struct GatherFreeVars : public Visitor { } void visit(const Accumulate *node) override { - seen_vars.insert(node->loc.base); + // Accumulating into a location makes that location a free variable of + // the enclosing traversal; without this the generated traversal + // function never receives the accumulator. + if (!seen_vars.contains(node->loc.base)) { + seen_vars.insert(node->loc.base); + free_vars.push_back({node->loc.base, node->loc.base_type}); + } for (const auto &value : node->loc.accesses) { if (std::holds_alternative(value)) { std::get(value).accept(this); @@ -413,7 +419,7 @@ std::vector gather_struct_types(const Program &program) { // TODO: merge with is_const ? bool is_constant_expr(const Expr &expr) { // TODO: constant fold first? - if (expr.is()) { + if (expr.is()) { return true; } else if (expr.is()) { return is_constant_expr(expr.as()->value); diff --git a/src/IR/Equality.cpp b/src/IR/Equality.cpp index f9ef11e61..8bce5e12c 100644 --- a/src/IR/Equality.cpp +++ b/src/IR/Equality.cpp @@ -273,9 +273,15 @@ Cmp compare_types(const Type &t0, const Type &t1) { } } else if (const auto *vol0 = annot0.as()) { const auto *vol1 = annot1.as(); - if (!vol1) - return annot1.as() ? Cmp::Greater - : Cmp::Less; + if (!vol1) { + // Less than if annot1 is Aggregate or Interval, + // Otherwise greater than + if (annot1.as() || + annot1.as()) + return Cmp::Less; + return Cmp::Greater; + } + if (const Cmp geometry = compare_primitives(vol0->geometry, vol1->geometry); geometry != Cmp::Equals) { @@ -302,8 +308,14 @@ Cmp compare_types(const Type &t0, const Type &t1) { } } else if (const auto *int0 = annot0.as()) { const auto *int1 = annot1.as(); - if (!int1) + if (!int1) { + // Less than if annot1 is Aggregate, + // Otherwise greater than + if (annot1.as()) + return Cmp::Less; return Cmp::Greater; + } + if (const Cmp valid = compare_primitives(int0->scalar, int1->scalar); valid != Cmp::Equals) { @@ -315,11 +327,31 @@ Cmp compare_types(const Type &t0, const Type &t1) { return valid; } - if (const Cmp valid = - compare_primitives(int0->high, int1->high); + return compare_primitives(int0->high, int1->high); + } else if (const auto *agg0 = annot0.as()) { + const auto *agg1 = annot1.as(); + if (!agg1) + return Cmp::Greater; + if (const Cmp op = compare_primitives(agg0->op, agg1->op); + op != Cmp::Equals) { + return op; + } + + if (const Cmp valid = compare_primitives(agg0->args.size(), + agg1->args.size()); valid != Cmp::Equals) { return valid; } + + for (size_t i = 0, e = agg0->args.size(); i < e; i++) { + if (const Cmp cmp = + compare_primitives(agg0->args[i], agg0->args[i]); + cmp != Cmp::Equals) { + return cmp; + } + } + + return compare_primitives(agg0->value, agg1->value); } else { internal_error << "TODO: support variant other than Data or " @@ -430,10 +462,13 @@ Cmp compare_exprs(const Expr &e0, const Expr &e1) { return *nodes_cmp; } - internal_assert(e0.type().defined() && e1.type().defined()); - if (const Cmp types = compare_types(e0.type(), e1.type()); - types != Cmp::Equals) { - return types; + if (e0.type().defined() && e1.type().defined()) { + if (const Cmp types = compare_types(e0.type(), e1.type()); + types != Cmp::Equals) { + return types; + } + } else if (e0.type().defined() != e1.type().defined()) { + return e0.type().defined() ? Cmp::Greater : Cmp::Less; } // Must both be the same node type. @@ -469,9 +504,8 @@ Cmp compare_exprs(const Expr &e0, const Expr &e1) { case IRExprEnum::Var: { return compare_primitives(e0.as()->name, e1.as()->name); } - case IRExprEnum::Infinity: { - // Equal because types are equal. - return Cmp::Equals; + case IRExprEnum::Extrema: { + return compare_primitives(e0.as()->op, e1.as()->op); } case IRExprEnum::BinOp: { const BinOp *b0 = e0.as(); @@ -631,6 +665,25 @@ Cmp compare_exprs(const Expr &e0, const Expr &e1) { } return compare_exprs(v0->b, v1->b); } + case IRExprEnum::AggOp: { + const AggOp *v0 = e0.as(); + const AggOp *v1 = e1.as(); + if (const Cmp op = compare_primitives(v0->op, v1->op); + op != Cmp::Equals) { + return op; + } + if (v0->op == AggOp::reduce) { + if (const Cmp id = compare_exprs(v0->identity, v1->identity); + id != Cmp::Equals) { + return id; + } + if (const Cmp c = compare_exprs(v0->combiner, v1->combiner); + c != Cmp::Equals) { + return c; + } + } + return compare_exprs(v0->a, v1->a); + } case IRExprEnum::Call: { const Call *v0 = e0.as(); const Call *v1 = e1.as(); diff --git a/src/IR/Expr.cpp b/src/IR/Expr.cpp index 2d5b7e0e6..6785fb363 100644 --- a/src/IR/Expr.cpp +++ b/src/IR/Expr.cpp @@ -169,11 +169,12 @@ Expr StringImm::make(std::string value) { return node; } -Expr Infinity::make(Type t) { +Expr Extrema::make(Type t, Extrema::OpType op) { internal_assert(t.defined() && t.is_numeric()) - << "Infinity can be made for numeric types only: " << t; + << "Extrema can be made for numeric types only: " << t; - Infinity *node = new Infinity; + Extrema *node = new Extrema; + node->op = op; node->type = std::move(t); return node; } @@ -465,6 +466,17 @@ Expr Broadcast::make(uint32_t lanes, Expr value) { Expr VectorReduce::make(VectorReduce::OpType op, Expr value) { internal_assert(value.defined()) << "VectorReduce of undefined."; + // `sum` and `prod` spell both a vector reduction and a set aggregation. + // Dispatch on the argument's type. + if (value.type().defined() && value.type().is()) { + if (op == VectorReduce::Add) { + return AggOp::make(AggOp::sum, std::move(value)); + } + if (op == VectorReduce::Mul) { + return AggOp::make(AggOp::prod, std::move(value)); + } + } + VectorReduce *node = new VectorReduce; const bool infer_types = @@ -814,7 +826,7 @@ Expr Access::make(std::string field, Expr value) { Expr Unwrap::make(size_t index, Expr value) { internal_assert(value.defined() && value.type().is()) - << "Bad Unwrap parameters: " << value; + << "Bad Unwrap parameters: " << value << " has type: " << value.type(); internal_assert(index < value.type().as()->nodes.size()) << "Bad Unwrap parameters: " << value << " unwrapped with " << index; @@ -1037,11 +1049,11 @@ Expr GeomOp::make(OpType op, Expr a, Expr b) { namespace { +// Must stay in the same order as GeomOp::OpType. const char *const geometric_op_names[] = { - "contains", - "distmax", - "distmin", - "intersects", + "contains", "covers", "disjoint", "equals", "intersects", + "touches", "within", "lex", "ley", "lez", + "ltx", "lty", "ltz", "distmax", "distmin", }; static_assert(sizeof(geometric_op_names) / sizeof(geometric_op_names[0]) == @@ -1054,6 +1066,69 @@ const char *GeomOp::intrinsic_name(const OpType &op) { return geometric_op_names[op]; } +namespace { + +// Shared validation for the Figure 2 operators that apply a lambda to a set's +// elements. Checks that `b` is a set and that `a`, when its type is known, is +// a function accepting those elements. Returns the lambda's function type, or +// null when types are not being inferred. +const Function_t *check_element_lambda(const char *name, const Expr &a, + const Expr &b) { + internal_assert((b.type().is())) + << "Expected rhs of " << name << " to be a set, instead received: " << b + << " : " << b.type(); + + const Function_t *f = a.type().as(); + if (f == nullptr) { + internal_assert(!a.type().defined() && !type_enforcement_enabled()) + << "Expected lhs of " << name + << " to be a function, instead received: " << a << " : " + << a.type(); + return nullptr; + } + + if (f->arg_types.size() == 1) { + internal_assert(equals(f->arg_types[0].type, b.type().element_of())) + << "Expected the " << name + << " function to accept an element of type: " + << b.type().element_of() << " instead got " << a << " : " + << a.type(); + } else { + // Coiterating several sets passes the element tuple componentwise. + internal_assert(b.type().element_of().is() && + b.type().element_of().as()->etypes.size() == + f->arg_types.size()) + << "Expected the " << name + << " function to accept elements of group: " + << b.type().element_of() << " instead got " << a << " : " + << a.type(); + } + return f; +} + +void check_returns_bool(const char *name, const Function_t *f, const Expr &a) { + internal_assert(f == nullptr || f->ret_type.is_bool()) + << "Expected lhs of " << name + << " to be a boolean function, instead received: " << a << " : " + << a.type(); +} + +void check_returns_numeric(const char *name, const Function_t *f, + const Expr &a) { + internal_assert(f == nullptr || f->ret_type.is_numeric()) + << "Expected lhs of " << name + << " to be a numeric function, instead received: " << a << " : " + << a.type(); +} + +// A reduction over a set that might be empty has no value to report, so its +// result becomes optional. +Type optional_if_empty(Type t, const Expr &set) { + return can_be_empty(set) ? Option_t::make(std::move(t)) : t; +} + +} // namespace + Expr SetOp::make(OpType op, Expr a, Expr b) { internal_assert(a.defined() && b.defined()) << "SetOp::make received undefined value: " << to_string(op) << " " << a @@ -1061,79 +1136,58 @@ Expr SetOp::make(OpType op, Expr a, Expr b) { SetOp *node = new SetOp; // Only do type inference if it's enabled or both types are defined. - // Filters and argmins can be infered regardless of the lambda type. + // Operators whose result does not depend on the lambda's return type can + // be inferred regardless of it. + const bool result_independent_of_lambda = + op == SetOp::filter || op == SetOp::argmin || op == SetOp::argmax || + op == SetOp::any || op == SetOp::all; const bool infer_types = type_enforcement_enabled() || - ((op == SetOp::filter || op == SetOp::argmin || a.type().defined()) && + ((result_independent_of_lambda || a.type().defined()) && b.type().defined()); if (infer_types) { - if (op == SetOp::filter) { - // TODO: allow this assert to pass if !a.type().defined() only if - // !type_enforcement_enabled() - internal_assert( - (!a.type().defined() && !type_enforcement_enabled()) || - (a.type().is() && - a.type().as()->ret_type.is_bool())) - << "Expected lhs of filter to be a boolean function, instead " - "received: " - << a << " : " << a.type(); - internal_assert((b.type().is())) - << "Expected rhs of filter to be a (set|bvh), instead " - "received: " - << b << " : " << b.type(); - if (const Function_t *f = a.type().as()) { - if (f->arg_types.size() == 1) { - internal_assert( - equals(f->arg_types[0].type, b.type().element_of())) - << "Expected filter function to accept element of " - "type: " - << b.type().element_of() << " instead got " << a - << " : " << a.type(); - } else { - internal_assert( - b.type().element_of().is() && - b.type().element_of().as()->etypes.size() == - f->arg_types.size()) - << "Expected filter function to accept elements of " - "group: " - << b.type().element_of() << " instead got " << a - << " : " << a.type(); - } - } + switch (op) { + case SetOp::filter: { + const Function_t *f = check_element_lambda("filter", a, b); + check_returns_bool("filter", f, a); node->type = b.type(); - } else if (op == SetOp::argmin) { - internal_assert( - (!a.type().defined() && !type_enforcement_enabled()) || - (a.type().is() && - a.type().as()->ret_type.is_numeric())) - << "Expected lhs of argmin to be a numeric function, instead " - "received: " - << a << " : " << a.type(); - internal_assert(b.type().is() || b.type().is()) - << "Expected rhs of argmin to be a set, instead received: " << b - << " : " << b.type(); - if (const Function_t *f = a.type().as()) { - internal_assert( - f->arg_types.size() == 1 && - equals(f->arg_types[0].type, b.type().element_of())) - << "Expected argmin function to accept element of type: " - << b.type().element_of() << " instead got " << a << " : " - << a.type(); - } - if (can_be_empty(b)) { - node->type = Option_t::make(b.type().element_of()); - } else { - node->type = b.type().element_of(); - } - } else if (op == SetOp::map) { - internal_assert(a.type().is()) - << "Expected lhs of map to be afunction, instead received: " - << a << " : " << a.type(); + break; + } + case SetOp::any: + case SetOp::all: { + const char *name = op == SetOp::any ? "any" : "all"; + const Function_t *f = check_element_lambda(name, a, b); + check_returns_bool(name, f, a); + node->type = Bool_t::make(); + break; + } + case SetOp::argmin: + case SetOp::argmax: { + const char *name = op == SetOp::argmin ? "argmin" : "argmax"; + const Function_t *f = check_element_lambda(name, a, b); + check_returns_numeric(name, f, a); + node->type = optional_if_empty(b.type().element_of(), b); + break; + } + case SetOp::minimum: + case SetOp::maximum: { + const char *name = op == SetOp::minimum ? "minimum" : "maximum"; + const Function_t *f = check_element_lambda(name, a, b); + check_returns_numeric(name, f, a); + internal_assert(f) << "Cannot infer the type of " << name + << " without a typed metric: " << a; + node->type = optional_if_empty(f->ret_type, b); + break; + } + case SetOp::map: { internal_assert((b.type().is())) << "Expected rhs of map to be a set or array, instead " "received: " << b << " : " << b.type(); + internal_assert(a.type().is()) + << "Expected lhs of map to be a function, instead received: " + << a << " : " << a.type(); const Function_t *f = a.type().as(); internal_assert(f->arg_types.size() == 1 && equals(f->arg_types[0].type, b.type().element_of())) @@ -1146,7 +1200,9 @@ Expr SetOp::make(OpType op, Expr a, Expr b) { Expr size = b.type().as()->size; node->type = Array_t::make(f->ret_type, std::move(size)); } - } else if (op == SetOp::product) { + break; + } + case SetOp::product: { internal_assert(a.type().is_iterable() && b.type().is_iterable()) << "Expected args of product to be iterables, instead " "received: " @@ -1155,6 +1211,8 @@ Expr SetOp::make(OpType op, Expr a, Expr b) { Type btype = b.type().element_of(); Type tuple_t = Tuple_t::make({std::move(atype), std::move(btype)}); node->type = Set_t::make(std::move(tuple_t)); + break; + } } } @@ -1164,6 +1222,60 @@ Expr SetOp::make(OpType op, Expr a, Expr b) { return node; } +Expr AggOp::make(Expr identity, Expr combiner, Expr a) { + internal_assert(identity.defined() && combiner.defined() && a.defined()) + << "AggOp::make received undefined value in reduce"; + internal_assert(!a.type().defined() || a.type().is()) + << "Expected the argument of reduce to be a set, instead received: " + << a << " : " << a.type(); + + AggOp *node = new AggOp; + node->op = OpType::reduce; + node->identity = std::move(identity); + node->combiner = std::move(combiner); + node->a = std::move(a); + node->type = node->identity.type(); + + if (const Function_t *f = node->combiner.type().as()) { + internal_assert(f->arg_types.size() == 2 && + equals(f->arg_types[0].type, node->type) && + equals(f->arg_types[1].type, node->type) && + equals(f->ret_type, node->type)) + << "Expected the combiner of reduce to be a binary function on " + << node->type << ", instead received: " << node->combiner << " : " + << node->combiner.type(); + } + return node; +} + +Expr AggOp::make(OpType op, Expr a) { + internal_assert(op != OpType::reduce) + << "reduce must be built with an identity and a combiner"; + internal_assert(a.defined()) + << "AggOp::make received undefined value: " << to_string(op) << " " + << a; + AggOp *node = new AggOp; + + // Only do type inference if it's enabled or a's type is defined. + const bool infer_types = + type_enforcement_enabled() || a.type().defined() || op == OpType::count; + + if (infer_types) { + if (op == OpType::count) { + node->type = UInt_t::make(64); + } else { + internal_assert(a.type().defined() && a.type().as()) + << "AggOp received undefined or non-set expr: " << a + << " of type " << a.type(); + node->type = a.type().element_of(); + } + } + + node->op = op; + node->a = std::move(a); + return node; +} + Expr Call::make(Expr func, std::vector args) { internal_assert(func.defined()) << "Call::make received undefined func"; internal_assert(std::all_of(args.cbegin(), args.cend(), diff --git a/src/IR/Mutator.cpp b/src/IR/Mutator.cpp index e9ed49855..8889c7047 100644 --- a/src/IR/Mutator.cpp +++ b/src/IR/Mutator.cpp @@ -205,8 +205,10 @@ Type Mutator::visit(const BVH_t *node) { vol->broadcast}}; } else { const auto *interval = annot.as(); - internal_assert(interval) - << "Handle non-(Data | Volume | Interval) in Mutator"; + const auto *aggregate = annot.as(); + internal_assert(interval || aggregate) + << "Handle non-(Data | Volume | Interval | Aggregate) in " + "Mutator"; // TODO: handle low/high as Exprs? return annot; } @@ -271,7 +273,7 @@ Expr Mutator::visit(const VecImm *node) { return node; } Expr Mutator::visit(const StringImm *node) { return node; } -Expr Mutator::visit(const Infinity *node) { return node; } +Expr Mutator::visit(const Extrema *node) { return node; } Expr Mutator::visit(const Var *node) { return node; } @@ -423,6 +425,23 @@ Expr Mutator::visit(const SetOp *node) { return SetOp::make(node->op, std::move(a), std::move(b)); } +Expr Mutator::visit(const AggOp *node) { + Expr a = mutate(node->a); + if (node->op != AggOp::reduce) { + if (a.same_as(node->a)) { + return node; + } + return AggOp::make(node->op, std::move(a)); + } + Expr identity = mutate(node->identity); + Expr combiner = mutate(node->combiner); + if (a.same_as(node->a) && identity.same_as(node->identity) && + combiner.same_as(node->combiner)) { + return node; + } + return AggOp::make(std::move(identity), std::move(combiner), std::move(a)); +} + Expr Mutator::visit(const Call *node) { Expr func = mutate(node->func); auto [args, not_changed] = visit_list(this, node->args); @@ -620,15 +639,16 @@ Stmt Mutator::visit(const Iterate *node) { if (value.same_as(node->value)) { return node; } - return Scan::make(std::move(value)); + return Iterate::make(std::move(value)); } Stmt Mutator::visit(const Scan *node) { Expr value = mutate(node->value); - if (value.same_as(node->value)) { + Expr func = node->func.defined() ? mutate(node->func) : node->func; + if (value.same_as(node->value) && func.same_as(node->func)) { return node; } - return Scan::make(std::move(value)); + return Scan::make(node->op, node->loc, std::move(func), std::move(value)); } Stmt Mutator::visit(const YieldFrom *node) { diff --git a/src/IR/Operators.cpp b/src/IR/Operators.cpp index a1f219a00..bced754f4 100644 --- a/src/IR/Operators.cpp +++ b/src/IR/Operators.cpp @@ -1,6 +1,9 @@ #include "IR/Operators.h" #include "IR/Equality.h" +#include "IR/Printer.h" + +#include "Utils.h" namespace bonsai { namespace ir { @@ -93,6 +96,35 @@ Expr contains(Expr a, Expr b) { return GeomOp::make(GeomOp::contains, std::move(a), std::move(b)); } +Expr covers(Expr a, Expr b) { + return GeomOp::make(GeomOp::covers, std::move(a), std::move(b)); +} + +Expr disjoint(Expr a, Expr b) { + return GeomOp::make(GeomOp::disjoint, std::move(a), std::move(b)); +} + +// Named to avoid colliding with ir::equals, the structural comparison. +Expr equals_geom(Expr a, Expr b) { + return GeomOp::make(GeomOp::equals, std::move(a), std::move(b)); +} + +Expr touches(Expr a, Expr b) { + return GeomOp::make(GeomOp::touches, std::move(a), std::move(b)); +} + +Expr within(Expr a, Expr b) { + return GeomOp::make(GeomOp::within, std::move(a), std::move(b)); +} + +Expr ordering(GeomOp::OpType op, Expr a, Expr b) { + internal_assert(op == GeomOp::lex || op == GeomOp::ley || + op == GeomOp::lez || op == GeomOp::ltx || + op == GeomOp::lty || op == GeomOp::ltz) + << "Not an ordering predicate: " << GeomOp::intrinsic_name(op); + return GeomOp::make(op, std::move(a), std::move(b)); +} + Expr filter(Expr predicate, Expr set) { return SetOp::make(SetOp::filter, std::move(predicate), std::move(set)); } @@ -105,10 +137,94 @@ Expr map(Expr func, Expr set) { return SetOp::make(SetOp::map, std::move(func), std::move(set)); } +Expr argmax(Expr metric, Expr set) { + return SetOp::make(SetOp::argmax, std::move(metric), std::move(set)); +} + +Expr minimum(Expr metric, Expr set) { + return SetOp::make(SetOp::minimum, std::move(metric), std::move(set)); +} + +Expr maximum(Expr metric, Expr set) { + return SetOp::make(SetOp::maximum, std::move(metric), std::move(set)); +} + +Expr any(Expr predicate, Expr set) { + return SetOp::make(SetOp::any, std::move(predicate), std::move(set)); +} + +Expr all(Expr predicate, Expr set) { + return SetOp::make(SetOp::all, std::move(predicate), std::move(set)); +} + Expr product(Expr a, Expr b) { return SetOp::make(SetOp::product, std::move(a), std::move(b)); } +Expr avg(Expr a) { return AggOp::make(AggOp::avg, std::move(a)); } + +Expr count(Expr a) { return AggOp::make(AggOp::count, std::move(a)); } + +Expr prod(Expr a) { return AggOp::make(AggOp::prod, std::move(a)); } + +Expr sum(Expr a) { + internal_assert(a.type().defined()) << a; + if (a.type().as()) { + return AggOp::make(AggOp::sum, std::move(a)); + } else if (a.type().as()) { + return VectorReduce::make(VectorReduce::Add, std::move(a)); + } + internal_error << a; +} + +Expr reduce(Expr identity, Expr combiner, Expr a) { + return AggOp::make(std::move(identity), std::move(combiner), std::move(a)); +} + +Expr binary_lambda(BinOp::OpType op, Type t) { + Expr a = Var::make(t, "_a"); + Expr b = Var::make(t, "_b"); + Expr body = BinOp::make(op, std::move(a), std::move(b)); + return Lambda::make({{"_a", t}, {"_b", t}}, std::move(body)); +} + +Expr expand_aggregate(const AggOp *agg) { + internal_assert(agg) << "expand_aggregate received null"; + if (agg->op == AggOp::reduce) { + return agg; + } + + const Type elem_t = agg->a.type().element_of(); + switch (agg->op) { + case AggOp::count: { + // count(S) = reduce(0, +, map(|x| 1, S)) + const Type t = agg->type; + Expr one = Lambda::make({{"_x", elem_t}}, make_one(t)); + return reduce(make_zero(t), binary_lambda(BinOp::Add, t), + map(std::move(one), agg->a)); + } + case AggOp::sum: { + // sum(S) = reduce(0, +, S) + return reduce(make_zero(elem_t), binary_lambda(BinOp::Add, elem_t), + agg->a); + } + case AggOp::prod: { + // prod(S) = reduce(1, *, S) + return reduce(make_one(elem_t), binary_lambda(BinOp::Mul, elem_t), + agg->a); + } + case AggOp::avg: { + // avg needs a pair reduction (running sum and count) which the + // combiner machinery does not build yet. + internal_error << "[unimplemented] avg over a set: " << Expr(agg); + } + case AggOp::reduce: { + break; // handled above + } + } + internal_error << "Unknown aggregation: " << Expr(agg); +} + Expr abs(Expr a) { return Intrinsic::make(Intrinsic::abs, {std::move(a)}); } Expr max(Expr a, Expr b) { diff --git a/src/IR/Printer.cpp b/src/IR/Printer.cpp index 4df45e9b2..be7bedd2a 100644 --- a/src/IR/Printer.cpp +++ b/src/IR/Printer.cpp @@ -599,7 +599,7 @@ void Printer::visit(const Generic_t *node) { void Printer::print(const BVH_t::Node &node) { const auto print_annotation = [&](const Annotation &annot) { - if (const Annotation::Data *data = annot.as()) { + if (const auto *data = annot.as()) { os << "data = " << data->name; } else if (const auto *vol = annot.as()) { internal_assert(vol->struct_type.is()); @@ -617,12 +617,22 @@ void Printer::print(const BVH_t::Node &node) { os << " on " << vol->geometry; } // TODO: print broadcast somehow? - } else { - const auto *interval = annot.as(); - internal_assert(interval) << "TODO: handle non-(Data | Volume | " - "Interval) annotions in Printer\n"; + } else if (const auto *interval = annot.as()) { os << interval->scalar << " in [" << interval->low << ", " << interval->high << "]"; + } else if (const auto *aggregate = annot.as()) { + os << to_string(aggregate->op) << "("; + for (size_t i = 0, e = aggregate->args.size(); i < e; i++) { + if (i != 0) { + os << ", "; + } + os << aggregate->args[i]; + } + os << ") = "; + os << aggregate->value; + } else { + internal_error << "TODO: handle non-(Data | Volume | Interval | " + "Aggregate) annotions in Printer\n"; } }; @@ -781,11 +791,20 @@ void Printer::visit(const StringImm *node) { print_string_imm(os, node->value); } -void Printer::visit(const Infinity *node) { +void Printer::visit(const Extrema *node) { os << "("; print(node->type); os << ")"; - os << "inf"; + switch (node->op) { + case Extrema::inf: { + os << "inf"; + break; + } + case Extrema::eps: { + os << "eps"; + break; + } + } } void Printer::visit(const Var *node) { os << node->name; } @@ -1063,17 +1082,59 @@ void Printer::visit(const GeomOp *node) { std::string to_string(const SetOp::OpType &op) { switch (op) { + case SetOp::all: + return "all"; + case SetOp::any: + return "any"; + case SetOp::argmax: + return "argmax"; case SetOp::argmin: return "argmin"; case SetOp::filter: return "filter"; case SetOp::map: return "map"; + case SetOp::maximum: + return "maximum"; + case SetOp::minimum: + return "minimum"; case SetOp::product: return "product"; } } +std::string to_string(const AggOp::OpType &op) { + switch (op) { + case AggOp::avg: + return "avg"; + case AggOp::count: + return "count"; + case AggOp::prod: + return "prod"; + case AggOp::reduce: + return "reduce"; + case AggOp::sum: + return "sum"; + } +} + +std::string to_string(const Annotation::Aggregate::OpType &op) { + switch (op) { + case Annotation::Aggregate::avg: + return "avg"; + case Annotation::Aggregate::count: + return "count"; + case Annotation::Aggregate::max: + return "max"; + case Annotation::Aggregate::min: + return "min"; + case Annotation::Aggregate::prod: + return "prod"; + case Annotation::Aggregate::sum: + return "sum"; + } +} + void Printer::visit(const SetOp *node) { // TODO: print type? os << to_string(node->op) << "("; @@ -1083,6 +1144,19 @@ void Printer::visit(const SetOp *node) { os << ")"; } +void Printer::visit(const AggOp *node) { + // TODO: print type? + os << to_string(node->op) << "("; + if (node->op == AggOp::reduce) { + print_no_parens(node->identity); + os << ", "; + print_no_parens(node->combiner); + os << ", "; + } + print_no_parens(node->a); + os << ")"; +} + void Printer::visit(const Call *node) { // TODO: print type? print_no_parens(node->func); @@ -1264,6 +1338,14 @@ void Printer::visit(const Accumulate *node) { os << " argmax= "; break; } + case Accumulate::OpType::Max: { + os << " max= "; + break; + } + case Accumulate::OpType::Min: { + os << " min= "; + break; + } default: { internal_error << "TODO: implement printing for all Accumulate op types!"; @@ -1335,7 +1417,15 @@ void Printer::visit(const Iterate *node) { void Printer::visit(const Scan *node) { os << get_indent(); - os << "scan "; + os << "scan"; + if (node->op) { + os << "<" << to_string(*(node->op)) << ">"; + } + os << " "; + if (node->func.defined()) { + print_no_parens(node->func); + os << " over "; + } print_no_parens(node->value); end_stmt(); } diff --git a/src/IR/Stmt.cpp b/src/IR/Stmt.cpp index 71718b7c3..23cbbe975 100644 --- a/src/IR/Stmt.cpp +++ b/src/IR/Stmt.cpp @@ -237,9 +237,16 @@ Stmt Iterate::make(Expr value) { return node; } -Stmt Scan::make(Expr value) { +Stmt Scan::make(std::optional op, WriteLoc loc, Expr func, + Expr value) { internal_assert(value.defined()) << "Undefined value in Scan::make"; + internal_assert(op.has_value() == loc.defined()) + << "A reducing scan needs an accumulator, and only a reducing scan " + "has one"; Scan *node = new Scan; + node->op = std::move(op); + node->loc = std::move(loc); + node->func = std::move(func); node->value = std::move(value); return node; } diff --git a/src/IR/Type.cpp b/src/IR/Type.cpp index fd480d971..390454c12 100644 --- a/src/IR/Type.cpp +++ b/src/IR/Type.cpp @@ -430,6 +430,24 @@ Type Generic_t::make(std::string name, Interface interface) { return node; } +Annotation::Aggregate::OpType +Annotation::Aggregate::str_to_op(const std::string &op) { + if (op == "avg") { + return Annotation::Aggregate::OpType::avg; + } else if (op == "count") { + return Annotation::Aggregate::OpType::count; + } else if (op == "max") { + return Annotation::Aggregate::OpType::max; + } else if (op == "min") { + return Annotation::Aggregate::OpType::min; + } else if (op == "prod") { + return Annotation::Aggregate::OpType::prod; + } else if (op == "sum") { + return Annotation::Aggregate::OpType::sum; + } + internal_error << "Unknown Aggregate type: " << op; +} + namespace { bool validate_volume(const Annotation::Volume &volume, diff --git a/src/IR/Visitor.cpp b/src/IR/Visitor.cpp index e4f81ea07..f00510d32 100644 --- a/src/IR/Visitor.cpp +++ b/src/IR/Visitor.cpp @@ -111,7 +111,7 @@ void Visitor::visit(const VecImm *) {} void Visitor::visit(const StringImm *) {} -void Visitor::visit(const Infinity *) {} +void Visitor::visit(const Extrema *) {} void Visitor::visit(const Var *) {} @@ -174,6 +174,16 @@ void Visitor::visit(const SetOp *node) { node->b.accept(this); } +void Visitor::visit(const AggOp *node) { + if (node->identity.defined()) { + node->identity.accept(this); + } + if (node->combiner.defined()) { + node->combiner.accept(this); + } + node->a.accept(this); +} + void Visitor::visit(const Call *node) { node->func.accept(this); visit_list(this, node->args); diff --git a/src/IR/WriteLoc.cpp b/src/IR/WriteLoc.cpp index 0eb7753a0..a0a7461b4 100644 --- a/src/IR/WriteLoc.cpp +++ b/src/IR/WriteLoc.cpp @@ -59,6 +59,29 @@ void WriteLoc::add_index_access(const Expr &index) { } } +ir::Expr WriteLoc::to_expr() const { + ir::Expr expr = ir::Var::make(base_type, base); + for (const auto &value : this->accesses) { + if (std::holds_alternative(value)) { + expr = Access::make(std::get(value), expr); + continue; + } + if (std::holds_alternative(value)) { + expr = Extract::make(expr, std::get(value)); + continue; + } + /* + if (std::holds_alternative(value)) { + auto cast = std::get(value); + expr = ir::Cast::make(cast.type, expr, cast.mode); + continue; + } + */ + internal_error << "[unexpected] variant in WriteLoc"; + } + return expr; +} + WriteLoc WriteLoc::rebuild_with_base_type(Type _type) const { internal_assert(_type.defined()) << "Write location rebuild triggered with undefined type for base: " diff --git a/src/Lower/Defers.cpp b/src/Lower/Defers.cpp index dd29ecca5..90ddde469 100644 --- a/src/Lower/Defers.cpp +++ b/src/Lower/Defers.cpp @@ -14,6 +14,7 @@ #include "Error.h" #include "Utils.h" +#include #include #include #include diff --git a/src/Lower/Lambdas.cpp b/src/Lower/Lambdas.cpp index fd91d4018..09005fd38 100644 --- a/src/Lower/Lambdas.cpp +++ b/src/Lower/Lambdas.cpp @@ -139,8 +139,13 @@ class Blacklist : public ir::Visitor { switch (node->op) { case ir::SetOp::OpType::product: return; + case ir::SetOp::OpType::all: + case ir::SetOp::OpType::any: + case ir::SetOp::OpType::argmax: case ir::SetOp::OpType::argmin: case ir::SetOp::OpType::map: + case ir::SetOp::OpType::maximum: + case ir::SetOp::OpType::minimum: case ir::SetOp::OpType::filter: { const ir::Lambda *op = node->a.as(); internal_assert(op) << "first operand of a ir::SetOp should have " diff --git a/src/Lower/Layouts.cpp b/src/Lower/Layouts.cpp index 0f3aa3fe0..164511ece 100644 --- a/src/Lower/Layouts.cpp +++ b/src/Lower/Layouts.cpp @@ -11,6 +11,7 @@ #include "Error.h" #include "Utils.h" +#include #include namespace bonsai { diff --git a/src/Lower/LoopTransforms.cpp b/src/Lower/LoopTransforms.cpp index 1d86e6665..0c5569679 100644 --- a/src/Lower/LoopTransforms.cpp +++ b/src/Lower/LoopTransforms.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace bonsai { diff --git a/src/Lower/PredicateAnalysis.cpp b/src/Lower/PredicateAnalysis.cpp index 5b7a3c33d..7a769e11a 100644 --- a/src/Lower/PredicateAnalysis.cpp +++ b/src/Lower/PredicateAnalysis.cpp @@ -43,6 +43,181 @@ void Interval::include(const ir::Expr &e) { namespace { +// The ordering predicate that is the strict/non-strict counterpart of `op`. +// `a <=_D b` is false exactly when `b <_D a`, and vice versa. +ir::GeomOp::OpType flip_strictness(ir::GeomOp::OpType op) { + switch (op) { + case ir::GeomOp::lex: + return ir::GeomOp::ltx; + case ir::GeomOp::ley: + return ir::GeomOp::lty; + case ir::GeomOp::lez: + return ir::GeomOp::ltz; + case ir::GeomOp::ltx: + return ir::GeomOp::lex; + case ir::GeomOp::lty: + return ir::GeomOp::ley; + case ir::GeomOp::ltz: + return ir::GeomOp::lez; + default: + internal_error << "Not an ordering predicate: " + << ir::GeomOp::intrinsic_name(op); + } +} + +bool is_ordering(ir::GeomOp::OpType op) { + switch (op) { + case ir::GeomOp::lex: + case ir::GeomOp::ley: + case ir::GeomOp::lez: + case ir::GeomOp::ltx: + case ir::GeomOp::lty: + case ir::GeomOp::ltz: + return true; + default: + return false; + } +} + +// Algorithm 5: Geometric Upper Bounds. Following the paper's notation, `u` is +// a uniform operand, taken as it is, and `v` a varying one bounded by a volume +// V. `va` and `vb` are those volumes, and are undefined for uniform operands. +// An undefined result is the algorithm's `_ |-> true`: no useful necessary +// condition, so the predicate may always hold. +ir::Expr geom_upper_bound(ir::GeomOp::OpType op, const ir::Expr &a, + const ir::Expr &b, const ir::Expr &va, + const ir::Expr &vb) { + const bool a_varying = va.defined(); + const bool b_varying = vb.defined(); + // Each operand, replaced by its bounding volume where it varies. + const ir::Expr &ea = a_varying ? va : a; + const ir::Expr &eb = b_varying ? vb : b; + + switch (op) { + case ir::GeomOp::contains: { + // Lines 5-7. Anything contained in the operand is also inside its + // volume, so at the very least the two must overlap. + if (a_varying && !b_varying) { + // Line 6 gives intersects(V_v, u). This is the tighter form, and + // still necessary: u lies inside v, which lies inside its own + // volume. It is also what the paper itself uses for the + // corresponding covers case on line 9. + return ir::contains(ea, eb); + } + return ir::intersects(ea, eb); + } + case ir::GeomOp::covers: { + // Lines 8-10. Everything a varying operand covers is inside its + // volume, which line 9 states tightly; the other two cases keep only + // that the operands must overlap. + if (a_varying && !b_varying) { + return ir::covers(ea, eb); + } + return ir::intersects(ea, eb); + } + case ir::GeomOp::touches: // Lines 22-24. + case ir::GeomOp::intersects: { // Lines 19-21. + return ir::intersects(ea, eb); + } + case ir::GeomOp::disjoint: { + // Lines 11-12. Disjointness is refuted only when the uniform operand + // swallows the whole volume, which forces an overlap. The two cases + // are the same relation spelled with the volume in the position its + // operand held. With both operands varying nothing can be refuted. + if (a_varying && b_varying) { + return ir::Expr(); + } + return a_varying ? ~ir::within(va, b) : ~ir::contains(a, vb); + } + case ir::GeomOp::within: { + // Lines 13-15. `within(a, b)` is `a` inside `b`. + if (a_varying && b_varying) { + return ir::intersects(ea, eb); + } + if (b_varying) { + // A uniform a inside a varying b must fit inside b's volume. + return ir::within(a, vb); + } + return ir::intersects(va, b); + } + case ir::GeomOp::equals: { + // Lines 16-18. Equality forces the uniform operand to fit inside the + // varying one's volume, whichever side it is on. The two single- + // varying cases are the same relation spelled two ways, each keeping + // the volume in the position its operand held: + // `within(u, V_v)` and `contains(V_v, u)`. + if (a_varying && b_varying) { + return ir::intersects(ea, eb); + } + return a_varying ? ir::contains(va, b) : ir::within(a, vb); + } + default: { + // Ordering predicates. Section 6.3 notes only that these are + // monotonic and bounded by rewriting varying arguments to their + // bounding volumes. Monotonicity gives the usual comparison rule: + // `a <=_D b` is necessary only if `b <_D a` fails on the volumes. + internal_assert(is_ordering(op)) + << "No upper bound rule for: " << ir::GeomOp::intrinsic_name(op); + return ~ir::ordering(flip_strictness(op), eb, ea); + } + } +} + +// Algorithm 6, lines 1-13: Geometric Lower Bounds. An undefined result is the +// algorithm's `_ |-> false`: no sufficient condition, so the predicate can +// never be proven to hold over a whole subtree. +ir::Expr geom_lower_bound(ir::GeomOp::OpType op, const ir::Expr &a, + const ir::Expr &b, const ir::Expr &va, + const ir::Expr &vb) { + const bool a_varying = va.defined(); + const bool b_varying = vb.defined(); + const ir::Expr &ea = a_varying ? va : a; + const ir::Expr &eb = b_varying ? vb : b; + + switch (op) { + case ir::GeomOp::contains: { + // Line 5. A uniform a that swallows b's whole volume contains every + // b in it. Nothing can be proven when a itself varies. + return (!a_varying && b_varying) ? ir::contains(a, vb) : ir::Expr(); + } + case ir::GeomOp::covers: { + // Line 6. + return (!a_varying && b_varying) ? ir::covers(a, vb) : ir::Expr(); + } + case ir::GeomOp::disjoint: { + // Lines 7-9. Disjointness is the one predicate that can be proven + // with both operands varying: disjoint volumes have disjoint + // contents. + return ir::disjoint(ea, eb); + } + case ir::GeomOp::intersects: { + // Lines 10-11. An operand whose entire volume sits inside the other + // necessarily meets it. + if (a_varying && b_varying) { + return ir::Expr(); + } + return b_varying ? ir::contains(a, vb) : ir::within(va, b); + } + case ir::GeomOp::within: { + // Line 12. `a` is inside `b` whenever a's whole volume is. + return (a_varying && !b_varying) ? ir::within(va, b) : ir::Expr(); + } + case ir::GeomOp::equals: + case ir::GeomOp::touches: { + // Line 13. A bounding volume can never prove an exact boundary + // relationship. + return ir::Expr(); + } + default: { + // Ordering predicates, as above: the relation holds for every object + // in the volumes exactly when it holds between the volumes. + internal_assert(is_ordering(op)) + << "No lower bound rule for: " << ir::GeomOp::intrinsic_name(op); + return ir::ordering(op, ea, eb); + } + } +} + struct PredicateAnalysis : public ir::Visitor { Interval interval; const VolumeMap &bounds; @@ -99,11 +274,14 @@ struct PredicateAnalysis : public ir::Visitor { void visit(const ir::BoolImm *node) override { set(node); } - void visit(const ir::Infinity *node) override { set(node); } + void visit(const ir::Extrema *node) override { set(node); } void visit(const ir::Var *node) override { set(node); } ir::Expr make_and(ir::Expr a, ir::Expr b) { + if (!a.defined() || !b.defined()) { + return ir::Expr(); + } if (is_const_one(a)) { return b; } @@ -119,6 +297,28 @@ struct PredicateAnalysis : public ir::Visitor { return a && b; } + ir::Expr make_or(ir::Expr a, ir::Expr b) { + // A disjunction with an unbounded side is only as good as the other + // side when that side settles the answer, which the constant folds + // below cover; otherwise nothing can be said. + if (is_const_one(a)) { + return a; + } + if (is_const_one(b)) { + return b; + } + if (!a.defined() || !b.defined()) { + return ir::Expr(); + } + if (is_const_zero(a)) { + return b; + } + if (is_const_zero(b)) { + return a; + } + return a || b; + } + // Handle Lt/Le void visit_compare(const ir::BinOp *node) { Interval a = get(node->a); @@ -198,20 +398,33 @@ struct PredicateAnalysis : public ir::Visitor { } return; } - case ir::BinOp::LAnd: { + // Section 6.2: boolean and and or are monotonically increasing in + // both arguments, so each bound is that operator applied to the + // corresponding bounds. `&` and `|` on booleans mean the same thing + // as `&&` and `||` here, and are what most predicates are written + // with. + case ir::BinOp::LAnd: + case ir::BinOp::BwAnd: + case ir::BinOp::LOr: + case ir::BinOp::BwOr: { if (!node->type.is_bool()) { break; // TODO: handle non-booleans } + const bool is_and = + node->op == ir::BinOp::LAnd || node->op == ir::BinOp::BwAnd; Interval a = get(node->a); Interval b = get(node->b); - // And is monotonic increasing in both args if (a.is_single_point(node->a) && b.is_single_point(node->b)) { interval = Interval::single_point(node); } else if (a.is_single_point() && b.is_single_point()) { - interval = Interval::single_point(a.min && b.min); - } else { + interval = Interval::single_point(is_and ? (a.min && b.min) + : (a.min || b.min)); + } else if (is_and) { interval.min = make_and(a.min, b.min); interval.max = make_and(a.max, b.max); + } else { + interval.min = make_or(a.min, b.min); + interval.max = make_or(a.max, b.max); } return; } @@ -277,18 +490,80 @@ struct PredicateAnalysis : public ir::Visitor { interval.max = std::move(e1); } else if (a.is_bounded()) { // Sign of b is unknown - ir::Expr cmp = - b.min >= make_zero(b.min.type().element_of()); + ir::Expr cmp = b.min >= make_zero(b.min.type()); interval.min = select(cmp, e1, e2); interval.max = select(cmp, e2, e1); } // else unbounded } else if (a.is_bounded() && b.is_bounded()) { // TODO: let exprs for linearity. + + ir::Expr low_high = a.min * b.max; + ir::Expr low_low = a.min * b.min; + ir::Expr high_low = a.max * b.min; + ir::Expr high_high = a.max * b.max; + + // TODO: could do tons of casework, be stupid for now. + + interval.min = + min(min(low_low, low_high), min(high_low, high_high)); + interval.max = + max(max(low_low, low_high), max(high_low, high_high)); } // TODO: for integers, need to handle overflow if defined. return; } + case ir::BinOp::Div: { + Interval a = get(node->a); + Interval b = get(node->b); + + internal_assert(node->type.is_float()) + << "TODO: handle non-float division in predicate analysis: " + << ir::Expr(node); + + // Do nothing with unbounded intervals + if (!a.is_bounded() || !b.is_bounded()) { + return; + } else if (a.is_single_point(node->a) && + b.is_single_point(node->b)) { + interval = Interval::single_point(node); + return; + } else if (a.is_single_point() && b.is_single_point()) { + interval = Interval::single_point(a.min / b.min); + return; + } + + // Both are fully bounded, at least one is a true interval, + // and the type is floating point. + + ir::Expr inf = ir::Extrema::make(node->type, ir::Extrema::inf); + ir::Expr zero = make_zero(node->type); + + ir::Expr denom_positive = b.min > zero; + ir::Expr denom_negative = b.max < zero; + ir::Expr denom_contains_zero = b.min < zero && b.max > zero; + // If ^ is true, bounds are infinite. + + ir::Expr num_nonneg = a.min >= zero; + ir::Expr num_nonpos = a.max <= zero; + ir::Expr num_spans = ~num_nonneg && ~num_nonpos; + + ir::Expr low_high = a.min / b.max; + ir::Expr low_low = a.min / b.min; + ir::Expr high_low = a.max / b.min; + ir::Expr high_high = a.max / b.max; + + // TODO: could do tons of casework, be stupid for now. + + interval.min = + min(min(low_low, low_high), min(high_low, high_high)); + interval.max = + max(max(low_low, low_high), max(high_low, high_high)); + + interval.min = select(denom_contains_zero, -inf, interval.min); + interval.max = select(denom_contains_zero, inf, interval.max); + return; + } default: { break; } @@ -297,8 +572,61 @@ struct PredicateAnalysis : public ir::Visitor { << ir::Expr(node); } - RESTRICT_VISITOR(ir::UnOp); - RESTRICT_VISITOR(ir::Select); + void visit(const ir::UnOp *node) override { + switch (node->op) { + case ir::UnOp::Not: { + // Section 6.2: negation is monotonically decreasing, so it is + // upper bounded by the negation of the lower bound and vice + // versa. + internal_assert(node->type.is_bool()) + << "TODO: predicate analysis of bitwise negation: " + << ir::Expr(node); + Interval a = get(node->a); + if (a.is_single_point(node->a)) { + interval = Interval::single_point(node); + return; + } + if (a.min.defined()) { + interval.max = ~a.min; + } + if (a.max.defined()) { + interval.min = ~a.max; + } + return; + } + case ir::UnOp::Neg: { + // Negation reverses the order of the bounds. + Interval a = get(node->a); + if (a.is_single_point(node->a)) { + interval = Interval::single_point(node); + return; + } + if (a.min.defined()) { + interval.max = -a.min; + } + if (a.max.defined()) { + interval.min = -a.max; + } + return; + } + } + } + + void visit(const ir::Select *node) override { + Interval c = get(node->cond); + Interval t = get(node->tvalue); + Interval f = get(node->fvalue); + + if (c.is_single_point()) { + interval.min = select(c.min, t.min, f.min); + interval.max = select(c.min, t.max, f.max); + } else { + interval.min = + min(select(c.min, t.min, f.min), select(c.max, t.min, f.min)); + interval.max = + max(select(c.min, t.max, f.max), select(c.max, t.max, f.max)); + } + } RESTRICT_VISITOR(ir::Cast); RESTRICT_VISITOR(ir::Broadcast); RESTRICT_VISITOR(ir::VectorReduce); @@ -306,7 +634,15 @@ struct PredicateAnalysis : public ir::Visitor { RESTRICT_VISITOR(ir::Ramp); RESTRICT_VISITOR(ir::Extract); RESTRICT_VISITOR(ir::Build); - RESTRICT_VISITOR(ir::Access); + + void visit(const ir::Access *node) override { + Interval a = get(node->value); + internal_assert(a.is_single_point(node->value)) + << "TODO: interval analysis of access on varying value: " << a.min + << ", " << a.max << " of " << ir::Expr(node); + interval = Interval::single_point(node); + } + RESTRICT_VISITOR(ir::Unwrap); void visit(const ir::Intrinsic *node) override { @@ -366,9 +702,28 @@ struct PredicateAnalysis : public ir::Visitor { internal_assert(node->args.size() == 1); Interval a = get(node->args[0]); if (a.is_bounded()) { - interval.min = select(a.min >= 0, a.min * a.min, - select(a.max <= 0, a.max * a.max, 0)); - interval.max = max(a.min * a.min, a.max * a.max); + interval.min = select(a.min >= 0, sqr(a.min), + select(a.max <= 0, sqr(a.max), 0)); + interval.max = max(sqr(a.min), sqr(a.max)); + } + return; + } + case ir::Intrinsic::max: { + internal_assert(node->args.size() == 2); + Interval a = get(node->args[0]); + Interval b = get(node->args[1]); + if (a.is_single_point(node->args[0]) && + b.is_single_point(node->args[1])) { + interval = Interval::single_point(node); + } else if (a.is_single_point() && b.is_single_point()) { + interval = Interval::single_point(max(a.min, b.min)); + } else { + if (a.has_lower_bound() && b.has_lower_bound()) { + interval.min = max(a.min, b.min); + } + if (a.has_upper_bound() && b.has_upper_bound()) { + interval.max = max(a.max, b.max); + } } return; } @@ -392,73 +747,50 @@ struct PredicateAnalysis : public ir::Visitor { const auto a_vol = bound(a_var->name); const auto b_vol = bound(b_var->name); - internal_assert(!a_vol.has_value() || a_vol->defined()) - << "LHS of geom op is varying but has no bounding volume: " - << ir::Expr(node); - internal_assert(!b_vol.has_value() || b_vol->defined()) - << "RHS of geom op is varying but has no bounding volume: " - << ir::Expr(node); - - const bool a_varying = a_vol.has_value(); - const bool b_varying = b_vol.has_value(); - - if (!a_varying && !b_varying) { - set(node); + // A varying operand with no bounding volume simply cannot be bounded. + // That is a legitimate outcome, not an error: the caller sees an + // undefined interval and emits an unguarded recursion. + const bool a_unbounded = a_vol.has_value() && !a_vol->defined(); + const bool b_unbounded = b_vol.has_value() && !b_vol->defined(); + if (a_unbounded || b_unbounded) { + interval = Interval{}; return; } - make_bool_bounds(); - - switch (node->op) { - case ir::GeomOp::intersects: { - ir::Expr a = a_varying ? *a_vol : node->a; - ir::Expr b = b_varying ? *b_vol : node->b; - - interval.max = ir::intersects(a, b); - - // TODO: handle lower bound? doesn't work for rays... - + // Neither operand varies, so the expression is its own value. + if (!a_vol.has_value() && !b_vol.has_value()) { + set(node); return; } - case ir::GeomOp::distmin: { - ir::Expr a = a_varying ? *a_vol : node->a; - ir::Expr b = b_varying ? *b_vol : node->b; - interval.min = ir::distmin(a, b); + const ir::Expr va = a_vol.has_value() ? *a_vol : ir::Expr(); + const ir::Expr vb = b_vol.has_value() ? *b_vol : ir::Expr(); - // TODO: handle upper bound? + if (is_geometric_metric(ir::GeomOp::intrinsic_name(node->op))) { + // Algorithm 6, lines 15-25. Both distances are bounded below by + // distmin and above by distmax over the bounding volumes. + const ir::Expr a = va.defined() ? va : node->a; + const ir::Expr b = vb.defined() ? vb : node->b; + interval.min = ir::distmin(a, b); interval.max = ir::distmax(a, b); - return; } - case ir::GeomOp::contains: { - if (!a_varying) { - // If a contains b's volume, a definitely contains b - interval.min = ir::contains(node->a, *b_vol); - // if a intersects b's volume, a could contain b. - interval.max = ir::intersects(node->a, *b_vol); - } else if (!b_varying) { - // If a's volume fully contains b, could be true - // otherwise, can't be true, because there is some space b - // exists that a does not. - interval.max = ir::contains(*a_vol, node->b); - // No way to prove this is always true if a is varying. - } else { - // Both varying! no way to prove always true. - // But can only be true if the volumes intersect/overlap in some - // way. - interval.max = ir::intersects(*a_vol, *b_vol); - } - return; - } - default: { - internal_error << "TODO: predicate analysis for: " - << ir::Expr(node); + + make_bool_bounds(); + if (ir::Expr upper = + geom_upper_bound(node->op, node->a, node->b, va, vb); + upper.defined()) { + interval.max = std::move(upper); } + if (ir::Expr lower = + geom_lower_bound(node->op, node->a, node->b, va, vb); + lower.defined()) { + interval.min = std::move(lower); } } RESTRICT_VISITOR(ir::SetOp); + RESTRICT_VISITOR(ir::AggOp); RESTRICT_VISITOR(ir::Call); RESTRICT_VISITOR(ir::Instantiate); RESTRICT_VISITOR(ir::PtrTo); @@ -471,8 +803,10 @@ struct PredicateAnalysis : public ir::Visitor { Interval predicate_analysis(const ir::Expr &expr, const VolumeMap &bounds, const IntervalMap &intervals) { PredicateAnalysis analysis(bounds, intervals); - expr.accept(&analysis); - return analysis.interval; + // Go through get() rather than accepting directly, so that a tagged + // interval on the expression itself is honoured. This matters for metrics + // like `|p| p.id`, where the whole expression is the annotated field. + return analysis.get(expr); } } // namespace lower diff --git a/src/Lower/Scans.cpp b/src/Lower/Scans.cpp index f91e6774a..b614a30a1 100644 --- a/src/Lower/Scans.cpp +++ b/src/Lower/Scans.cpp @@ -3,6 +3,7 @@ #include "IR/Analysis.h" #include "IR/Mutator.h" +#include "IR/Operators.h" #include "Error.h" #include "Utils.h" @@ -27,14 +28,41 @@ std::string scan_func_name(const std::vector &args) { return func_name; } -// TODO: support scan augmentations! -std::shared_ptr build_scan_func(const std::vector &args) { - std::string func_name = scan_func_name(args); +// The accumulate that combines a subtree's contribution into a reduction. +Accumulate::OpType accumulate_op_for(AggOp::OpType op) { + switch (op) { + case AggOp::sum: + return Accumulate::Add; + case AggOp::prod: + return Accumulate::Mul; + default: + internal_error << "Cannot scan a subtree with reduction: " + << to_string(op); + } +} + +// A scan traverses a whole subtree. By default it unions the elements into a +// set; when the scan carries a reduction it folds them into an accumulator +// instead, applying the scan's map function to each element first. +// Substitute an element into a unary lambda. +Expr apply_unary_lambda(const Expr &lambda_expr, const Expr &value) { + const Lambda *lambda = lambda_expr.as(); + internal_assert(lambda && lambda->args.size() == 1) + << "Scan map is not a unary lambda: " << lambda_expr; + return replace(lambda->args[0].name, value, lambda->value); +} + +std::shared_ptr +build_scan_func(const std::vector &args, const std::string &func_name, + const std::optional &op, const Expr &map_func) { std::vector f_args(args.size()); for (size_t i = 0, e = args.size(); i < e; i++) { f_args[i].name = args[i].name; f_args[i].type = args[i].type; - f_args[i].mutating = args[i].type.is(); + // The output -- a set to append to, or an accumulator to fold into -- + // is the last argument and is written through. + f_args[i].mutating = + args[i].type.is() || (op.has_value() && i + 1 == e); } std::shared_ptr func = std::make_shared( func_name, std::move(f_args), Void_t::make(), Stmt(), @@ -44,14 +72,60 @@ std::shared_ptr build_scan_func(const std::vector &args) { std::shared_ptr func; Expr write_expr; WriteLoc write_loc; + std::optional op; + Expr map_func; - ScansToCalls(std::shared_ptr _func) : func(std::move(_func)) { + ScansToCalls(std::shared_ptr func, + std::optional op, Expr map_func) + : func(std::move(func)), op(std::move(op)), + map_func(std::move(map_func)) { write_expr = Var::make(func->args.back().type, func->args.back().name); write_loc = WriteLoc(func->args.back().name, func->args.back().type); } + size_t counter = 0; + + // Contribute one element: append it to the output set, or fold it + // into the accumulator. + Stmt contribute(const Expr &value) { + if (!op.has_value()) { + return Append::make(write_loc, value); + } + Expr mapped = map_func.defined() + ? apply_unary_lambda(map_func, value) + : value; + return Accumulate::make(write_loc, accumulate_op_for(*op), + std::move(mapped)); + } + + // Appending an iterable adds all of its elements at once, but a + // reduction has to fold them in one at a time. + Stmt contribute_each(const Expr &values) { + if (!op.has_value()) { + return Append::make(write_loc, values); + } + // Summing the same constant over every element is that constant + // times how many there are, which the runtime answers directly + // rather than by iterating. + if (*op == AggOp::sum && map_func.defined()) { + const Lambda *lambda = map_func.as(); + internal_assert(lambda && lambda->args.size() == 1); + if (is_const(lambda->value)) { + Expr n = cast_to(write_loc.base_type, count(values)); + Expr total = is_const_one(lambda->value) + ? std::move(n) + : Expr(lambda->value) * std::move(n); + return Accumulate::make(write_loc, Accumulate::Add, + std::move(total)); + } + } + std::string name = "_elem" + std::to_string(counter++); + Expr element = Var::make(values.type().element_of(), name); + return ForEach::make(std::move(name), values, contribute(element)); + } + Stmt visit(const Scan *node) override { // return YieldFrom::make(node->value); @@ -82,22 +156,23 @@ std::shared_ptr build_scan_func(const std::vector &args) { } Stmt visit(const Iterate *node) override { - return Append::make(write_loc, node->value); + return contribute_each(node->value); } Stmt visit(const Yield *node) override { - return Append::make(write_loc, node->value); + return contribute(node->value); } RESTRICT_MUTATOR(Stmt, YieldFrom); }; // TODO: support product scans! - internal_assert(args.size() == 2); + internal_assert(args.size() == 2) + << "[unimplemented] scanning a product of trees"; const BVH_t *bvh_t0 = args.front().type.as(); internal_assert(bvh_t0); // write argument - internal_assert(args.back().type.is()); + internal_assert(op.has_value() || args.back().type.is()); Stmt match_body = build_base_scan(args.front().name, bvh_t0); // Need to rewrite scans in ^ to recursive calls. @@ -106,7 +181,7 @@ std::shared_ptr build_scan_func(const std::vector &args) { auto tree_args = args; tree_args.pop_back(); // lose write loc - func->body = ScansToCalls(func).mutate(match_body); + func->body = ScansToCalls(func, op, map_func).mutate(match_body); // func->body = RecLoop::make(std::move(tree_args), std::move(func->body)); return func; @@ -125,21 +200,63 @@ struct LowerScansImpl : public Mutator { return ir::Mutator::visit(node); } - Expr get_or_build_callable() { - std::vector scan_args; + // Scan functions built so far, so that two scans of the same trees with + // the same reduction and map share one function. + struct BuiltScan { + std::string base; + std::optional op; + Expr map_func; + std::string name; + }; + std::vector built; + + // The trees to traverse, and the location to write into. A set-union scan + // writes into the enclosing output set; a reducing scan writes into its + // own accumulator. + std::pair, TypedVar> + scan_arguments(const Scan *node) const { + std::vector trees; + for (const auto &arg : args) { + if (arg.type.is()) { + trees.push_back(arg); + } + } + if (node->op.has_value()) { + return {std::move(trees), + TypedVar{node->loc.base, node->loc.base_type}}; + } for (const auto &arg : args) { - if (arg.type.is() || arg.type.is()) { - scan_args.push_back(arg); + if (arg.type.is()) { + return {std::move(trees), arg}; } } - std::string name = scan_func_name(scan_args); - if (const auto &iter = new_funcs.find(name); iter != new_funcs.cend()) { - return Var::make(iter->second->call_type(), iter->second->name); + internal_error << "No output for scan: " << Stmt(node); + } + + Expr get_or_build_callable(const Scan *node, + const std::vector &scan_args) { + const std::string base = scan_func_name(scan_args); + for (const auto &b : built) { + const bool same_map = + b.map_func.defined() == node->func.defined() && + (!b.map_func.defined() || equals(b.map_func, node->func)); + if (b.base == base && b.op == node->op && same_map) { + const auto &f = new_funcs.at(b.name); + return Var::make(f->call_type(), f->name); + } } // Need to build this func. - auto func = build_scan_func(scan_args); + std::string name = base; + if (node->op.has_value()) { + name += "_" + to_string(*node->op); + } + if (new_funcs.contains(name)) { + name += "_" + std::to_string(built.size()); + } + auto func = build_scan_func(scan_args, name, node->op, node->func); Expr ret = Var::make(func->call_type(), func->name); new_funcs[name] = std::move(func); + built.push_back({base, node->op, node->func, std::move(name)}); return ret; } @@ -177,9 +294,11 @@ struct LowerScansImpl : public Mutator { internal_assert(!args.empty() && args.front().type.is()) << args.front(); - std::string bvh_name = args.front().type.as()->name; - Expr callable = get_or_build_callable(); + auto [trees, output] = scan_arguments(node); + std::vector scan_args = trees; + scan_args.push_back(output); + Expr callable = get_or_build_callable(node, scan_args); // Make n scan calls. for (const auto &id : ids) { @@ -192,7 +311,7 @@ struct LowerScansImpl : public Mutator { } else { call_args.push_back(id); } - call_args.push_back(args.back()); + call_args.push_back(Var::make(output.type, output.name)); stmts.push_back(CallStmt::make(callable, std::move(call_args))); } return Sequence::make(std::move(stmts)); diff --git a/src/Lower/Trees.cpp b/src/Lower/Trees.cpp index d559807f9..27537ac95 100644 --- a/src/Lower/Trees.cpp +++ b/src/Lower/Trees.cpp @@ -13,6 +13,7 @@ #include "Opt/Simplify.h" #include +#include #include #include #include @@ -68,6 +69,26 @@ analyze_node(const ir::BVH_t::Node &node, const ir::Type &prim_t) { return {data, children}; } +// Key under which a node's precomputed aggregate is recorded, e.g. "count()" +// or "min(id)". Must agree between the annotation and the query operator +// looking the aggregate up. +std::string aggregate_key(const ir::Annotation::Aggregate &agg) { + std::string key = to_string(agg.op) + "("; + for (size_t i = 0; i < agg.args.size(); i++) { + if (i != 0) { + key += ", "; + } + key += agg.args[i]; + } + key += ")"; + return key; +} + +std::string aggregate_key(ir::Annotation::Aggregate::OpType op, + const std::vector &args) { + return aggregate_key(ir::Annotation::Aggregate{op, args, ""}); +} + struct RewriteYields : public ir::Mutator { std::function f; RewriteYields(std::function f) @@ -76,6 +97,29 @@ struct RewriteYields : public ir::Mutator { ir::Stmt visit(const ir::Yield *node) override { return f(node->value); } }; +// Substitute a yielded element into a lambda over the set's elements. A +// multi-argument lambda comes from coiterating several sets, so the element is +// a tuple to be destructured componentwise. +ir::Expr apply_lambda(const ir::Expr &func, const ir::Expr &value) { + const ir::Lambda *lambda = func.as(); + internal_assert(lambda) << "Not a lambda: " << func; + if (lambda->args.size() == 1) { + internal_assert(ir::equals(lambda->args[0].type, value.type())) + << lambda->args[0].type << " versus " << value.type(); + return replace(lambda->args[0].name, value, lambda->value); + } + internal_assert(value.type().is()) << value; + std::map repls; + for (size_t i = 0; i < lambda->args.size(); i++) { + // TODO: this needs to simplify or have CSE for it to be efficient! + ir::Expr component = opt::Simplify::simplify( + ir::Extract::make(value, static_cast(i))); + internal_assert(ir::equals(component.type(), lambda->args[i].type)); + repls[lambda->args[i].name] = std::move(component); + } + return replace(repls, lambda->value); +} + ir::Expr make_tuple_pair(ir::Expr a, ir::Expr b) { ir::Type tuple_t = ir::Tuple_t::make({a.type(), b.type()}); std::vector values = {std::move(a), std::move(b)}; @@ -106,11 +150,19 @@ ir::Stmt lower_iterate(const ir::Expr &expr) { struct Rewriter : public ir::Mutator { // The list of volumes for the currently match arms. std::vector volumes; + // Volumes annotated on a node's children rather than on the node itself. + // Keyed by the matched tree variable, then by child field name. + std::map> child_volumes; // The list of tagged intervals. Holds scalar interval OR map of field // intervals. std::vector< std::variant>> intervals; + // Reduction augmentations for the current match arms: the field holding a + // precomputed aggregate over the subtree, keyed by the aggregate it + // stores. + // TODO: key is a string concat of the aggregation request, not ideal... + std::vector> aggregations; // The list of nodes for the current matches. std::vector locs; @@ -134,8 +186,8 @@ struct Rewriter : public ir::Mutator { return Interval{std::move(low_expr), std::move(high_expr)}; }; - if (bvh_node.has_volume()) { - const auto &volume = bvh_node.get_volume(); + const auto make_volume = + [&](const ir::Annotation::Volume *volume) -> ir::Expr { const auto &inits = volume->initializers; const size_t n_args = inits.size(); std::vector args(n_args); @@ -143,14 +195,19 @@ struct Rewriter : public ir::Mutator { const auto &name = inits[j]; args[j] = ir::Access::make(name, tree); } - ir::Expr vol = ir::Build::make(volume->struct_type, args); - volumes.emplace_back(std::move(vol)); + return ir::Build::make(volume->struct_type, args); + }; + + if (bvh_node.has_volume()) { + volumes.emplace_back(make_volume(bvh_node.get_volume())); } else { volumes.emplace_back(); // undef volume } std::variant> interval; + std::map aggregation; + std::map built_child_volumes; for (const auto &annot : node->arms[i].first.annotations) { if (const auto *a_interval = annot.as()) { @@ -174,13 +231,32 @@ struct Rewriter : public ir::Mutator { (*as_map)[a_interval->scalar] = m_interval; } } + } else if (const auto *agg = + annot.as()) { + aggregation[aggregate_key(*agg)] = + ir::Access::make(agg->value, tree); + } else if (const auto *vol = + annot.as()) { + if (vol->geometry.empty()) { + continue; // the node's own volume, handled above + } + // Tagged on a child. + built_child_volumes[vol->geometry] = make_volume(vol); } } intervals.emplace_back(std::move(interval)); + aggregations.emplace_back(std::move(aggregation)); + if (!built_child_volumes.empty()) { + child_volumes[var->name] = built_child_volumes; + } ir::Stmt stmt = mutate(node->arms[i].second); volumes.pop_back(); intervals.pop_back(); + aggregations.pop_back(); + if (!built_child_volumes.empty()) { + child_volumes.erase(var->name); + } new_arms[i] = {node->arms[i].first, std::move(stmt)}; } locs.pop_back(); @@ -202,6 +278,37 @@ struct Rewriter : public ir::Mutator { return vols; } + // Build a volume map from the child volumes attached to `value`, for the + // case where a node annotates its children's bounds rather than its own. + VolumeMap make_volume_map(const std::vector &args, + const ir::Expr &value) const { + const auto get_volume = [&](const ir::Expr &e) { + const ir::Access *access = e.as(); + internal_assert(access) << e; + const ir::Unwrap *unwrap = access->value.as(); + internal_assert(unwrap) << e; + const ir::Var *var = unwrap->value.as(); + internal_assert(var) << e; + const auto &iter = child_volumes.find(var->name); + internal_assert(iter != child_volumes.cend()) << e; + const auto &citer = iter->second.find(access->field); + internal_assert(citer != iter->second.cend()) << e; + return citer->second; + }; + VolumeMap vols; + if (args.size() == 1) { + vols[args[0].name] = get_volume(value); + } else { + const auto values = break_tuple(value); + internal_assert(args.size() == values.size()) << value; + for (size_t i = 0; i < args.size(); i++) { + // TODO(ajr): this doesn't work with mismatching BVHs. + vols[args[i].name] = get_volume(values[i]); + } + } + return vols; + } + IntervalMap make_interval_map(const std::vector &args, const IntervalMap &existing) const { IntervalMap ints = existing; @@ -305,9 +412,6 @@ ir::Stmt build_filter(ir::Stmt body, ir::Expr predicate, IntervalMap ints = make_interval_map(lambda->args, intervals); Interval bounds = predicate_analysis(lambda->value, vols, ints); - internal_assert(bounds.max.defined()) - << "Cannot accelerate predicate: " << predicate - << " on: " << ir::Stmt(node); // Make a recursive call // TODO: this should be wrapped in a filter, for cases with @@ -315,8 +419,11 @@ ir::Stmt build_filter(ir::Stmt body, ir::Expr predicate, // analysis of conjunctions/disjunctions. ir::Stmt body = // ir::YieldFrom::make(ir::filter(predicate, node->value)); ir::Stmt body = ir::YieldFrom::make(node->value); - // Add the maybe case -> recursive call - body = ir::IfElse::make(std::move(bounds.max), std::move(body)); + // Add the maybe case -> recursive call. A bound that is trivially + // true prunes nothing, so skip the guard entirely. + if (bounds.max.defined() && !is_const_one(bounds.max)) { + body = ir::IfElse::make(std::move(bounds.max), std::move(body)); + } // Check for always case if (bounds.min.defined() && !is_const_zero(bounds.min)) { @@ -338,8 +445,63 @@ ir::Stmt build_filter(ir::Stmt body, ir::Expr predicate, return RewriteFilter(std::move(predicate), intervals).mutate(body); } -ir::Expr try_fuse_filter(const ir::Lambda *metric, ir::Expr best, - ir::Expr maybe_filter) { +// Which end of the metric an extremum operator seeks. +enum class Extremum { Min, Max }; + +const char *name_of(Extremum dir, bool arg) { + if (arg) { + return dir == Extremum::Min ? "argmin" : "argmax"; + } + return dir == Extremum::Min ? "minimum" : "maximum"; +} + +// The identity of the extremum: the worst possible value, so that any element +// improves on it. Note that for signed integers this is -INT_MAX rather than +// INT_MIN, matching how Extrema::inf already approximates infinity there. +ir::Expr extremum_identity(const ir::Type &t, Extremum dir) { + ir::Expr inf = ir::Extrema::make(t, ir::Extrema::inf); + if (dir == Extremum::Min) { + return inf; + } + return t.is_uint() ? make_zero(t) : -inf; +} + +// `value` beats `best`. +ir::Expr improves_on(Extremum dir, ir::Expr value, ir::Expr best) { + return dir == Extremum::Min ? (std::move(value) < std::move(best)) + : (std::move(best) < std::move(value)); +} + +// What predicate analysis can say about the best value reachable in a +// subtree: for a minimum that is the metric's upper bound over the subtree, +// and for a maximum its lower bound. +ir::Expr reachable_bound(Extremum dir, const Interval &bounds) { + return dir == Extremum::Min ? bounds.max : bounds.min; +} + +// The bound that decides whether a subtree is worth visiting: a minimum has +// to descend when the subtree's smallest reachable value beats the running +// best, which is the metric's lower bound over the subtree. +ir::Expr promising_bound(Extremum dir, const Interval &bounds) { + return dir == Extremum::Min ? bounds.min : bounds.max; +} + +// The accumulator's own interval. A running minimum is an upper bound on any +// value that can still be accepted, and vice versa. +Interval accumulator_interval(Extremum dir, const ir::Expr &acc) { + return dir == Extremum::Min ? Interval{ir::Expr(), acc} + : Interval{acc, ir::Expr()}; +} + +// Fuse "this element improves on the running best" into an existing filter if +// there is one, so that the value-based pruning condition participates in +// predicate analysis. The bool reports whether an existing filter was fused +// into; when false the caller must still update the accumulator from +// recursive calls itself. +std::pair try_fuse_filter(Extremum dir, + const ir::Lambda *metric, + ir::Expr best, + ir::Expr maybe_filter) { if (const ir::SetOp *as_set = maybe_filter.as()) { if (as_set->op == ir::SetOp::filter) { // Can fuse! @@ -356,49 +518,50 @@ ir::Expr try_fuse_filter(const ir::Lambda *metric, ir::Expr best, } internal_assert( equals(metric->args[i].type, predicate->args[i].type)) - << "Mismatched types in argmin-filter fusion: " + << "Mismatched types in metric-filter fusion: " << metric->args[i].type << " != " << predicate->args[i].type; } // Check for convenient case of same naming / types. + ir::Expr value = + repls.empty() ? metric->value : replace(repls, metric->value); ir::Expr new_cond = - repls.empty() ? (predicate->value && (metric->value < best)) - : predicate->value && - (replace(repls, metric->value) < best); + predicate->value && improves_on(dir, std::move(value), best); // Construct fused filter. ir::Expr new_lambda = ir::Lambda::make(predicate->args, std::move(new_cond)); - return filter(std::move(new_lambda), as_set->b); + return {filter(std::move(new_lambda), as_set->b), true}; } } // Not a nested filter, so just wrap in a filter and return - ir::Expr new_cond = (metric->value < best); + ir::Expr new_cond = improves_on(dir, metric->value, std::move(best)); ir::Expr new_lambda = ir::Lambda::make(metric->args, std::move(new_cond)); - return filter(std::move(new_lambda), std::move(maybe_filter)); + return {filter(std::move(new_lambda), std::move(maybe_filter)), false}; } -ir::Stmt build_argmin(ir::Expr metric, ir::Expr inner, - const ir::TypeMap &tree_types, - const IntervalMap &intervals, ir::Type expect_type) { - struct RewriteArgmin : public Rewriter { +// Algorithm 3: argmin and argmax. These mirror Algorithm 2 but also track the +// element achieving the extremum, so the accumulator is a (metric, element) +// pair updated with an argmin/argmax accumulate. +ir::Stmt build_arg_extremum(Extremum dir, ir::Expr metric, ir::Expr inner, + const ir::TypeMap &tree_types, + const IntervalMap &intervals, + ir::Type expect_type) { + struct RewriteArgExtremum : public Rewriter { + Extremum dir; ir::Expr metric; ir::WriteLoc loc; ir::Type tuple_t; - RewriteArgmin(ir::Expr met, ir::WriteLoc l, ir::Type t) - : metric(std::move(met)), loc(std::move(l)), tuple_t(std::move(t)) { - } - - size_t counter = 0; - - std::string make_temp_name() { - return loc.base + "_temp" + std::to_string(counter++); - } + RewriteArgExtremum(Extremum dir, ir::Expr met, ir::WriteLoc l, + ir::Type t) + : dir(dir), metric(std::move(met)), loc(std::move(l)), + tuple_t(std::move(t)) {} using ir::Mutator::visit; + // yield x => upd a arg(a, (M(x), x)) ir::Stmt visit(const ir::Yield *node) override { internal_assert(!volumes.empty()); const ir::Lambda *lambda = metric.as(); @@ -413,7 +576,10 @@ ir::Stmt build_argmin(ir::Expr metric, ir::Expr inner, std::vector values = {std::move(value), node->value}; ir::Expr update = ir::Build::make(tuple_t, std::move(values)); - return ir::Accumulate::make(loc, ir::Accumulate::Argmin, + return ir::Accumulate::make(loc, + dir == Extremum::Min + ? ir::Accumulate::Argmin + : ir::Accumulate::Argmax, std::move(update)); } @@ -428,7 +594,8 @@ ir::Stmt build_argmin(ir::Expr metric, ir::Expr inner, }; const ir::Lambda *lambda = metric.as(); - internal_assert(lambda) << "Metric is not a lambda: " << metric; + internal_assert(lambda) + << "Metric of " << name_of(dir, true) << " is not a lambda: " << metric; ir::Type metric_t = lambda->value.type(); ir::Type ret_type = inner.type().element_of(); @@ -438,42 +605,599 @@ ir::Stmt build_argmin(ir::Expr metric, ir::Expr inner, std::string name = "_best" + std::to_string(counter++); ir::WriteLoc loc(name, tuple_t); - ir::Expr inf = ir::Infinity::make(std::move(metric_t)); + // WrapWithAccumulator(a, (worst, null)) + ir::Expr identity = extremum_identity(metric_t, dir); static const std::vector empty_list = {}; ir::Expr empty = ir::Build::make(ret_type, empty_list); - std::vector values = {inf, std::move(empty)}; + std::vector values = {identity, std::move(empty)}; ir::Expr init = ir::Build::make(tuple_t, std::move(values)); // TODO(ajr): is stack memory ok here? it's not an array. ir::Stmt header = ir::Allocate::make(loc, std::move(init), ir::Allocate::Memory::Stack); - // Make return ir::Expr ret_var = ir::Var::make(tuple_t, std::move(name)); ir::Expr best_metric = ir::Extract::make(ret_var, 0); ir::Expr best_ref = ir::Extract::make(ret_var, 1); // TODO: should this be a Return? ir::Stmt footer; if (!ir::equals(ret_type, expect_type)) { - // If (best[0] != inf) yield best[1] else {} + // If nothing improved on the identity, the set was empty. ir::Expr result = ir::Select::make( - best_metric != inf, ir::Build::make(expect_type, {best_ref}), + best_metric != identity, ir::Build::make(expect_type, {best_ref}), ir::Build::make(expect_type)); footer = ir::Yield::make(std::move(result)); } else { - footer = ir::Yield::make(std::move(best_ref)); + footer = ir::Yield::make(best_ref); } - // No lower bound (can always get better) - // Upper bound is the current value (must be at least that good). IntervalMap local_intervals = intervals; - local_intervals[best_metric] = Interval{ir::Expr(), best_metric}; + local_intervals[best_metric] = accumulator_interval(dir, best_metric); // Try to build fused filter inside. - ir::Expr fused_filter = try_fuse_filter(lambda, best_metric, inner); + auto [fused_filter, fused] = + try_fuse_filter(dir, lambda, best_metric, inner); ir::Stmt body = build_traversal(fused_filter, tree_types, local_intervals); - body = RewriteArgmin(std::move(metric), std::move(loc), std::move(tuple_t)) + body = RewriteArgExtremum(dir, std::move(metric), std::move(loc), + std::move(tuple_t)) + .mutate(body); + + return ir::Sequence::make( + {std::move(header), std::move(body), std::move(footer)}); +} + +// Algorithm 2 (LowerMin), and its mirror for maxima. These reductions are both +// associative and idempotent, so the traversal prunes in two complementary +// ways: value-based pruning, where a subtree whose metric cannot beat the +// running extremum is skipped, and inclusion, where a subtree that stores its +// own extremum updates the accumulator without being visited. +// +// The value-based half is expressed by fusing "improves on best" into the +// inner filter (try_fuse_filter) and letting predicate analysis bound it; the +// accumulator's interval is recorded so that analysis knows a candidate must +// beat the running value. +ir::Stmt build_extremum(Extremum dir, ir::Expr metric, ir::Expr inner, + const ir::TypeMap &tree_types, + const IntervalMap &intervals, ir::Type expect_type) { + struct RewriteExtremum : public Rewriter { + Extremum dir; + ir::Expr metric; + ir::WriteLoc loc; + const IntervalMap &intervals; + // The augmentation storing this extremum over a subtree, if any. + std::optional key; + // True when try_fuse_filter did not fuse, so recursive calls are not + // already guarded by the value-based condition and this rewrite must + // tighten the accumulator itself. + const bool update_from_yfs; + + RewriteExtremum(Extremum dir, ir::Expr met, ir::WriteLoc l, + const IntervalMap &intervals, + std::optional key, + const bool update_from_yfs) + : dir(dir), metric(std::move(met)), loc(std::move(l)), + intervals(intervals), key(std::move(key)), + update_from_yfs(update_from_yfs) {} + + ir::Accumulate::OpType accumulate_op() const { + return dir == Extremum::Min ? ir::Accumulate::Min + : ir::Accumulate::Max; + } + + using ir::Mutator::visit; + + // yield x => upd a minb(a, M(x)) + ir::Stmt visit(const ir::Yield *node) override { + internal_assert(!volumes.empty()); + const ir::Lambda *lambda = metric.as(); + internal_assert(lambda) << "Metric is not a lambda: " << metric; + internal_assert(volumes.size() == lambda->args.size()); + // TODO: handle tuple data, e.g. from product() + internal_assert(lambda->args.size() == 1); + internal_assert( + ir::equals(lambda->args[0].type, node->value.type())); + ir::Expr value = + replace(lambda->args[0].name, node->value, lambda->value); + return ir::Accumulate::make(loc, accumulate_op(), std::move(value)); + } + + // iter xs => upd a minb(a, min(M, xs)) + ir::Stmt visit(const ir::Iterate *node) override { + return mutate( + lower_iterate(node->value)); // lower into a concrete loop. + } + + // scan tr => if tr has min(M, tr) then upd a minb(a, min(M, tr)) + // else if maybe(min(M(tr)) < a): upd a minb(a, max(M, tr)); + // from tr + // + // A whole subtree is included here, so a node that stores this + // extremum over it settles the contribution exactly and does not need + // to be visited at all. That is the inclusion case of Section 2. + ir::Stmt visit(const ir::Scan *node) override { + const bool stored = + key.has_value() && + std::all_of( + aggregations.begin(), aggregations.end(), + [&](const auto &agg) { return agg.contains(*key); }); + if (stored) { + std::vector stmts; + for (const auto &agg : aggregations) { + stmts.push_back(ir::Accumulate::make(loc, accumulate_op(), + agg.at(*key))); + } + return stmts.size() == 1 ? stmts.front() + : ir::Sequence::make(std::move(stmts)); + } + + // Nothing stored, so the subtree has to be visited. Value-based + // pruning still applies: skip it when it cannot beat the running + // best, and tighten the accumulator with what it could reach. + Interval bounds = subtree_bounds(); + std::vector stmts; + if (ir::Expr reachable = reachable_bound(dir, bounds); + reachable.defined()) { + stmts.push_back(ir::Accumulate::make(loc, accumulate_op(), + std::move(reachable))); + } + stmts.push_back(ir::YieldFrom::make(node->value)); + ir::Stmt body = stmts.size() == 1 + ? std::move(stmts.front()) + : ir::Sequence::make(std::move(stmts)); + if (ir::Expr promising = promising_bound(dir, bounds); + promising.defined()) { + body = ir::IfElse::make( + improves_on(dir, std::move(promising), loc.to_expr()), + std::move(body)); + } + return body; + } + + // The bounds of the metric over the subtree currently being matched. + Interval subtree_bounds() const { + const ir::Lambda *lambda = metric.as(); + internal_assert(lambda) << "Metric is not a lambda: " << metric; + internal_assert(volumes.size() == lambda->args.size()); + VolumeMap vols = make_volume_map(lambda->args); + IntervalMap ints = make_interval_map(lambda->args, intervals); + return predicate_analysis(lambda->value, vols, ints); + } + + // from tr => upd a minb(a, max(M, tr)); from tr + ir::Stmt visit(const ir::YieldFrom *node) override { + if (!update_from_yfs) { + return node; + } + const ir::Lambda *lambda = metric.as(); + internal_assert(lambda) << "Metric is not a lambda: " << metric; + internal_assert(volumes.size() == lambda->args.size()); + // TODO: handle tuple data, e.g. from product() + internal_assert(lambda->args.size() == 1); + + ir::Expr bound = reachable_bound(dir, subtree_bounds()); + if (!bound.defined()) { + // Nothing can be said about this subtree; just recurse. + return node; + } + + // The best value in this subtree is no better than the metric's + // bound over the subtree's volume, so the accumulator can be + // tightened before recursing. + ir::Stmt do_update = + ir::Accumulate::make(loc, accumulate_op(), std::move(bound)); + return ir::Sequence::make({std::move(do_update), node}); + } + }; + + const ir::Lambda *lambda = metric.as(); + internal_assert(lambda) << "Metric of " << name_of(dir, false) + << " is not a lambda: " << metric; + ir::Type metric_t = lambda->value.type(); + + static size_t counter = 0; + std::string name = "_best" + std::to_string(counter++); + ir::WriteLoc loc(name, metric_t); + + // WrapWithAccumulator(a, worst) + ir::Expr identity = extremum_identity(metric_t, dir); + // TODO(ajr): is stack memory ok here? it's not an array. + ir::Stmt header = + ir::Allocate::make(loc, identity, ir::Allocate::Memory::Stack); + + ir::Expr ret_var = ir::Var::make(metric_t, std::move(name)); + // An extremum over a set that can be empty is optional: the accumulator is + // still at its identity exactly when nothing was visited. + ir::Stmt footer; + if (!ir::equals(metric_t, expect_type)) { + ir::Expr result = ir::Select::make( + ret_var != identity, ir::Build::make(expect_type, {ret_var}), + ir::Build::make(expect_type)); + footer = ir::Yield::make(std::move(result)); + } else { + footer = ir::Yield::make(ret_var); + } + + IntervalMap local_intervals = intervals; + local_intervals[ret_var] = accumulator_interval(dir, ret_var); + + // An included subtree can be folded in wholesale when the node stores this + // extremum over the field the metric reads. + std::optional key; + if (const ir::Access *access = lambda->value.as()) { + if (const ir::Var *var = access->value.as(); + var != nullptr && var->name == lambda->args[0].name) { + key = aggregate_key(dir == Extremum::Min + ? ir::Annotation::Aggregate::min + : ir::Annotation::Aggregate::max, + {access->field}); + } + } + + // Fusing the value-based condition into a filter is what lets predicate + // analysis prune on it, and it also puts the node-level bound in front of + // every arm, including the leaves. Synthesizing that filter consumes the + // `scan` standing for a wholly included subtree, so give it up only when + // this metric could actually read a stored extremum off a node -- that + // is, when the metric names a field an augmentation might cover. + auto [fused_filter, fused] = try_fuse_filter(dir, lambda, ret_var, inner); + const bool keep_scan = !fused && key.has_value(); + ir::Stmt body = build_traversal(keep_scan ? inner : fused_filter, + tree_types, local_intervals); + + body = RewriteExtremum(dir, std::move(metric), std::move(loc), intervals, + std::move(key), !fused) + .mutate(body); + + return ir::Sequence::make( + {std::move(header), std::move(body), std::move(footer)}); +} + +// Algorithm 4: any and all. Both are idempotent reductions over the boolean +// lattice, so a subtree can be skipped as soon as the answer it could +// contribute is settled: for `any` once the predicate is proven always true +// (the result is true) or never true (the subtree cannot help), and dually for +// `all`. +ir::Stmt build_quantifier(bool is_any, ir::Expr predicate, ir::Expr inner, + const ir::TypeMap &tree_types, + const IntervalMap &intervals) { + struct RewriteQuantifier : public Rewriter { + bool is_any; + ir::Expr predicate; + ir::WriteLoc loc; + const IntervalMap &intervals; + + RewriteQuantifier(bool is_any, ir::Expr p, ir::WriteLoc l, + const IntervalMap &intervals) + : is_any(is_any), predicate(std::move(p)), loc(std::move(l)), + intervals(intervals) {} + + using ir::Mutator::visit; + + // `a` is settled once it reaches the absorbing element of the lattice. + ir::Expr still_undecided() const { + ir::Expr acc = loc.to_expr(); + return is_any ? ~acc : acc; + } + + // yield x => upd a (a | P(x)) + ir::Stmt visit(const ir::Yield *node) override { + const ir::Lambda *lambda = predicate.as(); + internal_assert(lambda) + << "Predicate is not a lambda: " << predicate; + ir::Expr p = apply_lambda(predicate, node->value); + ir::Expr acc = loc.to_expr(); + ir::Expr combined = is_any ? (acc || p) : (acc && p); + return ir::Store::make(loc, std::move(combined)); + } + + ir::Stmt visit(const ir::Iterate *node) override { + return mutate( + lower_iterate(node->value)); // lower into a concrete loop. + } + + // The bounds of the predicate over the subtree currently being matched. + Interval subtree_bounds() const { + const ir::Lambda *lambda = predicate.as(); + internal_assert(lambda) + << "Predicate is not a lambda: " << predicate; + internal_assert(volumes.size() == lambda->args.size()); + VolumeMap vols = make_volume_map(lambda->args); + IntervalMap ints = make_interval_map(lambda->args, intervals); + return predicate_analysis(lambda->value, vols, ints); + } + + // scan tr => if always(P, tr): a = + // elif && maybe(P, tr): from tr + ir::Stmt visit(const ir::Scan *node) override { + Interval bounds = subtree_bounds(); + ir::Stmt recurse = ir::YieldFrom::make(node->value); + + // For `any`, a subtree the predicate can never hold on contributes + // nothing; for `all` it settles the answer to false. + ir::Stmt otherwise; + if (!is_any) { + otherwise = ir::Store::make(loc, ir::BoolImm::make(false)); + } + + if (bounds.max.defined() && !is_const_one(bounds.max)) { + recurse = + ir::IfElse::make(still_undecided() && bounds.max, + std::move(recurse), std::move(otherwise)); + } else if (otherwise.defined()) { + recurse = + ir::IfElse::make(still_undecided(), std::move(recurse)); + } else { + recurse = + ir::IfElse::make(still_undecided(), std::move(recurse)); + } + + if (bounds.min.defined() && !is_const_zero(bounds.min)) { + // Proven on the whole subtree: `any` is decided, `all` learns + // nothing new and can skip it. + ir::Stmt settled = + is_any ? ir::Store::make(loc, ir::BoolImm::make(true)) + : ir::Stmt(); + recurse = + settled.defined() + ? ir::IfElse::make(bounds.min, settled, + std::move(recurse)) + : ir::IfElse::make(~bounds.min, std::move(recurse)); + } + return recurse; + } + + // from tr => if && maybe(P, tr): from tr + ir::Stmt visit(const ir::YieldFrom *node) override { + Interval bounds = subtree_bounds(); + ir::Expr cond = still_undecided(); + if (bounds.max.defined() && !is_const_one(bounds.max)) { + cond = cond && bounds.max; + } + return ir::IfElse::make(std::move(cond), node); + } + }; + + const ir::Type bool_t = ir::Bool_t::make(); + + static size_t counter = 0; + std::string name = "_holds" + std::to_string(counter++); + ir::WriteLoc loc(name, bool_t); + + // WrapWithAccumulator(a, false) for any, (a, true) for all. + ir::Stmt header = ir::Allocate::make(loc, ir::BoolImm::make(!is_any), + ir::Allocate::Memory::Stack); + ir::Expr ret_var = ir::Var::make(bool_t, std::move(name)); + ir::Stmt footer = ir::Yield::make(ret_var); + + ir::Stmt body = build_traversal(inner, tree_types, intervals); + body = RewriteQuantifier(is_any, std::move(predicate), std::move(loc), + intervals) + .mutate(body); + + return ir::Sequence::make( + {std::move(header), std::move(body), std::move(footer)}); +} + +// Algorithm 1, lines 9-12. A map changes only what a traversal yields, so it +// rewrites into the yield, iter and scan constructs without affecting how +// recursion proceeds. +ir::Stmt build_map(ir::Stmt body, ir::Expr func) { + struct RewriteMap : public Rewriter { + ir::Expr func; + + RewriteMap(ir::Expr f) : func(std::move(f)) {} + + using ir::Mutator::visit; + + // yield x => yield F(x) + ir::Stmt visit(const ir::Yield *node) override { + return ir::Yield::make(apply_lambda(func, node->value)); + } + + // iter xs => iter map(F, xs) + ir::Stmt visit(const ir::Iterate *node) override { + return ir::Iterate::make(map(func, node->value)); + } + + // scan tr => scan F(tr) + ir::Stmt visit(const ir::Scan *node) override { + internal_assert(!node->func.defined()) + << "TODO: compose nested maps on a scan: " << ir::Stmt(node); + return ir::Scan::make(node->op, node->loc, func, node->value); + } + + // A recursive call already evaluates the mapped query. + ir::Stmt visit(const ir::YieldFrom *node) override { return node; } + }; + + return RewriteMap(std::move(func)).mutate(body); +} + +// The augmentation, if any, that stores this reduction's value over a +// subtree. A reduction is recognised by its combiner together with the map +// feeding it: summing a constant 1 is a `count()`, summing a field `f` is a +// `sum(f)`, and so on. +std::optional aggregate_key_for(const ir::Expr &combiner, + const ir::Expr &func) { + const ir::Lambda *lambda = combiner.as(); + if (lambda == nullptr || lambda->args.size() != 2) { + return {}; + } + const ir::BinOp *binop = lambda->value.as(); + if (binop == nullptr) { + return {}; + } + // The combiner must be exactly `|a, b| a b`. + const ir::Var *a = binop->a.as(); + const ir::Var *b = binop->b.as(); + if (a == nullptr || b == nullptr || a->name != lambda->args[0].name || + b->name != lambda->args[1].name) { + return {}; + } + + ir::Annotation::Aggregate::OpType op; + switch (binop->op) { + case ir::BinOp::Add: + op = ir::Annotation::Aggregate::sum; + break; + case ir::BinOp::Mul: + op = ir::Annotation::Aggregate::prod; + break; + default: + return {}; + } + + if (!func.defined()) { + // Reducing the elements themselves. + return aggregate_key(op, {}); + } + const ir::Lambda *map_fn = func.as(); + if (map_fn == nullptr || map_fn->args.size() != 1) { + return {}; + } + // Summing a constant 1 over the subtree counts it. + if (op == ir::Annotation::Aggregate::sum && is_const_one(map_fn->value)) { + return aggregate_key(ir::Annotation::Aggregate::count, {}); + } + // Reducing a single field. + if (const ir::Access *access = map_fn->value.as()) { + if (const ir::Var *var = access->value.as(); + var != nullptr && var->name == map_fn->args[0].name) { + return aggregate_key(op, {access->field}); + } + } + return {}; +} + +// `upd a (a (+) v)`. Emits an Accumulate for the combiners the backends know +// how to update in place, and otherwise inlines the combiner into a store. +ir::Stmt make_update(const ir::WriteLoc &loc, const ir::Expr &combiner, + ir::Expr value) { + const ir::Lambda *lambda = combiner.as(); + internal_assert(lambda && lambda->args.size() == 2) + << "Combiner is not a binary lambda: " << combiner; + + if (const ir::BinOp *binop = lambda->value.as()) { + const ir::Var *a = binop->a.as(); + const ir::Var *b = binop->b.as(); + if (a != nullptr && b != nullptr && a->name == lambda->args[0].name && + b->name == lambda->args[1].name) { + switch (binop->op) { + case ir::BinOp::Add: + return ir::Accumulate::make(loc, ir::Accumulate::Add, + std::move(value)); + case ir::BinOp::Mul: + return ir::Accumulate::make(loc, ir::Accumulate::Mul, + std::move(value)); + default: + break; + } + } + } + + std::map repls; + repls[lambda->args[0].name] = loc.to_expr(); + repls[lambda->args[1].name] = std::move(value); + return ir::Store::make(loc, replace(repls, lambda->value)); +} + +// Algorithm 1, lines 13-22. An associative reduction is computed into an +// accumulator: leaves update it with their own value, and a subtree that +// stores a precomputed aggregate updates it wholesale instead of being +// traversed. +ir::Stmt build_reduce(ir::Expr identity, ir::Expr combiner, ir::Expr inner, + const ir::TypeMap &tree_types, + const IntervalMap &intervals) { + struct RewriteReduce : public Rewriter { + ir::Expr combiner; + ir::WriteLoc loc; + // The augmentation this reduction can read off a node, if any. + std::optional key; + // The reduction, and the map feeding it, for a fallback scan. + std::optional scan_op; + ir::Expr func; + + RewriteReduce(ir::Expr c, ir::WriteLoc l, + std::optional key, + std::optional scan_op, ir::Expr func) + : combiner(std::move(c)), loc(std::move(l)), key(std::move(key)), + scan_op(std::move(scan_op)), func(std::move(func)) {} + + using ir::Mutator::visit; + + // yield x => upd a (a (+) x) + ir::Stmt visit(const ir::Yield *node) override { + return make_update(loc, combiner, node->value); + } + + // iter xs => upd a (a (+) reduce(xs)) + ir::Stmt visit(const ir::Iterate *node) override { + return mutate( + lower_iterate(node->value)); // lower into a concrete loop. + } + + // scan tr => if tr has C(tr) then upd a (a (+) tr.C) else scan tr + ir::Stmt visit(const ir::Scan *node) override { + if (key.has_value()) { + const bool all_stored = std::all_of( + aggregations.begin(), aggregations.end(), + [&](const auto &agg) { return agg.contains(*key); }); + if (all_stored) { + // Coiterating several trees reduces over their product. + ir::Expr total = aggregations.front().at(*key); + for (size_t i = 1; i < aggregations.size(); i++) { + total = total * aggregations[i].at(*key); + } + return make_update(loc, combiner, std::move(total)); + } + } + internal_assert(scan_op.has_value()) + << "Cannot scan a subtree for a reduction the runtime cannot " + "combine: " + << ir::Stmt(node); + return ir::Scan::make( + scan_op, loc, func.defined() ? func : node->func, node->value); + } + + // A recursive call updates the same accumulator. + ir::Stmt visit(const ir::YieldFrom *node) override { return node; } + }; + + const ir::Type acc_t = identity.type(); + + static size_t counter = 0; + std::string name = "_acc" + std::to_string(counter++); + ir::WriteLoc loc(name, acc_t); + + // WrapWithAccumulator(a, id) + ir::Stmt header = ir::Allocate::make(loc, std::move(identity), + ir::Allocate::Memory::Stack); + ir::Expr ret_var = ir::Var::make(acc_t, std::move(name)); + ir::Stmt footer = ir::Yield::make(ret_var); + + // Peel an immediately enclosed map so the reduction can be matched against + // the augmentations a node stores; the map itself is still lowered below. + ir::Expr func; + if (const ir::SetOp *as_map_op = as_map(inner)) { + func = as_map_op->a; + } + std::optional key = aggregate_key_for(combiner, func); + + std::optional scan_op; + if (const ir::Lambda *lambda = combiner.as()) { + if (const ir::BinOp *binop = lambda->value.as()) { + if (binop->op == ir::BinOp::Add) { + scan_op = ir::AggOp::sum; + } else if (binop->op == ir::BinOp::Mul) { + scan_op = ir::AggOp::prod; + } + } + } + + ir::Stmt body = build_traversal(inner, tree_types, intervals); + body = RewriteReduce(std::move(combiner), std::move(loc), std::move(key), + std::move(scan_op), std::move(func)) .mutate(body); return ir::Sequence::make( @@ -514,7 +1238,8 @@ ir::Stmt build_product(ir::Stmt a_body, ir::Stmt b_body, ir::Type ret_type) { for (const auto &a : as) { vals.push_back(make_tuple_pair(a, b)); } - return ir::Scan::make(make_tuple(std::move(vals))); + return ir::Scan::make(ir::Expr(), + make_tuple(std::move(vals))); } else if (const ir::YieldFrom *from = a_body.as()) { internal_error @@ -550,7 +1275,8 @@ ir::Stmt build_product(ir::Stmt a_body, ir::Stmt b_body, ir::Type ret_type) { for (const auto &a : as) { vals.push_back(make_tuple_pair(a, b)); } - return ir::Scan::make(make_tuple(std::move(vals))); + return ir::Scan::make(ir::Expr(), + make_tuple(std::move(vals))); } else if (const ir::YieldFrom *from = a_body.as()) { internal_error @@ -579,7 +1305,8 @@ ir::Stmt build_product(ir::Stmt a_body, ir::Stmt b_body, ir::Type ret_type) { for (const auto &b : bs) { vals.push_back(make_tuple_pair(a, b)); } - return ir::Scan::make(make_tuple(std::move(vals))); + return ir::Scan::make(ir::Expr(), + make_tuple(std::move(vals))); } else if (const ir::Iterate *iterate = a_body.as()) { internal_assert(locs.size() == 2); @@ -590,7 +1317,8 @@ ir::Stmt build_product(ir::Stmt a_body, ir::Stmt b_body, ir::Type ret_type) { for (const auto &b : bs) { vals.push_back(make_tuple_pair(a, b)); } - return ir::Scan::make(make_tuple(std::move(vals))); + return ir::Scan::make(ir::Expr(), + make_tuple(std::move(vals))); } else if (const ir::Scan *scan = a_body.as()) { // Cartesian product of nodes! TODO: doesn't have to be... // Make this scheduable? @@ -602,7 +1330,8 @@ ir::Stmt build_product(ir::Stmt a_body, ir::Stmt b_body, ir::Type ret_type) { pairs.push_back(make_tuple_pair(av, bv)); } } - return ir::Scan::make(make_tuple(std::move(pairs))); + return ir::Scan::make(ir::Expr(), + make_tuple(std::move(pairs))); } else if (const ir::YieldFrom *from = a_body.as()) { internal_error @@ -646,6 +1375,16 @@ ir::Stmt build_traversal(const ir::Expr &expr, const ir::TypeMap &tree_types, return build_base_scan(as_var->name, bvh); } + if (const ir::AggOp *as_agg = expr.as()) { + if (as_agg->op != ir::AggOp::reduce) { + // count, sum and prod are sugar for a map followed by a reduce. + return build_traversal(expand_aggregate(as_agg), tree_types, + intervals); + } + return build_reduce(as_agg->identity, as_agg->combiner, as_agg->a, + tree_types, intervals); + } + const ir::SetOp *as_set = expr.as(); if (as_set == nullptr) { internal_error << "[unimplemented] Unknown traversal pattern: " << expr; @@ -656,10 +1395,29 @@ ir::Stmt build_traversal(const ir::Expr &expr, const ir::TypeMap &tree_types, ir::Stmt body = build_traversal(as_set->b, tree_types, intervals); return build_filter(body, as_set->a, intervals); } - case ir::SetOp::argmin: { - // Argmin is a bit more complicated, because of filter fusion. - return build_argmin(as_set->a, as_set->b, tree_types, intervals, - expr.type()); + case ir::SetOp::map: { + ir::Stmt body = build_traversal(as_set->b, tree_types, intervals); + return build_map(body, as_set->a); + } + case ir::SetOp::argmin: + case ir::SetOp::argmax: { + // These are a bit more complicated, because of filter fusion. + const Extremum dir = + as_set->op == ir::SetOp::argmin ? Extremum::Min : Extremum::Max; + return build_arg_extremum(dir, as_set->a, as_set->b, tree_types, + intervals, expr.type()); + } + case ir::SetOp::minimum: + case ir::SetOp::maximum: { + const Extremum dir = + as_set->op == ir::SetOp::minimum ? Extremum::Min : Extremum::Max; + return build_extremum(dir, as_set->a, as_set->b, tree_types, intervals, + expr.type()); + } + case ir::SetOp::any: + case ir::SetOp::all: { + return build_quantifier(as_set->op == ir::SetOp::any, as_set->a, + as_set->b, tree_types, intervals); } case ir::SetOp::product: { ir::Stmt a_body = build_traversal(as_set->a, tree_types, intervals); @@ -756,6 +1514,7 @@ struct LowerBVH : public ir::Mutator { } ir::Expr visit(const ir::SetOp *op) override { return build_func(op); } + ir::Expr visit(const ir::AggOp *op) override { return build_func(op); } }; } // namespace @@ -796,7 +1555,7 @@ ir::Stmt build_base_scan(const std::string &name, const ir::BVH_t *bvh_t) { for (const auto &c : children) { cs.push_back(ir::Access::make(c.name, node)); } - stmts.back() = ir::Scan::make(make_tuple(cs)); + stmts.back() = ir::Scan::make(ir::Expr(), make_tuple(cs)); } arms[i].first = bvh_t->nodes[i]; diff --git a/src/Opt/DCE.cpp b/src/Opt/DCE.cpp index 20928bfe6..2da5cb00e 100644 --- a/src/Opt/DCE.cpp +++ b/src/Opt/DCE.cpp @@ -14,6 +14,7 @@ #include #include #include +#include namespace bonsai { namespace opt { diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index 2393d651d..13d6bf215 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -110,11 +110,25 @@ struct Parser { "sqrt", "tan", // Set operations + "argmax", "argmin", "filter", "map", + "maximum", + "minimum", "product", // Geometry operations + "covers", + "disjoint", + "equals", + "touches", + "within", + "lex", + "ley", + "lez", + "ltx", + "lty", + "ltz", "distmax", "distmin", "intersects", @@ -126,6 +140,10 @@ struct Parser { "idxmin", "prod", "any", + // Aggregations + "avg", + "count", + "reduce", // Other builtins "cast", "eps", @@ -1344,15 +1362,52 @@ struct Parser { }; static constexpr auto SPATTERNS = std::to_array({ + // `any` and `all` also name the vector reductions below, but at + // arity 1; here they take a predicate and a set. + {"all", ir::SetOp::all}, + {"any", ir::SetOp::any}, + {"argmax", ir::SetOp::argmax}, {"argmin", ir::SetOp::argmin}, {"filter", ir::SetOp::filter}, {"map", ir::SetOp::map}, + {"maximum", ir::SetOp::maximum}, + {"minimum", ir::SetOp::minimum}, {"product", ir::SetOp::product}, }); - if (auto op = try_match_pattern(name, args.size(), - SPATTERNS, 2)) { - return ir::SetOp::make(*op, std::move(args[0]), std::move(args[1])); + if (args.size() == 2) { + if (auto op = try_match_pattern( + name, args.size(), SPATTERNS, 2)) { + return ir::SetOp::make(*op, std::move(args[0]), + std::move(args[1])); + } + } + + // Aggregations over a set. Note that `sum` and `prod` are absent: + // they share a spelling with the vector reductions below, so + // VectorReduce::make dispatches them on the argument type instead. + struct AggPattern { + const std::string_view name; + ir::AggOp::OpType op; + }; + + static constexpr auto APATTERNS = std::to_array({ + {"avg", ir::AggOp::avg}, + {"count", ir::AggOp::count}, + }); + + if (auto op = try_match_pattern(name, args.size(), + APATTERNS, 1)) { + return ir::AggOp::make(*op, std::move(args[0])); + } + + if (name == "reduce") { + if (args.size() != 3) { + report_error() + << "reduce takes 3 argument(s), received " << args.size(); + } + return ir::AggOp::make(std::move(args[0]), std::move(args[1]), + std::move(args[2])); } // Geometry operations @@ -1362,10 +1417,21 @@ struct Parser { }; static const auto GPATTERNS = std::to_array({ + {"contains", ir::GeomOp::contains}, + {"covers", ir::GeomOp::covers}, + {"disjoint", ir::GeomOp::disjoint}, + {"equals", ir::GeomOp::equals}, + {"intersects", ir::GeomOp::intersects}, + {"touches", ir::GeomOp::touches}, + {"within", ir::GeomOp::within}, + {"lex", ir::GeomOp::lex}, + {"ley", ir::GeomOp::ley}, + {"lez", ir::GeomOp::lez}, + {"ltx", ir::GeomOp::ltx}, + {"lty", ir::GeomOp::lty}, + {"ltz", ir::GeomOp::ltz}, {"distmax", ir::GeomOp::distmax}, {"distmin", ir::GeomOp::distmin}, - {"intersects", ir::GeomOp::intersects}, - {"contains", ir::GeomOp::contains}, }); if (auto op = try_match_pattern(name, args.size(), @@ -1406,12 +1472,27 @@ struct Parser { ir::Expr parse_identifier() { const std::string name = get_id(); - if (program.funcs.contains(name) || is_builtin(name)) { + if (program.funcs.contains(name)) { + return parse_function_call(name); + } + + // A builtin name denotes an intrinsic only where it is actually + // applied, either directly as `f(x)` or with template arguments as + // `f[[T]](x)`. Everywhere else it is an ordinary identifier, which is + // what lets a layout field or variable be called e.g. `count`. A bare + // builtin name was already rejected ("Cannot use intrinsic as func + // pointer"), so nothing that used to parse stops parsing. + if (is_builtin(name) && (peek().type == Token::Type::LPAREN || + peek().type == Token::Type::LBRACKET)) { return parse_function_call(name); } if (name == "inf") { - return ir::Infinity::make(f32); + return ir::Extrema::make(f32, ir::Extrema::inf); + } + + if (name == "eps") { + return ir::Extrema::make(f32, ir::Extrema::eps); } if (consume(Token::Type::LPAREN)) { @@ -1538,7 +1619,7 @@ struct Parser { "received: " << type; // TODO: this should be handled in codegen... - return ir::FloatImm::make(type, machine_epsilon(type)); + return ir::Extrema::make(std::move(type), ir::Extrema::eps); } if (!template_types.empty()) { @@ -1678,6 +1759,18 @@ struct Parser { return types; } + std::vector parse_string_list_until(const Token::Type &token) { + std::vector strings; + if (consume(token)) { + return strings; + } + do { + strings.emplace_back(get_id()); + } while (consume(Token::Type::COMMA)); + expect(token); + return strings; + } + std::vector parse_lambda_args() { // arg := name (':' type)? // args := arg (',' arg)* @@ -2208,6 +2301,18 @@ struct Parser { "", std::move(low), std::move(high)}}; } + } else if (name == "avg" || name == "count" || name == "max" || + name == "min" || name == "prod" || name == "sum") { + // Reduction augmentation, e.g. `with min(id) = idl`: the node + // field `idl` stores the minimum `id` over the subtree. + expect(Token::Type::LPAREN); + std::vector args = + parse_string_list_until(Token::Type::RPAREN); + expect(Token::Type::ASSIGN); + std::string value = get_id(); + auto op = ir::Annotation::Aggregate::str_to_op(name); + return ir::Annotation{ir::Annotation::Aggregate{op, std::move(args), + std::move(value)}}; } else if (program.types.contains(name)) { ir::Type type = program.types[std::move(name)]; diff --git a/src/Utils.cpp b/src/Utils.cpp index 53be4743a..abb453f53 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -121,7 +121,7 @@ bool is_const(const Expr &e) { std::all_of(b->values.begin(), b->values.end(), [](const Expr &v) { return is_const(v); }); } - return e.is(); + return e.is(); } bool is_location_expr(const Expr &expr) { @@ -171,7 +171,7 @@ Expr make_all_ones(const Type &t) { return make_const(t, bit_mask(t.bits())); } Expr make_inf(const Type &t) { if (t.is()) { - return Infinity::make(t); + return Extrema::make(t, Extrema::inf); } internal_error << "Unknown infinity for type: " << t; } @@ -192,7 +192,7 @@ Expr make_tuple(std::vector exprs) { etypes.push_back(e.type()); } Type tuple_t = Tuple_t::make(std::move(etypes)); - return ir::Build::make(std::move(tuple_t), std::move(exprs)); + return Build::make(std::move(tuple_t), std::move(exprs)); } std::vector break_tuple(Expr expr) { @@ -261,8 +261,8 @@ Expr constant_cast(const Type &t, const Expr &e) { } } else if (e.is()) { return constant_cast(t, e.as()->value); - } else if (e.is()) { - return Infinity::make(t); + } else if (const auto *extrema = e.as()) { + return Extrema::make(t, extrema->op); } internal_error << "Unsure how to convert constant to type: " << t << " expr: " << e; @@ -342,7 +342,7 @@ struct VarNameReplacer : public Mutator { Expr visit(const Var *node) override { auto iter = repls.find(node->name); if (iter != repls.cend()) { - return ir::Var::make(node->type, iter->second); + return Var::make(node->type, iter->second); } else { return node; } @@ -356,6 +356,27 @@ Expr replace(const std::map &repls, const Expr &orig) { return replacer.mutate(orig); } +Expr replace(const std::map &repls, + const Expr &orig) { + struct Replacer : public Mutator { + const std::map &repls; + Replacer(const std::map &repls) + : repls(repls) {} + + Expr mutate(const Expr &expr) override { + const auto &iter = repls.find(expr); + if (iter != repls.cend()) { + return iter->second; + } else { + return Mutator::mutate(expr); + } + } + }; + + Replacer replacer(repls); + return replacer.mutate(orig); +} + Stmt replace(const std::map &repls, const Stmt &orig) { VarReplacer replacer(repls); return replacer.mutate(orig); @@ -520,7 +541,7 @@ Type flatten_array_type(const Type &type) { internal_error << "flatten_array_type called on non-Array_t: " << type; } -std::string get_specifier(const ir::Type &type) { +std::string get_specifier(const Type &type) { std::string specifier = "%"; const uint32_t width = type.bits(); if (type.is_bool()) { @@ -549,9 +570,9 @@ std::string get_specifier(const ir::Type &type) { internal_error << "[unimplemented] print: " << type; } -bool is_dynamic_array_struct_type(const ir::Type &type) { +bool is_dynamic_array_struct_type(const Type &type) { // Dynamic arrays are lowered to structs. - if (const auto *dynamic_array_t = type.as()) { + if (const auto *dynamic_array_t = type.as()) { if (dynamic_array_t->name.starts_with("__dyn_array")) { return true; } diff --git a/stdlib/distance.bonsai b/stdlib/distance.bonsai index 85e009a5e..1b570f619 100644 --- a/stdlib/distance.bonsai +++ b/stdlib/distance.bonsai @@ -154,3 +154,60 @@ func distmin(r : Ray, s : Sphere) -> Float { } return -inf; } + +// --------------------------------------------------------------------------- +// Metrics between bounding boxes and between a box and a query primitive. +// +// Predicate analysis bounds a metric over a subtree by applying distmin and +// distmax to the subtree's bounding volume (Algorithm 6, lines 15-25), so +// every metric a query uses needs a counterpart on the volumes. +// --------------------------------------------------------------------------- + +// The gap between the boxes along each axis, zero where they overlap. +func distmin(a : AABB, b : AABB) -> Float { + gap = max(max(a.low - b.high, b.low - a.high), 0.0); + return norm(gap); +} + +// The farthest a point of one box can be from a point of the other. +func distmax(a : AABB, b : AABB) -> Float { + span = max(abs(a.low - b.high), abs(a.high - b.low)); + return norm(span); +} + +func distmax(pt : Point, a : AABB) -> Float { + span = max(abs(pt.vec - a.low), abs(pt.vec - a.high)); + return norm(span); +} + +func distmin(a : AABB, pt : Point) -> Float { + return distmin(pt, a); +} + +func distmax(a : AABB, pt : Point) -> Float { + return distmax(pt, a); +} + +func distmin(p : Point, q : Point) -> Float { + return norm(p.vec - q.vec); +} + +func distmax(p : Point, q : Point) -> Float { + return norm(p.vec - q.vec); +} + +func distmin(pt : Point, s : Sphere) -> Float { + return max(norm(pt.vec - s.center) - s.radius, 0.0); +} + +func distmax(pt : Point, s : Sphere) -> Float { + return norm(pt.vec - s.center) + s.radius; +} + +// The triangle's farthest point from `pt` is one of its vertices. +func distmax(pt : Point, tri : Triangle) -> Float { + d0 = norm(pt.vec - tri.p0); + d1 = norm(pt.vec - tri.p1); + d2 = norm(pt.vec - tri.p2); + return max(max(d0, d1), d2); +} diff --git a/stdlib/topology.bonsai b/stdlib/topology.bonsai new file mode 100644 index 000000000..05f840024 --- /dev/null +++ b/stdlib/topology.bonsai @@ -0,0 +1,343 @@ +// The topological and ordering predicates of the Bonsai query language, +// following the semantics of Egenhofer and Herring. +// +// Predicate analysis derives pruning conditions in terms of bounding volumes, +// so the relations between two AABBs are the ones the compiler itself emits +// and must always be available. The relations between an AABB and a primitive +// are what user queries are written against. +// +// A relation is only defined here where it is meaningful for the pair of +// shapes. `touches` and `equals` compare boundaries exactly, so they are given +// only for pairs whose boundaries are comparable. + +import stdlib/aabb; +import stdlib/numerics; +import stdlib/point; +import stdlib/ray; +import stdlib/sphere; +import stdlib/triangle; + +// --------------------------------------------------------------------------- +// Exact bounding boxes of the primitive types. Ordinary functions do not +// overload, so each is named for its argument. +// --------------------------------------------------------------------------- + +func aabb_of_point(p : Point) -> AABB { + return AABB{p.vec, p.vec}; +} + +func aabb_of_sphere(s : Sphere) -> AABB { + return AABB{s.center - s.radius, s.center + s.radius}; +} + +func aabb_of_triangle(t : Triangle) -> AABB { + return AABB{min(min(t.p0, t.p1), t.p2), max(max(t.p0, t.p1), t.p2)}; +} + +// --------------------------------------------------------------------------- +// AABB against AABB. +// +// Boxes are closed regions, so `contains` and `covers` coincide: both ask +// whether b lies inside a with boundary contact allowed. They are kept +// separate because predicate analysis names them separately. +// --------------------------------------------------------------------------- + +func contains(a : AABB, b : AABB) -> bool { + return all((a.low <= b.low) & (b.high <= a.high)); +} + +func covers(a : AABB, b : AABB) -> bool { + return contains(a, b); +} + +func within(a : AABB, b : AABB) -> bool { + return contains(b, a); +} + +func intersects(a : AABB, b : AABB) -> bool { + return all(max(a.low, b.low) <= min(a.high, b.high)); +} + +func disjoint(a : AABB, b : AABB) -> bool { + return !intersects(a, b); +} + +func equals(a : AABB, b : AABB) -> bool { + return all((a.low == b.low) & (a.high == b.high)); +} + +// The boxes meet, but their interiors do not. +func touches(a : AABB, b : AABB) -> bool { + return intersects(a, b) & !all(max(a.low, b.low) < min(a.high, b.high)); +} + +// --------------------------------------------------------------------------- +// AABB against Point. +// +// A point has empty interior, so it can be covered by a box but never +// contains one, and it can touch a box only by lying on its boundary. +// --------------------------------------------------------------------------- + +func contains(a : AABB, p : Point) -> bool { + return all((a.low <= p.vec) & (p.vec <= a.high)); +} + +func covers(a : AABB, p : Point) -> bool { + return contains(a, p); +} + +func within(p : Point, a : AABB) -> bool { + return contains(a, p); +} + +func intersects(a : AABB, p : Point) -> bool { + return contains(a, p); +} + +func intersects(p : Point, a : AABB) -> bool { + return contains(a, p); +} + +func disjoint(a : AABB, p : Point) -> bool { + return !contains(a, p); +} + +func disjoint(p : Point, a : AABB) -> bool { + return !contains(a, p); +} + +func touches(a : AABB, p : Point) -> bool { + return contains(a, p) & !all((a.low < p.vec) & (p.vec < a.high)); +} + +// --------------------------------------------------------------------------- +// Point against Point. +// --------------------------------------------------------------------------- + +func equals(p : Point, q : Point) -> bool { + return all(p.vec == q.vec); +} + +func contains(p : Point, q : Point) -> bool { + return equals(p, q); +} + +func within(p : Point, q : Point) -> bool { + return equals(p, q); +} + +func intersects(p : Point, q : Point) -> bool { + return equals(p, q); +} + +func disjoint(p : Point, q : Point) -> bool { + return !equals(p, q); +} + +// --------------------------------------------------------------------------- +// AABB against Sphere. +// +// A sphere is contained in a box exactly when its own bounding box is, and +// meets a box exactly when the box's closest point is within the radius. +// --------------------------------------------------------------------------- + +func contains(a : AABB, s : Sphere) -> bool { + return contains(a, aabb_of_sphere(s)); +} + +func covers(a : AABB, s : Sphere) -> bool { + return contains(a, s); +} + +func within(s : Sphere, a : AABB) -> bool { + return contains(a, s); +} + +func intersects(a : AABB, s : Sphere) -> bool { + closest = min(max(s.center, a.low), a.high); + d = closest - s.center; + return sum(d * d) <= s.radius * s.radius; +} + +func intersects(s : Sphere, a : AABB) -> bool { + return intersects(a, s); +} + +func disjoint(a : AABB, s : Sphere) -> bool { + return !intersects(a, s); +} + +func disjoint(s : Sphere, a : AABB) -> bool { + return !intersects(a, s); +} + +func contains(s : Sphere, a : AABB) -> bool { + // Every corner of the box is inside the sphere exactly when the farthest + // one is, and the farthest corner is the one opposite the center. + far = max(abs(a.low - s.center), abs(a.high - s.center)); + return sum(far * far) <= s.radius * s.radius; +} + +func within(a : AABB, s : Sphere) -> bool { + return contains(s, a); +} + +// --------------------------------------------------------------------------- +// AABB against Triangle. +// +// Containment reduces to containing every vertex. Overlap is the separating +// axis test of Ericson, Real-Time Collision Detection 5.2.9, over the box's +// three face normals, the triangle's normal, and the nine edge cross +// products. +// --------------------------------------------------------------------------- + +func contains(a : AABB, t : Triangle) -> bool { + return contains(a, aabb_of_triangle(t)); +} + +func covers(a : AABB, t : Triangle) -> bool { + return contains(a, t); +} + +func within(t : Triangle, a : AABB) -> bool { + return contains(a, t); +} + +// Whether the projections of the triangle and of a box with half-extents `e` +// onto `axis` overlap. Both are centred on the origin. +func sat_axis_separates(axis : vec3f, e : vec3f, v0 : vec3f, v1 : vec3f, v2 : vec3f) -> bool { + p0 = dot(axis, v0); + p1 = dot(axis, v1); + p2 = dot(axis, v2); + r = dot(abs(axis), e); + return (min(min(p0, p1), p2) > r) | (max(max(p0, p1), p2) < -r); +} + +func intersects(a : AABB, t : Triangle) -> bool { + c = 0.5 * (a.low + a.high); + e = 0.5 * (a.high - a.low); + + v0 = t.p0 - c; + v1 = t.p1 - c; + v2 = t.p2 - c; + + f0 = v1 - v0; + f1 = v2 - v1; + f2 = v0 - v2; + + // The nine axes formed by crossing a box edge with a triangle edge. + if (sat_axis_separates(cross_({1.0, 0.0, 0.0}, f0), e, v0, v1, v2)) { return false; } + if (sat_axis_separates(cross_({1.0, 0.0, 0.0}, f1), e, v0, v1, v2)) { return false; } + if (sat_axis_separates(cross_({1.0, 0.0, 0.0}, f2), e, v0, v1, v2)) { return false; } + if (sat_axis_separates(cross_({0.0, 1.0, 0.0}, f0), e, v0, v1, v2)) { return false; } + if (sat_axis_separates(cross_({0.0, 1.0, 0.0}, f1), e, v0, v1, v2)) { return false; } + if (sat_axis_separates(cross_({0.0, 1.0, 0.0}, f2), e, v0, v1, v2)) { return false; } + if (sat_axis_separates(cross_({0.0, 0.0, 1.0}, f0), e, v0, v1, v2)) { return false; } + if (sat_axis_separates(cross_({0.0, 0.0, 1.0}, f1), e, v0, v1, v2)) { return false; } + if (sat_axis_separates(cross_({0.0, 0.0, 1.0}, f2), e, v0, v1, v2)) { return false; } + + // The box's three face normals. + if (sat_axis_separates({1.0, 0.0, 0.0}, e, v0, v1, v2)) { return false; } + if (sat_axis_separates({0.0, 1.0, 0.0}, e, v0, v1, v2)) { return false; } + if (sat_axis_separates({0.0, 0.0, 1.0}, e, v0, v1, v2)) { return false; } + + // The triangle's normal. + if (sat_axis_separates(cross_(f0, f1), e, v0, v1, v2)) { return false; } + + return true; +} + +func intersects(t : Triangle, a : AABB) -> bool { + return intersects(a, t); +} + +func disjoint(a : AABB, t : Triangle) -> bool { + return !intersects(a, t); +} + +func disjoint(t : Triangle, a : AABB) -> bool { + return !intersects(a, t); +} + +// --------------------------------------------------------------------------- +// Ray against AABB. +// +// A ray has empty interior in three dimensions, so it can only contain +// degenerate geometry. Reporting false is the sound answer: `contains` is +// used by predicate analysis as a sufficient condition, where a false +// negative merely costs pruning, and under negation as a necessary condition, +// where it is the weakest such condition. +// --------------------------------------------------------------------------- + +func contains(r : Ray, a : AABB) -> bool { + return false; +} + +func covers(r : Ray, a : AABB) -> bool { + return false; +} + +func within(a : AABB, r : Ray) -> bool { + return false; +} + +func disjoint(r : Ray, a : AABB) -> bool { + return !intersects(r, a); +} + +func contains(r : Ray, t : Triangle) -> bool { + return false; +} + +func disjoint(r : Ray, t : Triangle) -> bool { + return !intersects(r, t); +} + +func contains(r : Ray, s : Sphere) -> bool { + return false; +} + +func disjoint(r : Ray, s : Sphere) -> bool { + return !intersects(r, s); +} + +// --------------------------------------------------------------------------- +// Ordering predicates. +// +// `lex(a, b)` is `a <=_x b`: a lies at or before b along x, that is, a's +// extent ends no later than b's begins. `ltx` is the strict form. Predicate +// analysis bounds these by monotonicity, so the only requirement here is that +// the relation be monotone in each argument's extent along the dimension. +// --------------------------------------------------------------------------- + +func lex(a : AABB, b : AABB) -> bool { return a.high.x <= b.low.x; } +func ley(a : AABB, b : AABB) -> bool { return a.high.y <= b.low.y; } +func lez(a : AABB, b : AABB) -> bool { return a.high.z <= b.low.z; } + +func ltx(a : AABB, b : AABB) -> bool { return a.high.x < b.low.x; } +func lty(a : AABB, b : AABB) -> bool { return a.high.y < b.low.y; } +func ltz(a : AABB, b : AABB) -> bool { return a.high.z < b.low.z; } + +func lex(a : AABB, p : Point) -> bool { return a.high.x <= p.vec.x; } +func ley(a : AABB, p : Point) -> bool { return a.high.y <= p.vec.y; } +func lez(a : AABB, p : Point) -> bool { return a.high.z <= p.vec.z; } + +func ltx(a : AABB, p : Point) -> bool { return a.high.x < p.vec.x; } +func lty(a : AABB, p : Point) -> bool { return a.high.y < p.vec.y; } +func ltz(a : AABB, p : Point) -> bool { return a.high.z < p.vec.z; } + +func lex(p : Point, a : AABB) -> bool { return p.vec.x <= a.low.x; } +func ley(p : Point, a : AABB) -> bool { return p.vec.y <= a.low.y; } +func lez(p : Point, a : AABB) -> bool { return p.vec.z <= a.low.z; } + +func ltx(p : Point, a : AABB) -> bool { return p.vec.x < a.low.x; } +func lty(p : Point, a : AABB) -> bool { return p.vec.y < a.low.y; } +func ltz(p : Point, a : AABB) -> bool { return p.vec.z < a.low.z; } + +func lex(p : Point, q : Point) -> bool { return p.vec.x <= q.vec.x; } +func ley(p : Point, q : Point) -> bool { return p.vec.y <= q.vec.y; } +func lez(p : Point, q : Point) -> bool { return p.vec.z <= q.vec.z; } + +func ltx(p : Point, q : Point) -> bool { return p.vec.x < q.vec.x; } +func lty(p : Point, q : Point) -> bool { return p.vec.y < q.vec.y; } +func ltz(p : Point, q : Point) -> bool { return p.vec.z < q.vec.z; } diff --git a/tests/README.md b/tests/README.md index fe26128f0..6bb9cdd5c 100644 --- a/tests/README.md +++ b/tests/README.md @@ -5,7 +5,7 @@ run all tests. Some flags might be useful: - `-R`: select tests that match a particular regular expression. - `-L`: select a label to run. Existing labels include `backends`, - `correctness`, `errors`, `llvm`, and `parsing`. + `correctness`, `error`, `llvm`, `lower`, `opt`, and `parsing`. - `-j`: run on multiple processors To update the golden outputs during a test run, set the environment variable diff --git a/tests/bonsai/backends/llvm/exported-function.expect b/tests/bonsai/backends/llvm/exported-function.expect index e39322915..9498f4a9f 100644 --- a/tests/bonsai/backends/llvm/exported-function.expect +++ b/tests/bonsai/backends/llvm/exported-function.expect @@ -60,7 +60,7 @@ foo_entry: ; Function Attrs: nofree nounwind define noundef i32 @main() local_unnamed_addr #2 { main_entry: - %0 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @0, i32 11, i32 22, i32 33, float 4.200000e+01) + %0 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @0, i32 11, i32 22, i32 33, double 4.200000e+01) ret i32 0 } diff --git a/tests/bonsai/backends/llvm/rand-par.expect b/tests/bonsai/backends/llvm/rand-par.expect index 4339118aa..f90cc4090 100644 --- a/tests/bonsai/backends/llvm/rand-par.expect +++ b/tests/bonsai/backends/llvm/rand-par.expect @@ -26,15 +26,19 @@ _parfunc0_entry: %15 = bitcast <4 x i32> %14 to <4 x float> %16 = fadd <4 x float> %15, %17 = extractelement <4 x float> %16, i64 0 - %18 = extractelement <4 x float> %16, i64 1 - %19 = extractelement <4 x float> %16, i64 2 - %20 = extractelement <4 x float> %16, i64 3 - %21 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %8, float %17, float %18, float %19, float %20) - %22 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %16) + %18 = fpext float %17 to double + %19 = extractelement <4 x float> %16, i64 1 + %20 = fpext float %19 to double + %21 = extractelement <4 x float> %16, i64 2 + %22 = fpext float %21 to double + %23 = extractelement <4 x float> %16, i64 3 + %24 = fpext float %23 to double + %25 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %8, double %18, double %20, double %22, double %24) + %26 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %16) %ctx0__alloc0_ld = load ptr, ptr %ctx0, align 8 - %23 = sext i32 %8 to i64 - %ctx0__alloc0_ld1 = getelementptr inbounds float, ptr %ctx0__alloc0_ld, i64 %23 - store float %22, ptr %ctx0__alloc0_ld1, align 4 + %27 = sext i32 %8 to i64 + %ctx0__alloc0_ld1 = getelementptr inbounds float, ptr %ctx0__alloc0_ld, i64 %27 + store float %26, ptr %ctx0__alloc0_ld1, align 4 ret void } @@ -58,37 +62,53 @@ main_entry: call void @dispatch_apply_f(i64 16, ptr %0, ptr nonnull %ctx.i, ptr nonnull @_parfunc0) call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %ctx.i) %extract = load float, ptr %_alloc0_untyped.i, align 4 + %1 = fpext float %extract to double %extract_ptr1 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 4 %extract2 = load float, ptr %extract_ptr1, align 4 + %2 = fpext float %extract2 to double %extract_ptr3 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 8 %extract4 = load float, ptr %extract_ptr3, align 4 + %3 = fpext float %extract4 to double %extract_ptr5 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 12 %extract6 = load float, ptr %extract_ptr5, align 4 + %4 = fpext float %extract6 to double %extract_ptr7 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 16 %extract8 = load float, ptr %extract_ptr7, align 4 + %5 = fpext float %extract8 to double %extract_ptr9 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 20 %extract10 = load float, ptr %extract_ptr9, align 4 + %6 = fpext float %extract10 to double %extract_ptr11 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 24 %extract12 = load float, ptr %extract_ptr11, align 4 + %7 = fpext float %extract12 to double %extract_ptr13 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 28 %extract14 = load float, ptr %extract_ptr13, align 4 + %8 = fpext float %extract14 to double %extract_ptr15 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 32 %extract16 = load float, ptr %extract_ptr15, align 4 + %9 = fpext float %extract16 to double %extract_ptr17 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 36 %extract18 = load float, ptr %extract_ptr17, align 4 + %10 = fpext float %extract18 to double %extract_ptr19 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 40 %extract20 = load float, ptr %extract_ptr19, align 4 + %11 = fpext float %extract20 to double %extract_ptr21 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 44 %extract22 = load float, ptr %extract_ptr21, align 4 + %12 = fpext float %extract22 to double %extract_ptr23 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 48 %extract24 = load float, ptr %extract_ptr23, align 4 + %13 = fpext float %extract24 to double %extract_ptr25 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 52 %extract26 = load float, ptr %extract_ptr25, align 4 + %14 = fpext float %extract26 to double %extract_ptr27 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 56 %extract28 = load float, ptr %extract_ptr27, align 4 + %15 = fpext float %extract28 to double %extract_ptr29 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 60 %extract30 = load float, ptr %extract_ptr29, align 4 - %1 = call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @0, float %extract, float %extract2, float %extract4, float %extract6, float %extract8, float %extract10, float %extract12, float %extract14, float %extract16, float %extract18, float %extract20, float %extract22, float %extract24, float %extract26, float %extract28, float %extract30) + %16 = fpext float %extract30 to double + %17 = call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @0, double %1, double %2, double %3, double %4, double %5, double %6, double %7, double %8, double %9, double %10, double %11, double %12, double %13, double %14, double %15, double %16) ret i32 0 } @@ -107,12 +127,16 @@ test_rand_entry: %7 = bitcast <4 x i32> %6 to <4 x float> %8 = fadd <4 x float> %7, %9 = extractelement <4 x float> %8, i64 0 - %10 = extractelement <4 x float> %8, i64 1 - %11 = extractelement <4 x float> %8, i64 2 - %12 = extractelement <4 x float> %8, i64 3 - %13 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %i, float %9, float %10, float %11, float %12) - %14 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %8) - ret float %14 + %10 = fpext float %9 to double + %11 = extractelement <4 x float> %8, i64 1 + %12 = fpext float %11 to double + %13 = extractelement <4 x float> %8, i64 2 + %14 = fpext float %13 to double + %15 = extractelement <4 x float> %8, i64 3 + %16 = fpext float %15 to double + %17 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %i, double %10, double %12, double %14, double %16) + %18 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %8) + ret float %18 } define noundef ptr @test_rand_all(i32 noundef %i) local_unnamed_addr { diff --git a/tests/bonsai/backends/llvm/rand.expect b/tests/bonsai/backends/llvm/rand.expect index 62dd02de7..bdee2322a 100644 --- a/tests/bonsai/backends/llvm/rand.expect +++ b/tests/bonsai/backends/llvm/rand.expect @@ -11,7 +11,7 @@ _traverse_array0_entry: br label %_i00_for _i00_for: ; preds = %_traverse_array0_entry, %_i00_for - %0 = phi i32 [ 0, %_traverse_array0_entry ], [ %17, %_i00_for ] + %0 = phi i32 [ 0, %_traverse_array0_entry ], [ %21, %_i00_for ] %rng_seed.i = load <4 x i32>, ptr %_rng_state, align 16 %1 = mul <4 x i32> %rng_seed.i, %2 = add <4 x i32> %1, @@ -24,17 +24,21 @@ _i00_for: ; preds = %_traverse_array0_en %8 = bitcast <4 x i32> %7 to <4 x float> %9 = fadd <4 x float> %8, %10 = extractelement <4 x float> %9, i64 0 - %11 = extractelement <4 x float> %9, i64 1 - %12 = extractelement <4 x float> %9, i64 2 - %13 = extractelement <4 x float> %9, i64 3 - %14 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %0, float %10, float %11, float %12, float %13) - %15 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %9) - %16 = zext nneg i32 %0 to i64 - %_alloc0_ld1 = getelementptr inbounds float, ptr %_alloc0_untyped, i64 %16 - store float %15, ptr %_alloc0_ld1, align 4 - %17 = add nuw nsw i32 %0, 1 - %18 = icmp ugt i32 %0, 14 - br i1 %18, label %_i00_for_end, label %_i00_for + %11 = fpext float %10 to double + %12 = extractelement <4 x float> %9, i64 1 + %13 = fpext float %12 to double + %14 = extractelement <4 x float> %9, i64 2 + %15 = fpext float %14 to double + %16 = extractelement <4 x float> %9, i64 3 + %17 = fpext float %16 to double + %18 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %0, double %11, double %13, double %15, double %17) + %19 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %9) + %20 = zext nneg i32 %0 to i64 + %_alloc0_ld1 = getelementptr inbounds float, ptr %_alloc0_untyped, i64 %20 + store float %19, ptr %_alloc0_ld1, align 4 + %21 = add nuw nsw i32 %0, 1 + %22 = icmp ugt i32 %0, 14 + br i1 %22, label %_i00_for_end, label %_i00_for _i00_for_end: ; preds = %_i00_for ret ptr %_alloc0_untyped @@ -55,7 +59,7 @@ main_entry: _i00_for.i: ; preds = %_i00_for.i, %main_entry %_rng_state.0 = phi <4 x i32> [ %7, %main_entry ], [ %13, %_i00_for.i ] - %8 = phi i32 [ 0, %main_entry ], [ %25, %_i00_for.i ] + %8 = phi i32 [ 0, %main_entry ], [ %29, %_i00_for.i ] %9 = mul <4 x i32> %_rng_state.0, %10 = add <4 x i32> %9, %11 = mul <4 x i32> %10, %_rng_state.0 @@ -66,51 +70,71 @@ _i00_for.i: ; preds = %_i00_for.i, %main_e %16 = bitcast <4 x i32> %15 to <4 x float> %17 = fadd <4 x float> %16, %18 = extractelement <4 x float> %17, i64 0 - %19 = extractelement <4 x float> %17, i64 1 - %20 = extractelement <4 x float> %17, i64 2 - %21 = extractelement <4 x float> %17, i64 3 - %22 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %8, float %18, float %19, float %20, float %21) - %23 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %17) - %24 = zext nneg i32 %8 to i64 - %_alloc0_ld1.i = getelementptr inbounds float, ptr %_alloc0_untyped.i, i64 %24 - store float %23, ptr %_alloc0_ld1.i, align 4 - %25 = add nuw nsw i32 %8, 1 - %26 = icmp ugt i32 %8, 14 - br i1 %26, label %_traverse_array0.exit, label %_i00_for.i + %19 = fpext float %18 to double + %20 = extractelement <4 x float> %17, i64 1 + %21 = fpext float %20 to double + %22 = extractelement <4 x float> %17, i64 2 + %23 = fpext float %22 to double + %24 = extractelement <4 x float> %17, i64 3 + %25 = fpext float %24 to double + %26 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %8, double %19, double %21, double %23, double %25) + %27 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %17) + %28 = zext nneg i32 %8 to i64 + %_alloc0_ld1.i = getelementptr inbounds float, ptr %_alloc0_untyped.i, i64 %28 + store float %27, ptr %_alloc0_ld1.i, align 4 + %29 = add nuw nsw i32 %8, 1 + %30 = icmp ugt i32 %8, 14 + br i1 %30, label %_traverse_array0.exit, label %_i00_for.i _traverse_array0.exit: ; preds = %_i00_for.i %extract = load float, ptr %_alloc0_untyped.i, align 4 + %31 = fpext float %extract to double %extract_ptr1 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 4 %extract2 = load float, ptr %extract_ptr1, align 4 + %32 = fpext float %extract2 to double %extract_ptr3 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 8 %extract4 = load float, ptr %extract_ptr3, align 4 + %33 = fpext float %extract4 to double %extract_ptr5 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 12 %extract6 = load float, ptr %extract_ptr5, align 4 + %34 = fpext float %extract6 to double %extract_ptr7 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 16 %extract8 = load float, ptr %extract_ptr7, align 4 + %35 = fpext float %extract8 to double %extract_ptr9 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 20 %extract10 = load float, ptr %extract_ptr9, align 4 + %36 = fpext float %extract10 to double %extract_ptr11 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 24 %extract12 = load float, ptr %extract_ptr11, align 4 + %37 = fpext float %extract12 to double %extract_ptr13 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 28 %extract14 = load float, ptr %extract_ptr13, align 4 + %38 = fpext float %extract14 to double %extract_ptr15 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 32 %extract16 = load float, ptr %extract_ptr15, align 4 + %39 = fpext float %extract16 to double %extract_ptr17 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 36 %extract18 = load float, ptr %extract_ptr17, align 4 + %40 = fpext float %extract18 to double %extract_ptr19 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 40 %extract20 = load float, ptr %extract_ptr19, align 4 + %41 = fpext float %extract20 to double %extract_ptr21 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 44 %extract22 = load float, ptr %extract_ptr21, align 4 + %42 = fpext float %extract22 to double %extract_ptr23 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 48 %extract24 = load float, ptr %extract_ptr23, align 4 + %43 = fpext float %extract24 to double %extract_ptr25 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 52 %extract26 = load float, ptr %extract_ptr25, align 4 + %44 = fpext float %extract26 to double %extract_ptr27 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 56 %extract28 = load float, ptr %extract_ptr27, align 4 + %45 = fpext float %extract28 to double %extract_ptr29 = getelementptr inbounds i8, ptr %_alloc0_untyped.i, i64 60 %extract30 = load float, ptr %extract_ptr29, align 4 - %27 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @0, float %extract, float %extract2, float %extract4, float %extract6, float %extract8, float %extract10, float %extract12, float %extract14, float %extract16, float %extract18, float %extract20, float %extract22, float %extract24, float %extract26, float %extract28, float %extract30) + %46 = fpext float %extract30 to double + %47 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @0, double %31, double %32, double %33, double %34, double %35, double %36, double %37, double %38, double %39, double %40, double %41, double %42, double %43, double %44, double %45, double %46) ret i32 0 } @@ -129,12 +153,16 @@ test_rand_entry: %7 = bitcast <4 x i32> %6 to <4 x float> %8 = fadd <4 x float> %7, %9 = extractelement <4 x float> %8, i64 0 - %10 = extractelement <4 x float> %8, i64 1 - %11 = extractelement <4 x float> %8, i64 2 - %12 = extractelement <4 x float> %8, i64 3 - %13 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %i, float %9, float %10, float %11, float %12) - %14 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %8) - ret float %14 + %10 = fpext float %9 to double + %11 = extractelement <4 x float> %8, i64 1 + %12 = fpext float %11 to double + %13 = extractelement <4 x float> %8, i64 2 + %14 = fpext float %13 to double + %15 = extractelement <4 x float> %8, i64 3 + %16 = fpext float %15 to double + %17 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %i, double %10, double %12, double %14, double %16) + %18 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %8) + ret float %18 } ; Function Attrs: nofree nounwind @@ -144,7 +172,7 @@ test_rand_all_entry: br label %_i00_for.i _i00_for.i: ; preds = %_i00_for.i, %test_rand_all_entry - %0 = phi i32 [ 0, %test_rand_all_entry ], [ %17, %_i00_for.i ] + %0 = phi i32 [ 0, %test_rand_all_entry ], [ %21, %_i00_for.i ] %rng_seed.i.i = load <4 x i32>, ptr %_rng_state, align 16 %1 = mul <4 x i32> %rng_seed.i.i, %2 = add <4 x i32> %1, @@ -157,17 +185,21 @@ _i00_for.i: ; preds = %_i00_for.i, %test_r %8 = bitcast <4 x i32> %7 to <4 x float> %9 = fadd <4 x float> %8, %10 = extractelement <4 x float> %9, i64 0 - %11 = extractelement <4 x float> %9, i64 1 - %12 = extractelement <4 x float> %9, i64 2 - %13 = extractelement <4 x float> %9, i64 3 - %14 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %0, float %10, float %11, float %12, float %13) - %15 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %9) - %16 = zext nneg i32 %0 to i64 - %_alloc0_ld1.i = getelementptr inbounds float, ptr %_alloc0_untyped.i, i64 %16 - store float %15, ptr %_alloc0_ld1.i, align 4 - %17 = add nuw nsw i32 %0, 1 - %18 = icmp ugt i32 %0, 14 - br i1 %18, label %_traverse_array0.exit, label %_i00_for.i + %11 = fpext float %10 to double + %12 = extractelement <4 x float> %9, i64 1 + %13 = fpext float %12 to double + %14 = extractelement <4 x float> %9, i64 2 + %15 = fpext float %14 to double + %16 = extractelement <4 x float> %9, i64 3 + %17 = fpext float %16 to double + %18 = tail call i32 (ptr, ...) @printf(ptr nonnull dereferenceable(1) @1, i32 %0, double %11, double %13, double %15, double %17) + %19 = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> %9) + %20 = zext nneg i32 %0 to i64 + %_alloc0_ld1.i = getelementptr inbounds float, ptr %_alloc0_untyped.i, i64 %20 + store float %19, ptr %_alloc0_ld1.i, align 4 + %21 = add nuw nsw i32 %0, 1 + %22 = icmp ugt i32 %0, 14 + br i1 %22, label %_traverse_array0.exit, label %_i00_for.i _traverse_array0.exit: ; preds = %_i00_for.i ret ptr %_alloc0_untyped.i diff --git a/tests/bonsai/correctness/cpp/CMakeLists.txt b/tests/bonsai/correctness/cpp/CMakeLists.txt index a1fa7ae9e..d5e19bd7e 100644 --- a/tests/bonsai/correctness/cpp/CMakeLists.txt +++ b/tests/bonsai/correctness/cpp/CMakeLists.txt @@ -1,6 +1,7 @@ add_bonsai_tests( LABELS correctness cpp FILES + setops.bonsai simple.bonsai struct.bonsai swap.bonsai diff --git a/tests/bonsai/correctness/cpp/main_setops.cpp b/tests/bonsai/correctness/cpp/main_setops.cpp new file mode 100644 index 000000000..956aafe8d --- /dev/null +++ b/tests/bonsai/correctness/cpp/main_setops.cpp @@ -0,0 +1,131 @@ +// Drives the queries in setops.bonsai and checks each against a linear scan +// over the same data. The generated traversals prune, so agreeing with an +// unpruned scan on every query and every range is the property that matters. + +#include "setops.h" + +#include +#include +#include +#include +#include +#include + +namespace { + +// Build the packed interval tree that setops.bonsai's layout describes: the +// primitives sorted, and a balanced index over them where each node records +// the range of values beneath it. +_tree_layout0 build_tree(std::vector values) { + constexpr uint64_t MAX_LEAF_COUNT = 4; + + _tree_layout0 tree; + tree.pCount = values.size(); + tree.prims = static_cast(std::malloc(sizeof(float) * tree.pCount)); + std::sort(values.begin(), values.end()); + std::copy(values.begin(), values.end(), tree.prims); + + tree.count = 2 * tree.pCount - 1; + tree.group0_index = static_cast<_tree_layout1 *>( + std::malloc(sizeof(_tree_layout1) * tree.count)); + + uint64_t next_node = 0; + std::function handle_range = + [&](uint64_t low, uint64_t high) -> uint64_t { + const uint64_t count = high - low; + const uint64_t this_index = next_node++; + assert(this_index < tree.count); + + tree.group0_index[this_index].low = tree.prims[low]; + tree.group0_index[this_index].high = tree.prims[high - 1]; + + if (count <= MAX_LEAF_COUNT) { + tree.group0_index[this_index].nPrims = count; + reinterpret_cast<_tree_layout3 *>( + &tree.group0_index[this_index].split0on_nPrims) + ->pOffset = low; + } else { + tree.group0_index[this_index].nPrims = 0; + const uint64_t mid = low + count / 2; + handle_range(low, mid); + const uint64_t right = handle_range(mid, high); + reinterpret_cast<_tree_layout2 *>( + &tree.group0_index[this_index].split0on_nPrims) + ->offset = right - this_index; + } + return this_index; + }; + handle_range(0, tree.pCount); + return tree; +} + +int failures = 0; + +template +void check(const char *what, float lo, float hi, T got, T want) { + if (got != want) { + std::cout << "MISMATCH " << what << " over [" << lo << ", " << hi + << "]: got " << got << ", want " << want << "\n"; + failures++; + } +} + +} // namespace + +int main() { + std::vector values; + for (int i = 0; i < 64; i++) { + // Spread unevenly so subtrees have different extents. + values.push_back(static_cast((i * 37) % 64)); + } + const _tree_layout0 tree = build_tree(values); + + // Ranges chosen to cover an empty result, a result entirely inside one + // subtree, a result spanning several, and one containing everything. + const float bounds[][2] = { + {100.f, 200.f}, {-5.f, -1.f}, {0.f, 0.f}, {10.f, 12.f}, + {0.f, 31.f}, {32.f, 63.f}, {7.f, 45.f}, {-1.f, 64.f}, + }; + + for (const auto &b : bounds) { + const float lo = b[0], hi = b[1]; + + uint64_t want_count = 0; + float want_sum = 0.f; + float want_min = std::numeric_limits::infinity(); + float want_max = -std::numeric_limits::infinity(); + bool want_any = false, want_all = true; + for (float v : values) { + const bool in = lo <= v && v <= hi; + want_any = want_any || in; + want_all = want_all && in; + if (!in) { + continue; + } + want_count++; + want_sum += v; + want_min = std::min(want_min, v); + want_max = std::max(want_max, v); + } + + check("count", lo, hi, q_count(lo, hi, tree), want_count); + check("sum", lo, hi, q_sum(lo, hi, tree), want_sum); + check("any", lo, hi, q_any(lo, hi, tree), want_any); + check("all", lo, hi, q_all(lo, hi, tree), want_all); + + const _option0 got_min = q_min(lo, hi, tree); + const _option0 got_max = q_max(lo, hi, tree); + check("minimum set", lo, hi, got_min.set, want_count != 0); + check("maximum set", lo, hi, got_max.set, want_count != 0); + if (want_count != 0) { + check("minimum", lo, hi, got_min.value, want_min); + check("maximum", lo, hi, got_max.value, want_max); + } + } + + std::cout << (failures == 0 ? "all queries match a linear scan\n" + : "FAILED\n"); + std::free(tree.prims); + std::free(tree.group0_index); + return failures == 0 ? 0 : 1; +} diff --git a/tests/bonsai/correctness/cpp/setops.bonsai b/tests/bonsai/correctness/cpp/setops.bonsai new file mode 100644 index 000000000..4af2f7ecb --- /dev/null +++ b/tests/bonsai/correctness/cpp/setops.bonsai @@ -0,0 +1,39 @@ +//! flags: -p canonicalize -p lower-trees -p lower-externs -p lower-dynamic-sets -p lower-scans -p lower-layouts -p lower-recloops -p lower-foreachs -p lower-yields -p lower-option -p lower-tuples -p unswitch -b cppx -o tests/bonsai/correctness/cpp/setops +//! commands: clang++ -std=c++20 -O2 -c $<>/main_setops.cpp -I. -o $<>/main_setops.o, clang++ -std=c++20 -O2 -c $<>/setops.cpp -I. -o $<>/setops_gen.o, clang++ $<>/main_setops.o $<>/setops_gen.o -o $<>/setops_run, ./$<>/setops_run, rm $<>/setops_run, rm $<>/main_setops.o, rm $<>/setops_gen.o, rm $<>/setops.h, rm $<>/setops.cpp + +// Every query below is checked against a linear scan over the same data +// by main_setops.cpp, so a lowering that traverses the tree wrongly -- +// pruning a subtree it should have visited, or double-counting one it +// folded in from an augmentation -- shows up as a mismatch rather than +// as a golden that merely looks plausible. +extern input : set[f32]; + +func [[export]] q_count(lo : f32, hi : f32) = count(filter(|i : f32| lo <= i && i <= hi, input)); +func [[export]] q_sum(lo : f32, hi : f32) = sum(map(|i : f32| i, filter(|j : f32| lo <= j && j <= hi, input))); +func [[export]] q_min(lo : f32, hi : f32) = minimum(|i : f32| i, filter(|j : f32| lo <= j && j <= hi, input)); +func [[export]] q_max(lo : f32, hi : f32) = maximum(|i : f32| i, filter(|j : f32| lo <= j && j <= hi, input)); +func [[export]] q_any(lo : f32, hi : f32) = any(|i : f32| lo <= i && i <= hi, input); +func [[export]] q_all(lo : f32, hi : f32) = all(|i : f32| lo <= i && i <= hi, input); + +schedule { + tree[[f32]] IntervalTree(low high : f32) with data in [low, high] = + | Interior(left : IntervalTree, right : IntervalTree) + | Leaf(d : array[f32]) with data = d; + + input : IntervalTree; + + layout input { + pCount : u64; + prims : array[f32, pCount]; + count : u64; + group[count] index : u64 { + low : f32; + high : f32; + nPrims : u8; + switch nPrims { + 0 => Interior { offset : u56; left = index + 1; right = index + cast[[u64]](offset); }; + _ => Leaf { pOffset : u56; d = range(prims, cast[[u64]](pOffset), cast[[u64]](nPrims)); }; + }; + }; + }; +} diff --git a/tests/bonsai/correctness/cpp/setops.expect b/tests/bonsai/correctness/cpp/setops.expect new file mode 100644 index 000000000..f328350d3 --- /dev/null +++ b/tests/bonsai/correctness/cpp/setops.expect @@ -0,0 +1,5 @@ +[test] clang++ -std=c++20 -O2 -c tests/bonsai/correctness/cpp/main_setops.cpp -I. -o tests/bonsai/correctness/cpp/main_setops.o +[test] clang++ -std=c++20 -O2 -c tests/bonsai/correctness/cpp/setops.cpp -I. -o tests/bonsai/correctness/cpp/setops_gen.o +[test] clang++ tests/bonsai/correctness/cpp/main_setops.o tests/bonsai/correctness/cpp/setops_gen.o -o tests/bonsai/correctness/cpp/setops_run +[test] ./tests/bonsai/correctness/cpp/setops_run +all queries match a linear scan diff --git a/tests/bonsai/correctness/cpp/vec.expect b/tests/bonsai/correctness/cpp/vec.expect index cc984ecc8..089e48bd8 100644 --- a/tests/bonsai/correctness/cpp/vec.expect +++ b/tests/bonsai/correctness/cpp/vec.expect @@ -1,8 +1,9 @@ [test] clang++ -std=c++20 -O3 -c tests/bonsai/correctness/cpp/main_vec.cpp -I. -o tests/bonsai/correctness/cpp/main_vec.o [test] clang++ tests/bonsai/correctness/cpp/main_vec.o tests/bonsai/correctness/cpp/vec.o -o tests/bonsai/correctness/cpp/vec [test] ./tests/bonsai/correctness/cpp/vec -[-1.000000, 0.000000, 0.000000] +-2 and 5 +[-1.000000, -2.000000, 5.000000] Sphere { c: [4.000000, -8.000000, 16.000000] - r: 203935396562725628553461760.000000 + r: 100.000000 } diff --git a/tests/bonsai/error/generic_fib.expect b/tests/bonsai/error/generic_fib.expect index f12502ac2..210c2d020 100644 --- a/tests/bonsai/error/generic_fib.expect +++ b/tests/bonsai/error/generic_fib.expect @@ -1,7 +1,7 @@ ---CODE--- 1 ---STDERR--- -[internal] Error: src/IR/Expr.cpp:340 +[internal] Error: src/IR/Expr.cpp:341 --> a.type().is_numeric() || a.type().is_bool() BinOp of non-number or boolean types: fib[[T -> T]](i - 1) : T + fib[[T -> T]](i - 2) : T diff --git a/tests/bonsai/error/mismatched_types.expect b/tests/bonsai/error/mismatched_types.expect index 4e70f967c..6f8ad5a06 100644 --- a/tests/bonsai/error/mismatched_types.expect +++ b/tests/bonsai/error/mismatched_types.expect @@ -1,7 +1,7 @@ ---CODE--- 1 ---STDERR--- -[internal] Error: src/IR/Expr.cpp:276 +[internal] Error: src/IR/Expr.cpp:277 --> is_const(a) || is_const(b) Implicit casting of types: x is not the same type as y: i32 versus f32 diff --git a/tests/bonsai/error/string-literal.expect b/tests/bonsai/error/string-literal.expect deleted file mode 100644 index 45d3a764e..000000000 --- a/tests/bonsai/error/string-literal.expect +++ /dev/null @@ -1,7 +0,0 @@ ----CODE--- -1 ----STDERR--- -[internal] Error: src/Parser/Lexer.cpp:261 - ---> program_stream.is_open() -Error: Could not open file tests/bonsai/error/string-literal.bonsai diff --git a/tests/bonsai/error/struct_duplicate_default.expect b/tests/bonsai/error/struct_duplicate_default.expect index d3b6bc9ee..7e4f35365 100644 --- a/tests/bonsai/error/struct_duplicate_default.expect +++ b/tests/bonsai/error/struct_duplicate_default.expect @@ -1,7 +1,7 @@ ---CODE--- 1 ---STDERR--- -[internal] Error: src/Parser/Parser.cpp:451 +[internal] Error: src/Parser/Parser.cpp:469 --> !defaults.contains(field_name) Duplicate default value for field o of element Ray individually marked: f32x3(2f) and group marked: f32x3(1f) diff --git a/tests/bonsai/lower/CMakeLists.txt b/tests/bonsai/lower/CMakeLists.txt index 02c6dec15..98b32aa89 100644 --- a/tests/bonsai/lower/CMakeLists.txt +++ b/tests/bonsai/lower/CMakeLists.txt @@ -35,3 +35,6 @@ add_bonsai_tests( unswitch.bonsai vector-immediate.bonsai ) + +add_subdirectory(geomops) +add_subdirectory(setops) diff --git a/tests/bonsai/lower/bvh.expect b/tests/bonsai/lower/bvh.expect index 804f72513..7d50f0c1c 100644 --- a/tests/bonsai/lower/bvh.expect +++ b/tests/bonsai/lower/bvh.expect @@ -16,11 +16,15 @@ func _traverse_tree0(ray : Ray, triangles : tree[[Triangle]] TriangleBVH) -> set rec(triangles : tree[[Triangle]] TriangleBVH) { match triangles { | Interior(low : f32x3, high : f32x3, left : (const TriangleBVH&), right : (const TriangleBVH&)) with AABB(low, high) -> - if (intersects(ray, build((triangles as Interior).low, (triangles as Interior).high))) { + if (contains(ray, build((triangles as Interior).low, (triangles as Interior).high))) { + scan ((triangles as Interior).left, (triangles as Interior).right) + } else if (intersects(ray, build((triangles as Interior).low, (triangles as Interior).high))) { from ((triangles as Interior).left, (triangles as Interior).right) } | Leaf(low : f32x3, high : f32x3, data : Triangle) with data = data with AABB(low, high) -> - if (intersects(ray, build((triangles as Leaf).low, (triangles as Leaf).high))) { + if (contains(ray, build((triangles as Leaf).low, (triangles as Leaf).high))) { + yield (triangles as Leaf).data + } else if (intersects(ray, build((triangles as Leaf).low, (triangles as Leaf).high))) { if (intersects(ray, (triangles as Leaf).data)) { yield (triangles as Leaf).data } diff --git a/tests/bonsai/lower/geomops/CMakeLists.txt b/tests/bonsai/lower/geomops/CMakeLists.txt new file mode 100644 index 000000000..2edf731cf --- /dev/null +++ b/tests/bonsai/lower/geomops/CMakeLists.txt @@ -0,0 +1,16 @@ +add_bonsai_tests( + LABELS lower + FILES + contains.bonsai + covers.bonsai + disjoint.bonsai + dual-tree.bonsai + equals.bonsai + intersects.bonsai + metrics.bonsai + ordering-le.bonsai + ordering-lt.bonsai + stdlib-resolution.bonsai + touches.bonsai + within.bonsai +) diff --git a/tests/bonsai/lower/geomops/contains.bonsai b/tests/bonsai/lower/geomops/contains.bonsai new file mode 100644 index 000000000..f437f247e --- /dev/null +++ b/tests/bonsai/lower/geomops/contains.bonsai @@ -0,0 +1,26 @@ +//! flags: -p canonicalize -p lower-trees + +element Float = f32; +element vec3f = vector[Float, 3]; + +element AABB { + low, high : vec3f; +} + +element Point { + data : vec3f; +} + +extern points : set[Point]; + +// Algorithm 5 lines 5-6 and Algorithm 6 line 5. +func q_uv(r : AABB) = filter(|p| contains(r, p), points); +func q_vu(r : AABB) = filter(|p| contains(p, r), points); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/contains.expect b/tests/bonsai/lower/geomops/contains.expect new file mode 100644 index 000000000..e4693d96a --- /dev/null +++ b/tests/bonsai/lower/geomops/contains.expect @@ -0,0 +1,60 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type Float = f32 +type Point = struct Point{ data : f32x3 } +type PointBVH = (const PointBVH&) +type vec3f = f32x3 + +extern points : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (contains(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (intersects(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (contains(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (intersects(r, build((points as Leaf).low, (points as Leaf).high))) { + if (contains(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree1(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (contains(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (contains(build((points as Leaf).low, (points as Leaf).high), r)) { + if (contains((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func q_uv(r : AABB) -> set { + return _traverse_tree0(r, points) +} + +func q_vu(r : AABB) -> set { + return _traverse_tree1(r, points) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/geomops/covers.bonsai b/tests/bonsai/lower/geomops/covers.bonsai new file mode 100644 index 000000000..724a0422d --- /dev/null +++ b/tests/bonsai/lower/geomops/covers.bonsai @@ -0,0 +1,26 @@ +//! flags: -p canonicalize -p lower-trees + +element Float = f32; +element vec3f = vector[Float, 3]; + +element AABB { + low, high : vec3f; +} + +element Point { + data : vec3f; +} + +extern points : set[Point]; + +// Algorithm 5 lines 8-9 and Algorithm 6 line 6. +func q_uv(r : AABB) = filter(|p| covers(r, p), points); +func q_vu(r : AABB) = filter(|p| covers(p, r), points); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/covers.expect b/tests/bonsai/lower/geomops/covers.expect new file mode 100644 index 000000000..587f053c7 --- /dev/null +++ b/tests/bonsai/lower/geomops/covers.expect @@ -0,0 +1,60 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type Float = f32 +type Point = struct Point{ data : f32x3 } +type PointBVH = (const PointBVH&) +type vec3f = f32x3 + +extern points : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (covers(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (intersects(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (covers(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (intersects(r, build((points as Leaf).low, (points as Leaf).high))) { + if (covers(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree1(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (covers(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (covers(build((points as Leaf).low, (points as Leaf).high), r)) { + if (covers((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func q_uv(r : AABB) -> set { + return _traverse_tree0(r, points) +} + +func q_vu(r : AABB) -> set { + return _traverse_tree1(r, points) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/geomops/disjoint.bonsai b/tests/bonsai/lower/geomops/disjoint.bonsai new file mode 100644 index 000000000..aff598856 --- /dev/null +++ b/tests/bonsai/lower/geomops/disjoint.bonsai @@ -0,0 +1,26 @@ +//! flags: -p canonicalize -p lower-trees + +element Float = f32; +element vec3f = vector[Float, 3]; + +element AABB { + low, high : vec3f; +} + +element Point { + data : vec3f; +} + +extern points : set[Point]; + +// Algorithm 5 lines 11-12 and Algorithm 6 lines 7-8. Disjointness is refuted only when the query swallows the whole volume. +func q_uv(r : AABB) = filter(|p| disjoint(r, p), points); +func q_vu(r : AABB) = filter(|p| disjoint(p, r), points); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/disjoint.expect b/tests/bonsai/lower/geomops/disjoint.expect new file mode 100644 index 000000000..761d24675 --- /dev/null +++ b/tests/bonsai/lower/geomops/disjoint.expect @@ -0,0 +1,64 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type Float = f32 +type Point = struct Point{ data : f32x3 } +type PointBVH = (const PointBVH&) +type vec3f = f32x3 + +extern points : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (disjoint(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!contains(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (disjoint(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (!contains(r, build((points as Leaf).low, (points as Leaf).high))) { + if (disjoint(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree1(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (disjoint(build((points as Interior).low, (points as Interior).high), r)) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!within(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (disjoint(build((points as Leaf).low, (points as Leaf).high), r)) { + yield (points as Leaf).data + } else if (!within(build((points as Leaf).low, (points as Leaf).high), r)) { + if (disjoint((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func q_uv(r : AABB) -> set { + return _traverse_tree0(r, points) +} + +func q_vu(r : AABB) -> set { + return _traverse_tree1(r, points) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/geomops/dual-tree.bonsai b/tests/bonsai/lower/geomops/dual-tree.bonsai new file mode 100644 index 000000000..924587806 --- /dev/null +++ b/tests/bonsai/lower/geomops/dual-tree.bonsai @@ -0,0 +1,26 @@ +//! flags: -p canonicalize -p lower-trees + +element Float = f32; +element vec3f = vector[Float, 3]; + +element AABB { + low, high : vec3f; +} + +element Point { + data : vec3f; +} + +extern points : set[Point]; +extern points2 : set[Point]; + +func q() = filter(|a : Point, b : Point| intersects(a, b), product(points, points2)); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; + points2 : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/dual-tree.expect b/tests/bonsai/lower/geomops/dual-tree.expect new file mode 100644 index 000000000..23a2a96db --- /dev/null +++ b/tests/bonsai/lower/geomops/dual-tree.expect @@ -0,0 +1,52 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type Float = f32 +type Point = struct Point{ data : f32x3 } +type PointBVH = (const PointBVH&) +type vec3f = f32x3 + +extern points : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) +extern points2 : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(points : tree[[Point]] PointBVH, points2 : tree[[Point]] PointBVH) -> set<(Point, Point)> { + rec(points : tree[[Point]] PointBVH, points2 : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + match points2 { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (intersects(build((points as Interior).low, (points as Interior).high), build((points2 as Interior).low, (points2 as Interior).high))) { + from (((points as Interior).left, (points2 as Interior).left), ((points as Interior).left, (points2 as Interior).right), ((points as Interior).right, (points2 as Interior).left), ((points as Interior).right, (points2 as Interior).right)) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (intersects(build((points as Interior).low, (points as Interior).high), build((points2 as Leaf).low, (points2 as Leaf).high))) { + from (((points as Interior).left, points2), ((points as Interior).right, points2)) + } + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + match points2 { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (intersects(build((points as Leaf).low, (points as Leaf).high), build((points2 as Interior).low, (points2 as Interior).high))) { + from ((points, (points2 as Interior).left), (points, (points2 as Interior).right)) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (intersects(build((points as Leaf).low, (points as Leaf).high), build((points2 as Leaf).low, (points2 as Leaf).high))) { + if (intersects((points as Leaf).data, (points2 as Leaf).data)) { + yield ((points as Leaf).data, (points2 as Leaf).data) + } + } + } + } + } +} + +func q() -> set<(Point, Point)> { + return _traverse_tree0(points, points2) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/geomops/equals.bonsai b/tests/bonsai/lower/geomops/equals.bonsai new file mode 100644 index 000000000..1f5cb8295 --- /dev/null +++ b/tests/bonsai/lower/geomops/equals.bonsai @@ -0,0 +1,26 @@ +//! flags: -p canonicalize -p lower-trees + +element Float = f32; +element vec3f = vector[Float, 3]; + +element AABB { + low, high : vec3f; +} + +element Point { + data : vec3f; +} + +extern points : set[Point]; + +// Algorithm 5 lines 16-17. Equality has no sufficient condition, so there is no always-branch. +func q_uv(r : AABB) = filter(|p| equals(r, p), points); +func q_vu(r : AABB) = filter(|p| equals(p, r), points); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/equals.expect b/tests/bonsai/lower/geomops/equals.expect new file mode 100644 index 000000000..b25db5f90 --- /dev/null +++ b/tests/bonsai/lower/geomops/equals.expect @@ -0,0 +1,56 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type Float = f32 +type Point = struct Point{ data : f32x3 } +type PointBVH = (const PointBVH&) +type vec3f = f32x3 + +extern points : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (within(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (within(r, build((points as Leaf).low, (points as Leaf).high))) { + if (equals(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree1(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (contains(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (contains(build((points as Leaf).low, (points as Leaf).high), r)) { + if (equals((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func q_uv(r : AABB) -> set { + return _traverse_tree0(r, points) +} + +func q_vu(r : AABB) -> set { + return _traverse_tree1(r, points) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/geomops/intersects.bonsai b/tests/bonsai/lower/geomops/intersects.bonsai new file mode 100644 index 000000000..d51bac6a4 --- /dev/null +++ b/tests/bonsai/lower/geomops/intersects.bonsai @@ -0,0 +1,26 @@ +//! flags: -p canonicalize -p lower-trees + +element Float = f32; +element vec3f = vector[Float, 3]; + +element AABB { + low, high : vec3f; +} + +element Point { + data : vec3f; +} + +extern points : set[Point]; + +// Algorithm 5 lines 19-20 and Algorithm 6 lines 10-11. +func q_uv(r : AABB) = filter(|p| intersects(r, p), points); +func q_vu(r : AABB) = filter(|p| intersects(p, r), points); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/intersects.expect b/tests/bonsai/lower/geomops/intersects.expect new file mode 100644 index 000000000..015d6296a --- /dev/null +++ b/tests/bonsai/lower/geomops/intersects.expect @@ -0,0 +1,64 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type Float = f32 +type Point = struct Point{ data : f32x3 } +type PointBVH = (const PointBVH&) +type vec3f = f32x3 + +extern points : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (contains(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (intersects(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (contains(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (intersects(r, build((points as Leaf).low, (points as Leaf).high))) { + if (intersects(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree1(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (within(build((points as Interior).low, (points as Interior).high), r)) { + scan ((points as Interior).left, (points as Interior).right) + } else if (intersects(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (within(build((points as Leaf).low, (points as Leaf).high), r)) { + yield (points as Leaf).data + } else if (intersects(build((points as Leaf).low, (points as Leaf).high), r)) { + if (intersects((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func q_uv(r : AABB) -> set { + return _traverse_tree0(r, points) +} + +func q_vu(r : AABB) -> set { + return _traverse_tree1(r, points) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/geomops/metrics.bonsai b/tests/bonsai/lower/geomops/metrics.bonsai new file mode 100644 index 000000000..95d4e2cfd --- /dev/null +++ b/tests/bonsai/lower/geomops/metrics.bonsai @@ -0,0 +1,25 @@ +//! flags: -p canonicalize -p lower-trees + +element Float = f32; +element vec3f = vector[Float, 3]; + +element AABB { + low, high : vec3f; +} + +element Point { + data : vec3f; +} + +extern points : set[Point]; + +func q_closest(q : Point) = argmin(|p| distmin(q, p), points); +func q_farthest(q : Point) = maximum(|p| distmax(q, p), points); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/metrics.expect b/tests/bonsai/lower/geomops/metrics.expect new file mode 100644 index 000000000..9c1805f7a --- /dev/null +++ b/tests/bonsai/lower/geomops/metrics.expect @@ -0,0 +1,61 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type Float = f32 +type Point = struct Point{ data : f32x3 } +type PointBVH = (const PointBVH&) +type vec3f = f32x3 + +extern points : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(q : Point, points : tree[[Point]] PointBVH) -> Point { + _best0 : mut (f32, Point) := ((f32)inf, build()) + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (distmin(q, build((points as Interior).low, (points as Interior).high)) < _best0[0]) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (distmin(q, build((points as Leaf).low, (points as Leaf).high)) < _best0[0]) { + if (distmin(q, (points as Leaf).data) < _best0[0]) { + _best0 argmin= (distmin(q, (points as Leaf).data), (points as Leaf).data) + } + } + } + } + yield _best0[1] +} + +func _traverse_tree1(q : Point, points : tree[[Point]] PointBVH) -> f32 { + _best0 : mut f32 := -(f32)inf + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (_best0 < distmax(q, build((points as Interior).low, (points as Interior).high))) { + _best0 max= distmin(q, build((points as Interior).low, (points as Interior).high)) + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (_best0 < distmax(q, build((points as Leaf).low, (points as Leaf).high))) { + if (_best0 < distmax(q, (points as Leaf).data)) { + _best0 max= distmax(q, (points as Leaf).data) + } + } + } + } + yield _best0 +} + +func q_closest(q : Point) -> Point { + return _traverse_tree0(q, points) +} + +func q_farthest(q : Point) -> f32 { + return _traverse_tree1(q, points) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/geomops/ordering-le.bonsai b/tests/bonsai/lower/geomops/ordering-le.bonsai new file mode 100644 index 000000000..ed7ea1a5c --- /dev/null +++ b/tests/bonsai/lower/geomops/ordering-le.bonsai @@ -0,0 +1,26 @@ +//! flags: -p canonicalize -p lower-trees + +element Float = f32; +element vec3f = vector[Float, 3]; + +element AABB { + low, high : vec3f; +} + +element Point { + data : vec3f; +} + +extern points : set[Point]; + +func q_x(r : AABB) = filter(|p| lex(r, p), points); +func q_y(r : AABB) = filter(|p| ley(p, r), points); +func q_z(r : AABB) = filter(|p| lez(r, p), points); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/ordering-le.expect b/tests/bonsai/lower/geomops/ordering-le.expect new file mode 100644 index 000000000..3eeaa421d --- /dev/null +++ b/tests/bonsai/lower/geomops/ordering-le.expect @@ -0,0 +1,89 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type Float = f32 +type Point = struct Point{ data : f32x3 } +type PointBVH = (const PointBVH&) +type vec3f = f32x3 + +extern points : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (lex(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!ltx(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (lex(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (!ltx(build((points as Leaf).low, (points as Leaf).high), r)) { + if (lex(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree1(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (ley(build((points as Interior).low, (points as Interior).high), r)) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!lty(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (ley(build((points as Leaf).low, (points as Leaf).high), r)) { + yield (points as Leaf).data + } else if (!lty(r, build((points as Leaf).low, (points as Leaf).high))) { + if (ley((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree2(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (lez(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!ltz(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (lez(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (!ltz(build((points as Leaf).low, (points as Leaf).high), r)) { + if (lez(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func q_x(r : AABB) -> set { + return _traverse_tree0(r, points) +} + +func q_y(r : AABB) -> set { + return _traverse_tree1(r, points) +} + +func q_z(r : AABB) -> set { + return _traverse_tree2(r, points) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/geomops/ordering-lt.bonsai b/tests/bonsai/lower/geomops/ordering-lt.bonsai new file mode 100644 index 000000000..38b20a017 --- /dev/null +++ b/tests/bonsai/lower/geomops/ordering-lt.bonsai @@ -0,0 +1,26 @@ +//! flags: -p canonicalize -p lower-trees + +element Float = f32; +element vec3f = vector[Float, 3]; + +element AABB { + low, high : vec3f; +} + +element Point { + data : vec3f; +} + +extern points : set[Point]; + +func q_x(r : AABB) = filter(|p| ltx(r, p), points); +func q_y(r : AABB) = filter(|p| lty(p, r), points); +func q_z(r : AABB) = filter(|p| ltz(r, p), points); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/ordering-lt.expect b/tests/bonsai/lower/geomops/ordering-lt.expect new file mode 100644 index 000000000..3f29b1dcc --- /dev/null +++ b/tests/bonsai/lower/geomops/ordering-lt.expect @@ -0,0 +1,89 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type Float = f32 +type Point = struct Point{ data : f32x3 } +type PointBVH = (const PointBVH&) +type vec3f = f32x3 + +extern points : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (ltx(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!lex(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (ltx(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (!lex(build((points as Leaf).low, (points as Leaf).high), r)) { + if (ltx(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree1(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (lty(build((points as Interior).low, (points as Interior).high), r)) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!ley(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (lty(build((points as Leaf).low, (points as Leaf).high), r)) { + yield (points as Leaf).data + } else if (!ley(r, build((points as Leaf).low, (points as Leaf).high))) { + if (lty((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree2(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (ltz(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!lez(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (ltz(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (!lez(build((points as Leaf).low, (points as Leaf).high), r)) { + if (ltz(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func q_x(r : AABB) -> set { + return _traverse_tree0(r, points) +} + +func q_y(r : AABB) -> set { + return _traverse_tree1(r, points) +} + +func q_z(r : AABB) -> set { + return _traverse_tree2(r, points) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/geomops/stdlib-resolution.bonsai b/tests/bonsai/lower/geomops/stdlib-resolution.bonsai new file mode 100644 index 000000000..50fd4feb5 --- /dev/null +++ b/tests/bonsai/lower/geomops/stdlib-resolution.bonsai @@ -0,0 +1,50 @@ +//! flags: -p canonicalize -p lower-trees -p lower-geometrics + +// The other tests in this directory stop before lower-geometrics, so they +// check the conditions predicate analysis derives without needing any +// geometry to be implemented. This one goes all the way through, against the +// standard library, and so checks the other half: that every operator the +// analysis emits on a bounding volume has an implementation to resolve to. +// +// Predicate analysis rewrites a query predicate into relations between +// volumes, which are usually not the relation the query was written with -- +// a `covers` filter needs `intersects` on volumes, a `disjoint` filter needs +// `contains`, an ordering predicate needs its strict counterpart with the +// operands swapped, and any metric needs both distmin and distmax. A missing +// overload is an error from lower-geometrics, so this failing means the +// standard library has a hole. +// +// The golden is the whole program, so editing stdlib/topology.bonsai or +// stdlib/distance.bonsai will change it. Re-read the diff before blessing. + +import stdlib/distance; +import stdlib/topology; + +extern points : set[Point]; + +func q_contains(r : AABB) = filter(|p| contains(r, p), points); +func q_covers(r : AABB) = filter(|p| covers(r, p), points); +func q_disjoint(r : AABB) = filter(|p| disjoint(r, p), points); +func q_intersects(r : AABB) = filter(|p| intersects(r, p), points); +func q_within(r : AABB) = filter(|p| within(p, r), points); +func q_touches(r : AABB) = filter(|p| touches(r, p), points); +func q_equals(q : Point) = filter(|p| equals(p, q), points); + +func q_lex(r : AABB) = filter(|p| lex(r, p), points); +func q_ley(r : AABB) = filter(|p| ley(p, r), points); +func q_lez(r : AABB) = filter(|p| lez(r, p), points); +func q_ltx(r : AABB) = filter(|p| ltx(r, p), points); +func q_lty(r : AABB) = filter(|p| lty(p, r), points); +func q_ltz(r : AABB) = filter(|p| ltz(r, p), points); + +func q_closest(q : Point) = argmin(|p| distmin(q, p), points); +func q_nearest(q : Point) = minimum(|p| distmin(q, p), points); +func q_farthest(q : Point) = maximum(|p| distmax(q, p), points); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/stdlib-resolution.expect b/tests/bonsai/lower/geomops/stdlib-resolution.expect new file mode 100644 index 000000000..2a5a3cb38 --- /dev/null +++ b/tests/bonsai/lower/geomops/stdlib-resolution.expect @@ -0,0 +1,864 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type FInterval = struct FInterval{ low : f32; high : f32 } +type Float = f32 +type Half = f16 +type Point = struct Point{ vec : f32x3 } +type PointBVH = (const PointBVH&) +type Ray = struct Ray{ o : f32x3; d : f32x3; tmax : f32 = (f32)inf } +type Sphere = struct Sphere{ center : f32x3; radius : f32 } +type Triangle = struct Triangle{ p0 : f32x3; p1 : f32x3; p2 : f32x3 } +type TriangleIntersection = struct TriangleIntersection{ b0 : f32; b1 : f32; b2 : f32; t : f32 } +type vec3f = f32x3 +type vec3h = f16x3 + +extern points : tree[[struct Point{ vec : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(q : Point, points : tree[[Point]] PointBVH) -> Point { + _best0 : mut (f32, Point) := ((f32)inf, build()) + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (distmin_Point_AABB(q, build((points as Interior).low, (points as Interior).high)) < _best0[0]) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (distmin_Point_AABB(q, build((points as Leaf).low, (points as Leaf).high)) < _best0[0]) { + if (distmin_Point_Point(q, (points as Leaf).data) < _best0[0]) { + _best0 argmin= (distmin_Point_Point(q, (points as Leaf).data), (points as Leaf).data) + } + } + } + } + yield _best0[1] +} + +func _traverse_tree1(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (contains_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (intersects_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (contains_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (intersects_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + if (contains_AABB_Point(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree10(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (ltx_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!lex_AABB_AABB(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (ltx_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (!lex_AABB_AABB(build((points as Leaf).low, (points as Leaf).high), r)) { + if (ltx_AABB_Point(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree11(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (lty_AABB_AABB(build((points as Interior).low, (points as Interior).high), r)) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!ley_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (lty_AABB_AABB(build((points as Leaf).low, (points as Leaf).high), r)) { + yield (points as Leaf).data + } else if (!ley_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + if (lty_Point_AABB((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree12(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (ltz_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!lez_AABB_AABB(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (ltz_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (!lez_AABB_AABB(build((points as Leaf).low, (points as Leaf).high), r)) { + if (ltz_AABB_Point(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree13(q : Point, points : tree[[Point]] PointBVH) -> f32 { + _best1 : mut f32 := (f32)inf + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (distmin_Point_AABB(q, build((points as Interior).low, (points as Interior).high)) < _best1) { + _best1 min= distmax_Point_AABB(q, build((points as Interior).low, (points as Interior).high)) + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (distmin_Point_AABB(q, build((points as Leaf).low, (points as Leaf).high)) < _best1) { + if (distmin_Point_Point(q, (points as Leaf).data) < _best1) { + _best1 min= distmin_Point_Point(q, (points as Leaf).data) + } + } + } + } + yield _best1 +} + +func _traverse_tree14(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (intersects_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (intersects_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + if (touches_AABB_Point(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree15(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (within_AABB_AABB(build((points as Interior).low, (points as Interior).high), r)) { + scan ((points as Interior).left, (points as Interior).right) + } else if (intersects_AABB_AABB(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (within_AABB_AABB(build((points as Leaf).low, (points as Leaf).high), r)) { + yield (points as Leaf).data + } else if (intersects_AABB_AABB(build((points as Leaf).low, (points as Leaf).high), r)) { + if (within_Point_AABB((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree2(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (covers_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (intersects_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (covers_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (intersects_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + if (covers_AABB_Point(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree3(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (disjoint_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!contains_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (disjoint_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (!contains_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + if (disjoint_AABB_Point(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree4(q : Point, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (contains_AABB_Point(build((points as Interior).low, (points as Interior).high), q)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (contains_AABB_Point(build((points as Leaf).low, (points as Leaf).high), q)) { + if (equals_Point_Point((points as Leaf).data, q)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree5(q : Point, points : tree[[Point]] PointBVH) -> f32 { + _best0 : mut f32 := -(f32)inf + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (_best0 < distmax_Point_AABB(q, build((points as Interior).low, (points as Interior).high))) { + _best0 max= distmin_Point_AABB(q, build((points as Interior).low, (points as Interior).high)) + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (_best0 < distmax_Point_AABB(q, build((points as Leaf).low, (points as Leaf).high))) { + if (_best0 < distmax_Point_Point(q, (points as Leaf).data)) { + _best0 max= distmax_Point_Point(q, (points as Leaf).data) + } + } + } + } + yield _best0 +} + +func _traverse_tree6(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (contains_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (intersects_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (contains_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (intersects_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + if (intersects_AABB_Point(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree7(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (lex_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!ltx_AABB_AABB(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (lex_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (!ltx_AABB_AABB(build((points as Leaf).low, (points as Leaf).high), r)) { + if (lex_AABB_Point(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree8(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (ley_AABB_AABB(build((points as Interior).low, (points as Interior).high), r)) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!lty_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (ley_AABB_AABB(build((points as Leaf).low, (points as Leaf).high), r)) { + yield (points as Leaf).data + } else if (!lty_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + if (ley_Point_AABB((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree9(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (lez_AABB_AABB(r, build((points as Interior).low, (points as Interior).high))) { + scan ((points as Interior).left, (points as Interior).right) + } else if (!ltz_AABB_AABB(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (lez_AABB_AABB(r, build((points as Leaf).low, (points as Leaf).high))) { + yield (points as Leaf).data + } else if (!ltz_AABB_AABB(build((points as Leaf).low, (points as Leaf).high), r)) { + if (lez_AABB_Point(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func contains_AABB_AABB(a : AABB, b : AABB) -> bool { + return reduce((a.low <= b.low) & (b.high <= a.high)) +} + +func contains_AABB_Point(a : AABB, p : Point) -> bool { + return reduce((a.low <= p.vec) & (p.vec <= a.high)) +} + +func contains_AABB_Sphere(a : AABB, s : Sphere) -> bool { + return contains_AABB_AABB(a, aabb_of_sphere(s)) +} + +func contains_AABB_Triangle(a : AABB, t : Triangle) -> bool { + return contains_AABB_AABB(a, aabb_of_triangle(t)) +} + +func contains_Point_Point(p : Point, q : Point) -> bool { + return equals_Point_Point(p, q) +} + +func contains_Ray_AABB(r : Ray, a : AABB) -> bool { + return false +} + +func contains_Ray_Sphere(r : Ray, s : Sphere) -> bool { + return false +} + +func contains_Ray_Triangle(r : Ray, t : Triangle) -> bool { + return false +} + +func contains_Sphere_AABB(s : Sphere, a : AABB) -> bool { + let far = max(abs(a.low - s.center), abs(a.high - s.center)) in + return reduce<+>(far * far) <= (s.radius * s.radius) +} + +func covers_AABB_AABB(a : AABB, b : AABB) -> bool { + return contains_AABB_AABB(a, b) +} + +func covers_AABB_Point(a : AABB, p : Point) -> bool { + return contains_AABB_Point(a, p) +} + +func covers_AABB_Sphere(a : AABB, s : Sphere) -> bool { + return contains_AABB_Sphere(a, s) +} + +func covers_AABB_Triangle(a : AABB, t : Triangle) -> bool { + return contains_AABB_Triangle(a, t) +} + +func covers_Ray_AABB(r : Ray, a : AABB) -> bool { + return false +} + +func disjoint_AABB_AABB(a : AABB, b : AABB) -> bool { + return !intersects_AABB_AABB(a, b) +} + +func disjoint_AABB_Point(a : AABB, p : Point) -> bool { + return !contains_AABB_Point(a, p) +} + +func disjoint_AABB_Sphere(a : AABB, s : Sphere) -> bool { + return !intersects_AABB_Sphere(a, s) +} + +func disjoint_AABB_Triangle(a : AABB, t : Triangle) -> bool { + return !intersects_AABB_Triangle(a, t) +} + +func disjoint_Point_AABB(p : Point, a : AABB) -> bool { + return !contains_AABB_Point(a, p) +} + +func disjoint_Point_Point(p : Point, q : Point) -> bool { + return !equals_Point_Point(p, q) +} + +func disjoint_Ray_AABB(r : Ray, a : AABB) -> bool { + return !intersects_Ray_AABB(r, a) +} + +func disjoint_Ray_Sphere(r : Ray, s : Sphere) -> bool { + return !intersects_Ray_Sphere(r, s) +} + +func disjoint_Ray_Triangle(r : Ray, t : Triangle) -> bool { + return !intersects_Ray_Triangle(r, t) +} + +func disjoint_Sphere_AABB(s : Sphere, a : AABB) -> bool { + return !intersects_AABB_Sphere(a, s) +} + +func disjoint_Triangle_AABB(t : Triangle, a : AABB) -> bool { + return !intersects_AABB_Triangle(a, t) +} + +func distmax_AABB_AABB(a : AABB, b : AABB) -> f32 { + let span = max(abs(a.low - b.high), abs(a.high - b.low)) in + return norm(span) +} + +func distmax_AABB_Point(a : AABB, pt : Point) -> f32 { + return distmax_Point_AABB(pt, a) +} + +func distmax_Point_AABB(pt : Point, a : AABB) -> f32 { + let span = max(abs(pt.vec - a.low), abs(pt.vec - a.high)) in + return norm(span) +} + +func distmax_Point_Point(p : Point, q : Point) -> f32 { + return norm(p.vec - q.vec) +} + +func distmax_Point_Sphere(pt : Point, s : Sphere) -> f32 { + return norm(pt.vec - s.center) + s.radius +} + +func distmax_Point_Triangle(pt : Point, tri : Triangle) -> f32 { + let d0 = norm(pt.vec - tri.p0) in + let d1 = norm(pt.vec - tri.p1) in + let d2 = norm(pt.vec - tri.p2) in + return max(max(d0, d1), d2) +} + +func distmax_Ray_AABB(r : Ray, b : AABB) -> f32 { + let interval = intersectsp_ray_aabb(r, b) in + if (cast(interval)) { + let extract = cast(interval) in + return extract.high + } + return (f32)inf +} + +func distmax_Ray_Sphere(r : Ray, s : Sphere) -> f32 { + let interval = intersectsp_ray_sphere(r, s) in + if (cast(interval)) { + let extract = cast(interval) in + return extract.high + } + return (f32)inf +} + +func distmax_Ray_Triangle(ray : Ray, tri : Triangle) -> f32 { + let isect = intersectsp_ray_tri(ray, tri) in + if (cast(isect)) { + let isect_ = cast(isect) in + return isect_.t + } else { + return -(f32)inf + } +} + +func distmin_AABB_AABB(a : AABB, b : AABB) -> f32 { + let gap = max(max(a.low - b.high, b.low - a.high), f32x3(0f)) in + return norm(gap) +} + +func distmin_AABB_Point(a : AABB, pt : Point) -> f32 { + return distmin_Point_AABB(pt, a) +} + +func distmin_Point_AABB(pt : Point, a : AABB) -> f32 { + return sqrt(SqDistPointAABB(pt, a)) +} + +func distmin_Point_Point(p : Point, q : Point) -> f32 { + return norm(p.vec - q.vec) +} + +func distmin_Point_Sphere(pt : Point, s : Sphere) -> f32 { + return max(norm(pt.vec - s.center) - s.radius, 0f) +} + +func distmin_Point_Triangle(p : Point, tri : Triangle) -> f32 { + let pts = closestPointonTriangle(p, tri) in + return norm(p.vec - pts[0].vec) +} + +func distmin_Ray_AABB(r : Ray, b : AABB) -> f32 { + let interval = intersectsp_ray_aabb(r, b) in + if (cast(interval)) { + let extract = cast(interval) in + return extract.low + } + return -(f32)inf +} + +func distmin_Ray_Sphere(r : Ray, s : Sphere) -> f32 { + let interval = intersectsp_ray_sphere(r, s) in + if (cast(interval)) { + let extract = cast(interval) in + return extract.low + } + return -(f32)inf +} + +func distmin_Ray_Triangle(ray : Ray, tri : Triangle) -> f32 { + let isect = intersectsp_ray_tri(ray, tri) in + if (cast(isect)) { + let isect_ = cast(isect) in + return isect_.t + } else { + return (f32)inf + } +} + +func equals_AABB_AABB(a : AABB, b : AABB) -> bool { + return reduce((a.low == b.low) & (a.high == b.high)) +} + +func equals_Point_Point(p : Point, q : Point) -> bool { + return reduce(p.vec == q.vec) +} + +func intersects_AABB_AABB(a : AABB, b : AABB) -> bool { + return reduce(max(a.low, b.low) <= min(a.high, b.high)) +} + +func intersects_AABB_Point(a : AABB, p : Point) -> bool { + return contains_AABB_Point(a, p) +} + +func intersects_AABB_Sphere(a : AABB, s : Sphere) -> bool { + let closest = min(max(s.center, a.low), a.high) in + let d = closest - s.center in + return reduce<+>(d * d) <= (s.radius * s.radius) +} + +func intersects_AABB_Triangle(a : AABB, t : Triangle) -> bool { + let c = f32x3(0.5f) * (a.low + a.high) in + let e = f32x3(0.5f) * (a.high - a.low) in + let v0 = t.p0 - c in + let v1 = t.p1 - c in + let v2 = t.p2 - c in + let f0 = v1 - v0 in + let f1 = v2 - v1 in + let f2 = v0 - v2 in + if (sat_axis_separates(cross_(build(1f, 0f, 0f), f0), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(cross_(build(1f, 0f, 0f), f1), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(cross_(build(1f, 0f, 0f), f2), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(cross_(build(0f, 1f, 0f), f0), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(cross_(build(0f, 1f, 0f), f1), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(cross_(build(0f, 1f, 0f), f2), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(cross_(build(0f, 0f, 1f), f0), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(cross_(build(0f, 0f, 1f), f1), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(cross_(build(0f, 0f, 1f), f2), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(build(1f, 0f, 0f), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(build(0f, 1f, 0f), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(build(0f, 0f, 1f), e, v0, v1, v2)) { + return false + } + if (sat_axis_separates(cross_(f0, f1), e, v0, v1, v2)) { + return false + } + return true +} + +func intersects_Point_AABB(p : Point, a : AABB) -> bool { + return contains_AABB_Point(a, p) +} + +func intersects_Point_Point(p : Point, q : Point) -> bool { + return equals_Point_Point(p, q) +} + +func intersects_Ray_AABB(r : Ray, b : AABB) -> bool { + let interval = intersectsp_ray_aabb(r, b) in + if (cast(interval)) { + let extract = cast(interval) in + return (extract.low < r.tmax) & (0f < extract.high) + } + return false +} + +func intersects_Ray_Sphere(ray : Ray, s : Sphere) -> bool { + let interval = intersectsp_ray_sphere(ray, s) in + if (cast(interval)) { + let extract = cast(interval) in + return (extract.low < ray.tmax) & (0f < extract.high) + } + return false +} + +func intersects_Ray_Triangle(ray : Ray, tri : Triangle) -> bool { + return cast(intersectsp_ray_tri(ray, tri)) +} + +func intersects_Sphere_AABB(s : Sphere, a : AABB) -> bool { + return intersects_AABB_Sphere(a, s) +} + +func intersects_Triangle_AABB(t : Triangle, a : AABB) -> bool { + return intersects_AABB_Triangle(a, t) +} + +func lex_AABB_AABB(a : AABB, b : AABB) -> bool { + return a.high[0u] <= b.low[0u] +} + +func lex_AABB_Point(a : AABB, p : Point) -> bool { + return a.high[0u] <= p.vec[0u] +} + +func lex_Point_AABB(p : Point, a : AABB) -> bool { + return p.vec[0u] <= a.low[0u] +} + +func lex_Point_Point(p : Point, q : Point) -> bool { + return p.vec[0u] <= q.vec[0u] +} + +func ley_AABB_AABB(a : AABB, b : AABB) -> bool { + return a.high[1u] <= b.low[1u] +} + +func ley_AABB_Point(a : AABB, p : Point) -> bool { + return a.high[1u] <= p.vec[1u] +} + +func ley_Point_AABB(p : Point, a : AABB) -> bool { + return p.vec[1u] <= a.low[1u] +} + +func ley_Point_Point(p : Point, q : Point) -> bool { + return p.vec[1u] <= q.vec[1u] +} + +func lez_AABB_AABB(a : AABB, b : AABB) -> bool { + return a.high[2u] <= b.low[2u] +} + +func lez_AABB_Point(a : AABB, p : Point) -> bool { + return a.high[2u] <= p.vec[2u] +} + +func lez_Point_AABB(p : Point, a : AABB) -> bool { + return p.vec[2u] <= a.low[2u] +} + +func lez_Point_Point(p : Point, q : Point) -> bool { + return p.vec[2u] <= q.vec[2u] +} + +func ltx_AABB_AABB(a : AABB, b : AABB) -> bool { + return a.high[0u] < b.low[0u] +} + +func ltx_AABB_Point(a : AABB, p : Point) -> bool { + return a.high[0u] < p.vec[0u] +} + +func ltx_Point_AABB(p : Point, a : AABB) -> bool { + return p.vec[0u] < a.low[0u] +} + +func ltx_Point_Point(p : Point, q : Point) -> bool { + return p.vec[0u] < q.vec[0u] +} + +func lty_AABB_AABB(a : AABB, b : AABB) -> bool { + return a.high[1u] < b.low[1u] +} + +func lty_AABB_Point(a : AABB, p : Point) -> bool { + return a.high[1u] < p.vec[1u] +} + +func lty_Point_AABB(p : Point, a : AABB) -> bool { + return p.vec[1u] < a.low[1u] +} + +func lty_Point_Point(p : Point, q : Point) -> bool { + return p.vec[1u] < q.vec[1u] +} + +func ltz_AABB_AABB(a : AABB, b : AABB) -> bool { + return a.high[2u] < b.low[2u] +} + +func ltz_AABB_Point(a : AABB, p : Point) -> bool { + return a.high[2u] < p.vec[2u] +} + +func ltz_Point_AABB(p : Point, a : AABB) -> bool { + return p.vec[2u] < a.low[2u] +} + +func ltz_Point_Point(p : Point, q : Point) -> bool { + return p.vec[2u] < q.vec[2u] +} + +func q_closest(q : Point) -> Point { + return _traverse_tree0(q, points) +} + +func q_contains(r : AABB) -> set { + return _traverse_tree1(r, points) +} + +func q_covers(r : AABB) -> set { + return _traverse_tree2(r, points) +} + +func q_disjoint(r : AABB) -> set { + return _traverse_tree3(r, points) +} + +func q_equals(q : Point) -> set { + return _traverse_tree4(q, points) +} + +func q_farthest(q : Point) -> f32 { + return _traverse_tree5(q, points) +} + +func q_intersects(r : AABB) -> set { + return _traverse_tree6(r, points) +} + +func q_lex(r : AABB) -> set { + return _traverse_tree7(r, points) +} + +func q_ley(r : AABB) -> set { + return _traverse_tree8(r, points) +} + +func q_lez(r : AABB) -> set { + return _traverse_tree9(r, points) +} + +func q_ltx(r : AABB) -> set { + return _traverse_tree10(r, points) +} + +func q_lty(r : AABB) -> set { + return _traverse_tree11(r, points) +} + +func q_ltz(r : AABB) -> set { + return _traverse_tree12(r, points) +} + +func q_nearest(q : Point) -> f32 { + return _traverse_tree13(q, points) +} + +func q_touches(r : AABB) -> set { + return _traverse_tree14(r, points) +} + +func q_within(r : AABB) -> set { + return _traverse_tree15(r, points) +} + +func touches_AABB_AABB(a : AABB, b : AABB) -> bool { + return intersects_AABB_AABB(a, b) & !reduce(max(a.low, b.low) < min(a.high, b.high)) +} + +func touches_AABB_Point(a : AABB, p : Point) -> bool { + return contains_AABB_Point(a, p) & !reduce((a.low < p.vec) & (p.vec < a.high)) +} + +func within_AABB_AABB(a : AABB, b : AABB) -> bool { + return contains_AABB_AABB(b, a) +} + +func within_AABB_Ray(a : AABB, r : Ray) -> bool { + return false +} + +func within_AABB_Sphere(a : AABB, s : Sphere) -> bool { + return contains_Sphere_AABB(s, a) +} + +func within_Point_AABB(p : Point, a : AABB) -> bool { + return contains_AABB_Point(a, p) +} + +func within_Point_Point(p : Point, q : Point) -> bool { + return equals_Point_Point(p, q) +} + +func within_Sphere_AABB(s : Sphere, a : AABB) -> bool { + return contains_AABB_Sphere(a, s) +} + +func within_Triangle_AABB(t : Triangle, a : AABB) -> bool { + return contains_AABB_Triangle(a, t) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/geomops/touches.bonsai b/tests/bonsai/lower/geomops/touches.bonsai new file mode 100644 index 000000000..0f559660a --- /dev/null +++ b/tests/bonsai/lower/geomops/touches.bonsai @@ -0,0 +1,26 @@ +//! flags: -p canonicalize -p lower-trees + +element Float = f32; +element vec3f = vector[Float, 3]; + +element AABB { + low, high : vec3f; +} + +element Point { + data : vec3f; +} + +extern points : set[Point]; + +// Algorithm 5 lines 22-23. A bounding volume cannot prove a boundary relationship, so there is no always-branch. +func q_uv(r : AABB) = filter(|p| touches(r, p), points); +func q_vu(r : AABB) = filter(|p| touches(p, r), points); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/touches.expect b/tests/bonsai/lower/geomops/touches.expect new file mode 100644 index 000000000..cecd0f254 --- /dev/null +++ b/tests/bonsai/lower/geomops/touches.expect @@ -0,0 +1,56 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type Float = f32 +type Point = struct Point{ data : f32x3 } +type PointBVH = (const PointBVH&) +type vec3f = f32x3 + +extern points : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (intersects(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (intersects(r, build((points as Leaf).low, (points as Leaf).high))) { + if (touches(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree1(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (intersects(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (intersects(build((points as Leaf).low, (points as Leaf).high), r)) { + if (touches((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func q_uv(r : AABB) -> set { + return _traverse_tree0(r, points) +} + +func q_vu(r : AABB) -> set { + return _traverse_tree1(r, points) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/geomops/within.bonsai b/tests/bonsai/lower/geomops/within.bonsai new file mode 100644 index 000000000..6be2457db --- /dev/null +++ b/tests/bonsai/lower/geomops/within.bonsai @@ -0,0 +1,26 @@ +//! flags: -p canonicalize -p lower-trees + +element Float = f32; +element vec3f = vector[Float, 3]; + +element AABB { + low, high : vec3f; +} + +element Point { + data : vec3f; +} + +extern points : set[Point]; + +// Algorithm 5 lines 13-14 and Algorithm 6 line 12. +func q_uv(r : AABB) = filter(|p| within(r, p), points); +func q_vu(r : AABB) = filter(|p| within(p, r), points); + +schedule { + tree[[Point]] PointBVH(low high : vec3f) with AABB(low, high) = + | Interior(left : PointBVH, right : PointBVH) + | Leaf(data : Point) with data = data; + + points : PointBVH; +} diff --git a/tests/bonsai/lower/geomops/within.expect b/tests/bonsai/lower/geomops/within.expect new file mode 100644 index 000000000..20e526bb9 --- /dev/null +++ b/tests/bonsai/lower/geomops/within.expect @@ -0,0 +1,60 @@ +type AABB = struct AABB{ low : f32x3; high : f32x3 } +type Float = f32 +type Point = struct Point{ data : f32x3 } +type PointBVH = (const PointBVH&) +type vec3f = f32x3 + +extern points : tree[[struct Point{ data : f32x3 }]] PointBVH + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) + +func _traverse_tree0(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (within(r, build((points as Interior).low, (points as Interior).high))) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (within(r, build((points as Leaf).low, (points as Leaf).high))) { + if (within(r, (points as Leaf).data)) { + yield (points as Leaf).data + } + } + } + } +} + +func _traverse_tree1(r : AABB, points : tree[[Point]] PointBVH) -> set { + rec(points : tree[[Point]] PointBVH) { + match points { + | Interior(low : f32x3, high : f32x3, left : (const PointBVH&), right : (const PointBVH&)) with AABB(low, high) -> + if (within(build((points as Interior).low, (points as Interior).high), r)) { + scan ((points as Interior).left, (points as Interior).right) + } else if (intersects(build((points as Interior).low, (points as Interior).high), r)) { + from ((points as Interior).left, (points as Interior).right) + } + | Leaf(low : f32x3, high : f32x3, data : Point) with data = data with AABB(low, high) -> + if (within(build((points as Leaf).low, (points as Leaf).high), r)) { + yield (points as Leaf).data + } else if (intersects(build((points as Leaf).low, (points as Leaf).high), r)) { + if (within((points as Leaf).data, r)) { + yield (points as Leaf).data + } + } + } + } +} + +func q_uv(r : AABB) -> set { + return _traverse_tree0(r, points) +} + +func q_vu(r : AABB) -> set { + return _traverse_tree1(r, points) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/CMakeLists.txt b/tests/bonsai/lower/setops/CMakeLists.txt new file mode 100644 index 000000000..e1791f3b0 --- /dev/null +++ b/tests/bonsai/lower/setops/CMakeLists.txt @@ -0,0 +1,20 @@ +add_bonsai_tests( + LABELS lower + FILES + all.bonsai + any.bonsai + argmax.bonsai + argmin.bonsai + count.bonsai + count-augmented.bonsai + filter.bonsai + map.bonsai + maximum.bonsai + maximum-augmented.bonsai + minimum.bonsai + minimum-augmented.bonsai + minimum-unfiltered.bonsai + product.bonsai + reduce.bonsai + sum.bonsai +) diff --git a/tests/bonsai/lower/setops/all.bonsai b/tests/bonsai/lower/setops/all.bonsai new file mode 100644 index 000000000..dcecaeeb9 --- /dev/null +++ b/tests/bonsai/lower/setops/all.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q(lo : i32, hi : i32) = all(|x| (lo <= x.id) & (x.id <= hi), items); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/all.expect b/tests/bonsai/lower/setops/all.expect new file mode 100644 index 000000000..911b3339b --- /dev/null +++ b/tests/bonsai/lower/setops/all.expect @@ -0,0 +1,34 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(lo : i32, hi : i32, items : tree[[Item]] ItemTree) -> bool { + _holds0 : mut bool := true + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if (!((lo <= (items as Interior).idl) && ((items as Interior).idh <= hi))) { + if (_holds0 && ((lo <= (items as Interior).idh) && ((items as Interior).idl <= hi))) { + from ((items as Interior).left, (items as Interior).right) + } else { + _holds0 = false + } + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + _holds0 = _holds0 && ((lo <= (items as Leaf).data.id) & ((items as Leaf).data.id <= hi)) + } + } + yield _holds0 +} + +func q(lo : i32, hi : i32) -> bool { + return _traverse_tree0(lo, hi, items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/any.bonsai b/tests/bonsai/lower/setops/any.bonsai new file mode 100644 index 000000000..a5071adfb --- /dev/null +++ b/tests/bonsai/lower/setops/any.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q(lo : i32, hi : i32) = any(|x| (lo <= x.id) & (x.id <= hi), items); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/any.expect b/tests/bonsai/lower/setops/any.expect new file mode 100644 index 000000000..41adb26dd --- /dev/null +++ b/tests/bonsai/lower/setops/any.expect @@ -0,0 +1,32 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(lo : i32, hi : i32, items : tree[[Item]] ItemTree) -> bool { + _holds0 : mut bool := false + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if ((lo <= (items as Interior).idl) && ((items as Interior).idh <= hi)) { + _holds0 = true + } else if (!_holds0 && ((lo <= (items as Interior).idh) && ((items as Interior).idl <= hi))) { + from ((items as Interior).left, (items as Interior).right) + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + _holds0 = _holds0 || ((lo <= (items as Leaf).data.id) & ((items as Leaf).data.id <= hi)) + } + } + yield _holds0 +} + +func q(lo : i32, hi : i32) -> bool { + return _traverse_tree0(lo, hi, items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/argmax.bonsai b/tests/bonsai/lower/setops/argmax.bonsai new file mode 100644 index 000000000..03b346317 --- /dev/null +++ b/tests/bonsai/lower/setops/argmax.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q(lo : i32, hi : i32) = argmax(|x| x.id, filter(|x| (lo <= x.id) & (x.id <= hi), items)); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/argmax.expect b/tests/bonsai/lower/setops/argmax.expect new file mode 100644 index 000000000..7eac649b0 --- /dev/null +++ b/tests/bonsai/lower/setops/argmax.expect @@ -0,0 +1,34 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(lo : i32, hi : i32, items : tree[[Item]] ItemTree) -> option { + _best0 : mut (i32, Item) := (-(i32)inf, build()) + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if (((lo <= (items as Interior).idh) && ((items as Interior).idl <= hi)) && (_best0[0] < (items as Interior).idh)) { + from ((items as Interior).left, (items as Interior).right) + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + if (((lo <= (items as Leaf).idh) && ((items as Leaf).idl <= hi)) && (_best0[0] < (items as Leaf).idh)) { + if (((lo <= (items as Leaf).data.id) & ((items as Leaf).data.id <= hi)) && (_best0[0] < (items as Leaf).data.id)) { + _best0 argmax= ((items as Leaf).data.id, (items as Leaf).data) + } + } + } + } + yield select(_best0[0] != -(i32)inf, build>(_best0[1]), build>()) +} + +func q(lo : i32, hi : i32) -> option { + return _traverse_tree0(lo, hi, items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/argmin.bonsai b/tests/bonsai/lower/setops/argmin.bonsai new file mode 100644 index 000000000..161385e34 --- /dev/null +++ b/tests/bonsai/lower/setops/argmin.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q(lo : i32, hi : i32) = argmin(|x| x.id, filter(|x| (lo <= x.id) & (x.id <= hi), items)); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/argmin.expect b/tests/bonsai/lower/setops/argmin.expect new file mode 100644 index 000000000..4823d32f4 --- /dev/null +++ b/tests/bonsai/lower/setops/argmin.expect @@ -0,0 +1,34 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(lo : i32, hi : i32, items : tree[[Item]] ItemTree) -> option { + _best0 : mut (i32, Item) := ((i32)inf, build()) + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if (((lo <= (items as Interior).idh) && ((items as Interior).idl <= hi)) && ((items as Interior).idl < _best0[0])) { + from ((items as Interior).left, (items as Interior).right) + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + if (((lo <= (items as Leaf).idh) && ((items as Leaf).idl <= hi)) && ((items as Leaf).idl < _best0[0])) { + if (((lo <= (items as Leaf).data.id) & ((items as Leaf).data.id <= hi)) && ((items as Leaf).data.id < _best0[0])) { + _best0 argmin= ((items as Leaf).data.id, (items as Leaf).data) + } + } + } + } + yield select(_best0[0] != (i32)inf, build>(_best0[1]), build>()) +} + +func q(lo : i32, hi : i32) -> option { + return _traverse_tree0(lo, hi, items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/count-augmented.bonsai b/tests/bonsai/lower/setops/count-augmented.bonsai new file mode 100644 index 000000000..859978b5f --- /dev/null +++ b/tests/bonsai/lower/setops/count-augmented.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q(lo : i32, hi : i32) = count(filter(|x| (lo <= x.id) & (x.id <= hi), items)); + +schedule { + tree[[Item]] ItemTree(idl idh : i32, n : u64) with id in [idl, idh] with count() = n = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/count-augmented.expect b/tests/bonsai/lower/setops/count-augmented.expect new file mode 100644 index 000000000..89e7df410 --- /dev/null +++ b/tests/bonsai/lower/setops/count-augmented.expect @@ -0,0 +1,38 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, n : u64, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] with count() = n + | Leaf(idl : i32, idh : i32, n : u64, data : Item) with data = data with id in [idl, idh] with count() = n + +func _traverse_tree0(lo : i32, hi : i32, items : tree[[Item]] ItemTree) -> u64 { + _acc0 : mut u64 := 0u + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, n : u64, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] with count() = n -> + if ((lo <= (items as Interior).idl) && ((items as Interior).idh <= hi)) { + _acc0 += (items as Interior).n + } else if ((lo <= (items as Interior).idh) && ((items as Interior).idl <= hi)) { + from ((items as Interior).left, (items as Interior).right) + } + | Leaf(idl : i32, idh : i32, n : u64, data : Item) with data = data with id in [idl, idh] with count() = n -> + if ((lo <= (items as Leaf).idl) && ((items as Leaf).idh <= hi)) { + _acc0 += 1u + } else if ((lo <= (items as Leaf).idh) && ((items as Leaf).idl <= hi)) { + if ((lo <= (items as Leaf).data.id) & ((items as Leaf).data.id <= hi)) { + _acc0 += 1u + } + } + } + } + yield _acc0 +} + +func q(lo : i32, hi : i32) -> u64 { + return _traverse_tree0(lo, hi, items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/count.bonsai b/tests/bonsai/lower/setops/count.bonsai new file mode 100644 index 000000000..f2a373def --- /dev/null +++ b/tests/bonsai/lower/setops/count.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q(lo : i32, hi : i32) = count(filter(|x| (lo <= x.id) & (x.id <= hi), items)); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/count.expect b/tests/bonsai/lower/setops/count.expect new file mode 100644 index 000000000..b5d878153 --- /dev/null +++ b/tests/bonsai/lower/setops/count.expect @@ -0,0 +1,38 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(lo : i32, hi : i32, items : tree[[Item]] ItemTree) -> u64 { + _acc0 : mut u64 := 0u + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if ((lo <= (items as Interior).idl) && ((items as Interior).idh <= hi)) { + scan |_x : Item| 1u over ((items as Interior).left, (items as Interior).right) + } else if ((lo <= (items as Interior).idh) && ((items as Interior).idl <= hi)) { + from ((items as Interior).left, (items as Interior).right) + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + if ((lo <= (items as Leaf).idl) && ((items as Leaf).idh <= hi)) { + _acc0 += 1u + } else if ((lo <= (items as Leaf).idh) && ((items as Leaf).idl <= hi)) { + if ((lo <= (items as Leaf).data.id) & ((items as Leaf).data.id <= hi)) { + _acc0 += 1u + } + } + } + } + yield _acc0 +} + +func q(lo : i32, hi : i32) -> u64 { + return _traverse_tree0(lo, hi, items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/filter.bonsai b/tests/bonsai/lower/setops/filter.bonsai new file mode 100644 index 000000000..9f1dc514d --- /dev/null +++ b/tests/bonsai/lower/setops/filter.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q(lo : i32, hi : i32) = filter(|x| (lo <= x.id) & (x.id <= hi), items); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/filter.expect b/tests/bonsai/lower/setops/filter.expect new file mode 100644 index 000000000..dd8a754ef --- /dev/null +++ b/tests/bonsai/lower/setops/filter.expect @@ -0,0 +1,36 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(lo : i32, hi : i32, items : tree[[Item]] ItemTree) -> set { + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if ((lo <= (items as Interior).idl) && ((items as Interior).idh <= hi)) { + scan ((items as Interior).left, (items as Interior).right) + } else if ((lo <= (items as Interior).idh) && ((items as Interior).idl <= hi)) { + from ((items as Interior).left, (items as Interior).right) + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + if ((lo <= (items as Leaf).idl) && ((items as Leaf).idh <= hi)) { + yield (items as Leaf).data + } else if ((lo <= (items as Leaf).idh) && ((items as Leaf).idl <= hi)) { + if ((lo <= (items as Leaf).data.id) & ((items as Leaf).data.id <= hi)) { + yield (items as Leaf).data + } + } + } + } +} + +func q(lo : i32, hi : i32) -> set { + return _traverse_tree0(lo, hi, items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/map.bonsai b/tests/bonsai/lower/setops/map.bonsai new file mode 100644 index 000000000..fb9f5a488 --- /dev/null +++ b/tests/bonsai/lower/setops/map.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q(lo : i32, hi : i32) = map(|x| x.id * 2, filter(|x| (lo <= x.id) & (x.id <= hi), items)); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/map.expect b/tests/bonsai/lower/setops/map.expect new file mode 100644 index 000000000..ff6dec226 --- /dev/null +++ b/tests/bonsai/lower/setops/map.expect @@ -0,0 +1,36 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(lo : i32, hi : i32, items : tree[[Item]] ItemTree) -> set { + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if ((lo <= (items as Interior).idl) && ((items as Interior).idh <= hi)) { + scan |x : Item| x.id * 2 over ((items as Interior).left, (items as Interior).right) + } else if ((lo <= (items as Interior).idh) && ((items as Interior).idl <= hi)) { + from ((items as Interior).left, (items as Interior).right) + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + if ((lo <= (items as Leaf).idl) && ((items as Leaf).idh <= hi)) { + yield (items as Leaf).data.id * 2 + } else if ((lo <= (items as Leaf).idh) && ((items as Leaf).idl <= hi)) { + if ((lo <= (items as Leaf).data.id) & ((items as Leaf).data.id <= hi)) { + yield (items as Leaf).data.id * 2 + } + } + } + } +} + +func q(lo : i32, hi : i32) -> set { + return _traverse_tree0(lo, hi, items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/maximum-augmented.bonsai b/tests/bonsai/lower/setops/maximum-augmented.bonsai new file mode 100644 index 000000000..323eda24d --- /dev/null +++ b/tests/bonsai/lower/setops/maximum-augmented.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q() = maximum(|x| x.id, items); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] with max(id) = idh = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/maximum-augmented.expect b/tests/bonsai/lower/setops/maximum-augmented.expect new file mode 100644 index 000000000..e22a13e98 --- /dev/null +++ b/tests/bonsai/lower/setops/maximum-augmented.expect @@ -0,0 +1,28 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] with max(id) = idh + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] with max(id) = idh + +func _traverse_tree0(items : tree[[Item]] ItemTree) -> i32 { + _best0 : mut i32 := -(i32)inf + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] with max(id) = idh -> + _best0 max= (items as Interior).idh + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] with max(id) = idh -> + _best0 max= (items as Leaf).data.id + } + } + yield _best0 +} + +func q() -> i32 { + return _traverse_tree0(items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/maximum.bonsai b/tests/bonsai/lower/setops/maximum.bonsai new file mode 100644 index 000000000..9456a1473 --- /dev/null +++ b/tests/bonsai/lower/setops/maximum.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q(lo : i32, hi : i32) = maximum(|x| x.id, filter(|x| (lo <= x.id) & (x.id <= hi), items)); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/maximum.expect b/tests/bonsai/lower/setops/maximum.expect new file mode 100644 index 000000000..be2f4d70f --- /dev/null +++ b/tests/bonsai/lower/setops/maximum.expect @@ -0,0 +1,34 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(lo : i32, hi : i32, items : tree[[Item]] ItemTree) -> option { + _best0 : mut i32 := -(i32)inf + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if (((lo <= (items as Interior).idh) && ((items as Interior).idl <= hi)) && (_best0 < (items as Interior).idh)) { + from ((items as Interior).left, (items as Interior).right) + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + if (((lo <= (items as Leaf).idh) && ((items as Leaf).idl <= hi)) && (_best0 < (items as Leaf).idh)) { + if (((lo <= (items as Leaf).data.id) & ((items as Leaf).data.id <= hi)) && (_best0 < (items as Leaf).data.id)) { + _best0 max= (items as Leaf).data.id + } + } + } + } + yield select(_best0 != -(i32)inf, build>(_best0), build>()) +} + +func q(lo : i32, hi : i32) -> option { + return _traverse_tree0(lo, hi, items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/minimum-augmented.bonsai b/tests/bonsai/lower/setops/minimum-augmented.bonsai new file mode 100644 index 000000000..9ac04f140 --- /dev/null +++ b/tests/bonsai/lower/setops/minimum-augmented.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q() = minimum(|x| x.id, items); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] with min(id) = idl = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/minimum-augmented.expect b/tests/bonsai/lower/setops/minimum-augmented.expect new file mode 100644 index 000000000..95ccc5944 --- /dev/null +++ b/tests/bonsai/lower/setops/minimum-augmented.expect @@ -0,0 +1,28 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] with min(id) = idl + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] with min(id) = idl + +func _traverse_tree0(items : tree[[Item]] ItemTree) -> i32 { + _best0 : mut i32 := (i32)inf + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] with min(id) = idl -> + _best0 min= (items as Interior).idl + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] with min(id) = idl -> + _best0 min= (items as Leaf).data.id + } + } + yield _best0 +} + +func q() -> i32 { + return _traverse_tree0(items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/minimum-unfiltered.bonsai b/tests/bonsai/lower/setops/minimum-unfiltered.bonsai new file mode 100644 index 000000000..bd9419840 --- /dev/null +++ b/tests/bonsai/lower/setops/minimum-unfiltered.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q() = minimum(|x| x.id, items); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/minimum-unfiltered.expect b/tests/bonsai/lower/setops/minimum-unfiltered.expect new file mode 100644 index 000000000..f4f3e3041 --- /dev/null +++ b/tests/bonsai/lower/setops/minimum-unfiltered.expect @@ -0,0 +1,31 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(items : tree[[Item]] ItemTree) -> i32 { + _best0 : mut i32 := (i32)inf + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if ((items as Interior).idl < _best0) { + _best0 min= (items as Interior).idh + from ((items as Interior).left, (items as Interior).right) + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + _best0 min= (items as Leaf).data.id + } + } + yield _best0 +} + +func q() -> i32 { + return _traverse_tree0(items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/minimum.bonsai b/tests/bonsai/lower/setops/minimum.bonsai new file mode 100644 index 000000000..91d2d4e5e --- /dev/null +++ b/tests/bonsai/lower/setops/minimum.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q(lo : i32, hi : i32) = minimum(|x| x.id, filter(|x| (lo <= x.id) & (x.id <= hi), items)); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/minimum.expect b/tests/bonsai/lower/setops/minimum.expect new file mode 100644 index 000000000..9f1347abf --- /dev/null +++ b/tests/bonsai/lower/setops/minimum.expect @@ -0,0 +1,34 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(lo : i32, hi : i32, items : tree[[Item]] ItemTree) -> option { + _best0 : mut i32 := (i32)inf + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if (((lo <= (items as Interior).idh) && ((items as Interior).idl <= hi)) && ((items as Interior).idl < _best0)) { + from ((items as Interior).left, (items as Interior).right) + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + if (((lo <= (items as Leaf).idh) && ((items as Leaf).idl <= hi)) && ((items as Leaf).idl < _best0)) { + if (((lo <= (items as Leaf).data.id) & ((items as Leaf).data.id <= hi)) && ((items as Leaf).data.id < _best0)) { + _best0 min= (items as Leaf).data.id + } + } + } + } + yield select(_best0 != (i32)inf, build>(_best0), build>()) +} + +func q(lo : i32, hi : i32) -> option { + return _traverse_tree0(lo, hi, items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/product.bonsai b/tests/bonsai/lower/setops/product.bonsai new file mode 100644 index 000000000..a6a52ace9 --- /dev/null +++ b/tests/bonsai/lower/setops/product.bonsai @@ -0,0 +1,19 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; +extern items2 : set[Item]; + +func q() = filter(|a : Item, b : Item| a.id == b.id, product(items, items2)); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; + items2 : ItemTree; +} diff --git a/tests/bonsai/lower/setops/product.expect b/tests/bonsai/lower/setops/product.expect new file mode 100644 index 000000000..3307420d1 --- /dev/null +++ b/tests/bonsai/lower/setops/product.expect @@ -0,0 +1,57 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] +extern items2 : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(items : tree[[Item]] ItemTree, items2 : tree[[Item]] ItemTree) -> set<(Item, Item)> { + rec(items : tree[[Item]] ItemTree, items2 : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + match items2 { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if ((((items as Interior).idl == (items as Interior).idh) && ((items2 as Interior).idl == (items2 as Interior).idh)) && ((items as Interior).idl == (items2 as Interior).idl)) { + scan (((items as Interior).left, (items2 as Interior).left), ((items as Interior).left, (items2 as Interior).right), ((items as Interior).right, (items2 as Interior).left), ((items as Interior).right, (items2 as Interior).right)) + } else if (((items as Interior).idl <= (items2 as Interior).idh) && ((items2 as Interior).idl <= (items as Interior).idh)) { + from (((items as Interior).left, (items2 as Interior).left), ((items as Interior).left, (items2 as Interior).right), ((items as Interior).right, (items2 as Interior).left), ((items as Interior).right, (items2 as Interior).right)) + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + if ((((items as Interior).idl == (items as Interior).idh) && ((items2 as Leaf).idl == (items2 as Leaf).idh)) && ((items as Interior).idl == (items2 as Leaf).idl)) { + scan (((items as Interior).left, items2), ((items as Interior).right, items2)) + } else if (((items as Interior).idl <= (items2 as Leaf).idh) && ((items2 as Leaf).idl <= (items as Interior).idh)) { + from (((items as Interior).left, items2), ((items as Interior).right, items2)) + } + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + match items2 { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + if ((((items as Leaf).idl == (items as Leaf).idh) && ((items2 as Interior).idl == (items2 as Interior).idh)) && ((items as Leaf).idl == (items2 as Interior).idl)) { + scan ((items, (items2 as Interior).left), (items, (items2 as Interior).right)) + } else if (((items as Leaf).idl <= (items2 as Interior).idh) && ((items2 as Interior).idl <= (items as Leaf).idh)) { + from ((items, (items2 as Interior).left), (items, (items2 as Interior).right)) + } + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + if ((((items as Leaf).idl == (items as Leaf).idh) && ((items2 as Leaf).idl == (items2 as Leaf).idh)) && ((items as Leaf).idl == (items2 as Leaf).idl)) { + yield ((items as Leaf).data, (items2 as Leaf).data) + } else if (((items as Leaf).idl <= (items2 as Leaf).idh) && ((items2 as Leaf).idl <= (items as Leaf).idh)) { + if ((items as Leaf).data.id == (items2 as Leaf).data.id) { + yield ((items as Leaf).data, (items2 as Leaf).data) + } + } + } + } + } +} + +func q() -> set<(Item, Item)> { + return _traverse_tree0(items, items2) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/reduce.bonsai b/tests/bonsai/lower/setops/reduce.bonsai new file mode 100644 index 000000000..6a8f7a111 --- /dev/null +++ b/tests/bonsai/lower/setops/reduce.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q() = reduce(0, |a : i32, b : i32| a + b, map(|x| x.id, items)); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/reduce.expect b/tests/bonsai/lower/setops/reduce.expect new file mode 100644 index 000000000..ee4e10c39 --- /dev/null +++ b/tests/bonsai/lower/setops/reduce.expect @@ -0,0 +1,28 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(items : tree[[Item]] ItemTree) -> i32 { + _acc0 : mut i32 := 0 + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + scan |x : Item| x.id over ((items as Interior).left, (items as Interior).right) + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + _acc0 += (items as Leaf).data.id + } + } + yield _acc0 +} + +func q() -> i32 { + return _traverse_tree0(items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/setops/sum.bonsai b/tests/bonsai/lower/setops/sum.bonsai new file mode 100644 index 000000000..7febfe229 --- /dev/null +++ b/tests/bonsai/lower/setops/sum.bonsai @@ -0,0 +1,17 @@ +//! flags: -p canonicalize -p lower-trees + +element Item { + id : i32; +} + +extern items : set[Item]; + +func q() = sum(map(|x| x.id, items)); + +schedule { + tree[[Item]] ItemTree(idl idh : i32) with id in [idl, idh] = + | Interior(left : ItemTree, right : ItemTree) + | Leaf(data : Item) with data = data; + + items : ItemTree; +} diff --git a/tests/bonsai/lower/setops/sum.expect b/tests/bonsai/lower/setops/sum.expect new file mode 100644 index 000000000..ee4e10c39 --- /dev/null +++ b/tests/bonsai/lower/setops/sum.expect @@ -0,0 +1,28 @@ +type Item = struct Item{ id : i32 } +type ItemTree = (const ItemTree&) + +extern items : tree[[struct Item{ id : i32 }]] ItemTree + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] + +func _traverse_tree0(items : tree[[Item]] ItemTree) -> i32 { + _acc0 : mut i32 := 0 + rec(items : tree[[Item]] ItemTree) { + match items { + | Interior(idl : i32, idh : i32, left : (const ItemTree&), right : (const ItemTree&)) with id in [idl, idh] -> + scan |x : Item| x.id over ((items as Interior).left, (items as Interior).right) + | Leaf(idl : i32, idh : i32, data : Item) with data = data with id in [idl, idh] -> + _acc0 += (items as Leaf).data.id + } + } + yield _acc0 +} + +func q() -> i32 { + return _traverse_tree0(items) +} + + +schedule host { + +} diff --git a/tests/bonsai/lower/trace.expect b/tests/bonsai/lower/trace.expect index 3f6f421bb..3e4147c46 100644 --- a/tests/bonsai/lower/trace.expect +++ b/tests/bonsai/lower/trace.expect @@ -66,6 +66,35 @@ func _unexported_trace(ray : (Ray*), triangles : (_tree_layout0*)) -> _option0 { return _traverse_tree0(ray, triangles) } +func distmax_AABB_AABB(a : (AABB*), b : (AABB*)) -> f32 { + let span = max(abs((*a).low - (*b).high), abs((*a).high - (*b).low)) in + return norm(span) +} + +func distmax_AABB_Point(a : (AABB*), pt : (Point*)) -> f32 { + return distmax_Point_AABB(pt, a) +} + +func distmax_Point_AABB(pt : (Point*), a : (AABB*)) -> f32 { + let span = max(abs((*pt).vec - (*a).low), abs((*pt).vec - (*a).high)) in + return norm(span) +} + +func distmax_Point_Point(p : (Point*), q : (Point*)) -> f32 { + return norm((*p).vec - (*q).vec) +} + +func distmax_Point_Sphere(pt : (Point*), s : (Sphere*)) -> f32 { + return norm((*pt).vec - (*s).center) + (*s).radius +} + +func distmax_Point_Triangle(pt : (Point*), tri : (Triangle*)) -> f32 { + let d0 = norm((*pt).vec - (*tri).p0) in + let d1 = norm((*pt).vec - (*tri).p1) in + let d2 = norm((*pt).vec - (*tri).p2) in + return max(max(d0, d1), d2) +} + func distmax_Ray_AABB(r : (Ray*), b : (AABB*)) -> f32 { let interval = intersectsp_ray_aabb(r, b) in if (interval.set) { @@ -94,10 +123,27 @@ func distmax_Ray_Triangle(ray : (Ray*), tri : (Triangle*)) -> f32 { } } +func distmin_AABB_AABB(a : (AABB*), b : (AABB*)) -> f32 { + let gap = max(max((*a).low - (*b).high, (*b).low - (*a).high), f32x3(0f)) in + return norm(gap) +} + +func distmin_AABB_Point(a : (AABB*), pt : (Point*)) -> f32 { + return sqrt(SqDistPointAABB(pt, a)) +} + func distmin_Point_AABB(pt : (Point*), a : (AABB*)) -> f32 { return sqrt(SqDistPointAABB(pt, a)) } +func distmin_Point_Point(p : (Point*), q : (Point*)) -> f32 { + return norm((*p).vec - (*q).vec) +} + +func distmin_Point_Sphere(pt : (Point*), s : (Sphere*)) -> f32 { + return max(norm((*pt).vec - (*s).center) - (*s).radius, 0f) +} + func distmin_Point_Triangle(p : (Point*), tri : (Triangle*)) -> f32 { let pts = closestPointonTriangle(p, tri) in return norm((*p).vec - pts._field0.vec) diff --git a/tests/bonsai/lower/trace_aosoa.expect b/tests/bonsai/lower/trace_aosoa.expect index a25edac90..d9ea380e0 100644 --- a/tests/bonsai/lower/trace_aosoa.expect +++ b/tests/bonsai/lower/trace_aosoa.expect @@ -54,6 +54,35 @@ func _unexported_trace(ray : (Ray*), ts : (_tree_layout0*)) -> _option0 { return _traverse_tree0(ray, ts) } +func distmax_AABB_AABB(a : (AABB*), b : (AABB*)) -> f32 { + let span = max(abs((*a).low - (*b).high), abs((*a).high - (*b).low)) in + return norm(span) +} + +func distmax_AABB_Point(a : (AABB*), pt : (Point*)) -> f32 { + return distmax_Point_AABB(pt, a) +} + +func distmax_Point_AABB(pt : (Point*), a : (AABB*)) -> f32 { + let span = max(abs((*pt).vec - (*a).low), abs((*pt).vec - (*a).high)) in + return norm(span) +} + +func distmax_Point_Point(p : (Point*), q : (Point*)) -> f32 { + return norm((*p).vec - (*q).vec) +} + +func distmax_Point_Sphere(pt : (Point*), s : (Sphere*)) -> f32 { + return norm((*pt).vec - (*s).center) + (*s).radius +} + +func distmax_Point_Triangle(pt : (Point*), tri : (Triangle*)) -> f32 { + let d0 = norm((*pt).vec - (*tri).p0) in + let d1 = norm((*pt).vec - (*tri).p1) in + let d2 = norm((*pt).vec - (*tri).p2) in + return max(max(d0, d1), d2) +} + func distmax_Ray_AABB(r : (Ray*), b : (AABB*)) -> f32 { let interval = intersectsp_ray_aabb(r, b) in if (interval.set) { @@ -82,10 +111,27 @@ func distmax_Ray_Triangle(ray : (Ray*), tri : (Triangle*)) -> f32 { } } +func distmin_AABB_AABB(a : (AABB*), b : (AABB*)) -> f32 { + let gap = max(max((*a).low - (*b).high, (*b).low - (*a).high), f32x3(0f)) in + return norm(gap) +} + +func distmin_AABB_Point(a : (AABB*), pt : (Point*)) -> f32 { + return sqrt(SqDistPointAABB(pt, a)) +} + func distmin_Point_AABB(pt : (Point*), a : (AABB*)) -> f32 { return sqrt(SqDistPointAABB(pt, a)) } +func distmin_Point_Point(p : (Point*), q : (Point*)) -> f32 { + return norm((*p).vec - (*q).vec) +} + +func distmin_Point_Sphere(pt : (Point*), s : (Sphere*)) -> f32 { + return max(norm((*pt).vec - (*s).center) - (*s).radius, 0f) +} + func distmin_Point_Triangle(p : (Point*), tri : (Triangle*)) -> f32 { let pts = closestPointonTriangle(p, tri) in return norm((*p).vec - pts._field0.vec) From 2a7c6a3319ae3da9e07bfb44ad993856d9288571 Mon Sep 17 00:00:00 2001 From: Alexander J Root Date: Sun, 30 Aug 2026 10:48:21 -0700 Subject: [PATCH 3/7] Fix error handling printing for assertion failures (#210) * Stop ScansToCalls dereferencing its moved-from constructor parameter --- CMakeLists.txt | 12 ++++++++ include/Error.h | 45 +++++++++++++++++++++-------- src/Lower/Scans.cpp | 11 ++++--- src/Lower/TypeInference.cpp | 8 +++-- tests/bonsai/error/no-return.expect | 4 +-- 5 files changed, 60 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b6f5f2767..e5fa930a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,18 @@ add_library(bonsai_library STATIC) add_library(bonsai::library ALIAS bonsai_library) target_compile_features(bonsai_library PUBLIC cxx_std_20) + +# include/Error.h trims this prefix off __FILE__, so that the "[internal] +# Error: :" lines checked in under tests/bonsai/error/*.expect do +# not depend on the name of the directory the repository was cloned into. +# PUBLIC because bonsai_compiler and bonsai_test_runner include Error.h too; +# BUILD_INTERFACE keeps a local absolute path out of anything installed. +target_compile_definitions( + bonsai_library + PUBLIC + "$" +) + target_link_libraries(bonsai_library PUBLIC ModernLLVM::LLVM) target_sources( diff --git a/include/Error.h b/include/Error.h index d6eae8450..24892c5a0 100644 --- a/include/Error.h +++ b/include/Error.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace bonsai { @@ -11,22 +12,42 @@ class Error final : public std::runtime_error { using std::runtime_error::runtime_error; }; +namespace detail { + +// __FILE__ is an absolute path into whatever directory the repository was +// cloned into. Trim it back to a repository-relative path, so that the +// "[internal] Error: :" lines that tests/bonsai/error/*.expect +// compares byte-for-byte do not depend on where the checkout lives. +inline std::string source_relative_path(const char *file) { + std::string f(file); + +#ifdef BONSAI_SOURCE_DIR + // The repository root, injected by the top-level CMakeLists.txt. + constexpr std::string_view root = BONSAI_SOURCE_DIR; + if (!root.empty() && f.size() > root.size() && + f.compare(0, root.size(), root) == 0 && f[root.size()] == '/') { + return f.substr(root.size() + 1); + } +#endif + + // For a translation unit built outside our CMake, or one whose __FILE__ + // has been rewritten (-ffile-prefix-map), fall back to the last top-level + // source directory. Printing the whole path is the honest last resort. + for (const std::string_view dir : {"/src/", "/include/", "/tests/"}) { + if (const size_t pos = f.rfind(dir); pos != std::string::npos) { + return f.substr(pos + 1); + } + } + return f; +} + +} // namespace detail + class ErrorReport { public: ErrorReport(const char *cond_str, const char *file, size_t line) { - // Print the file path proceeding the root directory (inclusive). - constexpr std::string_view ROOT_DIRECTORY = "bonsai"; - std::string f(file); - - // TODO(cgyurgyik): Fix this hack. - // Finds the last occurrence of `bonsai` to conform with Github Actions, - // where both the WORKSPACE and the REPOSITORY are named `bonsai`. - if (size_t pos = f.rfind(ROOT_DIRECTORY); pos != std::string::npos) { - f = f.substr(pos + ROOT_DIRECTORY.length() + 1); - } - stream << "[internal] Error: "; - stream << f << ":" << line << "\n"; + stream << detail::source_relative_path(file) << ":" << line << "\n"; if (cond_str) { stream << "\n--> " << cond_str << "\n"; } diff --git a/src/Lower/Scans.cpp b/src/Lower/Scans.cpp index b614a30a1..ae68e4cf8 100644 --- a/src/Lower/Scans.cpp +++ b/src/Lower/Scans.cpp @@ -75,10 +75,13 @@ build_scan_func(const std::vector &args, const std::string &func_name, std::optional op; Expr map_func; - ScansToCalls(std::shared_ptr func, - std::optional op, Expr map_func) - : func(std::move(func)), op(std::move(op)), - map_func(std::move(map_func)) { + // The parameters deliberately do not share the members' names. The + // body reads `func` after the mem-initializer has moved from it, so a + // parameter named `func` would shadow the member and be null there. + ScansToCalls(std::shared_ptr scan_func, + std::optional agg_op, Expr map) + : func(std::move(scan_func)), op(std::move(agg_op)), + map_func(std::move(map)) { write_expr = Var::make(func->args.back().type, func->args.back().name); write_loc = diff --git a/src/Lower/TypeInference.cpp b/src/Lower/TypeInference.cpp index 98da055d1..2f779c3ed 100644 --- a/src/Lower/TypeInference.cpp +++ b/src/Lower/TypeInference.cpp @@ -506,8 +506,12 @@ infer_types(const std::shared_ptr &fnotypes, coerce_return_types(std::move(ftypes->body), ftypes->ret_type); ftypes->body = infer_build_types(std::move(ftypes->body), ftypes->ret_type); - internal_assert(ftypes->ret_type.is() || - always_returns(ftypes->body)) + // Keep the internal_assert invocation on one line: GCC reports __LINE__ as + // the line the macro name is on and Clang as the line the closing paren is + // on, and tests/bonsai/error/no-return.expect records that number. + const bool returns_on_all_paths = + ftypes->ret_type.is() || always_returns(ftypes->body); + internal_assert(returns_on_all_paths) << "Function: " << ftypes->name << " does not return in all code paths."; diff --git a/tests/bonsai/error/no-return.expect b/tests/bonsai/error/no-return.expect index 277b8a2f1..d55276822 100644 --- a/tests/bonsai/error/no-return.expect +++ b/tests/bonsai/error/no-return.expect @@ -1,7 +1,7 @@ ---CODE--- 1 ---STDERR--- -[internal] Error: src/Lower/TypeInference.cpp:510 +[internal] Error: src/Lower/TypeInference.cpp:514 ---> ftypes->ret_type.is() || always_returns(ftypes->body) +--> returns_on_all_paths Function: foo does not return in all code paths. From e6c5cf19feac07a4510e32ff8efda072b6dc11bf Mon Sep 17 00:00:00 2001 From: Alexander J Root Date: Sun, 30 Aug 2026 11:39:49 -0700 Subject: [PATCH 4/7] fix arch-specific codegen diff tests (#211) * Stop the LLVM backend building a 32-bit target machine make_target_machine computed the host triple into target_triple, used it to look up the llvm::Target, and then passed module.getTargetTriple() to createTargetMachine. The module only gets a triple further down, and only for the ASM and CPP backends, so that argument was the empty string every time. On an x86 host an empty triple selects a generic i386 subtarget. Two things followed. The data layout the CPP backend installs from that machine has 32-bit pointers, so the optimizer laid structs out with 4-byte pointers while the object itself was emitted 64-bit by the separate, correctly tripled target machine in CPP.cpp -- a real miscompile, not a formatting difference. And its TargetTransformInfo reports zero vector registers, so the loop and SLP vectorizers silently did nothing at -O3. AArch64 keeps 64-bit pointers and NEON even for an unknown triple, so none of this showed on the macOS/arm64 machines that CI and the goldens come from. Pass target_triple, which is what the surrounding code already assumes. backends/cpp/not-exported, backends/llvm/exported-function and backends/llvm/map-func have been recording the correct vectorized output all along and now match again. * Emit the grow-capacity select's operands in a defined order CreateSelect was called with CreateICmpEQ and CreateMul as two of its arguments. Each appends an instruction to the block, and the order in which a call's arguments are evaluated is unspecified, so which of the two landed first depended on the compiler that built Bonsai: GCC evaluates right to left and emitted the mul first, Clang left to right and emitted the icmp first. The generated code was correct either way, but the IR a given Bonsai binary prints should not depend on how that binary was built. backends/llvm/dynamic-array records the Clang order. * Reserve a fixed 64 bytes for a dynamic array's mutex The field was sized with the host's sizeof(pthread_mutex_t): 40 bytes on glibc/x86-64, 48 on glibc/aarch64, 64 on Apple libc. That put the host's libc into the layout of a generated struct. apps/cd/cpu/fcl/main.h mirrors this struct by hand and declares the field as a 64-byte vector, so on anything but macOS the generated code and that driver disagreed about the struct's size -- an ABI mismatch, not just golden churn. Reserve the upper bound instead, with a static_assert for a platform that would need more. Over-reserving costs a few bytes per array. * Let a golden be overridden per target architecture Three backends goldens differ between AArch64 and x86-64 for reasons that are not the compiler's doing: LLVM 19 SLP vectorizes a pair of i32 operations on AArch64 and not on x86-64 (backends/cpp/struct, backends/llvm/mut-basic), and SimplifyCFG names one select %spec.select there and %common.ret.op here (backends/llvm/index). No change to Bonsai can make one host produce the other's output. Look for ..expect next to the base golden and prefer it when it exists. The base files are untouched, so the macOS CI job keeps comparing against exactly what it does today, and the x86-64 overrides added here are what a correct compiler emits on this architecture. check_output.cmake swaps EXPECT before both the diff and the update path, so BONSAI_UPDATE_EXPECT re-blesses an override where one exists rather than overwriting the golden the other architecture depends on. The README says when to reach for this and when not to. It is worth being strict about: the other three differences in this area all looked like the same thing and were real bugs. ctest is now 306/306 on Linux/x86-64. --- src/CodeGen/CodeGen_LLVM.cpp | 19 +++++-- src/Lower/DynamicArrays.cpp | 25 +++++++-- tests/README.md | 22 ++++++++ tests/bonsai/CMakeLists.txt | 13 +++++ .../bonsai/backends/cpp/struct.x86_64.expect | 52 +++++++++++++++++++ .../bonsai/backends/llvm/index.x86_64.expect | 47 +++++++++++++++++ .../backends/llvm/mut-basic.x86_64.expect | 50 ++++++++++++++++++ tests/bonsai/check_output.cmake | 9 ++++ 8 files changed, 229 insertions(+), 8 deletions(-) create mode 100644 tests/bonsai/backends/cpp/struct.x86_64.expect create mode 100644 tests/bonsai/backends/llvm/index.x86_64.expect create mode 100644 tests/bonsai/backends/llvm/mut-basic.x86_64.expect diff --git a/src/CodeGen/CodeGen_LLVM.cpp b/src/CodeGen/CodeGen_LLVM.cpp index 166e4766a..5b74ce746 100644 --- a/src/CodeGen/CodeGen_LLVM.cpp +++ b/src/CodeGen/CodeGen_LLVM.cpp @@ -116,7 +116,13 @@ CodeGen_LLVM::make_target_machine(llvm::Module &module, // use_large_code_model); auto *tm = llvm_target->createTargetMachine( - module.getTargetTriple(), + // Not module.getTargetTriple(): the module only gets a triple below, + // and only for ASM/CPP, so this was the empty string for every + // backend. On an x86 host that yields a generic *i386* machine -- + // 32-bit pointers in the data layout the CPP backend then installs, + // and a TTI reporting zero vector registers, which silently disables + // vectorization everywhere. + target_triple, /*CPU target=*/"", /*Features=*/"", target_options, use_pic ? llvm::Reloc::PIC_ : llvm::Reloc::Static, use_large_code_model ? llvm::CodeModel::Large : llvm::CodeModel::Small, @@ -2284,10 +2290,15 @@ void CodeGen_LLVM::ensure_capacity( builder->CreateCondBr(condition, grow_bb, unlock_bb); builder->SetInsertPoint(grow_bb); - // Handle the zero capacity case. + // Handle the zero capacity case. Emit the two operands in separate + // statements: as arguments to one call their evaluation order is + // unspecified, and each one appends an instruction, so the order they + // appear in the IR would otherwise depend on the compiler that built + // Bonsai -- GCC evaluates right to left, Clang left to right. + llvm::Value *capacity_is_zero = builder->CreateICmpEQ(capacity, zero); + llvm::Value *doubled_capacity = builder->CreateMul(capacity, two); llvm::Value *new_capacity = builder->CreateSelect( - builder->CreateICmpEQ(capacity, zero), one, - builder->CreateMul(capacity, two), base_n + ".new-capacity"); + capacity_is_zero, one, doubled_capacity, base_n + ".new-capacity"); const llvm::DataLayout &layout = module->getDataLayout(); llvm::Type *i8_t = llvm::Type::getInt8Ty(*context); llvm::Type *s_t = layout.getIntPtrType(*context); diff --git a/src/Lower/DynamicArrays.cpp b/src/Lower/DynamicArrays.cpp index 4d571b89d..12cbe4761 100644 --- a/src/Lower/DynamicArrays.cpp +++ b/src/Lower/DynamicArrays.cpp @@ -8,6 +8,8 @@ #include "Error.h" #include "Utils.h" +#include + #include #include #include @@ -17,6 +19,12 @@ namespace bonsai { namespace lower { namespace { + +// Bytes reserved in a dynamic array for its pthread_mutex_t. Fixed rather +// than sizeof(pthread_mutex_t) so the generated layout is the same on every +// host; see build_struct below. +constexpr size_t MUTEX_BYTES = 64; + std::string unique_dynamic_array_name() { static size_t counter = 0; return "__dyn_array" + std::to_string(counter++); @@ -37,10 +45,19 @@ struct DynamicArraysToStructs : public ir::Mutator { std::string name = unique_dynamic_array_name(); fields.push_back(ir::TypedVar("size", type)); fields.push_back(ir::TypedVar("capacity", node->capacity.type())); - // TODO(cgyurgyik): This invalidates cross-compilation. - fields.push_back( - ir::TypedVar("mutex", ir::Vector_t::make(ir::UInt_t::make(8), - sizeof(pthread_mutex_t)))); + // Opaque storage for the pthread_mutex_t that the backends initialise + // and lock through this field. Reserve a fixed upper bound rather than + // the host's own sizeof: that is 40 bytes on glibc/x86-64, 48 on + // glibc/aarch64 and 64 on Apple libc, and baking it in makes the + // struct's layout differ between hosts. Hand-written drivers mirror + // this struct -- apps/cd/cpu/fcl/main.h declares the field as a + // 64-byte vector -- so a host-dependent size is an ABI mismatch, not + // just golden churn. Over-reserving is harmless; under-reserving is + // not, hence the assertion. + static_assert(sizeof(pthread_mutex_t) <= MUTEX_BYTES, + "reserved mutex storage is too small for this platform"); + fields.push_back(ir::TypedVar( + "mutex", ir::Vector_t::make(ir::UInt_t::make(8), MUTEX_BYTES))); defaults["size"] = make_zero(type); ir::Type new_struct = diff --git a/tests/README.md b/tests/README.md index 6bb9cdd5c..5632b3850 100644 --- a/tests/README.md +++ b/tests/README.md @@ -15,5 +15,27 @@ To update the golden outputs during a test run, set the environment variable $ BONSAI_UPDATE_EXPECT=1 ctest -L llvm ``` +### Architecture-specific goldens + +A test's expected output normally lives in `.expect`. A few of the +`backends` goldens record what LLVM's optimizer produced, and that genuinely +differs between target architectures -- LLVM 19 SLP vectorizes a pair of `i32` +operations on AArch64 but not on x86-64, and SimplifyCFG names the same select +`%spec.select` on one and `%common.ret.op` on the other. + +Where that happens, an optional `..expect` overrides the base +golden on that architecture, with `` being `CMAKE_SYSTEM_PROCESSOR` +lower-cased (`x86_64`, `arm64`). The base file stays whatever CI blesses, so +adding an override never disturbs the other platform. `BONSAI_UPDATE_EXPECT` +re-blesses the override where one exists and the base golden where one does +not. + +Only add an override once you have confirmed the difference is the optimizer +targeting a different architecture, and not the compiler being wrong. Three +differences that looked like this turned out to be real bugs: a target machine +built from an empty triple, a struct field sized with the host's +`sizeof(pthread_mutex_t)`, and two `IRBuilder` calls whose evaluation order was +unspecified. + Also consult the official CTest documentation: https://cmake.org/cmake/help/latest/manual/ctest.1.html diff --git a/tests/bonsai/CMakeLists.txt b/tests/bonsai/CMakeLists.txt index 34fe6aac3..5044bc51e 100644 --- a/tests/bonsai/CMakeLists.txt +++ b/tests/bonsai/CMakeLists.txt @@ -1,15 +1,27 @@ ## # CTest implementation +# A handful of the backends goldens record what LLVM's optimizer produced, and +# that legitimately differs between target architectures: LLVM 19 SLP +# vectorizes a pair of i32 operations on AArch64 but not on x86-64, and +# SimplifyCFG names the same select %spec.select on one and %common.ret.op on +# the other. Where that happens, an optional ..expect overrides the +# base golden on that architecture, and the base file stays whatever the +# machine CI runs on blesses. +string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" BONSAI_TEST_ARCH) + function(add_bonsai_tests) cmake_parse_arguments(PARSE_ARGV 0 ARG "" "" "FILES;LABELS") foreach (file IN LISTS ARG_FILES) cmake_path(SET input "${file}") cmake_path(REPLACE_EXTENSION input "expect" OUTPUT_VARIABLE expect) cmake_path(REPLACE_EXTENSION input "actual" OUTPUT_VARIABLE actual) + cmake_path(REPLACE_EXTENSION input "${BONSAI_TEST_ARCH}.expect" + OUTPUT_VARIABLE expect_arch) cmake_path(ABSOLUTE_PATH input) cmake_path(ABSOLUTE_PATH expect) + cmake_path(ABSOLUTE_PATH expect_arch) cmake_path(ABSOLUTE_PATH actual BASE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") cmake_path(REMOVE_FILENAME actual OUTPUT_VARIABLE actual_dir) @@ -37,6 +49,7 @@ function(add_bonsai_tests) NAME "${check_test}" COMMAND "${CMAKE_COMMAND}" "-DACTUAL=${actual}" "-DEXPECT=${expect}" + "-DEXPECT_ARCH=${expect_arch}" -P "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/check_output.cmake" ) set_tests_properties( diff --git a/tests/bonsai/backends/cpp/struct.x86_64.expect b/tests/bonsai/backends/cpp/struct.x86_64.expect new file mode 100644 index 000000000..60cca909a --- /dev/null +++ b/tests/bonsai/backends/cpp/struct.x86_64.expect @@ -0,0 +1,52 @@ +// Bonsai Header +#pragma once + +#include +#include "runtime/bonsai_cpp.h" + + +extern "C" { +struct Position { + int32_t x; + int32_t y = 42; +}; + +void add(Position& _ret0, const Position& i, const Position& j); +} + +------------------------------------------ + +; LLVM Module +; ModuleID = 'bonsai_module' +source_filename = "bonsai_module" + +%struct.Position = type { i32, i32 } + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) +define %struct.Position @_unexported_add(ptr nocapture noundef nonnull readonly %i, ptr nocapture noundef nonnull readonly %j) local_unnamed_addr #0 { +_unexported_add_entry: + %deref_temp.unpack = load i32, ptr %i, align 4 + %deref_temp.elt6 = getelementptr inbounds i8, ptr %i, i64 4 + %deref_temp.unpack7 = load i32, ptr %deref_temp.elt6, align 4 + %deref_temp1.unpack = load i32, ptr %j, align 4 + %deref_temp1.elt9 = getelementptr inbounds i8, ptr %j, i64 4 + %deref_temp1.unpack10 = load i32, ptr %deref_temp1.elt9, align 4 + %0 = add i32 %deref_temp1.unpack, %deref_temp.unpack + %1 = add i32 %deref_temp1.unpack10, %deref_temp.unpack7 + %2 = insertvalue %struct.Position undef, i32 %0, 0 + %3 = insertvalue %struct.Position %2, i32 %1, 1 + ret %struct.Position %3 +} + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) +define void @add(ptr nocapture noundef nonnull writeonly %_ret0, ptr nocapture noundef nonnull readonly %i, ptr nocapture noundef nonnull readonly %j) local_unnamed_addr #1 { +add_entry: + %0 = load <2 x i32>, ptr %i, align 4 + %1 = load <2 x i32>, ptr %j, align 4 + %2 = add <2 x i32> %1, %0 + store <2 x i32> %2, ptr %_ret0, align 4 + ret void +} + +attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) } +attributes #1 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) } diff --git a/tests/bonsai/backends/llvm/index.x86_64.expect b/tests/bonsai/backends/llvm/index.x86_64.expect new file mode 100644 index 000000000..e7f2bb851 --- /dev/null +++ b/tests/bonsai/backends/llvm/index.x86_64.expect @@ -0,0 +1,47 @@ +; ModuleID = 'bonsai_module' +source_filename = "bonsai_module" + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) +define noundef float @first(<3 x float> noundef %v) local_unnamed_addr #0 { +first_entry: + %0 = extractelement <3 x float> %v, i64 0 + ret float %0 +} + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) +define float @idx(<3 x float> noundef %v, i32 noundef %i) local_unnamed_addr #0 { +idx_entry: + %0 = extractelement <3 x float> %v, i32 %i + ret float %0 +} + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) +define float @safe_idx(<3 x float> noundef %v, i32 noundef %i) local_unnamed_addr #0 { +safe_idx_entry: + %or.cond = icmp ult i32 %i, 3 + %0 = extractelement <3 x float> %v, i32 %i + %common.ret.op = select i1 %or.cond, float %0, float 0.000000e+00 + ret float %common.ret.op +} + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) +define noundef float @safe_oob(<3 x float> noundef %v) local_unnamed_addr #0 { +safe_oob_entry: + ret float 0.000000e+00 +} + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) +define noundef float @second(<3 x float> noundef %v) local_unnamed_addr #0 { +second_entry: + %0 = extractelement <3 x float> %v, i64 1 + ret float %0 +} + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) +define noundef float @third(<3 x float> noundef %v) local_unnamed_addr #0 { +third_entry: + %0 = extractelement <3 x float> %v, i64 2 + ret float %0 +} + +attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) } diff --git a/tests/bonsai/backends/llvm/mut-basic.x86_64.expect b/tests/bonsai/backends/llvm/mut-basic.x86_64.expect new file mode 100644 index 000000000..e577ecbaf --- /dev/null +++ b/tests/bonsai/backends/llvm/mut-basic.x86_64.expect @@ -0,0 +1,50 @@ +; ModuleID = 'bonsai_module' +source_filename = "bonsai_module" + +%struct.Example = type { i32, ptr } + +; Function Attrs: mustprogress nofree nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: readwrite) +define void @write_w(ptr nocapture noundef nonnull readonly %e) local_unnamed_addr #0 { +write_w_entry: + %_untyped = tail call dereferenceable_or_null(8) ptr @malloc(i64 8) + store i32 2, ptr %_untyped, align 4 + %build_ptr1 = getelementptr inbounds i8, ptr %_untyped, i64 4 + store i32 3, ptr %build_ptr1, align 4 + %0 = insertvalue %struct.Example { i32 1, ptr undef }, ptr %_untyped, 1 + %e_ld = load ptr, ptr %e, align 8 + store %struct.Example %0, ptr %e_ld, align 8 + ret void +} + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) +define void @write_x(ptr nocapture noundef nonnull writeonly %e) local_unnamed_addr #1 { +write_x_entry: + store i32 3, ptr %e, align 4 + ret void +} + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none) +define void @write_y(ptr nocapture noundef nonnull readonly %e) local_unnamed_addr #2 { +write_y_entry: + %e_y = getelementptr inbounds i8, ptr %e, i64 8 + %e_y_ld = load ptr, ptr %e_y, align 8 + store i32 3, ptr %e_y_ld, align 4 + ret void +} + +; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none) +define void @write_z(ptr nocapture noundef nonnull readonly %z) local_unnamed_addr #2 { +write_z_entry: + %z_ld = load ptr, ptr %z, align 8 + %z_ld1 = getelementptr inbounds i8, ptr %z_ld, i64 4 + store i32 3, ptr %z_ld1, align 4 + ret void +} + +; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) +declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #3 + +attributes #0 = { mustprogress nofree nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: readwrite) } +attributes #1 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) } +attributes #2 = { mustprogress nofree norecurse nosync nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none) } +attributes #3 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" } diff --git a/tests/bonsai/check_output.cmake b/tests/bonsai/check_output.cmake index e5ee2a130..48f24cd1f 100644 --- a/tests/bonsai/check_output.cmake +++ b/tests/bonsai/check_output.cmake @@ -9,6 +9,15 @@ if (NOT EXISTS "${ACTUAL}") message(FATAL_ERROR "Missing actual file: ${ACTUAL}") endif () +# An ..expect next to the base golden overrides it on that +# architecture; see the comment in CMakeLists.txt. Swapping EXPECT here means +# BONSAI_UPDATE_EXPECT re-blesses the override where one exists and the base +# golden where one does not, so updating on a host that has an override can +# never silently clobber the golden the other architecture relies on. +if (EXPECT_ARCH AND EXISTS "${EXPECT_ARCH}") + set(EXPECT "${EXPECT_ARCH}") +endif () + if ("$ENV{BONSAI_UPDATE_EXPECT}") file(COPY_FILE "${ACTUAL}" "${EXPECT}" RESULT error From a7bc119293b824f800f30728448845bf89b329e6 Mon Sep 17 00:00:00 2001 From: AJ Root Date: Mon, 31 Aug 2026 10:06:15 -0700 Subject: [PATCH 5/7] migrate rtiow's schedule to bind --- apps/rtiow/cpu/compare.sh | 11 +++++------ apps/rtiow/cpu/main.bonsai | 13 ++++++++++--- apps/rtiow/cpu/main.h | 3 +-- apps/rtiow/cpu/test.sh | 8 +++++--- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/apps/rtiow/cpu/compare.sh b/apps/rtiow/cpu/compare.sh index e0dcd1352..74a13e42b 100755 --- a/apps/rtiow/cpu/compare.sh +++ b/apps/rtiow/cpu/compare.sh @@ -26,13 +26,12 @@ OUT="${OUT:-$PREFIX/compare-out}" rm -rf "$OUT" mkdir -p "$OUT" -# The checked-in schedule splits the image loop across threads with -# `cpu_thread`, which generates calls to libdispatch -- a macOS library, so it -# does not link elsewhere -- and which the SSA pipeline does not implement -# anyway. Both sides are built from a copy without it, so that what is being -# compared is the two pipelines and not two different schedules. +# The checked-in schedule collapses the image loops and binds the result to +# CPU threads, which only the SSA pipeline implements. Both sides are built +# from a copy without it, so that what is being compared is the two pipelines +# and not two different schedules. SRC="$OUT/scene.bonsai" -sed '/image.split/,/cpu_thread/d' "$PREFIX/main.bonsai" > "$SRC" +sed '/image.collapse/,/bind(/d' "$PREFIX/main.bonsai" > "$SRC" build() { #