mbs-octoml commented on a change in pull request #8423:
URL: https://github.com/apache/tvm/pull/8423#discussion_r709299078



##########
File path: src/relay/backend/contrib/example_target_hooks/relay_to_tir.cc
##########
@@ -0,0 +1,131 @@
+
+/*
+ * 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/relay/expr_functor.h>
+#include <tvm/relay/transform.h>
+#include <tvm/tir/buffer.h>
+#include <tvm/tir/builtin.h>
+#include <tvm/tir/expr.h>
+#include <tvm/tir/function.h>
+#include <tvm/tir/op.h>
+
+namespace tvm {
+namespace relay {
+namespace contrib {
+namespace example_target_hooks {
+
+class ConvertAddToSubtract : public MixedModeMutator {
+ public:
+  explicit ConvertAddToSubtract(IRModule ir_module, Target host_target)
+      : ir_module_(ir_module), host_target_(host_target) {}
+
+  IRModule Mutate() {
+    GlobalVar main_global_var = ir_module_->GetGlobalVar("main");
+    BaseFunc main = ir_module_->Lookup(main_global_var);
+    Function main_func = GetRef<Function>(main.as<FunctionNode>());
+
+    // Copy everything across and mutate the body
+    Function mutated_main =
+        Function(main_func->params, VisitExpr(main_func->body), 
main_func->ret_type,
+                 main_func->type_params, main_func->attrs, main_func->span);
+
+    ir_module_->Update(main_global_var, mutated_main);
+
+    return ir_module_;
+  }
+
+ private:
+  tir::Load LoadIndex(const tir::Buffer& buffer, const PrimExpr& index) {
+    return tir::Load(DataType::Float(32), buffer->data, index, 
tir::const_true());
+  }
+
+  void ReplaceAddWithSubtractPrimFunc(const GlobalVar& new_global_var, const 
Function& func) {
+    tir::Buffer x_buffer = tir::decl_buffer({8}, DataType::Float(32), "x");
+    tir::Buffer y_buffer = tir::decl_buffer({8}, DataType::Float(32), "y");
+    tir::Buffer out_buffer = tir::decl_buffer({8}, DataType::Float(32));
+
+    tir::Var x_var("x", DataType::Handle());
+    tir::Var y_var("y", DataType::Handle());
+    tir::Var out_var("out", DataType::Handle());
+
+    Map<String, ObjectRef> dict_attrs;
+    dict_attrs.Set("global_symbol", new_global_var->name_hint);
+    dict_attrs.Set("tir.noalias", Bool(true));
+
+    te::Var index("index", DataType::Int(32));
+    tir::Sub indexed_sub = tir::Sub(LoadIndex(x_buffer, index), 
LoadIndex(y_buffer, index));
+    tir::Stmt math_body = tir::Store(out_buffer->data, indexed_sub, index, 
tir::const_true());
+    tir::Stmt math_loop = tir::For(index, 0, 8, tir::ForKind::kSerial, 
math_body);
+
+    Map<tir::Var, tir::Buffer> buffer_map = {
+        {x_var, x_buffer},
+        {y_var, y_buffer},
+        {out_var, out_buffer},
+    };
+
+    tir::PrimFunc replacement_func = tir::PrimFunc({x_var, y_var, out_var}, 
math_loop, VoidType(),

Review comment:
       I don't see it's necessary.
   Within a pass it's a few lines to ensure two calls to the same 
attrs::kPrimitive Function are rewritten to call the same PrimFunc. We could 
probably even use the memoization built into ExprMutator.
   Between passes there's nothing left to say -- it has all been encapsulated 
within the IRModule itself.
   Even if it were necessary for some peculiar reason then I'd turn the 
conversation to figuring out how to extend IRModule or attributes or whatever 
to again ensure there's no special state between passes other than what is 
spelled out in the IRModule.
   Does that make sense?
   
   BTW LGTM for this one exactly as is since I can see the caching issue has 
deserved more conversation who's outcome can easily go into a follow up. Thanks 
for pushing on the 'just a Pass' approach, it's so much better :-)




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