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/07/04 22:58:58 UTC

[GitHub] [tvm] ArmageddonKnight commented on a diff in pull request #11793: [DietCode] Local Padding

ArmageddonKnight commented on code in PR #11793:
URL: https://github.com/apache/tvm/pull/11793#discussion_r913288805


##########
src/tir/transforms/local_pad.cc:
##########
@@ -0,0 +1,302 @@
+/*
+ * 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.
+ */
+
+#include <tvm/meta_schedule/postproc.h>
+#include <tvm/tir/op.h>
+#include <tvm/tir/stmt.h>
+#include <tvm/tir/stmt_functor.h>
+#include <tvm/tir/transform.h>
+
+#include <array>
+#include <utility>
+#include <vector>
+
+namespace tvm {
+namespace tir {
+namespace transform {
+namespace {
+
+/*!
+ * \brief Analyze the read and write accesses of the body statements, used by `LocalPadder`.
+ */
+class StorageAccessAnalyzer : public StmtExprVisitor {
+ private:
+  struct StorageType {
+    enum { kGlobal = 0, kShared, kLocal, kOthers };
+  };
+
+  void VisitStmt_(const BufferStoreNode* op) final {
+    write_marker_.SetStorageAccessMarker_(op->buffer);
+    StmtExprVisitor::VisitStmt_(op);
+  }
+  void VisitExpr_(const BufferLoadNode* op) final {
+    read_marker_.SetStorageAccessMarker_(op->buffer);
+    StmtExprVisitor::VisitExpr_(op);
+  }
+  class AccessMarker {
+   public:
+    void SetStorageAccessMarker_(const Buffer& buf) {
+      if (buf.scope() == "global") {
+        bit_vector_[StorageType::kGlobal] = true;
+      } else if (buf.scope() == "shared") {
+        bit_vector_[StorageType::kShared] = true;
+      } else if (buf.scope() == "local") {
+        bit_vector_[StorageType::kLocal] = true;
+      } else {
+        bit_vector_[StorageType::kOthers] = true;
+      }
+    }
+    bool NoAccesses() const {
+      return !(bit_vector_[StorageType::kGlobal] || bit_vector_[StorageType::kShared] ||
+               bit_vector_[StorageType::kLocal] || bit_vector_[StorageType::kOthers]);
+    }
+    bool OnlyGlobalAccesses() const {
+      return !(bit_vector_[StorageType::kShared] || bit_vector_[StorageType::kLocal] ||
+               bit_vector_[StorageType::kOthers]) &&
+             bit_vector_[StorageType::kGlobal];
+    }
+    bool OnlyLocalOrSharedAccesses() const {
+      return !(bit_vector_[StorageType::kGlobal] || bit_vector_[StorageType::kOthers]) &&
+             (bit_vector_[StorageType::kShared] || bit_vector_[StorageType::kLocal]);
+    }
+
+   private:
+    std::array<bool, StorageType::kOthers + 1> bit_vector_ = {false};
+  };
+  AccessMarker read_marker_, write_marker_;
+  std::pair<AccessMarker, AccessMarker> Analyze_(const Stmt& stmt) {
+    VisitStmt(stmt);
+    return std::make_pair(read_marker_, write_marker_);
+  }
+
+  friend class LocalPadder;
+};
+
+/*!
+ * \brief Verify that all local variables are initialized to the same constant expression.
+ */
+class InitChecker : public StmtVisitor {
+ private:
+  void VisitStmt_(const BufferStoreNode* op) final {
+    // Read the check the RHS values, make sure that they are the same constant for all the
+    // initialization statements.
+    CheckInitValue_<IntImmNode>(op->value);
+    CheckInitValue_<FloatImmNode>(op->value);
+    return StmtVisitor::VisitStmt_(op);
+  }
+  template <typename ImmNodeType>
+  void CheckInitValue_(const PrimExpr& rhs) {
+    if (const ImmNodeType* const rhs_val = rhs.as<ImmNodeType>()) {
+      if (init_constexpr_.defined()) {
+        if (const ImmNodeType* const init_val = init_constexpr_.as<ImmNodeType>()) {
+          if (rhs_val->value != init_val->value) {
+            init_with_single_constexpr_ = false;
+          }
+        } else {
+          init_with_single_constexpr_ = false;
+        }
+      } else {
+        init_with_single_constexpr_ = true;
+        init_constexpr_ = rhs;
+      }
+    }
+  }
+  void operator()(const Stmt& stmt) {
+    StmtVisitor::operator()(stmt);
+    if (!init_with_single_constexpr_) {
+      init_constexpr_ = PrimExpr();
+    }
+  }
+
+  bool init_with_single_constexpr_ = false;
+  PrimExpr init_constexpr_;
+
+  friend class LocalPadder;
+};
+
+/*!
+ * \brief Split a predicate into inlinable and non-inlinable component.
+ *
+ *        We refer to "inlinable predicate" as
+ *
+ *            if (predicate) A = ...;
+ *            ↓
+ *            A = predicate ? ... : init_constexpr;
+ *
+ *        Note that not all predicates can be inlined. For example, if a predicate is there to guard
+ *        against out-of-boundary accesses to local/shared variables, then it cannot be inlined.
+ */
+class PredicateInliner : public StmtExprVisitor {
+ private:
+  explicit PredicateInliner(const Stmt& body_stmt) : body_stmt_(body_stmt) {}
+
+#define VISIT_PREDICATE(OpType)                      \
+  void VisitExpr_(const OpType##Node* op) final {    \
+    OpType predicate = GetRef<OpType>(op);           \
+    if (CanInlinePredicate_<OpType##Node>(op)) {     \
+      inlinable_predicates_.push_back(predicate);    \
+    } else {                                         \
+      non_inlinable_residuals_.push_back(predicate); \
+    }                                                \
+  }
+  VISIT_PREDICATE(LT)
+  VISIT_PREDICATE(LE)
+  VISIT_PREDICATE(GT)
+  VISIT_PREDICATE(GE)
+#undef VISIT_PREDICATE
+
+  void VisitStmt_(const BufferStoreNode* op) final {
+    if (op->indices.size() != 1) {
+      return StmtVisitor::VisitStmt_(op);
+    }
+    CHECK(op->buffer.scope() == "shared" || op->buffer.scope() == "local");
+    if (StructuralEqual()(op->indices[0], predicate_lhs_)) {
+      predicate_inlinable_ = false;
+    }

Review Comment:
   Yes.



-- 
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