MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
constraints.cpp
Go to the documentation of this file.
2
3#include <mim/lam.h>
4
6
7namespace {
8
9bool statically_le(const Def* lhs, const Def* rhs) {
10 if (lhs == rhs) return true;
11 auto l = Lit::isa<u64>(lhs);
12 auto r = Lit::isa<u64>(rhs);
13 return l && r && *l <= *r;
14}
15
16bool statically_violates(const Def* lhs, const Def* rhs) {
17 auto l = Lit::isa<u64>(lhs);
18 auto r = Lit::isa<u64>(rhs);
19 return l && r && *l > *r;
20}
21
22} // namespace
23
25 const Def* rank,
26 const Def* dim,
27 const Def* src_shape,
28 const Def* idx_shape) {
29 auto rank_l = Lit::isa<u64>(rank);
30 auto dim_l = Lit::isa<u64>(dim);
31 if (!rank_l || !dim_l) return false;
32
33 bool proven = true;
34 for (u64 d = 0; d < *rank_l; ++d)
35 if (d != *dim_l) {
36 auto lhs = idx_shape->proj(*rank_l, d);
37 auto rhs = src_shape->proj(*rank_l, d);
38 if (statically_violates(lhs, rhs))
39 app->blame("gather index shape exceeds input shape at axis {}", d).bail();
40 proven &= statically_le(lhs, rhs);
41 }
42 return proven;
43}
44
46 const Def* rank,
47 const Def* dim,
48 const Def* src_shape,
49 const Def* idx_shape,
50 const Def* updates_shape) {
51 auto rank_l = Lit::isa<u64>(rank);
52 auto dim_l = Lit::isa<u64>(dim);
53 if (!rank_l || !dim_l) return false;
54
55 bool proven = true;
56 for (u64 d = 0; d < *rank_l; ++d) {
57 auto idx = idx_shape->proj(*rank_l, d);
58 auto upd = updates_shape->proj(*rank_l, d);
59 if (d != *dim_l) {
60 auto src = src_shape->proj(*rank_l, d);
61 if (statically_violates(idx, src))
62 app->blame("scatter index shape exceeds input shape at axis {}", d).bail();
63 proven &= statically_le(idx, src);
64 }
65 if (statically_violates(idx, upd)) app->blame("scatter index shape exceeds updates shape at axis {}", d).bail();
66 proven &= statically_le(idx, upd);
67 }
68 return proven;
69}
70
71} // namespace mim::plug::tensor::phase
Base class for all Defs.
Definition def.h:289
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
Definition def.cpp:647
fe::Error & blame(fe::cite_string< Args... > s, Args &&... args) const
Reports an error that blames this; chain Error::n for Notes and Error::bail to throw.
Definition def.h:333
static std::optional< T > isa(const Def *def)
Definition def.h:976
bool check_scatter_shape_constraints(const App *app, const Def *rank, const Def *dim, const Def *src_shape, const Def *idx_shape, const Def *updates_shape)
Checks statically decidable scatter constraints, blaming app for a violation.
bool check_gather_shape_constraints(const App *app, const Def *rank, const Def *dim, const Def *src_shape, const Def *idx_shape)
Checks statically decidable gather constraints, blaming app for a violation.
uint64_t u64
Definition types.h:27