[AARCH64] Incorrect codegen for some `_lane_` intrinsics with O1,O2,O3 on big-endian aarch64
Instructions for reproduction:
Code: https://godbolt.org/z/88E7svWzP
#include <arm_neon.h>
#include <arm_fp16.h>
float16x4_t run_vcmla_lane_f16(float16_t *r_vals, float16_t *a_vals, float16_t *b_vals) {
const int32_t lane_val = 0;
float16x4_t r_val = vld1_f16(r_vals);
float16x4_t a_val = vld1_f16(a_vals);
float16x4_t b_val = vld1_f16(b_vals);
return vcmla_lane_f16(r_val, a_val, b_val, lane_val);
}
Then
Compile with clang for aarch64_be-unknown-linux-gnu with O2 and then again in debug and compare.
ASM/Output
With optimisations (incorrect)
[...]
ld1 { v0.4h }, [x2]
ld1 { v1.4h }, [x0]
ld1 { v2.4h }, [x1]
rev32 v0.8h, v0.8h
fcmla v1.4h, v2.4h, v0.h[0], #0
[...]
Without optimisations (correct)
[...]
ld1 { v0.4h }, [x10]
ld1 { v1.4h }, [x9]
ld1 { v2.4h }, [x8]
fcmla v0.4h, v1.4h, v2.4h, #0
[...]
Difference
As you can see, an extra rev32 instruction is added under the optimisations that makes the output faulty.
Suspected other faulty intrinsics when optimisations are enabled on big-endian
vcmla_lane_f16
vcmla_laneq_f16
vcmla_rot180_lane_f16
vcmla_rot180_laneq_f16
vcmla_rot270_lane_f16
vcmla_rot270_laneq_f16
vcmla_rot90_lane_f16
vcmla_rot90_laneq_f16
vcmlaq_lane_f16
vcmlaq_laneq_f16
vcmlaq_laneq_f32
vcmlaq_rot180_lane_f16
vcmlaq_rot180_laneq_f16
vcmlaq_rot180_laneq_f32
vcmlaq_rot270_lane_f16
vcmlaq_rot270_laneq_f16
vcmlaq_rot270_laneq_f32
vcmlaq_rot90_lane_f16
vcmlaq_rot90_laneq_f16
vcmlaq_rot90_laneq_f32
vdot_lane_s32
vdot_lane_u32
vdot_laneq_s32
vdot_laneq_u32
vdotq_lane_s32
vdotq_lane_u32
vdotq_laneq_s32
vdotq_laneq_u32
vsudot_lane_s32
vsudot_laneq_s32
vsudotq_lane_s32
vsudotq_laneq_s32
vusdot_lane_s32
vusdot_laneq_s32
vusdotq_lane_s32
vusdotq_laneq_s32
Edit: More minimal reproducer
@llvm/issue-subscribers-backend-aarch64
Author: Croose (CrooseGit)
Instructions for reproduction:
Code:
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <type_traits>
#include <cassert>
#include <arm_neon.h>
#include <arm_acle.h>
#include <arm_fp16.h>
#ifdef __aarch64__
std::ostream& operator<<(std::ostream& os, poly128_t value);
#endif
std::ostream& operator<<(std::ostream& os, float16_t value);
std::ostream& operator<<(std::ostream& os, uint8_t value);
int run_vdot_lane_s32() {
alignas(64) const int32_t r_val_vals[] = {
0x0,
0x800000,
0x3effffff,
0x3f000000,
0x3f000001,
0x3f7fffff,
0x3f800000,
0x3f800001,
0x3fc00000,
0x41200000,
0x7f8fffff,
0x7f800000,
0x7fd23456,
0x7fc00000,
0x7f923456,
0x7f800001,
0x123456,
0x7fffff,
0x1,
-0x7fffffff - 1,
-0x7f800000
};
alignas(64) const int8_t a_val_vals[] = {
0x0,
0x1,
0x2,
0x3,
0x4,
0x5,
0x6,
0x7,
0x8,
0x9,
0xa,
0xb,
0xc,
0xd,
0xe,
0xf,
-0x10,
-0x7f - 1,
0x3b,
-0x1,
0x0,
0x1,
0x2,
0x3,
0x4,
0x5,
0x6
};
alignas(64) const int8_t b_val_vals[] = {
0x0,
0x1,
0x2,
0x3,
0x4,
0x5,
0x6,
0x7,
0x8,
0x9,
0xa,
0xb,
0xc,
0xd,
0xe,
0xf,
-0x10,
-0x7f - 1,
0x3b,
-0x1,
0x0,
0x1,
0x2,
0x3,
0x4,
0x5,
0x6
};
const int32_t lane_val = (const int32_t)0;
for (int i=0; i<20; i++) {
int32x2_t r_val =vld1_s32(&r_val_vals[i]);
int8x8_t a_val = vld1_s8(&a_val_vals[i]);
int8x8_t b_val = vld1_s8(&b_val_vals[i]);
auto __return_value = vdot_lane_s32(r_val, a_val, b_val, lane_val);
std::cout << "Result -0-" << i+1 << ": int32x2_t(" << std::fixed << std::setprecision(150) << vget_lane_s32(__return_value, 0) << ", " << vget_lane_s32(__return_value, 1) << ")" << std::endl;
}
return 0;
}
int main(){
run_vdot_lane_s32();
}
(It may not look like the minimal reproduction, but cutting it down any further allows it to optimise its way out of the issue. This is the generated code from the rust test suite)
Then
Compile with clang for aarch64_be-unknown-linux-gnu with O2 and run.
Output
The above instructions should lead to the production of this erroneous output
Result -0-1: int32x2_t(4, 8388636)
Result -0-2: int32x2_t(8388628, 1056964667)
Result -0-3: int32x2_t(1056964651, 1056964708)
Result -0-4: int32x2_t(1056964684, 1056964757)
[...]
If you then repeat this without O2, you will get the correct output of
Result -0-1: int32x2_t(14, 8388646)
Result -0-2: int32x2_t(8388638, 1056964677)
Result -0-3: int32x2_t(1056964661, 1056964718)
Result -0-4: int32x2_t(1056964694, 1056964767)
Result -0-5: int32x2_t(1056964735, 1065353429)
[...]
Incorrect codegen:
As far as I can tell, the issue in the codegen lies in the rev32 instruction produced before the invocation of the intrinsic's underlying instruction sdot
ld1 { v0.8b }, [x8]
mov w2, #<!-- -->10 // =0xa
rev32 v1.16b, v0.16b
sdot v2.2s, v0.8b, v1.4b[0]
Removing this rev32 (and changing the final sdot operand) then compiling the assembly seems to fix the issue
ld1 { v0.8b }, [x8]
mov w2, #<!-- -->10 // =0xa
sdot v2.2s, v0.8b, v0.4b[0]
Suspected other faulty intrinsics in O2 on big-endian
vcmla_lane_f16
vcmla_laneq_f16
vcmla_rot180_lane_f16
vcmla_rot180_laneq_f16
vcmla_rot270_lane_f16
vcmla_rot270_laneq_f16
vcmla_rot90_lane_f16
vcmla_rot90_laneq_f16
vcmlaq_lane_f16
vcmlaq_laneq_f16
vcmlaq_laneq_f32
vcmlaq_rot180_lane_f16
vcmlaq_rot180_laneq_f16
vcmlaq_rot180_laneq_f32
vcmlaq_rot270_lane_f16
vcmlaq_rot270_laneq_f16
vcmlaq_rot270_laneq_f32
vcmlaq_rot90_lane_f16
vcmlaq_rot90_laneq_f16
vcmlaq_rot90_laneq_f32
vdot_lane_s32
vdot_lane_u32
vdot_laneq_s32
vdot_laneq_u32
vdotq_lane_s32
vdotq_lane_u32
vdotq_laneq_s32
vdotq_laneq_u32
vsudot_lane_s32
vsudot_laneq_s32
vsudotq_lane_s32
vsudotq_laneq_s32
vusdot_lane_s32
vusdot_laneq_s32
vusdotq_lane_s32
vusdotq_laneq_s32
I think the reproducer can be simplified to https://godbolt.org/z/ET94szT9n
I have just noticed this issue is also apparent in O1 and O3 aswell as O2
I think the reproducer can be simplified to https://godbolt.org/z/ET94szT9n
Yes, thank you. I was having problems trying to cut down the example.
I reproduced the indexed dot-product problem on Clang/LLVM cf44dd9e317e07bbf1b82635e882f7827e24f8eb with -march=armv8.2-a+dotprod, statically linked big-endian ELF under qemu-aarch64_be 8.2.2. Little and big endian were each tested at -O0/-O1/-O2/-O3/-Os. The reproducer initializes g8[i] = (uint8_t)(0x11*i+0x0d), then calls:
uint8x16_t x = vld1q_u8(g8);
uint8x8_t y = vld1_u8(g8 + 16);
uint32x2_t d = vdot_lane_u32(vdup_n_u32(3), vget_low_u8(x), y, 1);
/* Observe with vget_lane_u32(d, 0/1), not the endian-dependent d[i]. */
ACLE's lane 1 selects g8[20..23] = {0x61,0x72,0x83,0x94}. The scalar products give d[ACLE lane 0]=0x4f59 and d[ACLE lane 1]=0xd181. BE -O0 and LE at every level return those values. For this program shape, BE -O1/-O2/-O3/-Os use {0x94,0x83,0x72,0x61} and return 0x440f,0xc637. The same failure appears with vdot_lane_s32 and vdotq_lane_u32; the latter's four expected ACLE lanes are 0x4f5f,0xd187,0x153af,0x141d7, while optimized BE returns 0x4415,0xc63d,0x14865,0x1698d. A larger probe that also observes intermediate vectors sometimes avoids the bad indexed selection at -O2 and above, so the claim is about this reproducible shape rather than every use of these intrinsics.
The optimized IR still represents the correct RHS: it duplicates the selected <2 x i32> element and bitcasts the result to <8 x i8>. Under the BE data layout, opt -passes=instcombine folds the first and fourth bytes of i32 0x61728394 to 0x61 and 0x94. The BE assembly instead loads the RHS as .2s and feeds its .4b[1] sublane to UDOT, which presents the four bytes in reverse order. BaseSIMDThreeSameVectorIndexS in AArch64InstrFormats.td matches bitconvert(AArch64duplane32(...)) without a big-endian condition, and UDOTlane/SDOTlane instantiate that class. This is the same indexed-dot source path targeted by the nvcast change in the linked 474edcd commit. I have not built that proposed change.
Ordinary non-indexed vdot_u32/vdot_s32 are correct at -O1 and above in this test and select the separate SIMDThreeSameVectorDot class. Their BE -O0 failure comes earlier, from a high-half subvector stack store, so it is not evidence for the indexed-dot fix. Ordinary vdotq_u32 and vdotq_s32 were correct at every tested level.
Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.
Assisted-by: Grok