Skip to content

[FIX][LLVM] Support bool Add/Sub/Mul in LLVM codegen - #20570

Open
wwoosshh wants to merge 1 commit into
apache:mainfrom
wwoosshh:fix/20558-llvm-bool-arith
Open

wwoosshh wants to merge 1 commit into
apache:mainfrom
wwoosshh:fix/20558-llvm-bool-arith

Conversation

@wwoosshh

@wwoosshh wwoosshh commented Oct 6, 2026

Copy link
Copy Markdown

Fixes #20558.

+, - and * on bool operands hit Check failed: (t.MatchesCode(DLDataTypeCode::kDLFloat)) in the LLVM codegen, because DEFINE_CODEGEN_BINARY_OP only handled int, uint and float. The C target builds the same function.

This PR follows the C target. The bool operands are zero-extended to i32, the operation is done in i32, and the result is converted back to bool by comparing it with zero. For 0/1 inputs this gives OR for +, XOR for - and AND for *, and in the optimized IR (inspect_source("ll")) the vectorized case reduces to exactly or, xor and and. The same code path covers vector bool types.

To check that the behavior matches, I built the same PrimFunc for target="c", compiled it with export_library, and compared the stored bytes with the LLVM build for all four input combinations. They are identical.

In the issue I also asked whether bool arithmetic should instead be rejected earlier. I can switch to that approach if you prefer it.

Testing: added test_llvm_bool_arith to tests/python/codegen/test_target_codegen_llvm.py. It covers all input combinations for +, - and * with both a serial and a vectorized loop. It fails on main with the check above and passes with this change. The full test_target_codegen_llvm.py passes locally (381 passed).

Generated-by: Claude Code (Claude Opus 5.5)

LLVM codegen hit an internal check on arithmetic over bool operands,
while the C target builds the same function. Follow the C target:
compute in int and convert back to bool, so a non-zero result is true.

Fixes apache#20558

Generated-by: Claude Code (Claude Opus 5.5)
@wwoosshh

wwoosshh commented Oct 6, 2026

Copy link
Copy Markdown
Author

cc @tlopex @tqchen

This is a small fix for #20558: the LLVM codegen now handles bool +/-/* the same way as the C target (compute in int, then non-zero is true), with a regression test. Could you take a look when you have time? Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] LLVM codegen aborts on arithmetic over "bool" buffers (Add/Sub/Mul)

1 participant