[GitHub] [incubator-tvm] hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for indexing variables

2020-03-21 Thread GitBox
hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for 
indexing variables
URL: https://github.com/apache/incubator-tvm/pull/5092#discussion_r396002858
 
 

 ##
 File path: src/tir/pass/narrow_datatype.cc
 ##
 @@ -0,0 +1,363 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file narrow_datatype.cc
+ * \brief narrow the datatype of indexing vars
+ */
+
+#include 
+#include 
+#include "../../arith/ir_mutator_with_analyzer.h"
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+// This pass narrows indexing expressions (like StoreNode::Index)
+// that trivially fit into i32 to i32. Considering that i32 indices
+// may be more efficient on some backends (while i64 may be more
+// efficient on others, like llvm), we may want this pass when i32
+// indices are more efficient.
+//
+// For Var v, we determine its dtype by examining all the PrimExpr
+// that contains v, denoted by E = {e_0 = v, e_1, e_2, ..., e_k}.
+// If all expressions in E fit into i32, then we think v can be narrowed
+// to i32.
+//
+// To make an indexing expression i32, we must make sure that every
+// component of that expression is of dtype i32. So besides Var, we
+// rewrite the following inside an indexing expression
+// - Var
+// - IntImm
+// - Cast
+//
+// Algorithm:
+// - Use DataTypeVisitor to determine whether a Var can be narrowed or not.
+// - Use DataTypeRewritter to rewrite the components of an indexing expression.
+
+using arith::Analyzer;
+using arith::IRMutatorWithAnalyzer;
+using arith::ConstIntBound;
+
+class DataTypeRewriter;
+
+class DataTypeVisitor final : public StmtExprVisitor {
+ public:
+  void VisitExpr(const PrimExpr& e) {
+if (e.dtype().is_int()) {
+  int bits = 64;
+  if (e.dtype().bits() <= 32 ||
+  analyzer_.CanProve(e <= max_value(DataType::Int(32)) &&
+ e >= min_value(DataType::Int(32 {
+bits = 32;
+  }
+  int tmp = bits_;
 
 Review comment:
   Fixed


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for indexing variables

2020-03-21 Thread GitBox
hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for 
indexing variables
URL: https://github.com/apache/incubator-tvm/pull/5092#discussion_r396002811
 
 

 ##
 File path: src/tir/pass/narrow_datatype.cc
 ##
 @@ -0,0 +1,363 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file narrow_datatype.cc
+ * \brief narrow the datatype of indexing vars
+ */
+
+#include 
+#include 
+#include "../../arith/ir_mutator_with_analyzer.h"
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+// This pass narrows indexing expressions (like StoreNode::Index)
+// that trivially fit into i32 to i32. Considering that i32 indices
+// may be more efficient on some backends (while i64 may be more
+// efficient on others, like llvm), we may want this pass when i32
+// indices are more efficient.
+//
+// For Var v, we determine its dtype by examining all the PrimExpr
+// that contains v, denoted by E = {e_0 = v, e_1, e_2, ..., e_k}.
+// If all expressions in E fit into i32, then we think v can be narrowed
+// to i32.
+//
+// To make an indexing expression i32, we must make sure that every
+// component of that expression is of dtype i32. So besides Var, we
+// rewrite the following inside an indexing expression
+// - Var
+// - IntImm
+// - Cast
+//
+// Algorithm:
+// - Use DataTypeVisitor to determine whether a Var can be narrowed or not.
+// - Use DataTypeRewritter to rewrite the components of an indexing expression.
+
+using arith::Analyzer;
+using arith::IRMutatorWithAnalyzer;
+using arith::ConstIntBound;
+
+class DataTypeRewriter;
+
+class DataTypeVisitor final : public StmtExprVisitor {
+ public:
+  void VisitExpr(const PrimExpr& e) {
+if (e.dtype().is_int()) {
+  int bits = 64;
+  if (e.dtype().bits() <= 32 ||
+  analyzer_.CanProve(e <= max_value(DataType::Int(32)) &&
 
 Review comment:
   Fixed.


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for indexing variables

2020-03-21 Thread GitBox
hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for 
indexing variables
URL: https://github.com/apache/incubator-tvm/pull/5092#discussion_r396002834
 
 

 ##
 File path: src/tir/pass/narrow_datatype.cc
 ##
 @@ -0,0 +1,363 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file narrow_datatype.cc
+ * \brief narrow the datatype of indexing vars
+ */
+
+#include 
+#include 
+#include "../../arith/ir_mutator_with_analyzer.h"
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+// This pass narrows indexing expressions (like StoreNode::Index)
+// that trivially fit into i32 to i32. Considering that i32 indices
+// may be more efficient on some backends (while i64 may be more
+// efficient on others, like llvm), we may want this pass when i32
+// indices are more efficient.
+//
+// For Var v, we determine its dtype by examining all the PrimExpr
+// that contains v, denoted by E = {e_0 = v, e_1, e_2, ..., e_k}.
+// If all expressions in E fit into i32, then we think v can be narrowed
+// to i32.
+//
+// To make an indexing expression i32, we must make sure that every
+// component of that expression is of dtype i32. So besides Var, we
+// rewrite the following inside an indexing expression
+// - Var
+// - IntImm
+// - Cast
+//
+// Algorithm:
+// - Use DataTypeVisitor to determine whether a Var can be narrowed or not.
+// - Use DataTypeRewritter to rewrite the components of an indexing expression.
+
+using arith::Analyzer;
+using arith::IRMutatorWithAnalyzer;
+using arith::ConstIntBound;
+
+class DataTypeRewriter;
+
+class DataTypeVisitor final : public StmtExprVisitor {
+ public:
+  void VisitExpr(const PrimExpr& e) {
+if (e.dtype().is_int()) {
+  int bits = 64;
+  if (e.dtype().bits() <= 32 ||
 
 Review comment:
   Added a parameter `target_bits_`


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for indexing variables

2020-03-20 Thread GitBox
hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for 
indexing variables
URL: https://github.com/apache/incubator-tvm/pull/5092#discussion_r395732407
 
 

 ##
 File path: src/tir/pass/rewrite_datatype.cc
 ##
 @@ -0,0 +1,322 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file rewrite_datatype.cc
 
 Review comment:
   Agree. I added a brief description about the motivation and how the pass 
works.


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for indexing variables

2020-03-20 Thread GitBox
hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for 
indexing variables
URL: https://github.com/apache/incubator-tvm/pull/5092#discussion_r395731195
 
 

 ##
 File path: include/tvm/tir/ir_pass.h
 ##
 @@ -385,6 +385,13 @@ Stmt DecorateDeviceScope(Stmt stmt);
  */
 Stmt HoistIfThenElse(Stmt stmt);
 
+/*!
+ * \brief Narrow down PrimExpr datatype in stmt
+ * \param stmt The stmt to do datatype rewrite
+ * \return Transformed stmt.
+ */
+Stmt DataTypeRewrite(Stmt stmt);
 
 Review comment:
   Renamed.


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for indexing variables

2020-03-20 Thread GitBox
hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for 
indexing variables
URL: https://github.com/apache/incubator-tvm/pull/5092#discussion_r395721838
 
 

 ##
 File path: src/tir/pass/unroll_loop.cc
 ##
 @@ -160,7 +160,9 @@ class LoopUnroller : public StmtExprMutator {
 PrimExpr extent = tir::Simplify(op->extent);
 const IntImmNode  *v1 = extent.as();
 int value = -1;
-if (v1 != nullptr) {
+// integers that do not fit in int32_t are treated as symbolic,
+// as it's impossible to unroll such large loops
 
 Review comment:
   I'm not sure. A warning here may be raised for loops that are not marked as 
`unroll`. So every loop with a i64 extent raises the warning here, even when 
the user does not intend to unroll it.


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for indexing variables

2020-03-20 Thread GitBox
hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for 
indexing variables
URL: https://github.com/apache/incubator-tvm/pull/5092#discussion_r395701694
 
 

 ##
 File path: src/tir/pass/rewrite_datatype.cc
 ##
 @@ -0,0 +1,326 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file rewrite_datatype.cc
+ * \brief narrow the datatype of indexing vars
+ */
+
+#include 
+#include 
+#include "../../arith/ir_mutator_with_analyzer.h"
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+using arith::Analyzer;
+using arith::IRMutatorWithAnalyzer;
+using arith::ConstIntBound;
+
+class DataTypeRewriter;
+
+class DataTypeVisitor final : public StmtExprVisitor {
+ public:
+  void VisitExpr(const PrimExpr& e) {
+if (e.dtype().is_int()) {
+  int bits = 64;
+  if (e.dtype().bits() <= 32 ||
+  analyzer_.CanProve(e <= max_value(DataType::Int(32)) &&
+ e >= min_value(DataType::Int(32 {
+bits = 32;
+  }
+  int tmp = bits_;
+  bits_ = bits > bits_ ? bits :  bits_;
+  StmtExprVisitor::VisitExpr(e);
+  bits_ = tmp;
+} else {
+  StmtExprVisitor::VisitExpr(e);
+}
+  }
+
+  void VisitStmt_(const ForNode* op) {
+analyzer_.Bind(op->loop_var,
+   Range::make_by_min_extent(op->min, op->extent));
+vextent_[op->loop_var.as()] = op->extent.dtype();
+return StmtExprVisitor::VisitStmt_(op);
+  }
+
+  void VisitStmt_(const AttrStmtNode* op) {
+if (op->attr_key == attr::thread_extent ||
+op->attr_key == attr::virtual_thread) {
+  IterVar iv = Downcast(op->node);
+  CHECK_NE(iv->thread_tag.length(), 0U);
+  analyzer_.Bind(iv->var,
+  Range::make_by_min_extent(0, op->value));
+  vextent_[iv->var.as()] = op->value.dtype();
+  StmtExprVisitor::VisitStmt_(op);
+} else {
+  StmtExprVisitor::VisitStmt_(op);
+}
+  }
+
+  void VisitExpr_(const ReduceNode* op) {
+// Setup the domain information before simplification.
+for (const IterVar& iv : op->axis) {
+  analyzer_.Bind(iv->var, iv->dom);
+  vextent_[iv->var.as()] = iv->dom->extent.dtype();
+}
+// Recursively call simplification when necessary.
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  void VisitExpr_(const VarNode* op) {
+if (vextent_.find(op) != vextent_.end()) {
+  int bits = std::min(vextent_[op].bits(), bits_);
+  if (vmap.find(op) == vmap.end()) {
+vmap[op] = op->dtype.with_bits(bits);
+  } else {
+vmap[op] = op->dtype.with_bits(std::max(vmap[op].bits(), bits));
+  }
+}
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  void VisitExpr_(const IntImmNode* op) {
+if (op->dtype.is_int()) {
+  int bits = std::min(op->dtype.bits(), bits_);
+  if (vmap.find(op) == vmap.end()) {
+vmap[op] = op->dtype.with_bits(bits);
+  } else {
+vmap[op] = op->dtype.with_bits(std::max(vmap[op].bits(), bits));
+  }
+}
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  void VisitExpr_(const CastNode* op) {
+if (op->dtype.is_int()) {
+  int bits = std::min(op->dtype.bits(), bits_);
+  if (vmap.find(op) == vmap.end()) {
+vmap[op] = op->dtype.with_bits(bits);
+  } else {
+vmap[op] = op->dtype.with_bits(std::max(vmap[op].bits(), bits));
+  }
+}
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  // the narrowed datatype of Var and IntImm
+  std::unordered_map vmap;
+
+ protected:
+  // internal analyzer
+  arith::Analyzer analyzer_;
+
+ private:
+  // the maximum possible bit of the current expression's return dtype
+  int bits_;
+  // the extent of vars to be rewritten
+  std::unordered_map vextent_;
+};
+
+class DataTypeRewriter : public StmtExprMutator {
+ public:
+  Stmt operator()(Stmt s) {
+visitor_(s);
+for (auto i = visitor_.vmap.begin(), last = visitor_.vmap.end(); i != 
last;) {
+  PrimExpr e = GetRef(i->first);
+  if (e.dtype() == i->second) {
+i = visitor_.vmap.erase(i);
+  } else {
+++i;
+  }
+}
+return VisitStmt(s);
+  }
+
+  Stmt VisitStmt_(const StoreNode* op) final {
+PrimExpr value = 

[GitHub] [incubator-tvm] hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for indexing variables

2020-03-19 Thread GitBox
hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for 
indexing variables
URL: https://github.com/apache/incubator-tvm/pull/5092#discussion_r395156871
 
 

 ##
 File path: src/tir/pass/rewrite_datatype.cc
 ##
 @@ -0,0 +1,322 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file rewrite_datatype.cc
 
 Review comment:
   Added a term `\brief`


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for indexing variables

2020-03-19 Thread GitBox
hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for 
indexing variables
URL: https://github.com/apache/incubator-tvm/pull/5092#discussion_r395155823
 
 

 ##
 File path: src/tir/pass/rewrite_datatype.cc
 ##
 @@ -0,0 +1,322 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file rewrite_datatype.cc
+ */
+
+#include 
+#include 
+#include "../../arith/ir_mutator_with_analyzer.h"
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+using arith::Analyzer;
+using arith::IRMutatorWithAnalyzer;
+using arith::ConstIntBound;
+
+class DataTypeRewriter;
+
+class DataTypeVisitor final : public StmtExprVisitor {
+ public:
+  void VisitExpr(const PrimExpr& e) {
+if (e.dtype().is_int()) {
+  int bits = 64;
+  if (e.dtype().bits() <= 32 ||
+  analyzer_.CanProve(e <= max_value(DataType::Int(32)) &&
+ e >= min_value(DataType::Int(32 {
+bits = 32;
+  }
+  int tmp = bits_;
+  bits_ = bits > bits_ ? bits :  bits_;
+  StmtExprVisitor::VisitExpr(e);
+  bits_ = tmp;
+} else {
+  StmtExprVisitor::VisitExpr(e);
+}
+  }
+
+  void VisitStmt_(const ForNode* op) {
+analyzer_.Bind(op->loop_var,
+   Range::make_by_min_extent(op->min, op->extent));
+vset_.insert(op->loop_var.as());
+vextent_[op->loop_var.as()] = op->extent.dtype();
+return StmtExprVisitor::VisitStmt_(op);
+  }
+
+  void VisitStmt_(const AttrStmtNode* op) {
+if (op->attr_key == attr::thread_extent ||
+op->attr_key == attr::virtual_thread) {
+  IterVar iv = Downcast(op->node);
+  CHECK_NE(iv->thread_tag.length(), 0U);
+  analyzer_.Bind(iv->var,
+  Range::make_by_min_extent(0, op->value));
+  vset_.insert(iv->var.as());
+  vextent_[iv->var.as()] = op->value.dtype();
+  StmtExprVisitor::VisitStmt_(op);
+} else {
+  StmtExprVisitor::VisitStmt_(op);
+}
+  }
+
+  void VisitExpr_(const ReduceNode* op) {
+// Setup the domain information before simplification.
+for (const IterVar& iv : op->axis) {
+  analyzer_.Bind(iv->var, iv->dom);
+  vset_.insert(iv->var.as());
+  vextent_[iv->var.as()] = iv->dom->extent.dtype();
+}
+// Recursively call simplification when necessary.
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  void VisitExpr_(const VarNode* op) {
+if (vset_.find(op) != vset_.end()) {
+  int bits = std::min(vextent_[op].bits(), bits_);
+  if (vmap.find(op) == vmap.end()) {
+vmap[op] = op->dtype.with_bits(bits);
+  } else {
+vmap[op] = op->dtype.with_bits(std::max(vmap[op].bits(), bits));
+  }
+}
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  void VisitExpr_(const IntImmNode* op) {
+if (op->dtype.is_int()) {
+  int bits = std::min(op->dtype.bits(), bits_);
+  if (vmap.find(op) == vmap.end()) {
+vmap[op] = op->dtype.with_bits(bits);
+  } else {
+vmap[op] = op->dtype.with_bits(std::max(vmap[op].bits(), bits));
+  }
+}
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  void VisitExpr_(const CastNode* op) {
+if (op->dtype.is_int()) {
+  int bits = std::min(op->dtype.bits(), bits_);
+  if (vmap.find(op) == vmap.end()) {
+vmap[op] = op->dtype.with_bits(bits);
+  } else {
+vmap[op] = op->dtype.with_bits(std::max(vmap[op].bits(), bits));
+  }
+}
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  // the narrowed datatype of Var and IntImm
+  std::unordered_map vmap;
+
+ protected:
+  // internal analyzer
+  arith::Analyzer analyzer_;
+
+ private:
+  // the maximum bits of all containing expressions
 
 Review comment:
   Yes. I changed the comments accordingly.


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for indexing variables

2020-03-19 Thread GitBox
hzfan commented on a change in pull request #5092: [PASS] dtype rewrite for 
indexing variables
URL: https://github.com/apache/incubator-tvm/pull/5092#discussion_r395155241
 
 

 ##
 File path: src/tir/pass/rewrite_datatype.cc
 ##
 @@ -0,0 +1,322 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file rewrite_datatype.cc
+ */
+
+#include 
+#include 
+#include "../../arith/ir_mutator_with_analyzer.h"
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+using arith::Analyzer;
+using arith::IRMutatorWithAnalyzer;
+using arith::ConstIntBound;
+
+class DataTypeRewriter;
+
+class DataTypeVisitor final : public StmtExprVisitor {
+ public:
+  void VisitExpr(const PrimExpr& e) {
+if (e.dtype().is_int()) {
+  int bits = 64;
+  if (e.dtype().bits() <= 32 ||
+  analyzer_.CanProve(e <= max_value(DataType::Int(32)) &&
+ e >= min_value(DataType::Int(32 {
+bits = 32;
+  }
+  int tmp = bits_;
+  bits_ = bits > bits_ ? bits :  bits_;
+  StmtExprVisitor::VisitExpr(e);
+  bits_ = tmp;
+} else {
+  StmtExprVisitor::VisitExpr(e);
+}
+  }
+
+  void VisitStmt_(const ForNode* op) {
+analyzer_.Bind(op->loop_var,
+   Range::make_by_min_extent(op->min, op->extent));
+vset_.insert(op->loop_var.as());
+vextent_[op->loop_var.as()] = op->extent.dtype();
+return StmtExprVisitor::VisitStmt_(op);
+  }
+
+  void VisitStmt_(const AttrStmtNode* op) {
+if (op->attr_key == attr::thread_extent ||
+op->attr_key == attr::virtual_thread) {
+  IterVar iv = Downcast(op->node);
+  CHECK_NE(iv->thread_tag.length(), 0U);
+  analyzer_.Bind(iv->var,
+  Range::make_by_min_extent(0, op->value));
+  vset_.insert(iv->var.as());
+  vextent_[iv->var.as()] = op->value.dtype();
+  StmtExprVisitor::VisitStmt_(op);
+} else {
+  StmtExprVisitor::VisitStmt_(op);
+}
+  }
+
+  void VisitExpr_(const ReduceNode* op) {
+// Setup the domain information before simplification.
+for (const IterVar& iv : op->axis) {
+  analyzer_.Bind(iv->var, iv->dom);
+  vset_.insert(iv->var.as());
+  vextent_[iv->var.as()] = iv->dom->extent.dtype();
+}
+// Recursively call simplification when necessary.
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  void VisitExpr_(const VarNode* op) {
+if (vset_.find(op) != vset_.end()) {
+  int bits = std::min(vextent_[op].bits(), bits_);
+  if (vmap.find(op) == vmap.end()) {
+vmap[op] = op->dtype.with_bits(bits);
+  } else {
+vmap[op] = op->dtype.with_bits(std::max(vmap[op].bits(), bits));
+  }
+}
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  void VisitExpr_(const IntImmNode* op) {
+if (op->dtype.is_int()) {
+  int bits = std::min(op->dtype.bits(), bits_);
+  if (vmap.find(op) == vmap.end()) {
+vmap[op] = op->dtype.with_bits(bits);
+  } else {
+vmap[op] = op->dtype.with_bits(std::max(vmap[op].bits(), bits));
+  }
+}
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  void VisitExpr_(const CastNode* op) {
+if (op->dtype.is_int()) {
+  int bits = std::min(op->dtype.bits(), bits_);
+  if (vmap.find(op) == vmap.end()) {
+vmap[op] = op->dtype.with_bits(bits);
+  } else {
+vmap[op] = op->dtype.with_bits(std::max(vmap[op].bits(), bits));
+  }
+}
+StmtExprVisitor::VisitExpr_(op);
+  }
+
+  // the narrowed datatype of Var and IntImm
+  std::unordered_map vmap;
+
+ protected:
+  // internal analyzer
+  arith::Analyzer analyzer_;
+
+ private:
+  // the maximum bits of all containing expressions
+  int bits_;
+  // the vars to be rewritten
+  std::unordered_set vset_;
 
 Review comment:
   Agree. `vset_` removed


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services