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

[TIR] Expose shift_left and shift_right to Python #12584

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/tvm/tir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
from .op import likely, isnan, isnullptr, isfinite, isinf, copysign
from .op import div, indexdiv, indexmod, truncdiv, truncmod, floordiv, floormod, ceildiv
from .op import comm_reducer, min, max, sum
from .op import q_multiply_shift
from .op import q_multiply_shift, shift_left, shift_right
from .op import TVMBackendAllocWorkspace, TVMBackendFreeWorkspace

from .schedule import StmtSRef, BlockScope, ScheduleState, Schedule, ScheduleError
Expand Down
38 changes: 38 additions & 0 deletions python/tvm/tir/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,44 @@ def q_multiply_shift(x, y, q, s):
return call_intrin("int32", "tir.q_multiply_shift", x, y, q, s)


def shift_left(x, y, span=None):
"""Return the result of x left shifted by y bits.

Parameters
----------
x : PrimExpr
Input argument.

y : PrimExpr
Input argument.

Returns
-------
z : PrimExpr
The result.
"""
return _ffi_api.left_shift(x, y, span)


def shift_right(x, y, span=None):
"""Return the result of x right shifted by y bits.

Parameters
----------
x : PrimExpr
Input argument.

y : PrimExpr
Input argument.

Returns
-------
z : PrimExpr
The result.
"""
return _ffi_api.right_shift(x, y, span)


def fmod(x, y):
"""Return the remainder of x divided by y with the same sign as x.

Expand Down
16 changes: 16 additions & 0 deletions tests/python/unittest/test_tir_op_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ def test_tir_op_vectorcombine():
assert expr.op.name == "tir.vectorcombine"


def test_tir_op_shift_left():
x = tir.Var("x", dtype="int32")
y = tir.Var("x", dtype="int32")
expr = tir.shift_left(x, y)
assert expr.op.name == "tir.shift_left"


def test_tir_op_shift_right():
x = tir.Var("x", dtype="int32")
y = tir.Var("x", dtype="int32")
expr = tir.shift_right(x, y)
assert expr.op.name == "tir.shift_right"


def test_tir_op_TVMBackendAllocWorkspace():
expr = tir.TVMBackendAllocWorkspace(0, 1, 2, 3, 4)
assert expr.op.name == "tir.TVMBackendAllocWorkspace"
Expand Down Expand Up @@ -154,5 +168,7 @@ def test_tir_op_TVMBackendFreeWorkspace():
test_tir_op_vectorlow()
test_tir_op_vectorhigh()
test_tir_op_vectorcombine()
test_tir_op_shift_left()
test_tir_op_shift_right()
test_tir_op_TVMBackendAllocWorkspace()
test_tir_op_TVMBackendFreeWorkspace()