You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tvm.apache.org by Krzysztof Parzyszek via Apache TVM Discuss <no...@discuss.tvm.ai> on 2021/08/05 19:01:00 UTC

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?


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?





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/1) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/5190cf052f7a113a0a6ba2b1cdbbae67bc3b7bbb0602f5f1178f8d0441b14da7).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by Junru Shao via Apache TVM Discuss <no...@discuss.tvm.ai>.

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





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/10) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/47d09ccbd91df4df3d1a66a69b63aa274f37ae3100be187c3e0cc7d6730a3773).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by tqchen via Apache TVM Discuss <no...@discuss.tvm.ai>.

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





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/11) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/ff5b17e89d7170df4e7c264a78bedb89157bb7bcc6adbe79f8dc1c30c74125ff).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by tqchen via Apache TVM Discuss <no...@discuss.tvm.ai>.

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.





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/13) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/5ac7d923d21e48c8031a3fe075b2278f73873d02ef9db8111a6d2fc7b1d9ffb8).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by Krzysztof Parzyszek via Apache TVM Discuss <no...@discuss.tvm.ai>.

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.





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/12) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/5b8807f663102e0bc848dd5656c1b6e00f44e04ff958497b67c2d58e5b08091e).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by tqchen via Apache TVM Discuss <no...@discuss.tvm.ai>.

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.





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/9) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/99b86cfdb99cb42cce3b70e15b8beffbb78ba6689263bd6dd8d5a968698d1ab3).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by Krzysztof Parzyszek via Apache TVM Discuss <no...@discuss.tvm.ai>.

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?





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/8) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/79b689241fe86cb111049983a28125ad911d34a70e541538dafe28051b053b68).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by Xiyou Zhou via Apache TVM Discuss <no...@discuss.tvm.ai>.

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.





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/7) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/3a75005bb48b96de9d26e54c415813bf29039dee2097b3c54c8bee72a370b6b9).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by Krzysztof Parzyszek via Apache TVM Discuss <no...@discuss.tvm.ai>.

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.





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/6) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/030e1b6afd2f66d6142d46a938098d58ef6fb729f17b9525e6ffa038d43d1032).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by Xiyou Zhou via Apache TVM Discuss <no...@discuss.tvm.ai>.

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.





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/5) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/3b74efc067bdc8b8c2f65b10ba18940bbcc62a5c809303eb6ac6bfc36922c391).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by Xiyou Zhou via Apache TVM Discuss <no...@discuss.tvm.ai>.

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.





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/4) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/40256c0665c9a8cf5549a724be2c193cbdf8796adc8b10806159117bd51c8c06).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by Krzysztof Parzyszek via Apache TVM Discuss <no...@discuss.tvm.ai>.

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





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/3) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/740cbb1cb07ac39583df802b77d88c6709b78d0ec6db32bea641a63da822e550).

[Apache TVM Discuss] [Development] Is there a difference between "lower" and "legalize" in TIR?

Posted by Xiyou Zhou via Apache TVM Discuss <no...@discuss.tvm.ai>.

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.





---
[Visit Topic](https://discuss.tvm.apache.org/t/is-there-a-difference-between-lower-and-legalize-in-tir/10714/2) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/be41a0fa0ff75f867c8d4aa6ab8647dd5252288e005973f947bcdf2f25e00668).