tqchen commented on a change in pull request #9533:
URL: https://github.com/apache/tvm/pull/9533#discussion_r752720591



##########
File path: src/relay/ir/expr.cc
##########
@@ -76,6 +76,26 @@ TVM_REGISTER_NODE_TYPE(TupleNode);
 
TVM_REGISTER_GLOBAL("relay.ir.Tuple").set_body_typed([](tvm::Array<relay::Expr> 
fields, Span span) {
   return Tuple(fields, span);
 });
+Tuple WithFields(Tuple tuple, Optional<Array<Expr>> opt_fields, Optional<Span> 
opt_span) {
+  Array<Expr> fields = opt_fields.value_or(tuple->fields);
+  Span span = opt_span.value_or(tuple->span);
+
+  bool all_fields_unchanged = true;
+  if (fields.size() == tuple->fields.size()) {
+    for (uint i = 0; i < fields.size(); i++) {
+      all_fields_unchanged &= fields[i] == (tuple->fields[i]);
+    }
+  } else {
+    all_fields_unchanged = false;
+  }
+
+  all_fields_unchanged = all_fields_unchanged && span == tuple->span;
+  if (all_fields_unchanged) {
+    return std::move(tuple);
+  } else {
+    return Tuple(std::move(fields), std::move(span));

Review comment:
       it is useful to actually invoke COW here. 
https://github.com/apache/tvm/blob/a6c948ac642534ade6f3625201bd30d6a8da71e4/include/tvm/ir/attrs.h#L374
 Again a very sutble thing.
   
   If the tuple is moved in and a single copy, it is actually useful to 
directly reuse that part of the memory. This will allow calls like below to 
only create one copy instead of two
   
   ```
   x = Tuple(fields, span);
   x = WithFields(std::move(x), new_fields, span)
   ```

##########
File path: src/relay/ir/expr.cc
##########
@@ -76,6 +76,26 @@ TVM_REGISTER_NODE_TYPE(TupleNode);
 
TVM_REGISTER_GLOBAL("relay.ir.Tuple").set_body_typed([](tvm::Array<relay::Expr> 
fields, Span span) {
   return Tuple(fields, span);
 });
+Tuple WithFields(Tuple tuple, Optional<Array<Expr>> opt_fields, Optional<Span> 
opt_span) {
+  Array<Expr> fields = opt_fields.value_or(tuple->fields);
+  Span span = opt_span.value_or(tuple->span);
+
+  bool all_fields_unchanged = true;
+  if (fields.size() == tuple->fields.size()) {
+    for (uint i = 0; i < fields.size(); i++) {
+      all_fields_unchanged &= fields[i] == (tuple->fields[i]);

Review comment:
       consider use same_as to avoid future overload of operator ==




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


Reply via email to