Skip to content
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
51 changes: 27 additions & 24 deletions include/tvm/script/printer/doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,34 +454,37 @@ class OperationDocNode : public ExprDocNode {
kUnaryStart = 0,
kUSub = 1, // -x
kInvert = 2, // ~x
kUnaryEnd = 3,
kNot = 3, // not x
kUnaryEnd = 4,

// Binary operators
kBinaryStart = 4,
kAdd = 5, // +
kSub = 6, // -
kMult = 7, // *
kDiv = 8, // /
kFloorDiv = 9, // // in Python
kMod = 10, // % in Python
kPow = 11, // ** in Python
kLShift = 12, // <<
kRShift = 13, // >>
kBitAnd = 14, // &
kBitOr = 15, // |
kBitXor = 16, // ^
kLt = 17, // <
kLtE = 18, // <=
kEq = 19, // ==
kNotEq = 20, // !=
kGt = 21, // >
kGtE = 22, // >=
kBinaryEnd = 23,
kBinaryStart = 5,
kAdd = 6, // +
kSub = 7, // -
kMult = 8, // *
kDiv = 9, // /
kFloorDiv = 10, // // in Python
kMod = 11, // % in Python
kPow = 12, // ** in Python
kLShift = 13, // <<
kRShift = 14, // >>
kBitAnd = 15, // &
kBitOr = 16, // |
kBitXor = 17, // ^
kLt = 18, // <
kLtE = 19, // <=
kEq = 20, // ==
kNotEq = 21, // !=
kGt = 22, // >
kGtE = 23, // >=
kAnd = 24, // and
kOr = 25, // or
kBinaryEnd = 26,

// Special
kSpecialStart = 24,
kIfThenElse = 25, // <operands[1]> if <operands[0]> else <operands[2]>
kSpecialEnd = 26
kSpecialStart = 27,
kIfThenElse = 28, // <operands[1]> if <operands[0]> else <operands[2]>
kSpecialEnd = 29
};

/*! \brief The kind of operation (operator) */
Expand Down
55 changes: 29 additions & 26 deletions python/tvm/script/printer/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,32 +237,35 @@ class OperationKind(IntEnum):
_UnaryStart = 0
USub = 1
Invert = 2
_UnaryEnd = 3

_BinaryStart = 4
Add = 5
Sub = 6
Mult = 7
Div = 8
FloorDiv = 9
Mod = 10
Pow = 11
LShift = 12
RShift = 13
BitAnd = 14
BitOr = 15
BitXor = 16
Lt = 17
LtE = 18
Eq = 19
NotEq = 20
Gt = 21
GtE = 22
_BinaryEnd = 23

_SpecialStart = 24
IfThenElse = 25
_SpecialEnd = 26
Not = 3
_UnaryEnd = 4

_BinaryStart = 5
Add = 6
Sub = 7
Mult = 8
Div = 9
FloorDiv = 10
Mod = 11
Pow = 12
LShift = 13
RShift = 14
BitAnd = 15
BitOr = 16
BitXor = 17
Lt = 18
LtE = 19
Eq = 20
NotEq = 21
Gt = 22
GtE = 23
And = 24
Or = 25
_BinaryEnd = 26

_SpecialStart = 27
IfThenElse = 28
_SpecialEnd = 29

# pylint: enable=invalid-name

