| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// This contains code to emit Builtin calls as CIR or a function call to be |
| 10 |
// later resolved. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#include "CIRGenCall.h" |
| 15 |
#include "CIRGenFunction.h" |
| 16 |
#include "CIRGenModule.h" |
| 17 |
#include "CIRGenValue.h" |
| 18 |
#include "mlir/IR/BuiltinAttributes.h" |
| 19 |
#include "mlir/IR/Value.h" |
| 20 |
#include "mlir/Support/LLVM.h" |
| 21 |
#include "clang/AST/DeclBase.h" |
| 22 |
#include "clang/AST/Expr.h" |
| 23 |
#include "clang/AST/GlobalDecl.h" |
| 24 |
#include "clang/Basic/Builtins.h" |
| 25 |
#include "clang/Basic/OperatorKinds.h" |
| 26 |
#include "clang/CIR/Dialect/IR/CIRTypes.h" |
| 27 |
#include "clang/CIR/MissingFeatures.h" |
| 28 |
#include "llvm/Support/ErrorHandling.h" |
| 29 |
|
| 30 |
using namespace clang; |
| 31 |
using namespace clang::CIRGen; |
| 32 |
using namespace llvm; |
| 33 |
|
| 34 |
static RValue emitLibraryCall(CIRGenFunction &cgf, const FunctionDecl *fd, |
| 35 |
const CallExpr *e, mlir::Operation *calleeValue) { |
| 36 |
CIRGenCallee callee = CIRGenCallee::forDirect(calleeValue, GlobalDecl(fd)); |
| 37 |
return cgf.emitCall(e->getCallee()->getType(), callee, e, ReturnValueSlot()); |
| 38 |
} |
| 39 |
|
| 40 |
template <typename Op> |
| 41 |
static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const CallExpr *e, |
| 42 |
bool poisonZero = false) { |
| 43 |
assert(!cir::MissingFeatures::builtinCheckKind()); |
| 44 |
|
| 45 |
mlir::Value arg = cgf.emitScalarExpr(e->getArg(0)); |
| 46 |
CIRGenBuilderTy &builder = cgf.getBuilder(); |
| 47 |
|
| 48 |
Op op; |
| 49 |
if constexpr (std::is_same_v<Op, cir::BitClzOp> || |
| 50 |
std::is_same_v<Op, cir::BitCtzOp>) |
| 51 |
op = Op::create(builder, cgf.getLoc(e->getSourceRange()), arg, poisonZero); |
| 52 |
else |
| 53 |
op = Op::create(builder, cgf.getLoc(e->getSourceRange()), arg); |
| 54 |
|
| 55 |
mlir::Value result = op.getResult(); |
| 56 |
mlir::Type exprTy = cgf.convertType(e->getType()); |
| 57 |
if (exprTy != result.getType()) |
| 58 |
result = builder.createIntCast(result, exprTy); |
| 59 |
|
| 60 |
return RValue::get(result); |
| 61 |
} |
| 62 |
|
| 63 |
static void emitAtomicFenceOp(CIRGenFunction &cgf, const CallExpr *expr, |
| 64 |
cir::SyncScopeKind syncScope) { |
| 65 |
CIRGenBuilderTy &builder = cgf.getBuilder(); |
| 66 |
mlir::Location loc = cgf.getLoc(expr->getSourceRange()); |
| 67 |
|
| 68 |
auto emitAtomicOpCallBackFn = [&](cir::MemOrder memOrder) { |
| 69 |
cir::AtomicFenceOp::create( |
| 70 |
builder, loc, memOrder, |
| 71 |
cir::SyncScopeKindAttr::get(&cgf.getMLIRContext(), syncScope)); |
| 72 |
}; |
| 73 |
|
| 74 |
cgf.emitAtomicExprWithMemOrder(expr->getArg(0), /*isStore*/ false, |
| 75 |
/*isLoad*/ false, /*isFence*/ true, |
| 76 |
emitAtomicOpCallBackFn); |
| 77 |
} |
| 78 |
|
| 79 |
namespace { |
| 80 |
struct WidthAndSignedness { |
| 81 |
unsigned width; |
| 82 |
bool isSigned; |
| 83 |
}; |
| 84 |
} // namespace |
| 85 |
|
| 86 |
static WidthAndSignedness |
| 87 |
getIntegerWidthAndSignedness(const clang::ASTContext &astContext, |
| 88 |
const clang::QualType type) { |
| 89 |
assert(type->isIntegerType() && "Given type is not an integer."); |
| 90 |
unsigned width = type->isBooleanType() ? 1 |
| 91 |
: type->isBitIntType() ? astContext.getIntWidth(type) |
| 92 |
: astContext.getTypeInfo(type).Width; |
| 93 |
bool isSigned = type->isSignedIntegerType(); |
| 94 |
return {width, isSigned}; |
| 95 |
} |
| 96 |
|
| 97 |
// Given one or more integer types, this function produces an integer type that |
| 98 |
// encompasses them: any value in one of the given types could be expressed in |
| 99 |
// the encompassing type. |
| 100 |
static struct WidthAndSignedness |
| 101 |
EncompassingIntegerType(ArrayRef<struct WidthAndSignedness> types) { |
| 102 |
assert(types.size() > 0 && "Empty list of types."); |
| 103 |
|
| 104 |
// If any of the given types is signed, we must return a signed type. |
| 105 |
bool isSigned = llvm::any_of(types, [](const auto &t) { return t.isSigned; }); |
| 106 |
|
| 107 |
// The encompassing type must have a width greater than or equal to the width |
| 108 |
// of the specified types. Additionally, if the encompassing type is signed, |
| 109 |
// its width must be strictly greater than the width of any unsigned types |
| 110 |
// given. |
| 111 |
unsigned width = 0; |
| 112 |
for (const auto &type : types) |
| 113 |
width = std::max(width, type.width + (isSigned && !type.isSigned)); |
| 114 |
|
| 115 |
return {width, isSigned}; |
| 116 |
} |
| 117 |
|
| 118 |
RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) { |
| 119 |
mlir::Value input = emitScalarExpr(e->getArg(0)); |
| 120 |
mlir::Value amount = emitScalarExpr(e->getArg(1)); |
| 121 |
|
| 122 |
// TODO(cir): MSVC flavor bit rotate builtins use different types for input |
| 123 |
// and amount, but cir.rotate requires them to have the same type. Cast amount |
| 124 |
// to the type of input when necessary. |
| 125 |
assert(!cir::MissingFeatures::msvcBuiltins()); |
| 126 |
|
| 127 |
auto r = cir::RotateOp::create(builder, getLoc(e->getSourceRange()), input, |
| 128 |
amount, isRotateLeft); |
| 129 |
return RValue::get(r); |
| 130 |
} |
| 131 |
|
| 132 |
template <class Operation> |
| 133 |
static RValue emitUnaryMaybeConstrainedFPBuiltin(CIRGenFunction &cgf, |
| 134 |
const CallExpr &e) { |
| 135 |
mlir::Value arg = cgf.emitScalarExpr(e.getArg(0)); |
| 136 |
|
| 137 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 138 |
assert(!cir::MissingFeatures::fpConstraints()); |
| 139 |
|
| 140 |
auto call = |
| 141 |
Operation::create(cgf.getBuilder(), arg.getLoc(), arg.getType(), arg); |
| 142 |
return RValue::get(call->getResult(0)); |
| 143 |
} |
| 144 |
|
| 145 |
template <class Operation> |
| 146 |
static RValue emitUnaryFPBuiltin(CIRGenFunction &cgf, const CallExpr &e) { |
| 147 |
mlir::Value arg = cgf.emitScalarExpr(e.getArg(0)); |
| 148 |
auto call = |
| 149 |
Operation::create(cgf.getBuilder(), arg.getLoc(), arg.getType(), arg); |
| 150 |
return RValue::get(call->getResult(0)); |
| 151 |
} |
| 152 |
|
| 153 |
static RValue errorBuiltinNYI(CIRGenFunction &cgf, const CallExpr *e, |
| 154 |
unsigned builtinID) { |
| 155 |
|
| 156 |
if (cgf.getContext().BuiltinInfo.isLibFunction(builtinID)) { |
| 157 |
cgf.cgm.errorNYI( |
| 158 |
e->getSourceRange(), |
| 159 |
std::string("unimplemented X86 library function builtin call: ") + |
| 160 |
cgf.getContext().BuiltinInfo.getName(builtinID)); |
| 161 |
} else { |
| 162 |
cgf.cgm.errorNYI(e->getSourceRange(), |
| 163 |
std::string("unimplemented X86 builtin call: ") + |
| 164 |
cgf.getContext().BuiltinInfo.getName(builtinID)); |
| 165 |
} |
| 166 |
|
| 167 |
return cgf.getUndefRValue(e->getType()); |
| 168 |
} |
| 169 |
|
| 170 |
static RValue emitBuiltinAlloca(CIRGenFunction &cgf, const CallExpr *e, |
| 171 |
unsigned builtinID) { |
| 172 |
assert(builtinID == Builtin::BI__builtin_alloca || |
| 173 |
builtinID == Builtin::BI__builtin_alloca_uninitialized || |
| 174 |
builtinID == Builtin::BIalloca || builtinID == Builtin::BI_alloca); |
| 175 |
|
| 176 |
// Get alloca size input |
| 177 |
mlir::Value size = cgf.emitScalarExpr(e->getArg(0)); |
| 178 |
|
| 179 |
// The alignment of the alloca should correspond to __BIGGEST_ALIGNMENT__. |
| 180 |
const TargetInfo &ti = cgf.getContext().getTargetInfo(); |
| 181 |
const CharUnits suitableAlignmentInBytes = |
| 182 |
cgf.getContext().toCharUnitsFromBits(ti.getSuitableAlign()); |
| 183 |
|
| 184 |
// Emit the alloca op with type `u8 *` to match the semantics of |
| 185 |
// `llvm.alloca`. We later bitcast the type to `void *` to match the |
| 186 |
// semantics of C/C++ |
| 187 |
// FIXME(cir): It may make sense to allow AllocaOp of type `u8` to return a |
| 188 |
// pointer of type `void *`. This will require a change to the allocaOp |
| 189 |
// verifier. |
| 190 |
CIRGenBuilderTy &builder = cgf.getBuilder(); |
| 191 |
mlir::Value allocaAddr = builder.createAlloca( |
| 192 |
cgf.getLoc(e->getSourceRange()), builder.getUInt8PtrTy(), |
| 193 |
builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size); |
| 194 |
|
| 195 |
// Initialize the allocated buffer if required. |
| 196 |
if (builtinID != Builtin::BI__builtin_alloca_uninitialized) { |
| 197 |
// Initialize the alloca with the given size and alignment according to |
| 198 |
// the lang opts. Only the trivial non-initialization is supported for |
| 199 |
// now. |
| 200 |
|
| 201 |
switch (cgf.getLangOpts().getTrivialAutoVarInit()) { |
| 202 |
case LangOptions::TrivialAutoVarInitKind::Uninitialized: |
| 203 |
// Nothing to initialize. |
| 204 |
break; |
| 205 |
case LangOptions::TrivialAutoVarInitKind::Zero: |
| 206 |
case LangOptions::TrivialAutoVarInitKind::Pattern: |
| 207 |
cgf.cgm.errorNYI("trivial auto var init"); |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// An alloca will always return a pointer to the alloca (stack) address |
| 213 |
// space. This address space need not be the same as the AST / Language |
| 214 |
// default (e.g. in C / C++ auto vars are in the generic address space). At |
| 215 |
// the AST level this is handled within CreateTempAlloca et al., but for the |
| 216 |
// builtin / dynamic alloca we have to handle it here. |
| 217 |
|
| 218 |
if (!cir::isMatchingAddressSpace( |
| 219 |
cgf.getCIRAllocaAddressSpace(), |
| 220 |
e->getType()->getPointeeType().getAddressSpace())) { |
| 221 |
cgf.cgm.errorNYI(e->getSourceRange(), |
| 222 |
"Non-default address space for alloca"); |
| 223 |
} |
| 224 |
|
| 225 |
// Bitcast the alloca to the expected type. |
| 226 |
return RValue::get(builder.createBitcast( |
| 227 |
allocaAddr, builder.getVoidPtrTy(cgf.getCIRAllocaAddressSpace()))); |
| 228 |
} |
| 229 |
|
| 230 |
static bool shouldCIREmitFPMathIntrinsic(CIRGenFunction &cgf, const CallExpr *e, |
| 231 |
unsigned builtinID) { |
| 232 |
std::optional<bool> errnoOverriden; |
| 233 |
// ErrnoOverriden is true if math-errno is overriden via the |
| 234 |
// '#pragma float_control(precise, on)'. This pragma disables fast-math, |
| 235 |
// which implies math-errno. |
| 236 |
if (e->hasStoredFPFeatures()) { |
| 237 |
FPOptionsOverride op = e->getFPFeatures(); |
| 238 |
if (op.hasMathErrnoOverride()) |
| 239 |
errnoOverriden = op.getMathErrnoOverride(); |
| 240 |
} |
| 241 |
// True if 'attribute__((optnone))' is used. This attribute overrides |
| 242 |
// fast-math which implies math-errno. |
| 243 |
bool optNone = |
| 244 |
cgf.curFuncDecl && cgf.curFuncDecl->hasAttr<OptimizeNoneAttr>(); |
| 245 |
bool isOptimizationEnabled = cgf.cgm.getCodeGenOpts().OptimizationLevel != 0; |
| 246 |
bool generateFPMathIntrinsics = |
| 247 |
cgf.getContext().BuiltinInfo.shouldGenerateFPMathIntrinsic( |
| 248 |
builtinID, cgf.cgm.getTriple(), errnoOverriden, |
| 249 |
cgf.getLangOpts().MathErrno, optNone, isOptimizationEnabled); |
| 250 |
return generateFPMathIntrinsics; |
| 251 |
} |
| 252 |
|
| 253 |
static RValue tryEmitFPMathIntrinsic(CIRGenFunction &cgf, const CallExpr *e, |
| 254 |
unsigned builtinID) { |
| 255 |
assert(!cir::MissingFeatures::fastMathFlags()); |
| 256 |
switch (builtinID) { |
| 257 |
case Builtin::BIacos: |
| 258 |
case Builtin::BIacosf: |
| 259 |
case Builtin::BIacosl: |
| 260 |
case Builtin::BI__builtin_acos: |
| 261 |
case Builtin::BI__builtin_acosf: |
| 262 |
case Builtin::BI__builtin_acosf16: |
| 263 |
case Builtin::BI__builtin_acosl: |
| 264 |
case Builtin::BI__builtin_acosf128: |
| 265 |
case Builtin::BI__builtin_elementwise_acos: |
| 266 |
case Builtin::BIasin: |
| 267 |
case Builtin::BIasinf: |
| 268 |
case Builtin::BIasinl: |
| 269 |
case Builtin::BI__builtin_asin: |
| 270 |
case Builtin::BI__builtin_asinf: |
| 271 |
case Builtin::BI__builtin_asinf16: |
| 272 |
case Builtin::BI__builtin_asinl: |
| 273 |
case Builtin::BI__builtin_asinf128: |
| 274 |
case Builtin::BI__builtin_elementwise_asin: |
| 275 |
case Builtin::BIatan: |
| 276 |
case Builtin::BIatanf: |
| 277 |
case Builtin::BIatanl: |
| 278 |
case Builtin::BI__builtin_atan: |
| 279 |
case Builtin::BI__builtin_atanf: |
| 280 |
case Builtin::BI__builtin_atanf16: |
| 281 |
case Builtin::BI__builtin_atanl: |
| 282 |
case Builtin::BI__builtin_atanf128: |
| 283 |
case Builtin::BI__builtin_elementwise_atan: |
| 284 |
case Builtin::BIatan2: |
| 285 |
case Builtin::BIatan2f: |
| 286 |
case Builtin::BIatan2l: |
| 287 |
case Builtin::BI__builtin_atan2: |
| 288 |
case Builtin::BI__builtin_atan2f: |
| 289 |
case Builtin::BI__builtin_atan2f16: |
| 290 |
case Builtin::BI__builtin_atan2l: |
| 291 |
case Builtin::BI__builtin_atan2f128: |
| 292 |
case Builtin::BI__builtin_elementwise_atan2: |
| 293 |
return RValue::getIgnored(); |
| 294 |
case Builtin::BIceil: |
| 295 |
case Builtin::BIceilf: |
| 296 |
case Builtin::BIceill: |
| 297 |
case Builtin::BI__builtin_ceil: |
| 298 |
case Builtin::BI__builtin_ceilf: |
| 299 |
case Builtin::BI__builtin_ceilf16: |
| 300 |
case Builtin::BI__builtin_ceill: |
| 301 |
case Builtin::BI__builtin_ceilf128: |
| 302 |
return emitUnaryMaybeConstrainedFPBuiltin<cir::CeilOp>(cgf, *e); |
| 303 |
case Builtin::BI__builtin_elementwise_ceil: |
| 304 |
case Builtin::BIcopysign: |
| 305 |
case Builtin::BIcopysignf: |
| 306 |
case Builtin::BIcopysignl: |
| 307 |
case Builtin::BI__builtin_copysign: |
| 308 |
case Builtin::BI__builtin_copysignf: |
| 309 |
case Builtin::BI__builtin_copysignf16: |
| 310 |
case Builtin::BI__builtin_copysignl: |
| 311 |
case Builtin::BI__builtin_copysignf128: |
| 312 |
return RValue::getIgnored(); |
| 313 |
case Builtin::BIcos: |
| 314 |
case Builtin::BIcosf: |
| 315 |
case Builtin::BIcosl: |
| 316 |
case Builtin::BI__builtin_cos: |
| 317 |
case Builtin::BI__builtin_cosf: |
| 318 |
case Builtin::BI__builtin_cosf16: |
| 319 |
case Builtin::BI__builtin_cosl: |
| 320 |
case Builtin::BI__builtin_cosf128: |
| 321 |
return emitUnaryMaybeConstrainedFPBuiltin<cir::CosOp>(cgf, *e); |
| 322 |
case Builtin::BI__builtin_elementwise_cos: |
| 323 |
case Builtin::BIcosh: |
| 324 |
case Builtin::BIcoshf: |
| 325 |
case Builtin::BIcoshl: |
| 326 |
case Builtin::BI__builtin_cosh: |
| 327 |
case Builtin::BI__builtin_coshf: |
| 328 |
case Builtin::BI__builtin_coshf16: |
| 329 |
case Builtin::BI__builtin_coshl: |
| 330 |
case Builtin::BI__builtin_coshf128: |
| 331 |
case Builtin::BI__builtin_elementwise_cosh: |
| 332 |
return RValue::getIgnored(); |
| 333 |
case Builtin::BIexp: |
| 334 |
case Builtin::BIexpf: |
| 335 |
case Builtin::BIexpl: |
| 336 |
case Builtin::BI__builtin_exp: |
| 337 |
case Builtin::BI__builtin_expf: |
| 338 |
case Builtin::BI__builtin_expf16: |
| 339 |
case Builtin::BI__builtin_expl: |
| 340 |
case Builtin::BI__builtin_expf128: |
| 341 |
return emitUnaryMaybeConstrainedFPBuiltin<cir::ExpOp>(cgf, *e); |
| 342 |
case Builtin::BI__builtin_elementwise_exp: |
| 343 |
return RValue::getIgnored(); |
| 344 |
case Builtin::BIexp2: |
| 345 |
case Builtin::BIexp2f: |
| 346 |
case Builtin::BIexp2l: |
| 347 |
case Builtin::BI__builtin_exp2: |
| 348 |
case Builtin::BI__builtin_exp2f: |
| 349 |
case Builtin::BI__builtin_exp2f16: |
| 350 |
case Builtin::BI__builtin_exp2l: |
| 351 |
case Builtin::BI__builtin_exp2f128: |
| 352 |
return emitUnaryMaybeConstrainedFPBuiltin<cir::Exp2Op>(cgf, *e); |
| 353 |
case Builtin::BI__builtin_elementwise_exp2: |
| 354 |
case Builtin::BI__builtin_exp10: |
| 355 |
case Builtin::BI__builtin_exp10f: |
| 356 |
case Builtin::BI__builtin_exp10f16: |
| 357 |
case Builtin::BI__builtin_exp10l: |
| 358 |
case Builtin::BI__builtin_exp10f128: |
| 359 |
case Builtin::BI__builtin_elementwise_exp10: |
| 360 |
return RValue::getIgnored(); |
| 361 |
case Builtin::BIfabs: |
| 362 |
case Builtin::BIfabsf: |
| 363 |
case Builtin::BIfabsl: |
| 364 |
case Builtin::BI__builtin_fabs: |
| 365 |
case Builtin::BI__builtin_fabsf: |
| 366 |
case Builtin::BI__builtin_fabsf16: |
| 367 |
case Builtin::BI__builtin_fabsl: |
| 368 |
case Builtin::BI__builtin_fabsf128: |
| 369 |
return emitUnaryMaybeConstrainedFPBuiltin<cir::FAbsOp>(cgf, *e); |
| 370 |
case Builtin::BIfloor: |
| 371 |
case Builtin::BIfloorf: |
| 372 |
case Builtin::BIfloorl: |
| 373 |
case Builtin::BI__builtin_floor: |
| 374 |
case Builtin::BI__builtin_floorf: |
| 375 |
case Builtin::BI__builtin_floorf16: |
| 376 |
case Builtin::BI__builtin_floorl: |
| 377 |
case Builtin::BI__builtin_floorf128: |
| 378 |
return emitUnaryMaybeConstrainedFPBuiltin<cir::FloorOp>(cgf, *e); |
| 379 |
case Builtin::BI__builtin_elementwise_floor: |
| 380 |
case Builtin::BIfma: |
| 381 |
case Builtin::BIfmaf: |
| 382 |
case Builtin::BIfmal: |
| 383 |
case Builtin::BI__builtin_fma: |
| 384 |
case Builtin::BI__builtin_fmaf: |
| 385 |
case Builtin::BI__builtin_fmaf16: |
| 386 |
case Builtin::BI__builtin_fmal: |
| 387 |
case Builtin::BI__builtin_fmaf128: |
| 388 |
case Builtin::BI__builtin_elementwise_fma: |
| 389 |
case Builtin::BIfmax: |
| 390 |
case Builtin::BIfmaxf: |
| 391 |
case Builtin::BIfmaxl: |
| 392 |
case Builtin::BI__builtin_fmax: |
| 393 |
case Builtin::BI__builtin_fmaxf: |
| 394 |
case Builtin::BI__builtin_fmaxf16: |
| 395 |
case Builtin::BI__builtin_fmaxl: |
| 396 |
case Builtin::BI__builtin_fmaxf128: |
| 397 |
case Builtin::BIfmin: |
| 398 |
case Builtin::BIfminf: |
| 399 |
case Builtin::BIfminl: |
| 400 |
case Builtin::BI__builtin_fmin: |
| 401 |
case Builtin::BI__builtin_fminf: |
| 402 |
case Builtin::BI__builtin_fminf16: |
| 403 |
case Builtin::BI__builtin_fminl: |
| 404 |
case Builtin::BI__builtin_fminf128: |
| 405 |
case Builtin::BIfmaximum_num: |
| 406 |
case Builtin::BIfmaximum_numf: |
| 407 |
case Builtin::BIfmaximum_numl: |
| 408 |
case Builtin::BI__builtin_fmaximum_num: |
| 409 |
case Builtin::BI__builtin_fmaximum_numf: |
| 410 |
case Builtin::BI__builtin_fmaximum_numf16: |
| 411 |
case Builtin::BI__builtin_fmaximum_numl: |
| 412 |
case Builtin::BI__builtin_fmaximum_numf128: |
| 413 |
case Builtin::BIfminimum_num: |
| 414 |
case Builtin::BIfminimum_numf: |
| 415 |
case Builtin::BIfminimum_numl: |
| 416 |
case Builtin::BI__builtin_fminimum_num: |
| 417 |
case Builtin::BI__builtin_fminimum_numf: |
| 418 |
case Builtin::BI__builtin_fminimum_numf16: |
| 419 |
case Builtin::BI__builtin_fminimum_numl: |
| 420 |
case Builtin::BI__builtin_fminimum_numf128: |
| 421 |
case Builtin::BIfmod: |
| 422 |
case Builtin::BIfmodf: |
| 423 |
case Builtin::BIfmodl: |
| 424 |
case Builtin::BI__builtin_fmod: |
| 425 |
case Builtin::BI__builtin_fmodf: |
| 426 |
case Builtin::BI__builtin_fmodf16: |
| 427 |
case Builtin::BI__builtin_fmodl: |
| 428 |
case Builtin::BI__builtin_fmodf128: |
| 429 |
case Builtin::BI__builtin_elementwise_fmod: |
| 430 |
case Builtin::BIlog: |
| 431 |
case Builtin::BIlogf: |
| 432 |
case Builtin::BIlogl: |
| 433 |
case Builtin::BI__builtin_log: |
| 434 |
case Builtin::BI__builtin_logf: |
| 435 |
case Builtin::BI__builtin_logf16: |
| 436 |
case Builtin::BI__builtin_logl: |
| 437 |
case Builtin::BI__builtin_logf128: |
| 438 |
case Builtin::BI__builtin_elementwise_log: |
| 439 |
case Builtin::BIlog10: |
| 440 |
case Builtin::BIlog10f: |
| 441 |
case Builtin::BIlog10l: |
| 442 |
case Builtin::BI__builtin_log10: |
| 443 |
case Builtin::BI__builtin_log10f: |
| 444 |
case Builtin::BI__builtin_log10f16: |
| 445 |
case Builtin::BI__builtin_log10l: |
| 446 |
case Builtin::BI__builtin_log10f128: |
| 447 |
case Builtin::BI__builtin_elementwise_log10: |
| 448 |
case Builtin::BIlog2: |
| 449 |
case Builtin::BIlog2f: |
| 450 |
case Builtin::BIlog2l: |
| 451 |
case Builtin::BI__builtin_log2: |
| 452 |
case Builtin::BI__builtin_log2f: |
| 453 |
case Builtin::BI__builtin_log2f16: |
| 454 |
case Builtin::BI__builtin_log2l: |
| 455 |
case Builtin::BI__builtin_log2f128: |
| 456 |
case Builtin::BI__builtin_elementwise_log2: |
| 457 |
case Builtin::BInearbyint: |
| 458 |
case Builtin::BInearbyintf: |
| 459 |
case Builtin::BInearbyintl: |
| 460 |
case Builtin::BI__builtin_nearbyint: |
| 461 |
case Builtin::BI__builtin_nearbyintf: |
| 462 |
case Builtin::BI__builtin_nearbyintl: |
| 463 |
case Builtin::BI__builtin_nearbyintf128: |
| 464 |
case Builtin::BI__builtin_elementwise_nearbyint: |
| 465 |
case Builtin::BIpow: |
| 466 |
case Builtin::BIpowf: |
| 467 |
case Builtin::BIpowl: |
| 468 |
case Builtin::BI__builtin_pow: |
| 469 |
case Builtin::BI__builtin_powf: |
| 470 |
case Builtin::BI__builtin_powf16: |
| 471 |
case Builtin::BI__builtin_powl: |
| 472 |
case Builtin::BI__builtin_powf128: |
| 473 |
case Builtin::BI__builtin_elementwise_pow: |
| 474 |
case Builtin::BIrint: |
| 475 |
case Builtin::BIrintf: |
| 476 |
case Builtin::BIrintl: |
| 477 |
case Builtin::BI__builtin_rint: |
| 478 |
case Builtin::BI__builtin_rintf: |
| 479 |
case Builtin::BI__builtin_rintf16: |
| 480 |
case Builtin::BI__builtin_rintl: |
| 481 |
case Builtin::BI__builtin_rintf128: |
| 482 |
case Builtin::BI__builtin_elementwise_rint: |
| 483 |
case Builtin::BIround: |
| 484 |
case Builtin::BIroundf: |
| 485 |
case Builtin::BIroundl: |
| 486 |
case Builtin::BI__builtin_round: |
| 487 |
case Builtin::BI__builtin_roundf: |
| 488 |
case Builtin::BI__builtin_roundf16: |
| 489 |
case Builtin::BI__builtin_roundl: |
| 490 |
case Builtin::BI__builtin_roundf128: |
| 491 |
case Builtin::BI__builtin_elementwise_round: |
| 492 |
case Builtin::BIroundeven: |
| 493 |
case Builtin::BIroundevenf: |
| 494 |
case Builtin::BIroundevenl: |
| 495 |
case Builtin::BI__builtin_roundeven: |
| 496 |
case Builtin::BI__builtin_roundevenf: |
| 497 |
case Builtin::BI__builtin_roundevenf16: |
| 498 |
case Builtin::BI__builtin_roundevenl: |
| 499 |
case Builtin::BI__builtin_roundevenf128: |
| 500 |
case Builtin::BI__builtin_elementwise_roundeven: |
| 501 |
case Builtin::BIsin: |
| 502 |
case Builtin::BIsinf: |
| 503 |
case Builtin::BIsinl: |
| 504 |
case Builtin::BI__builtin_sin: |
| 505 |
case Builtin::BI__builtin_sinf: |
| 506 |
case Builtin::BI__builtin_sinf16: |
| 507 |
case Builtin::BI__builtin_sinl: |
| 508 |
case Builtin::BI__builtin_sinf128: |
| 509 |
case Builtin::BI__builtin_elementwise_sin: |
| 510 |
case Builtin::BIsinh: |
| 511 |
case Builtin::BIsinhf: |
| 512 |
case Builtin::BIsinhl: |
| 513 |
case Builtin::BI__builtin_sinh: |
| 514 |
case Builtin::BI__builtin_sinhf: |
| 515 |
case Builtin::BI__builtin_sinhf16: |
| 516 |
case Builtin::BI__builtin_sinhl: |
| 517 |
case Builtin::BI__builtin_sinhf128: |
| 518 |
case Builtin::BI__builtin_elementwise_sinh: |
| 519 |
case Builtin::BI__builtin_sincospi: |
| 520 |
case Builtin::BI__builtin_sincospif: |
| 521 |
case Builtin::BI__builtin_sincospil: |
| 522 |
case Builtin::BIsincos: |
| 523 |
case Builtin::BIsincosf: |
| 524 |
case Builtin::BIsincosl: |
| 525 |
case Builtin::BI__builtin_sincos: |
| 526 |
case Builtin::BI__builtin_sincosf: |
| 527 |
case Builtin::BI__builtin_sincosf16: |
| 528 |
case Builtin::BI__builtin_sincosl: |
| 529 |
case Builtin::BI__builtin_sincosf128: |
| 530 |
case Builtin::BIsqrt: |
| 531 |
case Builtin::BIsqrtf: |
| 532 |
case Builtin::BIsqrtl: |
| 533 |
case Builtin::BI__builtin_sqrt: |
| 534 |
case Builtin::BI__builtin_sqrtf: |
| 535 |
case Builtin::BI__builtin_sqrtf16: |
| 536 |
case Builtin::BI__builtin_sqrtl: |
| 537 |
case Builtin::BI__builtin_sqrtf128: |
| 538 |
case Builtin::BI__builtin_elementwise_sqrt: |
| 539 |
case Builtin::BItan: |
| 540 |
case Builtin::BItanf: |
| 541 |
case Builtin::BItanl: |
| 542 |
case Builtin::BI__builtin_tan: |
| 543 |
case Builtin::BI__builtin_tanf: |
| 544 |
case Builtin::BI__builtin_tanf16: |
| 545 |
case Builtin::BI__builtin_tanl: |
| 546 |
case Builtin::BI__builtin_tanf128: |
| 547 |
case Builtin::BI__builtin_elementwise_tan: |
| 548 |
case Builtin::BItanh: |
| 549 |
case Builtin::BItanhf: |
| 550 |
case Builtin::BItanhl: |
| 551 |
case Builtin::BI__builtin_tanh: |
| 552 |
case Builtin::BI__builtin_tanhf: |
| 553 |
case Builtin::BI__builtin_tanhf16: |
| 554 |
case Builtin::BI__builtin_tanhl: |
| 555 |
case Builtin::BI__builtin_tanhf128: |
| 556 |
case Builtin::BI__builtin_elementwise_tanh: |
| 557 |
case Builtin::BItrunc: |
| 558 |
case Builtin::BItruncf: |
| 559 |
case Builtin::BItruncl: |
| 560 |
case Builtin::BI__builtin_trunc: |
| 561 |
case Builtin::BI__builtin_truncf: |
| 562 |
case Builtin::BI__builtin_truncf16: |
| 563 |
case Builtin::BI__builtin_truncl: |
| 564 |
case Builtin::BI__builtin_truncf128: |
| 565 |
case Builtin::BI__builtin_elementwise_trunc: |
| 566 |
case Builtin::BIlround: |
| 567 |
case Builtin::BIlroundf: |
| 568 |
case Builtin::BIlroundl: |
| 569 |
case Builtin::BI__builtin_lround: |
| 570 |
case Builtin::BI__builtin_lroundf: |
| 571 |
case Builtin::BI__builtin_lroundl: |
| 572 |
case Builtin::BI__builtin_lroundf128: |
| 573 |
case Builtin::BIllround: |
| 574 |
case Builtin::BIllroundf: |
| 575 |
case Builtin::BIllroundl: |
| 576 |
case Builtin::BI__builtin_llround: |
| 577 |
case Builtin::BI__builtin_llroundf: |
| 578 |
case Builtin::BI__builtin_llroundl: |
| 579 |
case Builtin::BI__builtin_llroundf128: |
| 580 |
case Builtin::BIlrint: |
| 581 |
case Builtin::BIlrintf: |
| 582 |
case Builtin::BIlrintl: |
| 583 |
case Builtin::BI__builtin_lrint: |
| 584 |
case Builtin::BI__builtin_lrintf: |
| 585 |
case Builtin::BI__builtin_lrintl: |
| 586 |
case Builtin::BI__builtin_lrintf128: |
| 587 |
case Builtin::BIllrint: |
| 588 |
case Builtin::BIllrintf: |
| 589 |
case Builtin::BIllrintl: |
| 590 |
case Builtin::BI__builtin_llrint: |
| 591 |
case Builtin::BI__builtin_llrintf: |
| 592 |
case Builtin::BI__builtin_llrintl: |
| 593 |
case Builtin::BI__builtin_llrintf128: |
| 594 |
case Builtin::BI__builtin_ldexp: |
| 595 |
case Builtin::BI__builtin_ldexpf: |
| 596 |
case Builtin::BI__builtin_ldexpl: |
| 597 |
case Builtin::BI__builtin_ldexpf16: |
| 598 |
case Builtin::BI__builtin_ldexpf128: |
| 599 |
case Builtin::BI__builtin_elementwise_ldexp: |
| 600 |
default: |
| 601 |
break; |
| 602 |
} |
| 603 |
|
| 604 |
return RValue::getIgnored(); |
| 605 |
} |
| 606 |
|
| 607 |
RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, |
| 608 |
const CallExpr *e, |
| 609 |
ReturnValueSlot returnValue) { |
| 610 |
mlir::Location loc = getLoc(e->getSourceRange()); |
| 611 |
|
| 612 |
// See if we can constant fold this builtin. If so, don't emit it at all. |
| 613 |
// TODO: Extend this handling to all builtin calls that we can constant-fold. |
| 614 |
Expr::EvalResult result; |
| 615 |
if (e->isPRValue() && e->EvaluateAsRValue(result, cgm.getASTContext()) && |
| 616 |
!result.hasSideEffects()) { |
| 617 |
if (result.Val.isInt()) |
| 618 |
return RValue::get(builder.getConstInt(loc, result.Val.getInt())); |
| 619 |
if (result.Val.isFloat()) { |
| 620 |
// Note: we are using result type of CallExpr to determine the type of |
| 621 |
// the constant. Classic codegen uses the result value to determine the |
| 622 |
// type. We feel it should be Ok to use expression type because it is |
| 623 |
// hard to imagine a builtin function evaluates to a value that |
| 624 |
// over/underflows its own defined type. |
| 625 |
mlir::Type type = convertType(e->getType()); |
| 626 |
return RValue::get(builder.getConstFP(loc, type, result.Val.getFloat())); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
const FunctionDecl *fd = gd.getDecl()->getAsFunction(); |
| 631 |
|
| 632 |
assert(!cir::MissingFeatures::builtinCallF128()); |
| 633 |
|
| 634 |
// If the builtin has been declared explicitly with an assembler label, |
| 635 |
// disable the specialized emitting below. Ideally we should communicate the |
| 636 |
// rename in IR, or at least avoid generating the intrinsic calls that are |
| 637 |
// likely to get lowered to the renamed library functions. |
| 638 |
unsigned builtinIDIfNoAsmLabel = fd->hasAttr<AsmLabelAttr>() ? 0 : builtinID; |
| 639 |
|
| 640 |
bool generateFPMathIntrinsics = |
| 641 |
shouldCIREmitFPMathIntrinsic(*this, e, builtinID); |
| 642 |
|
| 643 |
if (generateFPMathIntrinsics) { |
| 644 |
// Try to match the builtinID with a floating point math builtin. |
| 645 |
RValue rv = tryEmitFPMathIntrinsic(*this, e, builtinIDIfNoAsmLabel); |
| 646 |
|
| 647 |
// Return the result directly if a math intrinsic was generated. |
| 648 |
if (!rv.isIgnored()) { |
| 649 |
return rv; |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
assert(!cir::MissingFeatures::builtinCall()); |
| 654 |
|
| 655 |
switch (builtinIDIfNoAsmLabel) { |
| 656 |
default: |
| 657 |
break; |
| 658 |
|
| 659 |
// C stdarg builtins. |
| 660 |
case Builtin::BI__builtin_stdarg_start: |
| 661 |
case Builtin::BI__builtin_va_start: |
| 662 |
case Builtin::BI__va_start: { |
| 663 |
mlir::Value vaList = builtinID == Builtin::BI__va_start |
| 664 |
? emitScalarExpr(e->getArg(0)) |
| 665 |
: emitVAListRef(e->getArg(0)).getPointer(); |
| 666 |
mlir::Value count = emitScalarExpr(e->getArg(1)); |
| 667 |
emitVAStart(vaList, count); |
| 668 |
return {}; |
| 669 |
} |
| 670 |
|
| 671 |
case Builtin::BI__builtin_va_end: |
| 672 |
emitVAEnd(emitVAListRef(e->getArg(0)).getPointer()); |
| 673 |
return {}; |
| 674 |
case Builtin::BI__builtin_va_copy: { |
| 675 |
mlir::Value dstPtr = emitVAListRef(e->getArg(0)).getPointer(); |
| 676 |
mlir::Value srcPtr = emitVAListRef(e->getArg(1)).getPointer(); |
| 677 |
cir::VACopyOp::create(builder, dstPtr.getLoc(), dstPtr, srcPtr); |
| 678 |
return {}; |
| 679 |
} |
| 680 |
case Builtin::BI__assume: |
| 681 |
case Builtin::BI__builtin_assume: { |
| 682 |
if (e->getArg(0)->HasSideEffects(getContext())) |
| 683 |
return RValue::get(nullptr); |
| 684 |
|
| 685 |
mlir::Value argValue = emitCheckedArgForAssume(e->getArg(0)); |
| 686 |
cir::AssumeOp::create(builder, loc, argValue); |
| 687 |
return RValue::get(nullptr); |
| 688 |
} |
| 689 |
|
| 690 |
case Builtin::BI__builtin_assume_separate_storage: { |
| 691 |
mlir::Value value0 = emitScalarExpr(e->getArg(0)); |
| 692 |
mlir::Value value1 = emitScalarExpr(e->getArg(1)); |
| 693 |
cir::AssumeSepStorageOp::create(builder, loc, value0, value1); |
| 694 |
return RValue::get(nullptr); |
| 695 |
} |
| 696 |
|
| 697 |
case Builtin::BI__builtin_assume_aligned: { |
| 698 |
const Expr *ptrExpr = e->getArg(0); |
| 699 |
mlir::Value ptrValue = emitScalarExpr(ptrExpr); |
| 700 |
mlir::Value offsetValue = |
| 701 |
(e->getNumArgs() > 2) ? emitScalarExpr(e->getArg(2)) : nullptr; |
| 702 |
|
| 703 |
std::optional<llvm::APSInt> alignment = |
| 704 |
e->getArg(1)->getIntegerConstantExpr(getContext()); |
| 705 |
assert(alignment.has_value() && |
| 706 |
"the second argument to __builtin_assume_aligned must be an " |
| 707 |
"integral constant expression"); |
| 708 |
|
| 709 |
mlir::Value result = |
| 710 |
emitAlignmentAssumption(ptrValue, ptrExpr, ptrExpr->getExprLoc(), |
| 711 |
alignment->getSExtValue(), offsetValue); |
| 712 |
return RValue::get(result); |
| 713 |
} |
| 714 |
|
| 715 |
case Builtin::BI__builtin_complex: { |
| 716 |
mlir::Value real = emitScalarExpr(e->getArg(0)); |
| 717 |
mlir::Value imag = emitScalarExpr(e->getArg(1)); |
| 718 |
mlir::Value complex = builder.createComplexCreate(loc, real, imag); |
| 719 |
return RValue::getComplex(complex); |
| 720 |
} |
| 721 |
|
| 722 |
case Builtin::BI__builtin_creal: |
| 723 |
case Builtin::BI__builtin_crealf: |
| 724 |
case Builtin::BI__builtin_creall: |
| 725 |
case Builtin::BIcreal: |
| 726 |
case Builtin::BIcrealf: |
| 727 |
case Builtin::BIcreall: { |
| 728 |
mlir::Value complex = emitComplexExpr(e->getArg(0)); |
| 729 |
mlir::Value real = builder.createComplexReal(loc, complex); |
| 730 |
return RValue::get(real); |
| 731 |
} |
| 732 |
|
| 733 |
case Builtin::BI__builtin_cimag: |
| 734 |
case Builtin::BI__builtin_cimagf: |
| 735 |
case Builtin::BI__builtin_cimagl: |
| 736 |
case Builtin::BIcimag: |
| 737 |
case Builtin::BIcimagf: |
| 738 |
case Builtin::BIcimagl: { |
| 739 |
mlir::Value complex = emitComplexExpr(e->getArg(0)); |
| 740 |
mlir::Value imag = builder.createComplexImag(loc, complex); |
| 741 |
return RValue::get(imag); |
| 742 |
} |
| 743 |
|
| 744 |
case Builtin::BI__builtin_conj: |
| 745 |
case Builtin::BI__builtin_conjf: |
| 746 |
case Builtin::BI__builtin_conjl: |
| 747 |
case Builtin::BIconj: |
| 748 |
case Builtin::BIconjf: |
| 749 |
case Builtin::BIconjl: { |
| 750 |
mlir::Value complex = emitComplexExpr(e->getArg(0)); |
| 751 |
mlir::Value conj = builder.createUnaryOp(getLoc(e->getExprLoc()), |
| 752 |
cir::UnaryOpKind::Not, complex); |
| 753 |
return RValue::getComplex(conj); |
| 754 |
} |
| 755 |
|
| 756 |
case Builtin::BI__builtin_clrsb: |
| 757 |
case Builtin::BI__builtin_clrsbl: |
| 758 |
case Builtin::BI__builtin_clrsbll: |
| 759 |
return emitBuiltinBitOp<cir::BitClrsbOp>(*this, e); |
| 760 |
|
| 761 |
case Builtin::BI__builtin_ctzs: |
| 762 |
case Builtin::BI__builtin_ctz: |
| 763 |
case Builtin::BI__builtin_ctzl: |
| 764 |
case Builtin::BI__builtin_ctzll: |
| 765 |
case Builtin::BI__builtin_ctzg: |
| 766 |
assert(!cir::MissingFeatures::builtinCheckKind()); |
| 767 |
return emitBuiltinBitOp<cir::BitCtzOp>(*this, e, /*poisonZero=*/true); |
| 768 |
|
| 769 |
case Builtin::BI__builtin_clzs: |
| 770 |
case Builtin::BI__builtin_clz: |
| 771 |
case Builtin::BI__builtin_clzl: |
| 772 |
case Builtin::BI__builtin_clzll: |
| 773 |
case Builtin::BI__builtin_clzg: |
| 774 |
assert(!cir::MissingFeatures::builtinCheckKind()); |
| 775 |
return emitBuiltinBitOp<cir::BitClzOp>(*this, e, /*poisonZero=*/true); |
| 776 |
|
| 777 |
case Builtin::BI__builtin_ffs: |
| 778 |
case Builtin::BI__builtin_ffsl: |
| 779 |
case Builtin::BI__builtin_ffsll: |
| 780 |
return emitBuiltinBitOp<cir::BitFfsOp>(*this, e); |
| 781 |
|
| 782 |
case Builtin::BI__builtin_parity: |
| 783 |
case Builtin::BI__builtin_parityl: |
| 784 |
case Builtin::BI__builtin_parityll: |
| 785 |
return emitBuiltinBitOp<cir::BitParityOp>(*this, e); |
| 786 |
|
| 787 |
case Builtin::BI__lzcnt16: |
| 788 |
case Builtin::BI__lzcnt: |
| 789 |
case Builtin::BI__lzcnt64: |
| 790 |
assert(!cir::MissingFeatures::builtinCheckKind()); |
| 791 |
return emitBuiltinBitOp<cir::BitClzOp>(*this, e, /*poisonZero=*/false); |
| 792 |
|
| 793 |
case Builtin::BI__popcnt16: |
| 794 |
case Builtin::BI__popcnt: |
| 795 |
case Builtin::BI__popcnt64: |
| 796 |
case Builtin::BI__builtin_popcount: |
| 797 |
case Builtin::BI__builtin_popcountl: |
| 798 |
case Builtin::BI__builtin_popcountll: |
| 799 |
case Builtin::BI__builtin_popcountg: |
| 800 |
return emitBuiltinBitOp<cir::BitPopcountOp>(*this, e); |
| 801 |
|
| 802 |
case Builtin::BI__builtin_expect: |
| 803 |
case Builtin::BI__builtin_expect_with_probability: { |
| 804 |
mlir::Value argValue = emitScalarExpr(e->getArg(0)); |
| 805 |
mlir::Value expectedValue = emitScalarExpr(e->getArg(1)); |
| 806 |
|
| 807 |
mlir::FloatAttr probAttr; |
| 808 |
if (builtinIDIfNoAsmLabel == Builtin::BI__builtin_expect_with_probability) { |
| 809 |
llvm::APFloat probability(0.0); |
| 810 |
const Expr *probArg = e->getArg(2); |
| 811 |
[[maybe_unused]] bool evalSucceeded = |
| 812 |
probArg->EvaluateAsFloat(probability, cgm.getASTContext()); |
| 813 |
assert(evalSucceeded && |
| 814 |
"probability should be able to evaluate as float"); |
| 815 |
bool loseInfo = false; // ignored |
| 816 |
probability.convert(llvm::APFloat::IEEEdouble(), |
| 817 |
llvm::RoundingMode::Dynamic, &loseInfo); |
| 818 |
probAttr = mlir::FloatAttr::get(mlir::Float64Type::get(&getMLIRContext()), |
| 819 |
probability); |
| 820 |
} |
| 821 |
|
| 822 |
auto result = cir::ExpectOp::create(builder, loc, argValue.getType(), |
| 823 |
argValue, expectedValue, probAttr); |
| 824 |
return RValue::get(result); |
| 825 |
} |
| 826 |
|
| 827 |
case Builtin::BI__builtin_bswap16: |
| 828 |
case Builtin::BI__builtin_bswap32: |
| 829 |
case Builtin::BI__builtin_bswap64: |
| 830 |
case Builtin::BI_byteswap_ushort: |
| 831 |
case Builtin::BI_byteswap_ulong: |
| 832 |
case Builtin::BI_byteswap_uint64: { |
| 833 |
mlir::Value arg = emitScalarExpr(e->getArg(0)); |
| 834 |
return RValue::get(cir::ByteSwapOp::create(builder, loc, arg)); |
| 835 |
} |
| 836 |
|
| 837 |
case Builtin::BI__builtin_bitreverse8: |
| 838 |
case Builtin::BI__builtin_bitreverse16: |
| 839 |
case Builtin::BI__builtin_bitreverse32: |
| 840 |
case Builtin::BI__builtin_bitreverse64: { |
| 841 |
mlir::Value arg = emitScalarExpr(e->getArg(0)); |
| 842 |
return RValue::get(cir::BitReverseOp::create(builder, loc, arg)); |
| 843 |
} |
| 844 |
|
| 845 |
case Builtin::BI__builtin_rotateleft8: |
| 846 |
case Builtin::BI__builtin_rotateleft16: |
| 847 |
case Builtin::BI__builtin_rotateleft32: |
| 848 |
case Builtin::BI__builtin_rotateleft64: |
| 849 |
return emitRotate(e, /*isRotateLeft=*/true); |
| 850 |
|
| 851 |
case Builtin::BI__builtin_rotateright8: |
| 852 |
case Builtin::BI__builtin_rotateright16: |
| 853 |
case Builtin::BI__builtin_rotateright32: |
| 854 |
case Builtin::BI__builtin_rotateright64: |
| 855 |
return emitRotate(e, /*isRotateLeft=*/false); |
| 856 |
|
| 857 |
case Builtin::BI__builtin_coro_id: |
| 858 |
case Builtin::BI__builtin_coro_promise: |
| 859 |
case Builtin::BI__builtin_coro_resume: |
| 860 |
case Builtin::BI__builtin_coro_noop: |
| 861 |
case Builtin::BI__builtin_coro_destroy: |
| 862 |
case Builtin::BI__builtin_coro_done: |
| 863 |
case Builtin::BI__builtin_coro_alloc: |
| 864 |
case Builtin::BI__builtin_coro_begin: |
| 865 |
case Builtin::BI__builtin_coro_end: |
| 866 |
case Builtin::BI__builtin_coro_suspend: |
| 867 |
case Builtin::BI__builtin_coro_align: |
| 868 |
cgm.errorNYI(e->getSourceRange(), "BI__builtin_coro_id like NYI"); |
| 869 |
return getUndefRValue(e->getType()); |
| 870 |
|
| 871 |
case Builtin::BI__builtin_coro_frame: { |
| 872 |
return emitCoroutineFrame(); |
| 873 |
} |
| 874 |
case Builtin::BI__builtin_coro_free: |
| 875 |
case Builtin::BI__builtin_coro_size: { |
| 876 |
GlobalDecl gd{fd}; |
| 877 |
mlir::Type ty = cgm.getTypes().getFunctionType( |
| 878 |
cgm.getTypes().arrangeGlobalDeclaration(gd)); |
| 879 |
const auto *nd = cast<NamedDecl>(gd.getDecl()); |
| 880 |
cir::FuncOp fnOp = |
| 881 |
cgm.getOrCreateCIRFunction(nd->getName(), ty, gd, /*ForVTable=*/false); |
| 882 |
fnOp.setBuiltin(true); |
| 883 |
return emitCall(e->getCallee()->getType(), CIRGenCallee::forDirect(fnOp), e, |
| 884 |
returnValue); |
| 885 |
} |
| 886 |
|
| 887 |
case Builtin::BI__builtin_constant_p: { |
| 888 |
mlir::Type resultType = convertType(e->getType()); |
| 889 |
|
| 890 |
const Expr *arg = e->getArg(0); |
| 891 |
QualType argType = arg->getType(); |
| 892 |
// FIXME: The allowance for Obj-C pointers and block pointers is historical |
| 893 |
// and likely a mistake. |
| 894 |
if (!argType->isIntegralOrEnumerationType() && !argType->isFloatingType() && |
| 895 |
!argType->isObjCObjectPointerType() && !argType->isBlockPointerType()) { |
| 896 |
// Per the GCC documentation, only numeric constants are recognized after |
| 897 |
// inlining. |
| 898 |
return RValue::get( |
| 899 |
builder.getConstInt(getLoc(e->getSourceRange()), |
| 900 |
mlir::cast<cir::IntType>(resultType), 0)); |
| 901 |
} |
| 902 |
|
| 903 |
if (arg->HasSideEffects(getContext())) { |
| 904 |
// The argument is unevaluated, so be conservative if it might have |
| 905 |
// side-effects. |
| 906 |
return RValue::get( |
| 907 |
builder.getConstInt(getLoc(e->getSourceRange()), |
| 908 |
mlir::cast<cir::IntType>(resultType), 0)); |
| 909 |
} |
| 910 |
|
| 911 |
mlir::Value argValue = emitScalarExpr(arg); |
| 912 |
if (argType->isObjCObjectPointerType()) { |
| 913 |
cgm.errorNYI(e->getSourceRange(), |
| 914 |
"__builtin_constant_p: Obj-C object pointer"); |
| 915 |
return {}; |
| 916 |
} |
| 917 |
argValue = builder.createBitcast(argValue, convertType(argType)); |
| 918 |
|
| 919 |
mlir::Value result = cir::IsConstantOp::create( |
| 920 |
builder, getLoc(e->getSourceRange()), argValue); |
| 921 |
// IsConstantOp returns a bool, but __builtin_constant_p returns an int. |
| 922 |
result = builder.createBoolToInt(result, resultType); |
| 923 |
return RValue::get(result); |
| 924 |
} |
| 925 |
case Builtin::BI__builtin_dynamic_object_size: |
| 926 |
case Builtin::BI__builtin_object_size: { |
| 927 |
unsigned type = |
| 928 |
e->getArg(1)->EvaluateKnownConstInt(getContext()).getZExtValue(); |
| 929 |
auto resType = mlir::cast<cir::IntType>(convertType(e->getType())); |
| 930 |
|
| 931 |
// We pass this builtin onto the optimizer so that it can figure out the |
| 932 |
// object size in more complex cases. |
| 933 |
bool isDynamic = builtinID == Builtin::BI__builtin_dynamic_object_size; |
| 934 |
return RValue::get(emitBuiltinObjectSize(e->getArg(0), type, resType, |
| 935 |
/*EmittedE=*/nullptr, isDynamic)); |
| 936 |
} |
| 937 |
|
| 938 |
case Builtin::BI__builtin_prefetch: { |
| 939 |
auto evaluateOperandAsInt = [&](const Expr *arg) { |
| 940 |
Expr::EvalResult res; |
| 941 |
[[maybe_unused]] bool evalSucceed = |
| 942 |
arg->EvaluateAsInt(res, cgm.getASTContext()); |
| 943 |
assert(evalSucceed && "expression should be able to evaluate as int"); |
| 944 |
return res.Val.getInt().getZExtValue(); |
| 945 |
}; |
| 946 |
|
| 947 |
bool isWrite = false; |
| 948 |
if (e->getNumArgs() > 1) |
| 949 |
isWrite = evaluateOperandAsInt(e->getArg(1)); |
| 950 |
|
| 951 |
int locality = 3; |
| 952 |
if (e->getNumArgs() > 2) |
| 953 |
locality = evaluateOperandAsInt(e->getArg(2)); |
| 954 |
|
| 955 |
mlir::Value address = emitScalarExpr(e->getArg(0)); |
| 956 |
cir::PrefetchOp::create(builder, loc, address, locality, isWrite); |
| 957 |
return RValue::get(nullptr); |
| 958 |
} |
| 959 |
case Builtin::BI__builtin_readcyclecounter: |
| 960 |
case Builtin::BI__builtin_readsteadycounter: |
| 961 |
case Builtin::BI__builtin___clear_cache: |
| 962 |
return errorBuiltinNYI(*this, e, builtinID); |
| 963 |
case Builtin::BI__builtin_trap: |
| 964 |
emitTrap(loc, /*createNewBlock=*/true); |
| 965 |
return RValue::getIgnored(); |
| 966 |
case Builtin::BI__builtin_verbose_trap: |
| 967 |
case Builtin::BI__debugbreak: |
| 968 |
return errorBuiltinNYI(*this, e, builtinID); |
| 969 |
case Builtin::BI__builtin_unreachable: |
| 970 |
emitUnreachable(e->getExprLoc(), /*createNewBlock=*/true); |
| 971 |
return RValue::getIgnored(); |
| 972 |
case Builtin::BI__builtin_powi: |
| 973 |
case Builtin::BI__builtin_powif: |
| 974 |
case Builtin::BI__builtin_powil: |
| 975 |
case Builtin::BI__builtin_frexpl: |
| 976 |
case Builtin::BI__builtin_frexp: |
| 977 |
case Builtin::BI__builtin_frexpf: |
| 978 |
case Builtin::BI__builtin_frexpf128: |
| 979 |
case Builtin::BI__builtin_frexpf16: |
| 980 |
case Builtin::BImodf: |
| 981 |
case Builtin::BImodff: |
| 982 |
case Builtin::BImodfl: |
| 983 |
case Builtin::BI__builtin_modf: |
| 984 |
case Builtin::BI__builtin_modff: |
| 985 |
case Builtin::BI__builtin_modfl: |
| 986 |
case Builtin::BI__builtin_isgreater: |
| 987 |
case Builtin::BI__builtin_isgreaterequal: |
| 988 |
case Builtin::BI__builtin_isless: |
| 989 |
case Builtin::BI__builtin_islessequal: |
| 990 |
case Builtin::BI__builtin_islessgreater: |
| 991 |
case Builtin::BI__builtin_isunordered: |
| 992 |
// From https://clang.llvm.org/docs/LanguageExtensions.html#builtin-isfpclass |
| 993 |
// |
| 994 |
// The `__builtin_isfpclass()` builtin is a generalization of functions |
| 995 |
// isnan, isinf, isfinite and some others defined by the C standard. It tests |
| 996 |
// if the floating-point value, specified by the first argument, falls into |
| 997 |
// any of data classes, specified by the second argument. |
| 998 |
case Builtin::BI__builtin_isnan: { |
| 999 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 1000 |
mlir::Value v = emitScalarExpr(e->getArg(0)); |
| 1001 |
assert(!cir::MissingFeatures::fpConstraints()); |
| 1002 |
mlir::Location loc = getLoc(e->getBeginLoc()); |
| 1003 |
return RValue::get(builder.createBoolToInt( |
| 1004 |
builder.createIsFPClass(loc, v, cir::FPClassTest::Nan), |
| 1005 |
convertType(e->getType()))); |
| 1006 |
} |
| 1007 |
|
| 1008 |
case Builtin::BI__builtin_issignaling: { |
| 1009 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 1010 |
mlir::Value v = emitScalarExpr(e->getArg(0)); |
| 1011 |
mlir::Location loc = getLoc(e->getBeginLoc()); |
| 1012 |
return RValue::get(builder.createBoolToInt( |
| 1013 |
builder.createIsFPClass(loc, v, cir::FPClassTest::SignalingNaN), |
| 1014 |
convertType(e->getType()))); |
| 1015 |
} |
| 1016 |
|
| 1017 |
case Builtin::BI__builtin_isinf: { |
| 1018 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 1019 |
mlir::Value v = emitScalarExpr(e->getArg(0)); |
| 1020 |
assert(!cir::MissingFeatures::fpConstraints()); |
| 1021 |
mlir::Location loc = getLoc(e->getBeginLoc()); |
| 1022 |
return RValue::get(builder.createBoolToInt( |
| 1023 |
builder.createIsFPClass(loc, v, cir::FPClassTest::Infinity), |
| 1024 |
convertType(e->getType()))); |
| 1025 |
} |
| 1026 |
case Builtin::BIfinite: |
| 1027 |
case Builtin::BI__finite: |
| 1028 |
case Builtin::BIfinitef: |
| 1029 |
case Builtin::BI__finitef: |
| 1030 |
case Builtin::BIfinitel: |
| 1031 |
case Builtin::BI__finitel: |
| 1032 |
case Builtin::BI__builtin_isfinite: { |
| 1033 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 1034 |
mlir::Value v = emitScalarExpr(e->getArg(0)); |
| 1035 |
assert(!cir::MissingFeatures::fpConstraints()); |
| 1036 |
mlir::Location loc = getLoc(e->getBeginLoc()); |
| 1037 |
return RValue::get(builder.createBoolToInt( |
| 1038 |
builder.createIsFPClass(loc, v, cir::FPClassTest::Finite), |
| 1039 |
convertType(e->getType()))); |
| 1040 |
} |
| 1041 |
|
| 1042 |
case Builtin::BI__builtin_isnormal: { |
| 1043 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 1044 |
mlir::Value v = emitScalarExpr(e->getArg(0)); |
| 1045 |
mlir::Location loc = getLoc(e->getBeginLoc()); |
| 1046 |
return RValue::get(builder.createBoolToInt( |
| 1047 |
builder.createIsFPClass(loc, v, cir::FPClassTest::Normal), |
| 1048 |
convertType(e->getType()))); |
| 1049 |
} |
| 1050 |
|
| 1051 |
case Builtin::BI__builtin_issubnormal: { |
| 1052 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 1053 |
mlir::Value v = emitScalarExpr(e->getArg(0)); |
| 1054 |
mlir::Location loc = getLoc(e->getBeginLoc()); |
| 1055 |
return RValue::get(builder.createBoolToInt( |
| 1056 |
builder.createIsFPClass(loc, v, cir::FPClassTest::Subnormal), |
| 1057 |
convertType(e->getType()))); |
| 1058 |
} |
| 1059 |
|
| 1060 |
case Builtin::BI__builtin_iszero: { |
| 1061 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 1062 |
mlir::Value v = emitScalarExpr(e->getArg(0)); |
| 1063 |
mlir::Location loc = getLoc(e->getBeginLoc()); |
| 1064 |
return RValue::get(builder.createBoolToInt( |
| 1065 |
builder.createIsFPClass(loc, v, cir::FPClassTest::Zero), |
| 1066 |
convertType(e->getType()))); |
| 1067 |
} |
| 1068 |
case Builtin::BI__builtin_isfpclass: { |
| 1069 |
Expr::EvalResult result; |
| 1070 |
if (!e->getArg(1)->EvaluateAsInt(result, cgm.getASTContext())) |
| 1071 |
break; |
| 1072 |
|
| 1073 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 1074 |
mlir::Value v = emitScalarExpr(e->getArg(0)); |
| 1075 |
uint64_t test = result.Val.getInt().getLimitedValue(); |
| 1076 |
mlir::Location loc = getLoc(e->getBeginLoc()); |
| 1077 |
// |
| 1078 |
return RValue::get(builder.createBoolToInt( |
| 1079 |
builder.createIsFPClass(loc, v, cir::FPClassTest(test)), |
| 1080 |
convertType(e->getType()))); |
| 1081 |
} |
| 1082 |
case Builtin::BI__builtin_nondeterministic_value: |
| 1083 |
case Builtin::BI__builtin_elementwise_abs: |
| 1084 |
return errorBuiltinNYI(*this, e, builtinID); |
| 1085 |
case Builtin::BI__builtin_elementwise_acos: |
| 1086 |
return emitUnaryFPBuiltin<cir::ACosOp>(*this, *e); |
| 1087 |
case Builtin::BI__builtin_elementwise_asin: |
| 1088 |
return emitUnaryFPBuiltin<cir::ASinOp>(*this, *e); |
| 1089 |
case Builtin::BI__builtin_elementwise_atan: |
| 1090 |
return emitUnaryFPBuiltin<cir::ATanOp>(*this, *e); |
| 1091 |
case Builtin::BI__builtin_elementwise_atan2: |
| 1092 |
case Builtin::BI__builtin_elementwise_ceil: |
| 1093 |
case Builtin::BI__builtin_elementwise_exp: |
| 1094 |
case Builtin::BI__builtin_elementwise_exp2: |
| 1095 |
case Builtin::BI__builtin_elementwise_exp10: |
| 1096 |
case Builtin::BI__builtin_elementwise_ldexp: |
| 1097 |
case Builtin::BI__builtin_elementwise_log: |
| 1098 |
case Builtin::BI__builtin_elementwise_log2: |
| 1099 |
case Builtin::BI__builtin_elementwise_log10: |
| 1100 |
case Builtin::BI__builtin_elementwise_pow: |
| 1101 |
case Builtin::BI__builtin_elementwise_bitreverse: |
| 1102 |
return errorBuiltinNYI(*this, e, builtinID); |
| 1103 |
case Builtin::BI__builtin_elementwise_cos: |
| 1104 |
return emitUnaryFPBuiltin<cir::CosOp>(*this, *e); |
| 1105 |
case Builtin::BI__builtin_elementwise_cosh: |
| 1106 |
case Builtin::BI__builtin_elementwise_floor: |
| 1107 |
case Builtin::BI__builtin_elementwise_popcount: |
| 1108 |
case Builtin::BI__builtin_elementwise_roundeven: |
| 1109 |
case Builtin::BI__builtin_elementwise_round: |
| 1110 |
case Builtin::BI__builtin_elementwise_rint: |
| 1111 |
case Builtin::BI__builtin_elementwise_nearbyint: |
| 1112 |
case Builtin::BI__builtin_elementwise_sin: |
| 1113 |
case Builtin::BI__builtin_elementwise_sinh: |
| 1114 |
case Builtin::BI__builtin_elementwise_tan: |
| 1115 |
case Builtin::BI__builtin_elementwise_tanh: |
| 1116 |
case Builtin::BI__builtin_elementwise_trunc: |
| 1117 |
case Builtin::BI__builtin_elementwise_canonicalize: |
| 1118 |
case Builtin::BI__builtin_elementwise_copysign: |
| 1119 |
case Builtin::BI__builtin_elementwise_fma: |
| 1120 |
case Builtin::BI__builtin_elementwise_fshl: |
| 1121 |
case Builtin::BI__builtin_elementwise_fshr: |
| 1122 |
case Builtin::BI__builtin_elementwise_add_sat: |
| 1123 |
case Builtin::BI__builtin_elementwise_sub_sat: |
| 1124 |
case Builtin::BI__builtin_elementwise_max: |
| 1125 |
case Builtin::BI__builtin_elementwise_min: |
| 1126 |
case Builtin::BI__builtin_elementwise_maxnum: |
| 1127 |
case Builtin::BI__builtin_elementwise_minnum: |
| 1128 |
case Builtin::BI__builtin_elementwise_maximum: |
| 1129 |
case Builtin::BI__builtin_elementwise_minimum: |
| 1130 |
case Builtin::BI__builtin_elementwise_maximumnum: |
| 1131 |
case Builtin::BI__builtin_elementwise_minimumnum: |
| 1132 |
case Builtin::BI__builtin_reduce_max: |
| 1133 |
case Builtin::BI__builtin_reduce_min: |
| 1134 |
case Builtin::BI__builtin_reduce_add: |
| 1135 |
case Builtin::BI__builtin_reduce_mul: |
| 1136 |
case Builtin::BI__builtin_reduce_xor: |
| 1137 |
case Builtin::BI__builtin_reduce_or: |
| 1138 |
case Builtin::BI__builtin_reduce_and: |
| 1139 |
case Builtin::BI__builtin_reduce_maximum: |
| 1140 |
case Builtin::BI__builtin_reduce_minimum: |
| 1141 |
case Builtin::BI__builtin_matrix_transpose: |
| 1142 |
case Builtin::BI__builtin_matrix_column_major_load: |
| 1143 |
case Builtin::BI__builtin_matrix_column_major_store: |
| 1144 |
case Builtin::BI__builtin_masked_load: |
| 1145 |
case Builtin::BI__builtin_masked_expand_load: |
| 1146 |
case Builtin::BI__builtin_masked_gather: |
| 1147 |
case Builtin::BI__builtin_masked_store: |
| 1148 |
case Builtin::BI__builtin_masked_compress_store: |
| 1149 |
case Builtin::BI__builtin_masked_scatter: |
| 1150 |
case Builtin::BI__builtin_isinf_sign: |
| 1151 |
case Builtin::BI__builtin_flt_rounds: |
| 1152 |
case Builtin::BI__builtin_set_flt_rounds: |
| 1153 |
case Builtin::BI__builtin_fpclassify: |
| 1154 |
return errorBuiltinNYI(*this, e, builtinID); |
| 1155 |
case Builtin::BIalloca: |
| 1156 |
case Builtin::BI_alloca: |
| 1157 |
case Builtin::BI__builtin_alloca_uninitialized: |
| 1158 |
case Builtin::BI__builtin_alloca: |
| 1159 |
return emitBuiltinAlloca(*this, e, builtinID); |
| 1160 |
case Builtin::BI__builtin_alloca_with_align_uninitialized: |
| 1161 |
case Builtin::BI__builtin_alloca_with_align: |
| 1162 |
case Builtin::BI__builtin_infer_alloc_token: |
| 1163 |
case Builtin::BIbzero: |
| 1164 |
case Builtin::BI__builtin_bzero: |
| 1165 |
case Builtin::BIbcopy: |
| 1166 |
case Builtin::BI__builtin_bcopy: |
| 1167 |
return errorBuiltinNYI(*this, e, builtinID); |
| 1168 |
case Builtin::BImemcpy: |
| 1169 |
case Builtin::BI__builtin_memcpy: |
| 1170 |
case Builtin::BImempcpy: |
| 1171 |
case Builtin::BI__builtin_mempcpy: |
| 1172 |
case Builtin::BI__builtin_memcpy_inline: |
| 1173 |
case Builtin::BI__builtin_char_memchr: |
| 1174 |
case Builtin::BI__builtin___memcpy_chk: |
| 1175 |
case Builtin::BI__builtin_objc_memmove_collectable: |
| 1176 |
case Builtin::BI__builtin___memmove_chk: |
| 1177 |
case Builtin::BI__builtin_trivially_relocate: |
| 1178 |
case Builtin::BImemmove: |
| 1179 |
case Builtin::BI__builtin_memmove: |
| 1180 |
case Builtin::BImemset: |
| 1181 |
case Builtin::BI__builtin_memset: |
| 1182 |
case Builtin::BI__builtin_memset_inline: |
| 1183 |
case Builtin::BI__builtin___memset_chk: |
| 1184 |
case Builtin::BI__builtin_wmemchr: |
| 1185 |
case Builtin::BI__builtin_wmemcmp: |
| 1186 |
break; // Handled as library calls below. |
| 1187 |
case Builtin::BI__builtin_dwarf_cfa: |
| 1188 |
return errorBuiltinNYI(*this, e, builtinID); |
| 1189 |
case Builtin::BI__builtin_return_address: { |
| 1190 |
llvm::APSInt level = e->getArg(0)->EvaluateKnownConstInt(getContext()); |
| 1191 |
return RValue::get(cir::ReturnAddrOp::create( |
| 1192 |
builder, getLoc(e->getExprLoc()), |
| 1193 |
builder.getConstAPInt(loc, builder.getUInt32Ty(), level))); |
| 1194 |
} |
| 1195 |
case Builtin::BI_ReturnAddress: { |
| 1196 |
return RValue::get(cir::ReturnAddrOp::create( |
| 1197 |
builder, getLoc(e->getExprLoc()), |
| 1198 |
builder.getConstInt(loc, builder.getUInt32Ty(), 0))); |
| 1199 |
} |
| 1200 |
case Builtin::BI__builtin_frame_address: { |
| 1201 |
llvm::APSInt level = e->getArg(0)->EvaluateKnownConstInt(getContext()); |
| 1202 |
mlir::Location loc = getLoc(e->getExprLoc()); |
| 1203 |
mlir::Value addr = cir::FrameAddrOp::create( |
| 1204 |
builder, loc, allocaInt8PtrTy, |
| 1205 |
builder.getConstAPInt(loc, builder.getUInt32Ty(), level)); |
| 1206 |
return RValue::get( |
| 1207 |
builder.createCast(loc, cir::CastKind::bitcast, addr, voidPtrTy)); |
| 1208 |
} |
| 1209 |
case Builtin::BI__builtin_extract_return_addr: |
| 1210 |
case Builtin::BI__builtin_frob_return_addr: |
| 1211 |
case Builtin::BI__builtin_dwarf_sp_column: |
| 1212 |
case Builtin::BI__builtin_init_dwarf_reg_size_table: |
| 1213 |
case Builtin::BI__builtin_eh_return: |
| 1214 |
case Builtin::BI__builtin_unwind_init: |
| 1215 |
case Builtin::BI__builtin_extend_pointer: |
| 1216 |
case Builtin::BI__builtin_setjmp: |
| 1217 |
case Builtin::BI__builtin_longjmp: |
| 1218 |
case Builtin::BI__builtin_launder: |
| 1219 |
case Builtin::BI__sync_fetch_and_add: |
| 1220 |
case Builtin::BI__sync_fetch_and_sub: |
| 1221 |
case Builtin::BI__sync_fetch_and_or: |
| 1222 |
case Builtin::BI__sync_fetch_and_and: |
| 1223 |
case Builtin::BI__sync_fetch_and_xor: |
| 1224 |
case Builtin::BI__sync_fetch_and_nand: |
| 1225 |
case Builtin::BI__sync_add_and_fetch: |
| 1226 |
case Builtin::BI__sync_sub_and_fetch: |
| 1227 |
case Builtin::BI__sync_and_and_fetch: |
| 1228 |
case Builtin::BI__sync_or_and_fetch: |
| 1229 |
case Builtin::BI__sync_xor_and_fetch: |
| 1230 |
case Builtin::BI__sync_nand_and_fetch: |
| 1231 |
case Builtin::BI__sync_val_compare_and_swap: |
| 1232 |
case Builtin::BI__sync_bool_compare_and_swap: |
| 1233 |
case Builtin::BI__sync_lock_test_and_set: |
| 1234 |
case Builtin::BI__sync_lock_release: |
| 1235 |
case Builtin::BI__sync_swap: |
| 1236 |
case Builtin::BI__sync_fetch_and_add_1: |
| 1237 |
case Builtin::BI__sync_fetch_and_add_2: |
| 1238 |
case Builtin::BI__sync_fetch_and_add_4: |
| 1239 |
case Builtin::BI__sync_fetch_and_add_8: |
| 1240 |
case Builtin::BI__sync_fetch_and_add_16: |
| 1241 |
case Builtin::BI__sync_fetch_and_sub_1: |
| 1242 |
case Builtin::BI__sync_fetch_and_sub_2: |
| 1243 |
case Builtin::BI__sync_fetch_and_sub_4: |
| 1244 |
case Builtin::BI__sync_fetch_and_sub_8: |
| 1245 |
case Builtin::BI__sync_fetch_and_sub_16: |
| 1246 |
case Builtin::BI__sync_fetch_and_or_1: |
| 1247 |
case Builtin::BI__sync_fetch_and_or_2: |
| 1248 |
case Builtin::BI__sync_fetch_and_or_4: |
| 1249 |
case Builtin::BI__sync_fetch_and_or_8: |
| 1250 |
case Builtin::BI__sync_fetch_and_or_16: |
| 1251 |
case Builtin::BI__sync_fetch_and_and_1: |
| 1252 |
case Builtin::BI__sync_fetch_and_and_2: |
| 1253 |
case Builtin::BI__sync_fetch_and_and_4: |
| 1254 |
case Builtin::BI__sync_fetch_and_and_8: |
| 1255 |
case Builtin::BI__sync_fetch_and_and_16: |
| 1256 |
case Builtin::BI__sync_fetch_and_xor_1: |
| 1257 |
case Builtin::BI__sync_fetch_and_xor_2: |
| 1258 |
case Builtin::BI__sync_fetch_and_xor_4: |
| 1259 |
case Builtin::BI__sync_fetch_and_xor_8: |
| 1260 |
case Builtin::BI__sync_fetch_and_xor_16: |
| 1261 |
case Builtin::BI__sync_fetch_and_nand_1: |
| 1262 |
case Builtin::BI__sync_fetch_and_nand_2: |
| 1263 |
case Builtin::BI__sync_fetch_and_nand_4: |
| 1264 |
case Builtin::BI__sync_fetch_and_nand_8: |
| 1265 |
case Builtin::BI__sync_fetch_and_nand_16: |
| 1266 |
case Builtin::BI__sync_fetch_and_min: |
| 1267 |
case Builtin::BI__sync_fetch_and_max: |
| 1268 |
case Builtin::BI__sync_fetch_and_umin: |
| 1269 |
case Builtin::BI__sync_fetch_and_umax: |
| 1270 |
case Builtin::BI__sync_add_and_fetch_1: |
| 1271 |
case Builtin::BI__sync_add_and_fetch_2: |
| 1272 |
case Builtin::BI__sync_add_and_fetch_4: |
| 1273 |
case Builtin::BI__sync_add_and_fetch_8: |
| 1274 |
case Builtin::BI__sync_add_and_fetch_16: |
| 1275 |
case Builtin::BI__sync_sub_and_fetch_1: |
| 1276 |
case Builtin::BI__sync_sub_and_fetch_2: |
| 1277 |
case Builtin::BI__sync_sub_and_fetch_4: |
| 1278 |
case Builtin::BI__sync_sub_and_fetch_8: |
| 1279 |
case Builtin::BI__sync_sub_and_fetch_16: |
| 1280 |
case Builtin::BI__sync_and_and_fetch_1: |
| 1281 |
case Builtin::BI__sync_and_and_fetch_2: |
| 1282 |
case Builtin::BI__sync_and_and_fetch_4: |
| 1283 |
case Builtin::BI__sync_and_and_fetch_8: |
| 1284 |
case Builtin::BI__sync_and_and_fetch_16: |
| 1285 |
case Builtin::BI__sync_or_and_fetch_1: |
| 1286 |
case Builtin::BI__sync_or_and_fetch_2: |
| 1287 |
case Builtin::BI__sync_or_and_fetch_4: |
| 1288 |
case Builtin::BI__sync_or_and_fetch_8: |
| 1289 |
case Builtin::BI__sync_or_and_fetch_16: |
| 1290 |
case Builtin::BI__sync_xor_and_fetch_1: |
| 1291 |
case Builtin::BI__sync_xor_and_fetch_2: |
| 1292 |
case Builtin::BI__sync_xor_and_fetch_4: |
| 1293 |
case Builtin::BI__sync_xor_and_fetch_8: |
| 1294 |
case Builtin::BI__sync_xor_and_fetch_16: |
| 1295 |
case Builtin::BI__sync_nand_and_fetch_1: |
| 1296 |
case Builtin::BI__sync_nand_and_fetch_2: |
| 1297 |
case Builtin::BI__sync_nand_and_fetch_4: |
| 1298 |
case Builtin::BI__sync_nand_and_fetch_8: |
| 1299 |
case Builtin::BI__sync_nand_and_fetch_16: |
| 1300 |
case Builtin::BI__sync_val_compare_and_swap_1: |
| 1301 |
case Builtin::BI__sync_val_compare_and_swap_2: |
| 1302 |
case Builtin::BI__sync_val_compare_and_swap_4: |
| 1303 |
case Builtin::BI__sync_val_compare_and_swap_8: |
| 1304 |
case Builtin::BI__sync_val_compare_and_swap_16: |
| 1305 |
case Builtin::BI__sync_bool_compare_and_swap_1: |
| 1306 |
case Builtin::BI__sync_bool_compare_and_swap_2: |
| 1307 |
case Builtin::BI__sync_bool_compare_and_swap_4: |
| 1308 |
case Builtin::BI__sync_bool_compare_and_swap_8: |
| 1309 |
case Builtin::BI__sync_bool_compare_and_swap_16: |
| 1310 |
case Builtin::BI__sync_swap_1: |
| 1311 |
case Builtin::BI__sync_swap_2: |
| 1312 |
case Builtin::BI__sync_swap_4: |
| 1313 |
case Builtin::BI__sync_swap_8: |
| 1314 |
case Builtin::BI__sync_swap_16: |
| 1315 |
case Builtin::BI__sync_lock_test_and_set_1: |
| 1316 |
case Builtin::BI__sync_lock_test_and_set_2: |
| 1317 |
case Builtin::BI__sync_lock_test_and_set_4: |
| 1318 |
case Builtin::BI__sync_lock_test_and_set_8: |
| 1319 |
case Builtin::BI__sync_lock_test_and_set_16: |
| 1320 |
case Builtin::BI__sync_lock_release_1: |
| 1321 |
case Builtin::BI__sync_lock_release_2: |
| 1322 |
case Builtin::BI__sync_lock_release_4: |
| 1323 |
case Builtin::BI__sync_lock_release_8: |
| 1324 |
case Builtin::BI__sync_lock_release_16: |
| 1325 |
case Builtin::BI__sync_synchronize: |
| 1326 |
case Builtin::BI__builtin_nontemporal_load: |
| 1327 |
case Builtin::BI__builtin_nontemporal_store: |
| 1328 |
case Builtin::BI__c11_atomic_is_lock_free: |
| 1329 |
case Builtin::BI__atomic_is_lock_free: |
| 1330 |
case Builtin::BI__atomic_test_and_set: |
| 1331 |
case Builtin::BI__atomic_clear: |
| 1332 |
return errorBuiltinNYI(*this, e, builtinID); |
| 1333 |
case Builtin::BI__atomic_thread_fence: |
| 1334 |
case Builtin::BI__c11_atomic_thread_fence: { |
| 1335 |
emitAtomicFenceOp(*this, e, cir::SyncScopeKind::System); |
| 1336 |
return RValue::get(nullptr); |
| 1337 |
} |
| 1338 |
case Builtin::BI__atomic_signal_fence: |
| 1339 |
case Builtin::BI__c11_atomic_signal_fence: { |
| 1340 |
emitAtomicFenceOp(*this, e, cir::SyncScopeKind::SingleThread); |
| 1341 |
return RValue::get(nullptr); |
| 1342 |
} |
| 1343 |
case Builtin::BI__scoped_atomic_thread_fence: |
| 1344 |
case Builtin::BI__builtin_signbit: |
| 1345 |
case Builtin::BI__builtin_signbitf: |
| 1346 |
case Builtin::BI__builtin_signbitl: |
| 1347 |
case Builtin::BI__warn_memset_zero_len: |
| 1348 |
case Builtin::BI__annotation: |
| 1349 |
case Builtin::BI__builtin_annotation: |
| 1350 |
case Builtin::BI__builtin_addcb: |
| 1351 |
case Builtin::BI__builtin_addcs: |
| 1352 |
case Builtin::BI__builtin_addc: |
| 1353 |
case Builtin::BI__builtin_addcl: |
| 1354 |
case Builtin::BI__builtin_addcll: |
| 1355 |
case Builtin::BI__builtin_subcb: |
| 1356 |
case Builtin::BI__builtin_subcs: |
| 1357 |
case Builtin::BI__builtin_subc: |
| 1358 |
case Builtin::BI__builtin_subcl: |
| 1359 |
case Builtin::BI__builtin_subcll: |
| 1360 |
return errorBuiltinNYI(*this, e, builtinID); |
| 1361 |
|
| 1362 |
case Builtin::BI__builtin_add_overflow: |
| 1363 |
case Builtin::BI__builtin_sub_overflow: |
| 1364 |
case Builtin::BI__builtin_mul_overflow: { |
| 1365 |
const clang::Expr *leftArg = e->getArg(0); |
| 1366 |
const clang::Expr *rightArg = e->getArg(1); |
| 1367 |
const clang::Expr *resultArg = e->getArg(2); |
| 1368 |
|
| 1369 |
clang::QualType resultQTy = |
| 1370 |
resultArg->getType()->castAs<clang::PointerType>()->getPointeeType(); |
| 1371 |
|
| 1372 |
WidthAndSignedness leftInfo = |
| 1373 |
getIntegerWidthAndSignedness(cgm.getASTContext(), leftArg->getType()); |
| 1374 |
WidthAndSignedness rightInfo = |
| 1375 |
getIntegerWidthAndSignedness(cgm.getASTContext(), rightArg->getType()); |
| 1376 |
WidthAndSignedness resultInfo = |
| 1377 |
getIntegerWidthAndSignedness(cgm.getASTContext(), resultQTy); |
| 1378 |
|
| 1379 |
// Note we compute the encompassing type with the consideration to the |
| 1380 |
// result type, so later in LLVM lowering we don't get redundant integral |
| 1381 |
// extension casts. |
| 1382 |
WidthAndSignedness encompassingInfo = |
| 1383 |
EncompassingIntegerType({leftInfo, rightInfo, resultInfo}); |
| 1384 |
|
| 1385 |
auto encompassingCIRTy = cir::IntType::get( |
| 1386 |
&getMLIRContext(), encompassingInfo.width, encompassingInfo.isSigned); |
| 1387 |
auto resultCIRTy = mlir::cast<cir::IntType>(cgm.convertType(resultQTy)); |
| 1388 |
|
| 1389 |
mlir::Value left = emitScalarExpr(leftArg); |
| 1390 |
mlir::Value right = emitScalarExpr(rightArg); |
| 1391 |
Address resultPtr = emitPointerWithAlignment(resultArg); |
| 1392 |
|
| 1393 |
// Extend each operand to the encompassing type, if necessary. |
| 1394 |
if (left.getType() != encompassingCIRTy) |
| 1395 |
left = |
| 1396 |
builder.createCast(cir::CastKind::integral, left, encompassingCIRTy); |
| 1397 |
if (right.getType() != encompassingCIRTy) |
| 1398 |
right = |
| 1399 |
builder.createCast(cir::CastKind::integral, right, encompassingCIRTy); |
| 1400 |
|
| 1401 |
// Perform the operation on the extended values. |
| 1402 |
cir::BinOpOverflowKind opKind; |
| 1403 |
switch (builtinID) { |
| 1404 |
default: |
| 1405 |
llvm_unreachable("Unknown overflow builtin id."); |
| 1406 |
case Builtin::BI__builtin_add_overflow: |
| 1407 |
opKind = cir::BinOpOverflowKind::Add; |
| 1408 |
break; |
| 1409 |
case Builtin::BI__builtin_sub_overflow: |
| 1410 |
opKind = cir::BinOpOverflowKind::Sub; |
| 1411 |
break; |
| 1412 |
case Builtin::BI__builtin_mul_overflow: |
| 1413 |
opKind = cir::BinOpOverflowKind::Mul; |
| 1414 |
break; |
| 1415 |
} |
| 1416 |
|
| 1417 |
mlir::Location loc = getLoc(e->getSourceRange()); |
| 1418 |
auto arithOp = cir::BinOpOverflowOp::create(builder, loc, resultCIRTy, |
| 1419 |
opKind, left, right); |
| 1420 |
|
| 1421 |
// Here is a slight difference from the original clang CodeGen: |
| 1422 |
// - In the original clang CodeGen, the checked arithmetic result is |
| 1423 |
// first computed as a value of the encompassing type, and then it is |
| 1424 |
// truncated to the actual result type with a second overflow checking. |
| 1425 |
// - In CIRGen, the checked arithmetic operation directly produce the |
| 1426 |
// checked arithmetic result in its expected type. |
| 1427 |
// |
| 1428 |
// So we don't need a truncation and a second overflow checking here. |
| 1429 |
|
| 1430 |
// Finally, store the result using the pointer. |
| 1431 |
bool isVolatile = |
| 1432 |
resultArg->getType()->getPointeeType().isVolatileQualified(); |
| 1433 |
builder.createStore(loc, emitToMemory(arithOp.getResult(), resultQTy), |
| 1434 |
resultPtr, isVolatile); |
| 1435 |
|
| 1436 |
return RValue::get(arithOp.getOverflow()); |
| 1437 |
} |
| 1438 |
|
| 1439 |
case Builtin::BI__builtin_uadd_overflow: |
| 1440 |
case Builtin::BI__builtin_uaddl_overflow: |
| 1441 |
case Builtin::BI__builtin_uaddll_overflow: |
| 1442 |
case Builtin::BI__builtin_usub_overflow: |
| 1443 |
case Builtin::BI__builtin_usubl_overflow: |
| 1444 |
case Builtin::BI__builtin_usubll_overflow: |
| 1445 |
case Builtin::BI__builtin_umul_overflow: |
| 1446 |
case Builtin::BI__builtin_umull_overflow: |
| 1447 |
case Builtin::BI__builtin_umulll_overflow: |
| 1448 |
case Builtin::BI__builtin_sadd_overflow: |
| 1449 |
case Builtin::BI__builtin_saddl_overflow: |
| 1450 |
case Builtin::BI__builtin_saddll_overflow: |
| 1451 |
case Builtin::BI__builtin_ssub_overflow: |
| 1452 |
case Builtin::BI__builtin_ssubl_overflow: |
| 1453 |
case Builtin::BI__builtin_ssubll_overflow: |
| 1454 |
case Builtin::BI__builtin_smul_overflow: |
| 1455 |
case Builtin::BI__builtin_smull_overflow: |
| 1456 |
case Builtin::BI__builtin_smulll_overflow: { |
| 1457 |
// Scalarize our inputs. |
| 1458 |
mlir::Value x = emitScalarExpr(e->getArg(0)); |
| 1459 |
mlir::Value y = emitScalarExpr(e->getArg(1)); |
| 1460 |
|
| 1461 |
const clang::Expr *resultArg = e->getArg(2); |
| 1462 |
Address resultPtr = emitPointerWithAlignment(resultArg); |
| 1463 |
|
| 1464 |
// Decide which of the arithmetic operation we are lowering to: |
| 1465 |
cir::BinOpOverflowKind arithKind; |
| 1466 |
switch (builtinID) { |
| 1467 |
default: |
| 1468 |
llvm_unreachable("Unknown overflow builtin id."); |
| 1469 |
case Builtin::BI__builtin_uadd_overflow: |
| 1470 |
case Builtin::BI__builtin_uaddl_overflow: |
| 1471 |
case Builtin::BI__builtin_uaddll_overflow: |
| 1472 |
case Builtin::BI__builtin_sadd_overflow: |
| 1473 |
case Builtin::BI__builtin_saddl_overflow: |
| 1474 |
case Builtin::BI__builtin_saddll_overflow: |
| 1475 |
arithKind = cir::BinOpOverflowKind::Add; |
| 1476 |
break; |
| 1477 |
case Builtin::BI__builtin_usub_overflow: |
| 1478 |
case Builtin::BI__builtin_usubl_overflow: |
| 1479 |
case Builtin::BI__builtin_usubll_overflow: |
| 1480 |
case Builtin::BI__builtin_ssub_overflow: |
| 1481 |
case Builtin::BI__builtin_ssubl_overflow: |
| 1482 |
case Builtin::BI__builtin_ssubll_overflow: |
| 1483 |
arithKind = cir::BinOpOverflowKind::Sub; |
| 1484 |
break; |
| 1485 |
case Builtin::BI__builtin_umul_overflow: |
| 1486 |
case Builtin::BI__builtin_umull_overflow: |
| 1487 |
case Builtin::BI__builtin_umulll_overflow: |
| 1488 |
case Builtin::BI__builtin_smul_overflow: |
| 1489 |
case Builtin::BI__builtin_smull_overflow: |
| 1490 |
case Builtin::BI__builtin_smulll_overflow: |
| 1491 |
arithKind = cir::BinOpOverflowKind::Mul; |
| 1492 |
break; |
| 1493 |
} |
| 1494 |
|
| 1495 |
clang::QualType resultQTy = |
| 1496 |
resultArg->getType()->castAs<clang::PointerType>()->getPointeeType(); |
| 1497 |
auto resultCIRTy = mlir::cast<cir::IntType>(cgm.convertType(resultQTy)); |
| 1498 |
|
| 1499 |
mlir::Location loc = getLoc(e->getSourceRange()); |
| 1500 |
cir::BinOpOverflowOp arithOp = cir::BinOpOverflowOp::create( |
| 1501 |
builder, loc, resultCIRTy, arithKind, x, y); |
| 1502 |
|
| 1503 |
bool isVolatile = |
| 1504 |
resultArg->getType()->getPointeeType().isVolatileQualified(); |
| 1505 |
builder.createStore(loc, emitToMemory(arithOp.getResult(), resultQTy), |
| 1506 |
resultPtr, isVolatile); |
| 1507 |
|
| 1508 |
return RValue::get(arithOp.getOverflow()); |
| 1509 |
} |
| 1510 |
|
| 1511 |
case Builtin::BIaddressof: |
| 1512 |
case Builtin::BI__addressof: |
| 1513 |
case Builtin::BI__builtin_addressof: |
| 1514 |
case Builtin::BI__builtin_function_start: |
| 1515 |
return errorBuiltinNYI(*this, e, builtinID); |
| 1516 |
case Builtin::BI__builtin_operator_new: |
| 1517 |
return emitNewOrDeleteBuiltinCall( |
| 1518 |
e->getCallee()->getType()->castAs<FunctionProtoType>(), e, OO_New); |
| 1519 |
case Builtin::BI__builtin_operator_delete: |
| 1520 |
emitNewOrDeleteBuiltinCall( |
| 1521 |
e->getCallee()->getType()->castAs<FunctionProtoType>(), e, OO_Delete); |
| 1522 |
return RValue::get(nullptr); |
| 1523 |
case Builtin::BI__builtin_is_aligned: |
| 1524 |
case Builtin::BI__builtin_align_up: |
| 1525 |
case Builtin::BI__builtin_align_down: |
| 1526 |
case Builtin::BI__noop: |
| 1527 |
case Builtin::BI__builtin_call_with_static_chain: |
| 1528 |
case Builtin::BI_InterlockedExchange8: |
| 1529 |
case Builtin::BI_InterlockedExchange16: |
| 1530 |
case Builtin::BI_InterlockedExchange: |
| 1531 |
case Builtin::BI_InterlockedExchangePointer: |
| 1532 |
case Builtin::BI_InterlockedCompareExchangePointer: |
| 1533 |
case Builtin::BI_InterlockedCompareExchangePointer_nf: |
| 1534 |
case Builtin::BI_InterlockedCompareExchange8: |
| 1535 |
case Builtin::BI_InterlockedCompareExchange16: |
| 1536 |
case Builtin::BI_InterlockedCompareExchange: |
| 1537 |
case Builtin::BI_InterlockedCompareExchange64: |
| 1538 |
case Builtin::BI_InterlockedIncrement16: |
| 1539 |
case Builtin::BI_InterlockedIncrement: |
| 1540 |
case Builtin::BI_InterlockedDecrement16: |
| 1541 |
case Builtin::BI_InterlockedDecrement: |
| 1542 |
case Builtin::BI_InterlockedAnd8: |
| 1543 |
case Builtin::BI_InterlockedAnd16: |
| 1544 |
case Builtin::BI_InterlockedAnd: |
| 1545 |
case Builtin::BI_InterlockedExchangeAdd8: |
| 1546 |
case Builtin::BI_InterlockedExchangeAdd16: |
| 1547 |
case Builtin::BI_InterlockedExchangeAdd: |
| 1548 |
case Builtin::BI_InterlockedExchangeSub8: |
| 1549 |
case Builtin::BI_InterlockedExchangeSub16: |
| 1550 |
case Builtin::BI_InterlockedExchangeSub: |
| 1551 |
case Builtin::BI_InterlockedOr8: |
| 1552 |
case Builtin::BI_InterlockedOr16: |
| 1553 |
case Builtin::BI_InterlockedOr: |
| 1554 |
case Builtin::BI_InterlockedXor8: |
| 1555 |
case Builtin::BI_InterlockedXor16: |
| 1556 |
case Builtin::BI_InterlockedXor: |
| 1557 |
case Builtin::BI_bittest64: |
| 1558 |
case Builtin::BI_bittest: |
| 1559 |
case Builtin::BI_bittestandcomplement64: |
| 1560 |
case Builtin::BI_bittestandcomplement: |
| 1561 |
case Builtin::BI_bittestandreset64: |
| 1562 |
case Builtin::BI_bittestandreset: |
| 1563 |
case Builtin::BI_bittestandset64: |
| 1564 |
case Builtin::BI_bittestandset: |
| 1565 |
case Builtin::BI_interlockedbittestandreset: |
| 1566 |
case Builtin::BI_interlockedbittestandreset64: |
| 1567 |
case Builtin::BI_interlockedbittestandreset64_acq: |
| 1568 |
case Builtin::BI_interlockedbittestandreset64_rel: |
| 1569 |
case Builtin::BI_interlockedbittestandreset64_nf: |
| 1570 |
case Builtin::BI_interlockedbittestandset64: |
| 1571 |
case Builtin::BI_interlockedbittestandset64_acq: |
| 1572 |
case Builtin::BI_interlockedbittestandset64_rel: |
| 1573 |
case Builtin::BI_interlockedbittestandset64_nf: |
| 1574 |
case Builtin::BI_interlockedbittestandset: |
| 1575 |
case Builtin::BI_interlockedbittestandset_acq: |
| 1576 |
case Builtin::BI_interlockedbittestandset_rel: |
| 1577 |
case Builtin::BI_interlockedbittestandset_nf: |
| 1578 |
case Builtin::BI_interlockedbittestandreset_acq: |
| 1579 |
case Builtin::BI_interlockedbittestandreset_rel: |
| 1580 |
case Builtin::BI_interlockedbittestandreset_nf: |
| 1581 |
case Builtin::BI__iso_volatile_load8: |
| 1582 |
case Builtin::BI__iso_volatile_load16: |
| 1583 |
case Builtin::BI__iso_volatile_load32: |
| 1584 |
case Builtin::BI__iso_volatile_load64: |
| 1585 |
case Builtin::BI__iso_volatile_store8: |
| 1586 |
case Builtin::BI__iso_volatile_store16: |
| 1587 |
case Builtin::BI__iso_volatile_store32: |
| 1588 |
case Builtin::BI__iso_volatile_store64: |
| 1589 |
case Builtin::BI__builtin_ptrauth_sign_constant: |
| 1590 |
case Builtin::BI__builtin_ptrauth_auth: |
| 1591 |
case Builtin::BI__builtin_ptrauth_auth_and_resign: |
| 1592 |
case Builtin::BI__builtin_ptrauth_blend_discriminator: |
| 1593 |
case Builtin::BI__builtin_ptrauth_sign_generic_data: |
| 1594 |
case Builtin::BI__builtin_ptrauth_sign_unauthenticated: |
| 1595 |
case Builtin::BI__builtin_ptrauth_strip: |
| 1596 |
case Builtin::BI__builtin_get_vtable_pointer: |
| 1597 |
case Builtin::BI__exception_code: |
| 1598 |
case Builtin::BI_exception_code: |
| 1599 |
case Builtin::BI__exception_info: |
| 1600 |
case Builtin::BI_exception_info: |
| 1601 |
case Builtin::BI__abnormal_termination: |
| 1602 |
case Builtin::BI_abnormal_termination: |
| 1603 |
case Builtin::BI_setjmpex: |
| 1604 |
case Builtin::BI_setjmp: |
| 1605 |
case Builtin::BImove: |
| 1606 |
case Builtin::BImove_if_noexcept: |
| 1607 |
case Builtin::BIforward: |
| 1608 |
case Builtin::BIforward_like: |
| 1609 |
case Builtin::BIas_const: |
| 1610 |
case Builtin::BI__GetExceptionInfo: |
| 1611 |
case Builtin::BI__fastfail: |
| 1612 |
case Builtin::BIread_pipe: |
| 1613 |
case Builtin::BIwrite_pipe: |
| 1614 |
case Builtin::BIreserve_read_pipe: |
| 1615 |
case Builtin::BIreserve_write_pipe: |
| 1616 |
case Builtin::BIwork_group_reserve_read_pipe: |
| 1617 |
case Builtin::BIwork_group_reserve_write_pipe: |
| 1618 |
case Builtin::BIsub_group_reserve_read_pipe: |
| 1619 |
case Builtin::BIsub_group_reserve_write_pipe: |
| 1620 |
case Builtin::BIcommit_read_pipe: |
| 1621 |
case Builtin::BIcommit_write_pipe: |
| 1622 |
case Builtin::BIwork_group_commit_read_pipe: |
| 1623 |
case Builtin::BIwork_group_commit_write_pipe: |
| 1624 |
case Builtin::BIsub_group_commit_read_pipe: |
| 1625 |
case Builtin::BIsub_group_commit_write_pipe: |
| 1626 |
case Builtin::BIget_pipe_num_packets: |
| 1627 |
case Builtin::BIget_pipe_max_packets: |
| 1628 |
case Builtin::BIto_global: |
| 1629 |
case Builtin::BIto_local: |
| 1630 |
case Builtin::BIto_private: |
| 1631 |
case Builtin::BIenqueue_kernel: |
| 1632 |
case Builtin::BIget_kernel_work_group_size: |
| 1633 |
case Builtin::BIget_kernel_preferred_work_group_size_multiple: |
| 1634 |
case Builtin::BIget_kernel_max_sub_group_size_for_ndrange: |
| 1635 |
case Builtin::BIget_kernel_sub_group_count_for_ndrange: |
| 1636 |
case Builtin::BI__builtin_store_half: |
| 1637 |
case Builtin::BI__builtin_store_halff: |
| 1638 |
case Builtin::BI__builtin_load_half: |
| 1639 |
case Builtin::BI__builtin_load_halff: |
| 1640 |
return errorBuiltinNYI(*this, e, builtinID); |
| 1641 |
case Builtin::BI__builtin_printf: |
| 1642 |
case Builtin::BIprintf: |
| 1643 |
break; |
| 1644 |
case Builtin::BI__builtin_canonicalize: |
| 1645 |
case Builtin::BI__builtin_canonicalizef: |
| 1646 |
case Builtin::BI__builtin_canonicalizef16: |
| 1647 |
case Builtin::BI__builtin_canonicalizel: |
| 1648 |
case Builtin::BI__builtin_thread_pointer: |
| 1649 |
case Builtin::BI__builtin_os_log_format: |
| 1650 |
case Builtin::BI__xray_customevent: |
| 1651 |
case Builtin::BI__xray_typedevent: |
| 1652 |
case Builtin::BI__builtin_ms_va_start: |
| 1653 |
case Builtin::BI__builtin_ms_va_end: |
| 1654 |
case Builtin::BI__builtin_ms_va_copy: |
| 1655 |
case Builtin::BI__builtin_get_device_side_mangled_name: |
| 1656 |
return errorBuiltinNYI(*this, e, builtinID); |
| 1657 |
} |
| 1658 |
|
| 1659 |
// If this is an alias for a lib function (e.g. __builtin_sin), emit |
| 1660 |
// the call using the normal call path, but using the unmangled |
| 1661 |
// version of the function name. |
| 1662 |
if (getContext().BuiltinInfo.isLibFunction(builtinID)) |
| 1663 |
return emitLibraryCall(*this, fd, e, |
| 1664 |
cgm.getBuiltinLibFunction(fd, builtinID)); |
| 1665 |
|
| 1666 |
// Some target-specific builtins can have aggregate return values, e.g. |
| 1667 |
// __builtin_arm_mve_vld2q_u32. So if the result is an aggregate, force |
| 1668 |
// returnValue to be non-null, so that the target-specific emission code can |
| 1669 |
// always just emit into it. |
| 1670 |
cir::TypeEvaluationKind evalKind = getEvaluationKind(e->getType()); |
| 1671 |
if (evalKind == cir::TEK_Aggregate && returnValue.isNull()) { |
| 1672 |
cgm.errorNYI(e->getSourceRange(), "aggregate return value from builtin"); |
| 1673 |
return getUndefRValue(e->getType()); |
| 1674 |
} |
| 1675 |
|
| 1676 |
// Now see if we can emit a target-specific builtin. |
| 1677 |
// FIXME: This is a temporary mechanism (double-optional semantics) that will |
| 1678 |
// go away once everything is implemented: |
| 1679 |
// 1. return `mlir::Value{}` for cases where we have issued the diagnostic. |
| 1680 |
// 2. return `std::nullopt` in cases where we didn't issue a diagnostic |
| 1681 |
// but also didn't handle the builtin. |
| 1682 |
if (std::optional<mlir::Value> rst = |
| 1683 |
emitTargetBuiltinExpr(builtinID, e, returnValue)) { |
| 1684 |
mlir::Value v = rst.value(); |
| 1685 |
// CIR dialect operations may have no results, no values will be returned |
| 1686 |
// even if it executes successfully. |
| 1687 |
if (!v) |
| 1688 |
return RValue::get(nullptr); |
| 1689 |
|
| 1690 |
switch (evalKind) { |
| 1691 |
case cir::TEK_Scalar: |
| 1692 |
if (mlir::isa<cir::VoidType>(v.getType())) |
| 1693 |
return RValue::get(nullptr); |
| 1694 |
return RValue::get(v); |
| 1695 |
case cir::TEK_Aggregate: |
| 1696 |
cgm.errorNYI(e->getSourceRange(), "aggregate return value from builtin"); |
| 1697 |
return getUndefRValue(e->getType()); |
| 1698 |
case cir::TEK_Complex: |
| 1699 |
llvm_unreachable("No current target builtin returns complex"); |
| 1700 |
} |
| 1701 |
llvm_unreachable("Bad evaluation kind in EmitBuiltinExpr"); |
| 1702 |
} |
| 1703 |
|
| 1704 |
cgm.errorNYI(e->getSourceRange(), |
| 1705 |
std::string("unimplemented builtin call: ") + |
| 1706 |
getContext().BuiltinInfo.getName(builtinID)); |
| 1707 |
return getUndefRValue(e->getType()); |
| 1708 |
} |
| 1709 |
|
| 1710 |
static std::optional<mlir::Value> |
| 1711 |
emitTargetArchBuiltinExpr(CIRGenFunction *cgf, unsigned builtinID, |
| 1712 |
const CallExpr *e, ReturnValueSlot &returnValue, |
| 1713 |
llvm::Triple::ArchType arch) { |
| 1714 |
// When compiling in HipStdPar mode we have to be conservative in rejecting |
| 1715 |
// target specific features in the FE, and defer the possible error to the |
| 1716 |
// AcceleratorCodeSelection pass, wherein iff an unsupported target builtin is |
| 1717 |
// referenced by an accelerator executable function, we emit an error. |
| 1718 |
// Returning nullptr here leads to the builtin being handled in |
| 1719 |
// EmitStdParUnsupportedBuiltin. |
| 1720 |
if (cgf->getLangOpts().HIPStdPar && cgf->getLangOpts().CUDAIsDevice && |
| 1721 |
arch != cgf->getTarget().getTriple().getArch()) |
| 1722 |
return std::nullopt; |
| 1723 |
|
| 1724 |
switch (arch) { |
| 1725 |
case llvm::Triple::arm: |
| 1726 |
case llvm::Triple::armeb: |
| 1727 |
case llvm::Triple::thumb: |
| 1728 |
case llvm::Triple::thumbeb: |
| 1729 |
// These are actually NYI, but that will be reported by emitBuiltinExpr. |
| 1730 |
// At this point, we don't even know that the builtin is target-specific. |
| 1731 |
return std::nullopt; |
| 1732 |
case llvm::Triple::aarch64: |
| 1733 |
case llvm::Triple::aarch64_32: |
| 1734 |
case llvm::Triple::aarch64_be: |
| 1735 |
return cgf->emitAArch64BuiltinExpr(builtinID, e, returnValue, arch); |
| 1736 |
case llvm::Triple::bpfeb: |
| 1737 |
case llvm::Triple::bpfel: |
| 1738 |
// These are actually NYI, but that will be reported by emitBuiltinExpr. |
| 1739 |
// At this point, we don't even know that the builtin is target-specific. |
| 1740 |
return std::nullopt; |
| 1741 |
|
| 1742 |
case llvm::Triple::x86: |
| 1743 |
case llvm::Triple::x86_64: |
| 1744 |
return cgf->emitX86BuiltinExpr(builtinID, e); |
| 1745 |
|
| 1746 |
case llvm::Triple::ppc: |
| 1747 |
case llvm::Triple::ppcle: |
| 1748 |
case llvm::Triple::ppc64: |
| 1749 |
case llvm::Triple::ppc64le: |
| 1750 |
case llvm::Triple::r600: |
| 1751 |
case llvm::Triple::amdgcn: |
| 1752 |
case llvm::Triple::systemz: |
| 1753 |
case llvm::Triple::nvptx: |
| 1754 |
case llvm::Triple::nvptx64: |
| 1755 |
case llvm::Triple::wasm32: |
| 1756 |
case llvm::Triple::wasm64: |
| 1757 |
case llvm::Triple::hexagon: |
| 1758 |
case llvm::Triple::riscv32: |
| 1759 |
case llvm::Triple::riscv64: |
| 1760 |
// These are actually NYI, but that will be reported by emitBuiltinExpr. |
| 1761 |
// At this point, we don't even know that the builtin is target-specific. |
| 1762 |
return std::nullopt; |
| 1763 |
default: |
| 1764 |
return std::nullopt; |
| 1765 |
} |
| 1766 |
} |
| 1767 |
|
| 1768 |
std::optional<mlir::Value> |
| 1769 |
CIRGenFunction::emitTargetBuiltinExpr(unsigned builtinID, const CallExpr *e, |
| 1770 |
ReturnValueSlot &returnValue) { |
| 1771 |
if (getContext().BuiltinInfo.isAuxBuiltinID(builtinID)) { |
| 1772 |
assert(getContext().getAuxTargetInfo() && "Missing aux target info"); |
| 1773 |
return emitTargetArchBuiltinExpr( |
| 1774 |
this, getContext().BuiltinInfo.getAuxBuiltinID(builtinID), e, |
| 1775 |
returnValue, getContext().getAuxTargetInfo()->getTriple().getArch()); |
| 1776 |
} |
| 1777 |
|
| 1778 |
return emitTargetArchBuiltinExpr(this, builtinID, e, returnValue, |
| 1779 |
getTarget().getTriple().getArch()); |
| 1780 |
} |
| 1781 |
|
| 1782 |
mlir::Value CIRGenFunction::emitScalarOrConstFoldImmArg( |
| 1783 |
const unsigned iceArguments, const unsigned idx, const Expr *argExpr) { |
| 1784 |
mlir::Value arg = {}; |
| 1785 |
if ((iceArguments & (1 << idx)) == 0) { |
| 1786 |
arg = emitScalarExpr(argExpr); |
| 1787 |
} else { |
| 1788 |
// If this is required to be a constant, constant fold it so that we |
| 1789 |
// know that the generated intrinsic gets a ConstantInt. |
| 1790 |
const std::optional<llvm::APSInt> result = |
| 1791 |
argExpr->getIntegerConstantExpr(getContext()); |
| 1792 |
assert(result && "Expected argument to be a constant"); |
| 1793 |
arg = builder.getConstInt(getLoc(argExpr->getSourceRange()), *result); |
| 1794 |
} |
| 1795 |
return arg; |
| 1796 |
} |
| 1797 |
|
| 1798 |
/// Given a builtin id for a function like "__builtin_fabsf", return a Function* |
| 1799 |
/// for "fabsf". |
| 1800 |
cir::FuncOp CIRGenModule::getBuiltinLibFunction(const FunctionDecl *fd, |
| 1801 |
unsigned builtinID) { |
| 1802 |
assert(astContext.BuiltinInfo.isLibFunction(builtinID)); |
| 1803 |
|
| 1804 |
// Get the name, skip over the __builtin_ prefix (if necessary). We may have |
| 1805 |
// to build this up so provide a small stack buffer to handle the vast |
| 1806 |
// majority of names. |
| 1807 |
llvm::SmallString<64> name; |
| 1808 |
|
| 1809 |
assert(!cir::MissingFeatures::asmLabelAttr()); |
| 1810 |
name = astContext.BuiltinInfo.getName(builtinID).substr(10); |
| 1811 |
|
| 1812 |
GlobalDecl d(fd); |
| 1813 |
mlir::Type type = convertType(fd->getType()); |
| 1814 |
return getOrCreateCIRFunction(name, type, d, /*forVTable=*/false); |
| 1815 |
} |
| 1816 |
|
| 1817 |
mlir::Value CIRGenFunction::emitCheckedArgForAssume(const Expr *e) { |
| 1818 |
mlir::Value argValue = evaluateExprAsBool(e); |
| 1819 |
if (!sanOpts.has(SanitizerKind::Builtin)) |
| 1820 |
return argValue; |
| 1821 |
|
| 1822 |
assert(!cir::MissingFeatures::sanitizers()); |
| 1823 |
cgm.errorNYI(e->getSourceRange(), |
| 1824 |
"emitCheckedArgForAssume: sanitizers are NYI"); |
| 1825 |
return {}; |
| 1826 |
} |
| 1827 |
|
| 1828 |
void CIRGenFunction::emitVAStart(mlir::Value vaList, mlir::Value count) { |
| 1829 |
// LLVM codegen casts to *i8, no real gain on doing this for CIRGen this |
| 1830 |
// early, defer to LLVM lowering. |
| 1831 |
cir::VAStartOp::create(builder, vaList.getLoc(), vaList, count); |
| 1832 |
} |
| 1833 |
|
| 1834 |
void CIRGenFunction::emitVAEnd(mlir::Value vaList) { |
| 1835 |
cir::VAEndOp::create(builder, vaList.getLoc(), vaList); |
| 1836 |
} |
| 1837 |
|
| 1838 |
// FIXME(cir): This completely abstracts away the ABI with a generic CIR Op. By |
| 1839 |
// default this lowers to llvm.va_arg which is incomplete and not ABI-compliant |
| 1840 |
// on most targets so cir.va_arg will need some ABI handling in LoweringPrepare |
| 1841 |
mlir::Value CIRGenFunction::emitVAArg(VAArgExpr *ve) { |
| 1842 |
assert(!cir::MissingFeatures::msabi()); |
| 1843 |
assert(!cir::MissingFeatures::vlas()); |
| 1844 |
mlir::Location loc = cgm.getLoc(ve->getExprLoc()); |
| 1845 |
mlir::Type type = convertType(ve->getType()); |
| 1846 |
mlir::Value vaList = emitVAListRef(ve->getSubExpr()).getPointer(); |
| 1847 |
return cir::VAArgOp::create(builder, loc, type, vaList); |
| 1848 |
} |
| 1849 |
|
| 1850 |
mlir::Value CIRGenFunction::emitBuiltinObjectSize(const Expr *e, unsigned type, |
| 1851 |
cir::IntType resType, |
| 1852 |
mlir::Value emittedE, |
| 1853 |
bool isDynamic) { |
| 1854 |
assert(!cir::MissingFeatures::opCallImplicitObjectSizeArgs()); |
| 1855 |
|
| 1856 |
// LLVM can't handle type=3 appropriately, and __builtin_object_size shouldn't |
| 1857 |
// evaluate e for side-effects. In either case, just like original LLVM |
| 1858 |
// lowering, we shouldn't lower to `cir.objsize` but to a constant instead. |
| 1859 |
if (type == 3 || (!emittedE && e->HasSideEffects(getContext()))) |
| 1860 |
return builder.getConstInt(getLoc(e->getSourceRange()), resType, |
| 1861 |
(type & 2) ? 0 : -1); |
| 1862 |
|
| 1863 |
mlir::Value ptr = emittedE ? emittedE : emitScalarExpr(e); |
| 1864 |
assert(mlir::isa<cir::PointerType>(ptr.getType()) && |
| 1865 |
"Non-pointer passed to __builtin_object_size?"); |
| 1866 |
|
| 1867 |
assert(!cir::MissingFeatures::countedBySize()); |
| 1868 |
|
| 1869 |
// Extract the min/max mode from type. CIR only supports type 0 |
| 1870 |
// (max, whole object) and type 2 (min, whole object), not type 1 or 3 |
| 1871 |
// (closest subobject variants). |
| 1872 |
const bool min = ((type & 2) != 0); |
| 1873 |
// For GCC compatibility, __builtin_object_size treats NULL as unknown size. |
| 1874 |
auto op = |
| 1875 |
cir::ObjSizeOp::create(builder, getLoc(e->getSourceRange()), resType, ptr, |
| 1876 |
min, /*nullUnknown=*/true, isDynamic); |
| 1877 |
return op.getResult(); |
| 1878 |
} |
| 1879 |
|
| 1880 |
mlir::Value CIRGenFunction::evaluateOrEmitBuiltinObjectSize( |
| 1881 |
const Expr *e, unsigned type, cir::IntType resType, mlir::Value emittedE, |
| 1882 |
bool isDynamic) { |
| 1883 |
uint64_t objectSize; |
| 1884 |
if (!e->tryEvaluateObjectSize(objectSize, getContext(), type)) |
| 1885 |
return emitBuiltinObjectSize(e, type, resType, emittedE, isDynamic); |
| 1886 |
return builder.getConstInt(getLoc(e->getSourceRange()), resType, objectSize); |
| 1887 |
} |