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] Add basic implementations of VectorType and PointerType #1948

Merged
merged 5 commits into from
Oct 13, 2020

Conversation

yuanming-hu
Copy link
Member

Related issue = #1905

Changes:

  • Added basic VectorType and PointerType
  • Updated TypeFactor to support creating the two new types
  • Removed using namespace llvm in codegen_llvm.h since now taichi::lang::PointerType conflicts with llvm::PointerType. Added some necessary llvm:: prefixes to fix compilation.
  • Moved Stmt::is_ptr to ArgLoadStmt::is_ptr since that's the only subclass that uses the member.
  • Moved Type-related classes from taichi/lang_util.h/cpp to taichi/ir/type.h/cpp

[Click here for the format server]


@@ -50,17 +50,17 @@ FunctionCreationGuard::FunctionCreationGuard(
// emit into loop body function
Copy link
Member Author

Choose a reason for hiding this comment

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

Changes in this file are mostly adding the llvm:: namespace.

Comment on lines -871 to +878
if (stmt->is_ptr) {
if (stmt->ret_type.is_pointer()) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the only substantial change in this file. Note that this part of the code is never used yet anyway.

@@ -10,8 +10,6 @@

TLANG_NAMESPACE_BEGIN

using namespace llvm;
Copy link
Member Author

Choose a reason for hiding this comment

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

Removing since llvm::PointerType conflicts with taichi::lang::PointerType.

Comment on lines +6 to +43
// 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.

// 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

DataType::DataType() : ptr_(PrimitiveType::unknown.ptr_) {
}

DataType PrimitiveType::get(PrimitiveType::primitive_type t) {
if (false) {
}
#define PER_TYPE(x) else if (t == primitive_type::x) return PrimitiveType::x;
#include "taichi/inc/data_type.inc.h"
#undef PER_TYPE
else {
TI_NOT_IMPLEMENTED
}
}

std::size_t DataType::hash() const {
if (auto primitive = dynamic_cast<const PrimitiveType *>(ptr_)) {
return (std::size_t)primitive->type;
} else {
TI_NOT_IMPLEMENTED
}
}

std::string PrimitiveType::to_string() const {
return data_type_name(DataType(this));
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This part is completely copy-pasted from lang_util.cpp.

Comment on lines +7 to +65
class Type {
public:
virtual std::string to_string() const = 0;
virtual ~Type() {
}
};

// A "Type" handle. This should be removed later.
class DataType {
public:
DataType();

DataType(const Type *ptr) : ptr_(ptr) {
}

bool operator==(const DataType &o) const {
return ptr_ == o.ptr_;
}

bool operator!=(const DataType &o) const {
return !(*this == o);
}

std::size_t hash() const;

std::string to_string() const {
return ptr_->to_string();
};

// TODO: DataType itself should be a pointer in the future
const Type *get_ptr() const {
return ptr_;
}

private:
const Type *ptr_;
};

class PrimitiveType : public Type {
public:
enum class primitive_type : int {
#define PER_TYPE(x) x,
#include "taichi/inc/data_type.inc.h"
#undef PER_TYPE
};

#define PER_TYPE(x) static DataType x;
#include "taichi/inc/data_type.inc.h"
#undef PER_TYPE

primitive_type type;

PrimitiveType(primitive_type type) : type(type) {
}

std::string to_string() const override;

static DataType get(primitive_type type);
};
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy-pasted from lang_util.h.

@@ -19,66 +20,6 @@ struct Context;

using FunctionType = std::function<void(Context &)>;

class Type {
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to type.cpp

@@ -30,45 +30,6 @@ real get_cpu_frequency() {

real default_measurement_time = 1;

// Note: these primitive types should never be freed. They are supposed to live
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to type.h.

Copy link
Collaborator

@TH3CHARLie TH3CHARLie left a comment

Choose a reason for hiding this comment

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

Generally LGTM, I have only one minor comment about an attribute.


private:
Type *pointee_{nullptr};
int addr_space_{0}; // TODO: make this an enum
Copy link
Collaborator

@TH3CHARLie TH3CHARLie Oct 13, 2020

Choose a reason for hiding this comment

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

how about documenting this attribute(a comment would do the job)?

Copy link
Member Author

Choose a reason for hiding this comment

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

After we switch this part to an enum, the enum values will document themselves :-)

@yuanming-hu yuanming-hu merged commit dcd5d7d into taichi-dev:master Oct 13, 2020
@@ -1,4 +1,5 @@
#include "taichi/ir/type_factory.h"
#include "type_factory.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if this line will cause some compilers to think "type_factory.h" and "taichi/ir/type_factory.h" are different files and include it twice?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. The file will be included twice, which is fine since we have #pragma once. But the second line is unnecessary so I'll remove it in a future PR :-)

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