Skip to content

Multiple loop unrolling passes? #34

Description

@jack-melchert

I was wondering if its possible to support multiple passes of loop unrolling.
This is the type of nested loop I want to unroll:

def test_nested_unroll():
    z = 3
    @end_rewrite()
    @loop_unroll()
    @begin_rewrite()
    def foo():
        for i in ast_tools.macros.unroll(range(z)):
            for x in ast_tools.macros.unroll(range(i)):
                print(i, x)

    print(inspect.getsource(foo))

Currently, this code produces:

def foo():
    for x in ast_tools.macros.unroll(range(0)):
        print(0, x)
    for x in ast_tools.macros.unroll(range(1)):
        print(1, x)
    for x in ast_tools.macros.unroll(range(2)):
        print(2, x)

While it would be helpful if it could unroll it again to:

def foo():
    print(1, 0)
    print(2, 0)
    print(2, 1)

Its possible to do what I want using the if_inline but, as you can imagine, the code gets very long if you have large loops:

def test_nested_unroll():
    z = 3
    @end_rewrite()
    @loop_unroll()
    @if_inline()
    @begin_rewrite()
    def foo():
        if inline(z == 1):
            for x in ast_tools.macros.unroll(range(0)):
                print(0, x)
        if inline(z == 2):
            for x in ast_tools.macros.unroll(range(0)):
                print(0, x)
            for x in ast_tools.macros.unroll(range(1)):
                print(1, x)
        if inline(z == 3):
            for x in ast_tools.macros.unroll(range(0)):
                print(0, x)
            for x in ast_tools.macros.unroll(range(1)):
                print(1, x)
            for x in ast_tools.macros.unroll(range(2)):
                print(2, x)

    print(inspect.getsource(foo))

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions