Skip to content

Affine

affine

AffineFunctions dataclass

Bases: InterpreterFunctions

Source code in xdsl/interpreters/affine.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@register_impls
class AffineFunctions(InterpreterFunctions):
    @impl(affine.StoreOp)
    def run_store(
        self, interpreter: Interpreter, op: affine.StoreOp, args: tuple[Any, ...]
    ):
        value, memref, *affine_dims = args

        affine_map = op.map

        assert affine_map is not None
        memref = cast(ShapedArray[Any], memref)
        indices = affine_map.data.eval(affine_dims, [])

        indices = tuple(indices)
        memref.store(indices, value)

        return ()

    @impl(affine.LoadOp)
    def run_load(
        self, interpreter: Interpreter, op: affine.LoadOp, args: tuple[Any, ...]
    ):
        memref, *affine_dims = args

        affine_map = op.map

        assert affine_map is not None
        memref = cast(ShapedArray[Any], memref)
        indices = affine_map.data.eval(affine_dims, [])
        indices = tuple(indices)
        value = memref.load(indices)

        return (value,)

    @impl(affine.ForOp)
    def run_for(
        self, interpreter: Interpreter, op: affine.ForOp, args: tuple[Any, ...]
    ):
        assert not args, "Arguments not supported yet"
        assert not op.results, "Results not supported yet"

        lower_bound = op.lowerBoundMap.data.eval([], [])
        upper_bound = op.upperBoundMap.data.eval([], [])
        assert len(lower_bound) == 1
        assert len(upper_bound) == 1

        lower_bound = lower_bound[0]
        upper_bound = upper_bound[0]
        step = op.step.value.data

        for i in range(lower_bound, upper_bound, step):
            for_results = interpreter.run_ssacfg_region(op.body, (i,))
            if for_results:
                raise NotImplementedError("affine block results not supported yet")

        return ()

    @impl(affine.ApplyOp)
    def run_apply(
        self, interpreter: Interpreter, op: affine.ApplyOp, args: tuple[Any, ...]
    ):
        return op.map.data.eval(args, ())

    @impl_terminator(affine.YieldOp)
    def run_yield(
        self, interpreter: Interpreter, op: affine.YieldOp, args: tuple[Any, ...]
    ):
        return ReturnedValues(args), ()

run_store(interpreter: Interpreter, op: affine.StoreOp, args: tuple[Any, ...])

Source code in xdsl/interpreters/affine.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@impl(affine.StoreOp)
def run_store(
    self, interpreter: Interpreter, op: affine.StoreOp, args: tuple[Any, ...]
):
    value, memref, *affine_dims = args

    affine_map = op.map

    assert affine_map is not None
    memref = cast(ShapedArray[Any], memref)
    indices = affine_map.data.eval(affine_dims, [])

    indices = tuple(indices)
    memref.store(indices, value)

    return ()

run_load(interpreter: Interpreter, op: affine.LoadOp, args: tuple[Any, ...])

Source code in xdsl/interpreters/affine.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@impl(affine.LoadOp)
def run_load(
    self, interpreter: Interpreter, op: affine.LoadOp, args: tuple[Any, ...]
):
    memref, *affine_dims = args

    affine_map = op.map

    assert affine_map is not None
    memref = cast(ShapedArray[Any], memref)
    indices = affine_map.data.eval(affine_dims, [])
    indices = tuple(indices)
    value = memref.load(indices)

    return (value,)

run_for(interpreter: Interpreter, op: affine.ForOp, args: tuple[Any, ...])

Source code in xdsl/interpreters/affine.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@impl(affine.ForOp)
def run_for(
    self, interpreter: Interpreter, op: affine.ForOp, args: tuple[Any, ...]
):
    assert not args, "Arguments not supported yet"
    assert not op.results, "Results not supported yet"

    lower_bound = op.lowerBoundMap.data.eval([], [])
    upper_bound = op.upperBoundMap.data.eval([], [])
    assert len(lower_bound) == 1
    assert len(upper_bound) == 1

    lower_bound = lower_bound[0]
    upper_bound = upper_bound[0]
    step = op.step.value.data

    for i in range(lower_bound, upper_bound, step):
        for_results = interpreter.run_ssacfg_region(op.body, (i,))
        if for_results:
            raise NotImplementedError("affine block results not supported yet")

    return ()

run_apply(interpreter: Interpreter, op: affine.ApplyOp, args: tuple[Any, ...])

Source code in xdsl/interpreters/affine.py
73
74
75
76
77
@impl(affine.ApplyOp)
def run_apply(
    self, interpreter: Interpreter, op: affine.ApplyOp, args: tuple[Any, ...]
):
    return op.map.data.eval(args, ())

run_yield(interpreter: Interpreter, op: affine.YieldOp, args: tuple[Any, ...])

Source code in xdsl/interpreters/affine.py
79
80
81
82
83
@impl_terminator(affine.YieldOp)
def run_yield(
    self, interpreter: Interpreter, op: affine.YieldOp, args: tuple[Any, ...]
):
    return ReturnedValues(args), ()