-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[IR] Add an IR Builder with some basic functions #2204
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3fcbb98
Add IR Builder with some basic functions
xumingkuan 93d5a7f
Merge branch 'master' into ir-builder
xumingkuan 19e94b8
[skip ci] enforce code format
taichi-gardener 9fab5aa
Apply review
xumingkuan cabf38f
Apply review
xumingkuan f558c9a
Apply review
xumingkuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "taichi/ir/ir_builder.h" | ||
#include "taichi/ir/statements.h" | ||
|
||
TLANG_NAMESPACE_BEGIN | ||
|
||
IRBuilder::IRBuilder() { | ||
root_ = std::make_unique<Block>(); | ||
insert_point_.block = root_->as<Block>(); | ||
insert_point_.position = 0; | ||
} | ||
|
||
Stmt *IRBuilder::insert(std::unique_ptr<Stmt> &&stmt) { | ||
return insert_point_.block->insert(std::move(stmt), insert_point_.position++); | ||
} | ||
|
||
Stmt *IRBuilder::get_int32(int32 value) { | ||
return insert( | ||
Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(TypedConstant( | ||
TypeFactory::get_instance().get_primitive_type(PrimitiveTypeID::i32), | ||
value)))); | ||
} | ||
|
||
Stmt *IRBuilder::get_int64(int64 value) { | ||
return insert( | ||
Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(TypedConstant( | ||
TypeFactory::get_instance().get_primitive_type(PrimitiveTypeID::i64), | ||
value)))); | ||
} | ||
|
||
Stmt *IRBuilder::get_float32(float32 value) { | ||
return insert( | ||
Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(TypedConstant( | ||
TypeFactory::get_instance().get_primitive_type(PrimitiveTypeID::f32), | ||
value)))); | ||
} | ||
|
||
Stmt *IRBuilder::get_float64(float64 value) { | ||
return insert( | ||
Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(TypedConstant( | ||
TypeFactory::get_instance().get_primitive_type(PrimitiveTypeID::f64), | ||
value)))); | ||
} | ||
|
||
Stmt *IRBuilder::get_argument(int arg_id, DataType dt, bool is_ptr) { | ||
return insert(Stmt::make<ArgLoadStmt>(arg_id, dt, is_ptr)); | ||
} | ||
|
||
Stmt *IRBuilder::create_add(Stmt *l, Stmt *r) { | ||
return insert(Stmt::make<BinaryOpStmt>(BinaryOpType::add, l, r)); | ||
} | ||
|
||
Stmt *IRBuilder::create_sub(Stmt *l, Stmt *r) { | ||
return insert(Stmt::make<BinaryOpStmt>(BinaryOpType::sub, l, r)); | ||
} | ||
|
||
Stmt *IRBuilder::create_mul(Stmt *l, Stmt *r) { | ||
return insert(Stmt::make<BinaryOpStmt>(BinaryOpType::mul, l, r)); | ||
} | ||
|
||
Stmt *IRBuilder::create_div(Stmt *l, Stmt *r) { | ||
return insert(Stmt::make<BinaryOpStmt>(BinaryOpType::div, l, r)); | ||
} | ||
|
||
Stmt *IRBuilder::create_floordiv(Stmt *l, Stmt *r) { | ||
return insert(Stmt::make<BinaryOpStmt>(BinaryOpType::floordiv, l, r)); | ||
} | ||
|
||
Stmt *IRBuilder::create_truediv(Stmt *l, Stmt *r) { | ||
return insert(Stmt::make<BinaryOpStmt>(BinaryOpType::truediv, l, r)); | ||
} | ||
|
||
template <typename... Args> | ||
Stmt *IRBuilder::create_print(Args &&... args) { | ||
return insert(Stmt::make<PrintStmt>(std::forward(args))); | ||
} | ||
|
||
TLANG_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
|
||
#include "taichi/ir/ir.h" | ||
|
||
TLANG_NAMESPACE_BEGIN | ||
|
||
class IRBuilder { | ||
public: | ||
struct InsertPoint { | ||
Block *block; | ||
int position; | ||
}; | ||
|
||
private: | ||
std::unique_ptr<IRNode> root_; | ||
InsertPoint insert_point_; | ||
|
||
public: | ||
IRBuilder(); | ||
|
||
// General inserter. Returns stmt.get(). | ||
Stmt *insert(std::unique_ptr<Stmt> &&stmt); | ||
|
||
// Constants. TODO: add more types | ||
Stmt *get_int32(int32 value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may also consider a |
||
Stmt *get_int64(int64 value); | ||
Stmt *get_float32(float32 value); | ||
Stmt *get_float64(float64 value); | ||
|
||
// Kernel arguments. | ||
Stmt *get_argument(int arg_id, DataType dt, bool is_ptr); | ||
xumingkuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Binary operations. Returns the result. | ||
Stmt *create_add(Stmt *l, Stmt *r); | ||
Stmt *create_sub(Stmt *l, Stmt *r); | ||
Stmt *create_mul(Stmt *l, Stmt *r); | ||
|
||
// l / r in C++ | ||
Stmt *create_div(Stmt *l, Stmt *r); | ||
// floor(1.0 * l / r) in C++ | ||
Stmt *create_floordiv(Stmt *l, Stmt *r); | ||
// 1.0 * l / r in C++ | ||
Stmt *create_truediv(Stmt *l, Stmt *r); | ||
|
||
// Print values and strings. Arguments can be Stmt* or std::string. | ||
template <typename... Args> | ||
Stmt *create_print(Args &&... args); | ||
xumingkuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
TLANG_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: does this need to be public?
There was a problem hiding this comment.
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 we need to modify the insertion point... I think it's fine to make it private now.