masahi commented on code in PR #13710:
URL: https://github.com/apache/tvm/pull/13710#discussion_r1063835555
##########
src/tir/ir/data_type_rewriter.cc:
##########
@@ -107,6 +107,26 @@ Stmt DataTypeLegalizer::VisitStmt_(const AttrStmtNode* op)
{
return StmtExprMutator::VisitStmt_(op);
}
+Stmt DataTypeLegalizer::VisitStmt_(const LetStmtNode* op) {
+ PrimExpr value = this->VisitExpr(op->value);
+ Stmt body = this->VisitStmt(op->body);
+ if (value.same_as(op->value) && body.same_as(op->body)) {
+ return GetRef<Stmt>(op);
+ } else if (value.dtype() == op->var->dtype) {
+ auto n = CopyOnWrite(op);
+ n->value = std::move(value);
+ n->body = std::move(body);
+ return Stmt(n);
+ } else {
+ auto new_var = op->var.copy_with_dtype(value.dtype());
+ Map<Var, PrimExpr> vmap{{op->var, new_var}};
+ auto new_body = SubstituteWithDataTypeLegalization(
+ std::move(body), [&](const Var& var) { return vmap.Get(var); });
+ // We need to visit the body again to insert additional casts
Review Comment:
I'm experimenting with the following definition of `LetStmt` visitor, which
looks better but it fails to pass the test for some reason (the cast before
buffer store is not inserted). Looking.
```
Stmt DataTypeLegalizer::VisitStmt_(const LetStmtNode* op) {
PrimExpr value = this->VisitExpr(op->value);
auto body = op->body;
auto new_var = op->var.copy_with_dtype(value.dtype());
if (value.dtype() != op->var->dtype) {
Map<Var, PrimExpr> vmap{{op->var, new_var}};
body =
SubstituteWithDataTypeLegalization(op->body, [&](const Var& var) {
return vmap.Get(var); });
}
Stmt new_body = this->VisitStmt(body);
if (value.same_as(op->value) && new_body.same_as(op->body)) {
return GetRef<Stmt>(op);
} else if (value.dtype() == op->var->dtype) {
auto n = CopyOnWrite(op);
n->value = std::move(value);
n->body = std::move(body);
return Stmt(n);
} else {
// We need to visit the body again to insert additional casts
return LetStmt(new_var, value, new_body, op->span);
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]