| 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 |
#ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENBUILDER_H |
| 10 |
#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENBUILDER_H |
| 11 |
|
| 12 |
#include "Address.h" |
| 13 |
#include "CIRGenRecordLayout.h" |
| 14 |
#include "CIRGenTypeCache.h" |
| 15 |
#include "mlir/IR/Attributes.h" |
| 16 |
#include "mlir/IR/BuiltinAttributes.h" |
| 17 |
#include "mlir/Support/LLVM.h" |
| 18 |
#include "clang/CIR/Dialect/IR/CIRDataLayout.h" |
| 19 |
#include "clang/CIR/MissingFeatures.h" |
| 20 |
|
| 21 |
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h" |
| 22 |
#include "clang/CIR/MissingFeatures.h" |
| 23 |
#include "llvm/ADT/APFloat.h" |
| 24 |
#include "llvm/ADT/STLExtras.h" |
| 25 |
|
| 26 |
namespace clang::CIRGen { |
| 27 |
|
| 28 |
class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { |
| 29 |
const CIRGenTypeCache &typeCache; |
| 30 |
llvm::StringMap<unsigned> recordNames; |
| 31 |
llvm::StringMap<unsigned> globalsVersioning; |
| 32 |
|
| 33 |
public: |
| 34 |
CIRGenBuilderTy(mlir::MLIRContext &mlirContext, const CIRGenTypeCache &tc) |
| 35 |
: CIRBaseBuilderTy(mlirContext), typeCache(tc) {} |
| 36 |
|
| 37 |
/// Get a cir::ConstArrayAttr for a string literal. |
| 38 |
/// Note: This is different from what is returned by |
| 39 |
/// mlir::Builder::getStringAttr() which is an mlir::StringAttr. |
| 40 |
mlir::Attribute getString(llvm::StringRef str, mlir::Type eltTy, |
| 41 |
std::optional<size_t> size) { |
| 42 |
size_t finalSize = size.value_or(str.size()); |
| 43 |
|
| 44 |
size_t lastNonZeroPos = str.find_last_not_of('\0'); |
| 45 |
// If the string is full of null bytes, emit a #cir.zero rather than |
| 46 |
// a #cir.const_array. |
| 47 |
if (lastNonZeroPos == llvm::StringRef::npos) { |
| 48 |
auto arrayTy = cir::ArrayType::get(eltTy, finalSize); |
| 49 |
return cir::ZeroAttr::get(arrayTy); |
| 50 |
} |
| 51 |
// We emit trailing zeros only if there are multiple trailing zeros. |
| 52 |
size_t trailingZerosNum = 0; |
| 53 |
if (finalSize > lastNonZeroPos + 2) |
| 54 |
trailingZerosNum = finalSize - lastNonZeroPos - 1; |
| 55 |
auto truncatedArrayTy = |
| 56 |
cir::ArrayType::get(eltTy, finalSize - trailingZerosNum); |
| 57 |
auto fullArrayTy = cir::ArrayType::get(eltTy, finalSize); |
| 58 |
return cir::ConstArrayAttr::get( |
| 59 |
fullArrayTy, |
| 60 |
mlir::StringAttr::get(str.drop_back(trailingZerosNum), |
| 61 |
truncatedArrayTy), |
| 62 |
trailingZerosNum); |
| 63 |
} |
| 64 |
|
| 65 |
cir::ConstArrayAttr getConstArray(mlir::Attribute attrs, |
| 66 |
cir::ArrayType arrayTy) const { |
| 67 |
return cir::ConstArrayAttr::get(arrayTy, attrs); |
| 68 |
} |
| 69 |
|
| 70 |
mlir::Attribute getConstRecordOrZeroAttr(mlir::ArrayAttr arrayAttr, |
| 71 |
bool packed = false, |
| 72 |
bool padded = false, |
| 73 |
mlir::Type type = {}); |
| 74 |
|
| 75 |
cir::ConstRecordAttr getAnonConstRecord(mlir::ArrayAttr arrayAttr, |
| 76 |
bool packed = false, |
| 77 |
bool padded = false, |
| 78 |
mlir::Type ty = {}) { |
| 79 |
llvm::SmallVector<mlir::Type, 4> members; |
| 80 |
for (auto &f : arrayAttr) { |
| 81 |
auto ta = mlir::cast<mlir::TypedAttr>(f); |
| 82 |
members.push_back(ta.getType()); |
| 83 |
} |
| 84 |
|
| 85 |
if (!ty) |
| 86 |
ty = getAnonRecordTy(members, packed, padded); |
| 87 |
|
| 88 |
auto sTy = mlir::cast<cir::RecordType>(ty); |
| 89 |
return cir::ConstRecordAttr::get(sTy, arrayAttr); |
| 90 |
} |
| 91 |
|
| 92 |
cir::TypeInfoAttr getTypeInfo(mlir::ArrayAttr fieldsAttr) { |
| 93 |
cir::ConstRecordAttr anonRecord = getAnonConstRecord(fieldsAttr); |
| 94 |
return cir::TypeInfoAttr::get(anonRecord.getType(), fieldsAttr); |
| 95 |
} |
| 96 |
|
| 97 |
std::string getUniqueAnonRecordName() { return getUniqueRecordName("anon"); } |
| 98 |
|
| 99 |
std::string getUniqueRecordName(const std::string &baseName) { |
| 100 |
auto it = recordNames.find(baseName); |
| 101 |
if (it == recordNames.end()) { |
| 102 |
recordNames[baseName] = 0; |
| 103 |
return baseName; |
| 104 |
} |
| 105 |
|
| 106 |
return baseName + "." + std::to_string(recordNames[baseName]++); |
| 107 |
} |
| 108 |
|
| 109 |
cir::LongDoubleType getLongDoubleTy(const llvm::fltSemantics &format) const { |
| 110 |
if (&format == &llvm::APFloat::IEEEdouble()) |
| 111 |
return cir::LongDoubleType::get(getContext(), typeCache.doubleTy); |
| 112 |
if (&format == &llvm::APFloat::x87DoubleExtended()) |
| 113 |
return cir::LongDoubleType::get(getContext(), typeCache.fP80Ty); |
| 114 |
if (&format == &llvm::APFloat::IEEEquad()) |
| 115 |
return cir::LongDoubleType::get(getContext(), typeCache.fP128Ty); |
| 116 |
if (&format == &llvm::APFloat::PPCDoubleDouble()) |
| 117 |
llvm_unreachable("NYI: PPC double-double format for long double"); |
| 118 |
llvm_unreachable("Unsupported format for long double"); |
| 119 |
} |
| 120 |
|
| 121 |
mlir::Type getPtrToVPtrType() { |
| 122 |
return getPointerTo(cir::VPtrType::get(getContext())); |
| 123 |
} |
| 124 |
|
| 125 |
cir::FuncType getFuncType(llvm::ArrayRef<mlir::Type> params, mlir::Type retTy, |
| 126 |
bool isVarArg = false) { |
| 127 |
return cir::FuncType::get(params, retTy, isVarArg); |
| 128 |
} |
| 129 |
|
| 130 |
/// Get a CIR record kind from a AST declaration tag. |
| 131 |
cir::RecordType::RecordKind getRecordKind(const clang::TagTypeKind kind) { |
| 132 |
switch (kind) { |
| 133 |
case clang::TagTypeKind::Class: |
| 134 |
return cir::RecordType::Class; |
| 135 |
case clang::TagTypeKind::Struct: |
| 136 |
return cir::RecordType::Struct; |
| 137 |
case clang::TagTypeKind::Union: |
| 138 |
return cir::RecordType::Union; |
| 139 |
case clang::TagTypeKind::Interface: |
| 140 |
llvm_unreachable("interface records are NYI"); |
| 141 |
case clang::TagTypeKind::Enum: |
| 142 |
llvm_unreachable("enums are not records"); |
| 143 |
} |
| 144 |
llvm_unreachable("Unsupported record kind"); |
| 145 |
} |
| 146 |
|
| 147 |
/// Get a CIR named record type. |
| 148 |
/// |
| 149 |
/// If a record already exists and is complete, but the client tries to fetch |
| 150 |
/// it with a different set of attributes, this method will crash. |
| 151 |
cir::RecordType getCompleteNamedRecordType(llvm::ArrayRef<mlir::Type> members, |
| 152 |
bool packed, bool padded, |
| 153 |
llvm::StringRef name) { |
| 154 |
const auto nameAttr = getStringAttr(name); |
| 155 |
auto kind = cir::RecordType::RecordKind::Struct; |
| 156 |
assert(!cir::MissingFeatures::astRecordDeclAttr()); |
| 157 |
|
| 158 |
// Create or get the record. |
| 159 |
auto type = |
| 160 |
getType<cir::RecordType>(members, nameAttr, packed, padded, kind); |
| 161 |
|
| 162 |
// If we found an existing type, verify that either it is incomplete or |
| 163 |
// it matches the requested attributes. |
| 164 |
assert(!type.isIncomplete() || |
| 165 |
(type.getMembers() == members && type.getPacked() == packed && |
| 166 |
type.getPadded() == padded)); |
| 167 |
|
| 168 |
// Complete an incomplete record or ensure the existing complete record |
| 169 |
// matches the requested attributes. |
| 170 |
type.complete(members, packed, padded); |
| 171 |
|
| 172 |
return type; |
| 173 |
} |
| 174 |
|
| 175 |
cir::RecordType getCompleteRecordType(mlir::ArrayAttr fields, |
| 176 |
bool packed = false, |
| 177 |
bool padded = false, |
| 178 |
llvm::StringRef name = ""); |
| 179 |
|
| 180 |
/// Get an incomplete CIR struct type. If we have a complete record |
| 181 |
/// declaration, we may create an incomplete type and then add the |
| 182 |
/// members, so \p rd here may be complete. |
| 183 |
cir::RecordType getIncompleteRecordTy(llvm::StringRef name, |
| 184 |
const clang::RecordDecl *rd) { |
| 185 |
const mlir::StringAttr nameAttr = getStringAttr(name); |
| 186 |
cir::RecordType::RecordKind kind = cir::RecordType::RecordKind::Struct; |
| 187 |
if (rd) |
| 188 |
kind = getRecordKind(rd->getTagKind()); |
| 189 |
return getType<cir::RecordType>(nameAttr, kind); |
| 190 |
} |
| 191 |
|
| 192 |
cir::DataMemberAttr getDataMemberAttr(cir::DataMemberType ty, |
| 193 |
unsigned memberIndex) { |
| 194 |
return cir::DataMemberAttr::get(ty, memberIndex); |
| 195 |
} |
| 196 |
|
| 197 |
cir::DataMemberAttr getNullDataMemberAttr(cir::DataMemberType ty) { |
| 198 |
return cir::DataMemberAttr::get(ty); |
| 199 |
} |
| 200 |
|
| 201 |
// Return true if the value is a null constant such as null pointer, (+0.0) |
| 202 |
// for floating-point or zero initializer |
| 203 |
bool isNullValue(mlir::Attribute attr) const { |
| 204 |
if (mlir::isa<cir::ZeroAttr>(attr)) |
| 205 |
return true; |
| 206 |
|
| 207 |
if (const auto ptrVal = mlir::dyn_cast<cir::ConstPtrAttr>(attr)) |
| 208 |
return ptrVal.isNullValue(); |
| 209 |
|
| 210 |
if (const auto intVal = mlir::dyn_cast<cir::IntAttr>(attr)) |
| 211 |
return intVal.isNullValue(); |
| 212 |
|
| 213 |
if (const auto boolVal = mlir::dyn_cast<cir::BoolAttr>(attr)) |
| 214 |
return !boolVal.getValue(); |
| 215 |
|
| 216 |
if (auto fpAttr = mlir::dyn_cast<cir::FPAttr>(attr)) { |
| 217 |
auto fpVal = fpAttr.getValue(); |
| 218 |
bool ignored; |
| 219 |
llvm::APFloat fv(+0.0); |
| 220 |
fv.convert(fpVal.getSemantics(), llvm::APFloat::rmNearestTiesToEven, |
| 221 |
&ignored); |
| 222 |
return fv.bitwiseIsEqual(fpVal); |
| 223 |
} |
| 224 |
if (const auto recordVal = mlir::dyn_cast<cir::ConstRecordAttr>(attr)) { |
| 225 |
for (const auto elt : recordVal.getMembers()) { |
| 226 |
// FIXME(cir): the record's ID should not be considered a member. |
| 227 |
if (mlir::isa<mlir::StringAttr>(elt)) |
| 228 |
continue; |
| 229 |
if (!isNullValue(elt)) |
| 230 |
return false; |
| 231 |
} |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
if (const auto arrayVal = mlir::dyn_cast<cir::ConstArrayAttr>(attr)) { |
| 236 |
if (mlir::isa<mlir::StringAttr>(arrayVal.getElts())) |
| 237 |
return false; |
| 238 |
|
| 239 |
return llvm::all_of( |
| 240 |
mlir::cast<mlir::ArrayAttr>(arrayVal.getElts()), |
| 241 |
[&](const mlir::Attribute &elt) { return isNullValue(elt); }); |
| 242 |
} |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
// |
| 247 |
// Type helpers |
| 248 |
// ------------ |
| 249 |
// |
| 250 |
cir::IntType getUIntNTy(int n) { |
| 251 |
switch (n) { |
| 252 |
case 8: |
| 253 |
return getUInt8Ty(); |
| 254 |
case 16: |
| 255 |
return getUInt16Ty(); |
| 256 |
case 32: |
| 257 |
return getUInt32Ty(); |
| 258 |
case 64: |
| 259 |
return getUInt64Ty(); |
| 260 |
default: |
| 261 |
return cir::IntType::get(getContext(), n, false); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
cir::IntType getSIntNTy(int n) { |
| 266 |
switch (n) { |
| 267 |
case 8: |
| 268 |
return getSInt8Ty(); |
| 269 |
case 16: |
| 270 |
return getSInt16Ty(); |
| 271 |
case 32: |
| 272 |
return getSInt32Ty(); |
| 273 |
case 64: |
| 274 |
return getSInt64Ty(); |
| 275 |
default: |
| 276 |
return cir::IntType::get(getContext(), n, true); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
cir::VoidType getVoidTy() { return typeCache.voidTy; } |
| 281 |
|
| 282 |
cir::IntType getSInt8Ty() { return typeCache.sInt8Ty; } |
| 283 |
cir::IntType getSInt16Ty() { return typeCache.sInt16Ty; } |
| 284 |
cir::IntType getSInt32Ty() { return typeCache.sInt32Ty; } |
| 285 |
cir::IntType getSInt64Ty() { return typeCache.sInt64Ty; } |
| 286 |
|
| 287 |
cir::IntType getUInt8Ty() { return typeCache.uInt8Ty; } |
| 288 |
cir::IntType getUInt16Ty() { return typeCache.uInt16Ty; } |
| 289 |
cir::IntType getUInt32Ty() { return typeCache.uInt32Ty; } |
| 290 |
cir::IntType getUInt64Ty() { return typeCache.uInt64Ty; } |
| 291 |
|
| 292 |
cir::FP16Type getFp16Ty() { return typeCache.fP16Ty; } |
| 293 |
cir::BF16Type getBfloat6Ty() { return typeCache.bFloat16Ty; } |
| 294 |
cir::SingleType getSingleTy() { return typeCache.floatTy; } |
| 295 |
cir::DoubleType getDoubleTy() { return typeCache.doubleTy; } |
| 296 |
|
| 297 |
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal); |
| 298 |
|
| 299 |
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APInt intVal, |
| 300 |
bool isUnsigned = true); |
| 301 |
|
| 302 |
cir::ConstantOp getConstInt(mlir::Location loc, mlir::Type t, uint64_t c); |
| 303 |
|
| 304 |
cir::ConstantOp getConstFP(mlir::Location loc, mlir::Type t, |
| 305 |
llvm::APFloat fpVal); |
| 306 |
|
| 307 |
bool isInt8Ty(mlir::Type i) { |
| 308 |
return i == typeCache.uInt8Ty || i == typeCache.sInt8Ty; |
| 309 |
} |
| 310 |
bool isInt16Ty(mlir::Type i) { |
| 311 |
return i == typeCache.uInt16Ty || i == typeCache.sInt16Ty; |
| 312 |
} |
| 313 |
bool isInt32Ty(mlir::Type i) { |
| 314 |
return i == typeCache.uInt32Ty || i == typeCache.sInt32Ty; |
| 315 |
} |
| 316 |
bool isInt64Ty(mlir::Type i) { |
| 317 |
return i == typeCache.uInt64Ty || i == typeCache.sInt64Ty; |
| 318 |
} |
| 319 |
bool isInt(mlir::Type i) { return mlir::isa<cir::IntType>(i); } |
| 320 |
|
| 321 |
// Fetch the type representing a pointer to unsigned int8 values. |
| 322 |
cir::PointerType getUInt8PtrTy() { return typeCache.uInt8PtrTy; } |
| 323 |
|
| 324 |
/// Get a CIR anonymous record type. |
| 325 |
cir::RecordType getAnonRecordTy(llvm::ArrayRef<mlir::Type> members, |
| 326 |
bool packed = false, bool padded = false) { |
| 327 |
assert(!cir::MissingFeatures::astRecordDeclAttr()); |
| 328 |
auto kind = cir::RecordType::RecordKind::Struct; |
| 329 |
return getType<cir::RecordType>(members, packed, padded, kind); |
| 330 |
} |
| 331 |
|
| 332 |
// |
| 333 |
// Constant creation helpers |
| 334 |
// ------------------------- |
| 335 |
// |
| 336 |
cir::ConstantOp getSInt32(int32_t c, mlir::Location loc) { |
| 337 |
return getConstantInt(loc, getSInt32Ty(), c); |
| 338 |
} |
| 339 |
cir::ConstantOp getUInt32(uint32_t c, mlir::Location loc) { |
| 340 |
return getConstantInt(loc, getUInt32Ty(), c); |
| 341 |
} |
| 342 |
cir::ConstantOp getSInt64(uint64_t c, mlir::Location loc) { |
| 343 |
return getConstantInt(loc, getSInt64Ty(), c); |
| 344 |
} |
| 345 |
cir::ConstantOp getUInt64(uint64_t c, mlir::Location loc) { |
| 346 |
return getConstantInt(loc, getUInt64Ty(), c); |
| 347 |
} |
| 348 |
|
| 349 |
mlir::Value createNeg(mlir::Value value) { |
| 350 |
|
| 351 |
if (auto intTy = mlir::dyn_cast<cir::IntType>(value.getType())) { |
| 352 |
// Source is a unsigned integer: first cast it to signed. |
| 353 |
if (intTy.isUnsigned()) |
| 354 |
value = createIntCast(value, getSIntNTy(intTy.getWidth())); |
| 355 |
return cir::UnaryOp::create(*this, value.getLoc(), value.getType(), |
| 356 |
cir::UnaryOpKind::Minus, value); |
| 357 |
} |
| 358 |
|
| 359 |
llvm_unreachable("negation for the given type is NYI"); |
| 360 |
} |
| 361 |
|
| 362 |
cir::IsFPClassOp createIsFPClass(mlir::Location loc, mlir::Value src, |
| 363 |
cir::FPClassTest flags) { |
| 364 |
return cir::IsFPClassOp::create(*this, loc, src, flags); |
| 365 |
} |
| 366 |
|
| 367 |
/// Create constant nullptr for pointer-to-data-member type ty. |
| 368 |
cir::ConstantOp getNullDataMemberPtr(cir::DataMemberType ty, |
| 369 |
mlir::Location loc) { |
| 370 |
return cir::ConstantOp::create(*this, loc, getNullDataMemberAttr(ty)); |
| 371 |
} |
| 372 |
|
| 373 |
// TODO: split this to createFPExt/createFPTrunc when we have dedicated cast |
| 374 |
// operations. |
| 375 |
mlir::Value createFloatingCast(mlir::Value v, mlir::Type destType) { |
| 376 |
assert(!cir::MissingFeatures::fpConstraints()); |
| 377 |
|
| 378 |
return cir::CastOp::create(*this, v.getLoc(), destType, |
| 379 |
cir::CastKind::floating, v); |
| 380 |
} |
| 381 |
|
| 382 |
mlir::Value createFSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) { |
| 383 |
assert(!cir::MissingFeatures::metaDataNode()); |
| 384 |
assert(!cir::MissingFeatures::fpConstraints()); |
| 385 |
assert(!cir::MissingFeatures::fastMathFlags()); |
| 386 |
|
| 387 |
return cir::BinOp::create(*this, loc, cir::BinOpKind::Sub, lhs, rhs); |
| 388 |
} |
| 389 |
|
| 390 |
mlir::Value createFAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) { |
| 391 |
assert(!cir::MissingFeatures::metaDataNode()); |
| 392 |
assert(!cir::MissingFeatures::fpConstraints()); |
| 393 |
assert(!cir::MissingFeatures::fastMathFlags()); |
| 394 |
|
| 395 |
return cir::BinOp::create(*this, loc, cir::BinOpKind::Add, lhs, rhs); |
| 396 |
} |
| 397 |
mlir::Value createFMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) { |
| 398 |
assert(!cir::MissingFeatures::metaDataNode()); |
| 399 |
assert(!cir::MissingFeatures::fpConstraints()); |
| 400 |
assert(!cir::MissingFeatures::fastMathFlags()); |
| 401 |
|
| 402 |
return cir::BinOp::create(*this, loc, cir::BinOpKind::Mul, lhs, rhs); |
| 403 |
} |
| 404 |
mlir::Value createFDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) { |
| 405 |
assert(!cir::MissingFeatures::metaDataNode()); |
| 406 |
assert(!cir::MissingFeatures::fpConstraints()); |
| 407 |
assert(!cir::MissingFeatures::fastMathFlags()); |
| 408 |
|
| 409 |
return cir::BinOp::create(*this, loc, cir::BinOpKind::Div, lhs, rhs); |
| 410 |
} |
| 411 |
|
| 412 |
mlir::Value createDynCast(mlir::Location loc, mlir::Value src, |
| 413 |
cir::PointerType destType, bool isRefCast, |
| 414 |
cir::DynamicCastInfoAttr info) { |
| 415 |
auto castKind = |
| 416 |
isRefCast ? cir::DynamicCastKind::Ref : cir::DynamicCastKind::Ptr; |
| 417 |
return cir::DynamicCastOp::create(*this, loc, destType, castKind, src, info, |
| 418 |
/*relative_layout=*/false); |
| 419 |
} |
| 420 |
|
| 421 |
mlir::Value createDynCastToVoid(mlir::Location loc, mlir::Value src, |
| 422 |
bool vtableUseRelativeLayout) { |
| 423 |
// TODO(cir): consider address space here. |
| 424 |
assert(!cir::MissingFeatures::addressSpace()); |
| 425 |
cir::PointerType destTy = getVoidPtrTy(); |
| 426 |
return cir::DynamicCastOp::create( |
| 427 |
*this, loc, destTy, cir::DynamicCastKind::Ptr, src, |
| 428 |
cir::DynamicCastInfoAttr{}, vtableUseRelativeLayout); |
| 429 |
} |
| 430 |
|
| 431 |
Address createBaseClassAddr(mlir::Location loc, Address addr, |
| 432 |
mlir::Type destType, unsigned offset, |
| 433 |
bool assumeNotNull) { |
| 434 |
if (destType == addr.getElementType()) |
| 435 |
return addr; |
| 436 |
|
| 437 |
auto ptrTy = getPointerTo(destType); |
| 438 |
auto baseAddr = |
| 439 |
cir::BaseClassAddrOp::create(*this, loc, ptrTy, addr.getPointer(), |
| 440 |
mlir::APInt(64, offset), assumeNotNull); |
| 441 |
return Address(baseAddr, destType, addr.getAlignment()); |
| 442 |
} |
| 443 |
|
| 444 |
Address createDerivedClassAddr(mlir::Location loc, Address addr, |
| 445 |
mlir::Type destType, unsigned offset, |
| 446 |
bool assumeNotNull) { |
| 447 |
if (destType == addr.getElementType()) |
| 448 |
return addr; |
| 449 |
|
| 450 |
cir::PointerType ptrTy = getPointerTo(destType); |
| 451 |
auto derivedAddr = |
| 452 |
cir::DerivedClassAddrOp::create(*this, loc, ptrTy, addr.getPointer(), |
| 453 |
mlir::APInt(64, offset), assumeNotNull); |
| 454 |
return Address(derivedAddr, destType, addr.getAlignment()); |
| 455 |
} |
| 456 |
|
| 457 |
mlir::Value createVTTAddrPoint(mlir::Location loc, mlir::Type retTy, |
| 458 |
mlir::Value addr, uint64_t offset) { |
| 459 |
return cir::VTTAddrPointOp::create(*this, loc, retTy, |
| 460 |
mlir::FlatSymbolRefAttr{}, addr, offset); |
| 461 |
} |
| 462 |
|
| 463 |
mlir::Value createVTTAddrPoint(mlir::Location loc, mlir::Type retTy, |
| 464 |
mlir::FlatSymbolRefAttr sym, uint64_t offset) { |
| 465 |
return cir::VTTAddrPointOp::create(*this, loc, retTy, sym, mlir::Value{}, |
| 466 |
offset); |
| 467 |
} |
| 468 |
|
| 469 |
/// Cast the element type of the given address to a different type, |
| 470 |
/// preserving information like the alignment. |
| 471 |
Address createElementBitCast(mlir::Location loc, Address addr, |
| 472 |
mlir::Type destType) { |
| 473 |
if (destType == addr.getElementType()) |
| 474 |
return addr; |
| 475 |
|
| 476 |
auto ptrTy = getPointerTo(destType); |
| 477 |
return Address(createBitcast(loc, addr.getPointer(), ptrTy), destType, |
| 478 |
addr.getAlignment()); |
| 479 |
} |
| 480 |
|
| 481 |
cir::LoadOp createLoad(mlir::Location loc, Address addr, |
| 482 |
bool isVolatile = false) { |
| 483 |
mlir::IntegerAttr align = getAlignmentAttr(addr.getAlignment()); |
| 484 |
return cir::LoadOp::create(*this, loc, addr.getPointer(), /*isDeref=*/false, |
| 485 |
isVolatile, /*alignment=*/align, |
| 486 |
/*sync_scope=*/cir::SyncScopeKindAttr{}, |
| 487 |
/*mem_order=*/cir::MemOrderAttr{}); |
| 488 |
} |
| 489 |
|
| 490 |
cir::LoadOp createAlignedLoad(mlir::Location loc, mlir::Type ty, |
| 491 |
mlir::Value ptr, llvm::MaybeAlign align) { |
| 492 |
if (ty != mlir::cast<cir::PointerType>(ptr.getType()).getPointee()) |
| 493 |
ptr = createPtrBitcast(ptr, ty); |
| 494 |
uint64_t alignment = align ? align->value() : 0; |
| 495 |
mlir::IntegerAttr alignAttr = getAlignmentAttr(alignment); |
| 496 |
return cir::LoadOp::create(*this, loc, ptr, /*isDeref=*/false, |
| 497 |
/*isVolatile=*/false, alignAttr, |
| 498 |
/*sync_scope=*/cir::SyncScopeKindAttr{}, |
| 499 |
/*mem_order=*/cir::MemOrderAttr{}); |
| 500 |
} |
| 501 |
|
| 502 |
cir::LoadOp |
| 503 |
createAlignedLoad(mlir::Location loc, mlir::Type ty, mlir::Value ptr, |
| 504 |
clang::CharUnits align = clang::CharUnits::One()) { |
| 505 |
return createAlignedLoad(loc, ty, ptr, align.getAsAlign()); |
| 506 |
} |
| 507 |
|
| 508 |
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, Address dst, |
| 509 |
bool isVolatile = false, |
| 510 |
mlir::IntegerAttr align = {}, |
| 511 |
cir::SyncScopeKindAttr scope = {}, |
| 512 |
cir::MemOrderAttr order = {}) { |
| 513 |
if (!align) |
| 514 |
align = getAlignmentAttr(dst.getAlignment()); |
| 515 |
return CIRBaseBuilderTy::createStore(loc, val, dst.getPointer(), isVolatile, |
| 516 |
align, scope, order); |
| 517 |
} |
| 518 |
|
| 519 |
/// Create a cir.complex.real_ptr operation that derives a pointer to the real |
| 520 |
/// part of the complex value pointed to by the specified pointer value. |
| 521 |
mlir::Value createComplexRealPtr(mlir::Location loc, mlir::Value value) { |
| 522 |
auto srcPtrTy = mlir::cast<cir::PointerType>(value.getType()); |
| 523 |
auto srcComplexTy = mlir::cast<cir::ComplexType>(srcPtrTy.getPointee()); |
| 524 |
return cir::ComplexRealPtrOp::create( |
| 525 |
*this, loc, getPointerTo(srcComplexTy.getElementType()), value); |
| 526 |
} |
| 527 |
|
| 528 |
Address createComplexRealPtr(mlir::Location loc, Address addr) { |
| 529 |
return Address{createComplexRealPtr(loc, addr.getPointer()), |
| 530 |
addr.getAlignment()}; |
| 531 |
} |
| 532 |
|
| 533 |
/// Create a cir.complex.imag_ptr operation that derives a pointer to the |
| 534 |
/// imaginary part of the complex value pointed to by the specified pointer |
| 535 |
/// value. |
| 536 |
mlir::Value createComplexImagPtr(mlir::Location loc, mlir::Value value) { |
| 537 |
auto srcPtrTy = mlir::cast<cir::PointerType>(value.getType()); |
| 538 |
auto srcComplexTy = mlir::cast<cir::ComplexType>(srcPtrTy.getPointee()); |
| 539 |
return cir::ComplexImagPtrOp::create( |
| 540 |
*this, loc, getPointerTo(srcComplexTy.getElementType()), value); |
| 541 |
} |
| 542 |
|
| 543 |
Address createComplexImagPtr(mlir::Location loc, Address addr) { |
| 544 |
return Address{createComplexImagPtr(loc, addr.getPointer()), |
| 545 |
addr.getAlignment()}; |
| 546 |
} |
| 547 |
|
| 548 |
cir::GetRuntimeMemberOp createGetIndirectMember(mlir::Location loc, |
| 549 |
mlir::Value objectPtr, |
| 550 |
mlir::Value memberPtr) { |
| 551 |
auto memberPtrTy = mlir::cast<cir::DataMemberType>(memberPtr.getType()); |
| 552 |
|
| 553 |
// TODO(cir): consider address space. |
| 554 |
assert(!cir::MissingFeatures::addressSpace()); |
| 555 |
cir::PointerType resultTy = getPointerTo(memberPtrTy.getMemberTy()); |
| 556 |
|
| 557 |
return cir::GetRuntimeMemberOp::create(*this, loc, resultTy, objectPtr, |
| 558 |
memberPtr); |
| 559 |
} |
| 560 |
|
| 561 |
/// Create a cir.ptr_stride operation to get access to an array element. |
| 562 |
/// \p idx is the index of the element to access, \p shouldDecay is true if |
| 563 |
/// the result should decay to a pointer to the element type. |
| 564 |
mlir::Value getArrayElement(mlir::Location arrayLocBegin, |
| 565 |
mlir::Location arrayLocEnd, mlir::Value arrayPtr, |
| 566 |
mlir::Type eltTy, mlir::Value idx, |
| 567 |
bool shouldDecay); |
| 568 |
|
| 569 |
/// Returns a decayed pointer to the first element of the array |
| 570 |
/// pointed to by \p arrayPtr. |
| 571 |
mlir::Value maybeBuildArrayDecay(mlir::Location loc, mlir::Value arrayPtr, |
| 572 |
mlir::Type eltTy); |
| 573 |
|
| 574 |
// Convert byte offset to sequence of high-level indices suitable for |
| 575 |
// GlobalViewAttr. Ideally we shouldn't deal with low-level offsets at all |
| 576 |
// but currently some parts of Clang AST, which we don't want to touch just |
| 577 |
// yet, return them. |
| 578 |
void computeGlobalViewIndicesFromFlatOffset( |
| 579 |
int64_t offset, mlir::Type ty, cir::CIRDataLayout layout, |
| 580 |
llvm::SmallVectorImpl<int64_t> &indices); |
| 581 |
|
| 582 |
/// Creates a versioned global variable. If the symbol is already taken, an ID |
| 583 |
/// will be appended to the symbol. The returned global must always be queried |
| 584 |
/// for its name so it can be referenced correctly. |
| 585 |
[[nodiscard]] cir::GlobalOp |
| 586 |
createVersionedGlobal(mlir::ModuleOp module, mlir::Location loc, |
| 587 |
mlir::StringRef name, mlir::Type type, bool isConstant, |
| 588 |
cir::GlobalLinkageKind linkage) { |
| 589 |
// Create a unique name if the given name is already taken. |
| 590 |
std::string uniqueName; |
| 591 |
if (unsigned version = globalsVersioning[name.str()]++) |
| 592 |
uniqueName = name.str() + "." + std::to_string(version); |
| 593 |
else |
| 594 |
uniqueName = name.str(); |
| 595 |
|
| 596 |
return createGlobal(module, loc, uniqueName, type, isConstant, linkage); |
| 597 |
} |
| 598 |
|
| 599 |
cir::StackSaveOp createStackSave(mlir::Location loc, mlir::Type ty) { |
| 600 |
return cir::StackSaveOp::create(*this, loc, ty); |
| 601 |
} |
| 602 |
|
| 603 |
cir::StackRestoreOp createStackRestore(mlir::Location loc, mlir::Value v) { |
| 604 |
return cir::StackRestoreOp::create(*this, loc, v); |
| 605 |
} |
| 606 |
|
| 607 |
mlir::Value createSetBitfield(mlir::Location loc, mlir::Type resultType, |
| 608 |
Address dstAddr, mlir::Type storageType, |
| 609 |
mlir::Value src, const CIRGenBitFieldInfo &info, |
| 610 |
bool isLvalueVolatile, bool useVolatile) { |
| 611 |
unsigned offset = useVolatile ? info.volatileOffset : info.offset; |
| 612 |
|
| 613 |
// If using AAPCS and the field is volatile, load with the size of the |
| 614 |
// declared field |
| 615 |
storageType = |
| 616 |
useVolatile ? cir::IntType::get(storageType.getContext(), |
| 617 |
info.volatileStorageSize, info.isSigned) |
| 618 |
: storageType; |
| 619 |
return cir::SetBitfieldOp::create( |
| 620 |
*this, loc, resultType, dstAddr.getPointer(), storageType, src, |
| 621 |
info.name, info.size, offset, info.isSigned, isLvalueVolatile, |
| 622 |
dstAddr.getAlignment().getAsAlign().value()); |
| 623 |
} |
| 624 |
|
| 625 |
mlir::Value createGetBitfield(mlir::Location loc, mlir::Type resultType, |
| 626 |
Address addr, mlir::Type storageType, |
| 627 |
const CIRGenBitFieldInfo &info, |
| 628 |
bool isLvalueVolatile, bool useVolatile) { |
| 629 |
unsigned offset = useVolatile ? info.volatileOffset : info.offset; |
| 630 |
|
| 631 |
// If using AAPCS and the field is volatile, load with the size of the |
| 632 |
// declared field |
| 633 |
storageType = |
| 634 |
useVolatile ? cir::IntType::get(storageType.getContext(), |
| 635 |
info.volatileStorageSize, info.isSigned) |
| 636 |
: storageType; |
| 637 |
return cir::GetBitfieldOp::create(*this, loc, resultType, addr.getPointer(), |
| 638 |
storageType, info.name, info.size, offset, |
| 639 |
info.isSigned, isLvalueVolatile, |
| 640 |
addr.getAlignment().getAsAlign().value()); |
| 641 |
} |
| 642 |
|
| 643 |
cir::VecShuffleOp |
| 644 |
createVecShuffle(mlir::Location loc, mlir::Value vec1, mlir::Value vec2, |
| 645 |
llvm::ArrayRef<mlir::Attribute> maskAttrs) { |
| 646 |
auto vecType = mlir::cast<cir::VectorType>(vec1.getType()); |
| 647 |
auto resultTy = |
| 648 |
cir::VectorType::get(vecType.getElementType(), maskAttrs.size()); |
| 649 |
return cir::VecShuffleOp::create(*this, loc, resultTy, vec1, vec2, |
| 650 |
getArrayAttr(maskAttrs)); |
| 651 |
} |
| 652 |
|
| 653 |
cir::VecShuffleOp createVecShuffle(mlir::Location loc, mlir::Value vec1, |
| 654 |
mlir::Value vec2, |
| 655 |
llvm::ArrayRef<int64_t> mask) { |
| 656 |
auto maskAttrs = llvm::to_vector_of<mlir::Attribute>( |
| 657 |
llvm::map_range(mask, [&](int32_t idx) { |
| 658 |
return cir::IntAttr::get(getSInt32Ty(), idx); |
| 659 |
})); |
| 660 |
return createVecShuffle(loc, vec1, vec2, maskAttrs); |
| 661 |
} |
| 662 |
|
| 663 |
cir::VecShuffleOp createVecShuffle(mlir::Location loc, mlir::Value vec1, |
| 664 |
llvm::ArrayRef<int64_t> mask) { |
| 665 |
/// Create a unary shuffle. The second vector operand of the IR instruction |
| 666 |
/// is poison. |
| 667 |
cir::ConstantOp poison = |
| 668 |
getConstant(loc, cir::PoisonAttr::get(vec1.getType())); |
| 669 |
return createVecShuffle(loc, vec1, poison, mask); |
| 670 |
} |
| 671 |
}; |
| 672 |
|
| 673 |
} // namespace clang::CIRGen |
| 674 |
|
| 675 |
#endif |