diff --git a/cmake/external/llvm.cmake b/cmake/external/llvm.cmake index a7a9e85ffd7314ac7026fccdf45fae2fa3de09d3..9f6fd32ad986c4a5911b1d00dfb548fa3320c34d 100644 --- a/cmake/external/llvm.cmake +++ b/cmake/external/llvm.cmake @@ -100,8 +100,8 @@ endfunction() function(mlir_add_rewriter td_base) set(LLVM_TARGET_DEFINITIONS ${td_base}.td) mlir_tablegen(${td_base}.cpp.inc -gen-rewriters "-I${CMAKE_SOURCE_DIR}/infrt/dialect/pass") - add_public_tablegen_target(${td_base}_IncGen) - add_custom_target(${td_base}_inc DEPENDS ${td_base}_IncGen) + add_public_tablegen_target(MLIR${td_base}IncGen) + add_dependencies(mlir-headers MLIR${td_base}IncGen) endfunction() # Execute the mlir script with infrt-exec program. diff --git a/paddle/infrt/CMakeLists.txt b/paddle/infrt/CMakeLists.txt index f2768f3dfa88d3405008baa7662f5e209ca3954c..ed29b5b44c7791d356ec1283a0027cacf1fd5e7a 100644 --- a/paddle/infrt/CMakeLists.txt +++ b/paddle/infrt/CMakeLists.txt @@ -95,9 +95,7 @@ set(infrt_mlir_incs dense_tensor_inc pd_ops_inc pd_extra_ops_inc - rewrite_inc trt_ops_inc - pd_lower_to_trt_inc ) if (INFRT_WITH_PHI) diff --git a/paddle/infrt/dialect/infrt/CMakeLists.txt b/paddle/infrt/dialect/infrt/CMakeLists.txt index daf710e0baf54549a2cc3e7a6e87c7b76a169f29..08ce2d4707bfdc8498610793437675ae8238475e 100644 --- a/paddle/infrt/dialect/infrt/CMakeLists.txt +++ b/paddle/infrt/dialect/infrt/CMakeLists.txt @@ -13,3 +13,5 @@ mlir_tablegen(infrt_opsAttributes.h.inc -gen-attrdef-decls -dialect=infrt) mlir_tablegen(infrt_opsAttributes.cpp.inc -gen-attrdef-defs -dialect=infrt) add_public_tablegen_target(MLIRinfrt_opsAttributesIncGen) add_dependencies(mlir-headers MLIRinfrt_opsAttributesIncGen) + +add_subdirectory(pass) diff --git a/paddle/infrt/dialect/infrt/pass/CMakeLists.txt b/paddle/infrt/dialect/infrt/pass/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..19c12251a2e6b4a71211fe88772d3b6759164c71 --- /dev/null +++ b/paddle/infrt/dialect/infrt/pass/CMakeLists.txt @@ -0,0 +1,7 @@ +core_gather_headers() + +gather_srcs(infrt_src SRCS + infrt_op_fuse_pass.cc + ) + +mlir_add_rewriter(infrt_op_fuse) diff --git a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse.td b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse.td new file mode 100644 index 0000000000000000000000000000000000000000..ef702650b6f1bbd3615ca7a70880d3c2c04e254b --- /dev/null +++ b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse.td @@ -0,0 +1,23 @@ +#ifndef INFRT_OP_FUSE +#define INFRT_OP_FUSE + +include "mlir/Interfaces/SideEffectInterfaces.td" +include "paddle/infrt/dialect/infrt/infrt_ops.td" +include "paddle/infrt/dialect/pd_ops.td" + +def FuseCvtTensorPattern : Pat< + (Infrt_CvtTensorOp (Infrt_CvtTensorOp $arg)), + (Infrt_CvtTensorOp $arg)>; + +def FuseFeedCvtTensorPattern : Pat< + (Infrt_CvtTensorOp (PD_FeedOp $name)), + (PD_FeedOp $name)>; + +def TypesAreIdentical : Constraint>; +def RedundantCvtTensorOptPattern : Pat< + (Infrt_CvtTensorOp:$res $arg), (replaceWithValue $arg), + [(TypesAreIdentical $res, $arg)]>; + + + +#endif // INFRT_OP_FUSE diff --git a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..cb16e054418b3b2c6ff843fdaf464d24a42249c2 --- /dev/null +++ b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc @@ -0,0 +1,52 @@ +// Copyright (c) 2022 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/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h" + +#include +#include "paddle/infrt/dialect/infrt/infrt_dialect.h" +#include "paddle/infrt/dialect/pd_ops.h" +namespace { +#include "paddle/infrt/dialect/infrt/pass/infrt_op_fuse.cpp.inc" // NOLINT + +/* + * infrtOpFusePass. + */ +struct InfrtOpFusePass + : public mlir::PassWrapper { + public: + ::llvm::StringRef getName() const override { return "infrtOpFusePass"; } + void runOnFunction() override; +}; +// Implementation of the InfrtOpFusePass. +void InfrtOpFusePass::runOnFunction() { + ::mlir::RewritePatternSet patterns(&getContext()); + populateWithGenerated(patterns); + (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)); + // Fuse pd.return Operation + auto terminator_op = getFunction().front().getTerminator(); + if (nullptr == terminator_op) return; + for (auto operand : terminator_op->getOperands()) { + auto *op1 = operand.getDefiningOp(); + auto cvt_op = ::llvm::dyn_cast<::infrt::CvtTensorOp>(op1); + if (!cvt_op) continue; + mlir::Value value = cvt_op.input(); + operand.replaceAllUsesWith(value); + cvt_op.erase(); + } +} +} // namespace +std::unique_ptr infrt::createInfrtOpFusePass() { + return std::make_unique(); +} diff --git a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..ef349a7bbc4c6531bb7c02cb69a1bd5c427af080 --- /dev/null +++ b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h @@ -0,0 +1,24 @@ +// Copyright (c) 2021 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 + +namespace infrt { +/* + * infrtOpFusePass. + */ +std::unique_ptr createInfrtOpFusePass(); + +} // namespace infrt diff --git a/paddle/infrt/dialect/pd_op_base.td b/paddle/infrt/dialect/pd_op_base.td index 266bdf60de788df0507a5bf0ef679945cb7c2abc..26425e3945caa2f85547b7b8e8be7dbeaf10e630 100644 --- a/paddle/infrt/dialect/pd_op_base.td +++ b/paddle/infrt/dialect/pd_op_base.td @@ -75,7 +75,7 @@ def PD_ElementType : Type; def PD_Tensor1 : TensorOf<[PD_ElementType]>; -def PD_Tensor : AnyTypeOf<[PD_Tensor1, LoDTensor],"pd.ttype">; +def PD_Tensor : AnyTypeOf<[PD_Tensor1, LoDTensor, DenseTensor],"pd.ttype">; def PD_Tensor_Array : VectorOf<[PD_Tensor]>; diff --git a/paddle/infrt/dialect/phi/phi_ir_exec.cc b/paddle/infrt/dialect/phi/phi_ir_exec.cc index 1df929895b1c704644a7ef136d939996249eba7f..559fb90a64a7868c9c150e12e881d73df7a4aaf2 100644 --- a/paddle/infrt/dialect/phi/phi_ir_exec.cc +++ b/paddle/infrt/dialect/phi/phi_ir_exec.cc @@ -16,6 +16,7 @@ #include #include #include "paddle/infrt/common/global.h" +#include "paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h" #include "paddle/infrt/dialect/mlir_loader.h" #include "paddle/infrt/dialect/phi/pass/phi_op_cvt_pass.h" @@ -38,6 +39,7 @@ int main(int argc, char** argv) { infrt::PrecisionType::FLOAT32, infrt::LayoutType::NCHW}}; phi_pass_manager.addPass(std::make_unique(valid_places)); + phi_pass_manager.addPass(infrt::createInfrtOpFusePass()); if (mlir::failed(pm.run(*module))) { std::cout << "\npass failed!\n" << std::endl; return 4; diff --git a/paddle/infrt/tests/dialect/pten/dense_tensor.mlir b/paddle/infrt/tests/dialect/phi/dense_tensor.mlir similarity index 100% rename from paddle/infrt/tests/dialect/pten/dense_tensor.mlir rename to paddle/infrt/tests/dialect/phi/dense_tensor.mlir diff --git a/paddle/infrt/tests/dialect/pten/pten_pass.mlir b/paddle/infrt/tests/dialect/phi/phi_pass.mlir similarity index 100% rename from paddle/infrt/tests/dialect/pten/pten_pass.mlir rename to paddle/infrt/tests/dialect/phi/phi_pass.mlir diff --git a/tools/infrt/custom_pdop.td b/tools/infrt/custom_pdop.td index 83e2957831296932242e984f83110fba897e5c03..2139fbd8155bb067c542f1e54f135c828e06cc58 100644 --- a/tools/infrt/custom_pdop.td +++ b/tools/infrt/custom_pdop.td @@ -1,4 +1,4 @@ -def PD_FeedOp : PD_Op<"feed"> { +def PD_FeedOp : PD_Op<"feed", [NoSideEffect]> { let summary = "Feed Op"; let description = [{