Skip to content

Commit e62e033

Browse files
ghehglanza
authored andcommitted
Lower neon_vmaxvq_u8,neon_vmaxvq_s8, neon_vmaxv_u8 and neon_vmaxvq_s8 (#1265)
[Neon definiton](https://developer.arm.com/architectures/instruction-sets/intrinsics/#f:@navigationhierarchiessimdisa=[Neon]&q=vmaxv_s8) [OG implementation](https://github.com/llvm/clangir/blob/04d7dcfb2582753f3eccbf01ec900d60297cbf4b/clang/lib/CodeGen/CGBuiltin.cpp#L13202) Implementation in this PR is different from OG as 1. avoided code duplication by extracting out the common pattern 2. avoided using i32 as return type of the intrinsic call, so eliminated the need for casting result of the intrinsic call. This way of OG's implementation is quite unnecessary IMHO, this is MAX, not ADD or MUL. After all, using the expected type as return type of intrinsic call produces [the same ASM code](https://godbolt.org/z/3nKG7fxPb).
1 parent d8df9fd commit e62e033

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,26 @@ emitCommonNeonCallPattern0(CIRGenFunction &cgf, llvm::StringRef intrincsName,
23492349
return builder.createBitcast(res, resultType);
23502350
}
23512351

2352+
/// The function `emitCommonNeonVecAcrossCall` implements a common way
2353+
/// to implement neon intrinsic which has the following pattern:
2354+
/// 1. There is only one argument which is of vector type
2355+
/// 2. The result of the neon intrinsic is the element type of the input.
2356+
/// This type of intrinsic usually is for across operations of the input vector.
2357+
2358+
static mlir::Value emitCommonNeonVecAcrossCall(CIRGenFunction &cgf,
2359+
llvm::StringRef intrincsName,
2360+
mlir::Type eltTy,
2361+
unsigned vecLen,
2362+
const clang::CallExpr *e) {
2363+
CIRGenBuilderTy &builder = cgf.getBuilder();
2364+
mlir::Value op = cgf.emitScalarExpr(e->getArg(0));
2365+
cir::VectorType vTy =
2366+
cir::VectorType::get(&cgf.getMLIRContext(), eltTy, vecLen);
2367+
llvm::SmallVector<mlir::Value, 1> args{op};
2368+
return emitNeonCall(builder, {vTy}, args, intrincsName, eltTy,
2369+
cgf.getLoc(e->getExprLoc()));
2370+
}
2371+
23522372
mlir::Value CIRGenFunction::emitCommonNeonBuiltinExpr(
23532373
unsigned builtinID, unsigned llvmIntrinsic, unsigned altLLVMIntrinsic,
23542374
const char *nameHint, unsigned modifier, const CallExpr *e,
@@ -4274,25 +4294,29 @@ CIRGenFunction::emitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E,
42744294
llvm_unreachable("NEON::BI__builtin_neon_vaddvq_s16 NYI");
42754295
}
42764296
case NEON::BI__builtin_neon_vmaxv_u8: {
4277-
llvm_unreachable("NEON::BI__builtin_neon_vmaxv_u8 NYI");
4297+
return emitCommonNeonVecAcrossCall(*this, "aarch64.neon.umaxv", UInt8Ty, 8,
4298+
E);
42784299
}
42794300
case NEON::BI__builtin_neon_vmaxv_u16: {
42804301
llvm_unreachable("NEON::BI__builtin_neon_vmaxv_u16 NYI");
42814302
}
42824303
case NEON::BI__builtin_neon_vmaxvq_u8: {
4283-
llvm_unreachable("NEON::BI__builtin_neon_vmaxvq_u8 NYI");
4304+
return emitCommonNeonVecAcrossCall(*this, "aarch64.neon.umaxv", UInt8Ty, 16,
4305+
E);
42844306
}
42854307
case NEON::BI__builtin_neon_vmaxvq_u16: {
42864308
llvm_unreachable("NEON::BI__builtin_neon_vmaxvq_u16 NYI");
42874309
}
42884310
case NEON::BI__builtin_neon_vmaxv_s8: {
4289-
llvm_unreachable("NEON::BI__builtin_neon_vmaxv_s8 NYI");
4311+
return emitCommonNeonVecAcrossCall(*this, "aarch64.neon.smaxv", SInt8Ty, 8,
4312+
E);
42904313
}
42914314
case NEON::BI__builtin_neon_vmaxv_s16: {
42924315
llvm_unreachable("NEON::BI__builtin_neon_vmaxv_s16 NYI");
42934316
}
42944317
case NEON::BI__builtin_neon_vmaxvq_s8: {
4295-
llvm_unreachable("NEON::BI__builtin_neon_vmaxvq_s8 NYI");
4318+
return emitCommonNeonVecAcrossCall(*this, "aarch64.neon.smaxv", SInt8Ty, 16,
4319+
E);
42964320
}
42974321
case NEON::BI__builtin_neon_vmaxvq_s16: {
42984322
llvm_unreachable("NEON::BI__builtin_neon_vmaxvq_s16 NYI");

clang/test/CIR/CodeGen/AArch64/neon-misc.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,3 +1751,51 @@ uint64_t test_vaddlvq_u32(uint32x4_t a) {
17511751
// LLVM-NEXT: [[VADDLVQ_U32_I:%.*]] = call i64 @llvm.aarch64.neon.uaddlv.i64.v4i32(<4 x i32> [[A]])
17521752
// LLVM-NEXT: ret i64 [[VADDLVQ_U32_I]]
17531753
}
1754+
1755+
int8_t test_vmaxv_s8(int8x8_t a) {
1756+
return vmaxv_s8(a);
1757+
1758+
// CIR-LABEL: vmaxv_s8
1759+
// CIR: cir.llvm.intrinsic "aarch64.neon.smaxv" {{%.*}} : (!cir.vector<!s8i x 8>) -> !s8i
1760+
1761+
// LLVM-LABEL: @test_vmaxv_s8
1762+
// LLVM-SAME: (<8 x i8> [[a:%.*]])
1763+
// LLVM: [[res:%.*]] = call i8 @llvm.aarch64.neon.smaxv.i8.v8i8(<8 x i8> [[a]])
1764+
// LLVM: ret i8 [[res]]
1765+
}
1766+
1767+
int8_t test_vmaxv_u8(uint8x8_t a) {
1768+
return vmaxv_u8(a);
1769+
1770+
// CIR-LABEL: vmaxv_u8
1771+
// CIR: cir.llvm.intrinsic "aarch64.neon.umaxv" {{%.*}} : (!cir.vector<!u8i x 8>) -> !u8i
1772+
1773+
// LLVM-LABEL: @test_vmaxv_u8
1774+
// LLVM-SAME: (<8 x i8> [[a:%.*]])
1775+
// LLVM: [[res:%.*]] = call i8 @llvm.aarch64.neon.umaxv.i8.v8i8(<8 x i8> [[a]])
1776+
// LLVM: ret i8 [[res]]
1777+
}
1778+
1779+
int8_t test_vmaxvq_s8(int8x16_t a) {
1780+
return vmaxvq_s8(a);
1781+
1782+
// CIR-LABEL: vmaxvq_s8
1783+
// CIR: cir.llvm.intrinsic "aarch64.neon.smaxv" {{%.*}} : (!cir.vector<!s8i x 16>) -> !s8i
1784+
1785+
// LLVM-LABEL: @test_vmaxvq_s8
1786+
// LLVM-SAME: (<16 x i8> [[a:%.*]])
1787+
// LLVM: [[res:%.*]] = call i8 @llvm.aarch64.neon.smaxv.i8.v16i8(<16 x i8> [[a]])
1788+
// LLVM: ret i8 [[res]]
1789+
}
1790+
1791+
int8_t test_vmaxvq_u8(uint8x16_t a) {
1792+
return vmaxvq_u8(a);
1793+
1794+
// CIR-LABEL: vmaxvq_u8
1795+
// CIR: cir.llvm.intrinsic "aarch64.neon.umaxv" {{%.*}} : (!cir.vector<!u8i x 16>) -> !u8i
1796+
1797+
// LLVM-LABEL: @test_vmaxvq_u8
1798+
// LLVM-SAME: (<16 x i8> [[a:%.*]])
1799+
// LLVM: [[res:%.*]] = call i8 @llvm.aarch64.neon.umaxv.i8.v16i8(<16 x i8> [[a]])
1800+
// LLVM: ret i8 [[res]]
1801+
}

0 commit comments

Comments
 (0)