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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0780205
add TypeUniquer and IrContext
zhangbo9674 Feb 10, 2023
819ed91
refine include code
zhangbo9674 Feb 10, 2023
e233603
add Type, TypeBase
zhangbo9674 Feb 10, 2023
40e8a6d
add built-in type
zhangbo9674 Feb 13, 2023
4cdcc24
add bulit-in Float32Type
zhangbo9674 Feb 14, 2023
f5cb63d
refine ut
zhangbo9674 Feb 14, 2023
4fe071a
refine code
zhangbo9674 Feb 15, 2023
23d22af
refine code
zhangbo9674 Feb 16, 2023
cc75738
delete type_base
zhangbo9674 Feb 16, 2023
7709d43
rename ImplType to StorageType
zhangbo9674 Feb 16, 2023
afd9c26
rename ImplType to StorageType
zhangbo9674 Feb 16, 2023
5697c4d
add macros util for register type
zhangbo9674 Feb 16, 2023
9df0868
add macros util for register type
zhangbo9674 Feb 16, 2023
571cec1
refine name
zhangbo9674 Feb 16, 2023
41268aa
refine name
zhangbo9674 Feb 17, 2023
d3acb79
change storage manager
zhangbo9674 Feb 17, 2023
4900064
add multi_thread for ir_ctx
zhangbo9674 Feb 20, 2023
39d69cf
rwlock_2_spinlock, add REGISTER_TYPE_2_IRCONTEXT
zhangbo9674 Feb 21, 2023
4736b46
DECLARE_TYPE_UTILITY_FUNCTOR
zhangbo9674 Feb 21, 2023
183e3a1
refine ircontext singleton
zhangbo9674 Feb 21, 2023
96cd329
del destructor for ParametricStorageManager
zhangbo9674 Feb 21, 2023
73efa45
refine code
zhangbo9674 Feb 21, 2023
89c953f
Add necessary logs for debugging
zhangbo9674 Feb 21, 2023
c4b5e71
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
zhangbo9674 Feb 21, 2023
5451cba
refine ir_context instance
zhangbo9674 Feb 22, 2023
c7cf04f
refine type get interface
zhangbo9674 Feb 22, 2023
85f3cd4
refine code by comment
zhangbo9674 Feb 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion paddle/ir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ if(NOT WITH_NEWIR)
return()
endif()

add_subdirectory(type)
set(NEWIR_SOURCE_DIR "${PADDLE_SOURCE_DIR}/paddle/ir")
set(NEWIR_BINARY_DIR "${PADDLE_BINARY_DIR}/paddle/ir")

# ir tests
add_subdirectory(tests)

file(GLOB IR_SRCS "*.cc")

cc_library(new_ir SRCS ${IR_SRCS})
42 changes: 42 additions & 0 deletions paddle/ir/builtin_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "paddle/ir/type.h"

namespace ir {
///
/// \brief Definitions of built-in type classes. The built-in type object get
/// method is as follows: Type fp32 = Float32Type::get(ctx);
///
class Float32Type : public ir::Type {
public:
using Type::Type;

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获取的

};

class Int32Type : public ir::Type {
public:
using Type::Type;

DECLARE_TYPE_UTILITY_FUNCTOR(Int32Type, ir::TypeStorage);

static Int32Type get(ir::IrContext *context);
};

} // namespace ir
118 changes: 118 additions & 0 deletions paddle/ir/ir_context.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// 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 <unordered_map>

#include "paddle/ir/builtin_type.h"
#include "paddle/ir/ir_context.h"
#include "paddle/ir/spin_lock.h"
#include "paddle/ir/type_base.h"

