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

[ir] Deprecate FrontendAtomicStmt #907

Merged
merged 3 commits into from
May 2, 2020
Merged

[ir] Deprecate FrontendAtomicStmt #907

merged 3 commits into from
May 2, 2020

Conversation

k-ye
Copy link
Member

@k-ye k-ye commented May 1, 2020

I added AtomicOpExpression back in Jan, where we wanted to make ti.atomic_*() return the old value. Now I think it's a good time to merge FrontendAtomicStmt into AtomicOpExpression.

Related issue = #689 , #332

[Click here for the format server]

@k-ye k-ye requested a review from yuanming-hu May 1, 2020 11:06
Copy link
Collaborator

@archibate archibate left a comment

Choose a reason for hiding this comment

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

Great step towards TAI/CHI IR standardization! LGTM except for a few questions..

Comment on lines +845 to +849
// replace atomic sub with negative atomic add
if (op_type == AtomicOpType::sub) {
val.set(Expr::make<UnaryOpExpression>(UnaryOpType::neg, val));
op_type = AtomicOpType::add;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why? Could you clarify the reason in the comment? I don't understand.. wasn't atomic_sub slimer than neg+atomic_add?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't make any change here. Please compare this with the old code (visit(FrontendAtomicStmt*))

Copy link
Collaborator

Choose a reason for hiding this comment

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

So we should ask @yuanming-hu for reason?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the reason is simply that the codegen assumes that sub is converted to add in the previous passes, see

if (stmt->op_type == AtomicOpType::add) {
if (is_integral(stmt->val->ret_type.data_type)) {
old_value = builder->CreateAtomicRMW(
llvm::AtomicRMWInst::BinOp::Add, llvm_val[stmt->dest],
llvm_val[stmt->val], llvm::AtomicOrdering::SequentiallyConsistent);
} else if (stmt->val->ret_type.data_type == DataType::f32) {
old_value =
builder->CreateCall(get_runtime_function("atomic_add_f32"),
{llvm_val[stmt->dest], llvm_val[stmt->val]});
} else if (stmt->val->ret_type.data_type == DataType::f64) {
old_value =
builder->CreateCall(get_runtime_function("atomic_add_f64"),
{llvm_val[stmt->dest], llvm_val[stmt->val]});
} else {
TI_NOT_IMPLEMENTED
}
} else if (stmt->op_type == AtomicOpType::min) {
. It only handles add, but not sub

Copy link
Collaborator

Choose a reason for hiding this comment

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

So let's just add handles for sub? It should have no harm but profitable right? Also metal and gl handles sub too, no reason special for 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.

I think the current approach is fine. Adding handling forsub will have to cover all the backends, not just for LLVM. Again, let’s always focus one PR for one thing.

taichi/ir/expr.cpp Outdated Show resolved Hide resolved
Copy link
Collaborator

@archibate archibate 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 in general, thank you!

void AtomicOpExpression::flatten(FlattenContext *ctx) {
// replace atomic sub with negative atomic add
if (op_type == AtomicOpType::sub) {
val.set(Expr::make<UnaryOpExpression>(UnaryOpType::neg, val));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will this cause an circular reference?

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, but nope. If you look into set():

taichi/taichi/ir/expr.h

Lines 47 to 49 in f5373b1

void set(const Expr &o) {
expr = o.expr;
}

It assigns expr to another std::shared_ptr. shared_ptr::operator=() will ref the new object, and deref the old object. https://en.cppreference.com/w/cpp/memory/shared_ptr/operator%3D

Comment on lines -139 to +140
current_ast_builder().insert(Stmt::make<FrontendAtomicStmt>(
AtomicOpType::add, *this, -load_if_ptr(o)));
(*this) = Expr::make<AtomicOpExpression>(
AtomicOpType::sub, ptr_if_global(*this), load_if_ptr(o));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why ptr_if_global appeared? I thought it's already done in L856?

Copy link
Member Author

@k-ye k-ye May 2, 2020

Choose a reason for hiding this comment

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

I think it's a bug in the old code. See L131 in the old code for how it handles ::add, which has ptr_if_global. Also L856 checks for GlobalPtrExpression, whereas ptr_if_global() checks for GlobalVariableExpression.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, my feeling is ptr_if_global should be removed completely in a future PR. We use this wrapper just to allow the syntax like a += 1 for a = ti.var(ti.f32, shape()) (0-D tensors). Enforcing the user to write ``a[None] += 1` will make learning easier since the rule is simpler (though more verbose).

Copy link
Member Author

Choose a reason for hiding this comment

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

ah ok, then let's do that in a separate PR..

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.

Thanks! LGTM.

Comment on lines -139 to +140
current_ast_builder().insert(Stmt::make<FrontendAtomicStmt>(
AtomicOpType::add, *this, -load_if_ptr(o)));
(*this) = Expr::make<AtomicOpExpression>(
AtomicOpType::sub, ptr_if_global(*this), load_if_ptr(o));
Copy link
Member

Choose a reason for hiding this comment

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

Actually, my feeling is ptr_if_global should be removed completely in a future PR. We use this wrapper just to allow the syntax like a += 1 for a = ti.var(ti.f32, shape()) (0-D tensors). Enforcing the user to write ``a[None] += 1` will make learning easier since the rule is simpler (though more verbose).

@k-ye k-ye merged commit d19ccd9 into taichi-dev:master May 2, 2020
@k-ye k-ye deleted the atomics branch May 2, 2020 06:29
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