Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
MIGraphX
Commits
82e3f0b1
Commit
82e3f0b1
authored
Jul 09, 2018
by
Paul
Browse files
Move instruction to end before removing them
parent
56537575
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
52 additions
and
14 deletions
+52
-14
src/dead_code_elimination.cpp
src/dead_code_elimination.cpp
+10
-12
src/include/migraph/program.hpp
src/include/migraph/program.hpp
+3
-0
src/program.cpp
src/program.cpp
+17
-0
test/CMakeLists.txt
test/CMakeLists.txt
+2
-0
test/dead_code_elimination_test.cpp
test/dead_code_elimination_test.cpp
+20
-2
No files found.
src/dead_code_elimination.cpp
View file @
82e3f0b1
...
...
@@ -8,26 +8,24 @@ namespace migraph {
void
dead_code_elimination
::
apply
(
program
&
p
)
const
{
for
(
auto
i
:
iterator_for
(
p
))
auto
last
=
std
::
prev
(
p
.
end
());
for
(
auto
ins
:
iterator_for
(
p
))
{
// Skip over instructions that may have been removed
if
(
!
p
.
has_instruction
(
i
))
continue
;
// Skip the last instruction
if
(
i
==
st
d
::
prev
(
p
.
end
())
)
if
(
i
ns
==
la
st
)
break
;
fix
([
&
](
auto
self
,
auto
ins
)
{
assert
(
p
.
has_instruction
(
ins
));
if
(
ins
->
output
.
empty
())
fix
([
&
](
auto
self
,
auto
leaf
)
{
assert
(
p
.
has_instruction
(
leaf
));
if
(
leaf
->
output
.
empty
())
{
std
::
cout
<<
p
<<
std
::
endl
;
auto
args
=
ins
->
arguments
;
p
.
remove_instruction
(
ins
);
auto
args
=
leaf
->
arguments
;
p
.
move_instruction
(
leaf
,
p
.
end
());
for
(
auto
arg
:
args
)
self
(
arg
);
}
})(
i
);
})(
i
ns
);
}
p
.
remove_instructions
(
std
::
next
(
last
),
p
.
end
());
}
}
// namespace migraph
src/include/migraph/program.hpp
View file @
82e3f0b1
...
...
@@ -53,6 +53,9 @@ struct program
replace_instruction
(
instruction_ref
ins
,
operation
op
,
std
::
vector
<
instruction_ref
>
args
);
instruction_ref
remove_instruction
(
instruction_ref
ins
);
instruction_ref
remove_instructions
(
instruction_ref
first
,
instruction_ref
last
);
instruction_ref
move_instruction
(
instruction_ref
src
,
instruction_ref
dst
);
template
<
class
...
Ts
>
instruction_ref
add_literal
(
Ts
&&
...
xs
)
...
...
src/program.cpp
View file @
82e3f0b1
...
...
@@ -60,6 +60,23 @@ instruction_ref program::remove_instruction(instruction_ref ins)
return
impl
->
instructions
.
erase
(
ins
);
}
instruction_ref
program
::
remove_instructions
(
instruction_ref
first
,
instruction_ref
last
)
{
assert
(
has_instruction
(
first
));
assert
(
has_instruction
(
last
));
std
::
for_each
(
first
,
last
,
[
&
](
instruction
&
ins
)
{
ins
.
clear_arguments
();
});
assert
(
std
::
all_of
(
first
,
last
,
[
&
](
instruction
&
ins
){
return
ins
.
output
.
empty
();
}));
return
impl
->
instructions
.
erase
(
first
,
last
);
}
instruction_ref
program
::
move_instruction
(
instruction_ref
src
,
instruction_ref
dst
)
{
impl
->
instructions
.
splice
(
dst
,
impl
->
instructions
,
src
);
return
src
;
}
instruction_ref
program
::
add_literal
(
literal
l
)
{
impl
->
instructions
.
emplace_front
(
std
::
move
(
l
));
...
...
test/CMakeLists.txt
View file @
82e3f0b1
...
...
@@ -52,6 +52,8 @@ function(add_test_command NAME EXE)
if(NOT RESULT EQUAL 0)
# TODO: check for core files based on pid when setting /proc/sys/kernel/core_uses_pid
if(EXISTS
${
TEST_DIR
}
/core)
set(
\$
ENV{UBSAN_OPTIONS} print_stacktrace=1)
set(
\$
ENV{ASAN_OPTIONS} print_stacktrace=1)
execute_process(COMMAND
${
MIGRAPH_GDB
}
$<TARGET_FILE:
${
EXE
}
>
${
TEST_DIR
}
/core -batch -ex bt)
endif()
message(FATAL_ERROR
\"
Test failed
\"
)
...
...
test/dead_code_elimination_test.cpp
View file @
82e3f0b1
...
...
@@ -27,7 +27,7 @@ void simple_test()
EXPECT
(
result
!=
migraph
::
literal
{
4
});
}
void
duplicate_test
()
void
duplicate_test
1
()
{
migraph
::
program
p
;
...
...
@@ -43,8 +43,26 @@ void duplicate_test()
EXPECT
(
result
!=
migraph
::
literal
{
4
});
}
void
duplicate_test2
()
{
migraph
::
program
p
;
auto
one
=
p
.
add_literal
(
1
);
auto
two
=
p
.
add_literal
(
2
);
p
.
add_instruction
(
sum_op
{},
one
,
two
);
p
.
add_instruction
(
minus_op
{},
one
,
two
);
p
.
add_instruction
(
sum_op
{},
one
,
two
);
auto
count
=
std
::
distance
(
p
.
begin
(),
p
.
end
());
p
.
compile
(
dce_target
{});
EXPECT
(
std
::
distance
(
p
.
begin
(),
p
.
end
())
==
(
count
-
2
));
auto
result
=
p
.
eval
({});
EXPECT
(
result
==
migraph
::
literal
{
3
});
EXPECT
(
result
!=
migraph
::
literal
{
4
});
}
int
main
()
{
simple_test
();
duplicate_test
();
duplicate_test1
();
duplicate_test2
();
}
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