You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/08/01 21:24:48 UTC

[GitHub] [tvm] Lunderberg opened a new pull request, #12266: [TIR] Add tir::builtin::undef

Lunderberg opened a new pull request, #12266:
URL: https://github.com/apache/tvm/pull/12266

   This PR introduces `tir::builtin::undef`, which is used to represent values that are an arbitrary value of a known datatype, along with a transformation to remove instances of `tir::builtin::undef`.
   
   This PR is part of the handling of padded buffer layout transformations ([tracking issue](https://github.com/apache/tvm/issues/12261), [rfc](https://github.com/apache/tvm-rfcs/pull/77)).


-- 
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-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] Lunderberg commented on pull request #12266: [TIR] Add tir::builtin::undef

Posted by GitBox <gi...@apache.org>.
Lunderberg commented on PR #12266:
URL: https://github.com/apache/tvm/pull/12266#issuecomment-1201738983

   This is currently a draft PR, as the unit tests depend on functionality introduced in #12264.


-- 
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-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] Lunderberg commented on a diff in pull request #12266: [TIR] Add tir::builtin::undef

Posted by GitBox <gi...@apache.org>.
Lunderberg commented on code in PR #12266:
URL: https://github.com/apache/tvm/pull/12266#discussion_r938946381


##########
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:
   Good point, and updated.  The private constructor is only ever called from one location, which uses `std::move`, but I like the const reference to tell the reader that it won't be further modified.



-- 
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-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] wrongtest-intellif commented on a diff in pull request #12266: [TIR] Add tir::builtin::undef

Posted by GitBox <gi...@apache.org>.
wrongtest-intellif commented on code in PR #12266:
URL: https://github.com/apache/tvm/pull/12266#discussion_r935022859


##########
src/tir/op/builtin.cc:
##########
@@ -288,6 +288,10 @@ TIR_DEFINE_BUILTIN_FUNC(texture2d_load)
 TIR_DEFINE_BUILTIN_FUNC(mem_copy).set_attr<TCallEffectKind>("TCallEffectKind",
                                                             Integer(CallEffectKind::kOpaque));
 
+TIR_DEFINE_BUILTIN_FUNC(undef)
+    .set_attr<TCallEffectKind>("TCallEffectKind", Integer(CallEffectKind::kReadState))

Review Comment:
   Why the undef's effect is `kReadState` than `kPure`?



-- 
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-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] wrongtest-intellif merged pull request #12266: [TIR] Add tir::builtin::undef

Posted by GitBox <gi...@apache.org>.
wrongtest-intellif merged PR #12266:
URL: https://github.com/apache/tvm/pull/12266


-- 
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-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] wrongtest-intellif commented on a diff in pull request #12266: [TIR] Add tir::builtin::undef

Posted by GitBox <gi...@apache.org>.
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-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] Lunderberg commented on a diff in pull request #12266: [TIR] Add tir::builtin::undef

Posted by GitBox <gi...@apache.org>.
Lunderberg commented on code in PR #12266:
URL: https://github.com/apache/tvm/pull/12266#discussion_r935648157


##########
src/tir/op/builtin.cc:
##########
@@ -288,6 +288,10 @@ TIR_DEFINE_BUILTIN_FUNC(texture2d_load)
 TIR_DEFINE_BUILTIN_FUNC(mem_copy).set_attr<TCallEffectKind>("TCallEffectKind",
                                                             Integer(CallEffectKind::kOpaque));
 
+TIR_DEFINE_BUILTIN_FUNC(undef)
+    .set_attr<TCallEffectKind>("TCallEffectKind", Integer(CallEffectKind::kReadState))

Review Comment:
   Primarily to avoid simplifications that rely on a pure function returning a consistent value for consistent inputs.  Since `undef` takes no arguments, this would imply that the return value of `undef` is always identical, which could result in incorrect simplifications.  For example, inlining of a let binding is valid if the bound value is `kPure`, but requires additional checks if the bound value is `kReadState`.
   
   I had picked `kReadState` as analogous to a hypothetical `builtin::system_clock_time`, which takes no arguments, but reads from a hidden state and may return a different value for each call.



-- 
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-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] Lunderberg commented on a diff in pull request #12266: [TIR] Add tir::builtin::undef

Posted by GitBox <gi...@apache.org>.
Lunderberg commented on code in PR #12266:
URL: https://github.com/apache/tvm/pull/12266#discussion_r938953546


##########
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:
   Good point.  I had avoided it to avoid re-walking the tree after looking for the `T.undef()`, but safety is the better option here.  Updated both here and in the `LetStmtNode` handler to verify that the expression has no other side effects.



-- 
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-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] Lunderberg commented on a diff in pull request #12266: [TIR] Add tir::builtin::undef

Posted by GitBox <gi...@apache.org>.
Lunderberg commented on code in PR #12266:
URL: https://github.com/apache/tvm/pull/12266#discussion_r938946381


##########
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:
   Good point, and updated.  The private constructor is only ever called from one location, which uses `std::move`, but I like the const reference to tell the reader that `to_remove_` won't be further modified after initial collection.



-- 
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-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] Lunderberg commented on pull request #12266: [TIR] Add tir::builtin::undef

Posted by GitBox <gi...@apache.org>.
Lunderberg commented on PR #12266:
URL: https://github.com/apache/tvm/pull/12266#issuecomment-1206609390

   Rebased onto main to resolve conflict with https://github.com/apache/tvm/pull/12267.


-- 
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-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] Lunderberg commented on a diff in pull request #12266: [TIR] Add tir::builtin::undef

Posted by GitBox <gi...@apache.org>.
Lunderberg commented on code in PR #12266:
URL: https://github.com/apache/tvm/pull/12266#discussion_r938949185


##########
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:
   I don't think it's necessary here, because the `Evaluate(0)` is already an rvalue reference and `std::move` would only be a cast to rvalue reference.  If I remember correctly, that warning occurs when an lvalue reference to a subclass is returned, and wouldn't occur here.



-- 
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-unsubscribe@tvm.apache.org

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