-
Notifications
You must be signed in to change notification settings - Fork 29
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
Allow divide_dim to divide to non-literal expressions #628
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4dda8e0
Implement Check_IsDivisible helper
SamirDroubi 1646ef9
Add divide_expr ir building helper
SamirDroubi 0bd8431
Allow divide_dim to divide non-literal dimensions
SamirDroubi 7bea646
Fix elif cond
SamirDroubi 894de93
Update docstring
SamirDroubi df33a58
Merge branch 'main' into divide-expr-dim
SamirDroubi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -264,6 +264,24 @@ def Check_CompareExprs(proc, stmts, lhs, op, rhs): | |
Check_ExprBound(proc, stmts, expr, op, 0) | ||
|
||
|
||
def Check_IsDivisible(proc, stmts, expr, quot): | ||
failed = False | ||
if not isinstance(expr, LoopIR.Const): | ||
try: | ||
quot = LoopIR.Const(quot, T.int, null_srcinfo()) | ||
expr_mod_quot = LoopIR.BinOp("%", expr, quot, T.index, null_srcinfo()) | ||
zero = LoopIR.Const(0, T.int, null_srcinfo()) | ||
Check_CompareExprs(proc, stmts, expr_mod_quot, "==", zero) | ||
except SchedulingError: | ||
failed = True | ||
else: | ||
# Fast path | ||
failed = expr.val % quot != 0 | ||
|
||
if failed: | ||
raise SchedulingError(f"cannot perfectly divide '{expr}' by {quot}") | ||
|
||
|
||
def extract_env(c: ic.Cursor) -> List[Tuple[Sym, ic.Cursor]]: | ||
""" | ||
Extract the environment of live variables at `c`. | ||
|
@@ -303,6 +321,28 @@ def move_back(c): | |
return c.prev() | ||
|
||
|
||
# --------------------------------------------------------------------------- # | ||
# --------------------------------------------------------------------------- # | ||
# IR Building Helpers | ||
|
||
|
||
def divide_expr(e, quot): | ||
assert isinstance(e, LoopIR.expr) | ||
if isinstance(quot, int): | ||
quot_int = quot | ||
quot_ir = LoopIR.Const(quot, e.type, e.srcinfo) | ||
elif isinstance(quot, LoopIR.Const): | ||
quot_int = quot.val | ||
quot_ir = quot | ||
else: | ||
assert False, f"Bad case {type(quot)}" | ||
if isinstance(e, LoopIR.Const) and e.val % quot == 0: | ||
div = LoopIR.Const(e.val // quot_int, e.type, e.srcinfo) | ||
else: | ||
div = LoopIR.BinOp("/", e, quot_ir, e.type, e.srcinfo) | ||
return div | ||
|
||
|
||
# --------------------------------------------------------------------------- # | ||
# --------------------------------------------------------------------------- # | ||
# Scheduling directives | ||
|
@@ -728,25 +768,10 @@ def ceildiv(lhs, rhs): | |
elif tail_strategy in ["cut", "cut_and_guard"]: | ||
outer_hi = szop("/", N, inner_hi) # floor div | ||
elif tail_strategy == "perfect": | ||
if not isinstance(N, LoopIR.Const): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is great! |
||
hi_mod_quot = boolop("%", N, cnst(quot), T.index) | ||
try: | ||
ir = loop_cursor.get_root() | ||
loop = loop_cursor._node | ||
Check_CompareExprs(ir, [loop], hi_mod_quot, "==", cnst(0)) | ||
except SchedulingError: | ||
raise SchedulingError( | ||
f"cannot perfectly split the '{loop.iter}' loop " f"by {quot}" | ||
) | ||
outer_hi = boolop("/", N, cnst(quot), T.index) | ||
else: | ||
if N.val % quot != 0: | ||
raise SchedulingError( | ||
f"cannot perfectly split the '{loop.iter}' loop " | ||
f"because {quot} does not evenly divide " | ||
f"{N.val}" | ||
) | ||
outer_hi = cnst(N.val // quot) | ||
ir = loop_cursor.get_root() | ||
loop = loop_cursor._node | ||
Check_IsDivisible(ir, [loop], N, quot) | ||
outer_hi = divide_expr(N, quot) | ||
else: | ||
assert False, f"bad tail strategy: {tail_strategy}" | ||
|
||
|
@@ -1728,17 +1753,13 @@ def DoDivideDim(alloc_cursor, dim_idx, quotient): | |
old_typ = alloc_s.type | ||
old_shp = old_typ.shape() | ||
dim = old_shp[dim_idx] | ||
if not isinstance(dim, LoopIR.Const): | ||
raise SchedulingError(f"Cannot divide non-literal dimension: {dim}") | ||
if not dim.val % quotient == 0: | ||
raise SchedulingError(f"Cannot divide {dim.val} evenly by {quotient}") | ||
denom = quotient | ||
numer = dim.val // denom | ||
Check_IsDivisible(alloc_cursor.get_root(), [alloc_s], dim, quotient) | ||
numer = divide_expr(dim, quotient) | ||
new_shp = ( | ||
old_shp[:dim_idx] | ||
+ [ | ||
LoopIR.Const(numer, T.int, dim.srcinfo), | ||
LoopIR.Const(denom, T.int, dim.srcinfo), | ||
numer, | ||
LoopIR.Const(quotient, T.int, dim.srcinfo), | ||
] | ||
+ old_shp[dim_idx + 1 :] | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def foo(n: size, m: size, A: R[n + m + 12] @ DRAM): | ||
x: R[n, 3 * m, 4, m] @ DRAM | ||
for i in seq(0, n): | ||
for j in seq(0, 12): | ||
for k in seq(0, m): | ||
x[i, j / 4, j % 4, k] = A[i + j + k] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def foo(n: size, m: size): | ||
x: R[n, 1, (7 + m) / 8 * 8 / 8, 8, 1, m, 1] @ DRAM | ||
for i in seq(0, n): | ||
for j in seq(0, m): | ||
for k in seq(0, m): | ||
x[i, 0, j / 8, j % 8, 0, k, 0] = 2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change the docstring above "This limited implementation of
divide_dim
requires that the dimension being divided is constant itself." ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!