From 24a3cb521fc8ca2b7f9ef1916647819b6bd0fce5 Mon Sep 17 00:00:00 2001 From: zhangbo9674 <82555433+zhangbo9674@users.noreply.github.com> Date: Tue, 20 Jun 2023 12:59:54 +0800 Subject: [PATCH] [IR] Change IR from Static library to dynamic library (#54729) * new_ir to shared * refine code * add ir lib path to env * refine type * refine code * fix bug * fix bug * refine code * refine code * close win * refine code * refine code * refine code * add win share * refine code * refie code * refine code * refine code * refien code * fix bug * fix bug * fix bug * solve conflict * solve conflict * fix bug * refine code * fix bug * fix bug * fix bug * fix bug * fix bug * refine code * fix interpretercore program bug * delete unuse code * fix bug * fix bug * fix bug * fix bug * fix bug * fix bug * fix bug * fix bug * fix cinn bug * fix cinn bug * debug * fix cinn bug * delete unused code * fix cinn bug * fix cinn bug * fix ug * test win openblas * test win openblas * fix win openblas bug * polish code * fix win open blas bug * close win dll * fix flag bug * test for windows * fix compile bug --- CMakeLists.txt | 2 +- paddle/fluid/CMakeLists.txt | 6 +- .../distributed/fleet_executor/interceptor.h | 2 + .../eager_manual/nodes/multiply_node.cc | 1 + .../eager/auto_code_generator/CMakeLists.txt | 8 ++ .../generator/eager_gen.py | 1 - .../eager/to_static/run_program_op_node.h | 3 + paddle/fluid/framework/executor_cache.cc | 4 +- paddle/fluid/framework/executor_cache.h | 3 +- .../framework/new_executor/CMakeLists.txt | 2 +- .../interpreter/interpreter_util.h | 1 - .../framework/new_executor/interpretercore.cc | 2 + .../framework/new_executor/interpretercore.h | 5 +- .../framework/paddle2cinn/cinn_compiler.cc | 3 + paddle/fluid/inference/CMakeLists.txt | 21 +++-- paddle/fluid/ir/dialect/CMakeLists.txt | 2 +- paddle/fluid/ir/dialect/kernel_attribute.cc | 17 ++++ paddle/fluid/ir/dialect/kernel_attribute.h | 2 + paddle/fluid/ir/dialect/kernel_dialect.cc | 2 + paddle/fluid/ir/dialect/kernel_dialect.h | 2 + paddle/fluid/ir/dialect/kernel_op.cc | 2 + paddle/fluid/ir/dialect/kernel_op.h | 2 + paddle/fluid/ir/dialect/kernel_type.cc | 2 + paddle/fluid/ir/dialect/kernel_type.h | 2 + paddle/fluid/ir/dialect/op_gen.py | 30 ++++++- paddle/fluid/ir/dialect/pd_attribute.cc | 6 ++ paddle/fluid/ir/dialect/pd_attribute.h | 6 ++ paddle/fluid/ir/dialect/pd_dialect.cc | 7 +- paddle/fluid/ir/dialect/pd_dialect.h | 3 + paddle/fluid/ir/dialect/pd_type.cc | 2 + paddle/fluid/ir/dialect/pd_type.h | 2 + paddle/fluid/ir/interface/CMakeLists.txt | 2 +- paddle/fluid/ir/interface/infershape.h | 2 + paddle/fluid/ir/interface/interface.cc | 19 +++++ paddle/fluid/ir/interface/op_yaml_info.h | 2 + paddle/fluid/ir/pass/CMakeLists.txt | 2 +- .../ir/phi_kernel_adaptor/CMakeLists.txt | 2 +- .../ir_adaptor/translator/CMakeLists.txt | 2 +- paddle/fluid/jit/engine/interpreter_engine.cc | 2 + .../operators/cinn/cinn_launch_context.cc | 3 + .../operators/cinn/cinn_launch_context.h | 2 +- paddle/fluid/operators/cinn/cinn_launch_op.h | 3 + paddle/fluid/platform/CMakeLists.txt | 2 +- paddle/fluid/pybind/CMakeLists.txt | 12 ++- paddle/fluid/pybind/tensor_py.h | 1 + paddle/ir/CMakeLists.txt | 82 ++++++++++++++++++- paddle/ir/core/CMakeLists.txt | 2 +- paddle/ir/core/attribute.h | 6 +- paddle/ir/core/attribute_base.h | 6 +- paddle/ir/core/block.h | 3 +- paddle/ir/core/builder.h | 10 +-- paddle/ir/core/builtin_attribute.cc | 9 ++ paddle/ir/core/builtin_attribute.h | 25 ++++-- paddle/ir/core/builtin_dialect.cc | 2 + paddle/ir/core/builtin_dialect.h | 4 +- paddle/ir/core/builtin_op.cc | 8 ++ paddle/ir/core/builtin_op.h | 22 +++-- paddle/ir/core/builtin_type.cc | 11 +++ paddle/ir/core/builtin_type.h | 47 +++++++---- paddle/ir/core/dialect.h | 2 +- paddle/ir/core/dialect_interface.h | 2 +- paddle/ir/core/dll_decl.h | 27 ++++++ paddle/ir/core/ir_context.cc | 2 + paddle/ir/core/ir_context.h | 7 +- paddle/ir/core/ir_printer.h | 2 +- paddle/ir/core/op_base.h | 4 +- paddle/ir/core/op_info.h | 4 +- paddle/ir/core/operation.h | 2 +- paddle/ir/core/parameter.h | 2 +- paddle/ir/core/program.h | 2 +- paddle/ir/core/region.h | 4 +- paddle/ir/core/storage_manager.h | 2 +- paddle/ir/core/type.h | 6 +- paddle/ir/core/type_base.h | 6 +- paddle/ir/core/type_id.h | 61 +++++++++++++- paddle/ir/core/utils.h | 4 +- paddle/ir/core/value.h | 6 +- paddle/ir/pass/CMakeLists.txt | 5 +- paddle/ir/pass/analysis_manager.h | 4 + paddle/ir/pass/pass.cc | 2 + paddle/ir/pass/pass.h | 9 +- paddle/ir/pass/pass_instrumentation.h | 2 +- paddle/ir/pass/pass_manager.h | 2 +- paddle/ir/pattern_rewrite/CMakeLists.txt | 5 +- paddle/ir/pattern_rewrite/pattern_match.h | 4 +- paddle/phi/api/yaml/legacy_ops.yaml | 12 --- paddle/phi/api/yaml/ops.yaml | 12 +++ paddle/scripts/paddle_build.bat | 2 +- python/env_dict.py.in | 3 + python/setup.py.in | 12 ++- setup.py | 22 ++++- test/CMakeLists.txt | 5 +- .../fluid/cinn/cinn_launch_context_test.cc | 3 + test/cpp/ir/CMakeLists.txt | 4 - test/cpp/ir/core/CMakeLists.txt | 24 +++--- test/cpp/ir/core/ir_attribute_test.cc | 18 ++-- test/cpp/ir/core/ir_infershape_test.cc | 4 + test/cpp/ir/core/ir_op_test.cc | 11 +++ test/cpp/ir/core/ir_program_test.cc | 2 + test/cpp/ir/core/ir_value_test.cc | 3 +- test/cpp/ir/core/type_test.cc | 35 ++++---- test/cpp/ir/kernel_dialect/CMakeLists.txt | 2 +- test/cpp/ir/pass/CMakeLists.txt | 2 +- test/cpp/ir/pass/pass_manager_test.cc | 2 + test/cpp/ir/pattern_rewrite/CMakeLists.txt | 9 +- .../pattern_rewrite/pattern_rewrite_test.cc | 4 + test/cpp/new_executor/CMakeLists.txt | 2 +- 107 files changed, 627 insertions(+), 190 deletions(-) create mode 100644 paddle/fluid/ir/dialect/kernel_attribute.cc create mode 100644 paddle/fluid/ir/interface/interface.cc create mode 100644 paddle/ir/core/dll_decl.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a2e933c838a29f..3a75d8c35552df 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -307,7 +307,7 @@ option(WITH_CUDNN_FRONTEND "Compile with CUDNN Frontend API support (experimental)" OFF) option(WITH_CUDNN_DSO "Compile PaddlePaddle with cuDNN dynamic-link libraries" OFF) -option(WITH_IR "Compile PaddlePaddle with NEWIR" ON) +option(WITH_SHARED_IR "Compile PaddlePaddle with SHARED LIB of IR" ON) if(WITH_RECORD_BUILDTIME) set_property( diff --git a/paddle/fluid/CMakeLists.txt b/paddle/fluid/CMakeLists.txt index 429f0ec31a9fea..ba37810ae0e7ec 100644 --- a/paddle/fluid/CMakeLists.txt +++ b/paddle/fluid/CMakeLists.txt @@ -8,9 +8,7 @@ add_subdirectory(pybind) add_subdirectory(eager) add_subdirectory(prim) add_subdirectory(jit) -if(WITH_IR) - add_subdirectory(ir) - add_subdirectory(ir_adaptor) -endif() +add_subdirectory(ir) +add_subdirectory(ir_adaptor) # NOTE: please add subdirectory inference at last. add_subdirectory(inference) diff --git a/paddle/fluid/distributed/fleet_executor/interceptor.h b/paddle/fluid/distributed/fleet_executor/interceptor.h index 0d0204c974d5a3..7c9cf9c8112ef0 100644 --- a/paddle/fluid/distributed/fleet_executor/interceptor.h +++ b/paddle/fluid/distributed/fleet_executor/interceptor.h @@ -29,6 +29,8 @@ #include "paddle/fluid/platform/errors.h" #include "paddle/fluid/platform/macros.h" #include "paddle/fluid/platform/place.h" +#include "paddle/ir/core/program.h" +#include "paddle/ir/core/value.h" namespace paddle { namespace framework { diff --git a/paddle/fluid/eager/api/manual/eager_manual/nodes/multiply_node.cc b/paddle/fluid/eager/api/manual/eager_manual/nodes/multiply_node.cc index 44c646e5be4482..ea849f7e4846aa 100644 --- a/paddle/fluid/eager/api/manual/eager_manual/nodes/multiply_node.cc +++ b/paddle/fluid/eager/api/manual/eager_manual/nodes/multiply_node.cc @@ -20,6 +20,7 @@ #include "paddle/fluid/eager/utils.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/imperative/tracer.h" +#include "paddle/fluid/platform/profiler/event_tracing.h" #include "paddle/fluid/prim/api/all.h" #include "paddle/fluid/prim/api/composite_backward/composite_backward_api.h" #include "paddle/fluid/prim/utils/utils.h" diff --git a/paddle/fluid/eager/auto_code_generator/CMakeLists.txt b/paddle/fluid/eager/auto_code_generator/CMakeLists.txt index 51ab946f4f0f2e..e27310dea5629b 100644 --- a/paddle/fluid/eager/auto_code_generator/CMakeLists.txt +++ b/paddle/fluid/eager/auto_code_generator/CMakeLists.txt @@ -61,6 +61,14 @@ if(WIN32) list(APPEND EAGER_CODEGEN_DEPS ${eager_generator_path}/phi.dll) endif() + if(WITH_SHARED_IR) + add_custom_command( + OUTPUT ${eager_generator_path}/ir.dll + COMMAND ${CMAKE_COMMAND} -E copy ${IR_LIB} ${eager_generator_path} + DEPENDS ir) + list(APPEND EAGER_CODEGEN_DEPS ${eager_generator_path}/ir.dll) + endif() + if(${CBLAS_PROVIDER} STREQUAL MKLML) message("Copied libiomp5md.dll for Eager AutoCodeGen") add_custom_command( diff --git a/paddle/fluid/eager/auto_code_generator/generator/eager_gen.py b/paddle/fluid/eager/auto_code_generator/generator/eager_gen.py index 8cbd3df77e2f67..fe3f73c845e446 100644 --- a/paddle/fluid/eager/auto_code_generator/generator/eager_gen.py +++ b/paddle/fluid/eager/auto_code_generator/generator/eager_gen.py @@ -409,7 +409,6 @@ class {} : public egr::GradNodeBase {{ #include "paddle/phi/api/all.h" #include "paddle/fluid/eager/utils.h" #include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/eager/to_static/run_program_op_func.h" #include "paddle/fluid/eager/api/manual/eager_manual/dygraph_forward_api.h" using CPUPlace = phi::CPUPlace; diff --git a/paddle/fluid/eager/to_static/run_program_op_node.h b/paddle/fluid/eager/to_static/run_program_op_node.h index 548af27a8ac4ff..574adc1f9d4d47 100644 --- a/paddle/fluid/eager/to_static/run_program_op_node.h +++ b/paddle/fluid/eager/to_static/run_program_op_node.h @@ -17,10 +17,13 @@ #include "paddle/fluid/eager/api/utils/global_utils.h" #include "paddle/fluid/eager/grad_node_info.h" #include "paddle/fluid/eager/tensor_wrapper.h" +#include "paddle/fluid/framework/new_executor/interpretercore.h" #include "paddle/fluid/framework/variable_helper.h" #include "paddle/fluid/operators/run_program_op.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/profiler/event_tracing.h" +#include "paddle/ir/core/program.h" +#include "paddle/ir/core/value.h" namespace details { using Tensor = paddle::Tensor; diff --git a/paddle/fluid/framework/executor_cache.cc b/paddle/fluid/framework/executor_cache.cc index d63921977e9544..9e8f4a25873d18 100644 --- a/paddle/fluid/framework/executor_cache.cc +++ b/paddle/fluid/framework/executor_cache.cc @@ -13,8 +13,10 @@ // limitations under the License. #include "paddle/fluid/framework/executor_cache.h" - +#include "paddle/fluid/framework/new_executor/interpretercore.h" #include "paddle/fluid/framework/op_info.h" +#include "paddle/ir/core/program.h" +#include "paddle/ir/core/value.h" namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/executor_cache.h b/paddle/fluid/framework/executor_cache.h index 420ccf4ee84c4d..f4d926d74c1466 100644 --- a/paddle/fluid/framework/executor_cache.h +++ b/paddle/fluid/framework/executor_cache.h @@ -23,7 +23,6 @@ #include #include -#include "paddle/fluid/framework/new_executor/interpretercore.h" #include "paddle/fluid/framework/op_proto_maker.h" #include "paddle/fluid/framework/parallel_executor.h" #include "paddle/fluid/framework/program_desc.h" @@ -36,6 +35,8 @@ namespace ir { class Graph; } +class InterpreterCore; + namespace details { void AppendSkipDeletionVars(const std::vector& append_vars, std::vector* all_vars); diff --git a/paddle/fluid/framework/new_executor/CMakeLists.txt b/paddle/fluid/framework/new_executor/CMakeLists.txt index 54902b86c3c483..05f820f1bf9ae8 100644 --- a/paddle/fluid/framework/new_executor/CMakeLists.txt +++ b/paddle/fluid/framework/new_executor/CMakeLists.txt @@ -14,7 +14,7 @@ set(STANDALONE_EXECUTOR_DEPS pd_op_to_kernel_pass phi_kernel_adaptor program_translator - new_ir) + ir) cc_library( standalone_executor diff --git a/paddle/fluid/framework/new_executor/interpreter/interpreter_util.h b/paddle/fluid/framework/new_executor/interpreter/interpreter_util.h index fb32b4f80d9615..28dc8d92b14ab7 100644 --- a/paddle/fluid/framework/new_executor/interpreter/interpreter_util.h +++ b/paddle/fluid/framework/new_executor/interpreter/interpreter_util.h @@ -38,7 +38,6 @@ #include "paddle/fluid/framework/variable_helper.h" #include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/init.h" -#include "paddle/ir/core/program.h" using AtomicVectorSizeT = std::vector>; diff --git a/paddle/fluid/framework/new_executor/interpretercore.cc b/paddle/fluid/framework/new_executor/interpretercore.cc index 6a5e92b1108cb8..ab68a477954d5a 100644 --- a/paddle/fluid/framework/new_executor/interpretercore.cc +++ b/paddle/fluid/framework/new_executor/interpretercore.cc @@ -16,6 +16,8 @@ #include "paddle/fluid/framework/new_executor/new_ir_interpreter.h" #include "paddle/fluid/framework/new_executor/program_interpreter.h" +#include "paddle/ir/core/program.h" +#include "paddle/ir/core/value.h" PADDLE_DEFINE_EXPORTED_bool( new_executor_serial_run, diff --git a/paddle/fluid/framework/new_executor/interpretercore.h b/paddle/fluid/framework/new_executor/interpretercore.h index af12b0904f3f37..69e0e91023791c 100644 --- a/paddle/fluid/framework/new_executor/interpretercore.h +++ b/paddle/fluid/framework/new_executor/interpretercore.h @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. #pragma once + #include "paddle/fluid/framework/new_executor/interpreter_base_impl.h" DECLARE_bool(new_executor_use_local_scope); @@ -20,10 +21,6 @@ namespace ir { class Program; } // namespace ir -namespace ir { -class Program; -} // namespace ir - namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc b/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc index c0013cfb99eecb..2cb74b2eccd513 100644 --- a/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc +++ b/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc @@ -38,6 +38,7 @@ #include "paddle/fluid/framework/ir/graph_helper.h" #include "paddle/fluid/framework/ir/node.h" #include "paddle/fluid/framework/lod_tensor.h" +#include "paddle/fluid/framework/new_executor/interpretercore.h" #include "paddle/fluid/framework/paddle2cinn/build_cinn_pass.h" #include "paddle/fluid/framework/paddle2cinn/cinn_graph_symbolization.h" #include "paddle/fluid/framework/paddle2cinn/transform_desc.h" @@ -47,6 +48,8 @@ #include "paddle/fluid/operators/cinn/cinn_launch_context.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/string/string_helper.h" +#include "paddle/ir/core/program.h" +#include "paddle/ir/core/value.h" #include "paddle/phi/core/flags.h" PHI_DECLARE_bool(enable_pe_launch_cinn); diff --git a/paddle/fluid/inference/CMakeLists.txt b/paddle/fluid/inference/CMakeLists.txt index e1de44fa37b2b0..5cb37102fade87 100644 --- a/paddle/fluid/inference/CMakeLists.txt +++ b/paddle/fluid/inference/CMakeLists.txt @@ -33,6 +33,7 @@ endif() # fluid_modules exclude API-interface of inference/api and inference/capi_exp get_property(fluid_modules GLOBAL PROPERTY FLUID_MODULES) get_property(phi_modules GLOBAL PROPERTY PHI_MODULES) +get_property(ir_targets GLOBAL PROPERTY IR_TARGETS) set(utils_modules pretty_log string_helper benchmark utf8proc) add_subdirectory(api) @@ -58,15 +59,20 @@ set(KERNEL_LIST #windows GPU static library over the limit, so not create_static_lib, and cc_library is dummy if(WIN32 AND WITH_GPU) - cc_library(paddle_inference DEPS ${fluid_modules} new_ir + cc_library(paddle_inference DEPS ${fluid_modules} ${ir_targets} ${STATIC_INFERENCE_API} ${utils_modules}) else() # message("${fluid_modules}") # message("${STATIC_INFERENCE_API}") # message("${utils_modules}") # message("${phi_modules}") - create_static_lib(paddle_inference ${phi_modules} ${fluid_modules} new_ir - ${STATIC_INFERENCE_API} ${utils_modules}) + if(WIN32) + create_static_lib(paddle_inference ${phi_modules} ${fluid_modules} + ${STATIC_INFERENCE_API} ${utils_modules}) + else() + create_static_lib(paddle_inference ${phi_modules} ${fluid_modules} + ${ir_targets} ${STATIC_INFERENCE_API} ${utils_modules}) + endif() endif() if(NOT APPLE) @@ -96,8 +102,13 @@ set(SHARED_INFERENCE_SRCS # shared inference library deps list(REMOVE_ITEM fluid_modules standalone_executor interpretercore_garbage_collector) -set(SHARED_INFERENCE_DEPS phi new_ir ${fluid_modules} analysis_predictor - ${utils_modules}) +if(WIN32) + set(SHARED_INFERENCE_DEPS phi ${fluid_modules} analysis_predictor + ${utils_modules}) +else() + set(SHARED_INFERENCE_DEPS phi ${fluid_modules} ${ir_targets} + analysis_predictor ${utils_modules}) +endif() if(WITH_CRYPTO) set(SHARED_INFERENCE_DEPS ${SHARED_INFERENCE_DEPS} paddle_crypto) diff --git a/paddle/fluid/ir/dialect/CMakeLists.txt b/paddle/fluid/ir/dialect/CMakeLists.txt index 9d180a23bd2861..8fa488fc14720f 100644 --- a/paddle/fluid/ir/dialect/CMakeLists.txt +++ b/paddle/fluid/ir/dialect/CMakeLists.txt @@ -50,5 +50,5 @@ file(GLOB PD_DIALECT_SRCS "*.cc") cc_library( pd_dialect SRCS ${PD_DIALECT_SRCS} ${op_source_file} - DEPS new_ir framework_proto phi phi_utils) + DEPS framework_proto phi phi_utils pd_interface ir) target_include_directories(pd_dialect PRIVATE ${PD_DIALECT_BINARY_DIR}) diff --git a/paddle/fluid/ir/dialect/kernel_attribute.cc b/paddle/fluid/ir/dialect/kernel_attribute.cc new file mode 100644 index 00000000000000..3109df13935957 --- /dev/null +++ b/paddle/fluid/ir/dialect/kernel_attribute.cc @@ -0,0 +1,17 @@ +// 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/fluid/ir/dialect/kernel_attribute.h" + +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::KernelAttribute) diff --git a/paddle/fluid/ir/dialect/kernel_attribute.h b/paddle/fluid/ir/dialect/kernel_attribute.h index d22bc9ff949270..c01326d41932fa 100644 --- a/paddle/fluid/ir/dialect/kernel_attribute.h +++ b/paddle/fluid/ir/dialect/kernel_attribute.h @@ -36,3 +36,5 @@ class KernelAttribute : public ir::Attribute { } // namespace dialect } // namespace paddle + +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::KernelAttribute) diff --git a/paddle/fluid/ir/dialect/kernel_dialect.cc b/paddle/fluid/ir/dialect/kernel_dialect.cc index be7ebceda75428..8578ddfa6a57af 100644 --- a/paddle/fluid/ir/dialect/kernel_dialect.cc +++ b/paddle/fluid/ir/dialect/kernel_dialect.cc @@ -69,3 +69,5 @@ void PaddleKernelDialect::PrintAttribute(ir::Attribute attr, } // namespace dialect } // namespace paddle + +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::PaddleKernelDialect) diff --git a/paddle/fluid/ir/dialect/kernel_dialect.h b/paddle/fluid/ir/dialect/kernel_dialect.h index 2cbbee316d75aa..401b6da4361d10 100644 --- a/paddle/fluid/ir/dialect/kernel_dialect.h +++ b/paddle/fluid/ir/dialect/kernel_dialect.h @@ -37,3 +37,5 @@ class PaddleKernelDialect : public ir::Dialect { } // namespace dialect } // namespace paddle + +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::PaddleKernelDialect) diff --git a/paddle/fluid/ir/dialect/kernel_op.cc b/paddle/fluid/ir/dialect/kernel_op.cc index eacf5347086455..b7bb3d663b7d81 100644 --- a/paddle/fluid/ir/dialect/kernel_op.cc +++ b/paddle/fluid/ir/dialect/kernel_op.cc @@ -34,3 +34,5 @@ void PhiKernelOp::Verify(const std::vector &inputs, } // namespace dialect } // namespace paddle + +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::PhiKernelOp) diff --git a/paddle/fluid/ir/dialect/kernel_op.h b/paddle/fluid/ir/dialect/kernel_op.h index 4fc297e8d656e6..b3b0fe4187a1b1 100644 --- a/paddle/fluid/ir/dialect/kernel_op.h +++ b/paddle/fluid/ir/dialect/kernel_op.h @@ -33,3 +33,5 @@ class PhiKernelOp : public ir::Op { } // namespace dialect } // namespace paddle + +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::PhiKernelOp) diff --git a/paddle/fluid/ir/dialect/kernel_type.cc b/paddle/fluid/ir/dialect/kernel_type.cc index 2aa4b32137dcea..59009eab2b17df 100644 --- a/paddle/fluid/ir/dialect/kernel_type.cc +++ b/paddle/fluid/ir/dialect/kernel_type.cc @@ -43,3 +43,5 @@ const size_t& AllocatedDenseTensorType::offset() const { } // namespace dialect } // namespace paddle + +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::AllocatedDenseTensorType) diff --git a/paddle/fluid/ir/dialect/kernel_type.h b/paddle/fluid/ir/dialect/kernel_type.h index a0a6be19605589..3bbae7f7f24683 100644 --- a/paddle/fluid/ir/dialect/kernel_type.h +++ b/paddle/fluid/ir/dialect/kernel_type.h @@ -66,3 +66,5 @@ class AllocatedDenseTensorType : public ir::Type { } // namespace dialect } // namespace paddle + +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::AllocatedDenseTensorType) diff --git a/paddle/fluid/ir/dialect/op_gen.py b/paddle/fluid/ir/dialect/op_gen.py index 9e0b08f393bcde..6aec43871bdbaa 100644 --- a/paddle/fluid/ir/dialect/op_gen.py +++ b/paddle/fluid/ir/dialect/op_gen.py @@ -42,12 +42,18 @@ #include "paddle/phi/core/infermeta_utils.h" {input} + +{declare_type_id} #endif """ GET_OP_LIST_TEMPALTE = """{} """ +DECLARE_OP_TYPE_ID = """ +IR_DECLARE_EXPLICIT_TYPE_ID({op_name}) +""" + OP_DECLARE_TEMPLATE = """ class {op_name} : public ir::Op<{op_name}{interfaces}{traits}> {{ public: @@ -98,6 +104,8 @@ class {op_name} : public ir::Op<{op_name}{interfaces}{traits}> {{ #include "paddle/phi/api/lib/utils/allocator.h" {input} + +{define_type_id} """ OP_N_ATTRIBUTE_DEFINED_TEMPLATE = """ @@ -238,6 +246,10 @@ class {op_name} : public ir::Op<{op_name}{interfaces}{traits}> {{ }} """ +DEFINE_OP_TYPE_ID = """ +IR_DEFINE_EXPLICIT_TYPE_ID({op_name}) +""" + def to_phi_and_fluid_op_name(op_item): # Templat: - op : phi_name (fluid_name) @@ -1701,6 +1713,11 @@ def OpGenerator( op_list_str = GET_OP_LIST_TEMPALTE.format( ", ".join(ops_name_with_namespace_list) ) # Add GET_OP_LIST + + declare_type_id_str = "" + for op in ops_name_with_namespace_list: + declare_type_id_str += DECLARE_OP_TYPE_ID.format(op_name=op) + head_file_str = "" head_file_str += "".join(ops_declare_list) # Add op class for name in reversed(namespaces): @@ -1708,7 +1725,9 @@ def OpGenerator( namespace=name, input=head_file_str ) # Add namespaces head_file_str = H_FILE_TEMPLATE.format( - op_declare=op_list_str, input=head_file_str + op_declare=op_list_str, + input=head_file_str, + declare_type_id=declare_type_id_str, ) # Add head # (5) Generate source file str @@ -1717,8 +1736,15 @@ def OpGenerator( source_file_str = NAMESPACE_GARD_TEMPLATE.format( namespace=name, input=source_file_str ) # Add namespaces + + define_type_id_str = "" + for op in ops_name_with_namespace_list: + define_type_id_str += DEFINE_OP_TYPE_ID.format(op_name=op) + source_file_str = CC_FILE_TEMPLATE.format( - h_file=op_def_h_file[:-4], input=source_file_str + h_file=op_def_h_file[:-4], + input=source_file_str, + define_type_id=define_type_id_str, ) # Add head # (5) Generate pd_op.h.tmp, pd_op.cc.tmp diff --git a/paddle/fluid/ir/dialect/pd_attribute.cc b/paddle/fluid/ir/dialect/pd_attribute.cc index 08f552683d1b21..78a71013b87b53 100644 --- a/paddle/fluid/ir/dialect/pd_attribute.cc +++ b/paddle/fluid/ir/dialect/pd_attribute.cc @@ -46,3 +46,9 @@ phi::Scalar ScalarAttribute::data() { } // namespace dialect } // namespace paddle + +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::IntArrayAttribute) +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::ScalarAttribute) +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::DataTypeAttribute) +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::PlaceAttribute) +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::DataLayoutAttribute) diff --git a/paddle/fluid/ir/dialect/pd_attribute.h b/paddle/fluid/ir/dialect/pd_attribute.h index c95dbe248caf6b..7d921ebd1df1b8 100644 --- a/paddle/fluid/ir/dialect/pd_attribute.h +++ b/paddle/fluid/ir/dialect/pd_attribute.h @@ -94,3 +94,9 @@ class DataLayoutAttribute : public ir::Attribute { } // namespace dialect } // namespace paddle + +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::IntArrayAttribute) +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::ScalarAttribute) +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::DataTypeAttribute) +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::PlaceAttribute) +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::DataLayoutAttribute) diff --git a/paddle/fluid/ir/dialect/pd_dialect.cc b/paddle/fluid/ir/dialect/pd_dialect.cc index b347d85d2a1cf0..e365758e81ba1a 100644 --- a/paddle/fluid/ir/dialect/pd_dialect.cc +++ b/paddle/fluid/ir/dialect/pd_dialect.cc @@ -13,11 +13,11 @@ // limitations under the License. #include "paddle/fluid/ir/dialect/pd_dialect.h" +#include "paddle/fluid/framework/convert_utils.h" +#include "paddle/fluid/framework/data_type.h" #include "paddle/fluid/ir/dialect/pd_attribute.h" // NOTE(zhangbo9674): File pd_op.h is generated by op_gen.py, see details in // paddle/fluid/ir/dialect/CMakeLists.txt. -#include "paddle/fluid/framework/convert_utils.h" -#include "paddle/fluid/framework/data_type.h" #include "paddle/fluid/ir/dialect/pd_op.h" #include "paddle/fluid/ir/dialect/pd_type.h" #include "paddle/fluid/ir/dialect/pd_type_storage.h" @@ -145,3 +145,6 @@ void PaddleDialect::PrintAttribute(ir::Attribute attr, std::ostream &os) const { } // namespace dialect } // namespace paddle + +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::PaddleDialect) +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::ParameterConvertInterface) diff --git a/paddle/fluid/ir/dialect/pd_dialect.h b/paddle/fluid/ir/dialect/pd_dialect.h index b8782c156d8851..a9ca4a497ef21f 100644 --- a/paddle/fluid/ir/dialect/pd_dialect.h +++ b/paddle/fluid/ir/dialect/pd_dialect.h @@ -48,3 +48,6 @@ class PaddleDialect : public ir::Dialect { } // namespace dialect } // namespace paddle + +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::PaddleDialect) +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::ParameterConvertInterface) diff --git a/paddle/fluid/ir/dialect/pd_type.cc b/paddle/fluid/ir/dialect/pd_type.cc index 435427d157c00e..8c96d2a1edc2fa 100644 --- a/paddle/fluid/ir/dialect/pd_type.cc +++ b/paddle/fluid/ir/dialect/pd_type.cc @@ -30,3 +30,5 @@ const size_t& DenseTensorType::offset() const { return storage()->offset_; } } // namespace dialect } // namespace paddle + +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::DenseTensorType) diff --git a/paddle/fluid/ir/dialect/pd_type.h b/paddle/fluid/ir/dialect/pd_type.h index 5de1a8f0e0b551..c0c45ebbecc299 100644 --- a/paddle/fluid/ir/dialect/pd_type.h +++ b/paddle/fluid/ir/dialect/pd_type.h @@ -41,3 +41,5 @@ class DenseTensorType : public ir::Type { } // namespace dialect } // namespace paddle + +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::DenseTensorType) diff --git a/paddle/fluid/ir/interface/CMakeLists.txt b/paddle/fluid/ir/interface/CMakeLists.txt index 4a8f02853641c5..8812bc3675a32b 100644 --- a/paddle/fluid/ir/interface/CMakeLists.txt +++ b/paddle/fluid/ir/interface/CMakeLists.txt @@ -4,4 +4,4 @@ file(GLOB PD_INTERFACE_SRCS "*.cc") cc_library( pd_interface SRCS ${PD_INTERFACE_SRCS} - DEPS new_ir framework_proto dense_tensor phi_utils) + DEPS ir framework_proto phi_utils) diff --git a/paddle/fluid/ir/interface/infershape.h b/paddle/fluid/ir/interface/infershape.h index 7b803abb7216b6..5b4f430413d1e6 100644 --- a/paddle/fluid/ir/interface/infershape.h +++ b/paddle/fluid/ir/interface/infershape.h @@ -48,3 +48,5 @@ class InferShapeInterface : public ir::OpInterfaceBase { } // namespace dialect } // namespace paddle + +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::InferShapeInterface) diff --git a/paddle/fluid/ir/interface/interface.cc b/paddle/fluid/ir/interface/interface.cc new file mode 100644 index 00000000000000..6d2cd0ae17bf62 --- /dev/null +++ b/paddle/fluid/ir/interface/interface.cc @@ -0,0 +1,19 @@ +// 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/fluid/ir/interface/infershape.h" +#include "paddle/fluid/ir/interface/op_yaml_info.h" + +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::InferShapeInterface) +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::OpYamlInfoInterface) diff --git a/paddle/fluid/ir/interface/op_yaml_info.h b/paddle/fluid/ir/interface/op_yaml_info.h index 051659d98b2b10..043243ddb894eb 100644 --- a/paddle/fluid/ir/interface/op_yaml_info.h +++ b/paddle/fluid/ir/interface/op_yaml_info.h @@ -50,3 +50,5 @@ class OpYamlInfoInterface : public ir::OpInterfaceBase { } // namespace dialect } // namespace paddle + +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::OpYamlInfoInterface) diff --git a/paddle/fluid/ir/pass/CMakeLists.txt b/paddle/fluid/ir/pass/CMakeLists.txt index 0d1214afe10c00..47ec2b0992949b 100644 --- a/paddle/fluid/ir/pass/CMakeLists.txt +++ b/paddle/fluid/ir/pass/CMakeLists.txt @@ -4,4 +4,4 @@ file(GLOB PD_PASS_SRCS "*.cc") cc_library( pd_op_to_kernel_pass SRCS ${PD_PASS_SRCS} - DEPS new_ir phi_utils) + DEPS ir phi_utils) diff --git a/paddle/fluid/ir/phi_kernel_adaptor/CMakeLists.txt b/paddle/fluid/ir/phi_kernel_adaptor/CMakeLists.txt index dc048d2917bc96..4b4175f99deb6f 100644 --- a/paddle/fluid/ir/phi_kernel_adaptor/CMakeLists.txt +++ b/paddle/fluid/ir/phi_kernel_adaptor/CMakeLists.txt @@ -4,4 +4,4 @@ file(GLOB PHI_KERNEL_ADAPTOR_SRCS "*.cc") cc_library( phi_kernel_adaptor SRCS ${PHI_KERNEL_ADAPTOR_SRCS} - DEPS new_ir phi_utils) + DEPS ir phi_utils) diff --git a/paddle/fluid/ir_adaptor/translator/CMakeLists.txt b/paddle/fluid/ir_adaptor/translator/CMakeLists.txt index 2f0014c69f74c4..632411383db561 100644 --- a/paddle/fluid/ir_adaptor/translator/CMakeLists.txt +++ b/paddle/fluid/ir_adaptor/translator/CMakeLists.txt @@ -20,4 +20,4 @@ file(GLOB PD_PROGRAM_TRANSLATOR_SRCS "*.cc") cc_library( program_translator SRCS ${PD_PROGRAM_TRANSLATOR_SRCS} ${op_compat_source_file} - DEPS proto_desc pd_dialect new_ir framework_proto) + DEPS proto_desc pd_dialect ir framework_proto) diff --git a/paddle/fluid/jit/engine/interpreter_engine.cc b/paddle/fluid/jit/engine/interpreter_engine.cc index 70d85a9d17f269..2905aef9256bb2 100644 --- a/paddle/fluid/jit/engine/interpreter_engine.cc +++ b/paddle/fluid/jit/engine/interpreter_engine.cc @@ -20,6 +20,8 @@ #include "paddle/fluid/framework/ir/pass.h" #include "paddle/fluid/framework/new_executor/interpretercore.h" #include "paddle/fluid/framework/program_desc.h" +#include "paddle/ir/core/program.h" +#include "paddle/ir/core/value.h" #include "paddle/phi/core/enforce.h" namespace paddle { diff --git a/paddle/fluid/operators/cinn/cinn_launch_context.cc b/paddle/fluid/operators/cinn/cinn_launch_context.cc index d5b28ae0de1c31..a42ea040bee797 100644 --- a/paddle/fluid/operators/cinn/cinn_launch_context.cc +++ b/paddle/fluid/operators/cinn/cinn_launch_context.cc @@ -31,6 +31,7 @@ #include "paddle/fluid/framework/details/execution_strategy.h" #include "paddle/fluid/framework/ir/graph.h" #include "paddle/fluid/framework/lod_tensor.h" +#include "paddle/fluid/framework/new_executor/interpretercore.h" #include "paddle/fluid/framework/paddle2cinn/build_cinn_pass.h" #include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" #include "paddle/fluid/framework/paddle2cinn/transform_type.h" @@ -41,6 +42,8 @@ #include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/place.h" #include "paddle/fluid/string/printf.h" +#include "paddle/ir/core/program.h" +#include "paddle/ir/core/value.h" #include "paddle/phi/core/ddim.h" #include "paddle/utils/string/string_helper.h" diff --git a/paddle/fluid/operators/cinn/cinn_launch_context.h b/paddle/fluid/operators/cinn/cinn_launch_context.h index b90591369ee755..34667bddc423d3 100644 --- a/paddle/fluid/operators/cinn/cinn_launch_context.h +++ b/paddle/fluid/operators/cinn/cinn_launch_context.h @@ -22,7 +22,6 @@ #include #include "paddle/fluid/framework/lod_tensor.h" -#include "paddle/fluid/framework/new_executor/interpretercore.h" #include "paddle/fluid/framework/parallel_executor.h" #include "paddle/fluid/platform/place.h" #include "paddle/phi/core/ddim.h" @@ -41,6 +40,7 @@ namespace framework { class ProgramDesc; class Scope; class VarDesc; +class InterpreterCore; namespace ir { class Graph; diff --git a/paddle/fluid/operators/cinn/cinn_launch_op.h b/paddle/fluid/operators/cinn/cinn_launch_op.h index ce0aa8fb888d2a..9cb7d601809bff 100644 --- a/paddle/fluid/operators/cinn/cinn_launch_op.h +++ b/paddle/fluid/operators/cinn/cinn_launch_op.h @@ -22,12 +22,15 @@ #include "cinn/common/target.h" #include "paddle/fluid/framework/data_type.h" +#include "paddle/fluid/framework/new_executor/interpretercore.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" #include "paddle/fluid/operators/cinn/cinn_launch_context.h" #include "paddle/fluid/operators/cinn/cinn_op_helper.h" #include "paddle/fluid/platform/profiler.h" +#include "paddle/ir/core/program.h" +#include "paddle/ir/core/value.h" #include "paddle/phi/core/flags.h" PHI_DECLARE_bool(enable_pe_launch_cinn); diff --git a/paddle/fluid/platform/CMakeLists.txt b/paddle/fluid/platform/CMakeLists.txt index 5daf484e70d291..a5cf7587c7ce0a 100644 --- a/paddle/fluid/platform/CMakeLists.txt +++ b/paddle/fluid/platform/CMakeLists.txt @@ -104,7 +104,7 @@ endif() cc_library( init SRCS init.cc - DEPS device_context phi memcpy pd_dialect new_ir) + DEPS device_context phi memcpy pd_dialect ir) # memcpy depends on device_context, here add deps individually for # avoiding cycle dependencies diff --git a/paddle/fluid/pybind/CMakeLists.txt b/paddle/fluid/pybind/CMakeLists.txt index 97842928ac581f..21516f3ced3401 100755 --- a/paddle/fluid/pybind/CMakeLists.txt +++ b/paddle/fluid/pybind/CMakeLists.txt @@ -40,8 +40,8 @@ set(PYBIND_DEPS phi phi_kernel_adaptor pd_dialect - new_ir program_translator + ir new_profiler jit_layer jit_property @@ -339,7 +339,15 @@ if(WITH_PYTHON) OUTPUT ${op_impl_path}/phi.dll COMMAND ${CMAKE_COMMAND} -E copy ${PHI_LIB} ${op_impl_path} DEPENDS phi) - list(APPEND EAGER_OP_IMPL_DEPS ${op_impl_path}/libiomp5md.dll) + list(APPEND EAGER_OP_IMPL_DEPS ${op_impl_path}/phi.dll) + endif() + + if(WITH_SHARED_IR) + add_custom_command( + OUTPUT ${op_impl_path}/ir.dll + COMMAND ${CMAKE_COMMAND} -E copy ${IR_LIB} ${op_impl_path} + DEPENDS ir) + list(APPEND EAGER_OP_IMPL_DEPS ${op_impl_path}/ir.dll) endif() if(${CBLAS_PROVIDER} STREQUAL MKLML) diff --git a/paddle/fluid/pybind/tensor_py.h b/paddle/fluid/pybind/tensor_py.h index 6f8ae115bd12ac..ba33fcd1d129fa 100644 --- a/paddle/fluid/pybind/tensor_py.h +++ b/paddle/fluid/pybind/tensor_py.h @@ -46,6 +46,7 @@ limitations under the License. */ #include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/float16.h" #include "paddle/fluid/platform/profiler/event_tracing.h" +#include "paddle/phi/api/lib/utils/allocator.h" #include "paddle/phi/common/pstring.h" #include "paddle/phi/core/string_tensor.h" #include "paddle/phi/kernels/strings/unicode.h" diff --git a/paddle/ir/CMakeLists.txt b/paddle/ir/CMakeLists.txt index c5524ee38754b1..ae7211226dd594 100644 --- a/paddle/ir/CMakeLists.txt +++ b/paddle/ir/CMakeLists.txt @@ -1,7 +1,85 @@ -if(NOT WITH_IR) - return() +set_property(GLOBAL PROPERTY IR_TARGETS "") +set_property(GLOBAL PROPERTY IR_MODULES "") +function(ir_library TARGET_NAME) + set(options STATIC static SHARED shared INTERFACE interface) + set(oneValueArgs "") + set(multiValueArgs SRCS DEPS) + cmake_parse_arguments(ir_library "${options}" "${oneValueArgs}" + "${multiValueArgs}" ${ARGN}) + set(OBJ_LIB ir_${TARGET_NAME}) + add_library(${OBJ_LIB} OBJECT ${ir_library_SRCS}) + + if(ir_library_SHARED OR ir_library_shared) # build *.so + cc_library( + ${TARGET_NAME} SHARED + SRCS $ + DEPS ${ir_library_DEPS}) + elseif(ir_library_INTERFACE OR ir_library_interface) + cc_library( + ${TARGET_NAME} INTERFACE + SRCS $ + DEPS ${ir_library_DEPS}) + else() + cc_library( + ${TARGET_NAME} + SRCS $ + DEPS ${ir_library_DEPS}) + set_property(GLOBAL APPEND PROPERTY IR_MODULES $) + + get_property(ir_targets GLOBAL PROPERTY IR_TARGETS) + set(ir_targets ${ir_targets} ${TARGET_NAME}) + set_property(GLOBAL PROPERTY IR_TARGETS "${ir_targets}") + + endif() +endfunction() + +if(WITH_SHARED_IR) + add_definitions(-DIR_DLL_EXPORT) endif() add_subdirectory(core) add_subdirectory(pass) add_subdirectory(pattern_rewrite) + +if(WIN32) + if(WITH_SHARED_IR) + set(IR_NAME + ir.dll + CACHE INTERNAL "" FORCE) + else() + set(IR_NAME + ir.lib + CACHE INTERNAL "" FORCE) + endif() +elseif(APPLE) + if(WITH_SHARED_IR) + set(IR_NAME + libir.dylib + CACHE INTERNAL "" FORCE) + else() + set(IR_NAME + libir.a + CACHE INTERNAL "" FORCE) + endif() +else() + if(WITH_SHARED_IR) + set(IR_NAME + libir.so + CACHE INTERNAL "" FORCE) + else() + set(IR_NAME + libir.a + CACHE INTERNAL "" FORCE) + endif() +endif() + +set(IR_LIB + "${CMAKE_CURRENT_BINARY_DIR}/${IR_NAME}" + CACHE FILEPATH "IR Library" FORCE) + +get_property(ir_modules GLOBAL PROPERTY IR_MODULES) +if(WITH_SHARED_IR) + add_library(ir SHARED ${ir_modules}) +else() + add_library(ir STATIC ${ir_modules}) +endif() diff --git a/paddle/ir/core/CMakeLists.txt b/paddle/ir/core/CMakeLists.txt index dde617e33a0502..c35bc02b344fad 100644 --- a/paddle/ir/core/CMakeLists.txt +++ b/paddle/ir/core/CMakeLists.txt @@ -3,4 +3,4 @@ set(NEWIR_BINARY_DIR "${PADDLE_BINARY_DIR}/paddle/ir") file(GLOB IR_SRCS "*.cc") -cc_library(new_ir SRCS ${IR_SRCS}) +ir_library(ir_core SRCS ${IR_SRCS}) diff --git a/paddle/ir/core/attribute.h b/paddle/ir/core/attribute.h index ea7b0f5daae811..0c0070a9698140 100644 --- a/paddle/ir/core/attribute.h +++ b/paddle/ir/core/attribute.h @@ -22,11 +22,11 @@ namespace ir { /// \brief Unified interface of the Attribute class. Derivation of all Attribute /// classes only derives interfaces, not members. /// -class Attribute { +class IR_API Attribute { public: using Storage = AttributeStorage; - constexpr Attribute() = default; + Attribute() = default; Attribute(const Storage *storage) // NOLINT : storage_(storage) {} @@ -85,7 +85,7 @@ class Attribute { const Storage *storage_{nullptr}; }; -std::ostream &operator<<(std::ostream &os, Attribute attr); +IR_API std::ostream &operator<<(std::ostream &os, Attribute attr); } // namespace ir namespace std { diff --git a/paddle/ir/core/attribute_base.h b/paddle/ir/core/attribute_base.h index dd120a02a450c9..daa3fed14f8a33 100644 --- a/paddle/ir/core/attribute_base.h +++ b/paddle/ir/core/attribute_base.h @@ -25,7 +25,7 @@ class Dialect; /// \brief Abstract the properties and behaviors common to all Attribute classes /// into an AbstractAttribute class. /// -class AbstractAttribute { +class IR_API AbstractAttribute { public: /// /// \brief Construct an AbstractAttribute by TypeId directly. @@ -98,7 +98,7 @@ struct AttributeManager; /// directly but parametric attribute should be constructed by Derived /// AttributeStorage. /// -class AttributeStorage : public StorageManager::StorageBase { +class IR_API AttributeStorage : public StorageManager::StorageBase { friend StorageManager; friend AttributeManager; @@ -141,7 +141,7 @@ class AttributeStorage : public StorageManager::StorageBase { /// \brief AttributeManager is a utility class that provides interfaces for get /// or unique Attribute instances in IrContext. /// -struct AttributeManager { +struct IR_API AttributeManager { /// /// \brief Get a unique instance of Attribute T from IrContext. Note: For a /// parametric attribute, if not found in IrContext, it will try to create a diff --git a/paddle/ir/core/block.h b/paddle/ir/core/block.h index 24b48036916c27..dd4663d0744ebe 100644 --- a/paddle/ir/core/block.h +++ b/paddle/ir/core/block.h @@ -17,12 +17,13 @@ #include #include +#include "paddle/ir/core/dll_decl.h" #include "paddle/ir/core/region.h" namespace ir { class Operation; -class Block { +class IR_API Block { using OpListType = std::list; public: diff --git a/paddle/ir/core/builder.h b/paddle/ir/core/builder.h index 0e7f7b427f6bdf..9a6c3d849ca473 100644 --- a/paddle/ir/core/builder.h +++ b/paddle/ir/core/builder.h @@ -73,13 +73,13 @@ class Builder { Block *block() const { return block_; } /// Creates an operation given the fields represented as an OperationState. - Operation *Build(OperationArgument &&argument); + IR_API Operation *Build(OperationArgument &&argument); /// Creates an operation with the given fields. - Operation *Build(const std::vector &inputs, - const AttributeMap &attribute, - const std::vector &output_types, - ir::OpInfo op_info); + IR_API Operation *Build(const std::vector &inputs, + const AttributeMap &attribute, + const std::vector &output_types, + ir::OpInfo op_info); /// Create an operation of specific op type at the current insertion point. template diff --git a/paddle/ir/core/builtin_attribute.cc b/paddle/ir/core/builtin_attribute.cc index d9f4798ccab266..06c0f347530cfb 100644 --- a/paddle/ir/core/builtin_attribute.cc +++ b/paddle/ir/core/builtin_attribute.cc @@ -36,3 +36,12 @@ std::vector ArrayAttribute::data() const { void* PointerAttribute::data() const { return storage()->GetAsKey(); } } // namespace ir + +IR_DEFINE_EXPLICIT_TYPE_ID(ir::StrAttribute) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::BoolAttribute) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::FloatAttribute) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::DoubleAttribute) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::Int32Attribute) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::Int64Attribute) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::ArrayAttribute) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::PointerAttribute) diff --git a/paddle/ir/core/builtin_attribute.h b/paddle/ir/core/builtin_attribute.h index ba013af6ff2835..472a5ae8c156e6 100644 --- a/paddle/ir/core/builtin_attribute.h +++ b/paddle/ir/core/builtin_attribute.h @@ -19,7 +19,7 @@ #include "paddle/ir/core/utils.h" namespace ir { -class StrAttribute : public Attribute { +class IR_API StrAttribute : public Attribute { public: using Attribute::Attribute; @@ -34,7 +34,7 @@ class StrAttribute : public Attribute { uint32_t size() const; }; -class BoolAttribute : public Attribute { +class IR_API BoolAttribute : public Attribute { public: using Attribute::Attribute; @@ -43,7 +43,7 @@ class BoolAttribute : public Attribute { bool data() const; }; -class FloatAttribute : public Attribute { +class IR_API FloatAttribute : public Attribute { public: using Attribute::Attribute; @@ -52,7 +52,7 @@ class FloatAttribute : public Attribute { float data() const; }; -class DoubleAttribute : public Attribute { +class IR_API DoubleAttribute : public Attribute { public: using Attribute::Attribute; @@ -61,7 +61,7 @@ class DoubleAttribute : public Attribute { double data() const; }; -class Int32Attribute : public Attribute { +class IR_API Int32Attribute : public Attribute { public: using Attribute::Attribute; @@ -70,7 +70,7 @@ class Int32Attribute : public Attribute { int32_t data() const; }; -class Int64Attribute : public Attribute { +class IR_API Int64Attribute : public Attribute { public: using Attribute::Attribute; @@ -79,7 +79,7 @@ class Int64Attribute : public Attribute { int64_t data() const; }; -class ArrayAttribute : public Attribute { +class IR_API ArrayAttribute : public Attribute { public: using Attribute::Attribute; @@ -94,7 +94,7 @@ class ArrayAttribute : public Attribute { Attribute operator[](size_t index) const { return data()[index]; } }; -class PointerAttribute : public Attribute { +class IR_API PointerAttribute : public Attribute { public: using Attribute::Attribute; @@ -104,3 +104,12 @@ class PointerAttribute : public Attribute { }; } // namespace ir + +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::StrAttribute) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::BoolAttribute) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::FloatAttribute) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::DoubleAttribute) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::Int32Attribute) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::Int64Attribute) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::ArrayAttribute) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::PointerAttribute) diff --git a/paddle/ir/core/builtin_dialect.cc b/paddle/ir/core/builtin_dialect.cc index 0be2bc1a2e3043..2766be29f91ca0 100644 --- a/paddle/ir/core/builtin_dialect.cc +++ b/paddle/ir/core/builtin_dialect.cc @@ -54,3 +54,5 @@ void BuiltinDialect::initialize() { } } // namespace ir + +IR_DEFINE_EXPLICIT_TYPE_ID(ir::BuiltinDialect) diff --git a/paddle/ir/core/builtin_dialect.h b/paddle/ir/core/builtin_dialect.h index ff7da2d7f5da5e..c5872f8142e7b5 100644 --- a/paddle/ir/core/builtin_dialect.h +++ b/paddle/ir/core/builtin_dialect.h @@ -22,7 +22,7 @@ namespace ir { /// all built-in types defined in builtin_type.h will be registered in this /// Dialect. /// -class BuiltinDialect : public ir::Dialect { +class IR_API BuiltinDialect : public ir::Dialect { public: explicit BuiltinDialect(ir::IrContext *context); /// @@ -38,3 +38,5 @@ class BuiltinDialect : public ir::Dialect { }; } // namespace ir + +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::BuiltinDialect) diff --git a/paddle/ir/core/builtin_op.cc b/paddle/ir/core/builtin_op.cc index 8d56fff1429a9d..df6f22b0b4406a 100644 --- a/paddle/ir/core/builtin_op.cc +++ b/paddle/ir/core/builtin_op.cc @@ -204,3 +204,11 @@ void ConstantOp::Verify(const std::vector &inputs, Attribute ConstantOp::value() { return operation()->attributes().at("value"); } } // namespace ir + +IR_DEFINE_EXPLICIT_TYPE_ID(ir::ModuleOp) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::GetParameterOp) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::SetParameterOp) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::CombineOp) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::SliceOp) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::ConstantLikeTrait) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::ConstantOp) diff --git a/paddle/ir/core/builtin_op.h b/paddle/ir/core/builtin_op.h index 8ce62b1e886c0a..13996397b37fcc 100644 --- a/paddle/ir/core/builtin_op.h +++ b/paddle/ir/core/builtin_op.h @@ -24,7 +24,7 @@ class Block; /// /// \brief ModuleOp /// -class ModuleOp : public ir::Op { +class IR_API ModuleOp : public ir::Op { public: using Op::Op; static const char *name() { return "builtin.module"; } @@ -48,7 +48,7 @@ class ModuleOp : public ir::Op { /// \brief GetParameterOp: OpResult = GetParameterOp({StrAttribute, /// StrAttribute}) /// -class GetParameterOp : public ir::Op { +class IR_API GetParameterOp : public ir::Op { public: using Op::Op; static const char *name() { return "builtin.get_parameter"; } @@ -63,7 +63,7 @@ class GetParameterOp : public ir::Op { /// \brief SetParameterOp: SetParameterOp(OpOperand, {StrAttribute, /// StrAttribute}) /// -class SetParameterOp : public ir::Op { +class IR_API SetParameterOp : public ir::Op { public: using Op::Op; static const char *name() { return "builtin.set_parameter"; } @@ -77,7 +77,7 @@ class SetParameterOp : public ir::Op { /// /// \brief CombineOp: CombineOp(OpOperand) /// -class CombineOp : public ir::Op { +class IR_API CombineOp : public ir::Op { public: using Op::Op; @@ -95,7 +95,7 @@ class CombineOp : public ir::Op { /// /// \brief SliceOp: SliceOp(OpOperand) /// -class SliceOp : public ir::Op { +class IR_API SliceOp : public ir::Op { public: using Op::Op; @@ -109,7 +109,7 @@ class SliceOp : public ir::Op { const ir::AttributeMap &attributes); }; -class ConstantLikeTrait : public OpTraitBase { +class IR_API ConstantLikeTrait : public OpTraitBase { public: explicit ConstantLikeTrait(Operation *op) : OpTraitBase(op) {} @@ -118,7 +118,7 @@ class ConstantLikeTrait : public OpTraitBase { /// /// \brief ConstantOp /// -class ConstantOp : public Op { +class IR_API ConstantOp : public Op { public: using Op::Op; static const char *name() { return "builtin.constant"; } @@ -139,3 +139,11 @@ class ConstantOp : public Op { }; } // namespace ir + +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::ModuleOp) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::GetParameterOp) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::SetParameterOp) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::CombineOp) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::SliceOp) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::ConstantLikeTrait) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::ConstantOp) diff --git a/paddle/ir/core/builtin_type.cc b/paddle/ir/core/builtin_type.cc index d9ee429a5f6d35..847ea0c97634c0 100644 --- a/paddle/ir/core/builtin_type.cc +++ b/paddle/ir/core/builtin_type.cc @@ -18,3 +18,14 @@ namespace ir { std::vector VectorType::data() const { return storage()->GetAsKey(); } } // namespace ir + +IR_DEFINE_EXPLICIT_TYPE_ID(ir::Int8Type) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::VectorType) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::BFloat16Type) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::Float16Type) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::Float32Type) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::Float64Type) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::Int16Type) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::Int32Type) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::Int64Type) +IR_DEFINE_EXPLICIT_TYPE_ID(ir::BoolType) diff --git a/paddle/ir/core/builtin_type.h b/paddle/ir/core/builtin_type.h index 1e8208708305f3..ed09254f5100e0 100644 --- a/paddle/ir/core/builtin_type.h +++ b/paddle/ir/core/builtin_type.h @@ -38,14 +38,14 @@ namespace ir { // NOTE(dev): Currently Int8 are not considered as a cached member // in IrContextImpl because it is not widely used. -class Int8Type : public Type { +class IR_API Int8Type : public Type { public: using Type::Type; DECLARE_TYPE_UTILITY_FUNCTOR(Int8Type, TypeStorage); }; -class VectorType : public Type { +class IR_API VectorType : public Type { public: using Type::Type; @@ -60,25 +60,25 @@ class VectorType : public Type { Type operator[](size_t index) const { return data()[index]; } }; -#define DECLARE_BUILTIN_TYPE(__name) \ - class __name##Type : public Type { \ - public: \ - using Type::Type; \ - \ - DECLARE_TYPE_UTILITY_FUNCTOR(__name##Type, TypeStorage); \ - \ - static __name##Type get(IrContext *context); \ +#define DECLARE_BUILTIN_TYPE(__name) \ + class IR_API __name : public Type { \ + public: \ + using Type::Type; \ + \ + DECLARE_TYPE_UTILITY_FUNCTOR(__name, TypeStorage); \ + \ + static __name get(IrContext *context); \ }; #define FOREACH_BUILTIN_TYPE(__macro) \ - __macro(BFloat16); \ - __macro(Float16); \ - __macro(Float32); \ - __macro(Float64); \ - __macro(Int16); \ - __macro(Int32); \ - __macro(Int64); \ - __macro(Bool); + __macro(BFloat16Type); \ + __macro(Float16Type); \ + __macro(Float32Type); \ + __macro(Float64Type); \ + __macro(Int16Type); \ + __macro(Int32Type); \ + __macro(Int64Type); \ + __macro(BoolType); FOREACH_BUILTIN_TYPE(DECLARE_BUILTIN_TYPE) @@ -86,3 +86,14 @@ FOREACH_BUILTIN_TYPE(DECLARE_BUILTIN_TYPE) #undef DECLARE_BUILTIN_TYPE } // namespace ir + +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::Int8Type) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::VectorType) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::BFloat16Type) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::Float16Type) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::Float32Type) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::Float64Type) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::Int16Type) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::Int32Type) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::Int64Type) +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::BoolType) diff --git a/paddle/ir/core/dialect.h b/paddle/ir/core/dialect.h index 0edce0e5ab585b..c5f9f86fc76e9f 100644 --- a/paddle/ir/core/dialect.h +++ b/paddle/ir/core/dialect.h @@ -38,7 +38,7 @@ class DialectInterface; /// compilers only need to combine existing dialects and add their own /// extensions or customizations. /// -class Dialect { +class IR_API Dialect { public: Dialect(std::string name, IrContext *context, TypeId id); diff --git a/paddle/ir/core/dialect_interface.h b/paddle/ir/core/dialect_interface.h index 0363ca0ebac178..e24b3481f4ef41 100644 --- a/paddle/ir/core/dialect_interface.h +++ b/paddle/ir/core/dialect_interface.h @@ -34,7 +34,7 @@ class DialectInterfaceBase : public BaseT { explicit DialectInterfaceBase(Dialect *dialect) : BaseT(dialect, id()) {} }; -class DialectInterface { +class IR_API DialectInterface { public: virtual ~DialectInterface(); diff --git a/paddle/ir/core/dll_decl.h b/paddle/ir/core/dll_decl.h new file mode 100644 index 00000000000000..d0be0eff305bed --- /dev/null +++ b/paddle/ir/core/dll_decl.h @@ -0,0 +1,27 @@ +// 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 + +#if defined(_WIN32) +#ifndef IR_API +#ifdef IR_DLL_EXPORT +#define IR_API __declspec(dllexport) +#else +#define IR_API __declspec(dllimport) +#endif // IR_DLL_EXPORT +#endif // IR_API +#else +#define IR_API +#endif // _WIN32 diff --git a/paddle/ir/core/ir_context.cc b/paddle/ir/core/ir_context.cc index 36621bf099e2ec..583eb0a19e1b86 100644 --- a/paddle/ir/core/ir_context.cc +++ b/paddle/ir/core/ir_context.cc @@ -182,6 +182,8 @@ IrContext *IrContext::Instance() { return &context; } +IrContext::~IrContext() { delete impl_; } + IrContext::IrContext() : impl_(new IrContextImpl()) { VLOG(4) << "BuiltinDialect registered into IrContext. ===>"; GetOrRegisterDialect(); diff --git a/paddle/ir/core/ir_context.h b/paddle/ir/core/ir_context.h index 9628a46aed8696..1ff5bb6e525046 100644 --- a/paddle/ir/core/ir_context.h +++ b/paddle/ir/core/ir_context.h @@ -18,6 +18,8 @@ #include #include +#include "paddle/ir/core/dll_decl.h" + namespace ir { class IrContextImpl; class StorageManager; @@ -37,7 +39,7 @@ using OpInfoMap = std::unordered_map; /// \brief IrContext is a global parameterless class used to store and manage /// Type, Attribute and other related data structures. /// -class IrContext { +class IR_API IrContext { public: /// /// \brief Initializes a new instance of IrContext. @@ -186,7 +188,8 @@ class IrContext { private: IrContext(); - const std::unique_ptr impl_; + ~IrContext(); + IrContextImpl *impl_; }; } // namespace ir diff --git a/paddle/ir/core/ir_printer.h b/paddle/ir/core/ir_printer.h index ed5565bcd81214..1692195cb8b69c 100644 --- a/paddle/ir/core/ir_printer.h +++ b/paddle/ir/core/ir_printer.h @@ -40,7 +40,7 @@ class BasicIrPrinter { std::ostream& os; }; -class IrPrinter : public BasicIrPrinter { +class IR_API IrPrinter : public BasicIrPrinter { public: explicit IrPrinter(std::ostream& os) : BasicIrPrinter(os) {} diff --git a/paddle/ir/core/op_base.h b/paddle/ir/core/op_base.h index 6df6e3a195655a..43644774688bb9 100644 --- a/paddle/ir/core/op_base.h +++ b/paddle/ir/core/op_base.h @@ -20,7 +20,7 @@ namespace ir { -class InterfaceValue { +class IR_API InterfaceValue { public: template static InterfaceValue get() { @@ -64,7 +64,7 @@ class InterfaceValue { void *model_{nullptr}; }; -class OpBase { +class IR_API OpBase { public: explicit OpBase(Operation *operation = nullptr) : operation_(operation) {} diff --git a/paddle/ir/core/op_info.h b/paddle/ir/core/op_info.h index 5b77aa8de3ec6d..485e116cf5ae8c 100644 --- a/paddle/ir/core/op_info.h +++ b/paddle/ir/core/op_info.h @@ -26,9 +26,9 @@ class Type; class Attribute; class Dialect; -class OpInfo { +class IR_API OpInfo { public: - constexpr OpInfo() = default; + OpInfo() = default; OpInfo(const OpInfo &other) = default; diff --git a/paddle/ir/core/operation.h b/paddle/ir/core/operation.h index 37b690618ec90f..30f25d83c4a7cd 100644 --- a/paddle/ir/core/operation.h +++ b/paddle/ir/core/operation.h @@ -26,7 +26,7 @@ class Program; class OpOperand; class OpResult; -class alignas(8) Operation final { +class IR_API alignas(8) Operation final { public: /// /// \brief Malloc memory and construct objects in the following order: diff --git a/paddle/ir/core/parameter.h b/paddle/ir/core/parameter.h index 74524537d2bf44..3dbe48935b09a5 100644 --- a/paddle/ir/core/parameter.h +++ b/paddle/ir/core/parameter.h @@ -20,7 +20,7 @@ namespace ir { /// /// \brief Parameter represents the weight in the calculation graph. /// -class Parameter { +class IR_API Parameter { public: Parameter(void* data, size_t size, ir::Type type) { data_ = malloc(size); diff --git a/paddle/ir/core/program.h b/paddle/ir/core/program.h index 74eb649447cac1..16ff15635f7df8 100644 --- a/paddle/ir/core/program.h +++ b/paddle/ir/core/program.h @@ -36,7 +36,7 @@ class IrContext; /// concepts such as basic blocks, closures, and functions will be introduced to /// continuously improve Program's ability to represent computational graphs. /// -class Program { +class IR_API Program { public: using ParameterMap = std::unordered_map>; diff --git a/paddle/ir/core/region.h b/paddle/ir/core/region.h index 5c08c675798223..b84ea97bfd9ed9 100644 --- a/paddle/ir/core/region.h +++ b/paddle/ir/core/region.h @@ -17,12 +17,14 @@ #include #include +#include "paddle/ir/core/dll_decl.h" + namespace ir { class Block; class Operation; -class Region { +class IR_API Region { public: using iterator = std::list::iterator; using reverse_iterator = std::list::reverse_iterator; diff --git a/paddle/ir/core/storage_manager.h b/paddle/ir/core/storage_manager.h index 9ff7f76e98def5..f2cda194ce2157 100644 --- a/paddle/ir/core/storage_manager.h +++ b/paddle/ir/core/storage_manager.h @@ -40,7 +40,7 @@ struct ParametricStorageManager; /// provide method 'bool operator==(const ParamKey &) const', used to compare /// Storage instance and ParamKey instance. /// -class StorageManager { +class IR_API StorageManager { public: /// /// \brief This class is the base class of all storage classes, diff --git a/paddle/ir/core/type.h b/paddle/ir/core/type.h index 45a34b0ccf864c..7647bf3641a8db 100644 --- a/paddle/ir/core/type.h +++ b/paddle/ir/core/type.h @@ -26,11 +26,11 @@ namespace ir { /// Float32Type, etc. are all derived classes of Type, but no new member /// variables will be added. /// -class Type { +class IR_API Type { public: using Storage = TypeStorage; - constexpr Type() = default; + Type() = default; Type(const Storage *storage) // NOLINT : storage_(const_cast(storage)) {} @@ -89,7 +89,7 @@ class Type { const Storage *storage_{nullptr}; }; -std::ostream &operator<<(std::ostream &os, Type type); +IR_API std::ostream &operator<<(std::ostream &os, Type type); } // namespace ir diff --git a/paddle/ir/core/type_base.h b/paddle/ir/core/type_base.h index b15b1b2656fe76..5ff4618dab7737 100644 --- a/paddle/ir/core/type_base.h +++ b/paddle/ir/core/type_base.h @@ -28,7 +28,7 @@ class Dialect; /// of all types is TypeId (and possibly others). Therefore, construct a class /// with TypeId as its member. /// -class AbstractType { +class IR_API AbstractType { public: /// /// \brief Construct an AbstractType by TypeId directly. @@ -100,7 +100,7 @@ struct TypeManager; /// be included. So that, non-parametric type can be constructed by TypeStorage /// directly but parametric type should be constructed by Derived TypeStorage. /// -class TypeStorage : public StorageManager::StorageBase { +class IR_API TypeStorage : public StorageManager::StorageBase { friend StorageManager; friend TypeManager; @@ -141,7 +141,7 @@ class TypeStorage : public StorageManager::StorageBase { /// \brief TypeManager is a utility class that provides interfaces for get or /// unique Type instances in IrContext. /// -struct TypeManager { +struct IR_API TypeManager { /// /// \brief Get a unique instance of Type T from IrContext. Note: For a /// parametric_type, if not found in IrContext, it will try to create a new diff --git a/paddle/ir/core/type_id.h b/paddle/ir/core/type_id.h index 7d22abb7388a26..aec6ecf343433a 100644 --- a/paddle/ir/core/type_id.h +++ b/paddle/ir/core/type_id.h @@ -17,6 +17,8 @@ #include #include +#include "paddle/ir/core/dll_decl.h" + namespace ir { /// @@ -40,10 +42,7 @@ class TypeId { /// \return The unique TypeId of Type T. /// template - static TypeId get() { - static Storage instance; - return TypeId(&instance); - } + static TypeId get(); TypeId() = default; @@ -85,6 +84,60 @@ class TypeId { Storage *storage_{nullptr}; }; +namespace detail { +class alignas(8) UniqueingId { + public: + UniqueingId() = default; + UniqueingId(const UniqueingId &) = delete; + UniqueingId &operator=(const UniqueingId &) = delete; + UniqueingId(UniqueingId &&) = delete; + UniqueingId &operator=(UniqueingId &&) = delete; + + operator TypeId() { return id(); } + TypeId id() { return TypeId::RecoverFromOpaquePointer(this); } +}; + +template +class TypeIdResolver; + +} // namespace detail + +template +TypeId TypeId::get() { + return detail::TypeIdResolver::Resolve(); +} + +#define IR_DECLARE_EXPLICIT_TYPE_ID(TYPE_CLASS) \ + namespace ir { \ + namespace detail { \ + template <> \ + class TypeIdResolver { \ + public: \ + static TypeId Resolve() { return id_; } \ + static UniqueingId id_; \ + }; \ + } \ + } // namespace ir + +#define IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(TYPE_CLASS) \ + namespace ir { \ + namespace detail { \ + template <> \ + class IR_API TypeIdResolver { \ + public: \ + static TypeId Resolve() { return id_; } \ + static UniqueingId id_; \ + }; \ + } \ + } // namespace ir + +#define IR_DEFINE_EXPLICIT_TYPE_ID(TYPE_CLASS) \ + namespace ir { \ + namespace detail { \ + UniqueingId TypeIdResolver::id_ = {}; \ + } \ + } // namespace ir + } // namespace ir namespace std { diff --git a/paddle/ir/core/utils.h b/paddle/ir/core/utils.h index b619bc065fef57..e7ddd5f26eadfd 100644 --- a/paddle/ir/core/utils.h +++ b/paddle/ir/core/utils.h @@ -20,11 +20,13 @@ #include #include +#include "paddle/ir/core/dll_decl.h" + namespace ir { /// /// \brief Equivalent to boost::hash_combine. /// -std::size_t hash_combine(std::size_t lhs, std::size_t rhs); +IR_API std::size_t hash_combine(std::size_t lhs, std::size_t rhs); /// /// \brief Aligned malloc and free functions. diff --git a/paddle/ir/core/value.h b/paddle/ir/core/value.h index 0825dd06cbd9bf..ad17f8b4611230 100644 --- a/paddle/ir/core/value.h +++ b/paddle/ir/core/value.h @@ -31,7 +31,7 @@ class OpResultImpl; /// \brief OpOperand class represents the operand of operation. This class only /// provides interfaces, for specific implementation, see Impl class. /// -class OpOperand { +class IR_API OpOperand { public: OpOperand() = default; @@ -103,7 +103,7 @@ class ValueUseIterator { /// \brief Value class represents the SSA value in the IR system. This class /// only provides interfaces, for specific implementation, see Impl class. /// -class Value { +class IR_API Value { public: Value() = default; @@ -161,7 +161,7 @@ class Value { /// This class only provides interfaces, for specific implementation, see Impl /// class. /// -class OpResult : public Value { +class IR_API OpResult : public Value { public: using Value::Value; diff --git a/paddle/ir/pass/CMakeLists.txt b/paddle/ir/pass/CMakeLists.txt index 042b3e8e1e9fc4..b4a1d99ab5fcd6 100644 --- a/paddle/ir/pass/CMakeLists.txt +++ b/paddle/ir/pass/CMakeLists.txt @@ -1,6 +1,3 @@ file(GLOB NEW_PASS_SRCS "*.cc") -cc_library( - new_pass - SRCS ${NEW_PASS_SRCS} - DEPS new_ir) +ir_library(ir_pass SRCS ${NEW_PASS_SRCS} DEPS ir_core) diff --git a/paddle/ir/pass/analysis_manager.h b/paddle/ir/pass/analysis_manager.h index 1638d885860589..b43c12b8b349e9 100644 --- a/paddle/ir/pass/analysis_manager.h +++ b/paddle/ir/pass/analysis_manager.h @@ -74,6 +74,8 @@ class PreservedAnalyses { preserved_ids_.erase(TypeId::get()); } + friend ir::detail::TypeIdResolver; + private: template friend struct AnalysisModel; @@ -304,3 +306,5 @@ class AnalysisManagerHolder { }; } // namespace ir + +IR_DECLARE_EXPLICIT_TYPE_ID(ir::detail::PreservedAnalyses::AllAnalysesType) diff --git a/paddle/ir/pass/pass.cc b/paddle/ir/pass/pass.cc index 4b4db8e1d28d4e..a3ccd178db1e16 100644 --- a/paddle/ir/pass/pass.cc +++ b/paddle/ir/pass/pass.cc @@ -227,3 +227,5 @@ void PassInstrumentor::AddInstrumentation( } } // namespace ir + +IR_DEFINE_EXPLICIT_TYPE_ID(ir::detail::PreservedAnalyses::AllAnalysesType) diff --git a/paddle/ir/pass/pass.h b/paddle/ir/pass/pass.h index 31b865e14f68e6..e45407b8465172 100644 --- a/paddle/ir/pass/pass.h +++ b/paddle/ir/pass/pass.h @@ -18,8 +18,8 @@ #include #include +#include "paddle/ir/core/enforce.h" #include "paddle/ir/pass/analysis_manager.h" -#include "paddle/phi/core/enforce.h" #include "paddle/utils/optional.h" namespace ir { @@ -68,7 +68,7 @@ struct PassInfo { } // namespace detail /// We can access pass only from PassManager. -class Pass { +class IR_API Pass { public: explicit Pass(const std::string& name, uint8_t opt_level, @@ -91,9 +91,8 @@ class Pass { AnalysisManager analysis_manager() { return pass_state().am; } detail::PassExecutionState& pass_state() { - PADDLE_ENFORCE_EQ(pass_state_.is_initialized(), - true, - phi::errors::Fatal("pass state was never initialized")); + IR_ENFORCE(pass_state_.is_initialized() == true, + "pass state was never initialized"); return *pass_state_; } diff --git a/paddle/ir/pass/pass_instrumentation.h b/paddle/ir/pass/pass_instrumentation.h index 2105d4b261b042..1c80682fc43c72 100644 --- a/paddle/ir/pass/pass_instrumentation.h +++ b/paddle/ir/pass/pass_instrumentation.h @@ -57,7 +57,7 @@ class PassInstrumentation { /// This class holds a collection of PassInstrumentation obejcts, and invokes /// their respective callbacks. -class PassInstrumentor { +class IR_API PassInstrumentor { public: PassInstrumentor(); ~PassInstrumentor(); diff --git a/paddle/ir/pass/pass_manager.h b/paddle/ir/pass/pass_manager.h index 83e71d60c5d90c..ec4191ebccb547 100644 --- a/paddle/ir/pass/pass_manager.h +++ b/paddle/ir/pass/pass_manager.h @@ -34,7 +34,7 @@ namespace detail { class PassAdaptor; } -class PassManager { +class IR_API PassManager { public: explicit PassManager(IrContext *context, uint8_t opt_level = 2); diff --git a/paddle/ir/pattern_rewrite/CMakeLists.txt b/paddle/ir/pattern_rewrite/CMakeLists.txt index 9d5dba05eebc58..e99611a4ca050e 100644 --- a/paddle/ir/pattern_rewrite/CMakeLists.txt +++ b/paddle/ir/pattern_rewrite/CMakeLists.txt @@ -1,6 +1,3 @@ file(GLOB PATTERN_SRCS "*.cc") -cc_library( - pattern_rewrite - SRCS ${PATTERN_SRCS} - DEPS new_ir) +ir_library(ir_pattern_rewrite SRCS ${PATTERN_SRCS} DEPS ir_core) diff --git a/paddle/ir/pattern_rewrite/pattern_match.h b/paddle/ir/pattern_rewrite/pattern_match.h index f2f073ffc047dc..97f3bf09c69460 100644 --- a/paddle/ir/pattern_rewrite/pattern_match.h +++ b/paddle/ir/pattern_rewrite/pattern_match.h @@ -57,7 +57,7 @@ class PatternBenefit { // This class contains all of the data related to a Pattern, but not contains // any methods for the matching. This class is used to interface with the // metadata of a pattern, such as benefit or root operation. -class Pattern { +class IR_API Pattern { enum class RootKind { // The pattern root matches "any" operation. Any, @@ -155,7 +155,7 @@ class Pattern { class PatternRewriter; -class RewritePattern : public Pattern { +class IR_API RewritePattern : public Pattern { public: virtual ~RewritePattern(); diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 25a821dc1a705e..17924cf0f8fccb 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -406,18 +406,6 @@ data_type : dtype backend : place -- op : full_int_array - args : (IntArray value, DataType dtype=DataType::FLOAT32, Place place=CPUPlace()) - output: Tensor(out) - infer_meta : - func : CreateIntArrayInferMeta - param : [value, dtype] - kernel : - func : full_int_array - param : [value, dtype] - data_type : dtype - backend : place - - op : full_like args : (Tensor x, Scalar value, DataType dtype = DataType::UNDEFINED, Place place = {}) output: Tensor(out) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index be791837304644..4b3524454c1392 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -919,6 +919,18 @@ func : frame backward : frame_grad +- op : full_int_array + args : (IntArray value, DataType dtype=DataType::FLOAT32, Place place=CPUPlace()) + output: Tensor(out) + infer_meta : + func : CreateIntArrayInferMeta + param : [value, dtype] + kernel : + func : full_int_array + param : [value, dtype] + data_type : dtype + backend : place + - op : gather_nd args : (Tensor x, Tensor index) output : Tensor diff --git a/paddle/scripts/paddle_build.bat b/paddle/scripts/paddle_build.bat index 1e87795d05216a..2b9471566fc1fd 100644 --- a/paddle/scripts/paddle_build.bat +++ b/paddle/scripts/paddle_build.bat @@ -695,7 +695,7 @@ set PATH=%THIRD_PARTY_PATH:/=\%\install\openblas\lib;%THIRD_PARTY_PATH:/=\%\inst %THIRD_PARTY_PATH:/=\%\install\zlib\bin;%THIRD_PARTY_PATH:/=\%\install\mklml\lib;^ %THIRD_PARTY_PATH:/=\%\install\mkldnn\bin;%THIRD_PARTY_PATH:/=\%\install\warpctc\bin;^ %THIRD_PARTY_PATH:/=\%\install\onnxruntime\lib;%THIRD_PARTY_PATH:/=\%\install\paddle2onnx\lib;^ -%work_dir%\%BUILD_DIR%\paddle\fluid\inference;%work_dir%\%BUILD_DIR%\paddle\fluid\inference\capi_exp;^ +%work_dir%\%BUILD_DIR%\paddle\fluid\inference;%work_dir%\%BUILD_DIR%\paddle\fluid\inference\capi_exp;%work_dir%\%BUILD_DIR%\paddle\ir;^ %PATH% REM TODO: make ut find .dll in install\onnxruntime\lib diff --git a/python/env_dict.py.in b/python/env_dict.py.in index ef199979eefba0..2280a730ece1c3 100644 --- a/python/env_dict.py.in +++ b/python/env_dict.py.in @@ -13,6 +13,9 @@ env_dict={ 'PHI_LIB':'@PHI_LIB@', 'PHI_NAME':'@PHI_NAME@', 'WITH_SHARED_PHI':'@WITH_SHARED_PHI@', + 'IR_LIB':'@IR_LIB@', + 'IR_NAME':'@IR_NAME@', + 'WITH_SHARED_IR':'@WITH_SHARED_IR@', 'WARPCTC_LIBRARIES':'@WARPCTC_LIBRARIES@', 'WARPRNNT_LIBRARIES':'@WARPRNNT_LIBRARIES@', 'FLASHATTN_LIBRARIES':'@FLASHATTN_LIBRARIES@', diff --git a/python/setup.py.in b/python/setup.py.in index 402fdd3f879055..1fe7264c715605 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -564,9 +564,13 @@ libs_path='${PADDLE_BINARY_DIR}/python/paddle/libs' package_data['paddle.libs']= [] if('${WITH_SHARED_PHI}' == 'ON'): - package_data['paddle.libs'] = [('libphi' if os.name != 'nt' else 'phi') + ext_name] + package_data['paddle.libs'] += [('libphi' if os.name != 'nt' else 'phi') + ext_name] shutil.copy('${PHI_LIB}', libs_path) +if('${WITH_SHARED_IR}' == 'ON'): + package_data['paddle.libs'] += [('libir' if os.name != 'nt' else 'ir') + ext_name] + shutil.copy('${IR_LIB}', libs_path) + package_data['paddle.libs']+=[ ('libwarpctc' if os.name != 'nt' else 'warpctc') + ext_name, ('libwarprnnt' if os.name != 'nt' else 'warprnnt') + ext_name, @@ -731,11 +735,17 @@ if '${CMAKE_BUILD_TYPE}' == 'Release': if('${WITH_SHARED_PHI}' == 'ON'): # change rpath of phi.ext for loading 3rd party libb commands.append("install_name_tool -add_rpath '@loader_path' ${PADDLE_BINARY_DIR}/python/paddle/libs/${PHI_NAME}") + if('${WITH_SHARED_IR}' == 'ON'): + # change rpath of ir.ext for loading 3rd party libb + commands.append("install_name_tool -add_rpath '@loader_path' ${PADDLE_BINARY_DIR}/python/paddle/libs/${IR_NAME}") else: commands = ["patchelf --set-rpath '$ORIGIN/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so'] if('${WITH_SHARED_PHI}' == 'ON'): # change rpath of phi.ext for loading 3rd party lib commands.append("patchelf --set-rpath '$ORIGIN' ${PADDLE_BINARY_DIR}/python/paddle/libs/${PHI_NAME}") + if('${WITH_SHARED_IR}' == 'ON'): + # change rpath of ir.ext for loading 3rd party lib + commands.append("patchelf --set-rpath '$ORIGIN' ${PADDLE_BINARY_DIR}/python/paddle/libs/${IR_NAME}") # The sw_64 not suppot patchelf, so we just disable that. if platform.machine() != 'sw_64' and platform.machine() != 'mips64': for command in commands: diff --git a/setup.py b/setup.py index ec37717b0e5f8d..de97ae234a2a37 100644 --- a/setup.py +++ b/setup.py @@ -966,11 +966,17 @@ def get_package_data_and_package_dir(): package_data['paddle.libs'] = [] if env_dict.get("WITH_SHARED_PHI") == "ON": - package_data['paddle.libs'] = [ + package_data['paddle.libs'] += [ ('libphi' if os.name != 'nt' else 'phi') + ext_suffix ] shutil.copy(env_dict.get("PHI_LIB"), libs_path) + if env_dict.get("WITH_SHARED_IR") == "ON": + package_data['paddle.libs'] += [ + ('libir' if os.name != 'nt' else 'ir') + ext_suffix + ] + shutil.copy(env_dict.get("IR_LIB"), libs_path) + package_data['paddle.libs'] += [ ('libwarpctc' if os.name != 'nt' else 'warpctc') + ext_suffix, ('libwarprnnt' if os.name != 'nt' else 'warprnnt') + ext_suffix, @@ -1216,6 +1222,13 @@ def get_package_data_and_package_dir(): + '/python/paddle/libs/' + env_dict.get("PHI_NAME") ) + if env_dict.get("WITH_SHARED_IR") == "ON": + commands.append( + "install_name_tool -add_rpath '@loader_path' " + + env_dict.get("PADDLE_BINARY_DIR") + + '/python/paddle/libs/' + + env_dict.get("IR_NAME") + ) else: commands = [ "patchelf --set-rpath '$ORIGIN/../libs/' " @@ -1231,6 +1244,13 @@ def get_package_data_and_package_dir(): + '/python/paddle/libs/' + env_dict.get("PHI_NAME") ) + if env_dict.get("WITH_SHARED_IR") == "ON": + commands.append( + "patchelf --set-rpath '$ORIGIN' " + + env_dict.get("PADDLE_BINARY_DIR") + + '/python/paddle/libs/' + + env_dict.get("IR_NAME") + ) # The sw_64 not suppot patchelf, so we just disable that. if platform.machine() != 'sw_64' and platform.machine() != 'mips64': for command in commands: diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index af6dc423561e7d..90c7c3898bccce 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -171,6 +171,9 @@ if(${len} GREATER_EQUAL 1) if(WITH_SHARED_PHI) target_link_libraries(${test_name} $) endif() + if(WITH_SHARED_IR) + target_link_libraries(${test_name} $) + endif() add_dependencies(${test_name} ${paddle_lib} paddle_gtest_main_new) if(WITH_GPU) target_link_libraries(${test_name} ${CUDA_CUDART_LIBRARY} @@ -182,7 +185,7 @@ if(${len} GREATER_EQUAL 1) if(APPLE) target_link_libraries( ${test_name} - "-Wl,-rpath,$ -Wl,-rpath,$" + "-Wl,-rpath,$ -Wl,-rpath,$ -Wl,-rpath,$" ) endif() if(NOT ((NOT WITH_PYTHON) AND ON_INFER)) diff --git a/test/cpp/fluid/cinn/cinn_launch_context_test.cc b/test/cpp/fluid/cinn/cinn_launch_context_test.cc index cb7d5965dc9999..97cc28f6293d00 100644 --- a/test/cpp/fluid/cinn/cinn_launch_context_test.cc +++ b/test/cpp/fluid/cinn/cinn_launch_context_test.cc @@ -27,6 +27,7 @@ limitations under the License. */ #include "cinn/hlir/framework/tensor.h" #include "cinn/runtime/cinn_runtime.h" #include "gtest/gtest.h" +#include "paddle/fluid/framework/new_executor/interpretercore.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/paddle2cinn/build_cinn_pass.h" #include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" @@ -34,6 +35,8 @@ limitations under the License. */ #include "paddle/fluid/framework/program_desc.h" #include "paddle/fluid/framework/scope.h" #include "paddle/fluid/operators/cinn/cinn_op_helper.h" +#include "paddle/ir/core/program.h" +#include "paddle/ir/core/value.h" #include "paddle/phi/core/ddim.h" USE_OP_ITSELF(cinn_instruction_run); diff --git a/test/cpp/ir/CMakeLists.txt b/test/cpp/ir/CMakeLists.txt index a94503c0e5a1ea..a458282794f038 100644 --- a/test/cpp/ir/CMakeLists.txt +++ b/test/cpp/ir/CMakeLists.txt @@ -1,7 +1,3 @@ -if(NOT WITH_IR) - return() -endif() - add_subdirectory(core) add_subdirectory(pass) add_subdirectory(pattern_rewrite) diff --git a/test/cpp/ir/core/CMakeLists.txt b/test/cpp/ir/core/CMakeLists.txt index b452bfbfe54e7f..cfc229534d7174 100644 --- a/test/cpp/ir/core/CMakeLists.txt +++ b/test/cpp/ir/core/CMakeLists.txt @@ -1,14 +1,14 @@ -cc_test_old(type_test SRCS type_test.cc DEPS new_ir gtest) -cc_test_old(ir_attribute_test SRCS ir_attribute_test.cc DEPS new_ir gtest) -cc_test_old(ir_value_test SRCS ir_value_test.cc DEPS new_ir gtest) -cc_test_old(ir_op_test SRCS ir_op_test.cc DEPS new_ir gtest) +cc_test_old(type_test SRCS type_test.cc DEPS ir gtest) +cc_test_old(ir_attribute_test SRCS ir_attribute_test.cc DEPS ir gtest) +cc_test_old(ir_value_test SRCS ir_value_test.cc DEPS ir gtest) +cc_test_old(ir_op_test SRCS ir_op_test.cc DEPS ir gtest) cc_test_old( ir_program_test SRCS ir_program_test.cc DEPS - new_ir pd_dialect + ir phi gtest) @@ -17,8 +17,8 @@ cc_test_old( SRCS ir_phi_kernel_op_test.cc DEPS - new_ir pd_dialect + ir phi gtest) @@ -27,8 +27,8 @@ cc_test_old( SRCS ir_infershape_test.cc DEPS - new_ir pd_dialect + ir phi gtest) @@ -37,9 +37,9 @@ cc_test_old( SRCS ir_exe_test.cc DEPS - new_ir pd_dialect phi_kernel_adaptor + ir phi gtest) @@ -49,7 +49,7 @@ cc_test_old( scalar_attribute_test.cc DEPS pd_dialect - new_ir + ir gtest) file( @@ -70,7 +70,7 @@ cc_test_old( DEPS program_translator gtest - new_ir - pd_dialect) + pd_dialect + ir) -cc_test_old(ir_op_info_test SRCS op_info_test.cc DEPS gtest new_ir) +cc_test_old(ir_op_info_test SRCS op_info_test.cc DEPS gtest ir) diff --git a/test/cpp/ir/core/ir_attribute_test.cc b/test/cpp/ir/core/ir_attribute_test.cc index 69a121fbecfd27..5c53e58a8b90e0 100644 --- a/test/cpp/ir/core/ir_attribute_test.cc +++ b/test/cpp/ir/core/ir_attribute_test.cc @@ -22,13 +22,19 @@ #include "paddle/ir/core/dialect.h" #include "paddle/ir/core/ir_context.h" +class AttributeA {}; +IR_DECLARE_EXPLICIT_TYPE_ID(AttributeA) +IR_DEFINE_EXPLICIT_TYPE_ID(AttributeA) + +struct FakeDialect : ir::Dialect { + explicit FakeDialect(ir::IrContext *context) + : ir::Dialect(name(), context, ir::TypeId::get()) {} + static const char *name() { return "fake"; } +}; +IR_DECLARE_EXPLICIT_TYPE_ID(FakeDialect) +IR_DEFINE_EXPLICIT_TYPE_ID(FakeDialect) + TEST(attribute_test, attribute_base) { - class AttributeA {}; - struct FakeDialect : ir::Dialect { - explicit FakeDialect(ir::IrContext *context) - : ir::Dialect(name(), context, ir::TypeId::get()) {} - static const char *name() { return "fake"; } - }; // Test 1: Test the function of IrContext to register Dialect. ir::IrContext *ctx = ir::IrContext::Instance(); ir::Dialect *fake_dialect = ctx->GetOrRegisterDialect(); diff --git a/test/cpp/ir/core/ir_infershape_test.cc b/test/cpp/ir/core/ir_infershape_test.cc index e23226b15c4f20..26ad377b06b718 100644 --- a/test/cpp/ir/core/ir_infershape_test.cc +++ b/test/cpp/ir/core/ir_infershape_test.cc @@ -53,6 +53,8 @@ class OperationTest fn(infer_meta); } }; +IR_DECLARE_EXPLICIT_TYPE_ID(OperationTest) +IR_DEFINE_EXPLICIT_TYPE_ID(OperationTest) const char *OperationTest::attributes_name[attributes_num] = {"op2_attr1", "op2_attr2"}; @@ -69,6 +71,8 @@ class TestDialect : public ir::Dialect { private: void initialize() { RegisterOps(); } }; +IR_DECLARE_EXPLICIT_TYPE_ID(TestDialect) +IR_DEFINE_EXPLICIT_TYPE_ID(TestDialect) TEST(infershape_test, infershape_test) { ir::IrContext *ctx = ir::IrContext::Instance(); diff --git a/test/cpp/ir/core/ir_op_test.cc b/test/cpp/ir/core/ir_op_test.cc index d9189df2be09f9..04758da4f6ecc3 100644 --- a/test/cpp/ir/core/ir_op_test.cc +++ b/test/cpp/ir/core/ir_op_test.cc @@ -34,6 +34,8 @@ class ReadOnlyTrait : public ir::OpTraitBase { explicit ReadOnlyTrait(ir::Operation *op) : ir::OpTraitBase(op) {} }; +IR_DECLARE_EXPLICIT_TYPE_ID(ReadOnlyTrait) +IR_DEFINE_EXPLICIT_TYPE_ID(ReadOnlyTrait) /// \brief Define built-in Interface, derived from OpInterfaceBase. Concepts and /// Models need to be defined within the class. Concept defines abstract @@ -66,6 +68,8 @@ class InferShapeInterface : public ir::OpInterfaceBase { private: Concept *impl_; }; +IR_DECLARE_EXPLICIT_TYPE_ID(InferShapeInterface) +IR_DEFINE_EXPLICIT_TYPE_ID(InferShapeInterface) ir::AttributeMap CreateAttributeMap(std::vector attribute_names, std::vector attributes) { @@ -118,6 +122,9 @@ class Operation1 : public ir::Op { const char *Operation1::attributes_name[attributes_num] = {"op1_attr1", "op1_attr2"}; +IR_DECLARE_EXPLICIT_TYPE_ID(Operation1) +IR_DEFINE_EXPLICIT_TYPE_ID(Operation1) + // Define op2. class Operation2 : public ir::Op { @@ -142,6 +149,8 @@ class Operation2 }; const char *Operation2::attributes_name[attributes_num] = {"op2_attr1", "op2_attr2"}; +IR_DECLARE_EXPLICIT_TYPE_ID(Operation2) +IR_DEFINE_EXPLICIT_TYPE_ID(Operation2) // Define a dialect, op1 and op2 will be registered by this dialect. class TestDialect : public ir::Dialect { @@ -164,6 +173,8 @@ class TestDialect : public ir::Dialect { private: void initialize() { RegisterOps(); } }; +IR_DECLARE_EXPLICIT_TYPE_ID(TestDialect) +IR_DEFINE_EXPLICIT_TYPE_ID(TestDialect) TEST(op_test, op_test) { // (1) Register Dialect, Operation1, Operation2 into IrContext. diff --git a/test/cpp/ir/core/ir_program_test.cc b/test/cpp/ir/core/ir_program_test.cc index 2b2e90f5e3ce17..57828c86c2122b 100644 --- a/test/cpp/ir/core/ir_program_test.cc +++ b/test/cpp/ir/core/ir_program_test.cc @@ -49,6 +49,8 @@ class AddOp : public ir::Op { } } }; +IR_DECLARE_EXPLICIT_TYPE_ID(AddOp) +IR_DEFINE_EXPLICIT_TYPE_ID(AddOp) TEST(program_test, program) { // (1) Init environment. diff --git a/test/cpp/ir/core/ir_value_test.cc b/test/cpp/ir/core/ir_value_test.cc index 31d89b45807cfc..d720efd450d923 100644 --- a/test/cpp/ir/core/ir_value_test.cc +++ b/test/cpp/ir/core/ir_value_test.cc @@ -92,7 +92,8 @@ TEST(value_test, value_test) { EXPECT_EQ(op3_first_input.next_use(), nullptr); // Test 3: Value iterator - ir::Value::use_iterator iter = op1->result(0).begin(); + using my_iterator = ir::Value::use_iterator; + my_iterator iter = op1->result(0).begin(); EXPECT_EQ(iter.owner(), op4); ++iter; EXPECT_EQ(iter.owner(), op3); diff --git a/test/cpp/ir/core/type_test.cc b/test/cpp/ir/core/type_test.cc index 9a75cf69134cda..0c343362954ebe 100644 --- a/test/cpp/ir/core/type_test.cc +++ b/test/cpp/ir/core/type_test.cc @@ -24,11 +24,15 @@ #include "paddle/ir/core/type_name.h" #include "paddle/ir/core/utils.h" -TEST(type_test, type_id) { - // Define two empty classes, just for testing. - class TypeA {}; - class TypeB {}; +class TypeA {}; +IR_DECLARE_EXPLICIT_TYPE_ID(TypeA) +IR_DEFINE_EXPLICIT_TYPE_ID(TypeA) + +class TypeB {}; +IR_DECLARE_EXPLICIT_TYPE_ID(TypeB) +IR_DEFINE_EXPLICIT_TYPE_ID(TypeB) +TEST(type_test, type_id) { // Test 1: Test construct TypeId by TypeId::get() and overloaded operator== // method. ir::TypeId a_id = ir::TypeId::get(); @@ -46,17 +50,16 @@ TEST(type_test, type_id) { } } -TEST(type_test, type_base) { - // Define two empty classes, just for testing. - class TypeA {}; - - // Define a FakeDialect without registering any types. - struct FakeDialect : ir::Dialect { - explicit FakeDialect(ir::IrContext *context) - : ir::Dialect(name(), context, ir::TypeId::get()) {} - static const char *name() { return "fake"; } - }; +// Define a FakeDialect without registering any types. +struct FakeDialect : ir::Dialect { + explicit FakeDialect(ir::IrContext *context) + : ir::Dialect(name(), context, ir::TypeId::get()) {} + static const char *name() { return "fake"; } +}; +IR_DECLARE_EXPLICIT_TYPE_ID(FakeDialect) +IR_DEFINE_EXPLICIT_TYPE_ID(FakeDialect) +TEST(type_test, type_base) { // Test 1: Test the function of IrContext to register Dialect. ir::IrContext *ctx = ir::IrContext::Instance(); ir::Dialect *fake_dialect = ctx->GetOrRegisterDialect(); @@ -181,6 +184,8 @@ class IntegerType : public ir::Type { using Type::Type; DECLARE_TYPE_UTILITY_FUNCTOR(IntegerType, IntegerTypeStorage); }; +IR_DECLARE_EXPLICIT_TYPE_ID(IntegerType) +IR_DEFINE_EXPLICIT_TYPE_ID(IntegerType) // Customize a Dialect IntegerDialect, registration type of IntegerType. struct IntegerDialect : ir::Dialect { @@ -190,6 +195,8 @@ struct IntegerDialect : ir::Dialect { } static const char *name() { return "integer"; } }; +IR_DECLARE_EXPLICIT_TYPE_ID(IntegerDialect) +IR_DEFINE_EXPLICIT_TYPE_ID(IntegerDialect) TEST(type_test, custom_type_dialect) { ir::IrContext *ctx = ir::IrContext::Instance(); diff --git a/test/cpp/ir/kernel_dialect/CMakeLists.txt b/test/cpp/ir/kernel_dialect/CMakeLists.txt index 2ef1c31a7957dc..fd5842cd8d22ef 100644 --- a/test/cpp/ir/kernel_dialect/CMakeLists.txt +++ b/test/cpp/ir/kernel_dialect/CMakeLists.txt @@ -4,8 +4,8 @@ cc_test_old( ir_kernel_dialect_pass_test.cc DEPS pd_op_to_kernel_pass - new_ir pd_dialect phi_kernel_adaptor + ir phi gtest) diff --git a/test/cpp/ir/pass/CMakeLists.txt b/test/cpp/ir/pass/CMakeLists.txt index 07234e6023abbd..061b1e8c9e2fb9 100644 --- a/test/cpp/ir/pass/CMakeLists.txt +++ b/test/cpp/ir/pass/CMakeLists.txt @@ -3,7 +3,7 @@ cc_test_old( SRCS pass_manager_test.cc DEPS - new_pass + ir pd_dialect phi gtest) diff --git a/test/cpp/ir/pass/pass_manager_test.cc b/test/cpp/ir/pass/pass_manager_test.cc index 3ee256fbcc86c4..fa12303d69d866 100644 --- a/test/cpp/ir/pass/pass_manager_test.cc +++ b/test/cpp/ir/pass/pass_manager_test.cc @@ -46,6 +46,8 @@ class AddOp : public ir::Op { } } }; +IR_DECLARE_EXPLICIT_TYPE_ID(AddOp) +IR_DEFINE_EXPLICIT_TYPE_ID(AddOp) class TestPass : public ir::Pass { public: diff --git a/test/cpp/ir/pattern_rewrite/CMakeLists.txt b/test/cpp/ir/pattern_rewrite/CMakeLists.txt index 4332a6989828e6..c5332866c6aef2 100644 --- a/test/cpp/ir/pattern_rewrite/CMakeLists.txt +++ b/test/cpp/ir/pattern_rewrite/CMakeLists.txt @@ -1,8 +1 @@ -cc_test_old( - pattern_rewrite_test - SRCS - pattern_rewrite_test.cc - DEPS - new_pass - pattern_rewrite - gtest) +cc_test_old(pattern_rewrite_test SRCS pattern_rewrite_test.cc DEPS ir gtest) diff --git a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc index a4eb263d35c1d5..ea036ae9c31f12 100644 --- a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc +++ b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc @@ -58,6 +58,8 @@ class Operation1 : public ir::Op { }; const char *Operation1::attributes_name[attributes_num] = {"op2_attr1", "op2_attr2"}; +IR_DECLARE_EXPLICIT_TYPE_ID(Operation1) +IR_DEFINE_EXPLICIT_TYPE_ID(Operation1) // Define a dialect, op1 and op2 will be registered by this dialect. class TestDialect : public ir::Dialect { @@ -71,6 +73,8 @@ class TestDialect : public ir::Dialect { private: void initialize() { RegisterOps(); } }; +IR_DECLARE_EXPLICIT_TYPE_ID(TestDialect) +IR_DEFINE_EXPLICIT_TYPE_ID(TestDialect) // TODO(wilber): Add logical when ir support erase, replace or update. class TestPatternRewrite : public ir::OpRewritePattern { diff --git a/test/cpp/new_executor/CMakeLists.txt b/test/cpp/new_executor/CMakeLists.txt index 14e3672fd1ce7d..fa7c462b9927f6 100644 --- a/test/cpp/new_executor/CMakeLists.txt +++ b/test/cpp/new_executor/CMakeLists.txt @@ -2,7 +2,7 @@ if(NOT WIN32) cc_test( standalone_executor_new_ir_test SRCS standalone_executor_new_ir_test.cc - DEPS phi_kernel_adaptor new_ir pd_dialect) + DEPS phi_kernel_adaptor pd_dialect ir) endif() # skip win32 since wget is not installed by default on windows machine.