# TensorIR call prim func

**URL:** <https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209>\
**Category:** Troubleshooting\
**Created:** [March 2, 2022, 8:12am UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209 "2022-03-02T08:12:04Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maximilianxu](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/maximilianxu/32/988_2.png) [@Maximilianxu](https://discuss.tvm.apache.org/u/Maximilianxu)\
**Post date:** [March 2, 2022, 8:12am UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/1 "2022-03-02T08:12:04Z")

</div>

Hi, all, How do I call a prim\_func from another prim\_func in TensorIR?

```auto
@tvm.script.ir_module
class MyModule:
    @T.prim_func
    def main(a: T.handle, b: T.handle):
        # We exchange data between function by handles, which are similar to pointer.
        T.func_attr({"global_symbol": "main", "tir.noalias": True})
        # Create buffer from handles.
        A = T.match_buffer(a, (8,), dtype="float32")
        B = T.match_buffer(b, (8,), dtype="float32")
        for i in range(8):
            # A block is an abstraction for computation.
            with T.block("B"):
                # Define a spatial block iterator and bind it to value i.
                vi = T.axis.spatial(8, i)
                B[vi] = A[vi] + 1.0
        T.evaluate(T.call_extern("plus_one", B.data, dtype="float32"))
    
    @T.prim_func
    def plus_one(a: T.handle):
        T.func_attr({"global_symbol": "plus_one", "tir.noalias": True,})
        A = T.match_buffer(a, (8, 8), dtype="float32")
        for i, j in T.grid(8, 8):
            with T.block("plus"):
                vi, vj = T.axis.remap("SS", [i, j])
                A[vi, vj] = A[vi, vj] + 1.0

```

I tried to build the above module with “llvm” target, however, I got an error:

```auto
at /project/ai/scratch01/maxxu01/workspaces/AIPU_tvm_design/src/target/llvm/codegen_llvm.cc:137
  File "/project/ai/scratch01/maxxu01/workspaces/AIPU_tvm_design/src/target/llvm/codegen_llvm.cc", line 137
TVMError: 
---------------------------------------------------------------
An error occurred during the execution of TVM.
For more information, please see: https://tvm.apache.org/docs/errors.html
---------------------------------------------------------------
  Check failed: (module_->getFunction(static_cast<std::string>(global_symbol.value())) == nullptr) is false: Function "plus_one" already exist in module

```

But I saw that many codes in unittest, such as “test\_tir\_usmp\_utils.py”, call another func with the above method, though they do not build the module.

Any ideas? THX.

---

<div class="post-metadata">

**Author:** ![wrongtest](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/wrongtest/32/2008_2.png) [@wrongtest](https://discuss.tvm.apache.org/u/wrongtest)\
**Post date:** [March 2, 2022, 8:17am UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/2 "2022-03-02T08:17:53Z")

</div>

refer to 🙂 [Calling a PrimFunc from TVM IR](https://discuss.tvm.apache.org/t/calling-a-primfunc-from-tvm-ir/11931)

---

<div class="post-metadata">

**Author:** ![Maximilianxu](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/maximilianxu/32/988_2.png) [@Maximilianxu](https://discuss.tvm.apache.org/u/Maximilianxu)\
**Post date:** [March 2, 2022, 8:34am UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/3 "2022-03-02T08:34:06Z")

</div>

Thanks for your reply.

But can’t it work like TE? Is Relay necessary in this case?

---

<div class="post-metadata">

**Author:** ![wrongtest](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/wrongtest/32/2008_2.png) [@wrongtest](https://discuss.tvm.apache.org/u/wrongtest)\
**Post date:** [March 2, 2022, 9:42am UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/4 "2022-03-02T09:42:53Z")

</div>

Hi, as a summary, TIR → TIR invocations are not fully supported yet. In your example, `T.call_extern("plus_one", B.data, dtype="float32")` do not means invoke the tir func within module generally. It should be just written as `plus_one(B.data, dtype="float32")`, which is not supported however.

---

<div class="post-metadata">

**Author:** ![SYangDong](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/syangdong/32/1679_2.png) [@SYangDong](https://discuss.tvm.apache.org/u/SYangDong)\
**Post date:** [March 2, 2022, 2:05pm UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/5 "2022-03-02T14:05:05Z")

</div>

Here is my solution, save the global vars and its corresponding prim\_funcs into a map at first. And then inline these functions together when calling another prim\_func. Function calls will be pretty complicated when analyzing the buffer.

---

<div class="post-metadata">

**Author:** ![Maximilianxu](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/maximilianxu/32/988_2.png) [@Maximilianxu](https://discuss.tvm.apache.org/u/Maximilianxu)\
**Post date:** [March 2, 2022, 2:12pm UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/6 "2022-03-02T14:12:03Z")

</div>

I agree that this is very complicated. I am trying to wrap these prim\_funcs with relay.Function, but I haven’t done yet. I am not familier with Relay DSL, especially the Function call (e.g., the args, and the ret). Again, it is very complicated to me.

---

<div class="post-metadata">

**Author:** ![SYangDong](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/syangdong/32/1679_2.png) [@SYangDong](https://discuss.tvm.apache.org/u/SYangDong)\
**Post date:** [March 2, 2022, 2:22pm UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/7 "2022-03-02T14:22:51Z")

</div>

What I want to do is to implement these long-tail operators using tir.script rather than IRbuilder. Because IRbuilder is hard to differentiate python ast and tir nodes which bother me a lot during debugging. To achieve better perfomance, I decide to inline these functions together. You can use relay as well, but fusing these operators will still be hard.

---

<div class="post-metadata">

**Author:** ![Maximilianxu](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/maximilianxu/32/988_2.png) [@Maximilianxu](https://discuss.tvm.apache.org/u/Maximilianxu)\
**Post date:** [March 2, 2022, 2:44pm UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/8 "2022-03-02T14:44:16Z")

</div>

When the prim\_func is simple, inlining will be OK. In my case, two prim\_funcs are both complicated.

---

<div class="post-metadata">

**Author:** ![masahi](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/masahi/32/74_2.png) [@masahi](https://discuss.tvm.apache.org/u/masahi)\
**Post date:** [March 2, 2022, 7:49pm UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/9 "2022-03-02T19:49:25Z")

</div>

> [@Maximilianxu](#):
>
> When the prim\_func is simple, inlining will be OK. In my case, two prim\_funcs are both complicated

TIR doesn’t have a construct for functional call, so this is a hard problem. Can you tell me your use case (the need to call a complicated func from another)?

---

<div class="post-metadata">

**Author:** ![junrushao](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/junrushao/32/205_2.png) [@junrushao](https://discuss.tvm.apache.org/u/junrushao)\
**Post date:** [March 2, 2022, 8:04pm UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/10 "2022-03-02T20:04:48Z")

</div>

Would you guys like to try `T.call_packed` instead of `T.call_extern`?

---

<div class="post-metadata">

**Author:** ![Maximilianxu](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/maximilianxu/32/988_2.png) [@Maximilianxu](https://discuss.tvm.apache.org/u/Maximilianxu)\
**Post date:** [March 3, 2022, 1:36am UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/11 "2022-03-03T01:36:53Z")

</div>

In our case, the callee is a common subroutine that we want to build as a separate function. It will be used to precompute some indices for other functions.

I am wondering is it sufficient to just provide a T.call intrinsic which will insert a tir.Call stmt in tir/intrinsic.py in this case?

---

<div class="post-metadata">

**Author:** ![Maximilianxu](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/maximilianxu/32/988_2.png) [@Maximilianxu](https://discuss.tvm.apache.org/u/Maximilianxu)\
**Post date:** [March 3, 2022, 1:41am UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/12 "2022-03-03T01:41:15Z")

</div>

It seems that there is no `T.call_packed` and `call_packed` only exists in the original TIR IRBuilder infra.

---

<div class="post-metadata">

**Author:** ![miraclezqc](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/miraclezqc/32/2096_2.png) [@miraclezqc](https://discuss.tvm.apache.org/u/miraclezqc)\
**Post date:** [March 12, 2022, 1:07pm UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/13 "2022-03-12T13:07:31Z")

</div>

It seems that the cross call of primfunc is not supported yet, hope it will be supported in the future

---

<div class="post-metadata">

**Author:** ![zxs185996](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/z/90ced4/32.png) [@zxs185996](https://discuss.tvm.apache.org/u/zxs185996)\
**Post date:** [March 22, 2022, 4:29pm UTC](https://discuss.tvm.apache.org/t/tensorir-call-prim-func/12209/14 "2022-03-22T16:29:18Z")

</div>

Can you show your code?
