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

feat[next]: Extend and refactor constant folding #1810

Open
wants to merge 42 commits into
base: main
Choose a base branch
from

Conversation

SF-N
Copy link
Contributor

@SF-N SF-N commented Jan 21, 2025

This replaces this PR.
It refactors the ConstantFolding pass doing transformations until a fixed point is reached, like in the CollapseTuple pass. The FixedPointTransformation class from PR 1826 is used here.

The following new transformations are introduced:
CANONICALIZE_OP_FUNCALL_SYMREF_LITERAL:
literal, symref -> symref, literal, prerequisite for FOLD_FUNCALL_LITERAL, FOLD_NEUTRAL_OP
literal, funcall -> funcall, literal, prerequisite for FOLD_FUNCALL_LITERAL, FOLD_NEUTRAL_OP
funcall, op -> op, funcall for s[0] + (s[0] + 1), prerequisite for FOLD_MIN_MAX_PLUS
CANONICALIZE_MINUS: a - b -> a + (-b), prerequisite for FOLD_MIN_MAX_PLUS
CANONICALIZE_MIN_MAX: maximum(a, maximum(...)) -> maximum(maximum(...), a), prerequisite for
FOLD_FUNCALL_LITERAL: (a + 1) + 1 -> a + (1 + 1)
FOLD_MIN_MAX:
maximum(maximum(a, 1), a) -> maximum(a, 1)
maximum(maximum(a, 1), 1) -> maximum(a, 1)
FOLD_MIN_MAX_PLUS: maximum(plus(a, 1), a) -> plus(a, 1)
maximum(plus(a, 1), plus(a, -1)) -> plus(a, maximum(1, -1))
FOLD_NEUTRAL_OP: a + 0 -> a, a * 1 -> a
additionally to the existing ones:
FOLD_ARITHMETIC_BUILTINS: 1 + 1 -> 2
FOLD_MIN_MAX_LITERALS: minimum(a, a) -> a
FOLD_IF: if_(True, true_branch, false_branch) -> true_branch

In the end, unary minuses are transformed back to minus-calls where possible by UndoCanonicalizeMinus: a + (-b) -> a - b , -a + b -> b - a, -a + (-b) -> -a - b

Previously, large nested maximum expressions like maximum(maximum(maximum(maximum(sym, 1),...), 1), maximum(maximum(sym, 1), 1)) caused timeouts in PMAP-GO.

src/gt4py/next/iterator/builtins.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/embedded.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/transforms/collapse_tuple.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/transforms/collapse_tuple.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/transforms/collapse_tuple.py Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants