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] Add compute_type for CustomIntType #2047

Merged
merged 32 commits into from Nov 20, 2020

Conversation

Hanke98
Copy link
Collaborator

@Hanke98 Hanke98 commented Nov 13, 2020

Related issue = #1905

In this pr, we add an attribute for CustomIntType named 'compute_type' and replace some 'hand-write 32'. Some TODO could be removed after this pr being merged.

[Click here for the format server]


@codecov
Copy link

codecov bot commented Nov 13, 2020

Codecov Report

Merging #2047 (985f35e) into master (46d5801) will increase coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2047      +/-   ##
==========================================
+ Coverage   43.54%   43.56%   +0.01%     
==========================================
  Files          45       45              
  Lines        6267     6267              
  Branches     1110     1110              
==========================================
+ Hits         2729     2730       +1     
  Misses       3366     3366              
+ Partials      172      171       -1     
Impacted Files Coverage Δ
python/taichi/lang/transformer.py 81.32% <0.00%> (-0.70%) ⬇️
python/taichi/lang/impl.py 66.93% <0.00%> (+0.53%) ⬆️
python/taichi/lang/snode.py 68.33% <0.00%> (+2.50%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c90be8a...985f35e. Read the comment docs.

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.

Looks good, thanks!

taichi/ir/type.h Outdated
// and should we expose it to users?
TI_ASSERT(num_bits <= 32);
compute_type = is_signed ? new PrimitiveType(PrimitiveTypeID::i32)
: new PrimitiveType(PrimitiveTypeID::u32);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe use type_factory here?

@Hanke98
Copy link
Collaborator Author

Hanke98 commented Nov 14, 2020

Please don't merge this pr for now. I have found some potential problems and will request your review after I fixed them. Thanks!

Comment on lines 1165 to 1171
auto left =
builder->CreateSub(tlctx->get_constant(compute_type_size), bit_end);
auto right = builder->CreateSub(tlctx->get_constant(compute_type_size),
tlctx->get_constant(cit->get_num_bits()));
left = builder->CreateIntCast(left, llvm_type(cit->get_compute_type()),
cit->get_is_signed());
right = builder->CreateIntCast(right, llvm_type(cit->get_compute_type()),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

After testing the case of 16-bits and 64-bits, I found that we should use the num of physical_type bits in BitStructType SNode here instead of the compute_type of CustomIntType. So I decided to convert this pr to draft and reimplement this part.

@Hanke98 Hanke98 marked this pull request as draft November 14, 2020 10:41
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

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

Cool! I didn't go through every file but it looks that we are on the right track!

Comment on lines 622 to 645
llvm::Type *CodeGenLLVM::llvm_ptr_type(DataType dt) {
if (dt->is_primitive(PrimitiveTypeID::i8) ||
dt->is_primitive(PrimitiveTypeID::u8)) {
return llvm::Type::getInt8PtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::i16) ||
dt->is_primitive(PrimitiveTypeID::u16)) {
return llvm::Type::getInt16PtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::i32) ||
dt->is_primitive(PrimitiveTypeID::u32)) {
return llvm::Type::getInt32PtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::i64) ||
dt->is_primitive(PrimitiveTypeID::u64)) {
return llvm::Type::getInt64PtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::u1)) {
return llvm::Type::getInt1PtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::f32)) {
return llvm::Type::getFloatPtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::f64)) {
return llvm::Type::getDoublePtrTy(*llvm_context);
} else {
TI_NOT_IMPLEMENTED;
}
return nullptr;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
llvm::Type *CodeGenLLVM::llvm_ptr_type(DataType dt) {
if (dt->is_primitive(PrimitiveTypeID::i8) ||
dt->is_primitive(PrimitiveTypeID::u8)) {
return llvm::Type::getInt8PtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::i16) ||
dt->is_primitive(PrimitiveTypeID::u16)) {
return llvm::Type::getInt16PtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::i32) ||
dt->is_primitive(PrimitiveTypeID::u32)) {
return llvm::Type::getInt32PtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::i64) ||
dt->is_primitive(PrimitiveTypeID::u64)) {
return llvm::Type::getInt64PtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::u1)) {
return llvm::Type::getInt1PtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::f32)) {
return llvm::Type::getFloatPtrTy(*llvm_context);
} else if (dt->is_primitive(PrimitiveTypeID::f64)) {
return llvm::Type::getDoublePtrTy(*llvm_context);
} else {
TI_NOT_IMPLEMENTED;
}
return nullptr;
}
llvm::Type *CodeGenLLVM::llvm_ptr_type(DataType dt) {
return llvm::PointerType::get(llvm_type(dt), 0);
}

taichi/codegen/codegen_llvm.cpp Outdated Show resolved Hide resolved
taichi/ir/type.cpp Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@Hanke98 Hanke98 left a comment

Choose a reason for hiding this comment

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

Finally, I think it's time for a formal review. Please help me to check my refactoring! Thanks a lot !

taichi/codegen/codegen_llvm.cpp Outdated Show resolved Hide resolved
Comment on lines 26 to 28
Type *get_custom_int_type_with_compute_type(int compute_type_bits,
int num_bits,
bool is_signed);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The function name might be too long. Any good advices would be appreciated!

Copy link
Member

Choose a reason for hiding this comment

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

My two cents: actually every CustomIntType has a compute type. I would use get_custom_int_type(int num_bits, bool is_signed, Type *compute_type=nullptr) if that makes sense to you. Then two APIs can be merged into one.

@Hanke98 Hanke98 marked this pull request as ready for review November 19, 2020 13:30
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

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

Very cool! Feel free to merge after the comments are addressed. Thanks!

taichi/ir/type.cpp Outdated Show resolved Hide resolved
taichi/ir/type.cpp Show resolved Hide resolved
taichi/ir/type.h Outdated
Comment on lines 169 to 176
CustomIntType(int num_bits, bool is_signed);

CustomIntType(int compute_type_bits, int numBits, bool isSigned);

CustomIntType(int compute_type_bits,
Type *physical_type,
int num_bits,
bool is_signed);
Copy link
Member

Choose a reason for hiding this comment

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

A single ctor CustomIntType(int num_bits, bool is_signed, Type *physical_type_=nullptr, Type *compute_type_=nullptr) may be cleaner?

Comment on lines 26 to 28
Type *get_custom_int_type_with_compute_type(int compute_type_bits,
int num_bits,
bool is_signed);
Copy link
Member

Choose a reason for hiding this comment

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

My two cents: actually every CustomIntType has a compute type. I would use get_custom_int_type(int num_bits, bool is_signed, Type *compute_type=nullptr) if that makes sense to you. Then two APIs can be merged into one.

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.

LGTM! Just one minor comment improvement

@@ -328,7 +328,8 @@ void CodeGenLLVM::visit(UnaryOpStmt *stmt) {
auto from_size = 0;
if (from->is<CustomIntType>()) {
// TODO: replace 32 with a customizable type
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove TODO here

@Hanke98 Hanke98 merged commit 2b9b0a2 into taichi-dev:master Nov 20, 2020
@yuanming-hu yuanming-hu mentioned this pull request Nov 20, 2020
@Hanke98 Hanke98 deleted the impl-compute-type branch November 27, 2020 06:32
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.

None yet

4 participants