# Implicit requirements on parameter types of AssertStmt?

**URL:** <https://discuss.tvm.apache.org/t/implicit-requirements-on-parameter-types-of-assertstmt/11129>\
**Category:** Troubleshooting\
**Created:** [September 25, 2021, 3:07pm UTC](https://discuss.tvm.apache.org/t/implicit-requirements-on-parameter-types-of-assertstmt/11129 "2021-09-25T15:07:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![syang](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/syang/32/2964_2.png) [@syang](https://discuss.tvm.apache.org/u/syang)\
**Post date:** [September 25, 2021, 3:07pm UTC](https://discuss.tvm.apache.org/t/implicit-requirements-on-parameter-types-of-assertstmt/11129/1 "2021-09-25T15:07:24Z")

</div>

Hi all, I found an interesting problem about AssertStmt in tir part.

When I try to build this module about AssertStmt, I got such an error:

```python
import tvm
from tvm import tir

v = tir.Var("v", "int32")
assert_stmt = tir.AssertStmt(v, v, tir.Evaluate(tir.const(0)))
prim_func = tir.PrimFunc({v}, assert_stmt)
mod = tvm.lower(prim_func)
tvm.build(mod)

```

 ![image](https://discuss.tvm.apache.org/uploads/default/original/2X/3/33dbdcf5d6e1292d2bc3cabe5da7270882c697d0.png)

But if I replace the condition variable with a comparison, I can build this module successfully:

 ![image](https://discuss.tvm.apache.org/uploads/default/original/2X/f/f2e822b21b525fa1068266e442e9ddd5fc2e76a5.png)

It seems that although the parameter type of `condition` declared in the document is `PrimExpr`, actually there are some implicit requirements on it? Or we just implement the assert statement wrong? I’m curious the reason since this behavior works well in other languages like Python 😂:

 ![image](https://discuss.tvm.apache.org/uploads/default/original/2X/9/96c8dcd2bb105ebb55a0a5b732a5ac4851f9dce3.png)

---

<div class="post-metadata">

**Author:** ![syang](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/syang/32/2964_2.png) [@syang](https://discuss.tvm.apache.org/u/syang)\
**Post date:** [September 25, 2021, 3:45pm UTC](https://discuss.tvm.apache.org/t/implicit-requirements-on-parameter-types-of-assertstmt/11129/2 "2021-09-25T15:45:18Z")

</div>

And I found actually we have a specific type check about `message` of `AssertStmt`, so it would be better if we also check the type of `condition` to prevent this error?

 ![image](https://discuss.tvm.apache.org/uploads/default/original/2X/b/b6b7c03bcd1201a9f9dec02e21dfeab5ed8af3a1.png)

---

<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:** [September 25, 2021, 8:19pm UTC](https://discuss.tvm.apache.org/t/implicit-requirements-on-parameter-types-of-assertstmt/11129/3 "2021-09-25T20:19:45Z")

</div>

In python, pretty much every object you can take has some “truth value”, i.e. it can be placed in a context where a true/false condition is expected, such as an `if` statement or an `assert` statement. This is simply because python is designed to work that way.

In TIR values are not automatically convertible to true/false, so in conditions, like the one in `Assert`, you need something that has a boolean type. The error you’re seeing comes from LLVM, and the complaint is that the argument to the `br` instruction is not of type `i1`. What most likely happened there, is that `v` got converted to an `llvm::Value`, with a type `i32`, and then used directly in the branch. This is not a legal LLVM IR, and you got a verifier error.

---

<div class="post-metadata">

**Author:** ![syang](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/syang/32/2964_2.png) [@syang](https://discuss.tvm.apache.org/u/syang)\
**Post date:** [September 26, 2021, 3:05am UTC](https://discuss.tvm.apache.org/t/implicit-requirements-on-parameter-types-of-assertstmt/11129/4 "2021-09-26T03:05:23Z")

</div>

Thanks for your reply! @kparzysz

It seems that for LLVM, it expects a boolean value as the argument of `assert` statement, while it can accept other types as the argument of `if` statement, here is an example:

 ![image](https://discuss.tvm.apache.org/uploads/default/original/2X/4/4abb574e52ea4de5a3b7adf04f4d62931c711732.png)

And I found another interesting error that will occur if I also add an `else_case`:

```python
import tvm
from tvm import tir

v = tir.Var("v", "int32")
assert_stmt = tir.IfThenElse(v, tir.Evaluate(tir.const(0)),tir.Evaluate(tir.const(0)))
prim_func = tir.PrimFunc({v}, assert_stmt)
print(prim_func)
mod = tvm.lower(prim_func)
tvm.build(mod)

```

 ![image](https://discuss.tvm.apache.org/uploads/default/original/2X/2/252cb1200686789f6b65346e6d08befe95237b3f.png)

I think TVM doesn’t check the type of condition strictly, since the function implemented in [src/arith/ir\_mutator\_with\_analyzer.cc](https://github.com/apache/tvm/blob/main/src/arith/ir_mutator_with_analyzer.cc#L73) calls `Not(real_condition)` directly, which means it expects the type of `condition` to be bool. While if we don’t pass a `then_case`, there will be no such implicit requirements:

```c++
Stmt IRMutatorWithAnalyzer::VisitStmt_(const IfThenElseNode* op) {
  // ...
  Stmt then_case, else_case;
  {
    With<ConstraintContext> ctx(analyzer_, real_condition);
    then_case = this->VisitStmt(op->then_case);
  }
  if (op->else_case.defined()) {
    With<ConstraintContext> ctx(analyzer_, analyzer_->rewrite_simplify(Not(real_condition)));
    else_case = this->VisitStmt(op->else_case);
  }
  // ...
}

```

---

<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:** [September 26, 2021, 3:21pm UTC](https://discuss.tvm.apache.org/t/implicit-requirements-on-parameter-types-of-assertstmt/11129/5 "2021-09-26T15:21:11Z")

</div>

You’re probably right. I guess TVM could be more consistent in checking TIR for errors.
