# Running LegalizeOps in C++

**URL:** <https://discuss.tvm.apache.org/t/running-legalizeops-in-c/18165>\
**Category:** Troubleshooting\
**Created:** [March 7, 2025, 3:17pm UTC](https://discuss.tvm.apache.org/t/running-legalizeops-in-c/18165 "2025-03-07T15:17:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbaquir](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@mbaquir](https://discuss.tvm.apache.org/u/mbaquir)\
**Post date:** [March 7, 2025, 3:17pm UTC](https://discuss.tvm.apache.org/t/running-legalizeops-in-c/18165/1 "2025-03-07T15:17:42Z")

</div>

I am having trouble running legalizeOps in C++. I have an IRModule in C++ and I try to run a few passes.

```
auto optMod = tvm::transform::Sequential({
          tvm::relax::transform::Normalize(),
          tvm::relax::transform::FoldConstant(),
          tvm::relax::transform::EliminateCommonSubexpr(),
          tvm::relax::transform::LegalizeOps(tvm::runtime::NullOptType{}, true),
          tvm::relax::transform::DeadCodeElimination(),
          tvm::tir::transform::LowerInitBlock(),
          tvm::tir::transform::RemoveNoOp(),
          tvm::tir::transform::Simplify(),
        })(tvmMod);

```

When I try to run this in C++ instead of python I get a bunch of warnings (for every relax op basically):

```
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.power is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.sum is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.reshape is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.multiply is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.add is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.rsqrt is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.multiply is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.multiply is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.permute_dims is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.reshape is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.matmul is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.permute_dims is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.reshape is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.matmul is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.permute_dims is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.reshape is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.matmul is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.reshape is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.reshape is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.reshape is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.strided_slice is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.strided_slice is found.
<...>/tvm/0.19.0/Release/src/relax/transform/legalize_ops.cc:323: Warning: No legalization func for relax.reshape is found

```

My understanding is that when we import tvm in python it automatically registers legalization functions for all the relax operator. Is this not possible in C++? Is there a way to run LegalizeOps in C++?

Thanks

---

<div class="post-metadata">

**Author:** ![Hzfengsy](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/hzfengsy/32/2961_2.png) [@Hzfengsy](https://discuss.tvm.apache.org/u/Hzfengsy)\
**Post date:** [March 8, 2025, 5:09am UTC](https://discuss.tvm.apache.org/t/running-legalizeops-in-c/18165/2 "2025-03-08T05:09:21Z")

</div>

You are right. All legalization functions for operators are registered in Python. So there is no way to run it in pure C++.

I wonder what’s the use case of running the compilation stage without python env

---

<div class="post-metadata">

**Author:** ![tqchen](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/tqchen/32/5_2.png) [@tqchen](https://discuss.tvm.apache.org/u/tqchen)\
**Post date:** [March 8, 2025, 11:21am UTC](https://discuss.tvm.apache.org/t/running-legalizeops-in-c/18165/3 "2025-03-08T11:21:01Z")

</div>

in general we are moving towards a stage where we focus on python-first compiler development, while enabling deployment without any python dependencies.
