Issue 244589
Summary [SelectionDAG][SPARC] convertMaskTreeImpl (WidenVSELECTMask) turns an all-true mask leaf into -1 on a ZeroOrOne-boolean target: select(shuffle(a<b, true), 1, 0) returns -1 lanes
Labels backend:Sparc, miscompilation, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```cpp
/* [SelectionDAG][SPARC] WidenVSELECTMask / convertMaskTreeImpl turns an all-true
 * BUILD_VECTOR mask leaf into all-ones (-1) lanes, ignoring the target's vector
 * boolean contents. SPARC uses ZeroOrOne booleans, so later folds that assume
 * 0/1 booleans copy the -1 into the result: `m ? 1 : 0` yields -1 in the lanes
 * that come from the constant-true operand of the shuffle.
 * Regression from 72e9d33d294 "Reapply [SelectionDAG] Recurse through mask
 * _expression_ trees in WidenVSELECTMask" (#217307).
 *
 * At -O0 the select reaches SelectionDAG as is (at -O1 and above InstCombine
 * turns `select m, 1, 0` into a zext, which is not affected).
 * Uses clang's ext_vector_type with bool elements and the vector ?: operator.
 *
 * Godbolt: compiler "x86-64 clang (trunk)" with flags:
 * --target=sparc-unknown-linux-gnu -O0
 * Godbolt cannot execute SPARC code: view the assembly of `n4`. Lanes 1 and 3
 * of the result come from one register (%i1 on Godbolt, %g2 in our build):
 *   buggy:   mov -1, %i1 (lanes 1 and 3 = -1)
 *   correct: mov 1, %i1       (lanes 1 and 3 = 1)
 * Run: qemu-sparc32plus ./a.out (any 32-bit SPARC Linux).
 *
 * EXPECTED (correct compiler):  r = 1 1 0 1 (expected 1 1 0 1)
 * BUGGY (vanilla trunk):        r = 1 -1 0 -1 (expected 1 1 0 1)
 * MISMATCH (exit 1)
 */
#include <stdio.h>

typedef int v4 __attribute__((ext_vector_type(4)));
typedef _Bool b4 __attribute__((ext_vector_type(4)));

__attribute__((noinline)) v4 n4(v4 a, v4 b) {
  /* lanes 0,2: a < b ; lanes 1,3: true  ->  1 or 0 */
  return __builtin_shufflevector(__builtin_convertvector(a < b, b4), (b4)1, 0, 5, 2, 7)
             ? (v4)1 : (v4)0;
}

volatile int ga[4] = {0, 5, 5, 0};
volatile int gb[4] = {1, 1, 1, 1};

int main(void) {
  v4 a = {ga[0], ga[1], ga[2], ga[3]};
  v4 b = {gb[0], gb[1], gb[2], gb[3]};
  v4 r = n4(a, b);
  printf("r = %d %d %d %d (expected 1 1 0 1)\n", r.x, r.y, r.z, r.w);
  int ok = r.x == 1 && r.y == 1 && r.z == 0 && r.w == 1;
  if (!ok)
 printf("MISMATCH\n");
  return !ok;
}
```

Resulted in this output:
```
Assembly (--target=sparc-unknown-linux-gnu -O0), end of n4:
	cmp	%i5, %g2
	mov	%i4, %g3
	movl	%icc, 1, %g3          ! lane 2: a2 < b2 -> 1 / 0
	...
	cmp	%i2, %i3
	movl	%icc, 1, %i4          ! lane 0: a0 < b0 -> 1 / 0
	mov	%i4, %i2
	mov	-1, %g2               ! lanes 1 and 3 ("true"): WRONG, must be 1
	mov	%g2, %i3
	mov	%g3, %i4
	mov	%g2, %i5
	std %i4, [%i1+8]
	std %i2, [%i1]

  lane | expected | actual
  0    |    1     |   1
  1    |    1     |  -1     <-- MISMATCH
  2    |    0     |   0
  3    |    1     |  -1     <-- MISMATCH

i.e. the program would print "r = 1 -1 0 -1 (expected 1 1 0 1)" and MISMATCH (lanes 1 and 3 are the
constant -1 regardless of input); clang on x86-64 -O0 prints "r = 1 1 0 1".
llc -O2 -mtriple=sparc on the equivalent IR gives `mov -1, %o1` for the same lanes.
```

Trunk: https://godbolt.org/z/qnYz4c5rv (x86-64 clang trunk, --target=sparc-unknown-linux-gnu -O0; assembly only, reproduces as of 2026-10-22)

Root cause: convertMaskTreeImpl in llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp (used by WidenVSELECTMask since #217307, commit 72e9d33d294) rebuilds a VSELECT mask tree at the widened mask type and returns DAG.getAllOnesConstant for an all-ones BUILD_VECTOR leaf, treating it as "type-lenient". That is only a valid "true" on ZeroOrNegativeOne targets; SPARC uses ZeroOrOne vector booleans, so after scalarization DAGCombiner::visitSELECT legitimately folds select(extract_vector_elt mask, i), 1, 0 to the mask element (known bits say 0/1), and the -1 leaks into the result in lanes coming from the constant-true shuffle operand. At -O1+ InstCombine turns select m, 1, 0 into zext and hides it; at -O0 it reaches the DAG as is.

Suggested fix: Return DAG.getBoolConstant(true, DL, ToVT, ToVT) for the all-ones leaf instead of DAG.getAllOnesConstant (1 on ZeroOrOne targets, -1 on ZeroOrNegativeOne targets, so x86/AArch64/AMDGPU are unchanged), or mark the leaf not lenient and convert it like a SETCC leaf. Validated locally (a patched tree carries this fix and lanes 1 and 3 become mov 1).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to