clang 24.0.0git
CIRGenBuiltinAArch64.cpp
Go to the documentation of this file.
1//===---- CIRGenBuiltinAArch64.cpp - Emit CIR for AArch64 builtins --------===//
2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3// See https://llvm.org/LICENSE.txt for license information.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7//
8// This contains code to emit ARM64 Builtin calls as CIR or a function call
9// to be later resolved.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIRGenBuilder.h"
14#include "CIRGenFunction.h"
20
21// TODO(cir): once all builtins are covered, decide whether we still
22// need to use LLVM intrinsics or if there's a better approach to follow. Right
23// now the intrinsics are reused to make it convenient to encode all thousands
24// of them and passing down to LLVM lowering.
25#include "llvm/IR/Intrinsics.h"
26#include "llvm/IR/IntrinsicsAArch64.h"
27
28#include "mlir/IR/BuiltinTypes.h"
29#include "mlir/IR/Value.h"
32
33using namespace clang;
34using namespace clang::CIRGen;
35using namespace llvm;
36using namespace clang::aarch64;
37
38// Generate vscale * scalingFactor
39static mlir::Value genVscaleTimesFactor(mlir::Location loc,
40 CIRGenBuilderTy &builder,
41 mlir::Type cirTy,
42 int32_t scalingFactor) {
43 mlir::Value vscale = builder.emitIntrinsicCallOp(loc, "vscale", cirTy);
44 return builder.createNUWAMul(loc, vscale,
45 builder.getUInt64(scalingFactor, loc));
46}
47
48#define SVEMAP1(NameBase, LLVMIntrinsic, TypeModifier) \
49 {SVE::BI__builtin_sve_##NameBase, Intrinsic::LLVMIntrinsic, TypeModifier}
50
51#define SVEMAP2(NameBase, TypeModifier) \
52 {SVE::BI__builtin_sve_##NameBase, 0, TypeModifier}
54#define GET_SVE_LLVM_INTRINSIC_MAP
55#include "clang/Basic/arm_sve_builtin_cg.inc"
56#undef GET_SVE_LLVM_INTRINSIC_MAP
57};
58
62
63// Check if Builtin `builtinId` is present in `intrinsicMap`. If yes, returns
64// the corresponding info struct.
65template <typename IntrinsicInfo>
66static const IntrinsicInfo *
68 unsigned builtinID, bool &mapProvenSorted) {
69
70#ifndef NDEBUG
71 if (!mapProvenSorted) {
72 assert(llvm::is_sorted(intrinsicMap));
73 mapProvenSorted = true;
74 }
75#endif
76
77 const IntrinsicInfo *info = llvm::lower_bound(intrinsicMap, builtinID);
78
79 if (info != intrinsicMap.end() && info->BuiltinID == builtinID)
80 return info;
81
82 return nullptr;
83}
84
85//===----------------------------------------------------------------------===//
86// Generic helpers
87//===----------------------------------------------------------------------===//
88static llvm::StringRef getLLVMIntrNameNoPrefix(llvm::Intrinsic::ID intrID) {
89 llvm::StringRef llvmIntrName = llvm::Intrinsic::getBaseName(intrID);
90 assert(llvmIntrName.starts_with("llvm.") && "Not an LLVM intrinsic!");
91 return llvmIntrName.drop_front(/*strlen("llvm.")=*/5);
92}
93
94static cir::VectorType getFloatNeonType(CIRGenFunction &cgf,
95 NeonTypeFlags intTypeFlags) {
96 int isQuad = intTypeFlags.isQuad();
97 switch (intTypeFlags.getEltType()) {
99 return cir::VectorType::getFixed(cgf.fP16Ty, (4ULL << isQuad));
101 return cir::VectorType::getFixed(cgf.floatTy, (2ULL << isQuad));
103 return cir::VectorType::getFixed(cgf.doubleTy, (1ULL << isQuad));
104 default:
105 llvm_unreachable("Type can't be converted to floating-point!");
106 }
107}
108
109static int64_t getIntValueFromConstOp(mlir::Value val) {
110 return val.getDefiningOp<cir::ConstantOp>().getIntValue().getSExtValue();
111}
112
113/// Build a constant shift amount vector of `vecTy` to shift a vector
114/// Here `shiftVal` is a constant integer that will be broadcast into a
115/// a const vector of `vecTy` which is the return value of this function
116/// If `neg` is true, the shift amount is negated before splatting (used
117/// when encoding a right shift as a left shift by a negative amount for
118/// intrinsics like aarch64.neon.{s,u}rshl).
119static mlir::Value emitNeonShiftVector(CIRGenBuilderTy &builder,
120 mlir::Value shiftVal,
121 cir::VectorType vecTy,
122 mlir::Location loc, bool neg) {
123 if (neg) {
124 int64_t shiftAmt = -getIntValueFromConstOp(shiftVal);
125 shiftVal = builder.getConstantInt(loc, vecTy.getElementType(), shiftAmt);
126 }
127 mlir::Type eltTy = vecTy.getElementType();
128 if (shiftVal.getType() != eltTy) {
129 shiftVal = builder.createIntCast(shiftVal, eltTy);
130 }
131 return cir::VecSplatOp::create(builder, loc, vecTy, shiftVal);
132}
133
134// TODO(cir): Remove `cgm` from the list of arguments once all NYI(s) are gone.
135template <typename Operation>
136static mlir::Value
140 std::optional<llvm::StringRef> intrinsicName,
141 mlir::Type funcResTy, mlir::Location loc,
142 bool isConstrainedFPIntrinsic = false, unsigned shift = 0,
143 bool rightshift = false) {
144 // TODO(cir): Consider removing the following unreachable when we have
145 // emitConstrainedFPCall feature implemented
147 if (isConstrainedFPIntrinsic)
148 cgm.errorNYI(loc, std::string("constrained FP intrinsic"));
149
150 for (unsigned j = 0; j < argTypes.size(); ++j) {
151 if (isConstrainedFPIntrinsic) {
153 }
154 if (shift > 0 && shift == j) {
155 args[j] = emitNeonShiftVector(builder, args[j],
156 mlir::cast<cir::VectorType>(argTypes[j]),
157 loc, rightshift);
158 } else {
159 args[j] = builder.createBitcast(args[j], argTypes[j]);
160 }
161 }
162 if (isConstrainedFPIntrinsic) {
164 return nullptr;
165 }
166 if constexpr (std::is_same_v<Operation, cir::LLVMIntrinsicCallOp>) {
167 return Operation::create(builder, loc,
168 builder.getStringAttr(intrinsicName.value()),
169 funcResTy, args)
170 .getResult();
171 }
172 if constexpr (std::is_same_v<Operation, cir::FMAOp>) {
173 assert(args.size() == 3 && "fma expects three operands");
174 return Operation::create(builder, loc, funcResTy, args[0], args[1], args[2],
175 builder.getConstrainedFPAttr(),
176 builder.getFastMathFlagsAttr())
177 .getResult();
178 }
179 if constexpr (std::is_same_v<Operation, cir::SqrtOp>) {
180 assert(args.size() == 1 && "sqrt expects one operand");
181 return Operation::create(builder, loc, funcResTy, args[0],
182 builder.getConstrainedFPAttr())
183 .getResult();
184 }
185 if constexpr (!std::is_same_v<Operation, cir::LLVMIntrinsicCallOp> &&
186 !std::is_same_v<Operation, cir::FMAOp> &&
187 !std::is_same_v<Operation, cir::SqrtOp>)
188 return Operation::create(
189 builder, loc, mlir::TypeRange{funcResTy}, args,
190 cir::getDefaultProperties<Operation>(builder.getContext()))
191 .getResult();
192}
193
194// TODO(cir): Remove `cgm` from the list of arguments once all NYI(s) are gone.
195static mlir::Value emitNeonCall(CIRGenModule &cgm, CIRGenBuilderTy &builder,
198 llvm::StringRef intrinsicName,
199 mlir::Type funcResTy, mlir::Location loc,
200 bool isConstrainedFPIntrinsic = false,
201 unsigned shift = 0, bool rightshift = false) {
203 cgm, builder, std::move(argTypes), args, intrinsicName, funcResTy, loc,
204 isConstrainedFPIntrinsic, shift, rightshift);
205}
206
207// Computes the input vector type for a NEON pairwise widening operation (e.g.
208// vpaddl/vpadal). Given a result vector type, it derives the corresponding
209// input type by halving the element bit width and doubling the number of lanes,
210// while setting the signedness based on usgn.
211static cir::VectorType getNeonPairwiseWidenInputType(cir::VectorType resType,
212 bool usgn) {
213 mlir::Type elemTy = resType.getElementType();
214 uint64_t resLanes = resType.getFixedSize();
215 auto intTy = mlir::dyn_cast<cir::IntType>(elemTy);
216 assert(intTy && "vpaddl result type must be an integer vector");
217
218 unsigned resWidth = intTy.getWidth();
219 assert((resWidth == 16 || resWidth == 32 || resWidth == 64) &&
220 "unexpected vpaddl result element width");
221
222 unsigned argWidth = resWidth / 2;
223 unsigned argLanes = resLanes * 2;
224 cir::VectorType result = cir::VectorType::getFixed(
225 cir::IntType::get(resType.getContext(), argWidth, /* is_signed */ !usgn),
226 argLanes);
227 return result;
228}
229
230// Derive the LLVM intrinsic's per-operand argument types and its result
231// type for use when emitting the intrinsic call.
232//
233// `modifier` is the TypeModifier bitmask from `ARMNeonVectorIntrinsicInfo`
234// (callers pass `info.TypeModifier`; see AArch64CodeGenUtils.h). It encodes
235// how the intrinsic's argument and return types relate to the builtin's
236// scalar types. For SISD builtins the key flags are:
237// - VectorizeArgTypes: wrap each arg type into a fixed-width vector
238// - Use64BitVectors / Use128BitVectors: choose the vector width
239// (when neither is set the vector has 1 element)
240// - AddRetType / VectorizeRetType: analogous flags for the return type
241//
242// ARM.cpp lets LLVM resolve the intrinsic's signature (via
243// `CGM.getIntrinsic`) and then walks the resolved Function* formal
244// parameter types. CIR has no LLVMContext here, so we derive the same
245// argument/result types directly from the Clang operand types.
246static std::pair<mlir::Type, llvm::SmallVector<mlir::Type>>
248 mlir::Type arg0Ty, mlir::Type resultTy,
250 unsigned iceArguments) {
251 int vectorSize = 0;
252 if (modifier & Use64BitVectors)
253 vectorSize = 64;
254 else if (modifier & Use128BitVectors)
255 vectorSize = 128;
256
257 auto wrapAsVector = [&](mlir::Type ty) -> cir::VectorType {
258 unsigned bits = cgf.cgm.getDataLayout().getTypeSizeInBits(ty);
259 unsigned elts = vectorSize ? vectorSize / bits : 1;
260 return cir::VectorType::getFixed(ty, elts);
261 };
262
263 // Determine the vectorized data type.
264 cir::VectorType vecArgTy;
265 if (modifier & VectorizeArgTypes)
266 vecArgTy = wrapAsVector(arg0Ty);
267
268 // Determine the intrinsic result type: `VectorizeRetType` returns a
269 // vector; otherwise, if data args are vectorized and `AddRetType` is
270 // unset, use a vector return with the same shape as those args.
271 mlir::Type funcResTy = resultTy;
272 if (modifier & VectorizeRetType)
273 funcResTy = wrapAsVector(resultTy);
274 else if (vecArgTy && !(modifier & AddRetType))
275 funcResTy = wrapAsVector(resultTy);
276
277 // AdvSIMD_2Arg_Scalar_Narrow_Intrinsic in IntrinsicsAArch64.td models
278 // operand 0 as LLVMExtendedType<0>: the result vector's lane count with
279 // double-width elements. Reconstruct that LLVM operand type from the Clang
280 // builtin's scalar argument type.
281 if (modifier & ArgAsWidenedRetType) {
282 auto resVecTy = mlir::dyn_cast<cir::VectorType>(funcResTy);
283 assert(resVecTy &&
284 "SISD LLVM argument reconstruction requires a vector result");
285 vecArgTy =
286 cir::VectorType::get(arg0Ty, resVecTy.getSize(), resVecTy.getKind());
287 }
288
289 // True if `ty` is arg0's type, or an integer of the same width that only
290 // differs in signedness. vsqadd/vuqadd mix the two on purpose: vsqaddb_u8
291 // takes a uint8_t and an int8_t, but both become the same vector type.
292 auto matchesArg0Ty = [&](mlir::Type ty) {
293 if (ty == arg0Ty)
294 return true;
295 auto intTy = mlir::dyn_cast<cir::IntType>(ty);
296 auto arg0IntTy = mlir::dyn_cast<cir::IntType>(arg0Ty);
297 return intTy && arg0IntTy && intTy.getWidth() == arg0IntTy.getWidth();
298 };
299
300 // `vecArgTy` is populated by `VectorizeArgTypes` or
301 // `ArgAsWidenedRetType`. When set, wrap every non-immediate data operand
302 // that has the same scalar type as arg0. Checking the ICE bitmap prevents
303 // an i32 immediate from being vectorized when it has the same type as a
304 // data operand (e.g. vqshrns_n_s32). `VectorizeImmArg` marks the entries
305 // whose trailing immediate must be widened as well.
306 const bool widenTrailingImm = vecArgTy && ops.size() == 2 &&
307 (modifier & VectorizeImmArg) &&
308 (iceArguments & (1U << 1));
310 argTypes.reserve(ops.size());
311 for (unsigned i = 0, e = ops.size(); i != e; ++i) {
312 bool isImmediate = iceArguments & (1U << i);
313 bool asVector =
314 vecArgTy && ((!isImmediate && matchesArg0Ty(ops[i].getType())) ||
315 (widenTrailingImm && i == 1));
316 argTypes.push_back(asVector ? mlir::Type(vecArgTy) : ops[i].getType());
317 }
318
319 return {funcResTy, std::move(argTypes)};
320}
321
322// Source-operand vector type for a common NEON binary intrinsic: the
323// double-element-width form of `vTy` when `ArgAsWidenedRetType` is set (e.g.
324// vraddhn), otherwise `vTy`.
325static cir::VectorType deriveNeonBinaryArgType(CIRGenBuilderTy &builder,
326 unsigned modifier,
327 cir::VectorType vTy) {
328 if (modifier & ArgAsWidenedRetType)
330 /*isExtended=*/true);
331 return vTy;
332}
333
334/// Create a vector from an input scalar argument, usually for a NEON SISD
335/// intrinsic call. Insert the argument into lane 0 of a poison vector.
336static void vecExtendIntValue(CIRGenFunction &cgf, cir::VectorType argVTy,
337 mlir::Value &arg, mlir::Location loc) {
338 CIRGenBuilderTy &builder = cgf.getBuilder();
339 // TODO: Support floating-point scalar arguments when a SISD intrinsic
340 // requires scalar-to-vector adaptation; current users are integer-only.
341 cir::IntType eltTy = mlir::dyn_cast<cir::IntType>(argVTy.getElementType());
342 assert(mlir::isa<cir::IntType>(arg.getType()) && eltTy);
343 // Cast the scalar data operand to the vector element type before inserting
344 // it into lane 0.
345 arg = builder.createIntCast(arg, eltTy);
346 mlir::Value zero = builder.getConstInt(loc, cgf.sizeTy, 0);
347 mlir::Value poison = builder.getConstant(loc, cir::PoisonAttr::get(argVTy));
348 arg = cir::VecInsertOp::create(builder, loc, poison, arg, zero);
349}
350
351static mlir::Value
353 const ARMNeonVectorIntrinsicInfo &info,
355 const CallExpr *expr, unsigned iceArguments) {
356 assert(info.LLVMIntrinsic && "Generic code assumes a valid intrinsic");
357
358 switch (info.BuiltinID) {
359 case NEON::BI__builtin_neon_vcled_s64:
360 case NEON::BI__builtin_neon_vcled_u64:
361 case NEON::BI__builtin_neon_vcles_f32:
362 case NEON::BI__builtin_neon_vcled_f64:
363 case NEON::BI__builtin_neon_vcltd_s64:
364 case NEON::BI__builtin_neon_vcltd_u64:
365 case NEON::BI__builtin_neon_vclts_f32:
366 case NEON::BI__builtin_neon_vcltd_f64:
367 case NEON::BI__builtin_neon_vcales_f32:
368 case NEON::BI__builtin_neon_vcaled_f64:
369 case NEON::BI__builtin_neon_vcalts_f32:
370 case NEON::BI__builtin_neon_vcaltd_f64:
371 cgf.cgm.errorNYI(expr->getSourceRange(),
372 std::string("unimplemented AArch64 builtin call: ") +
373 cgf.getContext().BuiltinInfo.getName(info.BuiltinID));
374 break;
375 }
376
377 unsigned intr = info.LLVMIntrinsic;
378
379 // Use fptosi.sat/fptoui.sat unless under strict FP.
381 if (intr == Intrinsic::aarch64_neon_fcvtzs)
382 intr = Intrinsic::fptosi_sat;
383 else if (intr == Intrinsic::aarch64_neon_fcvtzu)
384 intr = Intrinsic::fptoui_sat;
385
386 llvm::StringRef llvmIntrName =
387 getLLVMIntrNameNoPrefix(static_cast<llvm::Intrinsic::ID>(intr));
388 mlir::Location loc = cgf.getLoc(expr->getExprLoc());
389
390 // The switch stmt is intended to help catch NYI cases and will be removed
391 // once the CIR implementation is complete. Avoid adding specialized
392 // code in cases - that should only be required for a handful of examples.
393 switch (info.BuiltinID) {
394 default:
395 cgf.cgm.errorNYI(expr->getSourceRange(),
396 std::string("unimplemented AArch64 builtin call: ") +
397 cgf.getContext().BuiltinInfo.getName(info.BuiltinID));
398 break;
399 case NEON::BI__builtin_neon_vminv_s8:
400 case NEON::BI__builtin_neon_vminvq_s8:
401 case NEON::BI__builtin_neon_vminv_s16:
402 case NEON::BI__builtin_neon_vminvq_s16:
403 case NEON::BI__builtin_neon_vminv_s32:
404 case NEON::BI__builtin_neon_vminvq_s32:
405 case NEON::BI__builtin_neon_vminv_u8:
406 case NEON::BI__builtin_neon_vminvq_u8:
407 case NEON::BI__builtin_neon_vminv_u16:
408 case NEON::BI__builtin_neon_vminvq_u16:
409 case NEON::BI__builtin_neon_vminv_u32:
410 case NEON::BI__builtin_neon_vminvq_u32:
411 case NEON::BI__builtin_neon_vminv_f32:
412 case NEON::BI__builtin_neon_vminvq_f32:
413 case NEON::BI__builtin_neon_vminvq_f64:
414 case NEON::BI__builtin_neon_vminnmv_f32:
415 case NEON::BI__builtin_neon_vminnmvq_f32:
416 case NEON::BI__builtin_neon_vminnmvq_f64:
417 case NEON::BI__builtin_neon_vabdd_f64:
418 case NEON::BI__builtin_neon_vabds_f32:
419 case NEON::BI__builtin_neon_vshld_s64:
420 case NEON::BI__builtin_neon_vshld_u64:
421 case NEON::BI__builtin_neon_vpmins_f32:
422 case NEON::BI__builtin_neon_vpminqd_f64:
423 case NEON::BI__builtin_neon_vpminnms_f32:
424 case NEON::BI__builtin_neon_vpminnmqd_f64:
425 case NEON::BI__builtin_neon_vcvtd_s32_f64:
426 case NEON::BI__builtin_neon_vcvtd_s64_f64:
427 case NEON::BI__builtin_neon_vcvtd_u64_f64:
428 case NEON::BI__builtin_neon_vcvtd_u32_f64:
429 case NEON::BI__builtin_neon_vcvts_n_f32_s32:
430 case NEON::BI__builtin_neon_vcvts_n_f32_u32:
431 case NEON::BI__builtin_neon_vcvts_n_s32_f32:
432 case NEON::BI__builtin_neon_vcvts_n_u32_f32:
433 case NEON::BI__builtin_neon_vcvtd_n_f64_s64:
434 case NEON::BI__builtin_neon_vcvtd_n_f64_u64:
435 case NEON::BI__builtin_neon_vcvtd_n_s64_f64:
436 case NEON::BI__builtin_neon_vcvtd_n_u64_f64:
437 case NEON::BI__builtin_neon_vcvts_s32_f32:
438 case NEON::BI__builtin_neon_vcvts_s64_f32:
439 case NEON::BI__builtin_neon_vcvts_u32_f32:
440 case NEON::BI__builtin_neon_vcvts_u64_f32:
441 case NEON::BI__builtin_neon_vcvtns_s32_f32:
442 case NEON::BI__builtin_neon_vcvtns_s64_f32:
443 case NEON::BI__builtin_neon_vcvtns_u32_f32:
444 case NEON::BI__builtin_neon_vcvtns_u64_f32:
445 case NEON::BI__builtin_neon_vcvtnd_s32_f64:
446 case NEON::BI__builtin_neon_vcvtnd_s64_f64:
447 case NEON::BI__builtin_neon_vcvtnd_u32_f64:
448 case NEON::BI__builtin_neon_vcvtnd_u64_f64:
449 case NEON::BI__builtin_neon_vcvtms_s32_f32:
450 case NEON::BI__builtin_neon_vcvtms_s64_f32:
451 case NEON::BI__builtin_neon_vcvtms_u32_f32:
452 case NEON::BI__builtin_neon_vcvtms_u64_f32:
453 case NEON::BI__builtin_neon_vcvtmd_s32_f64:
454 case NEON::BI__builtin_neon_vcvtmd_u32_f64:
455 case NEON::BI__builtin_neon_vcvtmd_s64_f64:
456 case NEON::BI__builtin_neon_vcvtmd_u64_f64:
457 case NEON::BI__builtin_neon_vcvtps_s32_f32:
458 case NEON::BI__builtin_neon_vcvtps_s64_f32:
459 case NEON::BI__builtin_neon_vcvtps_u32_f32:
460 case NEON::BI__builtin_neon_vcvtps_u64_f32:
461 case NEON::BI__builtin_neon_vcvtpd_s32_f64:
462 case NEON::BI__builtin_neon_vcvtpd_u32_f64:
463 case NEON::BI__builtin_neon_vcvtpd_s64_f64:
464 case NEON::BI__builtin_neon_vcvtpd_u64_f64:
465 case NEON::BI__builtin_neon_vcvtas_s32_f32:
466 case NEON::BI__builtin_neon_vcvtas_s64_f32:
467 case NEON::BI__builtin_neon_vcvtas_u32_f32:
468 case NEON::BI__builtin_neon_vcvtas_u64_f32:
469 case NEON::BI__builtin_neon_vcvtad_s32_f64:
470 case NEON::BI__builtin_neon_vcvtad_u32_f64:
471 case NEON::BI__builtin_neon_vcvtad_s64_f64:
472 case NEON::BI__builtin_neon_vcvtad_u64_f64:
473 case NEON::BI__builtin_neon_vcvtxd_f32_f64:
474 case NEON::BI__builtin_neon_vaddlv_s32:
475 case NEON::BI__builtin_neon_vaddlv_u32:
476 case NEON::BI__builtin_neon_vaddlvq_s32:
477 case NEON::BI__builtin_neon_vaddlvq_u32:
478 case NEON::BI__builtin_neon_vaddv_s8:
479 case NEON::BI__builtin_neon_vaddv_s16:
480 case NEON::BI__builtin_neon_vaddv_s32:
481 case NEON::BI__builtin_neon_vaddv_u8:
482 case NEON::BI__builtin_neon_vaddv_u16:
483 case NEON::BI__builtin_neon_vaddv_u32:
484 case NEON::BI__builtin_neon_vaddv_f32:
485 case NEON::BI__builtin_neon_vaddvq_s8:
486 case NEON::BI__builtin_neon_vaddvq_s16:
487 case NEON::BI__builtin_neon_vaddvq_s32:
488 case NEON::BI__builtin_neon_vaddvq_s64:
489 case NEON::BI__builtin_neon_vaddvq_u8:
490 case NEON::BI__builtin_neon_vaddvq_u16:
491 case NEON::BI__builtin_neon_vaddvq_u32:
492 case NEON::BI__builtin_neon_vaddvq_u64:
493 case NEON::BI__builtin_neon_vaddvq_f32:
494 case NEON::BI__builtin_neon_vaddvq_f64:
495 case NEON::BI__builtin_neon_vabdh_f16:
496 case NEON::BI__builtin_neon_vrecpeh_f16:
497 case NEON::BI__builtin_neon_vrecpxh_f16:
498 case NEON::BI__builtin_neon_vrsqrteh_f16:
499 case NEON::BI__builtin_neon_vrsqrtsh_f16:
500 case NEON::BI__builtin_neon_vmaxv_s8:
501 case NEON::BI__builtin_neon_vmaxvq_s8:
502 case NEON::BI__builtin_neon_vmaxv_s16:
503 case NEON::BI__builtin_neon_vmaxvq_s16:
504 case NEON::BI__builtin_neon_vmaxv_s32:
505 case NEON::BI__builtin_neon_vmaxvq_s32:
506 case NEON::BI__builtin_neon_vmaxv_u8:
507 case NEON::BI__builtin_neon_vmaxvq_u8:
508 case NEON::BI__builtin_neon_vmaxv_u16:
509 case NEON::BI__builtin_neon_vmaxvq_u16:
510 case NEON::BI__builtin_neon_vmaxv_u32:
511 case NEON::BI__builtin_neon_vmaxvq_u32:
512 case NEON::BI__builtin_neon_vmaxv_f32:
513 case NEON::BI__builtin_neon_vmaxvq_f32:
514 case NEON::BI__builtin_neon_vmaxvq_f64:
515 case NEON::BI__builtin_neon_vqrshrund_n_s64:
516 case NEON::BI__builtin_neon_vqrshrnd_n_s64:
517 case NEON::BI__builtin_neon_vqrshrnd_n_u64:
518 case NEON::BI__builtin_neon_vqshrund_n_s64:
519 case NEON::BI__builtin_neon_vqshrnd_n_s64:
520 case NEON::BI__builtin_neon_vqshrnd_n_u64:
521 case NEON::BI__builtin_neon_vmaxnmv_f32:
522 case NEON::BI__builtin_neon_vmaxnmvq_f32:
523 case NEON::BI__builtin_neon_vmaxnmvq_f64:
524 case NEON::BI__builtin_neon_vsrid_n_s64:
525 case NEON::BI__builtin_neon_vsrid_n_u64:
526 case NEON::BI__builtin_neon_vslid_n_s64:
527 case NEON::BI__builtin_neon_vslid_n_u64:
528 case NEON::BI__builtin_neon_vpmaxs_f32:
529 case NEON::BI__builtin_neon_vpmaxqd_f64:
530 case NEON::BI__builtin_neon_vpmaxnms_f32:
531 case NEON::BI__builtin_neon_vpmaxnmqd_f64:
532 case NEON::BI__builtin_neon_vmulxh_f16:
533 case NEON::BI__builtin_neon_vqshrunh_n_s16:
534 case NEON::BI__builtin_neon_vqshruns_n_s32:
535 case NEON::BI__builtin_neon_vqshrnh_n_s16:
536 case NEON::BI__builtin_neon_vqshrns_n_s32:
537 case NEON::BI__builtin_neon_vqshrnh_n_u16:
538 case NEON::BI__builtin_neon_vqshrns_n_u32:
539 case NEON::BI__builtin_neon_vqsubb_s8:
540 case NEON::BI__builtin_neon_vqsubb_u8:
541 case NEON::BI__builtin_neon_vqsubh_s16:
542 case NEON::BI__builtin_neon_vqsubh_u16:
543 case NEON::BI__builtin_neon_vqsubs_s32:
544 case NEON::BI__builtin_neon_vqsubs_u32:
545 case NEON::BI__builtin_neon_vqsubd_s64:
546 case NEON::BI__builtin_neon_vqsubd_u64:
547 case NEON::BI__builtin_neon_vqaddb_s8:
548 case NEON::BI__builtin_neon_vqaddb_u8:
549 case NEON::BI__builtin_neon_vqaddh_s16:
550 case NEON::BI__builtin_neon_vqaddh_u16:
551 case NEON::BI__builtin_neon_vqadds_s32:
552 case NEON::BI__builtin_neon_vqadds_u32:
553 case NEON::BI__builtin_neon_vqaddd_s64:
554 case NEON::BI__builtin_neon_vqaddd_u64:
555 case NEON::BI__builtin_neon_vsqaddb_u8:
556 case NEON::BI__builtin_neon_vsqaddh_u16:
557 case NEON::BI__builtin_neon_vsqadds_u32:
558 case NEON::BI__builtin_neon_vsqaddd_u64:
559 case NEON::BI__builtin_neon_vuqaddb_s8:
560 case NEON::BI__builtin_neon_vuqaddh_s16:
561 case NEON::BI__builtin_neon_vuqadds_s32:
562 case NEON::BI__builtin_neon_vuqaddd_s64:
563 case NEON::BI__builtin_neon_vqrshrns_n_s32:
564 case NEON::BI__builtin_neon_vqrshrns_n_u32:
565 case NEON::BI__builtin_neon_vqrshrnh_n_s16:
566 case NEON::BI__builtin_neon_vqrshrnh_n_u16:
567 case NEON::BI__builtin_neon_vqrshruns_n_s32:
568 case NEON::BI__builtin_neon_vqrshrunh_n_s16:
569 case NEON::BI__builtin_neon_vqshlb_s8:
570 case NEON::BI__builtin_neon_vqshlb_u8:
571 case NEON::BI__builtin_neon_vqshlh_s16:
572 case NEON::BI__builtin_neon_vqshlh_u16:
573 case NEON::BI__builtin_neon_vqshls_s32:
574 case NEON::BI__builtin_neon_vqshls_u32:
575 case NEON::BI__builtin_neon_vqshld_s64:
576 case NEON::BI__builtin_neon_vqshld_u64:
577 case NEON::BI__builtin_neon_vqshlb_n_s8:
578 case NEON::BI__builtin_neon_vqshlb_n_u8:
579 case NEON::BI__builtin_neon_vqshlh_n_s16:
580 case NEON::BI__builtin_neon_vqshlh_n_u16:
581 case NEON::BI__builtin_neon_vqshls_n_s32:
582 case NEON::BI__builtin_neon_vqshls_n_u32:
583 case NEON::BI__builtin_neon_vqshlub_n_s8:
584 case NEON::BI__builtin_neon_vqshluh_n_s16:
585 case NEON::BI__builtin_neon_vqshlus_n_s32:
586 break;
587 }
588
589 CIRGenBuilderTy &builder = cgf.getBuilder();
590 mlir::Type arg0Ty = cgf.convertType(expr->getArg(0)->getType());
591 mlir::Type resultTy = cgf.convertType(expr->getType());
592
593 // Derive per-operand argument types and the result type from the
594 // TypeModifier flags. `emitNeonCall` takes care of per-operand
595 // bitcasts to `argTypes`.
596 auto [funcResTy, argTypes] = deriveNeonSISDIntrinsicOperandTypes(
597 cgf, info.TypeModifier, arg0Ty, resultTy, ops, iceArguments);
598
599 assert(argTypes.size() == ops.size());
600 for (unsigned i = 0, e = ops.size(); i != e; ++i) {
601 if (cgf.cgm.getDataLayout().getTypeSizeInBits(ops[i].getType()) ==
602 cgf.cgm.getDataLayout().getTypeSizeInBits(argTypes[i]))
603 continue;
604
605 auto argVecTy = mlir::dyn_cast<cir::VectorType>(argTypes[i]);
606 assert(argVecTy && !mlir::isa<cir::VectorType>(ops[i].getType()) &&
607 "expecting vector LLVM intrinsic type and scalar Clang builtin "
608 "type");
609
610 vecExtendIntValue(cgf, argVecTy, ops[i], loc);
611 }
612
613 mlir::Value result = emitNeonCall(cgf.cgm, builder, std::move(argTypes), ops,
614 llvmIntrName, funcResTy, loc);
615
616 if (cgf.cgm.getDataLayout().getTypeSizeInBits(resultTy) <
617 cgf.cgm.getDataLayout().getTypeSizeInBits(funcResTy)) {
618
619 assert(mlir::isa<cir::VectorType>(result.getType()));
620 return cir::VecExtractOp::create(builder, loc, result,
621 builder.getConstInt(loc, cgf.sizeTy, 0));
622 }
623
624 return builder.createBitcast(loc, result, resultTy);
625}
626
627//===----------------------------------------------------------------------===//
628// Emit-helpers
629//===----------------------------------------------------------------------===//
630template <typename CmpKindTy>
631static mlir::Value
633 mlir::Location loc, mlir::Value src,
634 mlir::Type retTy, const CmpKindTy kind) {
635
636 bool scalarCmp = !isa<cir::VectorType>(src.getType());
637 if (!scalarCmp) {
638 assert(!cast<cir::VectorType>(retTy).isScalable() &&
639 "This is only intended for fixed-width vectors");
640 // Vector types are cast to i8 vectors. Recover original type.
641 src = builder.createBitcast(src, retTy);
642 }
643
644 mlir::Value zero = builder.getNullValue(src.getType(), loc);
645
646 if (!scalarCmp) {
647 if constexpr (std::is_same<CmpKindTy, cir::FCmpOpKind>::value)
648 return builder.createVecFCompare(loc, kind, src, zero);
649 else
650 return builder.createVecCompare(loc, kind, src, zero);
651 }
652
653 // FIXME FCmpOpKind is only supported for vector comparisons now, modify the
654 // if when scalar comparisons are supported too;
655 if constexpr (std::is_same<CmpKindTy, cir::CmpOpKind>::value) {
656 // For scalars, cast !cir.bool to !cir.int<s, 1> so that the compare
657 // result is sign- rather zero-extended when casting to the output
658 // retType.
659 mlir::Value cmp = builder.createCast(
660 loc, cir::CastKind::bool_to_int,
661 builder.createCompare(loc, kind, src, zero), builder.getSIntNTy(1));
662
663 return builder.createCast(loc, cir::CastKind::integral, cmp, retTy);
664 }
665
666 llvm_unreachable("Should not end up here");
667 return {};
668}
669
670static cir::VectorType getNeonType(CIRGenFunction *cgf, NeonTypeFlags typeFlags,
671 bool hasLegalHalfType = true,
672 bool v1Ty = false,
673 bool allowBFloatArgsAndRet = true) {
674 int isQuad = typeFlags.isQuad();
675 switch (typeFlags.getEltType()) {
678 return cir::VectorType::getFixed(typeFlags.isUnsigned() ? cgf->uInt8Ty
679 : cgf->sInt8Ty,
680 v1Ty ? 1 : (8 << isQuad));
682 return cir::VectorType::getFixed(cgf->mFloat8Ty, v1Ty ? 1 : (8 << isQuad));
685 return cir::VectorType::getFixed(typeFlags.isUnsigned() ? cgf->uInt16Ty
686 : cgf->sInt16Ty,
687 v1Ty ? 1 : (4 << isQuad));
689 if (allowBFloatArgsAndRet)
690 return cir::VectorType::getFixed(cgf->getCIRGenModule().bFloat16Ty,
691 v1Ty ? 1 : (4 << isQuad));
692 return cir::VectorType::getFixed(cgf->uInt16Ty, v1Ty ? 1 : (4 << isQuad));
694 if (hasLegalHalfType)
695 return cir::VectorType::getFixed(cgf->getCIRGenModule().fP16Ty,
696 v1Ty ? 1 : (4 << isQuad));
697 return cir::VectorType::getFixed(cgf->uInt16Ty, v1Ty ? 1 : (4 << isQuad));
699 return cir::VectorType::getFixed(typeFlags.isUnsigned() ? cgf->uInt32Ty
700 : cgf->sInt32Ty,
701 v1Ty ? 1 : (2 << isQuad));
704 return cir::VectorType::getFixed(typeFlags.isUnsigned() ? cgf->uInt64Ty
705 : cgf->sInt64Ty,
706 v1Ty ? 1 : (1 << isQuad));
708 // FIXME: i128 and f128 doesn't get fully support in Clang and llvm.
709 // There is a lot of i128 and f128 API missing.
710 // so we use v16i8 to represent poly128 and get pattern matched.
711 return cir::VectorType::getFixed(cgf->uInt8Ty, 16);
713 return cir::VectorType::getFixed(cgf->getCIRGenModule().floatTy,
714 v1Ty ? 1 : (2 << isQuad));
716 return cir::VectorType::getFixed(cgf->getCIRGenModule().doubleTy,
717 v1Ty ? 1 : (1 << isQuad));
718 }
719 llvm_unreachable("Unknown vector element type!");
720}
721
722static mlir::Value emitNeonSplat(CIRGenBuilderTy &builder, mlir::Location loc,
723 mlir::Value v, mlir::Value lane,
724 unsigned int resEltCnt) {
725 assert(isa<cir::ConstantOp>(lane.getDefiningOp()) &&
726 "lane number is not a constant!");
727 int64_t laneCst = getIntValueFromConstOp(lane);
728 llvm::SmallVector<int64_t, 4> shuffleMask(resEltCnt, laneCst);
729 return builder.createVecShuffle(loc, v, shuffleMask);
730}
731
732/// Flip the signedness of `vecTy`'s element type, keeping the width and
733/// number of lanes the same. Used when a NEON intrinsic takes a shift
734/// amount vector that must be signed (e.g. aarch64.neon.urshl takes a
735/// signed amount even though the data vector is unsigned).
736static cir::VectorType getSignChangedVectorType(CIRGenBuilderTy &builder,
737 cir::VectorType vecTy) {
738 auto elemTy = mlir::cast<cir::IntType>(vecTy.getElementType());
739 elemTy = elemTy.isSigned() ? builder.getUIntNTy(elemTy.getWidth())
740 : builder.getSIntNTy(elemTy.getWidth());
741 return cir::VectorType::get(elemTy, vecTy.getSize(), vecTy.getKind());
742}
743
744static mlir::Value emitCommonNeonShift(CIRGenBuilderTy &builder,
745 mlir::Location loc,
746 cir::VectorType resTy,
747 mlir::Value shifTgt,
748 mlir::Value shiftAmt, bool shiftLeft) {
749 shiftAmt = emitNeonShiftVector(builder, shiftAmt, resTy, loc, /*neg=*/false);
750 return cir::ShiftOp::create(builder, loc, resTy,
751 builder.createBitcast(shifTgt, resTy), shiftAmt,
752 shiftLeft);
753}
754
755// Right-shift a vector by a constant.
756static mlir::Value emitNeonRShiftImm(CIRGenFunction &cgf, mlir::Value shiftVec,
757 mlir::Value shiftVal,
758 cir::VectorType vecTy, bool usgn,
759 mlir::Location loc) {
760 CIRGenBuilderTy &builder = cgf.getBuilder();
761 int64_t shiftAmt = getIntValueFromConstOp(shiftVal);
762 int eltSize =
763 cgf.cgm.getDataLayout().getTypeSizeInBits(vecTy.getElementType());
764
765 shiftVec = builder.createBitcast(shiftVec, vecTy);
766 // lshr/ashr are undefined when the shift amount is equal to the vector
767 // element size.
768 if (shiftAmt == eltSize) {
769 if (usgn) {
770 // Right-shifting an unsigned value by its size yields 0.
771 return builder.getZero(loc, vecTy);
772 }
773 // Right-shifting a signed value by its size is equivalent
774 // to a shift of size-1.
775 --shiftAmt;
776 shiftVal = builder.getConstInt(loc, vecTy.getElementType(), shiftAmt);
777 }
778 return emitCommonNeonShift(builder, loc, vecTy, shiftVec, shiftVal,
779 /*shiftLeft=*/false);
780}
781
782static cir::VectorType getIntVecFromVecTy(CIRGenBuilderTy &builder,
783 cir::VectorType vecTy) {
784 if (mlir::isa<cir::MFloat8Type>(vecTy.getElementType()))
785 return cir::VectorType::get(builder.getSInt8Ty(), vecTy.getSize(),
786 vecTy.getKind());
787 if (!cir::isAnyFloatingPointType(vecTy.getElementType()))
788 return vecTy;
789
790 if (mlir::isa<cir::SingleType>(vecTy.getElementType()))
791 return cir::VectorType::get(builder.getSInt32Ty(), vecTy.getSize(),
792 vecTy.getKind());
793 if (mlir::isa<cir::DoubleType>(vecTy.getElementType()))
794 return cir::VectorType::get(builder.getSInt64Ty(), vecTy.getSize(),
795 vecTy.getKind());
796 llvm_unreachable(
797 "Unsupported element type in getVecOfIntTypeWithSameEltWidth");
798}
799
800/// The vld1_xN builtins move their data through an aggregate of N vectors.
801/// Return N, or 0 if `builtinID` is not one of them.
802static unsigned getNeonMultiVecCount(unsigned builtinID) {
803 switch (builtinID) {
804 default:
805 return 0;
806 case NEON::BI__builtin_neon_vld1_x2_v:
807 case NEON::BI__builtin_neon_vld1q_x2_v:
808 return 2;
809 case NEON::BI__builtin_neon_vld1_x3_v:
810 case NEON::BI__builtin_neon_vld1q_x3_v:
811 return 3;
812 case NEON::BI__builtin_neon_vld1_x4_v:
813 case NEON::BI__builtin_neon_vld1q_x4_v:
814 return 4;
815 }
816}
817
818/// Return the anonymous record type `{vecTy, ..., vecTy}` (`numVecs` members)
819/// modelling the struct returned by LLVM's multi-vector NEON load intrinsics.
821 mlir::Type vecTy, unsigned numVecs) {
822 llvm::SmallVector<mlir::Type> members(numVecs, vecTy);
823 return builder.getAnonRecordTy(members, /*packed=*/false,
825}
826
827/// Call the multi-vector NEON load intrinsic `llvmIntrinsic` with `args` of
828/// `argTypes` and store the resulting `numVecs`-vector aggregate to the
829/// destination pointer `dest`. Mirrors the `CreateCall` +
830/// `CreateDefaultAlignedStore` pair used for `vld1_xN` in
831/// `EmitCommonNeonBuiltinExpr` and for `vld[234]{,_dup,_lane}` in
832/// `EmitAArch64BuiltinExpr` (ARM.cpp).
833static mlir::Value
837 llvm::Intrinsic::ID llvmIntrinsic, mlir::Type ty,
838 unsigned numVecs, mlir::Location loc) {
839 CIRGenBuilderTy &builder = cgf.getBuilder();
840 cir::RecordType recTy = getNeonMultiVecTy(builder, ty, numVecs);
841 mlir::Value result =
842 emitNeonCall(cgf.getCIRGenModule(), builder, std::move(argTypes), args,
843 getLLVMIntrNameNoPrefix(llvmIntrinsic), recTy, loc);
844 Address destAddr(builder.createPtrBitcast(dest, recTy), recTy,
846 cgf.cgm.getDataLayout().getABITypeAlign(recTy)));
847 builder.createStore(loc, result, destAddr);
848 return nullptr;
849}
850
851/// Emit a multi-vector NEON load: call `llvmIntrinsic` on the source pointer
852/// `ops[1]` and store the resulting aggregate to the destination pointer
853/// `ops[0]`.
854static mlir::Value emitNeonMultiVecLoad(CIRGenFunction &cgf,
856 unsigned llvmIntrinsic, mlir::Type ty,
857 unsigned numVecs, mlir::Location loc) {
858 llvm::SmallVector<mlir::Value> srcOp = {ops[1]};
860 cgf, ops[0], {cgf.getBuilder().getVoidPtrTy()}, srcOp,
861 static_cast<llvm::Intrinsic::ID>(llvmIntrinsic), ty, numVecs, loc);
862}
863
864/// Emit a multi-vector NEON load-lane: call `llvmIntrinsic` on the `numVecs`
865/// vector operands, the lane index, and the source pointer `ops[1]`, then
866/// store the resulting aggregate to the destination pointer `ops[0]`.
867/// `ops` on entry is `[dest, addr, vec0, ..., vec(numVecs-1), lane]`.
870 llvm::Intrinsic::ID llvmIntrinsic,
871 mlir::Type ty, unsigned numVecs,
872 mlir::Location loc) {
873 assert(ops.size() == numVecs + 3 && "expected [dest, addr, vecs..., lane]");
874 CIRGenBuilderTy &builder = cgf.getBuilder();
875 // Reorder to [vec0, ..., vec(numVecs-1), lane, addr] for the intrinsic call.
876 llvm::SmallVector<mlir::Value> callOps(ops.begin() + 2, ops.end());
877 callOps.push_back(ops[1]);
878 // The lane is a Sema-range-checked non-negative constant, so this
879 // sign-extending cast is equivalent to classic CodeGen's CreateZExt.
880 callOps[numVecs] = builder.createIntCast(callOps[numVecs], cgf.sInt64Ty);
881 llvm::SmallVector<mlir::Type> argTypes(numVecs, ty);
882 argTypes.push_back(cgf.sInt64Ty);
883 argTypes.push_back(builder.getVoidPtrTy());
884 return emitNeonMultiVecLoadCall(cgf, ops[0], std::move(argTypes), callOps,
885 llvmIntrinsic, ty, numVecs, loc);
886}
887
888static mlir::Value emitCommonNeonBuiltinExpr(
889 CIRGenFunction &cgf, unsigned builtinID, unsigned llvmIntrinsic,
890 unsigned altLLVMIntrinsic, const char *nameHint, unsigned modifier,
892 mlir::Location loc = cgf.getLoc(expr->getExprLoc());
893 clang::ASTContext &ctx = cgf.getContext();
894
895 // Extract the trailing immediate argument that encodes the type discriminator
896 // for this overloaded intrinsic.
897 // TODO: Move to the parent code that takes care of argument processing.
898 const clang::Expr *arg = expr->getArg(expr->getNumArgs() - 1);
899 std::optional<llvm::APSInt> neonTypeConst = arg->getIntegerConstantExpr(ctx);
900 if (!neonTypeConst)
901 return nullptr;
902
903 // Determine the type of this overloaded NEON intrinsic.
904 NeonTypeFlags neonType(neonTypeConst->getZExtValue());
905 const bool isUnsigned = neonType.isUnsigned();
906 const bool hasLegalHalfType = cgf.getTarget().hasFastHalfType();
907 const bool usgn = neonType.isUnsigned();
908 const bool isQuad = neonType.isQuad();
909 const bool floating = neonType.isFloatingPoint();
910
911 // The value of allowBFloatArgsAndRet is true for AArch64, but it should
912 // come from ABI info.
913 // TODO(cir): Use ABInfo to extract this information
914 const bool allowBFloatArgsAndRet = cgf.getTarget().hasFastHalfType();
915 // FIXME
916 // getTargetHooks().getABIInfo().allowBFloatArgsAndRet();
917
918 cir::VectorType vTy = getNeonType(&cgf, neonType, hasLegalHalfType,
919 /*v1Ty=*/false, allowBFloatArgsAndRet);
920 cir::VectorType ty = vTy;
921 if (!ty)
922 return nullptr;
923
924 switch (builtinID) {
925 case NEON::BI__builtin_neon_splat_lane_v:
926 case NEON::BI__builtin_neon_splat_laneq_v:
927 case NEON::BI__builtin_neon_splatq_lane_v:
928 case NEON::BI__builtin_neon_splatq_laneq_v: {
929 uint64_t numElements = vTy.getFixedSize();
930 if (builtinID == NEON::BI__builtin_neon_splatq_lane_v)
931 numElements *= 2;
932 if (builtinID == NEON::BI__builtin_neon_splat_laneq_v)
933 numElements /= 2;
934 ops[0] = cgf.getBuilder().createBitcast(loc, ops[0], vTy);
935 return emitNeonSplat(cgf.getBuilder(), loc, ops[0], ops[1], numElements);
936 }
937 case NEON::BI__builtin_neon_vpadd_v:
938 case NEON::BI__builtin_neon_vpaddq_v:
939 case NEON::BI__builtin_neon_vabs_v:
940 case NEON::BI__builtin_neon_vabsq_v:
941 cgf.cgm.errorNYI(expr->getSourceRange(),
942 std::string("unimplemented AArch64 builtin call: ") +
943 ctx.BuiltinInfo.getName(builtinID));
944 return mlir::Value{};
945 case NEON::BI__builtin_neon_vadd_v:
946 case NEON::BI__builtin_neon_vaddq_v: {
947 unsigned numBytes = (builtinID == NEON::BI__builtin_neon_vaddq_v) ? 16 : 8;
948 cir::VectorType byteTy =
949 cir::VectorType::getFixed(cgf.getBuilder().getUInt8Ty(), numBytes);
950 ops[0] = cgf.getBuilder().createBitcast(ops[0], byteTy);
951 ops[1] = cgf.getBuilder().createBitcast(ops[1], byteTy);
952 mlir::Value result = cgf.getBuilder().createXor(loc, ops[0], ops[1]);
953 return cgf.getBuilder().createBitcast(result, ty);
954 }
955 case NEON::BI__builtin_neon_vaddhn_v: {
956 // srcTy has double-width elements (e.g. <8 x i16> when VTy is <8 x i8>).
957 // Unsigned so createShiftRight emits lshr, not ashr.
958 cir::VectorType srcTy =
960 vTy, /*isExtended=*/true, /*isSigned=*/false);
961
962 // %sum = add <4 x i32> %lhs, %rhs
963 ops[0] = cgf.getBuilder().createBitcast(ops[0], srcTy);
964 ops[1] = cgf.getBuilder().createBitcast(ops[1], srcTy);
965 mlir::Value result = cgf.getBuilder().createAdd(loc, ops[0], ops[1]);
966
967 // %high = lshr <4 x i32> %sum, <i32 16, i32 16, i32 16, i32 16>
968 auto wideEltTy = mlir::cast<cir::IntType>(srcTy.getElementType());
969 mlir::Value shiftAmt = cgf.getBuilder().getConstantInt(
970 loc, wideEltTy, wideEltTy.getWidth() / 2);
971 mlir::Value shiftVec =
972 emitNeonShiftVector(cgf.getBuilder(), shiftAmt, srcTy, loc,
973 /*neg=*/false);
974 result = cgf.getBuilder().createShiftRight(loc, result, shiftVec);
975
976 // %res = trunc <4 x i32> %high to <4 x i16>
977 return cgf.getBuilder().createIntCast(result, vTy);
978 }
979 case NEON::BI__builtin_neon_vsubhn_v: {
980 // srcTy has double-width elements (e.g. <8 x i16> when VTy is <8 x i8>).
981 // Unsigned so createShiftRight emits lshr, not ashr.
982 cir::VectorType srcTy =
984 vTy, /*isExtended=*/true, /*isSigned=*/false);
985
986 // %diff = sub <4 x i32> %lhs, %rhs
987 ops[0] = cgf.getBuilder().createBitcast(ops[0], srcTy);
988 ops[1] = cgf.getBuilder().createBitcast(ops[1], srcTy);
989 mlir::Value result = cgf.getBuilder().createSub(loc, ops[0], ops[1]);
990
991 // %high = lshr <4 x i32> %diff, <i32 16, i32 16, i32 16, i32 16>
992 auto wideEltTy = mlir::cast<cir::IntType>(srcTy.getElementType());
993 mlir::Value shiftAmt = cgf.getBuilder().getConstantInt(
994 loc, wideEltTy, wideEltTy.getWidth() / 2);
995 mlir::Value shiftVec =
996 emitNeonShiftVector(cgf.getBuilder(), shiftAmt, srcTy, loc,
997 /*neg=*/false);
998 result = cgf.getBuilder().createShiftRight(loc, result, shiftVec);
999
1000 // %res = trunc <4 x i32> %high to <4 x i16>
1001 return cgf.getBuilder().createIntCast(result, vTy);
1002 }
1003 case NEON::BI__builtin_neon_vcale_v:
1004 case NEON::BI__builtin_neon_vcaleq_v:
1005 case NEON::BI__builtin_neon_vcalt_v:
1006 case NEON::BI__builtin_neon_vcaltq_v:
1007 case NEON::BI__builtin_neon_vcage_v:
1008 case NEON::BI__builtin_neon_vcageq_v:
1009 case NEON::BI__builtin_neon_vcagt_v:
1010 case NEON::BI__builtin_neon_vcagtq_v:
1011 cgf.cgm.errorNYI(expr->getSourceRange(),
1012 std::string("unimplemented AArch64 builtin call: ") +
1013 ctx.BuiltinInfo.getName(builtinID));
1014 return mlir::Value{};
1015 case NEON::BI__builtin_neon_vceqz_v:
1016 case NEON::BI__builtin_neon_vceqzq_v:
1017 if (floating)
1018 return emitAArch64CompareBuiltinExpr(cgf, cgf.getBuilder(), loc, ops[0],
1019 vTy, cir::FCmpOpKind::oeq);
1020 return emitAArch64CompareBuiltinExpr(cgf, cgf.getBuilder(), loc, ops[0],
1021 vTy, cir::CmpOpKind::eq);
1022 case NEON::BI__builtin_neon_vcgez_v:
1023 case NEON::BI__builtin_neon_vcgezq_v:
1024 case NEON::BI__builtin_neon_vclez_v:
1025 case NEON::BI__builtin_neon_vclezq_v:
1026 case NEON::BI__builtin_neon_vcgtz_v:
1027 case NEON::BI__builtin_neon_vcgtzq_v:
1028 case NEON::BI__builtin_neon_vcltz_v:
1029 case NEON::BI__builtin_neon_vcltzq_v:
1030 case NEON::BI__builtin_neon_vclz_v:
1031 case NEON::BI__builtin_neon_vclzq_v:
1032 cgf.cgm.errorNYI(expr->getSourceRange(),
1033 std::string("unimplemented AArch64 builtin call: ") +
1034 ctx.BuiltinInfo.getName(builtinID));
1035 return mlir::Value{};
1036 case NEON::BI__builtin_neon_vcvt_f32_v:
1037 case NEON::BI__builtin_neon_vcvtq_f32_v: {
1038 ops[0] = cgf.getBuilder().createBitcast(ops[0], ty);
1039 ty = getNeonType(&cgf, NeonTypeFlags(NeonTypeFlags::Float32, false, isQuad),
1040 hasLegalHalfType);
1041 return cgf.getBuilder().createCast(cir::CastKind::int_to_float, ops[0], ty);
1042 }
1043 case NEON::BI__builtin_neon_vcvt_f16_s16:
1044 case NEON::BI__builtin_neon_vcvt_f16_u16:
1045 case NEON::BI__builtin_neon_vcvtq_f16_s16:
1046 case NEON::BI__builtin_neon_vcvtq_f16_u16:
1047 case NEON::BI__builtin_neon_vcvt_n_f16_s16:
1048 case NEON::BI__builtin_neon_vcvt_n_f16_u16:
1049 case NEON::BI__builtin_neon_vcvtq_n_f16_s16:
1050 case NEON::BI__builtin_neon_vcvtq_n_f16_u16:
1051 cgf.cgm.errorNYI(expr->getSourceRange(),
1052 std::string("unimplemented AArch64 builtin call: ") +
1053 ctx.BuiltinInfo.getName(builtinID));
1054 return mlir::Value{};
1055 case NEON::BI__builtin_neon_vcvt_n_f32_v:
1056 case NEON::BI__builtin_neon_vcvt_n_f64_v:
1057 case NEON::BI__builtin_neon_vcvtq_n_f32_v:
1058 case NEON::BI__builtin_neon_vcvtq_n_f64_v: {
1059 // The constant argument to an _n_ intrinsic always is Int32Ty.
1060 mlir::Type cstIntTy = cgf.sInt32Ty;
1061 llvm::StringRef llvmIntrName =
1062 getLLVMIntrNameNoPrefix(static_cast<llvm::Intrinsic::ID>(
1063 usgn ? llvmIntrinsic : altLLVMIntrinsic));
1064 return emitNeonCall(cgf.getCIRGenModule(), cgf.getBuilder(),
1065 /*argTypes=*/{vTy, cstIntTy}, ops, llvmIntrName,
1066 /*funcResTy=*/getFloatNeonType(cgf, neonType), loc);
1067 }
1068 case NEON::BI__builtin_neon_vcvt_n_s16_f16:
1069 case NEON::BI__builtin_neon_vcvt_n_s32_v:
1070 case NEON::BI__builtin_neon_vcvt_n_u16_f16:
1071 case NEON::BI__builtin_neon_vcvt_n_u32_v:
1072 case NEON::BI__builtin_neon_vcvt_n_s64_v:
1073 case NEON::BI__builtin_neon_vcvt_n_u64_v:
1074 case NEON::BI__builtin_neon_vcvtq_n_s16_f16:
1075 case NEON::BI__builtin_neon_vcvtq_n_s32_v:
1076 case NEON::BI__builtin_neon_vcvtq_n_u16_f16:
1077 case NEON::BI__builtin_neon_vcvtq_n_u32_v:
1078 case NEON::BI__builtin_neon_vcvtq_n_s64_v:
1079 case NEON::BI__builtin_neon_vcvtq_n_u64_v: {
1080 // The constant argument to an _n_ intrinsic always is Int32Ty.
1081 mlir::Type cstIntTy = cgf.sInt32Ty;
1082 llvm::StringRef llvmIntrName = getLLVMIntrNameNoPrefix(
1083 static_cast<llvm::Intrinsic::ID>(llvmIntrinsic));
1084 return emitNeonCall(
1085 cgf.getCIRGenModule(), cgf.getBuilder(),
1086 /*argTypes=*/{getFloatNeonType(cgf, neonType), cstIntTy}, ops,
1087 llvmIntrName,
1088 /*funcResTy=*/vTy, loc);
1089 }
1090 case NEON::BI__builtin_neon_vcvt_s32_v:
1091 case NEON::BI__builtin_neon_vcvt_u32_v:
1092 case NEON::BI__builtin_neon_vcvt_s64_v:
1093 case NEON::BI__builtin_neon_vcvt_u64_v:
1094 case NEON::BI__builtin_neon_vcvt_s16_f16:
1095 case NEON::BI__builtin_neon_vcvt_u16_f16:
1096 case NEON::BI__builtin_neon_vcvtq_s32_v:
1097 case NEON::BI__builtin_neon_vcvtq_u32_v:
1098 case NEON::BI__builtin_neon_vcvtq_s64_v:
1099 case NEON::BI__builtin_neon_vcvtq_u64_v:
1100 case NEON::BI__builtin_neon_vcvtq_s16_f16:
1101 case NEON::BI__builtin_neon_vcvtq_u16_f16: {
1102 auto ty = getFloatNeonType(cgf, neonType);
1103 // Undo the bitcast inserted by intrinsics that expand to this builtin
1104 // (e.g. vcvt_u32_f32).
1105 // TODO: While the bitcasts eventually cancel each other out, we should
1106 // avoid them altogether.
1107 ops[0] =
1108 cgf.getBuilder().createCast(loc, cir::CastKind::bitcast, ops[0], ty);
1110 // AArch64: use fptosi.sat/fptoui.sat unless under strict FP.
1111 llvm::StringRef llvmIntrName = usgn ? "fptoui.sat" : "fptosi.sat";
1112 return emitNeonCall(cgf.getCIRGenModule(), cgf.getBuilder(),
1113 /*argTypes=*/{ty}, ops, llvmIntrName, vTy, loc);
1114 }
1115 case NEON::BI__builtin_neon_vcvta_s16_f16:
1116 case NEON::BI__builtin_neon_vcvta_s32_v:
1117 case NEON::BI__builtin_neon_vcvta_s64_v:
1118 case NEON::BI__builtin_neon_vcvta_u16_f16:
1119 case NEON::BI__builtin_neon_vcvta_u32_v:
1120 case NEON::BI__builtin_neon_vcvta_u64_v:
1121 case NEON::BI__builtin_neon_vcvtaq_s16_f16:
1122 case NEON::BI__builtin_neon_vcvtaq_s32_v:
1123 case NEON::BI__builtin_neon_vcvtaq_s64_v:
1124 case NEON::BI__builtin_neon_vcvtaq_u16_f16:
1125 case NEON::BI__builtin_neon_vcvtaq_u32_v:
1126 case NEON::BI__builtin_neon_vcvtaq_u64_v:
1127 case NEON::BI__builtin_neon_vcvtn_s16_f16:
1128 case NEON::BI__builtin_neon_vcvtn_s32_v:
1129 case NEON::BI__builtin_neon_vcvtn_s64_v:
1130 case NEON::BI__builtin_neon_vcvtn_u16_f16:
1131 case NEON::BI__builtin_neon_vcvtn_u32_v:
1132 case NEON::BI__builtin_neon_vcvtn_u64_v:
1133 case NEON::BI__builtin_neon_vcvtnq_s16_f16:
1134 case NEON::BI__builtin_neon_vcvtnq_s32_v:
1135 case NEON::BI__builtin_neon_vcvtnq_s64_v:
1136 case NEON::BI__builtin_neon_vcvtnq_u16_f16:
1137 case NEON::BI__builtin_neon_vcvtnq_u32_v:
1138 case NEON::BI__builtin_neon_vcvtnq_u64_v:
1139 case NEON::BI__builtin_neon_vcvtp_s16_f16:
1140 case NEON::BI__builtin_neon_vcvtp_s32_v:
1141 case NEON::BI__builtin_neon_vcvtp_s64_v:
1142 case NEON::BI__builtin_neon_vcvtp_u16_f16:
1143 case NEON::BI__builtin_neon_vcvtp_u32_v:
1144 case NEON::BI__builtin_neon_vcvtp_u64_v:
1145 case NEON::BI__builtin_neon_vcvtpq_s16_f16:
1146 case NEON::BI__builtin_neon_vcvtpq_s32_v:
1147 case NEON::BI__builtin_neon_vcvtpq_s64_v:
1148 case NEON::BI__builtin_neon_vcvtpq_u16_f16:
1149 case NEON::BI__builtin_neon_vcvtpq_u32_v:
1150 case NEON::BI__builtin_neon_vcvtpq_u64_v:
1151 case NEON::BI__builtin_neon_vcvtm_s16_f16:
1152 case NEON::BI__builtin_neon_vcvtm_s32_v:
1153 case NEON::BI__builtin_neon_vcvtm_s64_v:
1154 case NEON::BI__builtin_neon_vcvtm_u16_f16:
1155 case NEON::BI__builtin_neon_vcvtm_u32_v:
1156 case NEON::BI__builtin_neon_vcvtm_u64_v:
1157 case NEON::BI__builtin_neon_vcvtmq_s16_f16:
1158 case NEON::BI__builtin_neon_vcvtmq_s32_v:
1159 case NEON::BI__builtin_neon_vcvtmq_s64_v:
1160 case NEON::BI__builtin_neon_vcvtmq_u16_f16:
1161 case NEON::BI__builtin_neon_vcvtmq_u32_v:
1162 case NEON::BI__builtin_neon_vcvtmq_u64_v:
1163 cgf.cgm.errorNYI(expr->getSourceRange(),
1164 std::string("unimplemented AArch64 builtin call: ") +
1165 ctx.BuiltinInfo.getName(builtinID));
1166 return mlir::Value{};
1167 case NEON::BI__builtin_neon_vcvtx_f32_v: {
1168 llvm::StringRef llvmIntrName = getLLVMIntrNameNoPrefix(
1169 static_cast<llvm::Intrinsic::ID>(llvmIntrinsic));
1170 auto v2f64 = cir::VectorType::getFixed(cgf.cgm.doubleTy, 2);
1171 auto v2f32 = cir::VectorType::getFixed(cgf.cgm.floatTy, 2);
1172 return emitNeonCall(cgf.getCIRGenModule(), cgf.getBuilder(),
1173 /*argTypes=*/{v2f64}, ops, llvmIntrName,
1174 /*funcResTy=*/v2f32, loc);
1175 }
1176 case NEON::BI__builtin_neon_vext_v:
1177 case NEON::BI__builtin_neon_vextq_v:
1178 cgf.cgm.errorNYI(expr->getSourceRange(),
1179 std::string("unimplemented AArch64 builtin call: ") +
1180 ctx.BuiltinInfo.getName(builtinID));
1181 return mlir::Value{};
1182 case NEON::BI__builtin_neon_vfma_v:
1183 case NEON::BI__builtin_neon_vfmaq_v: {
1184 // NEON intrinsic: vfma(q)(accumulator, multiplicand1, multiplicand2)
1185 // CIR fma: fma(multiplicand1, multiplicand2, accumulator)
1186 // Reorder arguments to match fma signature.
1187 mlir::Value op0 = cgf.getBuilder().createBitcast(ops[0], ty);
1188 mlir::Value op1 = cgf.getBuilder().createBitcast(ops[1], ty);
1189 mlir::Value op2 = cgf.getBuilder().createBitcast(ops[2], ty);
1190 llvm::SmallVector<mlir::Value> fmaOps = {op1, op2, op0};
1191 return emitNeonCallToOp<cir::FMAOp>(cgf.cgm, cgf.getBuilder(), {ty, ty, ty},
1192 fmaOps, std::nullopt, ty, loc);
1193 }
1194 case NEON::BI__builtin_neon_vld1_x2_v:
1195 case NEON::BI__builtin_neon_vld1q_x2_v:
1196 case NEON::BI__builtin_neon_vld1_x3_v:
1197 case NEON::BI__builtin_neon_vld1q_x3_v:
1198 case NEON::BI__builtin_neon_vld1_x4_v:
1199 case NEON::BI__builtin_neon_vld1q_x4_v:
1200 return emitNeonMultiVecLoad(cgf, ops, llvmIntrinsic, ty,
1201 getNeonMultiVecCount(builtinID), loc);
1202 case NEON::BI__builtin_neon_vld1_v:
1203 case NEON::BI__builtin_neon_vld1q_v:
1204 case NEON::BI__builtin_neon_vld2_v:
1205 case NEON::BI__builtin_neon_vld2q_v:
1206 case NEON::BI__builtin_neon_vld3_v:
1207 case NEON::BI__builtin_neon_vld3q_v:
1208 case NEON::BI__builtin_neon_vld4_v:
1209 case NEON::BI__builtin_neon_vld4q_v:
1210 case NEON::BI__builtin_neon_vld2_dup_v:
1211 case NEON::BI__builtin_neon_vld2q_dup_v:
1212 case NEON::BI__builtin_neon_vld3_dup_v:
1213 case NEON::BI__builtin_neon_vld3q_dup_v:
1214 case NEON::BI__builtin_neon_vld4_dup_v:
1215 case NEON::BI__builtin_neon_vld4q_dup_v:
1216 case NEON::BI__builtin_neon_vld1_dup_v:
1217 case NEON::BI__builtin_neon_vld1q_dup_v:
1218 case NEON::BI__builtin_neon_vld2_lane_v:
1219 case NEON::BI__builtin_neon_vld2q_lane_v:
1220 case NEON::BI__builtin_neon_vld3_lane_v:
1221 case NEON::BI__builtin_neon_vld3q_lane_v:
1222 case NEON::BI__builtin_neon_vld4_lane_v:
1223 case NEON::BI__builtin_neon_vld4q_lane_v:
1224 cgf.cgm.errorNYI(expr->getSourceRange(),
1225 std::string("Reached code-path for ARM builtin call ") +
1226 ctx.BuiltinInfo.getName(builtinID) +
1227 "(ARM builtins are not supported ATM)");
1228 return mlir::Value{};
1229 case NEON::BI__builtin_neon_vmovl_v: {
1230 cir::VectorType dTy =
1232 ty, /*isExtended=*/false, !usgn);
1233 ops[0] = cgf.getBuilder().createBitcast(loc, ops[0], dTy);
1234 return cgf.getBuilder().createIntCast(ops[0], ty);
1235 }
1236 case NEON::BI__builtin_neon_vmovn_v:
1237 case NEON::BI__builtin_neon_vmull_v:
1238 case NEON::BI__builtin_neon_vpadal_v:
1239 case NEON::BI__builtin_neon_vpadalq_v:
1240 cgf.cgm.errorNYI(expr->getSourceRange(),
1241 std::string("Reached code-path for ARM builtin call ") +
1242 ctx.BuiltinInfo.getName(builtinID) +
1243 "(ARM builtins are not supported ATM)");
1244 return mlir::Value{};
1245 case NEON::BI__builtin_neon_vpaddl_v:
1246 case NEON::BI__builtin_neon_vpaddlq_v: {
1247 llvm::StringRef llvmIntrName =
1248 getLLVMIntrNameNoPrefix(static_cast<llvm::Intrinsic::ID>(
1249 usgn ? llvmIntrinsic : altLLVMIntrinsic));
1250 return emitNeonCall(cgf.getCIRGenModule(), cgf.getBuilder(),
1251 /*argTypes=*/{getNeonPairwiseWidenInputType(vTy, usgn)},
1252 ops, llvmIntrName,
1253 /*funcResTy=*/vTy, loc);
1254 }
1255 case NEON::BI__builtin_neon_vqdmlal_v:
1256 case NEON::BI__builtin_neon_vqdmlsl_v:
1257 case NEON::BI__builtin_neon_vqdmulhq_lane_v:
1258 case NEON::BI__builtin_neon_vqdmulh_lane_v:
1259 case NEON::BI__builtin_neon_vqrdmulhq_lane_v:
1260 case NEON::BI__builtin_neon_vqrdmulh_lane_v:
1261 case NEON::BI__builtin_neon_vqdmulhq_laneq_v:
1262 case NEON::BI__builtin_neon_vqdmulh_laneq_v:
1263 case NEON::BI__builtin_neon_vqrdmulhq_laneq_v:
1264 case NEON::BI__builtin_neon_vqrdmulh_laneq_v:
1265 cgf.cgm.errorNYI(expr->getSourceRange(),
1266 std::string("unimplemented AArch64 builtin call: ") +
1267 ctx.BuiltinInfo.getName(builtinID));
1268 return mlir::Value{};
1269 case NEON::BI__builtin_neon_vqshl_n_v:
1270 case NEON::BI__builtin_neon_vqshlq_n_v: {
1271 llvm::StringRef intrName =
1272 usgn ? "aarch64.neon.uqshl" : "aarch64.neon.sqshl";
1273 return emitNeonCall(cgf.cgm, cgf.getBuilder(), {ty, ty}, ops, intrName, ty,
1274 loc, /*isConstrainedFPIntrinsic=*/false, /*shift=*/1,
1275 /*rightshift=*/false);
1276 }
1277 case NEON::BI__builtin_neon_vqshlu_n_v:
1278 case NEON::BI__builtin_neon_vqshluq_n_v:
1279 return emitNeonCall(cgf.cgm, cgf.getBuilder(), {ty, ty}, ops,
1280 "aarch64.neon.sqshlu", ty, loc,
1281 /*isConstrainedFPIntrinsic=*/false, /*shift=*/1,
1282 /*rightshift=*/false);
1283 case NEON::BI__builtin_neon_vrecpe_v:
1284 case NEON::BI__builtin_neon_vrecpeq_v:
1285 case NEON::BI__builtin_neon_vrsqrte_v:
1286 case NEON::BI__builtin_neon_vrsqrteq_v:
1287 cgf.cgm.errorNYI(expr->getSourceRange(),
1288 std::string("unimplemented AArch64 builtin call: ") +
1289 ctx.BuiltinInfo.getName(builtinID));
1290 return mlir::Value{};
1291 case NEON::BI__builtin_neon_vrndi_v:
1292 case NEON::BI__builtin_neon_vrndiq_v:
1294 return emitNeonCallToOp<cir::NearbyintOp>(cgf.cgm, cgf.getBuilder(), {ty},
1295 ops, std::nullopt, ty, loc);
1296 case NEON::BI__builtin_neon_vrshr_n_v:
1297 case NEON::BI__builtin_neon_vrshrq_n_v: {
1298 llvm::StringRef intrName =
1299 usgn ? "aarch64.neon.urshl" : "aarch64.neon.srshl";
1300 return emitNeonCall(
1301 cgf.cgm, cgf.getBuilder(),
1302 {ty, usgn ? getSignChangedVectorType(cgf.getBuilder(), ty) : ty}, ops,
1303 intrName, ty, loc, /*isConstrainedFPIntrinsic=*/false,
1304 /*shift=*/1,
1305 /*rightshift=*/true);
1306 }
1307 case NEON::BI__builtin_neon_vsha512hq_u64:
1308 case NEON::BI__builtin_neon_vsha512h2q_u64:
1309 case NEON::BI__builtin_neon_vsha512su0q_u64:
1310 case NEON::BI__builtin_neon_vsha512su1q_u64:
1311 cgf.cgm.errorNYI(expr->getSourceRange(),
1312 std::string("unimplemented AArch64 builtin call: ") +
1313 ctx.BuiltinInfo.getName(builtinID));
1314 return mlir::Value{};
1315 case NEON::BI__builtin_neon_vshl_n_v:
1316 case NEON::BI__builtin_neon_vshlq_n_v:
1317 return emitCommonNeonShift(cgf.getBuilder(), loc, vTy, ops[0], ops[1],
1318 /*shiftLeft=*/true);
1319 case NEON::BI__builtin_neon_vshll_n_v: {
1320 CIRGenBuilderTy &builder = cgf.getBuilder();
1321 cir::VectorType narrowVecTy =
1323 /*isExtended=*/false,
1324 /*isSigned=*/!usgn);
1325 mlir::Value src = builder.createBitcast(ops[0], narrowVecTy);
1326 mlir::Value extended = builder.createIntCast(src, vTy);
1327 return emitCommonNeonShift(builder, loc, vTy, extended, ops[1],
1328 /*shiftLeft=*/true);
1329 }
1330 case NEON::BI__builtin_neon_vshrn_n_v: {
1331 CIRGenBuilderTy &builder = cgf.getBuilder();
1332 cir::VectorType wideVecTy =
1334 /*isExtended=*/true,
1335 /*isSigned=*/!usgn);
1336 mlir::Value src = builder.createBitcast(ops[0], wideVecTy);
1337 mlir::Value shifted = emitCommonNeonShift(builder, loc, wideVecTy, src,
1338 ops[1], /*shiftLeft=*/false);
1339 return builder.createIntCast(shifted, vTy);
1340 }
1341 case NEON::BI__builtin_neon_vshr_n_v:
1342 case NEON::BI__builtin_neon_vshrq_n_v:
1343 return emitNeonRShiftImm(cgf, ops[0], ops[1], vTy, isUnsigned, loc);
1344 case NEON::BI__builtin_neon_vst1_v:
1345 case NEON::BI__builtin_neon_vst1q_v:
1346 case NEON::BI__builtin_neon_vst2_v:
1347 case NEON::BI__builtin_neon_vst2q_v:
1348 case NEON::BI__builtin_neon_vst3_v:
1349 case NEON::BI__builtin_neon_vst3q_v:
1350 case NEON::BI__builtin_neon_vst4_v:
1351 case NEON::BI__builtin_neon_vst4q_v:
1352 case NEON::BI__builtin_neon_vst2_lane_v:
1353 case NEON::BI__builtin_neon_vst2q_lane_v:
1354 case NEON::BI__builtin_neon_vst3_lane_v:
1355 case NEON::BI__builtin_neon_vst3q_lane_v:
1356 case NEON::BI__builtin_neon_vst4_lane_v:
1357 case NEON::BI__builtin_neon_vst4q_lane_v:
1358 case NEON::BI__builtin_neon_vsm3partw1q_u32:
1359 case NEON::BI__builtin_neon_vsm3partw2q_u32:
1360 case NEON::BI__builtin_neon_vsm3ss1q_u32:
1361 case NEON::BI__builtin_neon_vsm4ekeyq_u32:
1362 case NEON::BI__builtin_neon_vsm4eq_u32:
1363 case NEON::BI__builtin_neon_vsm3tt1aq_u32:
1364 case NEON::BI__builtin_neon_vsm3tt1bq_u32:
1365 case NEON::BI__builtin_neon_vsm3tt2aq_u32:
1366 case NEON::BI__builtin_neon_vsm3tt2bq_u32:
1367 cgf.cgm.errorNYI(expr->getSourceRange(),
1368 std::string("unimplemented AArch64 builtin call: ") +
1369 ctx.BuiltinInfo.getName(builtinID));
1370 return mlir::Value{};
1371 case NEON::BI__builtin_neon_vst1_x2_v:
1372 case NEON::BI__builtin_neon_vst1q_x2_v:
1373 case NEON::BI__builtin_neon_vst1_x3_v:
1374 case NEON::BI__builtin_neon_vst1q_x3_v:
1375 case NEON::BI__builtin_neon_vst1_x4_v:
1376 case NEON::BI__builtin_neon_vst1q_x4_v: {
1377 // The builtin call has the pointer first, but the AArch64 st1x2/3/4
1378 // intrinsics take the vector operands first and the pointer last.
1379 std::rotate(ops.begin(), ops.begin() + 1, ops.end());
1380 llvm::SmallVector<mlir::Type> argTypes(ops.size() - 1, ty);
1381 argTypes.push_back(cgf.getBuilder().getVoidPtrTy());
1382 llvm::StringRef llvmIntrName = getLLVMIntrNameNoPrefix(
1383 static_cast<llvm::Intrinsic::ID>(llvmIntrinsic));
1384 return emitNeonCall(cgf.getCIRGenModule(), cgf.getBuilder(), argTypes, ops,
1385 llvmIntrName, cgf.cgm.voidTy, loc);
1386 }
1387 case NEON::BI__builtin_neon_vtrn_v:
1388 case NEON::BI__builtin_neon_vtrnq_v:
1389 case NEON::BI__builtin_neon_vtst_v:
1390 case NEON::BI__builtin_neon_vtstq_v:
1391 case NEON::BI__builtin_neon_vuzp_v:
1392 case NEON::BI__builtin_neon_vuzpq_v:
1393 case NEON::BI__builtin_neon_vxarq_u64:
1394 case NEON::BI__builtin_neon_vzip_v:
1395 case NEON::BI__builtin_neon_vzipq_v:
1396 case NEON::BI__builtin_neon_vdot_s32:
1397 case NEON::BI__builtin_neon_vdot_u32:
1398 case NEON::BI__builtin_neon_vdotq_s32:
1399 case NEON::BI__builtin_neon_vdotq_u32:
1400 case NEON::BI__builtin_neon_vfmlal_low_f16:
1401 case NEON::BI__builtin_neon_vfmlalq_low_f16:
1402 case NEON::BI__builtin_neon_vfmlsl_low_f16:
1403 case NEON::BI__builtin_neon_vfmlslq_low_f16:
1404 case NEON::BI__builtin_neon_vfmlal_high_f16:
1405 case NEON::BI__builtin_neon_vfmlalq_high_f16:
1406 case NEON::BI__builtin_neon_vfmlsl_high_f16:
1407 case NEON::BI__builtin_neon_vfmlslq_high_f16:
1408 case NEON::BI__builtin_neon_vmmlaq_s32:
1409 case NEON::BI__builtin_neon_vmmlaq_u32:
1410 cgf.cgm.errorNYI(expr->getSourceRange(),
1411 std::string("unimplemented AArch64 builtin call: ") +
1412 ctx.BuiltinInfo.getName(builtinID));
1413 return mlir::Value{};
1414 case NEON::BI__builtin_neon_vmul_v:
1415 case NEON::BI__builtin_neon_vmulq_v:
1416 return cgf.getBuilder().emitIntrinsicCallOp(loc, "aarch64.neon.pmul", vTy,
1417 ops);
1418 case NEON::BI__builtin_neon_vusmmlaq_s32:
1419 case NEON::BI__builtin_neon_vusdot_s32:
1420 case NEON::BI__builtin_neon_vusdotq_s32:
1421 case NEON::BI__builtin_neon_vbfdot_f32:
1422 case NEON::BI__builtin_neon_vbfdotq_f32:
1423 case NEON::BI__builtin_neon___a32_vcvt_bf16_f32:
1424 cgf.cgm.errorNYI(expr->getSourceRange(),
1425 std::string("unimplemented AArch64 builtin call: ") +
1426 ctx.BuiltinInfo.getName(builtinID));
1427 return mlir::Value{};
1428 }
1429
1430 // The switch stmt is intended to help catch NYI cases and will be removed
1431 // once the CIR implementation is complete. Avoid adding specialized
1432 // code in cases - that should only be required for a handful of examples.
1433 switch (builtinID) {
1434 default:
1435 cgf.cgm.errorNYI(expr->getSourceRange(),
1436 std::string("unimplemented AArch64 builtin call: ") +
1437 cgf.getContext().BuiltinInfo.getName(builtinID));
1438 break;
1439 case NEON::BI__builtin_neon_vrnd32x_f32:
1440 case NEON::BI__builtin_neon_vrnd32xq_f32:
1441 case NEON::BI__builtin_neon_vrnd32x_f64:
1442 case NEON::BI__builtin_neon_vrnd32xq_f64:
1443 case NEON::BI__builtin_neon_vrnd32z_f32:
1444 case NEON::BI__builtin_neon_vrnd32zq_f32:
1445 case NEON::BI__builtin_neon_vrnd32z_f64:
1446 case NEON::BI__builtin_neon_vrnd32zq_f64:
1447 case NEON::BI__builtin_neon_vrnd64x_f32:
1448 case NEON::BI__builtin_neon_vrnd64xq_f32:
1449 case NEON::BI__builtin_neon_vrnd64x_f64:
1450 case NEON::BI__builtin_neon_vrnd64xq_f64:
1451 case NEON::BI__builtin_neon_vrnd64z_f32:
1452 case NEON::BI__builtin_neon_vrnd64zq_f32:
1453 case NEON::BI__builtin_neon_vrnd64z_f64:
1454 case NEON::BI__builtin_neon_vrnd64zq_f64: {
1455 llvm::StringRef llvmIntrName = getLLVMIntrNameNoPrefix(
1456 static_cast<llvm::Intrinsic::ID>(llvmIntrinsic));
1457 mlir::Value result =
1458 emitNeonCall(cgf.cgm, cgf.getBuilder(), /*argTypes=*/{vTy}, ops,
1459 llvmIntrName, /*funcResTy=*/vTy, loc);
1460 mlir::Type resultType = cgf.convertType(expr->getType());
1461 return cgf.getBuilder().createBitcast(result, resultType);
1462 }
1463 case NEON::BI__builtin_neon_vhadd_v:
1464 case NEON::BI__builtin_neon_vhaddq_v:
1465 case NEON::BI__builtin_neon_vhsub_v:
1466 case NEON::BI__builtin_neon_vhsubq_v:
1467 case NEON::BI__builtin_neon_vqadd_v:
1468 case NEON::BI__builtin_neon_vqaddq_v:
1469 case NEON::BI__builtin_neon_vrhadd_v:
1470 case NEON::BI__builtin_neon_vrhaddq_v:
1471 case NEON::BI__builtin_neon_vqsub_v:
1472 case NEON::BI__builtin_neon_vqsubq_v:
1473 case NEON::BI__builtin_neon_vshl_v:
1474 case NEON::BI__builtin_neon_vshlq_v:
1475 case NEON::BI__builtin_neon_vraddhn_v:
1476 case NEON::BI__builtin_neon_vrsubhn_v:
1477 case NEON::BI__builtin_neon_vqshl_v:
1478 case NEON::BI__builtin_neon_vqshlq_v: {
1479 // Pick the signed/unsigned intrinsic when the builtin has both
1480 // (UnsignedAlts); otherwise there is a single intrinsic.
1481 unsigned intrinsic =
1482 ((modifier & UnsignedAlts) && !usgn) ? altLLVMIntrinsic : llvmIntrinsic;
1483 llvm::StringRef llvmIntrName =
1484 getLLVMIntrNameNoPrefix(static_cast<llvm::Intrinsic::ID>(intrinsic));
1485
1486 cir::VectorType argTy =
1487 deriveNeonBinaryArgType(cgf.getBuilder(), modifier, vTy);
1488
1489 mlir::Value result =
1491 /*argTypes=*/{argTy, argTy}, ops, llvmIntrName,
1492 /*funcResTy=*/vTy, loc);
1493 mlir::Type resultType = cgf.convertType(expr->getType());
1494 return cgf.getBuilder().createBitcast(result, resultType);
1495 }
1496 }
1497
1498 // NYI
1499 return nullptr;
1500}
1501
1503 unsigned builtinID, const CallExpr *expr, SmallVectorImpl<mlir::Value> &ops,
1504 SVETypeFlags typeFlags) {
1505 // Find out if any arguments are required to be integer constant expressions.
1506 unsigned iceArguments = 0;
1508 getContext().GetBuiltinType(builtinID, error, &iceArguments);
1509 assert(error == ASTContext::GE_None && "Should not codegen an error");
1510
1511 for (unsigned i = 0, e = expr->getNumArgs(); i != e; i++) {
1512 bool isIce = iceArguments & (1 << i);
1513 mlir::Value arg = emitScalarExpr(expr->getArg(i));
1514
1515 if (isIce) {
1516 cgm.errorNYI(expr->getSourceRange(),
1517 std::string("unimplemented AArch64 builtin call: ") +
1518 getContext().BuiltinInfo.getName(builtinID));
1519 }
1520
1521 // FIXME: Handle types like svint16x2_t, which are currently incorrectly
1522 // converted to i32. These should be treated as structs and unpacked.
1523
1524 ops.push_back(arg);
1525 }
1526 return true;
1527}
1528
1529// Reinterpret the input predicate so that it can be used to correctly isolate
1530// the elements of the specified datatype.
1531mlir::Value CIRGenFunction::emitSVEPredicateCast(mlir::Value pred,
1532 unsigned minNumElts,
1533 mlir::Location loc) {
1534
1535 // TODO: Handle "aarch64.svcount" once we get round to supporting SME.
1536
1537 auto retTy = cir::VectorType::getScalable(builder.getUIntNTy(1), minNumElts,
1538 cir::VectorAbiKind::SVEPredicate);
1539 if (pred.getType() == retTy)
1540 return pred;
1541
1542 llvm::Intrinsic::ID intID;
1543 switch (minNumElts) {
1544 default:
1545 llvm_unreachable("unsupported element count!");
1546 case 1:
1547 case 2:
1548 case 4:
1549 case 8:
1550 intID = Intrinsic::aarch64_sve_convert_from_svbool;
1551 break;
1552 case 16:
1553 intID = Intrinsic::aarch64_sve_convert_to_svbool;
1554 break;
1555 }
1556
1557 llvm::StringRef llvmIntrName = getLLVMIntrNameNoPrefix(intID);
1558 auto call = builder.emitIntrinsicCallOp(loc, llvmIntrName, retTy,
1559 mlir::ValueRange{pred});
1560 assert(call.getType() == retTy && "Unexpected return type!");
1561 return call;
1562}
1563
1564//===----------------------------------------------------------------------===//
1565// SVE helpers
1566//===----------------------------------------------------------------------===//
1567// Get the minimum number of elements in an SVE vector for the given element
1568// type. The actual number of elements in the vector would be an integer (power
1569// of two) multiple of this value.
1571 switch (sveType) {
1572 default:
1573 llvm_unreachable("Invalid SVETypeFlag!");
1574
1575 case SVETypeFlags::EltTyInt8:
1576 return 16;
1577 case SVETypeFlags::EltTyInt16:
1578 return 8;
1579 case SVETypeFlags::EltTyInt32:
1580 return 4;
1581 case SVETypeFlags::EltTyInt64:
1582 return 2;
1583
1584 case SVETypeFlags::EltTyMFloat8:
1585 return 16;
1586 case SVETypeFlags::EltTyFloat16:
1587 case SVETypeFlags::EltTyBFloat16:
1588 return 8;
1589 case SVETypeFlags::EltTyFloat32:
1590 return 4;
1591 case SVETypeFlags::EltTyFloat64:
1592 return 2;
1593
1594 case SVETypeFlags::EltTyBool8:
1595 return 16;
1596 case SVETypeFlags::EltTyBool16:
1597 return 8;
1598 case SVETypeFlags::EltTyBool32:
1599 return 4;
1600 case SVETypeFlags::EltTyBool64:
1601 return 2;
1602 }
1603}
1604
1605cir::VectorType CIRGenFunction::getSVEType(const SVETypeFlags &typeFlags) {
1606 switch (typeFlags.getEltType()) {
1607 default:
1608 llvm_unreachable("Invalid SVETypeFlag!");
1609
1610 case SVETypeFlags::EltTyInt8:
1611 return cir::VectorType::getScalable(builder.getUInt8Ty(), 16,
1612 cir::VectorAbiKind::SVEData);
1613 case SVETypeFlags::EltTyInt16:
1614 return cir::VectorType::getScalable(builder.getUInt16Ty(), 8,
1615 cir::VectorAbiKind::SVEData);
1616 case SVETypeFlags::EltTyInt32:
1617 return cir::VectorType::getScalable(builder.getUInt32Ty(), 4,
1618 cir::VectorAbiKind::SVEData);
1619 case SVETypeFlags::EltTyInt64:
1620 return cir::VectorType::getScalable(builder.getUInt64Ty(), 2,
1621 cir::VectorAbiKind::SVEData);
1622
1623 case SVETypeFlags::EltTyMFloat8:
1624 return cir::VectorType::getScalable(builder.getMFloat8Ty(), 16,
1625 cir::VectorAbiKind::SVEData);
1626 case SVETypeFlags::EltTyFloat16:
1627 return cir::VectorType::getScalable(builder.getFp16Ty(), 8,
1628 cir::VectorAbiKind::SVEData);
1629 case SVETypeFlags::EltTyBFloat16:
1630 return cir::VectorType::getScalable(builder.getBfloat16Ty(), 8,
1631 cir::VectorAbiKind::SVEData);
1632 case SVETypeFlags::EltTyFloat32:
1633 return cir::VectorType::getScalable(builder.getSingleTy(), 4,
1634 cir::VectorAbiKind::SVEData);
1635 case SVETypeFlags::EltTyFloat64:
1636 return cir::VectorType::getScalable(builder.getDoubleTy(), 2,
1637 cir::VectorAbiKind::SVEData);
1638
1639 case SVETypeFlags::EltTyBool8:
1640 return cir::VectorType::getScalable(builder.getUIntNTy(1), 16,
1641 cir::VectorAbiKind::SVEPredicate);
1642 case SVETypeFlags::EltTyBool16:
1643 return cir::VectorType::getScalable(builder.getUIntNTy(1), 8,
1644 cir::VectorAbiKind::SVEPredicate);
1645 case SVETypeFlags::EltTyBool32:
1646 return cir::VectorType::getScalable(builder.getUIntNTy(1), 4,
1647 cir::VectorAbiKind::SVEPredicate);
1648 case SVETypeFlags::EltTyBool64:
1649 return cir::VectorType::getScalable(builder.getUIntNTy(1), 2,
1650 cir::VectorAbiKind::SVEPredicate);
1651 }
1652}
1653
1654// TODO(cir): Share with OGCG
1655constexpr unsigned sveBitsPerBlock = 128;
1656
1657static cir::VectorType getSVEVectorForElementType(CIRGenModule &cgm,
1658 mlir::Type eltTy) {
1659 unsigned numElts =
1661 return cir::VectorType::getScalable(eltTy, numElts,
1662 cir::VectorAbiKind::SVEData);
1663}
1664
1665//===----------------------------------------------------------------------===//
1666// SVE helpers
1667//===----------------------------------------------------------------------===//
1668std::optional<mlir::Value>
1670 const CallExpr *expr) {
1671 mlir::Type ty = convertType(expr->getType());
1672
1673 if (builtinID >= SVE::BI__builtin_sve_reinterpret_s8_s8 &&
1674 builtinID <= SVE::BI__builtin_sve_reinterpret_f64_f64_x4) {
1675 cgm.errorNYI(expr->getSourceRange(),
1676 std::string("unimplemented AArch64 builtin call: ") +
1677 getContext().BuiltinInfo.getName(builtinID));
1678 return mlir::Value{};
1679 }
1680
1682
1683 auto *builtinIntrInfo =
1686
1687 // The operands of the builtin call
1689
1690 SVETypeFlags typeFlags(builtinIntrInfo->TypeModifier);
1692 typeFlags))
1693 return mlir::Value{};
1694
1695 if (typeFlags.isLoad() || typeFlags.isStore() || typeFlags.isGatherLoad() ||
1696 typeFlags.isScatterStore() || typeFlags.isPrefetch() ||
1697 typeFlags.isGatherPrefetch() || typeFlags.isStructLoad() ||
1698 typeFlags.isStructStore() || typeFlags.isTupleSet() ||
1699 typeFlags.isTupleGet() || typeFlags.isTupleCreate() ||
1700 typeFlags.isUndef())
1701 cgm.errorNYI(expr->getSourceRange(),
1702 std::string("unimplemented AArch64 builtin call: ") +
1703 getContext().BuiltinInfo.getName(builtinID));
1704
1705 mlir::Location loc = getLoc(expr->getExprLoc());
1706
1707 // Handle built-ins for which there is a corresponding LLVM Intrinsic.
1708 // -------------------------------------------------------------------
1709 if (builtinIntrInfo->LLVMIntrinsic != 0) {
1710 // Emit set FPMR for intrinsics that require it.
1711 if (typeFlags.setsFPMR())
1712 cgm.errorNYI(expr->getSourceRange(),
1713 std::string("unimplemented AArch64 builtin call: ") +
1714 getContext().BuiltinInfo.getName(builtinID));
1715
1716 // Zero-ing predication
1717 if (typeFlags.getMergeType() == SVETypeFlags::MergeZeroExp) {
1718 auto null = builder.getNullValue(convertType(expr->getType()),
1719 getLoc(expr->getExprLoc()));
1720 ops.insert(ops.begin(), null);
1721 }
1722
1723 if (typeFlags.getMergeType() == SVETypeFlags::MergeAnyExp)
1724 ops.insert(ops.begin(),
1725 builder.getConstant(loc, cir::UndefAttr::get(ty)));
1726
1727 // Some ACLE builtins leave out the argument to specify the predicate
1728 // pattern, which is expected to be expanded to an SV_ALL pattern.
1729 if (typeFlags.isAppendSVALL())
1730 cgm.errorNYI(expr->getSourceRange(),
1731 std::string("unimplemented AArch64 builtin call: ") +
1732 getContext().BuiltinInfo.getName(builtinID));
1733 if (typeFlags.isInsertOp1SVALL())
1734 cgm.errorNYI(expr->getSourceRange(),
1735 std::string("unimplemented AArch64 builtin call: ") +
1736 getContext().BuiltinInfo.getName(builtinID));
1737
1738 // Predicates must match the main datatype.
1739 for (mlir::Value &op : ops)
1740 if (auto predTy = dyn_cast<cir::VectorType>(op.getType()))
1741 if (auto cirInt = dyn_cast<cir::IntType>(predTy.getElementType()))
1742 if (cirInt.getWidth() == 1)
1744 op, getSVEMinEltCount(typeFlags.getEltType()), loc);
1745
1746 // Splat scalar operand to vector (intrinsics with _n infix)
1747 if (typeFlags.hasSplatOperand()) {
1748 unsigned opNo = typeFlags.getSplatOperand();
1749 ops[opNo] = cir::VecSplatOp::create(
1750 builder, loc, getSVEVectorForElementType(cgm, ops[opNo].getType()),
1751 ops[opNo]);
1752 }
1753
1754 if (typeFlags.isReverseCompare())
1755 cgm.errorNYI(expr->getSourceRange(),
1756 std::string("unimplemented AArch64 builtin call: ") +
1757 getContext().BuiltinInfo.getName(builtinID));
1758 if (typeFlags.isReverseUSDOT())
1759 cgm.errorNYI(expr->getSourceRange(),
1760 std::string("unimplemented AArch64 builtin call: ") +
1761 getContext().BuiltinInfo.getName(builtinID));
1762 if (typeFlags.isReverseMergeAnyBinOp() &&
1763 typeFlags.getMergeType() == SVETypeFlags::MergeAny)
1764 cgm.errorNYI(expr->getSourceRange(),
1765 std::string("unimplemented AArch64 builtin call: ") +
1766 getContext().BuiltinInfo.getName(builtinID));
1767 if (typeFlags.isReverseMergeAnyAccOp() &&
1768 typeFlags.getMergeType() == SVETypeFlags::MergeAny)
1769 cgm.errorNYI(expr->getSourceRange(),
1770 std::string("unimplemented AArch64 builtin call: ") +
1771 getContext().BuiltinInfo.getName(builtinID));
1772
1773 // Predicated intrinsics with _z suffix.
1774 if (typeFlags.getMergeType() == SVETypeFlags::MergeZero) {
1775 cgm.errorNYI(expr->getSourceRange(),
1776 std::string("unimplemented AArch64 builtin call: ") +
1777 getContext().BuiltinInfo.getName(builtinID));
1778 }
1779
1780 llvm::StringRef llvmIntrName = getLLVMIntrNameNoPrefix(
1781 static_cast<llvm::Intrinsic::ID>(builtinIntrInfo->LLVMIntrinsic));
1782 auto retTy = convertType(expr->getType());
1783
1784 auto call = builder.emitIntrinsicCallOp(loc, llvmIntrName, retTy,
1785 mlir::ValueRange{ops});
1786 if (call.getType() == retTy)
1787 return call;
1788
1789 // Predicate results must be converted to svbool_t.
1790 if (isa<mlir::VectorType>(retTy) &&
1791 cast<mlir::VectorType>(retTy).isScalable())
1792 cgm.errorNYI(expr->getSourceRange(),
1793 std::string("unimplemented AArch64 builtin call: ") +
1794 getContext().BuiltinInfo.getName(builtinID));
1795 // TODO Handle struct types, e.g. svint8x2_t (update the converter first).
1796
1797 llvm_unreachable("unsupported element count!");
1798 }
1799
1800 // Handle the remaining built-ins.
1801 // -------------------------------
1802 switch (builtinID) {
1803 default:
1804 return std::nullopt;
1805
1806 case SVE::BI__builtin_sve_svreinterpret_b:
1807 case SVE::BI__builtin_sve_svreinterpret_c:
1808 case SVE::BI__builtin_sve_svpsel_lane_b8:
1809 case SVE::BI__builtin_sve_svpsel_lane_b16:
1810 case SVE::BI__builtin_sve_svpsel_lane_b32:
1811 case SVE::BI__builtin_sve_svpsel_lane_b64:
1812 case SVE::BI__builtin_sve_svpsel_lane_c8:
1813 case SVE::BI__builtin_sve_svpsel_lane_c16:
1814 case SVE::BI__builtin_sve_svpsel_lane_c32:
1815 case SVE::BI__builtin_sve_svpsel_lane_c64:
1816 case SVE::BI__builtin_sve_svmov_b_z:
1817 case SVE::BI__builtin_sve_svnot_b_z:
1818 case SVE::BI__builtin_sve_svmovlb_u16:
1819 case SVE::BI__builtin_sve_svmovlb_u32:
1820 case SVE::BI__builtin_sve_svmovlb_u64:
1821 case SVE::BI__builtin_sve_svmovlb_s16:
1822 case SVE::BI__builtin_sve_svmovlb_s32:
1823 case SVE::BI__builtin_sve_svmovlb_s64:
1824 case SVE::BI__builtin_sve_svmovlt_u16:
1825 case SVE::BI__builtin_sve_svmovlt_u32:
1826 case SVE::BI__builtin_sve_svmovlt_u64:
1827 case SVE::BI__builtin_sve_svmovlt_s16:
1828 case SVE::BI__builtin_sve_svmovlt_s32:
1829 case SVE::BI__builtin_sve_svmovlt_s64:
1830 case SVE::BI__builtin_sve_svpmullt_u16:
1831 case SVE::BI__builtin_sve_svpmullt_u64:
1832 case SVE::BI__builtin_sve_svpmullt_n_u16:
1833 case SVE::BI__builtin_sve_svpmullt_n_u64:
1834 case SVE::BI__builtin_sve_svpmullb_u16:
1835 case SVE::BI__builtin_sve_svpmullb_u64:
1836 case SVE::BI__builtin_sve_svpmullb_n_u16:
1837 case SVE::BI__builtin_sve_svpmullb_n_u64:
1838 cgm.errorNYI(expr->getSourceRange(),
1839 std::string("unimplemented AArch64 builtin call: ") +
1840 getContext().BuiltinInfo.getName(builtinID));
1841 return mlir::Value{};
1842
1843 case SVE::BI__builtin_sve_svdup_n_b8:
1844 case SVE::BI__builtin_sve_svdup_n_b16:
1845 case SVE::BI__builtin_sve_svdup_n_b32:
1846 case SVE::BI__builtin_sve_svdup_n_b64: {
1847 // Cast from cir.bool (input type) to cir.int<u, 1> (element type of the
1848 // result vector).
1849 auto dup = builder.createBitcast(ops[0], builder.getUIntNTy(1));
1850
1851 // Splat
1852 dup = cir::VecSplatOp::create(builder, loc, getSVEType(typeFlags), dup);
1853
1854 // Cast to svbool_t, i.e. <vscale x 16 x i1>. The actual result could be
1855 // e.g. <vscale x 8 x i1> (for b16), but only svbool_t (i.e. full
1856 // predicate register) is "storable" (as per SVE ABI).
1857 return builtinID == SVE::BI__builtin_sve_svdup_n_b8
1858 ? dup
1859 : emitSVEPredicateCast(dup, 16, loc);
1860 }
1861
1862 case SVE::BI__builtin_sve_svdupq_n_b8:
1863 case SVE::BI__builtin_sve_svdupq_n_b16:
1864 case SVE::BI__builtin_sve_svdupq_n_b32:
1865 case SVE::BI__builtin_sve_svdupq_n_b64:
1866 case SVE::BI__builtin_sve_svdupq_n_u8:
1867 case SVE::BI__builtin_sve_svdupq_n_s8:
1868 case SVE::BI__builtin_sve_svdupq_n_u64:
1869 case SVE::BI__builtin_sve_svdupq_n_f64:
1870 case SVE::BI__builtin_sve_svdupq_n_s64:
1871 case SVE::BI__builtin_sve_svdupq_n_u16:
1872 case SVE::BI__builtin_sve_svdupq_n_f16:
1873 case SVE::BI__builtin_sve_svdupq_n_bf16:
1874 case SVE::BI__builtin_sve_svdupq_n_s16:
1875 case SVE::BI__builtin_sve_svdupq_n_u32:
1876 case SVE::BI__builtin_sve_svdupq_n_f32:
1877 case SVE::BI__builtin_sve_svdupq_n_s32:
1878 cgm.errorNYI(expr->getSourceRange(),
1879 std::string("unimplemented AArch64 builtin call: ") +
1880 getContext().BuiltinInfo.getName(builtinID));
1881 return mlir::Value{};
1882
1883 case SVE::BI__builtin_sve_svpfalse_b:
1884 return builder.getNullValue(convertType(expr->getType()),
1885 getLoc(expr->getExprLoc()));
1886
1887 case SVE::BI__builtin_sve_svpfalse_c:
1888 cgm.errorNYI(expr->getSourceRange(),
1889 std::string("unimplemented AArch64 builtin call: ") +
1890 getContext().BuiltinInfo.getName(builtinID));
1891 return mlir::Value{};
1892
1893 case SVE::BI__builtin_sve_svlen_u8:
1894 case SVE::BI__builtin_sve_svlen_s8:
1895 return genVscaleTimesFactor(loc, builder, convertType(expr->getType()), 16);
1896
1897 case SVE::BI__builtin_sve_svlen_u16:
1898 case SVE::BI__builtin_sve_svlen_s16:
1899 case SVE::BI__builtin_sve_svlen_f16:
1900 case SVE::BI__builtin_sve_svlen_bf16:
1901 return genVscaleTimesFactor(loc, builder, convertType(expr->getType()), 8);
1902
1903 case SVE::BI__builtin_sve_svlen_u32:
1904 case SVE::BI__builtin_sve_svlen_s32:
1905 case SVE::BI__builtin_sve_svlen_f32:
1906 return genVscaleTimesFactor(loc, builder, convertType(expr->getType()), 4);
1907
1908 case SVE::BI__builtin_sve_svlen_u64:
1909 case SVE::BI__builtin_sve_svlen_s64:
1910 case SVE::BI__builtin_sve_svlen_f64:
1911 return genVscaleTimesFactor(loc, builder, convertType(expr->getType()), 2);
1912
1913 case SVE::BI__builtin_sve_svtbl2_u8:
1914 case SVE::BI__builtin_sve_svtbl2_s8:
1915 case SVE::BI__builtin_sve_svtbl2_u16:
1916 case SVE::BI__builtin_sve_svtbl2_s16:
1917 case SVE::BI__builtin_sve_svtbl2_u32:
1918 case SVE::BI__builtin_sve_svtbl2_s32:
1919 case SVE::BI__builtin_sve_svtbl2_u64:
1920 case SVE::BI__builtin_sve_svtbl2_s64:
1921 case SVE::BI__builtin_sve_svtbl2_f16:
1922 case SVE::BI__builtin_sve_svtbl2_bf16:
1923 case SVE::BI__builtin_sve_svtbl2_f32:
1924 case SVE::BI__builtin_sve_svtbl2_f64:
1925 case SVE::BI__builtin_sve_svset_neonq_s8:
1926 case SVE::BI__builtin_sve_svset_neonq_s16:
1927 case SVE::BI__builtin_sve_svset_neonq_s32:
1928 case SVE::BI__builtin_sve_svset_neonq_s64:
1929 case SVE::BI__builtin_sve_svset_neonq_u8:
1930 case SVE::BI__builtin_sve_svset_neonq_u16:
1931 case SVE::BI__builtin_sve_svset_neonq_u32:
1932 case SVE::BI__builtin_sve_svset_neonq_u64:
1933 case SVE::BI__builtin_sve_svset_neonq_f16:
1934 case SVE::BI__builtin_sve_svset_neonq_f32:
1935 case SVE::BI__builtin_sve_svset_neonq_f64:
1936 case SVE::BI__builtin_sve_svset_neonq_bf16:
1937 case SVE::BI__builtin_sve_svset_neonq_mf8:
1938 case SVE::BI__builtin_sve_svget_neonq_s8:
1939 case SVE::BI__builtin_sve_svget_neonq_s16:
1940 case SVE::BI__builtin_sve_svget_neonq_s32:
1941 case SVE::BI__builtin_sve_svget_neonq_s64:
1942 case SVE::BI__builtin_sve_svget_neonq_u8:
1943 case SVE::BI__builtin_sve_svget_neonq_u16:
1944 case SVE::BI__builtin_sve_svget_neonq_u32:
1945 case SVE::BI__builtin_sve_svget_neonq_u64:
1946 case SVE::BI__builtin_sve_svget_neonq_f16:
1947 case SVE::BI__builtin_sve_svget_neonq_f32:
1948 case SVE::BI__builtin_sve_svget_neonq_f64:
1949 case SVE::BI__builtin_sve_svget_neonq_bf16:
1950 case SVE::BI__builtin_sve_svget_neonq_mf8:
1951 case SVE::BI__builtin_sve_svdup_neonq_s8:
1952 case SVE::BI__builtin_sve_svdup_neonq_s16:
1953 case SVE::BI__builtin_sve_svdup_neonq_s32:
1954 case SVE::BI__builtin_sve_svdup_neonq_s64:
1955 case SVE::BI__builtin_sve_svdup_neonq_u8:
1956 case SVE::BI__builtin_sve_svdup_neonq_u16:
1957 case SVE::BI__builtin_sve_svdup_neonq_u32:
1958 case SVE::BI__builtin_sve_svdup_neonq_u64:
1959 case SVE::BI__builtin_sve_svdup_neonq_f16:
1960 case SVE::BI__builtin_sve_svdup_neonq_f32:
1961 case SVE::BI__builtin_sve_svdup_neonq_f64:
1962 case SVE::BI__builtin_sve_svdup_neonq_bf16:
1963 case SVE::BI__builtin_sve_svdup_neonq_mf8:
1964 cgm.errorNYI(expr->getSourceRange(),
1965 std::string("unimplemented AArch64 builtin call: ") +
1966 getContext().BuiltinInfo.getName(builtinID));
1967 return mlir::Value{};
1968 }
1969
1970 // Unreachable: All cases in the switch above return.
1971}
1972
1973std::optional<mlir::Value>
1975 const CallExpr *expr) {
1977
1978 cgm.errorNYI(expr->getSourceRange(),
1979 std::string("unimplemented AArch64 builtin call: ") +
1980 getContext().BuiltinInfo.getName(builtinID));
1981 return mlir::Value{};
1982}
1983
1984// Some intrinsics are equivalent for codegen.
1985static const std::pair<unsigned, unsigned> neonEquivalentIntrinsicMap[] = {
1986 {
1987 NEON::BI__builtin_neon_vabd_f16,
1988 NEON::BI__builtin_neon_vabd_v,
1989 },
1990 {
1991 NEON::BI__builtin_neon_vabdq_f16,
1992 NEON::BI__builtin_neon_vabdq_v,
1993 },
1994 {
1995 NEON::BI__builtin_neon_vabs_f16,
1996 NEON::BI__builtin_neon_vabs_v,
1997 },
1998 {
1999 NEON::BI__builtin_neon_vabsq_f16,
2000 NEON::BI__builtin_neon_vabsq_v,
2001 },
2002 {
2003 NEON::BI__builtin_neon_vcage_f16,
2004 NEON::BI__builtin_neon_vcage_v,
2005 },
2006 {
2007 NEON::BI__builtin_neon_vcageq_f16,
2008 NEON::BI__builtin_neon_vcageq_v,
2009 },
2010 {
2011 NEON::BI__builtin_neon_vcagt_f16,
2012 NEON::BI__builtin_neon_vcagt_v,
2013 },
2014 {
2015 NEON::BI__builtin_neon_vcagtq_f16,
2016 NEON::BI__builtin_neon_vcagtq_v,
2017 },
2018 {
2019 NEON::BI__builtin_neon_vcale_f16,
2020 NEON::BI__builtin_neon_vcale_v,
2021 },
2022 {
2023 NEON::BI__builtin_neon_vcaleq_f16,
2024 NEON::BI__builtin_neon_vcaleq_v,
2025 },
2026 {
2027 NEON::BI__builtin_neon_vcalt_f16,
2028 NEON::BI__builtin_neon_vcalt_v,
2029 },
2030 {
2031 NEON::BI__builtin_neon_vcaltq_f16,
2032 NEON::BI__builtin_neon_vcaltq_v,
2033 },
2034 {
2035 NEON::BI__builtin_neon_vceqz_f16,
2036 NEON::BI__builtin_neon_vceqz_v,
2037 },
2038 {
2039 NEON::BI__builtin_neon_vceqzq_f16,
2040 NEON::BI__builtin_neon_vceqzq_v,
2041 },
2042 {
2043 NEON::BI__builtin_neon_vcgez_f16,
2044 NEON::BI__builtin_neon_vcgez_v,
2045 },
2046 {
2047 NEON::BI__builtin_neon_vcgezq_f16,
2048 NEON::BI__builtin_neon_vcgezq_v,
2049 },
2050 {
2051 NEON::BI__builtin_neon_vcgtz_f16,
2052 NEON::BI__builtin_neon_vcgtz_v,
2053 },
2054 {
2055 NEON::BI__builtin_neon_vcgtzq_f16,
2056 NEON::BI__builtin_neon_vcgtzq_v,
2057 },
2058 {
2059 NEON::BI__builtin_neon_vclez_f16,
2060 NEON::BI__builtin_neon_vclez_v,
2061 },
2062 {
2063 NEON::BI__builtin_neon_vclezq_f16,
2064 NEON::BI__builtin_neon_vclezq_v,
2065 },
2066 {
2067 NEON::BI__builtin_neon_vcltz_f16,
2068 NEON::BI__builtin_neon_vcltz_v,
2069 },
2070 {
2071 NEON::BI__builtin_neon_vcltzq_f16,
2072 NEON::BI__builtin_neon_vcltzq_v,
2073 },
2074 {
2075 NEON::BI__builtin_neon_vfma_f16,
2076 NEON::BI__builtin_neon_vfma_v,
2077 },
2078 {
2079 NEON::BI__builtin_neon_vfma_lane_f16,
2080 NEON::BI__builtin_neon_vfma_lane_v,
2081 },
2082 {
2083 NEON::BI__builtin_neon_vfma_laneq_f16,
2084 NEON::BI__builtin_neon_vfma_laneq_v,
2085 },
2086 {
2087 NEON::BI__builtin_neon_vfmaq_f16,
2088 NEON::BI__builtin_neon_vfmaq_v,
2089 },
2090 {
2091 NEON::BI__builtin_neon_vfmaq_lane_f16,
2092 NEON::BI__builtin_neon_vfmaq_lane_v,
2093 },
2094 {
2095 NEON::BI__builtin_neon_vfmaq_laneq_f16,
2096 NEON::BI__builtin_neon_vfmaq_laneq_v,
2097 },
2098 {
2099 NEON::BI__builtin_neon_vmax_f16,
2100 NEON::BI__builtin_neon_vmax_v,
2101 },
2102 {
2103 NEON::BI__builtin_neon_vmaxnm_f16,
2104 NEON::BI__builtin_neon_vmaxnm_v,
2105 },
2106 {
2107 NEON::BI__builtin_neon_vmaxnmq_f16,
2108 NEON::BI__builtin_neon_vmaxnmq_v,
2109 },
2110 {
2111 NEON::BI__builtin_neon_vmaxq_f16,
2112 NEON::BI__builtin_neon_vmaxq_v,
2113 },
2114 {
2115 NEON::BI__builtin_neon_vmin_f16,
2116 NEON::BI__builtin_neon_vmin_v,
2117 },
2118 {
2119 NEON::BI__builtin_neon_vminnm_f16,
2120 NEON::BI__builtin_neon_vminnm_v,
2121 },
2122 {
2123 NEON::BI__builtin_neon_vminnmq_f16,
2124 NEON::BI__builtin_neon_vminnmq_v,
2125 },
2126 {
2127 NEON::BI__builtin_neon_vminq_f16,
2128 NEON::BI__builtin_neon_vminq_v,
2129 },
2130 {
2131 NEON::BI__builtin_neon_vmulx_f16,
2132 NEON::BI__builtin_neon_vmulx_v,
2133 },
2134 {
2135 NEON::BI__builtin_neon_vmulxq_f16,
2136 NEON::BI__builtin_neon_vmulxq_v,
2137 },
2138 {
2139 NEON::BI__builtin_neon_vpadd_f16,
2140 NEON::BI__builtin_neon_vpadd_v,
2141 },
2142 {
2143 NEON::BI__builtin_neon_vpaddq_f16,
2144 NEON::BI__builtin_neon_vpaddq_v,
2145 },
2146 {
2147 NEON::BI__builtin_neon_vpmax_f16,
2148 NEON::BI__builtin_neon_vpmax_v,
2149 },
2150 {
2151 NEON::BI__builtin_neon_vpmaxnm_f16,
2152 NEON::BI__builtin_neon_vpmaxnm_v,
2153 },
2154 {
2155 NEON::BI__builtin_neon_vpmaxnmq_f16,
2156 NEON::BI__builtin_neon_vpmaxnmq_v,
2157 },
2158 {
2159 NEON::BI__builtin_neon_vpmaxq_f16,
2160 NEON::BI__builtin_neon_vpmaxq_v,
2161 },
2162 {
2163 NEON::BI__builtin_neon_vpmin_f16,
2164 NEON::BI__builtin_neon_vpmin_v,
2165 },
2166 {
2167 NEON::BI__builtin_neon_vpminnm_f16,
2168 NEON::BI__builtin_neon_vpminnm_v,
2169 },
2170 {
2171 NEON::BI__builtin_neon_vpminnmq_f16,
2172 NEON::BI__builtin_neon_vpminnmq_v,
2173 },
2174 {
2175 NEON::BI__builtin_neon_vpminq_f16,
2176 NEON::BI__builtin_neon_vpminq_v,
2177 },
2178 {
2179 NEON::BI__builtin_neon_vrecpe_f16,
2180 NEON::BI__builtin_neon_vrecpe_v,
2181 },
2182 {
2183 NEON::BI__builtin_neon_vrecpeq_f16,
2184 NEON::BI__builtin_neon_vrecpeq_v,
2185 },
2186 {
2187 NEON::BI__builtin_neon_vrecps_f16,
2188 NEON::BI__builtin_neon_vrecps_v,
2189 },
2190 {
2191 NEON::BI__builtin_neon_vrecpsq_f16,
2192 NEON::BI__builtin_neon_vrecpsq_v,
2193 },
2194 {
2195 NEON::BI__builtin_neon_vrnd_f16,
2196 NEON::BI__builtin_neon_vrnd_v,
2197 },
2198 {
2199 NEON::BI__builtin_neon_vrnda_f16,
2200 NEON::BI__builtin_neon_vrnda_v,
2201 },
2202 {
2203 NEON::BI__builtin_neon_vrndaq_f16,
2204 NEON::BI__builtin_neon_vrndaq_v,
2205 },
2206 {
2207 NEON::BI__builtin_neon_vrndi_f16,
2208 NEON::BI__builtin_neon_vrndi_v,
2209 },
2210 {
2211 NEON::BI__builtin_neon_vrndiq_f16,
2212 NEON::BI__builtin_neon_vrndiq_v,
2213 },
2214 {
2215 NEON::BI__builtin_neon_vrndm_f16,
2216 NEON::BI__builtin_neon_vrndm_v,
2217 },
2218 {
2219 NEON::BI__builtin_neon_vrndmq_f16,
2220 NEON::BI__builtin_neon_vrndmq_v,
2221 },
2222 {
2223 NEON::BI__builtin_neon_vrndn_f16,
2224 NEON::BI__builtin_neon_vrndn_v,
2225 },
2226 {
2227 NEON::BI__builtin_neon_vrndnq_f16,
2228 NEON::BI__builtin_neon_vrndnq_v,
2229 },
2230 {
2231 NEON::BI__builtin_neon_vrndp_f16,
2232 NEON::BI__builtin_neon_vrndp_v,
2233 },
2234 {
2235 NEON::BI__builtin_neon_vrndpq_f16,
2236 NEON::BI__builtin_neon_vrndpq_v,
2237 },
2238 {
2239 NEON::BI__builtin_neon_vrndq_f16,
2240 NEON::BI__builtin_neon_vrndq_v,
2241 },
2242 {
2243 NEON::BI__builtin_neon_vrndx_f16,
2244 NEON::BI__builtin_neon_vrndx_v,
2245 },
2246 {
2247 NEON::BI__builtin_neon_vrndxq_f16,
2248 NEON::BI__builtin_neon_vrndxq_v,
2249 },
2250 {
2251 NEON::BI__builtin_neon_vrsqrte_f16,
2252 NEON::BI__builtin_neon_vrsqrte_v,
2253 },
2254 {
2255 NEON::BI__builtin_neon_vrsqrteq_f16,
2256 NEON::BI__builtin_neon_vrsqrteq_v,
2257 },
2258 {
2259 NEON::BI__builtin_neon_vrsqrts_f16,
2260 NEON::BI__builtin_neon_vrsqrts_v,
2261 },
2262 {
2263 NEON::BI__builtin_neon_vrsqrtsq_f16,
2264 NEON::BI__builtin_neon_vrsqrtsq_v,
2265 },
2266 {
2267 NEON::BI__builtin_neon_vsqrt_f16,
2268 NEON::BI__builtin_neon_vsqrt_v,
2269 },
2270 {
2271 NEON::BI__builtin_neon_vsqrtq_f16,
2272 NEON::BI__builtin_neon_vsqrtq_v,
2273 },
2274 // The mangling rules cause us to have one ID for each type for
2275 // vldap1(q)_lane and vstl1(q)_lane, but codegen is equivalent for all of
2276 // them. Choose an arbitrary one to be handled as tha canonical variation.
2277 {NEON::BI__builtin_neon_vldap1_lane_u64,
2278 NEON::BI__builtin_neon_vldap1_lane_s64},
2279 {NEON::BI__builtin_neon_vldap1_lane_f64,
2280 NEON::BI__builtin_neon_vldap1_lane_s64},
2281 {NEON::BI__builtin_neon_vldap1_lane_p64,
2282 NEON::BI__builtin_neon_vldap1_lane_s64},
2283 {NEON::BI__builtin_neon_vldap1q_lane_u64,
2284 NEON::BI__builtin_neon_vldap1q_lane_s64},
2285 {NEON::BI__builtin_neon_vldap1q_lane_f64,
2286 NEON::BI__builtin_neon_vldap1q_lane_s64},
2287 {NEON::BI__builtin_neon_vldap1q_lane_p64,
2288 NEON::BI__builtin_neon_vldap1q_lane_s64},
2289 {NEON::BI__builtin_neon_vstl1_lane_u64,
2290 NEON::BI__builtin_neon_vstl1_lane_s64},
2291 {NEON::BI__builtin_neon_vstl1_lane_f64,
2292 NEON::BI__builtin_neon_vstl1_lane_s64},
2293 {NEON::BI__builtin_neon_vstl1_lane_p64,
2294 NEON::BI__builtin_neon_vstl1_lane_s64},
2295 {NEON::BI__builtin_neon_vstl1q_lane_u64,
2296 NEON::BI__builtin_neon_vstl1q_lane_s64},
2297 {NEON::BI__builtin_neon_vstl1q_lane_f64,
2298 NEON::BI__builtin_neon_vstl1q_lane_s64},
2299 {NEON::BI__builtin_neon_vstl1q_lane_p64,
2300 NEON::BI__builtin_neon_vstl1q_lane_s64},
2301};
2302
2303std::optional<mlir::Value>
2306 llvm::Triple::ArchType arch) {
2307 if (builtinID >= clang::AArch64::FirstSVEBuiltin &&
2308 builtinID <= clang::AArch64::LastSVEBuiltin)
2309 return emitAArch64SVEBuiltinExpr(builtinID, expr);
2310
2311 if (builtinID >= clang::AArch64::FirstSMEBuiltin &&
2312 builtinID <= clang::AArch64::LastSMEBuiltin)
2313 return emitAArch64SMEBuiltinExpr(builtinID, expr);
2314
2315 if (builtinID == Builtin::BI__builtin_cpu_supports) {
2316 cgm.errorNYI(expr->getSourceRange(),
2317 std::string("unimplemented AArch64 builtin call: ") +
2318 getContext().BuiltinInfo.getName(builtinID));
2319 return mlir::Value{};
2320 }
2321
2322 switch (builtinID) {
2323 default:
2324 break;
2325 case clang::AArch64::BI__builtin_arm_nop:
2326 case clang::AArch64::BI__builtin_arm_yield:
2327 case clang::AArch64::BI__yield:
2328 case clang::AArch64::BI__builtin_arm_wfe:
2329 case clang::AArch64::BI__wfe:
2330 case clang::AArch64::BI__builtin_arm_wfi:
2331 case clang::AArch64::BI__wfi:
2332 case clang::AArch64::BI__builtin_arm_sev:
2333 case clang::AArch64::BI__sev:
2334 case clang::AArch64::BI__builtin_arm_sevl:
2335 case clang::AArch64::BI__sevl:
2336 cgm.errorNYI(expr->getSourceRange(),
2337 std::string("unimplemented AArch64 builtin call: ") +
2338 getContext().BuiltinInfo.getName(builtinID));
2339 return mlir::Value{};
2340 }
2341
2342 if (builtinID == clang::AArch64::BI__builtin_arm_trap) {
2343 cgm.errorNYI(expr->getSourceRange(),
2344 std::string("unimplemented AArch64 builtin call: ") +
2345 getContext().BuiltinInfo.getName(builtinID));
2346 return mlir::Value{};
2347 }
2348
2349 if (builtinID == clang::AArch64::BI__builtin_arm_get_sme_state) {
2350 cgm.errorNYI(expr->getSourceRange(),
2351 std::string("unimplemented AArch64 builtin call: ") +
2352 getContext().BuiltinInfo.getName(builtinID));
2353 return mlir::Value{};
2354 }
2355
2356 if (builtinID == clang::AArch64::BI__builtin_arm_rbit) {
2357 cgm.errorNYI(expr->getSourceRange(),
2358 std::string("unimplemented AArch64 builtin call: ") +
2359 getContext().BuiltinInfo.getName(builtinID));
2360 return mlir::Value{};
2361 }
2362 if (builtinID == clang::AArch64::BI__builtin_arm_rbit64) {
2363 cgm.errorNYI(expr->getSourceRange(),
2364 std::string("unimplemented AArch64 builtin call: ") +
2365 getContext().BuiltinInfo.getName(builtinID));
2366 return mlir::Value{};
2367 }
2368
2369 if (builtinID == clang::AArch64::BI__builtin_arm_clz ||
2370 builtinID == clang::AArch64::BI__builtin_arm_clz64) {
2371 cgm.errorNYI(expr->getSourceRange(),
2372 std::string("unimplemented AArch64 builtin call: ") +
2373 getContext().BuiltinInfo.getName(builtinID));
2374 return mlir::Value{};
2375 }
2376
2377 if (builtinID == clang::AArch64::BI__builtin_arm_cls) {
2378 cgm.errorNYI(expr->getSourceRange(),
2379 std::string("unimplemented AArch64 builtin call: ") +
2380 getContext().BuiltinInfo.getName(builtinID));
2381 return mlir::Value{};
2382 }
2383 if (builtinID == clang::AArch64::BI__builtin_arm_cls64) {
2384 cgm.errorNYI(expr->getSourceRange(),
2385 std::string("unimplemented AArch64 builtin call: ") +
2386 getContext().BuiltinInfo.getName(builtinID));
2387 return mlir::Value{};
2388 }
2389
2390 if (builtinID == clang::AArch64::BI__builtin_arm_rint32zf ||
2391 builtinID == clang::AArch64::BI__builtin_arm_rint32z) {
2392 cgm.errorNYI(expr->getSourceRange(),
2393 std::string("unimplemented AArch64 builtin call: ") +
2394 getContext().BuiltinInfo.getName(builtinID));
2395 return mlir::Value{};
2396 }
2397
2398 if (builtinID == clang::AArch64::BI__builtin_arm_rint64zf ||
2399 builtinID == clang::AArch64::BI__builtin_arm_rint64z) {
2400 cgm.errorNYI(expr->getSourceRange(),
2401 std::string("unimplemented AArch64 builtin call: ") +
2402 getContext().BuiltinInfo.getName(builtinID));
2403 return mlir::Value{};
2404 }
2405
2406 if (builtinID == clang::AArch64::BI__builtin_arm_rint32xf ||
2407 builtinID == clang::AArch64::BI__builtin_arm_rint32x) {
2408 cgm.errorNYI(expr->getSourceRange(),
2409 std::string("unimplemented AArch64 builtin call: ") +
2410 getContext().BuiltinInfo.getName(builtinID));
2411 return mlir::Value{};
2412 }
2413
2414 if (builtinID == clang::AArch64::BI__builtin_arm_rint64xf ||
2415 builtinID == clang::AArch64::BI__builtin_arm_rint64x) {
2416 cgm.errorNYI(expr->getSourceRange(),
2417 std::string("unimplemented AArch64 builtin call: ") +
2418 getContext().BuiltinInfo.getName(builtinID));
2419 return mlir::Value{};
2420 }
2421
2422 if (builtinID == clang::AArch64::BI__builtin_arm_jcvt) {
2423 cgm.errorNYI(expr->getSourceRange(),
2424 std::string("unimplemented AArch64 builtin call: ") +
2425 getContext().BuiltinInfo.getName(builtinID));
2426 return mlir::Value{};
2427 }
2428
2429 if (builtinID == clang::AArch64::BI__builtin_arm_ld64b ||
2430 builtinID == clang::AArch64::BI__builtin_arm_st64b ||
2431 builtinID == clang::AArch64::BI__builtin_arm_st64bv ||
2432 builtinID == clang::AArch64::BI__builtin_arm_st64bv0) {
2433 cgm.errorNYI(expr->getSourceRange(),
2434 std::string("unimplemented AArch64 builtin call: ") +
2435 getContext().BuiltinInfo.getName(builtinID));
2436 return mlir::Value{};
2437 }
2438
2439 if (builtinID == clang::AArch64::BI__builtin_arm_rndr ||
2440 builtinID == clang::AArch64::BI__builtin_arm_rndrrs) {
2441 cgm.errorNYI(expr->getSourceRange(),
2442 std::string("unimplemented AArch64 builtin call: ") +
2443 getContext().BuiltinInfo.getName(builtinID));
2444 return mlir::Value{};
2445 }
2446
2447 if (builtinID == clang::AArch64::BI__clear_cache) {
2448 cgm.errorNYI(expr->getSourceRange(),
2449 std::string("unimplemented AArch64 builtin call: ") +
2450 getContext().BuiltinInfo.getName(builtinID));
2451 return mlir::Value{};
2452 }
2453
2454 if ((builtinID == clang::AArch64::BI__builtin_arm_ldrex ||
2455 builtinID == clang::AArch64::BI__builtin_arm_ldaex) &&
2456 getContext().getTypeSize(expr->getType()) == 128) {
2457 cgm.errorNYI(expr->getSourceRange(),
2458 std::string("unimplemented AArch64 builtin call: ") +
2459 getContext().BuiltinInfo.getName(builtinID));
2460 return mlir::Value{};
2461 }
2462 if (builtinID == clang::AArch64::BI__builtin_arm_ldrex ||
2463 builtinID == clang::AArch64::BI__builtin_arm_ldaex) {
2464 cgm.errorNYI(expr->getSourceRange(),
2465 std::string("unimplemented AArch64 builtin call: ") +
2466 getContext().BuiltinInfo.getName(builtinID));
2467 return mlir::Value{};
2468 }
2469
2470 if ((builtinID == clang::AArch64::BI__builtin_arm_strex ||
2471 builtinID == clang::AArch64::BI__builtin_arm_stlex) &&
2472 getContext().getTypeSize(expr->getArg(0)->getType()) == 128) {
2473 cgm.errorNYI(expr->getSourceRange(),
2474 std::string("unimplemented AArch64 builtin call: ") +
2475 getContext().BuiltinInfo.getName(builtinID));
2476 return mlir::Value{};
2477 }
2478
2479 if (builtinID == clang::AArch64::BI__builtin_arm_strex ||
2480 builtinID == clang::AArch64::BI__builtin_arm_stlex) {
2481 cgm.errorNYI(expr->getSourceRange(),
2482 std::string("unimplemented AArch64 builtin call: ") +
2483 getContext().BuiltinInfo.getName(builtinID));
2484 return mlir::Value{};
2485 }
2486
2487 if (builtinID == clang::AArch64::BI__getReg ||
2488 builtinID == clang::AArch64::BI__setReg ||
2489 builtinID == clang::AArch64::BI__getRegFp ||
2490 builtinID == clang::AArch64::BI__setRegFp) {
2491 cgm.errorNYI(expr->getSourceRange(),
2492 std::string("unimplemented AArch64 builtin call: ") +
2493 getContext().BuiltinInfo.getName(builtinID));
2494 return mlir::Value{};
2495 }
2496
2497 if (builtinID == clang::AArch64::BI__break) {
2498 cgm.errorNYI(expr->getSourceRange(),
2499 std::string("unimplemented AArch64 builtin call: ") +
2500 getContext().BuiltinInfo.getName(builtinID));
2501 return mlir::Value{};
2502 }
2503
2504 if (builtinID == clang::AArch64::BI__builtin_arm_clrex) {
2505 cgm.errorNYI(expr->getSourceRange(),
2506 std::string("unimplemented AArch64 builtin call: ") +
2507 getContext().BuiltinInfo.getName(builtinID));
2508 return mlir::Value{};
2509 }
2510
2511 if (builtinID == clang::AArch64::BI_ReadWriteBarrier) {
2512 cgm.errorNYI(expr->getSourceRange(),
2513 std::string("unimplemented AArch64 builtin call: ") +
2514 getContext().BuiltinInfo.getName(builtinID));
2515 return mlir::Value{};
2516 }
2517
2518 // CRC32
2519 Intrinsic::ID crcIntrinsicID = Intrinsic::not_intrinsic;
2520 switch (builtinID) {
2521 case clang::AArch64::BI__builtin_arm_crc32b:
2522 crcIntrinsicID = Intrinsic::aarch64_crc32b;
2523 break;
2524 case clang::AArch64::BI__builtin_arm_crc32cb:
2525 crcIntrinsicID = Intrinsic::aarch64_crc32cb;
2526 break;
2527 case clang::AArch64::BI__builtin_arm_crc32h:
2528 crcIntrinsicID = Intrinsic::aarch64_crc32h;
2529 break;
2530 case clang::AArch64::BI__builtin_arm_crc32ch:
2531 crcIntrinsicID = Intrinsic::aarch64_crc32ch;
2532 break;
2533 case clang::AArch64::BI__builtin_arm_crc32w:
2534 crcIntrinsicID = Intrinsic::aarch64_crc32w;
2535 break;
2536 case clang::AArch64::BI__builtin_arm_crc32cw:
2537 crcIntrinsicID = Intrinsic::aarch64_crc32cw;
2538 break;
2539 case clang::AArch64::BI__builtin_arm_crc32d:
2540 crcIntrinsicID = Intrinsic::aarch64_crc32x;
2541 break;
2542 case clang::AArch64::BI__builtin_arm_crc32cd:
2543 crcIntrinsicID = Intrinsic::aarch64_crc32cx;
2544 break;
2545 }
2546
2547 if (crcIntrinsicID != Intrinsic::not_intrinsic) {
2548 cgm.errorNYI(expr->getSourceRange(),
2549 std::string("unimplemented AArch64 builtin call: ") +
2550 getContext().BuiltinInfo.getName(builtinID));
2551 return mlir::Value{};
2552 }
2553
2554 // Memory Operations (MOPS)
2555 if (builtinID == AArch64::BI__builtin_arm_mops_memset_tag) {
2556 cgm.errorNYI(expr->getSourceRange(),
2557 std::string("unimplemented AArch64 builtin call: ") +
2558 getContext().BuiltinInfo.getName(builtinID));
2559 return mlir::Value{};
2560 }
2561
2562 // Memory Tagging Extensions (MTE) Intrinsics
2563 Intrinsic::ID mteIntrinsicID = Intrinsic::not_intrinsic;
2564 switch (builtinID) {
2565 case clang::AArch64::BI__builtin_arm_irg:
2566 mteIntrinsicID = Intrinsic::aarch64_irg;
2567 break;
2568 case clang::AArch64::BI__builtin_arm_addg:
2569 mteIntrinsicID = Intrinsic::aarch64_addg;
2570 break;
2571 case clang::AArch64::BI__builtin_arm_gmi:
2572 mteIntrinsicID = Intrinsic::aarch64_gmi;
2573 break;
2574 case clang::AArch64::BI__builtin_arm_ldg:
2575 mteIntrinsicID = Intrinsic::aarch64_ldg;
2576 break;
2577 case clang::AArch64::BI__builtin_arm_stg:
2578 mteIntrinsicID = Intrinsic::aarch64_stg;
2579 break;
2580 case clang::AArch64::BI__builtin_arm_subp:
2581 mteIntrinsicID = Intrinsic::aarch64_subp;
2582 break;
2583 }
2584
2585 if (mteIntrinsicID != Intrinsic::not_intrinsic) {
2586 cgm.errorNYI(expr->getSourceRange(),
2587 std::string("unimplemented AArch64 builtin call: ") +
2588 getContext().BuiltinInfo.getName(builtinID));
2589 return mlir::Value{};
2590 }
2591
2592 if (builtinID == clang::AArch64::BI__builtin_arm_rsr ||
2593 builtinID == clang::AArch64::BI__builtin_arm_rsr64 ||
2594 builtinID == clang::AArch64::BI__builtin_arm_rsr128 ||
2595 builtinID == clang::AArch64::BI__builtin_arm_rsrp ||
2596 builtinID == clang::AArch64::BI__builtin_arm_wsr ||
2597 builtinID == clang::AArch64::BI__builtin_arm_wsr64 ||
2598 builtinID == clang::AArch64::BI__builtin_arm_wsr128 ||
2599 builtinID == clang::AArch64::BI__builtin_arm_wsrp) {
2600 cgm.errorNYI(expr->getSourceRange(),
2601 std::string("unimplemented AArch64 builtin call: ") +
2602 getContext().BuiltinInfo.getName(builtinID));
2603 return mlir::Value{};
2604 }
2605
2606 if (builtinID == clang::AArch64::BI_ReadStatusReg ||
2607 builtinID == clang::AArch64::BI_WriteStatusReg ||
2608 builtinID == clang::AArch64::BI__sys) {
2609 cgm.errorNYI(expr->getSourceRange(),
2610 std::string("unimplemented AArch64 builtin call: ") +
2611 getContext().BuiltinInfo.getName(builtinID));
2612 return mlir::Value{};
2613 }
2614
2615 if (builtinID == clang::AArch64::BI_AddressOfReturnAddress) {
2616 cgm.errorNYI(expr->getSourceRange(),
2617 std::string("unimplemented AArch64 builtin call: ") +
2618 getContext().BuiltinInfo.getName(builtinID));
2619 return mlir::Value{};
2620 }
2621
2622 if (builtinID == clang::AArch64::BI__builtin_sponentry) {
2623 cgm.errorNYI(expr->getSourceRange(),
2624 std::string("unimplemented AArch64 builtin call: ") +
2625 getContext().BuiltinInfo.getName(builtinID));
2626 return mlir::Value{};
2627 }
2628
2629 if (builtinID == clang::AArch64::BI__mulh ||
2630 builtinID == clang::AArch64::BI__umulh) {
2631 cgm.errorNYI(expr->getSourceRange(),
2632 std::string("unimplemented AArch64 builtin call: ") +
2633 getContext().BuiltinInfo.getName(builtinID));
2634 return mlir::Value{};
2635 }
2636
2637 if (builtinID == AArch64::BI__writex18byte ||
2638 builtinID == AArch64::BI__writex18word ||
2639 builtinID == AArch64::BI__writex18dword ||
2640 builtinID == AArch64::BI__writex18qword) {
2641 cgm.errorNYI(expr->getSourceRange(),
2642 std::string("unimplemented AArch64 builtin call: ") +
2643 getContext().BuiltinInfo.getName(builtinID));
2644 return mlir::Value{};
2645 }
2646
2647 if (builtinID == AArch64::BI__readx18byte ||
2648 builtinID == AArch64::BI__readx18word ||
2649 builtinID == AArch64::BI__readx18dword ||
2650 builtinID == AArch64::BI__readx18qword) {
2651 cgm.errorNYI(expr->getSourceRange(),
2652 std::string("unimplemented AArch64 builtin call: ") +
2653 getContext().BuiltinInfo.getName(builtinID));
2654 return mlir::Value{};
2655 }
2656
2657 if (builtinID == AArch64::BI__addx18byte ||
2658 builtinID == AArch64::BI__addx18word ||
2659 builtinID == AArch64::BI__addx18dword ||
2660 builtinID == AArch64::BI__addx18qword ||
2661 builtinID == AArch64::BI__incx18byte ||
2662 builtinID == AArch64::BI__incx18word ||
2663 builtinID == AArch64::BI__incx18dword ||
2664 builtinID == AArch64::BI__incx18qword) {
2665 cgm.errorNYI(expr->getSourceRange(),
2666 std::string("unimplemented AArch64 builtin call: ") +
2667 getContext().BuiltinInfo.getName(builtinID));
2668 return mlir::Value{};
2669 }
2670
2671 if (builtinID == AArch64::BI_CopyDoubleFromInt64 ||
2672 builtinID == AArch64::BI_CopyFloatFromInt32 ||
2673 builtinID == AArch64::BI_CopyInt32FromFloat ||
2674 builtinID == AArch64::BI_CopyInt64FromDouble) {
2675 cgm.errorNYI(expr->getSourceRange(),
2676 std::string("unimplemented AArch64 builtin call: ") +
2677 getContext().BuiltinInfo.getName(builtinID));
2678 return mlir::Value{};
2679 }
2680
2681 if (builtinID == AArch64::BI_CountLeadingOnes ||
2682 builtinID == AArch64::BI_CountLeadingOnes64 ||
2683 builtinID == AArch64::BI_CountLeadingZeros ||
2684 builtinID == AArch64::BI_CountLeadingZeros64) {
2685 cgm.errorNYI(expr->getSourceRange(),
2686 std::string("unimplemented AArch64 builtin call: ") +
2687 getContext().BuiltinInfo.getName(builtinID));
2688 return mlir::Value{};
2689 }
2690
2691 if (builtinID == AArch64::BI_CountLeadingSigns ||
2692 builtinID == AArch64::BI_CountLeadingSigns64) {
2693 cgm.errorNYI(expr->getSourceRange(),
2694 std::string("unimplemented AArch64 builtin call: ") +
2695 getContext().BuiltinInfo.getName(builtinID));
2696 return mlir::Value{};
2697 }
2698
2699 if (builtinID == AArch64::BI_CountOneBits ||
2700 builtinID == AArch64::BI_CountOneBits64 ||
2701 builtinID == AArch64::BI_CountTrailingZeros ||
2702 builtinID == AArch64::BI_CountTrailingZeros64) {
2703 cgm.errorNYI(expr->getSourceRange(),
2704 std::string("unimplemented AArch64 builtin call: ") +
2705 getContext().BuiltinInfo.getName(builtinID));
2706 return mlir::Value{};
2707 }
2708
2709 if (builtinID == AArch64::BI__prefetch ||
2710 builtinID == AArch64::BI__prefetch2) {
2711 cgm.errorNYI(expr->getSourceRange(),
2712 std::string("unimplemented AArch64 builtin call: ") +
2713 getContext().BuiltinInfo.getName(builtinID));
2714 return mlir::Value{};
2715 }
2716
2717 if (builtinID == AArch64::BI__hlt) {
2718 cgm.errorNYI(expr->getSourceRange(),
2719 std::string("unimplemented AArch64 builtin call: ") +
2720 getContext().BuiltinInfo.getName(builtinID));
2721 return mlir::Value{};
2722 }
2723
2724 if (builtinID == NEON::BI__builtin_neon_vcvth_bf16_f32) {
2725 cgm.errorNYI(expr->getSourceRange(),
2726 std::string("unimplemented AArch64 builtin call: ") +
2727 getContext().BuiltinInfo.getName(builtinID));
2728 return mlir::Value{};
2729 }
2730
2731 // Handle MSVC intrinsics before argument evaluation to prevent double
2732 // evaluation.
2734
2735 CIRGenFPOptionsRAII fpOptsRAII(*this, expr);
2736
2737 // Some intrinsics are equivalent - if they are use the base intrinsic ID.
2738 auto it = llvm::find_if(neonEquivalentIntrinsicMap, [builtinID](auto &p) {
2739 return p.first == builtinID;
2740 });
2741 if (it != end(neonEquivalentIntrinsicMap))
2742 builtinID = it->second;
2743
2744 // Find out if any arguments are required to be integer constant
2745 // expressions.
2747 unsigned iceArguments = 0;
2749 getContext().GetBuiltinType(builtinID, error, &iceArguments);
2750 assert(error == ASTContext::GE_None && "Should not codegen an error");
2752
2753 // Captures the pointer and its alignment for builtins that store/load
2754 // directly through the first argument, i.e. operand 0 (set in the
2755 // arg-gathering loop below, used later when lowering the store/load
2756 // itself).
2757 Address ptrOp0 = Address::invalid();
2758
2759 // Skip extra arguments used to discriminate vector types and that are
2760 // intended for Sema checking.
2761 bool hasExtraArg = CodeGenUtils::hasExtraNeonArgument(builtinID);
2762 unsigned numArgs = expr->getNumArgs() - (hasExtraArg ? 1 : 0);
2763 for (unsigned i = 0, e = numArgs; i != e; i++) {
2764 if (i == 0) {
2765 switch (builtinID) {
2766 case NEON::BI__builtin_neon_vld1_v:
2767 case NEON::BI__builtin_neon_vld1q_v:
2768 case NEON::BI__builtin_neon_vld1_dup_v:
2769 case NEON::BI__builtin_neon_vld1q_dup_v:
2770 case NEON::BI__builtin_neon_vld1_lane_v:
2771 case NEON::BI__builtin_neon_vld1q_lane_v:
2772 case NEON::BI__builtin_neon_vst1_v:
2773 case NEON::BI__builtin_neon_vst1q_v:
2774 case NEON::BI__builtin_neon_vst1_lane_v:
2775 case NEON::BI__builtin_neon_vst1q_lane_v:
2776 case NEON::BI__builtin_neon_vstrq_p128:
2777 case NEON::BI__builtin_neon_vst2_v:
2778 case NEON::BI__builtin_neon_vst2q_v:
2779 case NEON::BI__builtin_neon_vst2_lane_v:
2780 case NEON::BI__builtin_neon_vst2q_lane_v:
2781 case NEON::BI__builtin_neon_vst3_v:
2782 case NEON::BI__builtin_neon_vst3q_v:
2783 case NEON::BI__builtin_neon_vst3_lane_v:
2784 case NEON::BI__builtin_neon_vst3q_lane_v:
2785 case NEON::BI__builtin_neon_vst4_v:
2786 case NEON::BI__builtin_neon_vst4q_v:
2787 case NEON::BI__builtin_neon_vst4_lane_v:
2788 case NEON::BI__builtin_neon_vst4q_lane_v:
2789 // Get the alignment for the argument in addition to the value;
2790 // we'll use it later.
2791 ptrOp0 = emitPointerWithAlignment(expr->getArg(0));
2792 ops.push_back(ptrOp0.getPointer());
2793 continue;
2794 case NEON::BI__builtin_neon_vldap1_lane_s64:
2795 case NEON::BI__builtin_neon_vldap1q_lane_s64:
2796 case NEON::BI__builtin_neon_vstl1_lane_s64:
2797 case NEON::BI__builtin_neon_vstl1q_lane_s64:
2798 cgm.errorNYI(
2799 expr->getSourceRange(),
2800 std::string("unimplemented AArch64 builtin argument handling ") +
2801 getContext().BuiltinInfo.getName(builtinID));
2802 break;
2803 }
2804 }
2805 ops.push_back(
2806 emitScalarOrConstFoldImmArg(iceArguments, i, expr->getArg(i)));
2807 }
2808
2809 const ARMNeonVectorIntrinsicInfo *builtin =
2812 if (builtin)
2813 return emitCommonNeonSISDBuiltinExpr(*this, *builtin, ops, expr,
2814 iceArguments);
2815
2816 // Not all intrinsics handled by the common case work for AArch64 yet, so only
2817 // defer to common code if it's been added to our special map.
2819
2821
2822 const Expr *arg = expr->getArg(expr->getNumArgs() - 1);
2824 // A trailing constant integer is used for discriminating overloaded builtin
2825 // calls. Use it to determine the type of this overloaded NEON intrinsic.
2826 if (std::optional<llvm::APSInt> result =
2827 arg->getIntegerConstantExpr(getContext()))
2828 type = NeonTypeFlags(result->getZExtValue());
2829
2830 bool usgn = type.isUnsigned();
2831
2832 mlir::Location loc = getLoc(expr->getExprLoc());
2833
2834 // Not all intrinsics handled by the common case work for AArch64 yet, so only
2835 // defer to common code if it's been added to our special map.
2836 builtin =
2839 if (builtin)
2841 *this, builtin->BuiltinID, builtin->LLVMIntrinsic,
2842 builtin->AltLLVMIntrinsic, builtin->NameHint, builtin->TypeModifier,
2843 expr, ops);
2844
2845 // Handle non-overloaded intrinsics first.
2846 switch (builtinID) {
2847 default:
2848 break;
2849 case NEON::BI__builtin_neon_vabsh_f16: {
2850 mlir::Type halfTy = builder.getFp16Ty();
2851 return emitNeonCallToOp<cir::FAbsOp>(cgm, builder, {halfTy}, ops,
2852 std::nullopt, halfTy, loc);
2853 }
2854 case NEON::BI__builtin_neon_vaddq_p128: {
2855 cir::VectorType byteTy =
2856 cir::VectorType::getFixed(builder.getUInt8Ty(), 16);
2857 ops[0] = builder.createBitcast(ops[0], byteTy);
2858 ops[1] = builder.createBitcast(ops[1], byteTy);
2859 mlir::Value result = builder.createXor(loc, ops[0], ops[1]);
2860 return builder.createBitcast(result, convertType(expr->getType()));
2861 }
2862 case NEON::BI__builtin_neon_vldrq_p128:
2863 cgm.errorNYI(expr->getSourceRange(),
2864 std::string("unimplemented AArch64 builtin call: ") +
2865 getContext().BuiltinInfo.getName(builtinID));
2866 return mlir::Value{};
2867 case NEON::BI__builtin_neon_vstrq_p128: {
2868 builder.createStore(loc, ops[1], ptrOp0);
2869 return nullptr;
2870 }
2871 case NEON::BI__builtin_neon_vcvts_f32_u32:
2872 case NEON::BI__builtin_neon_vcvtd_f64_u64:
2873 case NEON::BI__builtin_neon_vcvts_f32_s32:
2874 case NEON::BI__builtin_neon_vcvtd_f64_s64: {
2875 return builder.createCast(loc, cir::CastKind::int_to_float, ops[0],
2876 convertType(expr->getType()));
2877 }
2878 case NEON::BI__builtin_neon_vcvth_f16_u16:
2879 case NEON::BI__builtin_neon_vcvth_f16_u32:
2880 case NEON::BI__builtin_neon_vcvth_f16_u64:
2881 case NEON::BI__builtin_neon_vcvth_f16_s16:
2882 case NEON::BI__builtin_neon_vcvth_f16_s32:
2883 case NEON::BI__builtin_neon_vcvth_f16_s64:
2884 case NEON::BI__builtin_neon_vcvtah_u16_f16:
2885 case NEON::BI__builtin_neon_vcvtmh_u16_f16:
2886 case NEON::BI__builtin_neon_vcvtnh_u16_f16:
2887 case NEON::BI__builtin_neon_vcvtph_u16_f16:
2888 case NEON::BI__builtin_neon_vcvth_u16_f16:
2889 case NEON::BI__builtin_neon_vcvtah_s16_f16:
2890 case NEON::BI__builtin_neon_vcvtmh_s16_f16:
2891 case NEON::BI__builtin_neon_vcvtnh_s16_f16:
2892 case NEON::BI__builtin_neon_vcvtph_s16_f16:
2893 case NEON::BI__builtin_neon_vcvth_s16_f16:
2894 case NEON::BI__builtin_neon_vcaleh_f16:
2895 case NEON::BI__builtin_neon_vcalth_f16:
2896 case NEON::BI__builtin_neon_vcageh_f16:
2897 case NEON::BI__builtin_neon_vcagth_f16:
2898 case NEON::BI__builtin_neon_vcvth_n_s16_f16:
2899 case NEON::BI__builtin_neon_vcvth_n_u16_f16:
2900 case NEON::BI__builtin_neon_vcvth_n_f16_s16:
2901 case NEON::BI__builtin_neon_vcvth_n_f16_u16:
2902 case NEON::BI__builtin_neon_vpaddd_s64:
2903 case NEON::BI__builtin_neon_vpaddd_f64:
2904 case NEON::BI__builtin_neon_vpadds_f32:
2905 cgm.errorNYI(expr->getSourceRange(),
2906 std::string("unimplemented AArch64 builtin call: ") +
2907 getContext().BuiltinInfo.getName(builtinID));
2908 return mlir::Value{};
2909 case NEON::BI__builtin_neon_vceqzd_s64:
2910 case NEON::BI__builtin_neon_vceqzd_f64:
2911 case NEON::BI__builtin_neon_vceqzs_f32:
2912 case NEON::BI__builtin_neon_vceqzh_f16:
2914 *this, builder, loc, ops[0],
2915 convertType(expr->getCallReturnType(getContext())), cir::CmpOpKind::eq);
2916 case NEON::BI__builtin_neon_vcgezd_s64:
2917 case NEON::BI__builtin_neon_vcgezd_f64:
2918 case NEON::BI__builtin_neon_vcgezs_f32:
2919 case NEON::BI__builtin_neon_vcgezh_f16:
2920 case NEON::BI__builtin_neon_vclezd_s64:
2921 case NEON::BI__builtin_neon_vclezd_f64:
2922 case NEON::BI__builtin_neon_vclezs_f32:
2923 case NEON::BI__builtin_neon_vclezh_f16:
2924 case NEON::BI__builtin_neon_vcgtzd_s64:
2925 case NEON::BI__builtin_neon_vcgtzd_f64:
2926 case NEON::BI__builtin_neon_vcgtzs_f32:
2927 case NEON::BI__builtin_neon_vcgtzh_f16:
2928 case NEON::BI__builtin_neon_vcltzd_s64:
2929 case NEON::BI__builtin_neon_vcltzd_f64:
2930 case NEON::BI__builtin_neon_vcltzs_f32:
2931 case NEON::BI__builtin_neon_vcltzh_f16:
2932 case NEON::BI__builtin_neon_vceqzd_u64: {
2934 *this, builder, loc, ops[0],
2935 convertType(expr->getCallReturnType(getContext())), cir::CmpOpKind::eq);
2936 }
2937 case NEON::BI__builtin_neon_vceqd_f64:
2938 case NEON::BI__builtin_neon_vcled_f64:
2939 case NEON::BI__builtin_neon_vcltd_f64:
2940 case NEON::BI__builtin_neon_vcged_f64:
2941 case NEON::BI__builtin_neon_vcgtd_f64:
2942 case NEON::BI__builtin_neon_vceqs_f32:
2943 case NEON::BI__builtin_neon_vcles_f32:
2944 case NEON::BI__builtin_neon_vclts_f32:
2945 case NEON::BI__builtin_neon_vcges_f32:
2946 case NEON::BI__builtin_neon_vcgts_f32:
2947 case NEON::BI__builtin_neon_vceqh_f16:
2948 case NEON::BI__builtin_neon_vcleh_f16:
2949 case NEON::BI__builtin_neon_vclth_f16:
2950 case NEON::BI__builtin_neon_vcgeh_f16:
2951 case NEON::BI__builtin_neon_vcgth_f16:
2952 case NEON::BI__builtin_neon_vceqd_s64:
2953 case NEON::BI__builtin_neon_vceqd_u64:
2954 case NEON::BI__builtin_neon_vcgtd_s64:
2955 case NEON::BI__builtin_neon_vcgtd_u64:
2956 case NEON::BI__builtin_neon_vcltd_s64:
2957 case NEON::BI__builtin_neon_vcltd_u64:
2958 case NEON::BI__builtin_neon_vcged_u64:
2959 case NEON::BI__builtin_neon_vcged_s64:
2960 case NEON::BI__builtin_neon_vcled_u64:
2961 case NEON::BI__builtin_neon_vcled_s64:
2962 cgm.errorNYI(expr->getSourceRange(),
2963 std::string("unimplemented AArch64 builtin call: ") +
2964 getContext().BuiltinInfo.getName(builtinID));
2965 return mlir::Value{};
2966 case NEON::BI__builtin_neon_vnegd_s64: {
2967 return builder.createNeg(loc, ops[0]);
2968 }
2969 case NEON::BI__builtin_neon_vnegh_f16: {
2970 return builder.createFNeg(loc, ops[0]);
2971 }
2972 case NEON::BI__builtin_neon_vtstd_s64:
2973 case NEON::BI__builtin_neon_vtstd_u64:
2974 cgm.errorNYI(expr->getSourceRange(),
2975 std::string("unimplemented AArch64 builtin call: ") +
2976 getContext().BuiltinInfo.getName(builtinID));
2977 return mlir::Value{};
2978 case NEON::BI__builtin_neon_vset_lane_i8:
2979 case NEON::BI__builtin_neon_vset_lane_i16:
2980 case NEON::BI__builtin_neon_vset_lane_i32:
2981 case NEON::BI__builtin_neon_vset_lane_i64:
2982 case NEON::BI__builtin_neon_vset_lane_bf16:
2983 case NEON::BI__builtin_neon_vset_lane_f32:
2984 case NEON::BI__builtin_neon_vsetq_lane_i8:
2985 case NEON::BI__builtin_neon_vsetq_lane_i16:
2986 case NEON::BI__builtin_neon_vsetq_lane_i32:
2987 case NEON::BI__builtin_neon_vsetq_lane_i64:
2988 case NEON::BI__builtin_neon_vsetq_lane_bf16:
2989 case NEON::BI__builtin_neon_vsetq_lane_f32:
2990 case NEON::BI__builtin_neon_vset_lane_f64:
2991 case NEON::BI__builtin_neon_vset_lane_mf8:
2992 case NEON::BI__builtin_neon_vsetq_lane_mf8:
2993 case NEON::BI__builtin_neon_vsetq_lane_f64:
2994 return cir::VecInsertOp::create(builder, loc, ops[1], ops[0], ops[2]);
2995 case NEON::BI__builtin_neon_vget_lane_i8:
2996 case NEON::BI__builtin_neon_vdupb_lane_i8:
2997 case NEON::BI__builtin_neon_vgetq_lane_i8:
2998 case NEON::BI__builtin_neon_vdupb_laneq_i8:
2999 case NEON::BI__builtin_neon_vget_lane_mf8:
3000 case NEON::BI__builtin_neon_vdupb_lane_mf8:
3001 case NEON::BI__builtin_neon_vgetq_lane_mf8:
3002 case NEON::BI__builtin_neon_vdupb_laneq_mf8:
3003 case NEON::BI__builtin_neon_vget_lane_i16:
3004 case NEON::BI__builtin_neon_vduph_lane_i16:
3005 case NEON::BI__builtin_neon_vgetq_lane_i16:
3006 case NEON::BI__builtin_neon_vduph_laneq_i16:
3007 case NEON::BI__builtin_neon_vget_lane_i32:
3008 case NEON::BI__builtin_neon_vdups_lane_i32:
3009 case NEON::BI__builtin_neon_vdups_lane_f32:
3010 case NEON::BI__builtin_neon_vgetq_lane_i32:
3011 case NEON::BI__builtin_neon_vdups_laneq_i32:
3012 case NEON::BI__builtin_neon_vget_lane_i64:
3013 case NEON::BI__builtin_neon_vdupd_lane_i64:
3014 case NEON::BI__builtin_neon_vdupd_lane_f64:
3015 case NEON::BI__builtin_neon_vgetq_lane_i64:
3016 case NEON::BI__builtin_neon_vdupd_laneq_i64:
3017 case NEON::BI__builtin_neon_vget_lane_f32:
3018 case NEON::BI__builtin_neon_vget_lane_f64:
3019 case NEON::BI__builtin_neon_vgetq_lane_f32:
3020 case NEON::BI__builtin_neon_vdups_laneq_f32:
3021 case NEON::BI__builtin_neon_vgetq_lane_f64:
3022 case NEON::BI__builtin_neon_vdupd_laneq_f64:
3023 return cir::VecExtractOp::create(builder, loc, ops[0],
3024 emitScalarExpr(expr->getArg(1)));
3025 case NEON::BI__builtin_neon_vaddh_f16:
3026 return builder.createFAdd(loc, ops[0], ops[1]);
3027 case NEON::BI__builtin_neon_vsubh_f16:
3028 return builder.createFSub(loc, ops[0], ops[1]);
3029 case NEON::BI__builtin_neon_vmulh_f16:
3030 return builder.createFMul(loc, ops[0], ops[1]);
3031 case NEON::BI__builtin_neon_vdivh_f16:
3032 return builder.createFDiv(loc, ops[0], ops[1]);
3033 case NEON::BI__builtin_neon_vfmah_f16: {
3034 // NEON intrinsic puts accumulator first, unlike fma.
3035 std::rotate(ops.begin(), ops.begin() + 1, ops.end());
3036 mlir::Type ty = convertType(expr->getType());
3037 return emitNeonCallToOp<cir::FMAOp>(cgm, builder, {ty, ty, ty}, ops,
3038 std::nullopt, ty, loc);
3039 }
3040 case NEON::BI__builtin_neon_vfmsh_f16: {
3041 // NEON intrinsic puts accumulator first, unlike fma.
3042 std::rotate(ops.begin(), ops.begin() + 1, ops.end());
3043 ops[0] = builder.createFNeg(loc, ops[0]);
3044 mlir::Type ty = convertType(expr->getType());
3045 return emitNeonCallToOp<cir::FMAOp>(cgm, builder, {ty, ty, ty}, ops,
3046 std::nullopt, ty, loc);
3047 }
3048 case NEON::BI__builtin_neon_vaddd_s64:
3049 case NEON::BI__builtin_neon_vaddd_u64:
3050 return builder.createAdd(loc, ops[0], ops[1]);
3051 case NEON::BI__builtin_neon_vsubd_s64:
3052 case NEON::BI__builtin_neon_vsubd_u64:
3053 return builder.createSub(loc, ops[0], ops[1]);
3054 case NEON::BI__builtin_neon_vqdmlalh_s16:
3055 case NEON::BI__builtin_neon_vqdmlslh_s16:
3056 cgm.errorNYI(expr->getSourceRange(),
3057 std::string("unimplemented AArch64 builtin call: ") +
3058 getContext().BuiltinInfo.getName(builtinID));
3059 return mlir::Value{};
3060 case NEON::BI__builtin_neon_vqshlud_n_s64: {
3061 cir::IntType int64Type = builder.getSInt64Ty();
3062 ops[1] = builder.getSInt64(getZExtIntValueFromConstOp(ops[1]), loc);
3063 return emitNeonCall(cgm, builder, {int64Type, int64Type}, ops,
3064 "aarch64.neon.sqshlu", convertType(expr->getType()),
3065 loc);
3066 }
3067 case NEON::BI__builtin_neon_vqshld_n_u64:
3068 case NEON::BI__builtin_neon_vqshld_n_s64: {
3069 cir::IntType int64Type = builtinID == NEON::BI__builtin_neon_vqshld_n_u64
3070 ? builder.getUInt64Ty()
3071 : builder.getSInt64Ty();
3072 llvm::StringRef intrinsicName =
3073 builtinID == NEON::BI__builtin_neon_vqshld_n_u64 ? "aarch64.neon.uqshl"
3074 : "aarch64.neon.sqshl";
3075 ops[1] = builder.getSInt64(getZExtIntValueFromConstOp(ops[1]), loc);
3076 return emitNeonCall(cgm, builder, {int64Type, int64Type}, ops,
3077 intrinsicName, convertType(expr->getType()), loc);
3078 }
3079 case NEON::BI__builtin_neon_vrshrd_n_u64:
3080 case NEON::BI__builtin_neon_vrshrd_n_s64: {
3081 llvm::StringRef intrName = builtinID == NEON::BI__builtin_neon_vrshrd_n_s64
3082 ? "aarch64.neon.srshl"
3083 : "aarch64.neon.urshl";
3084 cir::IntType int64Ty = builtinID == NEON::BI__builtin_neon_vqshld_n_u64
3085 ? builder.getUInt64Ty()
3086 : builder.getSInt64Ty();
3087 int64_t sv = -cast<cir::IntAttr>(
3088 cast<cir::ConstantOp>(ops[1].getDefiningOp()).getValue())
3089 .getSInt();
3090 ops[1] = builder.getSInt64(sv, loc);
3091 return emitNeonCall(cgm, builder, {int64Ty, builder.getSInt64Ty()}, ops,
3092 intrName, int64Ty, loc);
3093 }
3094 case NEON::BI__builtin_neon_vrsrad_n_u64:
3095 case NEON::BI__builtin_neon_vrsrad_n_s64: {
3096 cir::IntType int64Type = builtinID == NEON::BI__builtin_neon_vrsrad_n_u64
3097 ? builder.getUInt64Ty()
3098 : builder.getSInt64Ty();
3099 ops[2] = builder.createNeg(loc, ops[2]);
3100 const StringRef intrName = builtinID == NEON::BI__builtin_neon_vrsrad_n_u64
3101 ? "aarch64.neon.urshl"
3102 : "aarch64.neon.srshl";
3103
3105 ops[1], builder.createIntCast(ops[2], builder.getSInt64Ty())};
3106 ops[1] = builder.emitIntrinsicCallOp(loc, intrName, int64Type, args);
3107 return builder.createAdd(loc, ops[0],
3108 builder.createBitcast(ops[1], int64Type));
3109 }
3110 case NEON::BI__builtin_neon_vshld_n_s64:
3111 case NEON::BI__builtin_neon_vshld_n_u64: {
3112 auto loc = getLoc(expr->getExprLoc());
3113 std::optional<llvm::APSInt> amt =
3114 expr->getArg(1)->getIntegerConstantExpr(getContext());
3115 assert(amt && "Expected argument to be a constant");
3116 return builder.createShiftLeft(loc, ops[0], amt->getZExtValue());
3117 }
3118 case NEON::BI__builtin_neon_vshrd_n_s64: {
3119 std::optional<llvm::APSInt> amt =
3120 expr->getArg(1)->getIntegerConstantExpr(getContext());
3121 assert(amt && "Expected argument to be a constant");
3122 return builder.createShiftRight(
3123 loc, ops[0], std::min(static_cast<uint64_t>(63), amt->getZExtValue()));
3124 }
3125 case NEON::BI__builtin_neon_vshrd_n_u64: {
3126 std::optional<llvm::APSInt> amt =
3127 expr->getArg(1)->getIntegerConstantExpr(getContext());
3128 assert(amt && "Expected argument to be a constant");
3129 uint64_t shiftAmt = amt->getZExtValue();
3130 // Right-shifting an unsigned value by its size yields 0.
3131 if (shiftAmt == 64)
3132 return builder.getConstInt(loc, builder.getUInt64Ty(), 0);
3133 return builder.createShiftRight(loc, ops[0], shiftAmt);
3134 }
3135 case NEON::BI__builtin_neon_vsrad_n_s64: {
3136 std::optional<llvm::APSInt> amt =
3137 expr->getArg(2)->getIntegerConstantExpr(getContext());
3138 assert(amt && "Expected argument to be a constant");
3139 uint64_t shiftAmt =
3140 std::min(static_cast<uint64_t>(63), amt->getZExtValue());
3141 mlir::Value shifted =
3142 builder.createShiftRight(loc, ops[1], static_cast<unsigned>(shiftAmt));
3143 return builder.createAdd(loc, ops[0], shifted);
3144 }
3145 case NEON::BI__builtin_neon_vsrad_n_u64: {
3146 std::optional<llvm::APSInt> amt =
3147 expr->getArg(2)->getIntegerConstantExpr(getContext());
3148 assert(amt && "Expected argument to be a constant");
3149 uint64_t shiftAmt = amt->getZExtValue();
3150 // Right-shifting an unsigned value by its size yields 0, so a + 0 = a.
3151 if (shiftAmt == 64)
3152 return ops[0];
3153 mlir::Value shifted =
3154 builder.createShiftRight(loc, ops[1], static_cast<unsigned>(shiftAmt));
3155 return builder.createAdd(loc, ops[0], shifted);
3156 }
3157 case NEON::BI__builtin_neon_vqdmlalh_lane_s16:
3158 case NEON::BI__builtin_neon_vqdmlalh_laneq_s16:
3159 case NEON::BI__builtin_neon_vqdmlslh_lane_s16:
3160 case NEON::BI__builtin_neon_vqdmlslh_laneq_s16:
3161 case NEON::BI__builtin_neon_vqdmlals_s32:
3162 case NEON::BI__builtin_neon_vqdmlsls_s32:
3163 case NEON::BI__builtin_neon_vqdmlals_lane_s32:
3164 case NEON::BI__builtin_neon_vqdmlals_laneq_s32:
3165 case NEON::BI__builtin_neon_vqdmlsls_lane_s32:
3166 case NEON::BI__builtin_neon_vqdmlsls_laneq_s32: {
3167 cgm.errorNYI(expr->getSourceRange(),
3168 std::string("unimplemented AArch64 builtin call: ") +
3169 getContext().BuiltinInfo.getName(builtinID));
3170 return mlir::Value{};
3171 }
3172 case NEON::BI__builtin_neon_vget_lane_bf16:
3173 case NEON::BI__builtin_neon_vduph_lane_bf16:
3174 case NEON::BI__builtin_neon_vduph_lane_f16:
3175 case NEON::BI__builtin_neon_vgetq_lane_bf16:
3176 case NEON::BI__builtin_neon_vduph_laneq_bf16:
3177 case NEON::BI__builtin_neon_vduph_laneq_f16: {
3178 return cir::VecExtractOp::create(builder, loc, ops[0], ops[1]);
3179 }
3180 case NEON::BI__builtin_neon_vcvt_bf16_f32:
3181 case NEON::BI__builtin_neon_vcvtq_low_bf16_f32:
3182 case NEON::BI__builtin_neon_vcvtq_high_bf16_f32:
3183 cgm.errorNYI(expr->getSourceRange(),
3184 std::string("unimplemented AArch64 builtin call: ") +
3185 getContext().BuiltinInfo.getName(builtinID));
3186 return mlir::Value{};
3187 case NEON::BI__builtin_neon_vcvt_f16_f32: {
3188 auto v4f32 = cir::VectorType::getFixed(cgm.floatTy, 4);
3189 auto v4f16 = cir::VectorType::getFixed(cgm.fP16Ty, 4);
3190 return builder.createFloatingCast(builder.createBitcast(ops[0], v4f32),
3191 v4f16);
3192 }
3193 case NEON::BI__builtin_neon_vcvt_f32_f16: {
3194 auto v4f32 = cir::VectorType::getFixed(cgm.floatTy, 4);
3195 auto v4f16 = cir::VectorType::getFixed(cgm.fP16Ty, 4);
3196 return builder.createFloatingCast(builder.createBitcast(ops[0], v4f16),
3197 v4f32);
3198 }
3199 case clang::AArch64::BI_InterlockedAdd:
3200 case clang::AArch64::BI_InterlockedAdd_acq:
3201 case clang::AArch64::BI_InterlockedAdd_rel:
3202 case clang::AArch64::BI_InterlockedAdd_nf:
3203 case clang::AArch64::BI_InterlockedAdd64:
3204 case clang::AArch64::BI_InterlockedAdd64_acq:
3205 case clang::AArch64::BI_InterlockedAdd64_rel:
3206 case clang::AArch64::BI_InterlockedAdd64_nf:
3207 cgm.errorNYI(expr->getSourceRange(),
3208 std::string("unimplemented AArch64 builtin call: ") +
3209 getContext().BuiltinInfo.getName(builtinID));
3210 return mlir::Value{};
3211 }
3212
3213 cir::VectorType ty = getNeonType(this, type);
3214 if (!ty)
3215 return nullptr;
3216
3217 llvm::StringRef intrName;
3218
3219 switch (builtinID) {
3220 default:
3221 return std::nullopt;
3222 case NEON::BI__builtin_neon_vbsl_v:
3223 case NEON::BI__builtin_neon_vbslq_v: {
3224
3225 cir::VectorType bitTy = getIntVecFromVecTy(builder, ty);
3226 ops[0] = builder.createBitcast(ops[0], bitTy);
3227 ops[1] = builder.createBitcast(ops[1], bitTy);
3228 ops[2] = builder.createBitcast(ops[2], bitTy);
3229
3230 ops[1] = builder.createAnd(loc, ops[0], ops[1]);
3231 ops[2] = builder.createAnd(loc, builder.createNot(ops[0]), ops[2]);
3232 ops[0] = builder.createOr(loc, ops[1], ops[2]);
3233 return builder.createBitcast(ops[0], ty);
3234 }
3235 case NEON::BI__builtin_neon_vfma_lane_v:
3236 case NEON::BI__builtin_neon_vfmaq_lane_v: {
3237 mlir::Value addend = builder.createBitcast(ops[0], ty);
3238 mlir::Value multiplicand = builder.createBitcast(ops[1], ty);
3239 // For vfmaq_lane, the lane source operand is the non-quad vector, so it has
3240 // half as many lanes as the quad result vector. For vfma_lane, it has the
3241 // same shape as the result vector.
3242 cir::VectorType sourceTy = cir::VectorType::getFixed(
3243 ty.getElementType(), builtinID == NEON::BI__builtin_neon_vfmaq_lane_v
3244 ? ty.getFixedSize() / 2
3245 : ty.getFixedSize());
3246 mlir::Value laneSource = builder.createBitcast(ops[2], sourceTy);
3247 laneSource =
3248 emitNeonSplat(builder, loc, laneSource, ops[3], ty.getFixedSize());
3249
3250 llvm::SmallVector<mlir::Value> fmaOps = {multiplicand, laneSource, addend};
3251 return emitNeonCallToOp<cir::FMAOp>(cgm, builder, {ty, ty, ty}, fmaOps,
3252 std::nullopt, ty, loc);
3253 }
3254 case NEON::BI__builtin_neon_vfma_laneq_v: {
3255 // v1f64 fma should be mapped to Neon scalar f64 fma.
3256 if (ty.getElementType() == cgm.doubleTy) {
3257 mlir::Value addend = builder.createBitcast(ops[0], cgm.doubleTy);
3258 mlir::Value multiplicand = builder.createBitcast(ops[1], cgm.doubleTy);
3259 // The laneq source operand is float64x2_t, so the source vector has two
3260 // double lanes.
3261 cir::VectorType sourceTy = cir::VectorType::getFixed(cgm.doubleTy, 2);
3262 mlir::Value laneSource = builder.createBitcast(ops[2], sourceTy);
3263 laneSource = builder.createExtractElement(
3264 loc, laneSource,
3265 static_cast<uint64_t>(getIntValueFromConstOp(ops[3])));
3266
3267 llvm::SmallVector<mlir::Value> fmaOps = {multiplicand, laneSource,
3268 addend};
3269 return builder.createBitcast(
3271 cgm, builder, {cgm.doubleTy, cgm.doubleTy, cgm.doubleTy}, fmaOps,
3272 std::nullopt, cgm.doubleTy, loc),
3273 ty);
3274 }
3275
3276 mlir::Value addend = builder.createBitcast(ops[0], ty);
3277 mlir::Value multiplicand = builder.createBitcast(ops[1], ty);
3278 // The laneq source operand is the quad vector, so it has twice as many
3279 // lanes as the non-quad result vector.
3280 cir::VectorType sourceTy =
3281 cir::VectorType::getFixed(ty.getElementType(), ty.getFixedSize() * 2);
3282 mlir::Value laneSource = builder.createBitcast(ops[2], sourceTy);
3283 laneSource =
3284 emitNeonSplat(builder, loc, laneSource, ops[3], ty.getFixedSize());
3285
3286 llvm::SmallVector<mlir::Value> fmaOps = {laneSource, multiplicand, addend};
3287 return emitNeonCallToOp<cir::FMAOp>(cgm, builder, {ty, ty, ty}, fmaOps,
3288 std::nullopt, ty, loc);
3289 }
3290 case NEON::BI__builtin_neon_vfmaq_laneq_v: {
3291 mlir::Value addend = builder.createBitcast(ops[0], ty);
3292 mlir::Value multiplicand = builder.createBitcast(ops[1], ty);
3293 mlir::Value laneSource = builder.createBitcast(ops[2], ty);
3294 laneSource =
3295 emitNeonSplat(builder, loc, laneSource, ops[3], ty.getFixedSize());
3296
3297 llvm::SmallVector<mlir::Value> fmaOps = {laneSource, multiplicand, addend};
3298 return emitNeonCallToOp<cir::FMAOp>(cgm, builder, {ty, ty, ty}, fmaOps,
3299 std::nullopt, ty, loc);
3300 }
3301 case NEON::BI__builtin_neon_vfmah_lane_f16:
3302 case NEON::BI__builtin_neon_vfmas_lane_f32:
3303 case NEON::BI__builtin_neon_vfmah_laneq_f16:
3304 case NEON::BI__builtin_neon_vfmas_laneq_f32: {
3305 // Scalar lane/laneq forms use one selected element from the lane source.
3306 mlir::Value laneSource = builder.createExtractElement(
3307 loc, ops[2], static_cast<uint64_t>(getIntValueFromConstOp(ops[3])));
3308
3309 llvm::SmallVector<mlir::Value> fmaOps = {ops[1], laneSource, ops[0]};
3310 mlir::Type ty = convertType(expr->getType());
3311 return emitNeonCallToOp<cir::FMAOp>(cgm, builder, {ty, ty, ty}, fmaOps,
3312 std::nullopt, ty, loc);
3313 }
3314 case NEON::BI__builtin_neon_vfmad_lane_f64:
3315 case NEON::BI__builtin_neon_vfmad_laneq_f64: {
3316 // The lane source operand is float64x1_t for lane forms and float64x2_t
3317 // for laneq forms.
3318 mlir::Value laneSource = builder.createExtractElement(
3319 loc, ops[2], static_cast<uint64_t>(getIntValueFromConstOp(ops[3])));
3320
3321 llvm::SmallVector<mlir::Value> fmaOps = {ops[1], laneSource, ops[0]};
3323 cgm, builder, {cgm.doubleTy, cgm.doubleTy, cgm.doubleTy}, fmaOps,
3324 std::nullopt, cgm.doubleTy, loc);
3325 }
3326 case NEON::BI__builtin_neon_vmull_v: {
3327 intrName = usgn ? "aarch64.neon.umull" : "aarch64.neon.smull";
3328 if (type.isPoly())
3329 intrName = "aarch64.neon.pmull";
3330 cir::VectorType argTy = builder.getExtendedOrTruncatedElementVectorType(
3331 ty, /*isExtended*/ false, !usgn);
3332 return emitNeonCall(cgm, builder, {argTy, argTy}, ops, intrName, ty, loc);
3333 }
3334 case NEON::BI__builtin_neon_vmax_v:
3335 case NEON::BI__builtin_neon_vmaxq_v:
3336 intrName = usgn ? "umax" : "smax";
3337 if (cir::isFPOrVectorOfFPType(ty))
3338 intrName = "aarch64.neon.fmax";
3339 return emitNeonCall(cgm, builder, {ty, ty}, ops, intrName, ty, loc);
3340 case NEON::BI__builtin_neon_vmaxh_f16: {
3341 auto halfTy = builder.getFp16Ty();
3342 return builder.emitIntrinsicCallOp(loc, "aarch64.neon.fmax", halfTy, ops);
3343 }
3344 case NEON::BI__builtin_neon_vmin_v:
3345 case NEON::BI__builtin_neon_vminq_v:
3346 intrName = usgn ? "umin" : "smin";
3347 if (cir::isFPOrVectorOfFPType(ty))
3348 intrName = "aarch64.neon.fmin";
3349 return emitNeonCall(cgm, builder, {ty, ty}, ops, intrName, ty, loc);
3350 case NEON::BI__builtin_neon_vminh_f16: {
3351 auto halfTy = builder.getFp16Ty();
3352 return builder.emitIntrinsicCallOp(loc, "aarch64.neon.fmin", halfTy, ops);
3353 }
3354 case NEON::BI__builtin_neon_vabd_v:
3355 case NEON::BI__builtin_neon_vabdq_v:
3356 intrName = usgn ? "aarch64.neon.uabd" : "aarch64.neon.sabd";
3357 if (cir::isFPOrVectorOfFPType(ty))
3358 intrName = "aarch64.neon.fabd";
3359 return emitNeonCall(cgm, builder, {ty, ty}, ops, intrName, ty, loc);
3360 case NEON::BI__builtin_neon_vpadal_v:
3361 case NEON::BI__builtin_neon_vpadalq_v: {
3362 intrName = usgn ? "aarch64.neon.uaddlp" : "aarch64.neon.saddlp";
3363 llvm::SmallVector<mlir::Value> inputs{ops[1]};
3364 mlir::Value pairwiseSum =
3365 emitNeonCall(cgm, builder, {getNeonPairwiseWidenInputType(ty, usgn)},
3366 inputs, intrName, ty, loc);
3367 mlir::Value accumValue = builder.createBitcast(loc, ops[0], ty);
3368 return cir::AddOp::create(builder, loc, ty, pairwiseSum, accumValue);
3369 }
3370 case NEON::BI__builtin_neon_vpmin_v:
3371 case NEON::BI__builtin_neon_vpminq_v:
3372 intrName = usgn ? "aarch64.neon.uminp" : "aarch64.neon.sminp";
3373 if (cir::isFPOrVectorOfFPType(ty))
3374 intrName = "aarch64.neon.fminp";
3375 return emitNeonCall(cgm, builder, {ty, ty}, ops, intrName, ty, loc);
3376 case NEON::BI__builtin_neon_vpmax_v:
3377 case NEON::BI__builtin_neon_vpmaxq_v:
3378 intrName = usgn ? "aarch64.neon.umaxp" : "aarch64.neon.smaxp";
3379 if (cir::isFPOrVectorOfFPType(ty))
3380 intrName = "aarch64.neon.fmaxp";
3381 return emitNeonCall(cgm, builder, {ty, ty}, ops, intrName, ty, loc);
3382 case NEON::BI__builtin_neon_vminnm_v:
3383 case NEON::BI__builtin_neon_vminnmq_v:
3384 intrName = "aarch64.neon.fminnm";
3385 return emitNeonCall(cgm, builder, {ty, ty}, ops, intrName, ty, loc);
3386 case NEON::BI__builtin_neon_vminnmh_f16: {
3387 auto halfTy = builder.getFp16Ty();
3388 return builder.emitIntrinsicCallOp(loc, "aarch64.neon.fminnm", halfTy, ops);
3389 }
3390 case NEON::BI__builtin_neon_vmaxnm_v:
3391 case NEON::BI__builtin_neon_vmaxnmq_v:
3392 intrName = "aarch64.neon.fmaxnm";
3393 return emitNeonCall(cgm, builder, {ty, ty}, ops, intrName, ty, loc);
3394 case NEON::BI__builtin_neon_vmaxnmh_f16: {
3395 auto halfTy = builder.getFp16Ty();
3396 return builder.emitIntrinsicCallOp(loc, "aarch64.neon.fmaxnm", halfTy, ops);
3397 }
3398 case NEON::BI__builtin_neon_vrecpss_f32:
3399 case NEON::BI__builtin_neon_vrecpsd_f64:
3400 cgm.errorNYI(expr->getSourceRange(),
3401 std::string("unimplemented AArch64 builtin call: ") +
3402 getContext().BuiltinInfo.getName(builtinID));
3403 return mlir::Value{};
3404 case NEON::BI__builtin_neon_vrecpsh_f16: {
3405 auto halfTy = builder.getFp16Ty();
3406 return builder.emitIntrinsicCallOp(loc, "aarch64.neon.frecps", halfTy, ops);
3407 }
3408 case NEON::BI__builtin_neon_vqshrun_n_v: {
3409 cir::VectorType argTy = builder.getExtendedOrTruncatedElementVectorType(
3410 ty, /*isExtended=*/true, /*isSigned=*/true);
3411 return emitNeonCall(cgm, builder, {argTy, sInt32Ty}, ops,
3412 "aarch64.neon.sqshrun", ty, loc);
3413 }
3414 case NEON::BI__builtin_neon_vqrshrun_n_v: {
3415 cir::VectorType argTy = builder.getExtendedOrTruncatedElementVectorType(
3416 ty, /*isExtended=*/true, /*isSigned=*/true);
3417 return emitNeonCall(cgm, builder, {argTy, sInt32Ty}, ops,
3418 "aarch64.neon.sqrshrun", ty, loc);
3419 }
3420 case NEON::BI__builtin_neon_vqshrn_n_v: {
3421 cir::VectorType argTy = builder.getExtendedOrTruncatedElementVectorType(
3422 ty, /*isExtended=*/true, /*isSigned=*/!usgn);
3423 llvm::StringRef intrName =
3424 usgn ? "aarch64.neon.uqshrn" : "aarch64.neon.sqshrn";
3425 return emitNeonCall(cgm, builder, {argTy, sInt32Ty}, ops, intrName, ty,
3426 loc);
3427 }
3428 case NEON::BI__builtin_neon_vrshrn_n_v: {
3429 cir::VectorType argTy = builder.getExtendedOrTruncatedElementVectorType(
3430 ty, /*isExtended=*/true, /*isSigned=*/!usgn);
3431 return emitNeonCall(cgm, builder, {argTy, sInt32Ty}, ops,
3432 "aarch64.neon.rshrn", ty, loc);
3433 }
3434 case NEON::BI__builtin_neon_vqrshrn_n_v: {
3435 cir::VectorType argTy = builder.getExtendedOrTruncatedElementVectorType(
3436 ty, /*isExtended=*/true, /*isSigned=*/!usgn);
3437 llvm::StringRef intrName =
3438 usgn ? "aarch64.neon.uqrshrn" : "aarch64.neon.sqrshrn";
3439 return emitNeonCall(cgm, builder, {argTy, sInt32Ty}, ops, intrName, ty,
3440 loc);
3441 }
3442 case NEON::BI__builtin_neon_vrndah_f16: {
3444 mlir::Type halfTy = builder.getFp16Ty();
3445 return emitNeonCallToOp<cir::RoundOp>(cgm, builder, {halfTy}, ops,
3446 std::nullopt, halfTy, loc);
3447 }
3448 case NEON::BI__builtin_neon_vrnda_v:
3449 case NEON::BI__builtin_neon_vrndaq_v:
3451 return emitNeonCallToOp<cir::RoundOp>(cgm, builder, {ty}, ops, std::nullopt,
3452 ty, loc);
3453 case NEON::BI__builtin_neon_vrndih_f16: {
3455 mlir::Type halfTy = builder.getFp16Ty();
3456 return emitNeonCallToOp<cir::NearbyintOp>(cgm, builder, {halfTy}, ops,
3457 std::nullopt, halfTy, loc);
3458 }
3459 case NEON::BI__builtin_neon_vrndmh_f16: {
3461 mlir::Type halfTy = builder.getFp16Ty();
3462 return emitNeonCallToOp<cir::FloorOp>(cgm, builder, {halfTy}, ops,
3463 std::nullopt, halfTy, loc);
3464 }
3465 case NEON::BI__builtin_neon_vrndm_v:
3466 case NEON::BI__builtin_neon_vrndmq_v:
3468 return emitNeonCallToOp<cir::FloorOp>(cgm, builder, {ty}, ops, std::nullopt,
3469 ty, loc);
3470 case NEON::BI__builtin_neon_vrndnh_f16: {
3472 mlir::Type halfTy = builder.getFp16Ty();
3473 return emitNeonCallToOp<cir::RoundEvenOp>(cgm, builder, {halfTy}, ops,
3474 std::nullopt, halfTy, loc);
3475 }
3476 case NEON::BI__builtin_neon_vrndn_v:
3477 case NEON::BI__builtin_neon_vrndnq_v:
3479 return emitNeonCallToOp<cir::RoundEvenOp>(cgm, builder, {ty}, ops,
3480 std::nullopt, ty, loc);
3481 case NEON::BI__builtin_neon_vrndns_f32: {
3483 mlir::Type floatTy = builder.getSingleTy();
3484 return emitNeonCallToOp<cir::RoundEvenOp>(cgm, builder, {floatTy}, ops,
3485 std::nullopt, floatTy, loc);
3486 }
3487 case NEON::BI__builtin_neon_vrndph_f16: {
3489 mlir::Type halfTy = builder.getFp16Ty();
3490 return emitNeonCallToOp<cir::CeilOp>(cgm, builder, {halfTy}, ops,
3491 std::nullopt, halfTy, loc);
3492 }
3493 case NEON::BI__builtin_neon_vrndp_v:
3494 case NEON::BI__builtin_neon_vrndpq_v:
3496 return emitNeonCallToOp<cir::CeilOp>(cgm, builder, {ty}, ops, std::nullopt,
3497 ty, loc);
3498 case NEON::BI__builtin_neon_vrndxh_f16: {
3500 mlir::Type halfTy = builder.getFp16Ty();
3501 return emitNeonCallToOp<cir::RintOp>(cgm, builder, {halfTy}, ops,
3502 std::nullopt, halfTy, loc);
3503 }
3504 case NEON::BI__builtin_neon_vrndx_v:
3505 case NEON::BI__builtin_neon_vrndxq_v:
3507 return emitNeonCallToOp<cir::RintOp>(cgm, builder, {ty}, ops, std::nullopt,
3508 ty, loc);
3509 case NEON::BI__builtin_neon_vrndh_f16: {
3511 mlir::Type halfTy = builder.getFp16Ty();
3512 return emitNeonCallToOp<cir::TruncOp>(cgm, builder, {halfTy}, ops,
3513 std::nullopt, halfTy, loc);
3514 }
3515 case NEON::BI__builtin_neon_vrnd_v:
3516 case NEON::BI__builtin_neon_vrndq_v:
3518 return emitNeonCallToOp<cir::TruncOp>(cgm, builder, {ty}, ops, std::nullopt,
3519 ty, loc);
3520 case NEON::BI__builtin_neon_vcvt_f64_v:
3521 case NEON::BI__builtin_neon_vcvtq_f64_v:
3522 ops[0] = builder.createBitcast(ops[0], ty);
3523 ty = getNeonType(
3524 this, NeonTypeFlags(NeonTypeFlags::Float64, false, type.isQuad()));
3525 return builder.createCast(loc, cir::CastKind::int_to_float, ops[0], ty);
3526 case NEON::BI__builtin_neon_vcvt_f64_f32: {
3527 auto v2f32 = cir::VectorType::getFixed(cgm.floatTy, 2);
3528 auto v2f64 = cir::VectorType::getFixed(cgm.doubleTy, 2);
3529 return builder.createFloatingCast(builder.createBitcast(ops[0], v2f32),
3530 v2f64);
3531 }
3532 case NEON::BI__builtin_neon_vcvt_f32_f64: {
3533 auto v2f32 = cir::VectorType::getFixed(cgm.floatTy, 2);
3534 auto v2f64 = cir::VectorType::getFixed(cgm.doubleTy, 2);
3535 return builder.createFloatingCast(builder.createBitcast(ops[0], v2f64),
3536 v2f32);
3537 }
3538 case NEON::BI__builtin_neon_vcvt_s32_v:
3539 case NEON::BI__builtin_neon_vcvt_u32_v:
3540 case NEON::BI__builtin_neon_vcvt_s64_v:
3541 case NEON::BI__builtin_neon_vcvt_u64_v:
3542 case NEON::BI__builtin_neon_vcvt_s16_f16:
3543 case NEON::BI__builtin_neon_vcvt_u16_f16:
3544 case NEON::BI__builtin_neon_vcvtq_s32_v:
3545 case NEON::BI__builtin_neon_vcvtq_u32_v:
3546 case NEON::BI__builtin_neon_vcvtq_s64_v:
3547 case NEON::BI__builtin_neon_vcvtq_u64_v:
3548 case NEON::BI__builtin_neon_vcvtq_s16_f16:
3549 case NEON::BI__builtin_neon_vcvtq_u16_f16:
3550
3551 case NEON::BI__builtin_neon_vcvta_s16_f16:
3552 case NEON::BI__builtin_neon_vcvta_u16_f16:
3553 case NEON::BI__builtin_neon_vcvtaq_s16_f16:
3554 case NEON::BI__builtin_neon_vcvtaq_u16_f16:
3555 cgm.errorNYI(expr->getSourceRange(),
3556 std::string("unimplemented AArch64 builtin call: ") +
3557 getContext().BuiltinInfo.getName(builtinID));
3558 return mlir::Value{};
3559 case NEON::BI__builtin_neon_vcvta_s32_v:
3560 case NEON::BI__builtin_neon_vcvtaq_s32_v:
3561 case NEON::BI__builtin_neon_vcvta_u32_v:
3562 case NEON::BI__builtin_neon_vcvtaq_u32_v:
3563 case NEON::BI__builtin_neon_vcvta_s64_v:
3564 case NEON::BI__builtin_neon_vcvtaq_s64_v:
3565 case NEON::BI__builtin_neon_vcvta_u64_v:
3566 case NEON::BI__builtin_neon_vcvtaq_u64_v: {
3567 auto argTy = getFloatNeonType(*this, type);
3568 llvm::StringRef intrName =
3569 usgn ? "aarch64.neon.fcvtau" : "aarch64.neon.fcvtas";
3570 return emitNeonCall(cgm, builder, {argTy}, ops, intrName, ty, loc);
3571 }
3572
3573 case NEON::BI__builtin_neon_vcvtm_s16_f16:
3574 case NEON::BI__builtin_neon_vcvtmq_s16_f16:
3575 case NEON::BI__builtin_neon_vcvtm_u16_f16:
3576 case NEON::BI__builtin_neon_vcvtmq_u16_f16:
3577 cgm.errorNYI(expr->getSourceRange(),
3578 std::string("unimplemented AArch64 builtin call: ") +
3579 getContext().BuiltinInfo.getName(builtinID));
3580 return mlir::Value{};
3581 case NEON::BI__builtin_neon_vcvtm_s32_v:
3582 case NEON::BI__builtin_neon_vcvtmq_s32_v:
3583 case NEON::BI__builtin_neon_vcvtm_u32_v:
3584 case NEON::BI__builtin_neon_vcvtmq_u32_v:
3585 case NEON::BI__builtin_neon_vcvtm_s64_v:
3586 case NEON::BI__builtin_neon_vcvtmq_s64_v:
3587 case NEON::BI__builtin_neon_vcvtm_u64_v:
3588 case NEON::BI__builtin_neon_vcvtmq_u64_v: {
3589 auto argTy = getFloatNeonType(*this, type);
3590 llvm::StringRef intrName =
3591 usgn ? "aarch64.neon.fcvtmu" : "aarch64.neon.fcvtms";
3592 return emitNeonCall(cgm, builder, {argTy}, ops, intrName, ty, loc);
3593 }
3594
3595 case NEON::BI__builtin_neon_vcvtn_s16_f16:
3596 case NEON::BI__builtin_neon_vcvtnq_s16_f16:
3597 case NEON::BI__builtin_neon_vcvtn_u16_f16:
3598 case NEON::BI__builtin_neon_vcvtnq_u16_f16:
3599 cgm.errorNYI(expr->getSourceRange(),
3600 std::string("unimplemented AArch64 builtin call: ") +
3601 getContext().BuiltinInfo.getName(builtinID));
3602 return mlir::Value{};
3603
3604 case NEON::BI__builtin_neon_vcvtn_s32_v:
3605 case NEON::BI__builtin_neon_vcvtnq_s32_v:
3606 case NEON::BI__builtin_neon_vcvtn_u32_v:
3607 case NEON::BI__builtin_neon_vcvtnq_u32_v:
3608 case NEON::BI__builtin_neon_vcvtn_s64_v:
3609 case NEON::BI__builtin_neon_vcvtnq_s64_v:
3610 case NEON::BI__builtin_neon_vcvtn_u64_v:
3611 case NEON::BI__builtin_neon_vcvtnq_u64_v: {
3612 auto argTy = getFloatNeonType(*this, type);
3613 llvm::StringRef intrName =
3614 usgn ? "aarch64.neon.fcvtnu" : "aarch64.neon.fcvtns";
3615 return emitNeonCall(cgm, builder, {argTy}, ops, intrName, ty, loc);
3616 }
3617
3618 case NEON::BI__builtin_neon_vcvtp_s16_f16:
3619 case NEON::BI__builtin_neon_vcvtpq_s16_f16:
3620 case NEON::BI__builtin_neon_vcvtp_u16_f16:
3621 case NEON::BI__builtin_neon_vcvtpq_u16_f16:
3622 cgm.errorNYI(expr->getSourceRange(),
3623 std::string("unimplemented AArch64 builtin call: ") +
3624 getContext().BuiltinInfo.getName(builtinID));
3625 return mlir::Value{};
3626 case NEON::BI__builtin_neon_vcvtp_s32_v:
3627 case NEON::BI__builtin_neon_vcvtpq_s32_v:
3628 case NEON::BI__builtin_neon_vcvtp_u32_v:
3629 case NEON::BI__builtin_neon_vcvtpq_u32_v:
3630 case NEON::BI__builtin_neon_vcvtp_s64_v:
3631 case NEON::BI__builtin_neon_vcvtpq_s64_v:
3632 case NEON::BI__builtin_neon_vcvtp_u64_v:
3633 case NEON::BI__builtin_neon_vcvtpq_u64_v: {
3634 auto argTy = getFloatNeonType(*this, type);
3635 llvm::StringRef intrName =
3636 usgn ? "aarch64.neon.fcvtpu" : "aarch64.neon.fcvtps";
3637 return emitNeonCall(cgm, builder, {argTy}, ops, intrName, ty, loc);
3638 }
3639
3640 case NEON::BI__builtin_neon_vmulx_v:
3641 case NEON::BI__builtin_neon_vmulxq_v:
3642 case NEON::BI__builtin_neon_vmulxh_lane_f16:
3643 case NEON::BI__builtin_neon_vmulxh_laneq_f16:
3644 case NEON::BI__builtin_neon_vmul_lane_v:
3645 case NEON::BI__builtin_neon_vmul_laneq_v:
3646 case NEON::BI__builtin_neon_vpmaxnm_v:
3647 case NEON::BI__builtin_neon_vpmaxnmq_v:
3648 cgm.errorNYI(expr->getSourceRange(),
3649 std::string("unimplemented AArch64 builtin call: ") +
3650 getContext().BuiltinInfo.getName(builtinID));
3651 return mlir::Value{};
3652 case NEON::BI__builtin_neon_vpminnm_v:
3653 case NEON::BI__builtin_neon_vpminnmq_v:
3654 intrName = "aarch64.neon.fminnmp";
3655 return emitNeonCall(cgm, builder, {ty, ty}, ops, intrName, ty, loc);
3656 case NEON::BI__builtin_neon_vsqrth_f16: {
3657 mlir::Type halfTy = builder.getFp16Ty();
3658 return emitNeonCallToOp<cir::SqrtOp>(cgm, builder, {halfTy}, ops,
3659 std::nullopt, halfTy, loc);
3660 }
3661 case NEON::BI__builtin_neon_vsqrt_v:
3662 case NEON::BI__builtin_neon_vsqrtq_v:
3663 return emitNeonCallToOp<cir::SqrtOp>(cgm, builder, {ty}, ops, std::nullopt,
3664 ty, loc);
3665 case NEON::BI__builtin_neon_vrbit_v:
3666 case NEON::BI__builtin_neon_vrbitq_v:
3667 case NEON::BI__builtin_neon_vmaxv_f16:
3668 case NEON::BI__builtin_neon_vmaxvq_f16:
3669 case NEON::BI__builtin_neon_vminv_f16:
3670 case NEON::BI__builtin_neon_vminvq_f16:
3671 case NEON::BI__builtin_neon_vmaxnmv_f16:
3672 case NEON::BI__builtin_neon_vmaxnmvq_f16:
3673 case NEON::BI__builtin_neon_vminnmv_f16:
3674 case NEON::BI__builtin_neon_vminnmvq_f16:
3675 case NEON::BI__builtin_neon_vmul_n_f64:
3676 cgm.errorNYI(expr->getSourceRange(),
3677 std::string("unimplemented AArch64 builtin call: ") +
3678 getContext().BuiltinInfo.getName(builtinID));
3679 return mlir::Value{};
3680 case NEON::BI__builtin_neon_vaddlv_u8:
3681 case NEON::BI__builtin_neon_vaddlvq_u8:
3682 case NEON::BI__builtin_neon_vaddlv_u16:
3683 case NEON::BI__builtin_neon_vaddlvq_u16:
3684 case NEON::BI__builtin_neon_vaddlv_s8:
3685 case NEON::BI__builtin_neon_vaddlvq_s8:
3686 case NEON::BI__builtin_neon_vaddlv_s16:
3687 case NEON::BI__builtin_neon_vaddlvq_s16: {
3688 mlir::Type argTy = convertType(expr->getArg(0)->getType());
3689 mlir::Type userRetTy = convertType(expr->getType());
3690 auto eltTy = mlir::cast<cir::IntType>(
3691 mlir::cast<cir::VectorType>(argTy).getElementType());
3692 bool isUnsigned = !eltTy.isSigned();
3693 // These builtins only use 8 and 16-bit element vectors; the intrinsic
3694 // always produces i32. The C result is i32 for 16-bit elements, but i16
3695 // for 8-bit elements, so we emit at i32 and narrow only in that case.
3696 bool needsTrunc = eltTy.getWidth() == 8;
3697 intrName = isUnsigned ? "aarch64.neon.uaddlv" : "aarch64.neon.saddlv";
3698 mlir::Type intrRetTy = userRetTy;
3699 if (needsTrunc)
3700 intrRetTy = isUnsigned ? builder.getUInt32Ty() : builder.getSInt32Ty();
3701 mlir::Value result =
3702 emitNeonCall(cgm, builder, {argTy}, ops, intrName, intrRetTy, loc);
3703 if (needsTrunc)
3704 result = builder.createIntCast(result, userRetTy);
3705 return result;
3706 }
3707 case NEON::BI__builtin_neon_vsri_n_v:
3708 case NEON::BI__builtin_neon_vsriq_n_v: {
3710 ops[0], ops[1], builder.createIntCast(ops[2], builder.getUInt32Ty())};
3711 return emitNeonCall(cgm, builder, {ty, ty, builder.getUInt32Ty()}, vsriArgs,
3712 "aarch64.neon.vsri", ty, loc);
3713 }
3714 case NEON::BI__builtin_neon_vsli_n_v:
3715 case NEON::BI__builtin_neon_vsliq_n_v: {
3716
3717 intrName = "aarch64.neon.vsli";
3718
3719 llvm::SmallVector<mlir::Type> argTypes = {ty, ty, ops[2].getType()};
3720
3721 return emitNeonCall(cgm, builder, argTypes, ops, intrName, ty, loc,
3722 /*isConstrainedFPIntrinsic=*/false,
3723 /*shift=*/0, /*rightshift=*/false);
3724 }
3725 case NEON::BI__builtin_neon_vsra_n_v:
3726 case NEON::BI__builtin_neon_vsraq_n_v: {
3727 ops[0] = builder.createBitcast(ops[0], ty);
3728 ops[1] = emitNeonRShiftImm(*this, ops[1], ops[2], ty, usgn, loc);
3729 return builder.createAdd(loc, ops[0], ops[1]);
3730 }
3731 case NEON::BI__builtin_neon_vrsra_n_v:
3732 case NEON::BI__builtin_neon_vrsraq_n_v: {
3733 intrName = usgn ? "aarch64.neon.urshl" : "aarch64.neon.srshl";
3734 // The llvm intrinsic is expecting negative shift amount for right shift.
3735 // Thus we have to make shift amount vec type to be signed.
3736 cir::VectorType shiftAmtVecTy =
3737 usgn ? getSignChangedVectorType(builder, ty) : ty;
3738 llvm::SmallVector<mlir::Value, 2> tmpOps = {ops[1], ops[2]};
3739 mlir::Value tmp = emitNeonCall(cgm, builder, {ty, shiftAmtVecTy}, tmpOps,
3740 intrName, ty, loc,
3741 /*isConstrainedFPIntrinsic=*/false,
3742 /*shift=*/1, /*rightshift=*/true);
3743 ops[0] = builder.createBitcast(ops[0], ty);
3744 return builder.createAdd(loc, ops[0], tmp);
3745 }
3746 case NEON::BI__builtin_neon_vld1_v:
3747 case NEON::BI__builtin_neon_vld1q_v:
3748 return builder.createLoad(loc, ptrOp0.withElementType(builder, ty));
3749 case NEON::BI__builtin_neon_vst1_v:
3750 case NEON::BI__builtin_neon_vst1q_v: {
3751 ops[1] = builder.createBitcast(ops[1], ty);
3752 builder.createStore(loc, ops[1], ptrOp0.withElementType(builder, ty));
3753 return nullptr;
3754 }
3755 case NEON::BI__builtin_neon_vld1_lane_v:
3756 case NEON::BI__builtin_neon_vld1q_lane_v: {
3757 ops[1] = builder.createBitcast(ops[1], ty);
3758 mlir::Value scalar = builder.createLoad(
3759 loc, ptrOp0.withElementType(builder, ty.getElementType()));
3760 return cir::VecInsertOp::create(builder, loc, ops[1], scalar, ops[2]);
3761 }
3762 case NEON::BI__builtin_neon_vld1_dup_v:
3763 case NEON::BI__builtin_neon_vld1q_dup_v: {
3764 mlir::Value scalar = builder.createLoad(
3765 loc, ptrOp0.withElementType(builder, ty.getElementType()));
3766 return cir::VecSplatOp::create(builder, loc, ty, scalar);
3767 }
3768 case NEON::BI__builtin_neon_vldap1_lane_s64:
3769 case NEON::BI__builtin_neon_vldap1q_lane_s64:
3770 cgm.errorNYI(expr->getSourceRange(),
3771 std::string("unimplemented AArch64 builtin call: ") +
3772 getContext().BuiltinInfo.getName(builtinID));
3773 return mlir::Value{};
3774 case NEON::BI__builtin_neon_vst1_lane_v:
3775 case NEON::BI__builtin_neon_vst1q_lane_v: {
3776 // An mfp8 lane is stored as an i8.
3777 cir::VectorType vecTy = ty;
3778 if (mlir::isa<cir::MFloat8Type>(ty.getElementType()))
3779 vecTy = getIntVecFromVecTy(builder, ty);
3780 ops[1] = builder.createBitcast(ops[1], vecTy);
3781 mlir::Value scalar = builder.createExtractElement(
3782 loc, ops[1], static_cast<uint64_t>(getIntValueFromConstOp(ops[2])));
3783 builder.createStore(loc, scalar,
3784 ptrOp0.withElementType(builder, scalar.getType()));
3785 return nullptr;
3786 }
3787 case NEON::BI__builtin_neon_vstl1_lane_s64:
3788 case NEON::BI__builtin_neon_vstl1q_lane_s64:
3789 cgm.errorNYI(expr->getSourceRange(),
3790 std::string("unimplemented AArch64 builtin call: ") +
3791 getContext().BuiltinInfo.getName(builtinID));
3792 return mlir::Value{};
3793 case NEON::BI__builtin_neon_vld2_v:
3794 case NEON::BI__builtin_neon_vld2q_v:
3795 return emitNeonMultiVecLoad(*this, ops, llvm::Intrinsic::aarch64_neon_ld2,
3796 ty, 2, loc);
3797 case NEON::BI__builtin_neon_vld3_v:
3798 case NEON::BI__builtin_neon_vld3q_v:
3799 return emitNeonMultiVecLoad(*this, ops, llvm::Intrinsic::aarch64_neon_ld3,
3800 ty, 3, loc);
3801 case NEON::BI__builtin_neon_vld4_v:
3802 case NEON::BI__builtin_neon_vld4q_v:
3803 return emitNeonMultiVecLoad(*this, ops, llvm::Intrinsic::aarch64_neon_ld4,
3804 ty, 4, loc);
3805 case NEON::BI__builtin_neon_vld2_dup_v:
3806 case NEON::BI__builtin_neon_vld2q_dup_v:
3807 return emitNeonMultiVecLoad(*this, ops, llvm::Intrinsic::aarch64_neon_ld2r,
3808 ty, 2, loc);
3809 case NEON::BI__builtin_neon_vld3_dup_v:
3810 case NEON::BI__builtin_neon_vld3q_dup_v:
3811 return emitNeonMultiVecLoad(*this, ops, llvm::Intrinsic::aarch64_neon_ld3r,
3812 ty, 3, loc);
3813 case NEON::BI__builtin_neon_vld4_dup_v:
3814 case NEON::BI__builtin_neon_vld4q_dup_v:
3815 return emitNeonMultiVecLoad(*this, ops, llvm::Intrinsic::aarch64_neon_ld4r,
3816 ty, 4, loc);
3817 case NEON::BI__builtin_neon_vld2_lane_v:
3818 case NEON::BI__builtin_neon_vld2q_lane_v:
3820 *this, ops, llvm::Intrinsic::aarch64_neon_ld2lane, ty, 2, loc);
3821 case NEON::BI__builtin_neon_vld3_lane_v:
3822 case NEON::BI__builtin_neon_vld3q_lane_v:
3824 *this, ops, llvm::Intrinsic::aarch64_neon_ld3lane, ty, 3, loc);
3825 case NEON::BI__builtin_neon_vld4_lane_v:
3826 case NEON::BI__builtin_neon_vld4q_lane_v:
3828 *this, ops, llvm::Intrinsic::aarch64_neon_ld4lane, ty, 4, loc);
3829 case NEON::BI__builtin_neon_vst2_v:
3830 case NEON::BI__builtin_neon_vst2q_v: {
3831 // The builtin call has the pointer first, but the AArch64 st2 intrinsic
3832 // takes the vector operands first and the pointer last.
3833 std::rotate(ops.begin(), ops.begin() + 1, ops.end());
3834 llvm::SmallVector<mlir::Type> argTypes = {ty, ty, builder.getVoidPtrTy()};
3835 return emitNeonCall(cgm, builder, argTypes, ops, "aarch64.neon.st2",
3836 cgm.voidTy, loc);
3837 }
3838 case NEON::BI__builtin_neon_vst2_lane_v:
3839 case NEON::BI__builtin_neon_vst2q_lane_v: {
3840 std::rotate(ops.begin(), ops.begin() + 1, ops.end());
3841 ops[2] = builder.createIntCast(ops[2], sInt64Ty);
3842 llvm::SmallVector<mlir::Type> argTypes = {ty, ty, sInt64Ty,
3843 builder.getVoidPtrTy()};
3844 return emitNeonCall(cgm, builder, argTypes, ops, "aarch64.neon.st2lane",
3845 cgm.voidTy, loc);
3846 }
3847 case NEON::BI__builtin_neon_vst3_v:
3848 case NEON::BI__builtin_neon_vst3q_v: {
3849 std::rotate(ops.begin(), ops.begin() + 1, ops.end());
3850 llvm::SmallVector<mlir::Type> argTypes = {ty, ty, ty,
3851 builder.getVoidPtrTy()};
3852 return emitNeonCall(cgm, builder, argTypes, ops, "aarch64.neon.st3",
3853 cgm.voidTy, loc);
3854 }
3855 case NEON::BI__builtin_neon_vst3_lane_v:
3856 case NEON::BI__builtin_neon_vst3q_lane_v: {
3857 std::rotate(ops.begin(), ops.begin() + 1, ops.end());
3858 ops[3] = builder.createIntCast(ops[3], sInt64Ty);
3859 llvm::SmallVector<mlir::Type> argTypes = {ty, ty, ty, sInt64Ty,
3860 builder.getVoidPtrTy()};
3861 return emitNeonCall(cgm, builder, argTypes, ops, "aarch64.neon.st3lane",
3862 cgm.voidTy, loc);
3863 }
3864 case NEON::BI__builtin_neon_vst4_v:
3865 case NEON::BI__builtin_neon_vst4q_v: {
3866 std::rotate(ops.begin(), ops.begin() + 1, ops.end());
3867 llvm::SmallVector<mlir::Type> argTypes = {ty, ty, ty, ty,
3868 builder.getVoidPtrTy()};
3869 return emitNeonCall(cgm, builder, argTypes, ops, "aarch64.neon.st4",
3870 cgm.voidTy, loc);
3871 }
3872 case NEON::BI__builtin_neon_vst4_lane_v:
3873 case NEON::BI__builtin_neon_vst4q_lane_v: {
3874 std::rotate(ops.begin(), ops.begin() + 1, ops.end());
3875 ops[4] = builder.createIntCast(ops[4], sInt64Ty);
3877 ty, ty, ty, ty, sInt64Ty, builder.getVoidPtrTy()};
3878 return emitNeonCall(cgm, builder, argTypes, ops, "aarch64.neon.st4lane",
3879 cgm.voidTy, loc);
3880 }
3881 case NEON::BI__builtin_neon_vtrn_v:
3882 case NEON::BI__builtin_neon_vtrnq_v: {
3883 ops[1] = builder.createBitcast(ops[1], ty);
3884 ops[2] = builder.createBitcast(ops[2], ty);
3885 // Adding a bitcast here as Ops[0] might be a void pointer.
3886 mlir::Value baseAddr =
3887 builder.createBitcast(ops[0], builder.getPointerTo(ty));
3888 mlir::Value sv;
3889
3890 for (unsigned vi = 0; vi != 2; ++vi) {
3892 for (unsigned i = 0, e = ty.getFixedSize(); i != e; i += 2) {
3893 indices.push_back(i + vi);
3894 indices.push_back(i + e + vi);
3895 }
3896 cir::ConstantOp idx = builder.getConstInt(loc, builder.getSInt32Ty(), vi);
3897 mlir::Value addr = builder.createPtrStride(loc, baseAddr, idx);
3898 sv = builder.createVecShuffle(loc, ops[1], ops[2], indices);
3899 (void)builder.CIRBaseBuilderTy::createStore(loc, sv, addr);
3900 }
3901 return sv;
3902 }
3903 case NEON::BI__builtin_neon_vuzp_v:
3904 case NEON::BI__builtin_neon_vuzpq_v: {
3905 ops[1] = builder.createBitcast(ops[1], ty);
3906 ops[2] = builder.createBitcast(ops[2], ty);
3907 // Adding a bitcast here as Ops[0] might be a void pointer.
3908 mlir::Value baseAddr =
3909 builder.createBitcast(ops[0], builder.getPointerTo(ty));
3910 mlir::Value sv;
3911 for (unsigned vi = 0; vi != 2; ++vi) {
3913 for (unsigned i = 0, e = ty.getFixedSize(); i != e; ++i) {
3914 indices.push_back(2 * i + vi);
3915 }
3916 cir::ConstantOp idx = builder.getConstInt(loc, builder.getSInt32Ty(), vi);
3917 mlir::Value addr = builder.createPtrStride(loc, baseAddr, idx);
3918 sv = builder.createVecShuffle(loc, ops[1], ops[2], indices);
3919 (void)builder.CIRBaseBuilderTy::createStore(loc, sv, addr);
3920 }
3921 return sv;
3922 }
3923 case NEON::BI__builtin_neon_vzip_v:
3924 case NEON::BI__builtin_neon_vzipq_v: {
3925 ops[1] = builder.createBitcast(ops[1], ty);
3926 ops[2] = builder.createBitcast(ops[2], ty);
3927 // Adding a bitcast here as Ops[0] might be a void pointer.
3928 mlir::Value baseAddr =
3929 builder.createBitcast(ops[0], builder.getPointerTo(ty));
3930 mlir::Value sv;
3931 for (unsigned vi = 0; vi != 2; ++vi) {
3933 for (unsigned i = 0, e = ty.getFixedSize(); i != e; i += 2) {
3934 indices.push_back((i + vi * e) >> 1);
3935 indices.push_back(((i + vi * e) >> 1) + e);
3936 }
3937 cir::ConstantOp idx = builder.getConstInt(loc, builder.getSInt32Ty(), vi);
3938 mlir::Value addr = builder.createPtrStride(loc, baseAddr, idx);
3939 sv = builder.createVecShuffle(loc, ops[1], ops[2], indices);
3940 (void)builder.CIRBaseBuilderTy::createStore(loc, sv, addr);
3941 }
3942 return sv;
3943 }
3944 case NEON::BI__builtin_neon_vqtbl1q_v:
3945 case NEON::BI__builtin_neon_vqtbl2q_v:
3946 case NEON::BI__builtin_neon_vqtbl3q_v:
3947 case NEON::BI__builtin_neon_vqtbl4q_v:
3948 case NEON::BI__builtin_neon_vqtbx1q_v:
3949 case NEON::BI__builtin_neon_vqtbx2q_v:
3950 case NEON::BI__builtin_neon_vqtbx3q_v:
3951 case NEON::BI__builtin_neon_vqtbx4q_v:
3952 cgm.errorNYI(expr->getSourceRange(),
3953 std::string("unimplemented AArch64 builtin call: ") +
3954 getContext().BuiltinInfo.getName(builtinID));
3955 return mlir::Value{};
3956 case NEON::BI__builtin_neon_vsqadd_v:
3957 case NEON::BI__builtin_neon_vsqaddq_v:
3958 return emitNeonCall(cgm, builder, {ty, ty}, ops, "aarch64.neon.usqadd", ty,
3959 loc);
3960 case NEON::BI__builtin_neon_vuqadd_v:
3961 case NEON::BI__builtin_neon_vuqaddq_v:
3962 return emitNeonCall(cgm, builder, {ty, ty}, ops, "aarch64.neon.suqadd", ty,
3963 loc);
3964 case NEON::BI__builtin_neon_vluti2_laneq_mf8:
3965 case NEON::BI__builtin_neon_vluti2_laneq_bf16:
3966 case NEON::BI__builtin_neon_vluti2_laneq_f16:
3967 case NEON::BI__builtin_neon_vluti2_laneq_p16:
3968 case NEON::BI__builtin_neon_vluti2_laneq_p8:
3969 case NEON::BI__builtin_neon_vluti2_laneq_s16:
3970 case NEON::BI__builtin_neon_vluti2_laneq_s8:
3971 case NEON::BI__builtin_neon_vluti2_laneq_u16:
3972 case NEON::BI__builtin_neon_vluti2_laneq_u8:
3973 case NEON::BI__builtin_neon_vluti2q_laneq_mf8:
3974 case NEON::BI__builtin_neon_vluti2q_laneq_bf16:
3975 case NEON::BI__builtin_neon_vluti2q_laneq_f16:
3976 case NEON::BI__builtin_neon_vluti2q_laneq_p16:
3977 case NEON::BI__builtin_neon_vluti2q_laneq_p8:
3978 case NEON::BI__builtin_neon_vluti2q_laneq_s16:
3979 case NEON::BI__builtin_neon_vluti2q_laneq_s8:
3980 case NEON::BI__builtin_neon_vluti2q_laneq_u16:
3981 case NEON::BI__builtin_neon_vluti2q_laneq_u8:
3982 case NEON::BI__builtin_neon_vluti2_lane_mf8:
3983 case NEON::BI__builtin_neon_vluti2_lane_bf16:
3984 case NEON::BI__builtin_neon_vluti2_lane_f16:
3985 case NEON::BI__builtin_neon_vluti2_lane_p16:
3986 case NEON::BI__builtin_neon_vluti2_lane_p8:
3987 case NEON::BI__builtin_neon_vluti2_lane_s16:
3988 case NEON::BI__builtin_neon_vluti2_lane_s8:
3989 case NEON::BI__builtin_neon_vluti2_lane_u16:
3990 case NEON::BI__builtin_neon_vluti2_lane_u8:
3991 case NEON::BI__builtin_neon_vluti2q_lane_mf8:
3992 case NEON::BI__builtin_neon_vluti2q_lane_bf16:
3993 case NEON::BI__builtin_neon_vluti2q_lane_f16:
3994 case NEON::BI__builtin_neon_vluti2q_lane_p16:
3995 case NEON::BI__builtin_neon_vluti2q_lane_p8:
3996 case NEON::BI__builtin_neon_vluti2q_lane_s16:
3997 case NEON::BI__builtin_neon_vluti2q_lane_s8:
3998 case NEON::BI__builtin_neon_vluti2q_lane_u16:
3999 case NEON::BI__builtin_neon_vluti2q_lane_u8:
4000 case NEON::BI__builtin_neon_vluti4q_lane_mf8:
4001 case NEON::BI__builtin_neon_vluti4q_lane_p8:
4002 case NEON::BI__builtin_neon_vluti4q_lane_s8:
4003 case NEON::BI__builtin_neon_vluti4q_lane_u8:
4004 case NEON::BI__builtin_neon_vluti4q_laneq_mf8:
4005 case NEON::BI__builtin_neon_vluti4q_laneq_p8:
4006 case NEON::BI__builtin_neon_vluti4q_laneq_s8:
4007 case NEON::BI__builtin_neon_vluti4q_laneq_u8:
4008 case NEON::BI__builtin_neon_vluti4q_lane_bf16_x2:
4009 case NEON::BI__builtin_neon_vluti4q_lane_f16_x2:
4010 case NEON::BI__builtin_neon_vluti4q_lane_p16_x2:
4011 case NEON::BI__builtin_neon_vluti4q_lane_s16_x2:
4012 case NEON::BI__builtin_neon_vluti4q_lane_u16_x2:
4013 case NEON::BI__builtin_neon_vluti4q_laneq_bf16_x2:
4014 case NEON::BI__builtin_neon_vluti4q_laneq_f16_x2:
4015 case NEON::BI__builtin_neon_vluti4q_laneq_p16_x2:
4016 case NEON::BI__builtin_neon_vluti4q_laneq_s16_x2:
4017 case NEON::BI__builtin_neon_vluti4q_laneq_u16_x2:
4018 case NEON::BI__builtin_neon_vmmlaq_f16_mf8_fpm:
4019 case NEON::BI__builtin_neon_vmmlaq_f32_mf8_fpm:
4020 case NEON::BI__builtin_neon_vcvt1_low_bf16_mf8_fpm:
4021 case NEON::BI__builtin_neon_vcvt1_bf16_mf8_fpm:
4022 case NEON::BI__builtin_neon_vcvt1_high_bf16_mf8_fpm:
4023 case NEON::BI__builtin_neon_vcvt2_low_bf16_mf8_fpm:
4024 case NEON::BI__builtin_neon_vcvt2_bf16_mf8_fpm:
4025 case NEON::BI__builtin_neon_vcvt2_high_bf16_mf8_fpm:
4026 case NEON::BI__builtin_neon_vcvt1_low_f16_mf8_fpm:
4027 case NEON::BI__builtin_neon_vcvt1_f16_mf8_fpm:
4028 case NEON::BI__builtin_neon_vcvt1_high_f16_mf8_fpm:
4029 case NEON::BI__builtin_neon_vcvt2_low_f16_mf8_fpm:
4030 case NEON::BI__builtin_neon_vcvt2_f16_mf8_fpm:
4031 case NEON::BI__builtin_neon_vcvt2_high_f16_mf8_fpm:
4032 case NEON::BI__builtin_neon_vcvt_mf8_f32_fpm:
4033 case NEON::BI__builtin_neon_vcvt_mf8_f16_fpm:
4034 case NEON::BI__builtin_neon_vcvtq_mf8_f16_fpm:
4035 case NEON::BI__builtin_neon_vcvt_high_mf8_f32_fpm:
4036 case NEON::BI__builtin_neon_vdot_f16_mf8_fpm:
4037 case NEON::BI__builtin_neon_vdotq_f16_mf8_fpm:
4038 case NEON::BI__builtin_neon_vdot_lane_f16_mf8_fpm:
4039 case NEON::BI__builtin_neon_vdotq_lane_f16_mf8_fpm:
4040 case NEON::BI__builtin_neon_vdot_laneq_f16_mf8_fpm:
4041 case NEON::BI__builtin_neon_vdotq_laneq_f16_mf8_fpm:
4042 case NEON::BI__builtin_neon_vdot_f32_mf8_fpm:
4043 case NEON::BI__builtin_neon_vdotq_f32_mf8_fpm:
4044 case NEON::BI__builtin_neon_vdot_lane_f32_mf8_fpm:
4045 case NEON::BI__builtin_neon_vdotq_lane_f32_mf8_fpm:
4046 case NEON::BI__builtin_neon_vdot_laneq_f32_mf8_fpm:
4047 case NEON::BI__builtin_neon_vdotq_laneq_f32_mf8_fpm:
4048 case NEON::BI__builtin_neon_vmlalbq_f16_mf8_fpm:
4049 case NEON::BI__builtin_neon_vmlaltq_f16_mf8_fpm:
4050 case NEON::BI__builtin_neon_vmlallbbq_f32_mf8_fpm:
4051 case NEON::BI__builtin_neon_vmlallbtq_f32_mf8_fpm:
4052 case NEON::BI__builtin_neon_vmlalltbq_f32_mf8_fpm:
4053 case NEON::BI__builtin_neon_vmlallttq_f32_mf8_fpm:
4054 case NEON::BI__builtin_neon_vmlalbq_lane_f16_mf8_fpm:
4055 case NEON::BI__builtin_neon_vmlalbq_laneq_f16_mf8_fpm:
4056 case NEON::BI__builtin_neon_vmlaltq_lane_f16_mf8_fpm:
4057 case NEON::BI__builtin_neon_vmlaltq_laneq_f16_mf8_fpm:
4058 case NEON::BI__builtin_neon_vmlallbbq_lane_f32_mf8_fpm:
4059 case NEON::BI__builtin_neon_vmlallbbq_laneq_f32_mf8_fpm:
4060 case NEON::BI__builtin_neon_vmlallbtq_lane_f32_mf8_fpm:
4061 case NEON::BI__builtin_neon_vmlallbtq_laneq_f32_mf8_fpm:
4062 case NEON::BI__builtin_neon_vmlalltbq_lane_f32_mf8_fpm:
4063 case NEON::BI__builtin_neon_vmlalltbq_laneq_f32_mf8_fpm:
4064 case NEON::BI__builtin_neon_vmlallttq_lane_f32_mf8_fpm:
4065 case NEON::BI__builtin_neon_vmlallttq_laneq_f32_mf8_fpm:
4066 case NEON::BI__builtin_neon_vamin_f16:
4067 case NEON::BI__builtin_neon_vaminq_f16:
4068 case NEON::BI__builtin_neon_vamin_f32:
4069 case NEON::BI__builtin_neon_vaminq_f32:
4070 case NEON::BI__builtin_neon_vaminq_f64:
4071 case NEON::BI__builtin_neon_vamax_f16:
4072 case NEON::BI__builtin_neon_vamaxq_f16:
4073 case NEON::BI__builtin_neon_vamax_f32:
4074 case NEON::BI__builtin_neon_vamaxq_f32:
4075 case NEON::BI__builtin_neon_vamaxq_f64:
4076 case NEON::BI__builtin_neon_vscale_f16:
4077 case NEON::BI__builtin_neon_vscaleq_f16:
4078 case NEON::BI__builtin_neon_vscale_f32:
4079 case NEON::BI__builtin_neon_vscaleq_f32:
4080 case NEON::BI__builtin_neon_vscaleq_f64:
4081 cgm.errorNYI(expr->getSourceRange(),
4082 std::string("unimplemented AArch64 builtin call: ") +
4083 getContext().BuiltinInfo.getName(builtinID));
4084 return mlir::Value{};
4085 }
4086
4087 // Unreachable: All cases in the switch above return.
4088}
Utilities used for generating code for AArch64 that are shared between the classic and ClangIR code-g...
static bool isUnsigned(SValBuilder &SVB, NonLoc Value)
Defines enum values for all the target-independent builtin functions.
static std::pair< mlir::Type, llvm::SmallVector< mlir::Type > > deriveNeonSISDIntrinsicOperandTypes(CIRGenFunction &cgf, unsigned modifier, mlir::Type arg0Ty, mlir::Type resultTy, llvm::ArrayRef< mlir::Value > ops, unsigned iceArguments)
static mlir::Value emitAArch64CompareBuiltinExpr(CIRGenFunction &cgf, CIRGenBuilderTy &builder, mlir::Location loc, mlir::Value src, mlir::Type retTy, const CmpKindTy kind)
static bool aarch64SVEIntrinsicsProvenSorted
static const std::pair< unsigned, unsigned > neonEquivalentIntrinsicMap[]
static mlir::Value emitNeonSplat(CIRGenBuilderTy &builder, mlir::Location loc, mlir::Value v, mlir::Value lane, unsigned int resEltCnt)
static mlir::Value emitNeonCallToOp(CIRGenModule &cgm, CIRGenBuilderTy &builder, llvm::SmallVector< mlir::Type > argTypes, llvm::SmallVectorImpl< mlir::Value > &args, std::optional< llvm::StringRef > intrinsicName, mlir::Type funcResTy, mlir::Location loc, bool isConstrainedFPIntrinsic=false, unsigned shift=0, bool rightshift=false)
static cir::VectorType getSVEVectorForElementType(CIRGenModule &cgm, mlir::Type eltTy)
static cir::RecordType getNeonMultiVecTy(CIRGenBuilderTy &builder, mlir::Type vecTy, unsigned numVecs)
Return the anonymous record type {vecTy, ..., vecTy} (numVecs members) modelling the struct returned ...
static unsigned getSVEMinEltCount(clang::SVETypeFlags::EltType sveType)
static cir::VectorType getFloatNeonType(CIRGenFunction &cgf, NeonTypeFlags intTypeFlags)
static llvm::StringRef getLLVMIntrNameNoPrefix(llvm::Intrinsic::ID intrID)
static mlir::Value emitNeonMultiVecLoadCall(CIRGenFunction &cgf, mlir::Value dest, llvm::SmallVector< mlir::Type > argTypes, llvm::SmallVectorImpl< mlir::Value > &args, llvm::Intrinsic::ID llvmIntrinsic, mlir::Type ty, unsigned numVecs, mlir::Location loc)
Call the multi-vector NEON load intrinsic llvmIntrinsic with args of argTypes and store the resulting...
static int64_t getIntValueFromConstOp(mlir::Value val)
static cir::VectorType deriveNeonBinaryArgType(CIRGenBuilderTy &builder, unsigned modifier, cir::VectorType vTy)
static const AArch64SVEAndSMEVectorIntrinsicInfo aarch64SVEIntrinsicMap[]
static mlir::Value emitCommonNeonBuiltinExpr(CIRGenFunction &cgf, unsigned builtinID, unsigned llvmIntrinsic, unsigned altLLVMIntrinsic, const char *nameHint, unsigned modifier, const CallExpr *expr, llvm::SmallVectorImpl< mlir::Value > &ops)
static mlir::Value emitNeonCall(CIRGenModule &cgm, CIRGenBuilderTy &builder, llvm::SmallVector< mlir::Type > argTypes, llvm::SmallVectorImpl< mlir::Value > &args, llvm::StringRef intrinsicName, mlir::Type funcResTy, mlir::Location loc, bool isConstrainedFPIntrinsic=false, unsigned shift=0, bool rightshift=false)
static unsigned getNeonMultiVecCount(unsigned builtinID)
The vld1_xN builtins move their data through an aggregate of N vectors.
static mlir::Value emitCommonNeonSISDBuiltinExpr(CIRGenFunction &cgf, const ARMNeonVectorIntrinsicInfo &info, llvm::SmallVectorImpl< mlir::Value > &ops, const CallExpr *expr, unsigned iceArguments)
static mlir::Value genVscaleTimesFactor(mlir::Location loc, CIRGenBuilderTy &builder, mlir::Type cirTy, int32_t scalingFactor)
static cir::VectorType getSignChangedVectorType(CIRGenBuilderTy &builder, cir::VectorType vecTy)
Flip the signedness of vecTy's element type, keeping the width and number of lanes the same.
static cir::VectorType getNeonType(CIRGenFunction *cgf, NeonTypeFlags typeFlags, bool hasLegalHalfType=true, bool v1Ty=false, bool allowBFloatArgsAndRet=true)
static cir::VectorType getNeonPairwiseWidenInputType(cir::VectorType resType, bool usgn)
static mlir::Value emitCommonNeonShift(CIRGenBuilderTy &builder, mlir::Location loc, cir::VectorType resTy, mlir::Value shifTgt, mlir::Value shiftAmt, bool shiftLeft)
static bool aarch64SIMDIntrinsicsProvenSorted
static cir::VectorType getIntVecFromVecTy(CIRGenBuilderTy &builder, cir::VectorType vecTy)
constexpr unsigned sveBitsPerBlock
static mlir::Value emitNeonMultiVecLoadLane(CIRGenFunction &cgf, llvm::ArrayRef< mlir::Value > ops, llvm::Intrinsic::ID llvmIntrinsic, mlir::Type ty, unsigned numVecs, mlir::Location loc)
Emit a multi-vector NEON load-lane: call llvmIntrinsic on the numVecs vector operands,...
static mlir::Value emitNeonMultiVecLoad(CIRGenFunction &cgf, llvm::ArrayRef< mlir::Value > ops, unsigned llvmIntrinsic, mlir::Type ty, unsigned numVecs, mlir::Location loc)
Emit a multi-vector NEON load: call llvmIntrinsic on the source pointer ops[1] and store the resultin...
static void vecExtendIntValue(CIRGenFunction &cgf, cir::VectorType argVTy, mlir::Value &arg, mlir::Location loc)
Create a vector from an input scalar argument, usually for a NEON SISD intrinsic call.
static mlir::Value emitNeonShiftVector(CIRGenBuilderTy &builder, mlir::Value shiftVal, cir::VectorType vecTy, mlir::Location loc, bool neg)
Build a constant shift amount vector of vecTy to shift a vector Here shiftVal is a constant integer t...
static const IntrinsicInfo * findARMVectorIntrinsicInMap(ArrayRef< IntrinsicInfo > intrinsicMap, unsigned builtinID, bool &mapProvenSorted)
static mlir::Value emitNeonRShiftImm(CIRGenFunction &cgf, mlir::Value shiftVec, mlir::Value shiftVal, cir::VectorType vecTy, bool usgn, mlir::Location loc)
static bool aarch64SISDIntrinsicsProvenSorted
TokenType getType() const
Returns the token's type, e.g.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
Enumerates target-specific builtins in their own namespaces within namespace clang.
mlir::Value createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowFlags flags=OverflowFlags::None)
cir::FenvAttr getConstrainedFPAttr()
Build the #cir.fenv attribute describing the constrained floating-point environment currently in effe...
mlir::Value createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowFlags flags=OverflowFlags::None)
cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc)
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr)
mlir::Value createCast(mlir::Location loc, cir::CastKind kind, mlir::Value src, mlir::Type newTy)
cir::VecFCmpOp createVecFCompare(mlir::Location loc, cir::FCmpOpKind kind, mlir::Value lhs, mlir::Value rhs, bool signaling=false)
Compares lhs and rhs element-wise.
mlir::Value createNUWAMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy)
cir::VecCmpOp createVecCompare(mlir::Location loc, cir::CmpOpKind kind, mlir::Value lhs, mlir::Value rhs)
Compares lhs and rhs element-wise.
mlir::Value createIntCast(mlir::Value src, mlir::Type newTy)
cir::FastMathFlagsAttr getFastMathFlagsAttr()
Returns a null attribute when no fast-math flags are set.
mlir::Value createBitcast(mlir::Value src, mlir::Type newTy)
cir::CmpOp createCompare(mlir::Location loc, cir::CmpOpKind kind, mlir::Value lhs, mlir::Value rhs)
cir::ConstantOp getConstantInt(mlir::Location loc, mlir::Type ty, int64_t value)
cir::PointerType getVoidPtrTy(clang::LangAS langAS=clang::LangAS::Default)
mlir::Value createShiftRight(mlir::Location loc, mlir::Value lhs, unsigned bits)
mlir::Value createXor(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
llvm::Align getABITypeAlign(mlir::Type ty) const
llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const
C++ view class that accepts both !cir.struct and !cir.union types.
Definition CIRTypes.h:153
static llvm::SmallVector< RecordMemberKind > getAllDataKinds(llvm::ArrayRef< mlir::Type > members)
One Data kind per member.
Definition CIRTypes.cpp:156
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
Builtin::Context & BuiltinInfo
Definition ASTContext.h:857
QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error, unsigned *IntegerConstantArgs=nullptr) const
Return the type for the specified builtin.
@ GE_None
No error.
std::string getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
Definition Builtins.cpp:94
mlir::Value getPointer() const
Definition Address.h:98
static Address invalid()
Definition Address.h:76
Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const
Return address with different element type, a bitcast pointer, and the same alignment.
cir::ConstantOp getUInt64(uint64_t c, mlir::Location loc)
mlir::Value emitIntrinsicCallOp(mlir::Location loc, const llvm::StringRef str, const mlir::Type &resTy, Operands &&...op)
cir::IntType getSIntNTy(int n)
cir::VecShuffleOp createVecShuffle(mlir::Location loc, mlir::Value vec1, mlir::Value vec2, llvm::ArrayRef< mlir::Attribute > maskAttrs)
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, Address dst, bool isVolatile=false, bool isNontemporal=false, mlir::IntegerAttr align={}, cir::SyncScopeKindAttr scope={}, cir::MemOrderAttr order={})
cir::ConstantOp getZero(mlir::Location loc, mlir::Type ty)
cir::StructType getAnonRecordTy(llvm::ArrayRef< mlir::Type > members, bool packed, llvm::ArrayRef< cir::RecordMemberKind > memberKinds)
Get a CIR anonymous struct type.
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal)
cir::VectorType getExtendedOrTruncatedElementVectorType(cir::VectorType vt, bool isExtended, bool isSigned=false)
cir::IntType getUIntNTy(int n)
mlir::Type convertType(clang::QualType t)
Address emitPointerWithAlignment(const clang::Expr *expr, LValueBaseInfo *baseInfo=nullptr)
Given an expression with a pointer type, emit the value and compute our best estimate of the alignmen...
const TargetInfo & getTarget() const
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
static int64_t getZExtIntValueFromConstOp(mlir::Value val)
Get zero-extended integer from a mlir::Value that is an int constant or a constant op.
mlir::Value emitSVEPredicateCast(mlir::Value pred, unsigned minNumElts, mlir::Location loc)
bool getAArch64SVEProcessedOperands(unsigned builtinID, const CallExpr *expr, SmallVectorImpl< mlir::Value > &ops, clang::SVETypeFlags typeFlags)
Address returnValue
The temporary alloca to hold the return value.
std::optional< mlir::Value > emitAArch64BuiltinExpr(unsigned builtinID, const CallExpr *expr, ReturnValueSlot returnValue, llvm::Triple::ArchType arch)
std::optional< mlir::Value > emitAArch64SMEBuiltinExpr(unsigned builtinID, const CallExpr *expr)
mlir::Value emitScalarExpr(const clang::Expr *e, bool ignoreResultAssign=false)
Emit the computation of the specified expression of scalar type.
CIRGenBuilderTy & getBuilder()
cir::VectorType getSVEType(const SVETypeFlags &typeFlags)
clang::ASTContext & getContext() const
std::optional< mlir::Value > emitAArch64SVEBuiltinExpr(unsigned builtinID, const CallExpr *expr)
mlir::Value emitScalarOrConstFoldImmArg(unsigned iceArguments, unsigned idx, const Expr *argExpr)
This class organizes the cross-function state that is used while generating CIR code.
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
const cir::CIRDataLayout & getDataLayout() const
Contains the address where the return value of a function can be stored, and whether the address is v...
Definition CIRGenCall.h:262
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:3012
static CharUnits fromQuantity(QuantityType Quantity)
Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:58
This represents one expression.
Definition Expr.h:114
Flags to identify the types for overloaded Neon builtins.
bool isFloatingPoint() const
EltType getEltType() const
Flags to identify the types for overloaded SVE builtins.
bool isReverseUSDOT() const
bool isGatherLoad() const
EltType getEltType() const
bool isPrefetch() const
bool isTupleSet() const
bool isReverseMergeAnyAccOp() const
bool isTupleGet() const
bool isInsertOp1SVALL() const
bool isAppendSVALL() const
bool isReverseMergeAnyBinOp() const
bool isStructStore() const
bool isTupleCreate() const
bool isGatherPrefetch() const
bool hasSplatOperand() const
MergeType getMergeType() const
bool isStructLoad() const
unsigned getSplatOperand() const
bool isScatterStore() const
bool isReverseCompare() const
virtual bool hasFastHalfType() const
Determine whether the target has fast native support for operations on half types.
Definition TargetInfo.h:707
Op::Properties getDefaultProperties(mlir::MLIRContext *context)
The properties of Op with every ODS-declared default applied.
Definition CIRDialect.h:85
bool hasExtraNeonArgument(unsigned BuiltinID)
Returns whether the Neon builtin BuiltinID takes a trailing argument that discriminates the operand t...
const ARMNeonVectorIntrinsicInfo AArch64SISDIntrinsicMap[]
const ARMNeonVectorIntrinsicInfo AArch64SIMDIntrinsicMap[]
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
U cast(CodeGen::Address addr)
Definition Address.h:327
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat int32_t
static bool msvcBuiltins()
static bool handleBuiltinICEArguments()
static bool aarch64SIMDIntrinsics()
static bool aarch64SVEIntrinsics()
static bool emitConstrainedFPCall()
static bool aarch64SMEIntrinsics()
static bool aarch64TblBuiltinExpr()
Describes an AArch64 SVE or SME intrinsic.
Describes an ARM or AArch64 NEON intrinsic, or an AArch64 SISD intrinsic.