namespace ir {
// The implementation class of the IrContext class
class IrContextImpl {
public:
IrContextImpl() {}

~IrContextImpl() {
std::lock_guard<ir::SpinLock> guard(registed_abstract_types_lock_);
for (auto abstract_type_map : registed_abstract_types_) {
delete abstract_type_map.second;
}
registed_abstract_types_.clear();
}

void RegisterAbstractType(ir::TypeId type_id, AbstractType *abstract_type) {
std::lock_guard<ir::SpinLock> guard(registed_abstract_types_lock_);
VLOG(4) << "IrContext register an abstract_type of: [TypeId_hash="
<< std::hash<ir::TypeId>()(type_id)
<< ", AbstractType_ptr=" << abstract_type << "].";
registed_abstract_types_.emplace(type_id, abstract_type);
}

AbstractType *lookup(ir::TypeId type_id) {
std::lock_guard<ir::SpinLock> guard(registed_abstract_types_lock_);
auto iter = registed_abstract_types_.find(type_id);
if (iter == registed_abstract_types_.end()) {
VLOG(4) << "IrContext not fonund cached abstract_type of: [TypeId_hash="
<< std::hash<ir::TypeId>()(type_id) << "].";
return nullptr;
} else {
VLOG(4) << "IrContext fonund a cached abstract_type of: [TypeId_hash="
<< std::hash<ir::TypeId>()(type_id)
<< ", AbstractType_ptr=" << iter->second << "].";
return iter->second;
}
}

ir::SpinLock registed_abstract_types_lock_;

// Cached AbstractType instances.
std::unordered_map<TypeId, AbstractType *> registed_abstract_types_;

// TypeStorage uniquer and cache instances.
StorageManager registed_storage_manager_;

// Some built-in type.
Float32Type fp32_type;
Int32Type int32_type;
};

IrContext *IrContext::Instance() {
static IrContext context;
return &context;
}

IrContext::IrContext() : impl_(new IrContextImpl()) {
VLOG(4) << "IrContext register built-in type...";
REGISTER_TYPE_2_IRCONTEXT(Float32Type, this);
impl_->fp32_type = TypeManager::get<Float32Type>(this);
VLOG(4) << "Float32Type registration complete";
REGISTER_TYPE_2_IRCONTEXT(Int32Type, this);
impl_->int32_type = TypeManager::get<Int32Type>(this);
VLOG(4) << "Int32Type registration complete";
}

void IrContext::RegisterAbstractType(ir::TypeId type_id,
AbstractType *abstract_type) {
impl().RegisterAbstractType(type_id, abstract_type);
}

StorageManager &IrContext::storage_manager() {
return impl().registed_storage_manager_;
}

std::unordered_map<TypeId, AbstractType *>
&IrContext::registed_abstracted_type() {
return impl().registed_abstract_types_;
}

const AbstractType &AbstractType::lookup(TypeId type_id, IrContext *ctx) {
VLOG(4) << "Lookup abstract type [TypeId_hash="
<< std::hash<ir::TypeId>()(type_id) << "] from IrContext [ptr=" << ctx
<< "].";
auto &impl = ctx->impl();
AbstractType *abstract_type = impl.lookup(type_id);
if (abstract_type) {
return *abstract_type;
} else {
throw("Abstract type not found in IrContext.");
}
}

Float32Type Float32Type::get(IrContext *ctx) { return ctx->impl().fp32_type; }

Int32Type Int32Type::get(IrContext *ctx) { return ctx->impl().int32_type; }

} // namespace ir
82 changes: 82 additions & 0 deletions paddle/ir/ir_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <glog/logging.h>
#include <memory>
#include <unordered_map>

namespace ir {
class IrContextImpl;
class StorageManager;
class AbstractType;
class TypeId;

///
/// \brief IrContext is a global parameterless class used to store and manage
/// Type and its related data structures.
///
class IrContext {
public:
///
/// \brief Initializes a new instance of IrContext.
///
static IrContext *Instance();

///
/// \brief Get an instance of IrContextImpl, a private member of IrContext.
/// For the specific definition of IrContextImpl, see ir_context.cc.
///
/// \return The instance of IrContextImpl.
///
IrContextImpl &impl() { return *impl_; }

///
/// \brief Register an AbstractType to IrContext
///
/// \param type_id The type id of the AbstractType.
/// \param abstract_type AbstractType* provided by user.
///
void RegisterAbstractType(ir::TypeId type_id, AbstractType *abstract_type);

///
/// \brief Returns the storage uniquer used for constructing TypeStorage
/// instances.
///
/// \return The storage uniquer used for constructing TypeStorage
/// instances.
///
StorageManager &storage_manager();

///
/// \brief Returns the storage uniquer used for constructing TypeStorage
/// instances.
///
/// \return The storage uniquer used for constructing TypeStorage
/// instances.
///
std::unordered_map<TypeId, AbstractType *> &registed_abstracted_type();

IrContext(const IrContext &) = delete;

void operator=(const IrContext &) = delete;

private:
IrContext();

const std::unique_ptr<IrContextImpl> impl_;
};

} // namespace ir
66 changes: 66 additions & 0 deletions paddle/ir/spin_lock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <atomic>
#if defined(_M_X64) || defined(__x86_64__) || defined(_M_IX86) || \
defined(__i386__)
#define __PADDLE_x86__
#include <immintrin.h>
#endif
#include <mutex>
#include <thread>

namespace ir {
static inline void CpuRelax() {
#if defined(__PADDLE_x86__)
_mm_pause();
#endif
}

class SpinLock {
public:
SpinLock() : mlock_(false) {}

void lock() {
for (;;) {
if (!mlock_.exchange(true, std::memory_order_acquire)) {
break;
}
constexpr int kMaxLoop = 32;
for (int loop = 1; mlock_.load(std::memory_order_relaxed);) {
if (loop <= kMaxLoop) {
for (int i = 1; i <= loop; ++i) {
CpuRelax();
}
loop *= 2;
} else {
std::this_thread::yield();
}
}
}
}

void unlock() { mlock_.store(false, std::memory_order_release); }

private:
SpinLock(const SpinLock&) = delete;
SpinLock(SpinLock&&) = delete;
SpinLock& operator=(const SpinLock&) = delete;
SpinLock& operator=(SpinLock&&) = delete;
std::atomic<bool> mlock_;
};

} // namespace ir
Loading