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 CustomFloatType #2062

Merged
merged 19 commits into from
Nov 26, 2020
Merged

Conversation

yuanming-hu
Copy link
Member

@yuanming-hu yuanming-hu commented Nov 25, 2020

Related issue = #1905

Sorry about the large changes.

  • Add CustomFloatType
  • Support global load/store of CustomFloatType (atomid_add will be done later.)
  • KernelReturnStmt/ArgLoadStmt ctor no longer takes data type. Their return data types are inferred by type_check
  • get_compute_type is now part of Type API. Overriden in derived classes CustomFloatType, CustomIntType, PrimitiveType.
  • Return type of GlobalPtrStmt is now a pointer type (used to be only the pointee type)
  • Refactor/implement SNode reader/writer for CustomFloat/IntType
  • Add is_real(DataType)
  • GlobalStoreStmt no longer has a return data type

[Click here for the format server]


@yuanming-hu yuanming-hu changed the title [type] Add CustumFloatType [type] Add CustomFloatType Nov 25, 2020
@codecov
Copy link

codecov bot commented Nov 25, 2020

Codecov Report

Merging #2062 (0234496) into master (0b8439f) will decrease coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2062      +/-   ##
==========================================
- Coverage   43.56%   43.53%   -0.03%     
==========================================
  Files          45       45              
  Lines        6267     6264       -3     
  Branches     1110     1110              
==========================================
- Hits         2730     2727       -3     
  Misses       3366     3366              
  Partials      171      171              
Impacted Files Coverage Δ
python/taichi/lang/transformer.py 81.22% <ø> (-0.10%) ⬇️
python/taichi/lang/expr.py 66.85% <100.00%> (ø)

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 0b8439f...29d2050. Read the comment docs.

Comment on lines -1129 to -1158
if (stmt->ptr->ret_type->as<PointerType>()->is_bit_pointer()) {
auto cit = stmt->ret_type->as<CustomIntType>();
// 1. load bit pointer
llvm::Value *byte_ptr, *bit_offset;
read_bit_pointer(llvm_val[stmt->ptr], byte_ptr, bit_offset);

auto bit_level_container = builder->CreateLoad(builder->CreateBitCast(
byte_ptr, llvm_ptr_type(cit->get_physical_type())));
// 2. bit shifting
// first left shift `physical_type - (offset + num_bits)`
// then right shift `physical_type - num_bits`
auto bit_end = builder->CreateAdd(bit_offset,
tlctx->get_constant(cit->get_num_bits()));
auto left = builder->CreateSub(
tlctx->get_constant(data_type_bits(cit->get_physical_type())), bit_end);
auto right = builder->CreateSub(
tlctx->get_constant(data_type_bits(cit->get_physical_type())),
tlctx->get_constant(cit->get_num_bits()));
left = builder->CreateIntCast(left, bit_level_container->getType(), false);
right =
builder->CreateIntCast(right, bit_level_container->getType(), false);
auto step1 = builder->CreateShl(bit_level_container, left);
llvm::Value *step2 = nullptr;
if (cit->get_is_signed())
step2 = builder->CreateAShr(step1, right);
else
step2 = builder->CreateLShr(step1, right);

llvm_val[stmt] = builder->CreateIntCast(
step2, llvm_type(cit->get_compute_type()), cit->get_is_signed());
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 moved without change into load_as_custom_int.

Comment on lines +1158 to +1188
llvm::Value *CodeGenLLVM::load_as_custom_int(Stmt *ptr, Type *load_type) {
auto *cit = load_type->as<CustomIntType>();
// 1. load bit pointer
llvm::Value *byte_ptr, *bit_offset;
read_bit_pointer(llvm_val[ptr], byte_ptr, bit_offset);

auto bit_level_container = builder->CreateLoad(builder->CreateBitCast(
byte_ptr, llvm_ptr_type(cit->get_physical_type())));
// 2. bit shifting
// first left shift `physical_type - (offset + num_bits)`
// then right shift `physical_type - num_bits`
auto bit_end =
builder->CreateAdd(bit_offset, tlctx->get_constant(cit->get_num_bits()));
auto left = builder->CreateSub(
tlctx->get_constant(data_type_bits(cit->get_physical_type())), bit_end);
auto right = builder->CreateSub(
tlctx->get_constant(data_type_bits(cit->get_physical_type())),
tlctx->get_constant(cit->get_num_bits()));
left = builder->CreateIntCast(left, bit_level_container->getType(), false);
right = builder->CreateIntCast(right, bit_level_container->getType(), false);
auto step1 = builder->CreateShl(bit_level_container, left);
llvm::Value *step2 = nullptr;

if (cit->get_is_signed())
step2 = builder->CreateAShr(step1, right);
else
step2 = builder->CreateLShr(step1, right);

return builder->CreateIntCast(step2, llvm_type(cit->get_compute_type()),
cit->get_is_signed());
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Extracted from CodeGenLLVM::visit(GlobalLoadStmt *stmt).

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.

Thanks! Looks great! Left one minor comment

taichi/ir/type.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@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.

Cool! Looks great to me! Thanks!

@yuanming-hu yuanming-hu merged commit db869f7 into taichi-dev:master Nov 26, 2020
@yuanming-hu yuanming-hu deleted the custom-float branch November 26, 2020 02:43
@yuanming-hu yuanming-hu mentioned this pull request Nov 30, 2020
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.

3 participants