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] Initial TypeFactory class #1942

Merged
merged 3 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions taichi/ir/type_factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "taichi/ir/type_factory.h"

TLANG_NAMESPACE_BEGIN

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

if (primitive_types_.find(id) == primitive_types_.end()) {
primitive_types_[id] = std::make_unique<PrimitiveType>(id);
}

return primitive_types_[id].get();
}

TLANG_NAMESPACE_END
18 changes: 18 additions & 0 deletions taichi/ir/type_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "taichi/lang_util.h"

#include <mutex>

TLANG_NAMESPACE_BEGIN

class TypeFactory {
public:
Type *get_primitive_type(PrimitiveType::primitive_type id);

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

std::mutex mut_;
};

TLANG_NAMESPACE_END
13 changes: 9 additions & 4 deletions taichi/lang_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "taichi/math/linalg.h"
#include "taichi/program/arch.h"
#include "taichi/program/program.h"
#include "taichi/program/compile_config.h"
#include "taichi/system/timer.h"

Expand Down Expand Up @@ -31,10 +32,14 @@ real default_measurement_time = 1;

// Note: these primitive types should never be freed. They are supposed to live
// together with the process. This is a temporary solution. Later we should
// manage its ownership more systematically
#define PER_TYPE(x) \
DataType PrimitiveType::x = \
DataType(new PrimitiveType(PrimitiveType::primitive_type::x));
// manage its ownership more systematically.

// 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( \
PrimitiveType::primitive_type::x));

#include "taichi/inc/data_type.inc.h"
#undef PER_TYPE

Expand Down
6 changes: 6 additions & 0 deletions taichi/program/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ Program::Program(Arch desired_arch) {
arch_name(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;
}

FunctionType Program::compile(Kernel &kernel) {
auto start_t = Time::get_time();
TI_AUTO_PROF;
Expand Down
5 changes: 5 additions & 0 deletions taichi/program/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#define TI_RUNTIME_HOST
#include "taichi/ir/ir.h"
#include "taichi/ir/type_factory.h"
#include "taichi/ir/snode.h"
#include "taichi/lang_util.h"
#include "taichi/llvm/llvm_context.h"
Expand Down Expand Up @@ -105,6 +106,10 @@ class Program {
jit_evaluator_cache;
std::mutex jit_evaluator_cache_mut;

// Note: for now we let all Programs share a single TypeFactory for smooth
// migration. In the future each program should have its own copy.
static TypeFactory &get_type_factory();

Program() : Program(default_compile_config.arch) {
}

Expand Down