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

[PASS] Export simplify and equal to python #14

Merged
merged 2 commits into from
Jan 16, 2017
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
17 changes: 17 additions & 0 deletions include/tvm/ir_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef TVM_IR_PASS_H_
#define TVM_IR_PASS_H_

#include <ir/IREquality.h>
#include <pass/Simplify.h>
#include <tvm/ir_functor.h>
#include <unordered_map>
#include <vector>
Expand All @@ -19,6 +21,21 @@
namespace tvm {
namespace ir {

inline bool Equal(Expr a, Expr b) {
return Halide::Internal::equal(a, b);
}

inline bool Equal(Stmt a, Stmt b) {
return Halide::Internal::equal(a, b);
}

inline Expr Simplify(Expr a) {
return Halide::Internal::simplify(a);
}

inline Stmt Simplify(Stmt a) {
return Halide::Internal::simplify(a);
}

/*!
* \brief Schedule s' dependent operations.
Expand Down
21 changes: 21 additions & 0 deletions src/c_api/c_api_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ namespace ir {
using ArgStack = const std::vector<APIVariantValue>;
using RetValue = APIVariantValue;

TVM_REGISTER_API(_pass_Simplify)
.set_body([](const ArgStack& args, RetValue *ret) {
CHECK(args.at(0).type_id == kNodeHandle);
if (dynamic_cast<Expr::ContainerType*>(args.at(0).sptr.get())) {
*ret = Simplify(args.at(0).operator Expr());
} else {
*ret = Simplify(args.at(0).operator Stmt());
}
});

TVM_REGISTER_API(_pass_Equal)
.set_body([](const ArgStack& args, RetValue *ret) {
CHECK(args.at(0).type_id == kNodeHandle);
CHECK(args.at(1).type_id == kNodeHandle);
if (dynamic_cast<Expr::ContainerType*>(args.at(0).sptr.get())) {
*ret = Equal(args.at(0).operator Expr(), args.at(1).operator Expr());
} else {
*ret = Equal(args.at(0).operator Stmt(), args.at(1).operator Stmt());
}
});

// make from two arguments
#define REGISTER_PASS1(PassName) \
TVM_REGISTER_API(_pass_## PassName) \
Expand Down
10 changes: 10 additions & 0 deletions tests/python/test_pass_basic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import tvm

def test_simplify():
x = tvm.Var('x')
e1 = tvm.ir_pass.Simplify(x + 2 + 1)
assert(tvm.ir_pass.Equal(e1, x + 3))
e2 = tvm.ir_pass.Simplify(x * 3 + 5 * x)
assert(tvm.ir_pass.Equal(e2, x * 8))
e3 = tvm.ir_pass.Simplify(x - x / 3 * 3)
assert(tvm.ir_pass.Equal(e3, tvm.make.Mod(x, 3)))


def test_verify_ssa():
x = tvm.Var('x')
y = tvm.Var()
Expand Down