wrongtest-intellif commented on code in PR #12266:
URL: https://github.com/apache/tvm/pull/12266#discussion_r936538582


##########
src/tir/transforms/remove_store_undef.cc:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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 remove_store_undef.cc
+ * \brief Remove stores of tir::builtin::undef
+ */
+#include <tvm/runtime/registry.h>
+#include <tvm/tir/analysis.h>
+#include <tvm/tir/builtin.h>
+#include <tvm/tir/op.h>
+#include <tvm/tir/stmt.h>
+#include <tvm/tir/stmt_functor.h>
+#include <tvm/tir/transform.h>
+
+namespace tvm {
+namespace tir {
+
+class StoreUndefLocator : public StmtExprVisitor {
+ public:
+  static std::unordered_set<const BufferStoreNode*> Locate(Stmt stmt) {
+    StoreUndefLocator locator;
+    locator(std::move(stmt));
+    return locator.undef_stores_;
+  }
+
+ private:
+  StoreUndefLocator() = default;
+
+  void VisitStmt_(const BufferStoreNode* op) final {
+    bool stash_undef = false;
+    std::swap(has_undef_, stash_undef);
+    StmtExprVisitor::VisitExpr(op->value);

Review Comment:
   It is more safe if we check effect of value <= `kReadState`? Of course it 
should be ok if we plan to only inject it as described by the RFC.



##########
src/tir/transforms/remove_store_undef.cc:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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 remove_store_undef.cc
+ * \brief Remove stores of tir::builtin::undef
+ */
+#include <tvm/runtime/registry.h>
+#include <tvm/tir/analysis.h>
+#include <tvm/tir/builtin.h>
+#include <tvm/tir/op.h>
+#include <tvm/tir/stmt.h>
+#include <tvm/tir/stmt_functor.h>
+#include <tvm/tir/transform.h>
+
+namespace tvm {
+namespace tir {
+
+class StoreUndefLocator : public StmtExprVisitor {
+ public:
+  static std::unordered_set<const BufferStoreNode*> Locate(Stmt stmt) {
+    StoreUndefLocator locator;
+    locator(std::move(stmt));
+    return locator.undef_stores_;
+  }
+
+ private:
+  StoreUndefLocator() = default;
+
+  void VisitStmt_(const BufferStoreNode* op) final {
+    bool stash_undef = false;
+    std::swap(has_undef_, stash_undef);
+    StmtExprVisitor::VisitExpr(op->value);
+    std::swap(has_undef_, stash_undef);
+    if (stash_undef) {
+      undef_stores_.insert(op);
+    }
+  }
+
+  void VisitExpr_(const BufferLoadNode* op) final {
+    // This function left deliberately empty.  builtin::undef()
+    // shouldn't occur in the indices of BufferLoad.  Avoiding
+    // visiting the indices catches the builtin::undef in
+    // ValidateAllUndefRemoved.
+  }
+
+  void VisitStmt_(const LetStmtNode* op) final {
+    bool stash_undef = false;
+    std::swap(has_undef_, stash_undef);
+    StmtExprVisitor::VisitExpr(op->value);
+    std::swap(has_undef_, stash_undef);
+    if (stash_undef) {
+      var_bindings_with_undef_.insert(op->var.get());
+    }
+
+    StmtExprVisitor::VisitStmt(op->body);
+  }
+
+  void VisitExpr_(const VarNode* op) final {
+    if (var_bindings_with_undef_.count(op)) {
+      has_undef_ = true;
+    }
+  }
+
+  void VisitExpr_(const CallNode* op) final {
+    if (op->op.same_as(builtin::undef())) {
+      has_undef_ = true;
+    }
+    StmtExprVisitor::VisitExpr_(op);
+  }
+
+  bool has_undef_{false};
+
+  std::unordered_set<const VarNode*> var_bindings_with_undef_;
+  std::unordered_set<const BufferStoreNode*> undef_stores_;
+};
+
+// Remove any BufferStores whose value depends on T.undef
+class StoreUndefRemover : public StmtExprMutator {
+ public:
+  static Stmt Apply(Stmt stmt) {
+    auto to_remove = StoreUndefLocator::Locate(stmt);
+    StoreUndefRemover mutator(std::move(to_remove));
+    return mutator(std::move(stmt));
+  }
+
+ private:
+  using Parent = StmtExprMutator;
+
+  explicit StoreUndefRemover(std::unordered_set<const BufferStoreNode*> 
to_remove)
+      : to_remove_(to_remove) {}
+
+  Stmt VisitStmt_(const BufferStoreNode* op) final {
+    if (to_remove_.count(op)) {
+      return Evaluate(0);

Review Comment:
   could it be `std::move(Evaluate(0))` ? or else maybe cause a compile warning 
on clang "Returning an instance of a subclass of the return type disable copy 
elision"



##########
src/tir/transforms/remove_store_undef.cc:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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 remove_store_undef.cc
+ * \brief Remove stores of tir::builtin::undef
+ */
+#include <tvm/runtime/registry.h>
+#include <tvm/tir/analysis.h>
+#include <tvm/tir/builtin.h>
+#include <tvm/tir/op.h>
+#include <tvm/tir/stmt.h>
+#include <tvm/tir/stmt_functor.h>
+#include <tvm/tir/transform.h>
+
+namespace tvm {
+namespace tir {
+
+class StoreUndefLocator : public StmtExprVisitor {
+ public:
+  static std::unordered_set<const BufferStoreNode*> Locate(Stmt stmt) {
+    StoreUndefLocator locator;
+    locator(std::move(stmt));
+    return locator.undef_stores_;
+  }
+
+ private:
+  StoreUndefLocator() = default;
+
+  void VisitStmt_(const BufferStoreNode* op) final {
+    bool stash_undef = false;
+    std::swap(has_undef_, stash_undef);
+    StmtExprVisitor::VisitExpr(op->value);
+    std::swap(has_undef_, stash_undef);
+    if (stash_undef) {
+      undef_stores_.insert(op);
+    }
+  }
+
+  void VisitExpr_(const BufferLoadNode* op) final {
+    // This function left deliberately empty.  builtin::undef()
+    // shouldn't occur in the indices of BufferLoad.  Avoiding
+    // visiting the indices catches the builtin::undef in
+    // ValidateAllUndefRemoved.
+  }
+
+  void VisitStmt_(const LetStmtNode* op) final {
+    bool stash_undef = false;
+    std::swap(has_undef_, stash_undef);
+    StmtExprVisitor::VisitExpr(op->value);
+    std::swap(has_undef_, stash_undef);
+    if (stash_undef) {
+      var_bindings_with_undef_.insert(op->var.get());
+    }
+
+    StmtExprVisitor::VisitStmt(op->body);
+  }
+
+  void VisitExpr_(const VarNode* op) final {
+    if (var_bindings_with_undef_.count(op)) {
+      has_undef_ = true;
+    }
+  }
+
+  void VisitExpr_(const CallNode* op) final {
+    if (op->op.same_as(builtin::undef())) {
+      has_undef_ = true;
+    }
+    StmtExprVisitor::VisitExpr_(op);
+  }
+
+  bool has_undef_{false};
+
+  std::unordered_set<const VarNode*> var_bindings_with_undef_;
+  std::unordered_set<const BufferStoreNode*> undef_stores_;
+};
+
+// Remove any BufferStores whose value depends on T.undef
+class StoreUndefRemover : public StmtExprMutator {
+ public:
+  static Stmt Apply(Stmt stmt) {
+    auto to_remove = StoreUndefLocator::Locate(stmt);
+    StoreUndefRemover mutator(std::move(to_remove));
+    return mutator(std::move(stmt));
+  }
+
+ private:
+  using Parent = StmtExprMutator;
+
+  explicit StoreUndefRemover(std::unordered_set<const BufferStoreNode*> 
to_remove)

Review Comment:
   `to_remove` and `to_remove_` may be a const reference since we do not mutate 
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.

To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to