Expand Down
6 changes: 6 additions & 0 deletions src/script/printer/python_doc_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ ExprPrecedence GetExprPrecedence(const ExprDoc& doc) {
std::map<OpKind, ExprPrecedence> raw_table = {
{OpKind::kUSub, ExprPrecedence::kUnary},
{OpKind::kInvert, ExprPrecedence::kUnary},
{OpKind::kNot, ExprPrecedence::kBooleanNot},
{OpKind::kAdd, ExprPrecedence::kAdd},
{OpKind::kSub, ExprPrecedence::kAdd},
{OpKind::kMult, ExprPrecedence::kMult},
Expand All @@ -97,6 +98,8 @@ ExprPrecedence GetExprPrecedence(const ExprDoc& doc) {
{OpKind::kNotEq, ExprPrecedence::kComparison},
{OpKind::kGt, ExprPrecedence::kComparison},
{OpKind::kGtE, ExprPrecedence::kComparison},
{OpKind::kAnd, ExprPrecedence::kBooleanAnd},
{OpKind::kOr, ExprPrecedence::kBooleanOr},
{OpKind::kIfThenElse, ExprPrecedence::kIfThenElse},
};
int n = static_cast<int>(OpKind::kSpecialEnd);
Expand Down Expand Up @@ -323,6 +326,7 @@ const std::string OperatorToString(OperationDocNode::Kind operation_kind) {
std::map<OpKind, std::string> raw_table = {
{OpKind::kUSub, "-"}, //
{OpKind::kInvert, "~"}, //
{OpKind::kNot, "not "}, //
{OpKind::kAdd, "+"}, //
{OpKind::kSub, "-"}, //
{OpKind::kMult, "*"}, //
Expand All @@ -341,6 +345,8 @@ const std::string OperatorToString(OperationDocNode::Kind operation_kind) {
{OpKind::kNotEq, "!="}, //
{OpKind::kGt, ">"}, //
{OpKind::kGtE, ">="}, //
{OpKind::kAnd, "and"}, //
{OpKind::kOr, "or"}, //
};

std::vector<std::string> table;
Expand Down
62 changes: 62 additions & 0 deletions tests/python/unittest/test_tvmscript_printer_python_doc_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def test_print_index_doc(indices, expected):
UNARY_OP_TOKENS = {
OperationKind.USub: "-",
OperationKind.Invert: "~",
OperationKind.Not: "not ",
}


Expand Down Expand Up @@ -193,6 +194,8 @@ def test_print_unary_operation_doc(op_kind, expected_token):
OperationKind.NotEq: "!=",
OperationKind.Gt: ">",
OperationKind.GtE: ">=",
OperationKind.And: "and",
OperationKind.Or: "or",
}


Expand Down Expand Up @@ -1060,6 +1063,9 @@ def negative(a):
def invert(a):
return OperationDoc(OperationKind.Invert, [a])

def not_(a):
return OperationDoc(OperationKind.Not, [a])

def add(a, b):
return OperationDoc(OperationKind.Add, [a, b])

Expand Down Expand Up @@ -1099,6 +1105,12 @@ def eq(a, b):
def not_eq(a, b):
return OperationDoc(OperationKind.NotEq, [a, b])

def and_(a, b):
return OperationDoc(OperationKind.And, [a, b])

def or_(a, b):
return OperationDoc(OperationKind.Or, [a, b])

def if_then_else(a, b, c):
return OperationDoc(OperationKind.IfThenElse, [a, b, c])

Expand Down Expand Up @@ -1285,6 +1297,56 @@ def if_then_else(a, b, c):
"x < (y if y else y)",
),
],
"boolean": [
(
not_(and_(x, y)),
"not (x and y)",
),
(
and_(not_(x), y),
"not x and y",
),
(
and_(or_(x, y), z),
"(x or y) and z",
),
(
or_(x, or_(y, z)),
"x or (y or z)",
),
(
or_(or_(x, y), z),
"x or y or z",
),
(
or_(and_(x, y), z),
# Maybe we should consider adding parentheses here
# for readability, even though it's not necessary.
"x and y or z",
),
(
and_(or_(not_(x), y), z),
"(not x or y) and z",
),
(
and_(lt(x, y), lt(y, z)),
"x < y and y < z",
),
(
or_(not_(eq(x, y)), lt(y, z)),
# Same as the previous one, the code here is not
# readable without parentheses.
"not x == y or y < z",
),
(
and_(if_then_else(x, y, z), x),
"(y if x else z) and x",
),
(
not_(if_then_else(x, y, z)),
"not (y if x else z)",
),
],
"if-then-else": [
(
if_then_else(x, if_then_else(y, y, y), z),
Expand Down