llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[LoopVectorize] `different types inferred for different operands` in truncateToMinimalBitwidths

Open arie2026 opened this issue 3 weeks ago • 0 comments

In IR the two ors share no operand, so MinBWs gives them different widths. VPlan then folds sext i8 1 to i32 into the live-in ir<1>, which the first or also uses. truncateToMinimalBitwidths caches truncates per operand only (ProcessedTruncs), so the second or reuses a trunc of ir<1> made for the other width, and cloning the recipe asserts.

Reproducer (opt -mtriple=x86_64-unknown-linux-gnu -mattr=+avx2 -passes=loop-vectorize; also reproduces on 5e457cda8d5a and f2808fc79):

define void @f(i32 %a, ptr %dst) {
entry:
  br label %loop

loop:
  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
  %o1 = or i32 %a, 1
  %c0 = sext i8 1 to i32
  %cmp = icmp sle i32 %o1, 0
  %z = zext i1 %cmp to i32
  %o2 = or i32 %c0, %z
  %t = trunc i32 %o2 to i16
  %found = icmp eq i16 %t, 0
  %gep = getelementptr i1, ptr %dst, i64 %iv
  store i1 %found, ptr %gep
  %iv.next = add i64 %iv, 1
  %ec = icmp eq i64 %iv.next, 1024
  br i1 %ec, label %exit, label %loop

exit:
  ret void
}
$ opt -mtriple=x86_64-unknown-linux-gnu -mattr=+avx2 -passes=loop-vectorize -disable-output test.ll
opt: llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp:498: auto llvm::computeScalarTypeForInstruction(unsigned int, ArrayRef<VPValue *>)::(anonymous class)::operator()(unsigned int, Type *) const: Assertion `(!OpTy || OpTy == ExpectedTy) && "different types inferred for different operands"' failed.
#11 llvm::computeScalarTypeForInstruction(...)
#12 llvm::VPWidenRecipe::VPWidenRecipe(...)
#13 llvm::VPWidenRecipe::cloneWithOperands(...)
#14 llvm::VPlanTransforms::truncateToMinimalBitwidths(...)
#15 llvm::VPlanTransforms::runPass<...>(...)
#16 llvm::LoopVectorizationPlanner::buildVPlans(...)
#17 llvm::LoopVectorizationPlanner::plan(...)
#18 llvm::LoopVectorizePass::processLoop(...)
#19 llvm::LoopVectorizePass::runImpl(...)

Expected: no crash. Each narrowed recipe gets operands truncated to its own width.

Notes: if the second or uses a literal 1 instead of %c0, the sharing exists in IR, MinBWs merges the two ors, and there is no crash. So the problem is sharing that VPlan creates after MinBWs was computed. Possible fix: key ProcessedTruncs by (Op, NewResTy), or don't reuse a cached trunc whose type differs.

Found with a mutation-based IR fuzzer. Triage, test-case minimization and parts of this write-up were assisted by an LLM; verified the reproducer on 9dce18f0e.

Assisted-by: Claude (Anthropic)

arie2026 avatar Nov 11 '26 20:11 arie2026