Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tic
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wenyuanbo
tic
Commits
ff6b8d82
Commit
ff6b8d82
authored
Nov 17, 2016
by
tqchen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
check substitute
parent
363cc280
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
0 deletions
+53
-0
include/tvm/ir_mutator.h
+8
-0
src/pass/ir_mutator.cc
+26
-0
tests/cpp/ir_mutator_test.cc
+19
-0
No files found.
include/tvm/ir_mutator.h
View file @
ff6b8d82
...
@@ -7,6 +7,7 @@
...
@@ -7,6 +7,7 @@
#define TVM_IR_MUTATOR_H_
#define TVM_IR_MUTATOR_H_
#include <tvm/ir_node.h>
#include <tvm/ir_node.h>
#include <unordered_map>
#include "./expr.h"
#include "./expr.h"
namespace
tvm
{
namespace
tvm
{
...
@@ -72,6 +73,13 @@ class IRMutatorExample : public IRMutator {
...
@@ -72,6 +73,13 @@ class IRMutatorExample : public IRMutator {
static
FMutateStmt
&
vtable_stmt
();
// NOLINT(*)
static
FMutateStmt
&
vtable_stmt
();
// NOLINT(*)
};
};
/*!
* \brief Substitute occurance of IRNode to be expr
* \param replacements The replacement rule of substitution
* \param expr The expression to be substituted.
*/
Expr
Substitute
(
const
std
::
unordered_map
<
const
IRNode
*
,
Expr
>&
replacements
,
Expr
expr
);
}
// namespace ir
}
// namespace ir
}
// namespace tvm
}
// namespace tvm
#endif // TVM_IR_MUTATOR_H_
#endif // TVM_IR_MUTATOR_H_
src/pass/ir_mutator.cc
View file @
ff6b8d82
...
@@ -8,6 +8,32 @@
...
@@ -8,6 +8,32 @@
namespace
tvm
{
namespace
tvm
{
namespace
ir
{
namespace
ir
{
namespace
{
// visitor to implement apply
class
IRSubstitute
:
public
IRMutator
{
public
:
Expr
mutate
(
Expr
expr
)
final
{
const
IRNode
*
v
=
expr
.
get
();
if
(
v
!=
nullptr
)
{
auto
it
=
replacements_
.
find
(
v
);
if
(
it
!=
replacements_
.
end
())
{
return
it
->
second
;
}
}
return
IRMutator
::
mutate
(
expr
);
}
explicit
IRSubstitute
(
const
std
::
unordered_map
<
const
IRNode
*
,
Expr
>&
replacements
)
:
replacements_
(
replacements
)
{}
private
:
const
std
::
unordered_map
<
const
IRNode
*
,
Expr
>&
replacements_
;
};
}
// namespace
Expr
Substitute
(
const
std
::
unordered_map
<
const
IRNode
*
,
Expr
>&
replacements
,
Expr
expr
)
{
return
IRSubstitute
(
replacements
).
mutate
(
expr
);
}
IRMutator
::
FMutateExpr
&
IRMutator
::
vtable_expr
()
{
// NOLINT(*)
IRMutator
::
FMutateExpr
&
IRMutator
::
vtable_expr
()
{
// NOLINT(*)
static
FMutateExpr
inst
;
return
inst
;
static
FMutateExpr
inst
;
return
inst
;
}
}
...
...
tests/cpp/ir_mutator_test.cc
View file @
ff6b8d82
...
@@ -52,6 +52,25 @@ TEST(IRMutator, Basic) {
...
@@ -52,6 +52,25 @@ TEST(IRMutator, Basic) {
CHECK
(
os
.
str
()
==
"(x + 10)"
);
CHECK
(
os
.
str
()
==
"(x + 10)"
);
}
}
TEST
(
IRMutator
,
Substitute
)
{
using
namespace
Halide
::
Internal
;
using
namespace
tvm
;
Var
x
(
"x"
),
y
;
auto
z
=
x
+
y
;
{
auto
zz
=
Substitute
({{
y
.
get
(),
11
}},
z
);
std
::
ostringstream
os
;
os
<<
zz
;
CHECK
(
os
.
str
()
==
"(x + 11)"
);
}
{
auto
zz
=
Substitute
({{
z
.
get
(),
11
}},
z
);
std
::
ostringstream
os
;
os
<<
zz
;
CHECK
(
os
.
str
()
==
"11"
);
}
}
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
testing
::
InitGoogleTest
(
&
argc
,
argv
);
testing
::
InitGoogleTest
(
&
argc
,
argv
);
testing
::
FLAGS_gtest_death_test_style
=
"threadsafe"
;
testing
::
FLAGS_gtest_death_test_style
=
"threadsafe"
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment