Issue 237727
Summary [Hexagon] Storing or bitcasting gives wrong results
Labels new issue
Assignees
Reporter jedisct1
    I ran into a miscompilation on Hexagon while working on Zig.

Repro:

```llvm
target triple = "hexagon-unknown-linux-musl"

define void @store_v4i1(<4 x i16> %a, <4 x i16> %b, ptr %p) {
  %c = icmp eq <4 x i16> %a, %b
  store <4 x i1> %c, ptr %p
  ret void
}

define i8 @bitcast_v4i1(<4 x i16> %a, <4 x i16> %b) {
 %c = icmp eq <4 x i16> %a, %b
  %m = bitcast <4 x i1> %c to i4
  %r = zext i4 %m to i8
  ret i8 %r
}
```

Output (trimmed to interesting parts):

```console
$ llc -O2 repro.ll -o -
store_v4i1:
	{
		p0 = vcmph.eq(r1:0,r3:2)
	}
	{
		r0 = p0
		jumpr r31
		memb(r4+#0) = r0.new
	}
bitcast_v4i1:
	{
		p0 = vcmph.eq(r1:0,r3:2)
		r29 = add(r29,#-8)
	}
	{
		r0 = p0
		jumpr r31
		r29 = add(r29,#8)
	}
```

`vcmph.eq` sets two bits of `p0` for each element, so `p0` is `0xff` when both vectors are equal.
And that byte then gets stored or returned as is.

So:

- `store_v4i1` writes `0xff` instead of `0x0f`.
- With `{1,2,3,4}` and `{1,9,3,9}`, `bitcast_v4i1` returns `0x33` instead of `0x5`.

`<2 x i1>` has the same problem and reductions break too, since they turn into bitcasts.

Reproduced on LLVM 23.1.4 and main. Same issue at -O0 and -O2.

<details><summary>Full repro (tested with qemu-hexagon)</summary>

```console
$ llc -O2 -filetype=obj test.ll -o test.o
$ zig cc -target hexagon-linux-musl driver.c test.o -o test
$ qemu-hexagon ./test
store <8 x i1> (all true)                got 0xff, expected 0xff
store <4 x i1> (all true)                got 0xff, expected 0xf  <-- WRONG
store <2 x i1> (all true)                got 0xff, expected 0x3  <-- WRONG
load <4 x i1> from 0x05, sext to i16     got 0xff00ff, expected 0xffff0000ffff  <-- WRONG
bitcast <4 x i1> {1,0,1,0} to i4 got 0x33, expected 0x5  <-- WRONG
bitcast i4 0x5 to <4 x i1>, sext to i16 got 0xff00ff, expected 0xffff0000ffff  <-- WRONG
reduce.and <4 x i1> (all true)           got 0x0, expected 0x1  <-- WRONG
```

`test.ll`:

```llvm
target triple = "hexagon-unknown-linux-musl"

define void @store_v8i1(<8 x i8> %a, <8 x i8> %b, ptr %p) {
  %c = icmp eq <8 x i8> %a, %b
  store <8 x i1> %c, ptr %p
  ret void
}

define void @store_v4i1(<4 x i16> %a, <4 x i16> %b, ptr %p) {
  %c = icmp eq <4 x i16> %a, %b
  store <4 x i1> %c, ptr %p
  ret void
}

define void @store_v2i1(<2 x i32> %a, <2 x i32> %b, ptr %p) {
 %c = icmp eq <2 x i32> %a, %b
  store <2 x i1> %c, ptr %p
  ret void
}

define <4 x i16> @load_v4i1(ptr %p) {
  %c = load <4 x i1>, ptr %p
  %r = sext <4 x i1> %c to <4 x i16>
  ret <4 x i16> %r
}

define zeroext i8 @bitcast_v4i1(<4 x i16> %a, <4 x i16> %b) {
  %c = icmp eq <4 x i16> %a, %b
  %m = bitcast <4 x i1> %c to i4
  %r = zext i4 %m to i8
  ret i8 %r
}

define <4 x i16> @bitcast_i4(i8 %m) {
  %t = trunc i8 %m to i4
 %c = bitcast i4 %t to <4 x i1>
  %r = sext <4 x i1> %c to <4 x i16>
  ret <4 x i16> %r
}

define zeroext i1 @all_eq_v4i1(<4 x i16> %a, <4 x i16> %b) {
  %c = icmp eq <4 x i16> %a, %b
  %r = call i1 @llvm.vector.reduce.and.v4i1(<4 x i1> %c)
  ret i1 %r
}
```

`driver.c`:

```c
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

typedef int8_t v8i8 __attribute__((vector_size(8)));
typedef int16_t v4i16 __attribute__((vector_size(8)));
typedef int32_t v2i32 __attribute__((vector_size(8)));

void store_v8i1(v8i8 a, v8i8 b, uint8_t *p);
void store_v4i1(v4i16 a, v4i16 b, uint8_t *p);
void store_v2i1(v2i32 a, v2i32 b, uint8_t *p);
v4i16 load_v4i1(const uint8_t *p);
uint8_t bitcast_v4i1(v4i16 a, v4i16 b);
v4i16 bitcast_i4(uint8_t m);
bool all_eq_v4i1(v4i16 a, v4i16 b);

static int failures;

static void check(const char *what, uint64_t got, uint64_t want) {
    printf("%-40s got 0x%llx, expected 0x%llx%s\n", what, (unsigned long long)got,
 (unsigned long long)want, got == want ? "" : "  <-- WRONG");
 failures += got != want;
}

static uint64_t bits(v4i16 v) {
    uint64_t u;
    memcpy(&u, &v, sizeof(u));
    return u;
}

int main(void) {
 v8i8 b8 = {1, 2, 3, 4, 5, 6, 7, 8};
    v4i16 h = {1, 2, 3, 4}, h_diff = {1, 9, 3, 9};
    v2i32 w = {5, 6};
    uint8_t byte;

    store_v8i1(b8, b8, &byte);
    check("store <8 x i1> (all true)", byte, 0xff);
 store_v4i1(h, h, &byte);
    check("store <4 x i1> (all true)", byte, 0x0f);
    store_v2i1(w, w, &byte);
    check("store <2 x i1> (all true)", byte, 0x03);

    byte = 0x05;
    check("load <4 x i1> from 0x05, sext to i16", bits(load_v4i1(&byte)), 0x0000ffff0000ffffull);

 check("bitcast <4 x i1> {1,0,1,0} to i4", bitcast_v4i1(h, h_diff), 0x5);
 check("bitcast i4 0x5 to <4 x i1>, sext to i16", bits(bitcast_i4(0x5)), 0x0000ffff0000ffffull);

    check("reduce.and <4 x i1> (all true)", all_eq_v4i1(h, h), 1);
    return failures != 0;
}
```

</details>

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to