Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7ace52ba
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
7ace52ba
编写于
6月 28, 2023
作者:
W
Wilber
提交者:
GitHub
6月 28, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[IR&Pass] Add DCE Pass for new ir. (#54935)
上级
9137adb9
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
105 addition
and
5 deletion
+105
-5
paddle/ir/CMakeLists.txt
paddle/ir/CMakeLists.txt
+1
-0
paddle/ir/transforms/CMakeLists.txt
paddle/ir/transforms/CMakeLists.txt
+10
-0
paddle/ir/transforms/dce.cc
paddle/ir/transforms/dce.cc
+61
-0
paddle/ir/transforms/dce.h
paddle/ir/transforms/dce.h
+25
-0
test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc
test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc
+8
-5
未找到文件。
paddle/ir/CMakeLists.txt
浏览文件 @
7ace52ba
...
...
@@ -40,6 +40,7 @@ endif()
add_subdirectory
(
core
)
add_subdirectory
(
pass
)
add_subdirectory
(
pattern_rewrite
)
add_subdirectory
(
transforms
)
if
(
WIN32
)
if
(
WITH_SHARED_IR
)
...
...
paddle/ir/transforms/CMakeLists.txt
0 → 100644
浏览文件 @
7ace52ba
file
(
GLOB PATTERN_SRCS
"*.cc"
)
ir_library
(
ir_builtin_transforms
SRCS
${
PATTERN_SRCS
}
DEPS
ir_core
ir_pattern_rewrite
ir_pass
)
paddle/ir/transforms/dce.cc
0 → 100644
浏览文件 @
7ace52ba
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/ir/transforms/dce.h"
#include <memory>
#include "paddle/ir/core/builtin_op.h"
#include "paddle/ir/pass/pass.h"
namespace
{
// TODO(wilber): After support SideEffectTrait, Only NoSideEffectTrait op can be
// removed by dce pass.
// Now just a naive implementation.
class
DCEPass
:
public
ir
::
Pass
{
public:
DCEPass
()
:
ir
::
Pass
(
"DCEPass"
,
0
)
{}
void
Run
(
ir
::
Operation
*
op
)
override
{
auto
module_op
=
op
->
dyn_cast
<
ir
::
ModuleOp
>
();
IR_ENFORCE
(
module_op
,
"DCEPass should run on module op."
);
auto
*
block
=
module_op
.
block
();
std
::
vector
<
ir
::
Operation
>
erased_op
;
for
(
auto
it
=
block
->
begin
();
it
!=
block
->
end
();
++
it
)
{
// TODO(wilber): Support NoSideEffect trait.
// if (!(*it)->HasTrait<NoSideEffect>()) continue;
bool
use_empty
=
true
;
for
(
uint32_t
i
=
0
;
i
<
(
*
it
)
->
num_results
();
++
i
)
{
use_empty
&=
(
*
it
)
->
result
(
i
).
use_empty
();
}
if
(
use_empty
&&
(
*
it
)
->
name
()
!=
"pd.fetch"
)
{
erased_op
.
push_back
(
**
it
);
}
}
for
(
auto
ep
:
erased_op
)
block
->
erase
(
ep
);
}
bool
CanApplyOn
(
ir
::
Operation
*
op
)
const
override
{
return
op
->
name
()
==
"builtin.module"
&&
op
->
num_regions
()
>
0
;
}
};
}
// namespace
namespace
ir
{
std
::
unique_ptr
<
Pass
>
CreateDCEPass
()
{
return
std
::
make_unique
<
DCEPass
>
();
}
}
// namespace ir
paddle/ir/transforms/dce.h
0 → 100644
浏览文件 @
7ace52ba
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <memory>
#include "paddle/ir/core/dll_decl.h"
namespace
ir
{
class
Pass
;
IR_API
std
::
unique_ptr
<
Pass
>
CreateDCEPass
();
}
// namespace ir
test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc
浏览文件 @
7ace52ba
...
...
@@ -22,6 +22,7 @@
#include "paddle/ir/core/builder.h"
#include "paddle/ir/core/builtin_attribute.h"
#include "paddle/ir/core/builtin_dialect.h"
#include "paddle/ir/core/builtin_op.h"
#include "paddle/ir/core/cast_utils.h"
#include "paddle/ir/core/dialect.h"
#include "paddle/ir/core/enforce.h"
...
...
@@ -34,6 +35,7 @@
#include "paddle/ir/pattern_rewrite/pattern_applicator.h"
#include "paddle/ir/pattern_rewrite/pattern_match.h"
#include "paddle/ir/pattern_rewrite/pattern_rewrite_driver.h"
#include "paddle/ir/transforms/dce.h"
// NOTE(zhangbo9674): File pd_op.h is generated by op_gen.py, see details in
// paddle/fluid/ir/dialect/CMakeLists.txt.
...
...
@@ -235,7 +237,7 @@ class TestPass : public ir::Pass {
ir
::
FrozenRewritePatternSet
frozen_ps
(
std
::
move
(
ps
));
ir
::
GreedyRewriteConfig
cfg
;
cfg
.
use_top_down_traversal
=
true
;
cfg
.
max_iterations
=
1
;
cfg
.
max_iterations
=
1
0
;
ir
::
ApplyPatternsGreedily
(
op
->
region
(
0
),
frozen_ps
,
cfg
);
}
...
...
@@ -255,10 +257,10 @@ void BuildProgram(ir::Builder &builder) { // NOLINT
auto
transpose1_op
=
builder
.
Build
<
paddle
::
dialect
::
TransposeOp
>
(
full_op_output
,
std
::
vector
<
int
>
{
0
,
2
,
3
,
1
});
builder
.
Build
<
paddle
::
dialect
::
TransposeOp
>
(
transpose1_op
.
out
(),
std
::
vector
<
int
>
{
0
,
3
,
1
,
2
});
auto
transpose2_op
=
builder
.
Build
<
paddle
::
dialect
::
TransposeOp
>
(
transpose1_op
.
out
(),
std
::
vector
<
int
>
{
0
,
3
,
1
,
2
});
// builder.Build<paddle::dialect::FetchOp>(transpose2_op.out()
);
builder
.
Build
<
paddle
::
dialect
::
FetchOp
>
(
transpose2_op
.
out
(),
"out"
);
}
// TODO(wilber): Add a normal test.
...
...
@@ -268,10 +270,11 @@ TEST(PatternRewrite, GreedyPatternRewriteDriver) {
ir
::
Program
program
(
ctx
);
ir
::
Builder
builder
=
ir
::
Builder
(
ctx
,
program
.
block
());
BuildProgram
(
builder
);
EXPECT_EQ
(
program
.
block
()
->
size
(),
3
u
);
EXPECT_EQ
(
program
.
block
()
->
size
(),
4
u
);
ir
::
PassManager
pm
(
ctx
);
pm
.
AddPass
(
std
::
make_unique
<
TestPass
>
());
pm
.
AddPass
(
ir
::
CreateDCEPass
());
std
::
stringstream
o1
,
o2
;
program
.
Print
(
o1
);
LOG
(
INFO
)
<<
o1
.
str
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录