MimIR
0.5-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
Loading...
Searching...
No Matches
constraints.cpp
Go to the documentation of this file.
1
#include "
mim/plug/tensor/phase/constraints.h
"
2
3
#include <
mim/lam.h
>
4
5
namespace
mim::plug::tensor::phase
{
6
7
namespace
{
8
9
bool
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
16
bool
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
24
bool
check_gather_shape_constraints
(
const
App
* app,
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
45
bool
check_scatter_shape_constraints
(
const
App
* app,
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
mim::App
Definition
lam.h:215
mim::Def
Base class for all Defs.
Definition
def.h:289
mim::Def::proj
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
mim::Def::blame
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
mim::Lit::isa
static std::optional< T > isa(const Def *def)
Definition
def.h:976
constraints.h
lam.h
mim::plug::core::ncmp::l
@ l
Definition
autogen.h:49
mim::plug::math::round::r
@ r
Definition
autogen.h:298
mim::plug::tensor::phase
Definition
constraints.h:5
mim::plug::tensor::phase::check_scatter_shape_constraints
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.
Definition
constraints.cpp:45
mim::plug::tensor::phase::check_gather_shape_constraints
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.
Definition
constraints.cpp:24
mim::u64
uint64_t u64
Definition
types.h:27
src
mim
plug
tensor
phase
constraints.cpp
Generated by
1.18.0