tvm icon indicating copy to clipboard operation
tvm copied to clipboard

[Bug]LLVM codegen crashes on Add over bool (internal ICHECK: MatchesCode(DLDataTypeCode::kDLFloat))

Open yunligou711-commits opened this issue 1 month ago • 1 comments

Thanks for participating in the TVM community! We use https://discuss.tvm.apache.org/ for any general usage questions and discussions. The issue tracker is used for actionable items such as feature proposals discussion, roadmaps, and bug tracking. You are always welcomed to post on the forum first :smile_cat:

Issues that are inactive for a period of time may get closed. We adopt this policy so that we won't lose track of actionable issues that may fall at the bottom of the pile. Feel free to reopen a new one if you feel there is an additional problem that needs attention when an old one gets closed.

Expected behavior

Building a PrimFunc that contains an Add over bool operands targeting llvm should either compile (bool add lowered to a bitwise/integer operation, as the VM and other backends accept it) or fail with a normal user-facing diagnostic that names the offending operator and dtype. An internal ICHECK with an empty message is not an acceptable outcome for a program the front end accepted.

Actual behavior

tvm.build(mod, target="llvm") aborts with an internal check failure. The message carries no dtype or operator information, and the failure surfaces as a TVM_FFI_ICHECK inside codegen rather than a diagnostic:

tvm.error.InternalError: Check failed: (t.MatchesCode(DLDataTypeCode::kDLFloat)) is false:

Traceback (tail):

  File ".../src/target/llvm/codegen_llvm.cc", line 2077, in CodeGenLLVM::VisitStmt_(tirx::BufferStoreNode const*)
    llvm::Value* value = MakeValue(op->value);
  File ".../src/target/llvm/codegen_llvm.cc", line 1608, in CodeGenLLVM::VisitExpr_(prim::AddNode const*)
    DEFINE_CODEGEN_BINARY_OP(Add);
  File ".../src/target/llvm/codegen_llvm.cc", line 1608, in CodeGenLLVM::CreateAdd(PrimType, llvm::Value*, llvm::Value*)
    DEFINE_CODEGEN_BINARY_OP(Add);
tvm.error.InternalError: Check failed: (t.MatchesCode(DLDataTypeCode::kDLFloat)) is false:

The front end accepts the program without complaint — x[0] + w[0] on bool buffers parses into a well-formed prim::AddNode of dtype bool, so nothing rejects it before codegen.

Environment

  • TVM commit 8312a17f8734ddfd56e5f3977cd5df25b83ec49f (0.26.dev0), built from source
  • Target: llvm (CPU backend, host x86_64)
  • Python 3.11 / linux x86_64

Steps to reproduce

import tvm
from tvm.script import ir as I
from tvm.script import tirx as T


@I.ir_module
class Module:
    @T.prim_func
    def add(x: T.Buffer((T.int64(8),), "bool"),
            w: T.Buffer((T.int64(8),), "bool"),
            T_add: T.Buffer((T.int64(8),), "bool")):
        T_add[0] = x[0] + w[0]


tvm.build(Module, target="llvm")

Reproduces 3/3. Changing the buffer dtype to float32, float16, float64, bfloat16, float8_e4m3fn, int8, int32, uint8 or uint1 makes it compile successfully — bool is the only dtype affected in this sweep.

Triage

Root cause is DEFINE_CODEGEN_BINARY_OP in src/target/llvm/codegen_llvm.cc:1584, whose three-way dispatch only covers integer and kDLFloat:

if (t.MatchesCode(DLDataTypeCode::kDLInt)) {
  ...
} else if (t.MatchesCode(DLDataTypeCode::kDLUInt)) {
  ...
} else {
  TVM_FFI_ICHECK(t.MatchesCode(DLDataTypeCode::kDLFloat));
  return builder_->CreateF##Op(a, b);
}

bool has its own DLPack code (kDLBool), so it matches neither of the first two branches and falls into the else, where the ICHECK has no message and the following CreateFAdd would not be the right operation for a boolean anyway. bfloat16 and float8* also fail MatchesCode(DLDataTypeCode::kDLFloat), but they compile fine because they are handled earlier in the pipeline (tirx::IsBFloat16Type / IsFloat8Type in src/tirx/op/op.cc), so in practice this else branch is reached only for bool.

yunligou711-commits avatar Oct 07 '26 01:10 yunligou711-commits

This looks like the same bug as #20558: +, - and * on bool operands hit the same check in DEFINE_CODEGEN_BINARY_OP. #20570 fixes it by computing in int32 and converting the result back to bool, as the C target does.

I ran the reproducer from this issue on current main (d55759ec1, with T.Buffer changed to T.Tensor) with #20570 applied: it now builds, and x[0] + w[0] returns x[0] or w[0] for all four input combinations.

wwoosshh avatar Oct 07 '26 05:10 wwoosshh