Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer range from body of `ForN` loops + packed loops #187

Merged
merged 4 commits into from May 18, 2016

Conversation

@bvssvni
Copy link
Member

commented May 18, 2016

Closes #181

This PR adds support for range inference and packed versions of loops.

For example:

fn randomize_tensor(mut tensor: [[[f64]]]) {
    for i, j, k {
        tensor[i][j][k] = random()
    }
}

Notice that there is no range information in the loop, and 3 indices are specified at once. Such notation is common in mathematics, because this information can be inferred from the body of the loop.

What happens behind the scene is Dyon first expanding the packed loop into 3 loops:

for i {
    for j {
        for k {
            tensor[i][j][k] = random()
        }
    }
}

Then it runs an inference on the body for k, and since neither i or j is declared in that loop, it infers the range to [0, len(tensor[i][j])), by cloning and truncating the AST item tensor[i][j][k] to where it finds the index.

This process is repeated for j, which gets the range [0, len(tensor[i])).

Then again for i, which gets the range [0, len(tensor)).

Now we have the full loops:

for i [0, len(tensor)) {
    for j [0, len(tensor[i])) {
        for k [0, len(tensor[i][j])) {
            tensor[i][j][k] = random()
        }
    }
}

You can also write ranges in the packed version, and depend on previous index:

// For all pairs...
for i n, j [i+1, n) { ... }

The inference and packed versions also works for sum/all/any/max/min/sift loops.

The min and max loops went through a breaking change to make them nicer under composition. See #181 for more information.

bvssvni added some commits May 18, 2016

Infer first index
- Moved “ast” to its own module
- Added some tests
Infer ordered indices
- Added `ast::Item::trunc`
Added packed loops
- Added multiple names to `lifetime::node::Node` for packed loops
- Fixed spelling error
- Added “syntax/infer_pass_3.dyon”
@bvssvni

This comment has been minimized.

Copy link
Member Author

commented May 18, 2016

Updated #116

@bvssvni bvssvni changed the title Infer range from body of `ForN` loops Infer range from body of `ForN` loops + packed loops May 18, 2016

@bvssvni bvssvni merged commit 24cf158 into PistonDevelopers:master May 18, 2016

@bvssvni bvssvni deleted the bvssvni:infer branch May 18, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.