# Is there a difference between "lower" and "legalize" in TIR?

**URL:** <https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714>\
**Category:** Development\
**Created:** [August 5, 2021, 6:50pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714 "2021-08-05T18:50:54Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kparzysz](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/kparzysz/32/252_2.png) [@kparzysz](https://discuss.tvm.apache.org/u/kparzysz)\
**Post date:** [August 5, 2021, 6:50pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/1 "2021-08-05T18:50:54Z")

</div>

Both, `FLowerIntrinsic` and `FLegalize` are the same type, and they are handled in a pretty much the same way. Is one of them being phased out?

---

<div class="post-metadata">

**Author:** ![zxybazh](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/zxybazh/32/2409_2.png) [@zxybazh](https://discuss.tvm.apache.org/u/zxybazh)\
**Post date:** [August 5, 2021, 7:26pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/2 "2021-08-05T19:26:18Z")

</div>

Hi, good catch! They are designed to deal with op intrinsic lowering and op legalization respectively in the lowering pass. Ideally, we always want to use intrinsic lowering before legalization if available. I don’t think we are going to phase out any of them for now because there could be diverse need for intrinsic lowering and legalization in packed function type in the future.

---

<div class="post-metadata">

**Author:** ![kparzysz](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/kparzysz/32/252_2.png) [@kparzysz](https://discuss.tvm.apache.org/u/kparzysz)\
**Post date:** [August 5, 2021, 7:41pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/3 "2021-08-05T19:41:05Z")

</div>

Intrinsics and ops are represented in the same way in TIR though. What is the intended behavior of “legalization” that is different from “lowering”?

---

<div class="post-metadata">

**Author:** ![zxybazh](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/zxybazh/32/2409_2.png) [@zxybazh](https://discuss.tvm.apache.org/u/zxybazh)\
**Post date:** [August 5, 2021, 8:14pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/4 "2021-08-05T20:14:27Z")

</div>

Legalization is when there is no available op intrinsic lowering function available, we might use other op to combine and implement some op. For example, [here](https://github.com/apache/tvm/blob/3d380fc5817ce335ea823ce7e6b7e35717e579cd/src/target/llvm/intrin_rule_llvm.cc#L106) the exp10 op on llvm platform is implemented as legalization.

---

<div class="post-metadata">

**Author:** ![zxybazh](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/zxybazh/32/2409_2.png) [@zxybazh](https://discuss.tvm.apache.org/u/zxybazh)\
**Post date:** [August 5, 2021, 8:16pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/5 "2021-08-05T20:16:09Z")

</div>

So technically I don’t think they are very different in behavior, i.e., the type of functions are the same in TIR. It’s more of a conceptual difference for now.

---

<div class="post-metadata">

**Author:** ![kparzysz](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/kparzysz/32/252_2.png) [@kparzysz](https://discuss.tvm.apache.org/u/kparzysz)\
**Post date:** [August 5, 2021, 8:21pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/6 "2021-08-05T20:21:11Z")

</div>

From what I’ve seen so far there is no difference at all, conceptual or not. We should stick to one and get rid of the other. Both operate on the same TIR representation. In one case the TIR expression is replaced by another function call, in the other the resulting expression is more complicated, but it’s still a TIR expression.

We should not have multiple concepts that do the same thing. On the contrary, we should aim to reduce the number of different abstractions.

---

<div class="post-metadata">

**Author:** ![zxybazh](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/zxybazh/32/2409_2.png) [@zxybazh](https://discuss.tvm.apache.org/u/zxybazh)\
**Post date:** [August 5, 2021, 8:44pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/7 "2021-08-05T20:44:55Z")

</div>

I do agree that we don’t want abstractions that are not useful.

Just to explain a bit more: any op could have both legalization and intrinsic lowering function. Type-wise, yes they are all TIR expressions and technically there’s no difference. Concept-wise, since any op can have both implementation and we always prefer lower intrinsic function rather than legalization function, they are different IMHO. And the legalization function could be shared across platform, that’s another difference.

---

<div class="post-metadata">

**Author:** ![kparzysz](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/kparzysz/32/252_2.png) [@kparzysz](https://discuss.tvm.apache.org/u/kparzysz)\
**Post date:** [August 5, 2021, 8:49pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/8 "2021-08-05T20:49:10Z")

</div>

Why would an op have both? This is still the question that hasn’t been answered. What was the intent behind having “lowering” and “legalization” as different things?

---

<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:** [August 5, 2021, 8:54pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/9 "2021-08-05T20:54:09Z")

</div>

Thanks @kparzysz . I believe the argument can go both ways in here. If we view the overall IR as “staged”, lowering means we have a strategy to map them to the corresponding low level intrinsics.

While legalization means we remap the op by composing using other expressions, which still need to be lowered in order to remain valid in the target platform.

For example, mapping `exp => llvm.exp` is a way of lowering, but because llvm does not have `sigmoid`, imagine we have a sigmoid op, mapping `sigmoid(x)=>1 /(1 + exp(-x))` will be called legalization, and we still need to map the exp to `llvm.exp` after legalize in order to run on the target platform.

---

<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:** [August 5, 2021, 8:55pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/10 "2021-08-05T20:55:40Z")

</div>

Sorry I was posting on a wrong thread

@kparzysz Thanks for asking! For example, there might be some rare TVM intrinsics like `tir.erf` that may be lowered to some cuda intrinsic (using `Lowering`), but those intrinsics aren’t guaranteed to exist on other hardware platforms, and in this case, we substitute the intrinsic with more complicated computation (using `Legalization`)

---

<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:** [August 5, 2021, 8:57pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/11 "2021-08-05T20:57:36Z")

</div>

Most op likely will have a generic legalization strategy(in case target lowering strategy does not exist), and specific lowering strategy per target

---

<div class="post-metadata">

**Author:** ![kparzysz](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/kparzysz/32/252_2.png) [@kparzysz](https://discuss.tvm.apache.org/u/kparzysz)\
**Post date:** [August 5, 2021, 9:00pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/12 "2021-08-05T21:00:30Z")

</div>

The goal is to rewrite the TIR into a way that the target’s codegen can handle. Rewriting `exp` into `llvm.exp` can be seen as legalization too. There can be targets that can generate code directly for `exp`. In fact, the translation from `exp` to `llvm.exp` is only there for convenience (automation). The LLVM code generator could be changed to handle `exp` just fine.

The distinction, as presented so far, is very subjective, and IMO unnecessary.

---

<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:** [August 5, 2021, 9:04pm UTC](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/13 "2021-08-05T21:04:17Z")

</div>

I do not disagree, that is why I think the argument can go both ways. One pt here though is that legalization means we need to recursively call the lowering strategy on the return expr of the legalized code, while in the case of lowering we do not need to do so.

We could certainly name all the cases `Legalization` and still make things work. So it is mainly a technical design tradeoff that we need to make and in this case we can see arguments for both sides.
