Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IR] Type system stage2: add class Type, type uniquer utils, class IRContext #50412

Merged
merged 27 commits into from
Feb 27, 2023

Conversation

zhangbo9674
Copy link
Contributor

@zhangbo9674 zhangbo9674 commented Feb 10, 2023

PR types

New features

PR changes

APIs

Describe

Paddle 类型系统开发主要分为4个阶段:(1)Type基础数据结构开发(PR1)、(2)Type类型生成工具开发(本PR)、(3)IRContext(本PR)、Dialect数据结构开发、(4)内置类型开发。

1. PR 主要内容:

  • type.h:定义 class Type,作为用户使用的唯一接口,是所有内置类型/自定义类型的基类;
  • ir_context.h:定义 class IrContext,全局单例类,用于管理/缓存已经注册的所有 Type,通过哈希表对AbstractType和TypeStorage进行管理:
    • AbstractType:unordered_map<TypeId, AbstractType *> registed_abstract_types_;
    • TypeStorage:StorageManager registed_storage_manager_;
  • storage_manager.h:定义 class StorageManager,由 IrContext 持有,通过哈希表对TypeStorage进行管理:
    • 无参类型缓存:unordered_map<TypeId, StorageBase *> parameterless_instances_;
    • 有参类型缓存:unordered_map<TypeId, unique_ptr> parametric_instance_;
  • type_base.h
    • 定义 TypeManager 工具类,提供 get/RegisterType 方法,用于向 IrContext 中获取、注册类型;
    • 定义 2个工具宏:DECLARE_TYPE_UTILITY_FUNCTOR、REGISTER_TYPE_2_IRCONTEXT,用于 Type 的定义及注册;
  • builtin_type.h:构建内置类型 Float32Type、Int32Type,并注册进 IrContext。

图片

2. Todo:

  • 按需丰富 Type 接口:如 is_a、dyn_cast 等
  • Dialect 支持(第3阶段)
  • 更多内置类型的定义、注册(第4阶段)

@paddle-bot
Copy link

paddle-bot bot commented Feb 10, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@zhangbo9674 zhangbo9674 changed the title [IR] Type system stage2: add class Type, class TypeUniquer, class IRContext [IR] Type system stage2: add class Type, type uniquer utils, class IRContext Feb 14, 2023
Copy link
Contributor

@winter-wang winter-wang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前paddle的单测文件好像是以test_打头或者以_tester结尾,建议保持一致。

/// \return Global parameterless for IrContext.
///
static IrContext *Instance() {
VLOG(4) << "=> Instance called with context (ir_context_ == " << ir_context_
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ir_context这儿多线程是不是有风险?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,我完善一下

///
template <typename T>
static void RegisterType(IrContext *ctx) {
RegisterType<T>(ctx, T::type_id()); // class Type需要提供type_id接口
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个直接调用 TypeID::get()不可以吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以

winter-wang
winter-wang previously approved these changes Feb 22, 2023
Copy link
Contributor

@winter-wang winter-wang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

template <typename T> \
static bool classof(T val) { \
return val.type_id() == type_id(); \
} \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add blank line here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, tks.


DECLARE_TYPE_UTILITY_FUNCTOR(Float32Type, ir::TypeStorage);

static Float32Type get(ir::IrContext *context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line can move into macro DECLARE_TYPE_UTILITY_FUNCTOR? and same for the first line?

Copy link
Contributor Author

@zhangbo9674 zhangbo9674 Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DECLARE_TYPE_UTILITY_FUNCTOR中已经提供了默认的get方法
template <typename... Args> static concrete_type get(ir::IrContext *ctx, Args... args) { return ir::TypeManager::template get<concrete_type>(ctx, args...); }
这里是针对内置类型,重载了一个get方法,直接从IrContext的缓存中获取,而上述宏定义中默认的get方法是通过TypeManager获取的


file(GLOB IR_SRCS "*.cc")

file(GLOB IR_TEST_SRCS "*_test.cc")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better move all tests into paddle/ir/tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done,tks.

///
class Type {
public:
using StorageType = TypeStorage;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why rename it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了统一有参类型的Storage(TypeStorage的派生)和无参类型Storage(TypeStorage)的名字,如果都使用TypeStorage,可能会误导认为是无参类型

abstract_type_ = const_cast<AbstractType *>(&abstract_type);
}

AbstractType *abstract_type_{nullptr};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be marked as not owned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, tks.

Copy link
Contributor

@zhiqiu zhiqiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants