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

[Type] [refactor] Make TypeFactory Global #1963

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
6 changes: 3 additions & 3 deletions taichi/ir/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ TLANG_NAMESPACE_BEGIN
// This part doesn't look good, but we will remove it soon anyway.
#define PER_TYPE(x) \
DataType PrimitiveType::x = \
DataType(Program::get_type_factory().get_primitive_type( \
DataType(TypeFactory::get_instance().get_primitive_type( \
PrimitiveType::primitive_type::x));

#include "taichi/inc/data_type.inc.h"
Expand Down Expand Up @@ -46,7 +46,7 @@ bool DataType::is_pointer() const {

void DataType::set_is_pointer(bool is_ptr) {
if (is_ptr && !ptr_->is<PointerType>()) {
ptr_ = Program::get_type_factory().get_pointer_type(ptr_);
ptr_ = TypeFactory::get_instance().get_pointer_type(ptr_);
}
if (!is_ptr && ptr_->is<PointerType>()) {
ptr_ = ptr_->cast<PointerType>()->get_pointee_type();
Expand All @@ -70,7 +70,7 @@ std::string PrimitiveType::to_string() const {
DataType LegacyVectorType(int width, DataType data_type, bool is_pointer) {
TI_ASSERT(width == 1);
if (is_pointer) {
return Program::get_type_factory().get_pointer_type(data_type.get_ptr());
return TypeFactory::get_instance().get_pointer_type(data_type.get_ptr());
} else {
return data_type;
}
Expand Down
8 changes: 8 additions & 0 deletions taichi/ir/type_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

TLANG_NAMESPACE_BEGIN

TypeFactory &TypeFactory::get_instance() {
static TypeFactory type_factory;
return type_factory;
}
Copy link
Member

Choose a reason for hiding this comment

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

We probably want to stick to the old implementation in Program::get_type_factory since if type_factory get destroyed before other global variables that use it, the program won't finish smoothly.

Copy link
Member

Choose a reason for hiding this comment

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

Btw, since #1962 is in, feel free to remove Program::get_type_factory() in a different PR :-) Thanks!

Copy link
Contributor Author

@Hanke98 Hanke98 Oct 16, 2020

Choose a reason for hiding this comment

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

We probably want to stick to the old implementation in Program::get_type_factory since if type_factory get destroyed before other global variables that use it, the program won't finish smoothly.

Yes, you are right. let me change it to the old implementation. Thanks for pointing it out!


Type *TypeFactory::get_primitive_type(PrimitiveType::primitive_type id) {
std::lock_guard<std::mutex> _(mut_);

Expand All @@ -28,4 +33,7 @@ Type *TypeFactory::get_pointer_type(Type *element) {
return pointer_types_[key].get();
}

TypeFactory::TypeFactory() {
}

TLANG_NAMESPACE_END
4 changes: 4 additions & 0 deletions taichi/ir/type_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ TLANG_NAMESPACE_BEGIN

class TypeFactory {
public:
static TypeFactory &get_instance();

Type *get_primitive_type(PrimitiveType::primitive_type id);

Type *get_vector_type(int num_elements, Type *element);

Type *get_pointer_type(Type *element);

private:
TypeFactory();

std::unordered_map<PrimitiveType::primitive_type, std::unique_ptr<Type>>
primitive_types_;

Expand Down
2 changes: 1 addition & 1 deletion taichi/lang_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ class TypePromotionMapping {
DataType query(DataType x, DataType y) {
auto primitive =
mapping[std::make_pair(to_primitive_type(x), to_primitive_type(y))];
return Program::get_type_factory().get_primitive_type(primitive);
return TypeFactory::get_instance().get_primitive_type(primitive);
}

private:
Expand Down
7 changes: 4 additions & 3 deletions taichi/program/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ Program::Program(Arch desired_arch) {
}

TypeFactory &Program::get_type_factory() {
// type_factory should never be destroyed, hence the raw new operator.
static TypeFactory *type_factory = new TypeFactory;
return *type_factory;
TI_WARN(
"Program::get_type_factory() will be deprecated, Please use "
"TypeFactory::get_instance()");
return TypeFactory::get_instance();
}

FunctionType Program::compile(Kernel &kernel) {
Expand Down