| 1 | //===- LLVMTypes.cpp - MLIR LLVM dialect types ------------------*- C++ -*-===// |
| 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 file implements the types for the LLVM dialect in MLIR. These MLIR types |
| 10 | // correspond to the LLVM IR type system. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "TypeDetail.h" |
| 15 | |
| 16 | #include "mlir/Dialect/LLVMIR/LLVMAttrs.h" |
| 17 | #include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h" |
| 18 | #include "mlir/Dialect/LLVMIR/LLVMTypes.h" |
| 19 | #include "mlir/IR/BuiltinTypes.h" |
| 20 | #include "mlir/IR/DialectImplementation.h" |
| 21 | #include "mlir/IR/TypeSupport.h" |
| 22 | |
| 23 | #include "llvm/ADT/TypeSwitch.h" |
| 24 | #include "llvm/Support/TypeSize.h" |
| 25 | #include <optional> |
| 26 | |
| 27 | using namespace mlir; |
| 28 | using namespace mlir::LLVM; |
| 29 | |
| 30 | constexpr const static uint64_t kBitsInByte = 8; |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // custom<FunctionTypes> |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | static ParseResult parseFunctionTypes(AsmParser &p, SmallVector<Type> ¶ms, |
| 37 | bool &isVarArg) { |
| 38 | isVarArg = false; |
| 39 | // `(` `)` |
| 40 | if (succeeded(Result: p.parseOptionalRParen())) |
| 41 | return success(); |
| 42 | |
| 43 | // `(` `...` `)` |
| 44 | if (succeeded(Result: p.parseOptionalEllipsis())) { |
| 45 | isVarArg = true; |
| 46 | return p.parseRParen(); |
| 47 | } |
| 48 | |
| 49 | // type (`,` type)* (`,` `...`)? |
| 50 | Type type; |
| 51 | if (parsePrettyLLVMType(p, type)) |
| 52 | return failure(); |
| 53 | params.push_back(Elt: type); |
| 54 | while (succeeded(Result: p.parseOptionalComma())) { |
| 55 | if (succeeded(Result: p.parseOptionalEllipsis())) { |
| 56 | isVarArg = true; |
| 57 | return p.parseRParen(); |
| 58 | } |
| 59 | if (parsePrettyLLVMType(p, type)) |
| 60 | return failure(); |
| 61 | params.push_back(Elt: type); |
| 62 | } |
| 63 | return p.parseRParen(); |
| 64 | } |
| 65 | |
| 66 | static void printFunctionTypes(AsmPrinter &p, ArrayRef<Type> params, |
| 67 | bool isVarArg) { |
| 68 | llvm::interleaveComma(c: params, os&: p, |
| 69 | each_fn: [&](Type type) { printPrettyLLVMType(p, type); }); |
| 70 | if (isVarArg) { |
| 71 | if (!params.empty()) |
| 72 | p << ", " ; |
| 73 | p << "..." ; |
| 74 | } |
| 75 | p << ')'; |
| 76 | } |
| 77 | |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | // custom<ExtTypeParams> |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | |
| 82 | /// Parses the parameter list for a target extension type. The parameter list |
| 83 | /// contains an optional list of type parameters, followed by an optional list |
| 84 | /// of integer parameters. Type and integer parameters cannot be interleaved in |
| 85 | /// the list. |
| 86 | /// extTypeParams ::= typeList? | intList? | (typeList "," intList) |
| 87 | /// typeList ::= type ("," type)* |
| 88 | /// intList ::= integer ("," integer)* |
| 89 | static ParseResult |
| 90 | parseExtTypeParams(AsmParser &p, SmallVectorImpl<Type> &typeParams, |
| 91 | SmallVectorImpl<unsigned int> &intParams) { |
| 92 | bool parseType = true; |
| 93 | auto typeOrIntParser = [&]() -> ParseResult { |
| 94 | unsigned int i; |
| 95 | auto intResult = p.parseOptionalInteger(result&: i); |
| 96 | if (intResult.has_value() && !failed(Result: *intResult)) { |
| 97 | // Successfully parsed an integer. |
| 98 | intParams.push_back(Elt: i); |
| 99 | // After the first integer was successfully parsed, no |
| 100 | // more types can be parsed. |
| 101 | parseType = false; |
| 102 | return success(); |
| 103 | } |
| 104 | if (parseType) { |
| 105 | Type t; |
| 106 | if (!parsePrettyLLVMType(p, type&: t)) { |
| 107 | // Successfully parsed a type. |
| 108 | typeParams.push_back(Elt: t); |
| 109 | return success(); |
| 110 | } |
| 111 | } |
| 112 | return failure(); |
| 113 | }; |
| 114 | if (p.parseCommaSeparatedList(parseElementFn: typeOrIntParser)) { |
| 115 | p.emitError(loc: p.getCurrentLocation(), |
| 116 | message: "failed to parse parameter list for target extension type" ); |
| 117 | return failure(); |
| 118 | } |
| 119 | return success(); |
| 120 | } |
| 121 | |
| 122 | static void printExtTypeParams(AsmPrinter &p, ArrayRef<Type> typeParams, |
| 123 | ArrayRef<unsigned int> intParams) { |
| 124 | p << typeParams; |
| 125 | if (!typeParams.empty() && !intParams.empty()) |
| 126 | p << ", " ; |
| 127 | |
| 128 | p << intParams; |
| 129 | } |
| 130 | |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | // ODS-Generated Definitions |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | |
| 135 | /// These are unused for now. |
| 136 | /// TODO: Move over to these once more types have been migrated to TypeDef. |
| 137 | [[maybe_unused]] static OptionalParseResult |
| 138 | generatedTypeParser(AsmParser &parser, StringRef *mnemonic, Type &value); |
| 139 | [[maybe_unused]] static LogicalResult generatedTypePrinter(Type def, |
| 140 | AsmPrinter &printer); |
| 141 | |
| 142 | #include "mlir/Dialect/LLVMIR/LLVMTypeInterfaces.cpp.inc" |
| 143 | |
| 144 | #define GET_TYPEDEF_CLASSES |
| 145 | #include "mlir/Dialect/LLVMIR/LLVMTypes.cpp.inc" |
| 146 | |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | // LLVMArrayType |
| 149 | //===----------------------------------------------------------------------===// |
| 150 | |
| 151 | bool LLVMArrayType::isValidElementType(Type type) { |
| 152 | return !llvm::isa<LLVMVoidType, LLVMLabelType, LLVMMetadataType, |
| 153 | LLVMFunctionType, TokenType>(Val: type); |
| 154 | } |
| 155 | |
| 156 | LLVMArrayType LLVMArrayType::get(Type elementType, uint64_t numElements) { |
| 157 | assert(elementType && "expected non-null subtype" ); |
| 158 | return Base::get(ctx: elementType.getContext(), args&: elementType, args&: numElements); |
| 159 | } |
| 160 | |
| 161 | LLVMArrayType |
| 162 | LLVMArrayType::getChecked(function_ref<InFlightDiagnostic()> emitError, |
| 163 | Type elementType, uint64_t numElements) { |
| 164 | assert(elementType && "expected non-null subtype" ); |
| 165 | return Base::getChecked(emitErrorFn: emitError, ctx: elementType.getContext(), args: elementType, |
| 166 | args: numElements); |
| 167 | } |
| 168 | |
| 169 | LogicalResult |
| 170 | LLVMArrayType::verify(function_ref<InFlightDiagnostic()> emitError, |
| 171 | Type elementType, uint64_t numElements) { |
| 172 | if (!isValidElementType(type: elementType)) |
| 173 | return emitError() << "invalid array element type: " << elementType; |
| 174 | return success(); |
| 175 | } |
| 176 | |
| 177 | //===----------------------------------------------------------------------===// |
| 178 | // DataLayoutTypeInterface |
| 179 | //===----------------------------------------------------------------------===// |
| 180 | |
| 181 | llvm::TypeSize |
| 182 | LLVMArrayType::getTypeSizeInBits(const DataLayout &dataLayout, |
| 183 | DataLayoutEntryListRef params) const { |
| 184 | return getTypeSize(dataLayout, params) * kBitsInByte; |
| 185 | } |
| 186 | |
| 187 | llvm::TypeSize LLVMArrayType::getTypeSize(const DataLayout &dataLayout, |
| 188 | DataLayoutEntryListRef params) const { |
| 189 | return dataLayout.getTypeAllocSize(t: getElementType()) * getNumElements(); |
| 190 | } |
| 191 | |
| 192 | uint64_t LLVMArrayType::getABIAlignment(const DataLayout &dataLayout, |
| 193 | DataLayoutEntryListRef params) const { |
| 194 | return dataLayout.getTypeABIAlignment(t: getElementType()); |
| 195 | } |
| 196 | |
| 197 | uint64_t |
| 198 | LLVMArrayType::getPreferredAlignment(const DataLayout &dataLayout, |
| 199 | DataLayoutEntryListRef params) const { |
| 200 | return dataLayout.getTypePreferredAlignment(t: getElementType()); |
| 201 | } |
| 202 | |
| 203 | //===----------------------------------------------------------------------===// |
| 204 | // LLVMByteType |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | |
| 207 | llvm::TypeSize |
| 208 | LLVMByteType::getTypeSizeInBits(const DataLayout &dataLayout, |
| 209 | DataLayoutEntryListRef params) const { |
| 210 | return llvm::TypeSize::getFixed(ExactSize: getBitWidth()); |
| 211 | } |
| 212 | |
| 213 | uint64_t LLVMByteType::getABIAlignment(const DataLayout &dataLayout, |
| 214 | DataLayoutEntryListRef params) const { |
| 215 | return llvm::PowerOf2Ceil(A: llvm::divideCeil(Numerator: getBitWidth(), Denominator: kBitsInByte)); |
| 216 | } |
| 217 | |
| 218 | LogicalResult LLVMByteType::verify(function_ref<InFlightDiagnostic()> emitError, |
| 219 | unsigned bitWidth) { |
| 220 | if (bitWidth == 0) |
| 221 | return emitError() << "bitwidth must be greater than 0" ; |
| 222 | |
| 223 | // Mirror LLVM IR, which limits the bit width to fit in 23 bits. |
| 224 | constexpr unsigned kMaxBitWidth = 1 << 23; |
| 225 | if (bitWidth >= kMaxBitWidth) |
| 226 | return emitError() << "bitwidth must be less than " << kMaxBitWidth |
| 227 | << ", but got " << bitWidth; |
| 228 | return success(); |
| 229 | } |
| 230 | |
| 231 | //===----------------------------------------------------------------------===// |
| 232 | // Function type. |
| 233 | //===----------------------------------------------------------------------===// |
| 234 | |
| 235 | bool LLVMFunctionType::isValidArgumentType(Type type) { |
| 236 | if (auto structType = dyn_cast<LLVMStructType>(Val&: type)) |
| 237 | return !structType.isOpaque(); |
| 238 | |
| 239 | return !llvm::isa<LLVMVoidType, LLVMFunctionType>(Val: type); |
| 240 | } |
| 241 | |
| 242 | bool LLVMFunctionType::isValidResultType(Type type) { |
| 243 | return !llvm::isa<LLVMFunctionType, LLVMMetadataType, LLVMLabelType>(Val: type); |
| 244 | } |
| 245 | |
| 246 | LLVMFunctionType LLVMFunctionType::get(Type result, ArrayRef<Type> arguments, |
| 247 | bool isVarArg) { |
| 248 | assert(result && "expected non-null result" ); |
| 249 | return Base::get(ctx: result.getContext(), args&: result, args&: arguments, args&: isVarArg); |
| 250 | } |
| 251 | |
| 252 | LLVMFunctionType |
| 253 | LLVMFunctionType::getChecked(function_ref<InFlightDiagnostic()> emitError, |
| 254 | Type result, ArrayRef<Type> arguments, |
| 255 | bool isVarArg) { |
| 256 | assert(result && "expected non-null result" ); |
| 257 | return Base::getChecked(emitErrorFn: emitError, ctx: result.getContext(), args: result, args: arguments, |
| 258 | args: isVarArg); |
| 259 | } |
| 260 | |
| 261 | LLVMFunctionType LLVMFunctionType::clone(TypeRange inputs, |
| 262 | TypeRange results) const { |
| 263 | // LLVM functions have exactly one return type. An empty results range |
| 264 | // corresponds to a void return type (as FunctionOpInterface represents void |
| 265 | // functions with 0 results). More than one result is not valid. |
| 266 | if (results.size() > 1) |
| 267 | return {}; |
| 268 | Type resultType = |
| 269 | results.empty() ? LLVMVoidType::get(ctx: getContext()) : results[0]; |
| 270 | if (!isValidResultType(type: resultType)) |
| 271 | return {}; |
| 272 | if (!llvm::all_of(Range&: inputs, P: isValidArgumentType)) |
| 273 | return {}; |
| 274 | return get(result: resultType, arguments: llvm::to_vector(Range&: inputs), isVarArg: isVarArg()); |
| 275 | } |
| 276 | |
| 277 | ArrayRef<Type> LLVMFunctionType::getReturnTypes() const { |
| 278 | return static_cast<detail::LLVMFunctionTypeStorage *>(getImpl())->returnType; |
| 279 | } |
| 280 | |
| 281 | LogicalResult |
| 282 | LLVMFunctionType::verify(function_ref<InFlightDiagnostic()> emitError, |
| 283 | Type result, ArrayRef<Type> arguments, bool) { |
| 284 | if (!isValidResultType(type: result)) |
| 285 | return emitError() << "invalid function result type: " << result; |
| 286 | |
| 287 | for (Type arg : arguments) |
| 288 | if (!isValidArgumentType(type: arg)) |
| 289 | return emitError() << "invalid function argument type: " << arg; |
| 290 | |
| 291 | return success(); |
| 292 | } |
| 293 | |
| 294 | //===----------------------------------------------------------------------===// |
| 295 | // DataLayoutTypeInterface |
| 296 | //===----------------------------------------------------------------------===// |
| 297 | |
| 298 | constexpr const static uint64_t kDefaultPointerSizeBits = 64; |
| 299 | constexpr const static uint64_t kDefaultPointerAlignment = 8; |
| 300 | |
| 301 | std::optional<uint64_t> mlir::LLVM::(Attribute attr, |
| 302 | PtrDLEntryPos pos) { |
| 303 | auto spec = cast<DenseIntElementsAttr>(Val&: attr); |
| 304 | auto idx = static_cast<int64_t>(pos); |
| 305 | if (idx >= spec.size()) |
| 306 | return std::nullopt; |
| 307 | return spec.getValues<uint64_t>()[idx]; |
| 308 | } |
| 309 | |
| 310 | /// Returns the part of the data layout entry that corresponds to `pos` for the |
| 311 | /// given `type` by interpreting the list of entries `params`. For the pointer |
| 312 | /// type in the default address space, returns the default value if the entries |
| 313 | /// do not provide a custom one, for other address spaces returns std::nullopt. |
| 314 | static std::optional<uint64_t> |
| 315 | getPointerDataLayoutEntry(DataLayoutEntryListRef params, LLVMPointerType type, |
| 316 | PtrDLEntryPos pos) { |
| 317 | // First, look for the entry for the pointer in the current address space. |
| 318 | Attribute currentEntry; |
| 319 | for (DataLayoutEntryInterface entry : params) { |
| 320 | if (!entry.isTypeEntry()) |
| 321 | continue; |
| 322 | if (cast<LLVMPointerType>(Val: cast<Type>(Val: entry.getKey())).getAddressSpace() == |
| 323 | type.getAddressSpace()) { |
| 324 | currentEntry = entry.getValue(); |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | if (currentEntry) { |
| 329 | std::optional<uint64_t> value = extractPointerSpecValue(attr: currentEntry, pos); |
| 330 | // If the optional `PtrDLEntryPos::Index` entry is not available, use the |
| 331 | // pointer size as the index bitwidth. |
| 332 | if (!value && pos == PtrDLEntryPos::Index) |
| 333 | value = extractPointerSpecValue(attr: currentEntry, pos: PtrDLEntryPos::Size); |
| 334 | bool isSizeOrIndex = |
| 335 | pos == PtrDLEntryPos::Size || pos == PtrDLEntryPos::Index; |
| 336 | return *value / (isSizeOrIndex ? 1 : kBitsInByte); |
| 337 | } |
| 338 | |
| 339 | // If not found, and this is the pointer to the default memory space, assume |
| 340 | // 64-bit pointers. |
| 341 | if (type.getAddressSpace() == 0) { |
| 342 | bool isSizeOrIndex = |
| 343 | pos == PtrDLEntryPos::Size || pos == PtrDLEntryPos::Index; |
| 344 | return isSizeOrIndex ? kDefaultPointerSizeBits : kDefaultPointerAlignment; |
| 345 | } |
| 346 | |
| 347 | return std::nullopt; |
| 348 | } |
| 349 | |
| 350 | llvm::TypeSize |
| 351 | LLVMPointerType::getTypeSizeInBits(const DataLayout &dataLayout, |
| 352 | DataLayoutEntryListRef params) const { |
| 353 | if (std::optional<uint64_t> size = |
| 354 | getPointerDataLayoutEntry(params, type: *this, pos: PtrDLEntryPos::Size)) |
| 355 | return llvm::TypeSize::getFixed(ExactSize: *size); |
| 356 | |
| 357 | // For other memory spaces, use the size of the pointer to the default memory |
| 358 | // space. |
| 359 | return dataLayout.getTypeSizeInBits(t: get(context: getContext())); |
| 360 | } |
| 361 | |
| 362 | uint64_t LLVMPointerType::getABIAlignment(const DataLayout &dataLayout, |
| 363 | DataLayoutEntryListRef params) const { |
| 364 | if (std::optional<uint64_t> alignment = |
| 365 | getPointerDataLayoutEntry(params, type: *this, pos: PtrDLEntryPos::Abi)) |
| 366 | return *alignment; |
| 367 | |
| 368 | return dataLayout.getTypeABIAlignment(t: get(context: getContext())); |
| 369 | } |
| 370 | |
| 371 | uint64_t |
| 372 | LLVMPointerType::getPreferredAlignment(const DataLayout &dataLayout, |
| 373 | DataLayoutEntryListRef params) const { |
| 374 | if (std::optional<uint64_t> alignment = |
| 375 | getPointerDataLayoutEntry(params, type: *this, pos: PtrDLEntryPos::Preferred)) |
| 376 | return *alignment; |
| 377 | |
| 378 | return dataLayout.getTypePreferredAlignment(t: get(context: getContext())); |
| 379 | } |
| 380 | |
| 381 | std::optional<uint64_t> |
| 382 | LLVMPointerType::getIndexBitwidth(const DataLayout &dataLayout, |
| 383 | DataLayoutEntryListRef params) const { |
| 384 | if (std::optional<uint64_t> indexBitwidth = |
| 385 | getPointerDataLayoutEntry(params, type: *this, pos: PtrDLEntryPos::Index)) |
| 386 | return *indexBitwidth; |
| 387 | |
| 388 | return dataLayout.getTypeIndexBitwidth(t: get(context: getContext())); |
| 389 | } |
| 390 | |
| 391 | bool LLVMPointerType::areCompatible( |
| 392 | DataLayoutEntryListRef oldLayout, DataLayoutEntryListRef newLayout, |
| 393 | DataLayoutSpecInterface newSpec, |
| 394 | const DataLayoutIdentifiedEntryMap &map) const { |
| 395 | for (DataLayoutEntryInterface newEntry : newLayout) { |
| 396 | if (!newEntry.isTypeEntry()) |
| 397 | continue; |
| 398 | uint64_t size = kDefaultPointerSizeBits; |
| 399 | uint64_t abi = kDefaultPointerAlignment; |
| 400 | auto newType = |
| 401 | llvm::cast<LLVMPointerType>(Val: llvm::cast<Type>(Val: newEntry.getKey())); |
| 402 | const auto *it = |
| 403 | llvm::find_if(Range&: oldLayout, P: [&](DataLayoutEntryInterface entry) { |
| 404 | if (auto type = llvm::dyn_cast_if_present<Type>(Val: entry.getKey())) { |
| 405 | return llvm::cast<LLVMPointerType>(Val&: type).getAddressSpace() == |
| 406 | newType.getAddressSpace(); |
| 407 | } |
| 408 | return false; |
| 409 | }); |
| 410 | if (it == oldLayout.end()) { |
| 411 | llvm::find_if(Range&: oldLayout, P: [&](DataLayoutEntryInterface entry) { |
| 412 | if (auto type = llvm::dyn_cast_if_present<Type>(Val: entry.getKey())) { |
| 413 | return llvm::cast<LLVMPointerType>(Val&: type).getAddressSpace() == 0; |
| 414 | } |
| 415 | return false; |
| 416 | }); |
| 417 | } |
| 418 | if (it != oldLayout.end()) { |
| 419 | size = *extractPointerSpecValue(attr: it->getValue(), pos: PtrDLEntryPos::Size); |
| 420 | abi = *extractPointerSpecValue(attr: it->getValue(), pos: PtrDLEntryPos::Abi); |
| 421 | } |
| 422 | |
| 423 | Attribute newSpec = llvm::cast<DenseIntElementsAttr>(Val: newEntry.getValue()); |
| 424 | uint64_t newSize = *extractPointerSpecValue(attr: newSpec, pos: PtrDLEntryPos::Size); |
| 425 | uint64_t newAbi = *extractPointerSpecValue(attr: newSpec, pos: PtrDLEntryPos::Abi); |
| 426 | if (size != newSize || abi < newAbi || abi % newAbi != 0) |
| 427 | return false; |
| 428 | } |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | LogicalResult LLVMPointerType::verifyEntries(DataLayoutEntryListRef entries, |
| 433 | Location loc) const { |
| 434 | for (DataLayoutEntryInterface entry : entries) { |
| 435 | if (!entry.isTypeEntry()) |
| 436 | continue; |
| 437 | auto key = llvm::cast<Type>(Val: entry.getKey()); |
| 438 | auto values = llvm::dyn_cast<DenseIntElementsAttr>(Val: entry.getValue()); |
| 439 | if (!values || (values.size() != 3 && values.size() != 4)) { |
| 440 | return emitError(loc) |
| 441 | << "expected layout attribute for " << key |
| 442 | << " to be a dense integer elements attribute with 3 or 4 " |
| 443 | "elements" ; |
| 444 | } |
| 445 | if (!values.getElementType().isInteger(width: 64)) |
| 446 | return emitError(loc) << "expected i64 parameters for " << key; |
| 447 | |
| 448 | if (extractPointerSpecValue(attr: values, pos: PtrDLEntryPos::Abi) > |
| 449 | extractPointerSpecValue(attr: values, pos: PtrDLEntryPos::Preferred)) { |
| 450 | return emitError(loc) << "preferred alignment is expected to be at least " |
| 451 | "as large as ABI alignment" ; |
| 452 | } |
| 453 | } |
| 454 | return success(); |
| 455 | } |
| 456 | |
| 457 | //===----------------------------------------------------------------------===// |
| 458 | // Struct type. |
| 459 | //===----------------------------------------------------------------------===// |
| 460 | |
| 461 | bool LLVMStructType::isValidElementType(Type type) { |
| 462 | return !llvm::isa<LLVMVoidType, LLVMLabelType, LLVMMetadataType, |
| 463 | LLVMFunctionType, TokenType>(Val: type); |
| 464 | } |
| 465 | |
| 466 | LLVMStructType LLVMStructType::getIdentified(MLIRContext *context, |
| 467 | StringRef name) { |
| 468 | return Base::get(ctx: context, args&: name, /*opaque=*/args: false); |
| 469 | } |
| 470 | |
| 471 | LLVMStructType LLVMStructType::getIdentifiedChecked( |
| 472 | function_ref<InFlightDiagnostic()> emitError, MLIRContext *context, |
| 473 | StringRef name) { |
| 474 | return Base::getChecked(emitErrorFn: emitError, ctx&: context, args: name, /*opaque=*/args: false); |
| 475 | } |
| 476 | |
| 477 | LLVMStructType LLVMStructType::getNewIdentified(MLIRContext *context, |
| 478 | StringRef name, |
| 479 | ArrayRef<Type> elements, |
| 480 | bool isPacked) { |
| 481 | std::string stringName = name.str(); |
| 482 | unsigned counter = 0; |
| 483 | do { |
| 484 | auto type = LLVMStructType::getIdentified(context, name: stringName); |
| 485 | if (type.isInitialized() || failed(Result: type.setBody(types: elements, isPacked))) { |
| 486 | counter += 1; |
| 487 | stringName = (Twine(name) + "." + std::to_string(val: counter)).str(); |
| 488 | continue; |
| 489 | } |
| 490 | return type; |
| 491 | } while (true); |
| 492 | } |
| 493 | |
| 494 | LLVMStructType LLVMStructType::getLiteral(MLIRContext *context, |
| 495 | ArrayRef<Type> types, bool isPacked) { |
| 496 | return Base::get(ctx: context, args&: types, args&: isPacked); |
| 497 | } |
| 498 | |
| 499 | LLVMStructType |
| 500 | LLVMStructType::getLiteralChecked(function_ref<InFlightDiagnostic()> emitError, |
| 501 | MLIRContext *context, ArrayRef<Type> types, |
| 502 | bool isPacked) { |
| 503 | return Base::getChecked(emitErrorFn: emitError, ctx&: context, args: types, args: isPacked); |
| 504 | } |
| 505 | |
| 506 | LLVMStructType LLVMStructType::getOpaque(StringRef name, MLIRContext *context) { |
| 507 | return Base::get(ctx: context, args&: name, /*opaque=*/args: true); |
| 508 | } |
| 509 | |
| 510 | LLVMStructType |
| 511 | LLVMStructType::getOpaqueChecked(function_ref<InFlightDiagnostic()> emitError, |
| 512 | MLIRContext *context, StringRef name) { |
| 513 | return Base::getChecked(emitErrorFn: emitError, ctx&: context, args: name, /*opaque=*/args: true); |
| 514 | } |
| 515 | |
| 516 | LogicalResult LLVMStructType::setBody(ArrayRef<Type> types, bool isPacked) { |
| 517 | assert(isIdentified() && "can only set bodies of identified structs" ); |
| 518 | assert(llvm::all_of(types, LLVMStructType::isValidElementType) && |
| 519 | "expected valid body types" ); |
| 520 | return Base::mutate(args&: types, args&: isPacked); |
| 521 | } |
| 522 | |
| 523 | bool LLVMStructType::isPacked() const { return getImpl()->isPacked(); } |
| 524 | bool LLVMStructType::isIdentified() const { return getImpl()->isIdentified(); } |
| 525 | bool LLVMStructType::isOpaque() const { |
| 526 | return getImpl()->isIdentified() && |
| 527 | (getImpl()->isOpaque() || !getImpl()->isInitialized()); |
| 528 | } |
| 529 | bool LLVMStructType::isInitialized() { return getImpl()->isInitialized(); } |
| 530 | StringRef LLVMStructType::getName() const { return getImpl()->getIdentifier(); } |
| 531 | ArrayRef<Type> LLVMStructType::getBody() const { |
| 532 | return isIdentified() ? getImpl()->getIdentifiedStructBody() |
| 533 | : getImpl()->getTypeList(); |
| 534 | } |
| 535 | |
| 536 | LogicalResult |
| 537 | LLVMStructType::verifyInvariants(function_ref<InFlightDiagnostic()>, StringRef, |
| 538 | bool) { |
| 539 | return success(); |
| 540 | } |
| 541 | |
| 542 | LogicalResult |
| 543 | LLVMStructType::verifyInvariants(function_ref<InFlightDiagnostic()> emitError, |
| 544 | ArrayRef<Type> types, bool) { |
| 545 | for (Type t : types) |
| 546 | if (!isValidElementType(type: t)) |
| 547 | return emitError() << "invalid LLVM structure element type: " << t; |
| 548 | |
| 549 | return success(); |
| 550 | } |
| 551 | |
| 552 | /// Returns the byte offset of the element of the specified structure at the |
| 553 | /// given index and the strictest alignment requirement of the elements up to |
| 554 | /// the index. For the index past the last element it thus returns the size and |
| 555 | /// alignment of the whole structure. |
| 556 | static uint64_t getOffsetAndAlignment(const DataLayout &dataLayout, |
| 557 | LLVMStructType structType, unsigned index, |
| 558 | uint64_t &structAlignment) { |
| 559 | uint64_t offset = 0; |
| 560 | structAlignment = 1; |
| 561 | for (auto [i, element] : llvm::enumerate(First: structType.getBody())) { |
| 562 | // Align the element unless the structure is packed. |
| 563 | uint64_t elementAlignment = dataLayout.getTypeABIAlignment(t: element); |
| 564 | if (!structType.isPacked()) { |
| 565 | offset = llvm::alignTo(Value: offset, Align: elementAlignment); |
| 566 | |
| 567 | // The structure alignment is the strictest alignment of its |
| 568 | // elements. |
| 569 | structAlignment = std::max(a: structAlignment, b: elementAlignment); |
| 570 | } |
| 571 | if (i == index) |
| 572 | return offset; |
| 573 | |
| 574 | // Each element gets its allocation size, even in packed structures. |
| 575 | offset += dataLayout.getTypeAllocSize(t: element); |
| 576 | } |
| 577 | return offset; |
| 578 | } |
| 579 | |
| 580 | uint64_t LLVMStructType::getElementOffset(const DataLayout &dataLayout, |
| 581 | unsigned index) const { |
| 582 | assert(index < getBody().size() && "expected a valid struct element index" ); |
| 583 | uint64_t structAlignment; |
| 584 | return getOffsetAndAlignment(dataLayout, structType: *this, index, structAlignment); |
| 585 | } |
| 586 | |
| 587 | llvm::TypeSize |
| 588 | LLVMStructType::getTypeSizeInBits(const DataLayout &dataLayout, |
| 589 | DataLayoutEntryListRef params) const { |
| 590 | uint64_t structAlignment; |
| 591 | uint64_t structSize = getOffsetAndAlignment( |
| 592 | dataLayout, structType: *this, index: getBody().size(), structAlignment); |
| 593 | // At the end, add padding to the struct to satisfy its own alignment |
| 594 | // requirement. Otherwise structs inside of arrays would be misaligned. |
| 595 | structSize = llvm::alignTo(Value: structSize, Align: structAlignment); |
| 596 | return llvm::TypeSize::getFixed(ExactSize: structSize * kBitsInByte); |
| 597 | } |
| 598 | |
| 599 | namespace { |
| 600 | enum class StructDLEntryPos { Abi = 0, Preferred = 1 }; |
| 601 | } // namespace |
| 602 | |
| 603 | static std::optional<uint64_t> |
| 604 | getStructDataLayoutEntry(DataLayoutEntryListRef params, LLVMStructType type, |
| 605 | StructDLEntryPos pos) { |
| 606 | const auto *currentEntry = |
| 607 | llvm::find_if(Range&: params, P: [](DataLayoutEntryInterface entry) { |
| 608 | return entry.isTypeEntry(); |
| 609 | }); |
| 610 | if (currentEntry == params.end()) |
| 611 | return std::nullopt; |
| 612 | |
| 613 | auto attr = llvm::cast<DenseIntElementsAttr>(Val: currentEntry->getValue()); |
| 614 | if (pos == StructDLEntryPos::Preferred && |
| 615 | attr.size() <= static_cast<int64_t>(StructDLEntryPos::Preferred)) |
| 616 | // If no preferred was specified, fall back to abi alignment |
| 617 | pos = StructDLEntryPos::Abi; |
| 618 | |
| 619 | return attr.getValues<uint64_t>()[static_cast<size_t>(pos)]; |
| 620 | } |
| 621 | |
| 622 | static uint64_t calculateStructAlignment(const DataLayout &dataLayout, |
| 623 | DataLayoutEntryListRef params, |
| 624 | LLVMStructType type, |
| 625 | StructDLEntryPos pos) { |
| 626 | // Packed structs always have an abi alignment of 1 |
| 627 | if (pos == StructDLEntryPos::Abi && type.isPacked()) { |
| 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | // The alignment requirement of a struct is equal to the strictest alignment |
| 632 | // requirement of its elements. |
| 633 | uint64_t structAlignment = 1; |
| 634 | for (Type iter : type.getBody()) { |
| 635 | structAlignment = |
| 636 | std::max(a: dataLayout.getTypeABIAlignment(t: iter), b: structAlignment); |
| 637 | } |
| 638 | |
| 639 | // Entries are only allowed to be stricter than the required alignment |
| 640 | if (std::optional<uint64_t> entryResult = |
| 641 | getStructDataLayoutEntry(params, type, pos)) |
| 642 | return std::max(a: *entryResult / kBitsInByte, b: structAlignment); |
| 643 | |
| 644 | return structAlignment; |
| 645 | } |
| 646 | |
| 647 | uint64_t LLVMStructType::getABIAlignment(const DataLayout &dataLayout, |
| 648 | DataLayoutEntryListRef params) const { |
| 649 | return calculateStructAlignment(dataLayout, params, type: *this, |
| 650 | pos: StructDLEntryPos::Abi); |
| 651 | } |
| 652 | |
| 653 | uint64_t |
| 654 | LLVMStructType::getPreferredAlignment(const DataLayout &dataLayout, |
| 655 | DataLayoutEntryListRef params) const { |
| 656 | return calculateStructAlignment(dataLayout, params, type: *this, |
| 657 | pos: StructDLEntryPos::Preferred); |
| 658 | } |
| 659 | |
| 660 | static uint64_t (Attribute attr, StructDLEntryPos pos) { |
| 661 | return llvm::cast<DenseIntElementsAttr>(Val&: attr) |
| 662 | .getValues<uint64_t>()[static_cast<size_t>(pos)]; |
| 663 | } |
| 664 | |
| 665 | bool LLVMStructType::areCompatible( |
| 666 | DataLayoutEntryListRef oldLayout, DataLayoutEntryListRef newLayout, |
| 667 | DataLayoutSpecInterface newSpec, |
| 668 | const DataLayoutIdentifiedEntryMap &map) const { |
| 669 | for (DataLayoutEntryInterface newEntry : newLayout) { |
| 670 | if (!newEntry.isTypeEntry()) |
| 671 | continue; |
| 672 | |
| 673 | const auto *previousEntry = |
| 674 | llvm::find_if(Range&: oldLayout, P: [](DataLayoutEntryInterface entry) { |
| 675 | return entry.isTypeEntry(); |
| 676 | }); |
| 677 | if (previousEntry == oldLayout.end()) |
| 678 | continue; |
| 679 | |
| 680 | uint64_t abi = extractStructSpecValue(attr: previousEntry->getValue(), |
| 681 | pos: StructDLEntryPos::Abi); |
| 682 | uint64_t newAbi = |
| 683 | extractStructSpecValue(attr: newEntry.getValue(), pos: StructDLEntryPos::Abi); |
| 684 | if (abi < newAbi || abi % newAbi != 0) |
| 685 | return false; |
| 686 | } |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | LogicalResult LLVMStructType::verifyEntries(DataLayoutEntryListRef entries, |
| 691 | Location loc) const { |
| 692 | for (DataLayoutEntryInterface entry : entries) { |
| 693 | if (!entry.isTypeEntry()) |
| 694 | continue; |
| 695 | |
| 696 | auto key = llvm::cast<LLVMStructType>(Val: llvm::cast<Type>(Val: entry.getKey())); |
| 697 | auto values = llvm::dyn_cast<DenseIntElementsAttr>(Val: entry.getValue()); |
| 698 | if (!values || (values.size() != 2 && values.size() != 1)) { |
| 699 | return emitError(loc) |
| 700 | << "expected layout attribute for " |
| 701 | << llvm::cast<Type>(Val: entry.getKey()) |
| 702 | << " to be a dense integer elements attribute of 1 or 2 elements" ; |
| 703 | } |
| 704 | if (!values.getElementType().isInteger(width: 64)) |
| 705 | return emitError(loc) << "expected i64 entries for " << key; |
| 706 | |
| 707 | if (key.isIdentified() || !key.getBody().empty()) { |
| 708 | return emitError(loc) << "unexpected layout attribute for struct " << key; |
| 709 | } |
| 710 | |
| 711 | if (values.size() == 1) |
| 712 | continue; |
| 713 | |
| 714 | if (extractStructSpecValue(attr: values, pos: StructDLEntryPos::Abi) > |
| 715 | extractStructSpecValue(attr: values, pos: StructDLEntryPos::Preferred)) { |
| 716 | return emitError(loc) << "preferred alignment is expected to be at least " |
| 717 | "as large as ABI alignment" ; |
| 718 | } |
| 719 | } |
| 720 | return mlir::success(); |
| 721 | } |
| 722 | |
| 723 | //===----------------------------------------------------------------------===// |
| 724 | // LLVMTargetExtType. |
| 725 | //===----------------------------------------------------------------------===// |
| 726 | |
| 727 | static constexpr llvm::StringRef kSpirvPrefix = "spirv." ; |
| 728 | static constexpr llvm::StringRef kArmSVCount = "aarch64.svcount" ; |
| 729 | static constexpr llvm::StringRef kAMDGCNNamedBarrier = "amdgcn.named.barrier" ; |
| 730 | |
| 731 | bool LLVM::LLVMTargetExtType::hasProperty(Property prop) const { |
| 732 | // See llvm/lib/IR/Type.cpp for reference. |
| 733 | uint64_t properties = 0; |
| 734 | |
| 735 | if (getExtTypeName().starts_with(Prefix: kSpirvPrefix)) |
| 736 | properties |= |
| 737 | (LLVMTargetExtType::HasZeroInit | LLVM::LLVMTargetExtType::CanBeGlobal); |
| 738 | |
| 739 | if (getExtTypeName() == kAMDGCNNamedBarrier) |
| 740 | properties |= LLVMTargetExtType::CanBeGlobal; |
| 741 | |
| 742 | return (properties & prop) == prop; |
| 743 | } |
| 744 | |
| 745 | bool LLVM::LLVMTargetExtType::supportsMemOps() const { |
| 746 | // See llvm/lib/IR/Type.cpp for reference. |
| 747 | if (getExtTypeName().starts_with(Prefix: kSpirvPrefix)) |
| 748 | return true; |
| 749 | |
| 750 | if (getExtTypeName() == kArmSVCount) |
| 751 | return true; |
| 752 | |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | //===----------------------------------------------------------------------===// |
| 757 | // LLVMPPCFP128Type |
| 758 | //===----------------------------------------------------------------------===// |
| 759 | |
| 760 | const llvm::fltSemantics &LLVMPPCFP128Type::getFloatSemantics() const { |
| 761 | return APFloat::PPCDoubleDouble(); |
| 762 | } |
| 763 | |
| 764 | //===----------------------------------------------------------------------===// |
| 765 | // Interfaces for destructurable types |
| 766 | //===----------------------------------------------------------------------===// |
| 767 | |
| 768 | std::optional<DenseMap<Attribute, Type>> |
| 769 | LLVM::LLVMStructType::getSubelementIndexMap() const { |
| 770 | // Empty structs have no sub-elements and cannot be destructured. |
| 771 | if (getBody().empty()) |
| 772 | return std::nullopt; |
| 773 | Type i32 = IntegerType::get(context: getContext(), width: 32); |
| 774 | DenseMap<Attribute, Type> destructured; |
| 775 | for (const auto &[index, elemType] : llvm::enumerate(First: getBody())) |
| 776 | destructured.insert(KV: {IntegerAttr::get(type: i32, value: index), elemType}); |
| 777 | return destructured; |
| 778 | } |
| 779 | |
| 780 | Type LLVM::LLVMStructType::getTypeAtIndex(Attribute index) const { |
| 781 | auto indexAttr = llvm::dyn_cast<IntegerAttr>(Val&: index); |
| 782 | if (!indexAttr || !indexAttr.getType().isInteger(width: 32)) |
| 783 | return {}; |
| 784 | int32_t indexInt = indexAttr.getInt(); |
| 785 | ArrayRef<Type> body = getBody(); |
| 786 | if (indexInt < 0 || body.size() <= static_cast<uint32_t>(indexInt)) |
| 787 | return {}; |
| 788 | return body[indexInt]; |
| 789 | } |
| 790 | |
| 791 | std::optional<DenseMap<Attribute, Type>> |
| 792 | LLVM::LLVMArrayType::getSubelementIndexMap() const { |
| 793 | constexpr size_t maxArraySizeForDestructuring = 16; |
| 794 | if (getNumElements() > maxArraySizeForDestructuring) |
| 795 | return {}; |
| 796 | int32_t numElements = getNumElements(); |
| 797 | |
| 798 | Type i32 = IntegerType::get(context: getContext(), width: 32); |
| 799 | DenseMap<Attribute, Type> destructured; |
| 800 | for (int32_t index = 0; index < numElements; ++index) |
| 801 | destructured.insert(KV: {IntegerAttr::get(type: i32, value: index), getElementType()}); |
| 802 | return destructured; |
| 803 | } |
| 804 | |
| 805 | Type LLVM::LLVMArrayType::getTypeAtIndex(Attribute index) const { |
| 806 | auto indexAttr = llvm::dyn_cast<IntegerAttr>(Val&: index); |
| 807 | if (!indexAttr || !indexAttr.getType().isInteger(width: 32)) |
| 808 | return {}; |
| 809 | int32_t indexInt = indexAttr.getInt(); |
| 810 | if (indexInt < 0 || getNumElements() <= static_cast<uint32_t>(indexInt)) |
| 811 | return {}; |
| 812 | return getElementType(); |
| 813 | } |
| 814 | |
| 815 | //===----------------------------------------------------------------------===// |
| 816 | // Utility functions. |
| 817 | //===----------------------------------------------------------------------===// |
| 818 | |
| 819 | /// Check whether type is a compatible ptr type. These are pointer-like types |
| 820 | /// with no element type, no metadata, and using the LLVM |
| 821 | /// LLVMAddrSpaceAttrInterface memory space. |
| 822 | static bool isCompatiblePtrType(Type type) { |
| 823 | auto ptrTy = dyn_cast<PtrLikeTypeInterface>(Val&: type); |
| 824 | if (!ptrTy) |
| 825 | return false; |
| 826 | return !ptrTy.hasPtrMetadata() && ptrTy.getElementType() == nullptr && |
| 827 | isa<LLVMAddrSpaceAttrInterface>(Val: ptrTy.getMemorySpace()); |
| 828 | } |
| 829 | |
| 830 | bool mlir::LLVM::isCompatibleOuterType(Type type) { |
| 831 | // clang-format off |
| 832 | if (llvm::isa< |
| 833 | BFloat16Type, |
| 834 | Float16Type, |
| 835 | Float32Type, |
| 836 | Float64Type, |
| 837 | Float80Type, |
| 838 | Float128Type, |
| 839 | LLVMArrayType, |
| 840 | LLVMByteType, |
| 841 | LLVMFunctionType, |
| 842 | LLVMLabelType, |
| 843 | LLVMMetadataType, |
| 844 | LLVMPPCFP128Type, |
| 845 | LLVMPointerType, |
| 846 | LLVMStructType, |
| 847 | LLVMTargetExtType, |
| 848 | LLVMVoidType, |
| 849 | LLVMX86AMXType, |
| 850 | TokenType |
| 851 | >(Val: type)) { |
| 852 | // clang-format on |
| 853 | return true; |
| 854 | } |
| 855 | |
| 856 | // Only signless integers are compatible. |
| 857 | if (auto intType = llvm::dyn_cast<IntegerType>(Val&: type)) |
| 858 | return intType.isSignless(); |
| 859 | |
| 860 | // 1D vector types are compatible. |
| 861 | if (auto vecType = llvm::dyn_cast<VectorType>(Val&: type)) |
| 862 | return vecType.getRank() == 1; |
| 863 | |
| 864 | return isCompatiblePtrType(type); |
| 865 | } |
| 866 | |
| 867 | static bool isCompatibleImpl(Type type, DenseSet<Type> &compatibleTypes) { |
| 868 | if (!compatibleTypes.insert(V: type).second) |
| 869 | return true; |
| 870 | |
| 871 | auto isCompatible = [&](Type type) { |
| 872 | return isCompatibleImpl(type, compatibleTypes); |
| 873 | }; |
| 874 | |
| 875 | bool result = |
| 876 | llvm::TypeSwitch<Type, bool>(type) |
| 877 | .Case(caseFn: [&](LLVMStructType structType) { |
| 878 | return llvm::all_of(Range: structType.getBody(), P: isCompatible); |
| 879 | }) |
| 880 | .Case(caseFn: [&](LLVMFunctionType funcType) { |
| 881 | return isCompatible(funcType.getReturnType()) && |
| 882 | llvm::all_of(Range: funcType.getParams(), P: isCompatible); |
| 883 | }) |
| 884 | .Case(caseFn: [](IntegerType intType) { return intType.isSignless(); }) |
| 885 | .Case(caseFn: [&](VectorType vecType) { |
| 886 | return vecType.getRank() == 1 && |
| 887 | isCompatible(vecType.getElementType()); |
| 888 | }) |
| 889 | .Case(caseFn: [&](LLVMPointerType pointerType) { return true; }) |
| 890 | .Case(caseFn: [&](LLVMTargetExtType extType) { |
| 891 | return llvm::all_of(Range: extType.getTypeParams(), P: isCompatible); |
| 892 | }) |
| 893 | // clang-format off |
| 894 | .Case(caseFn: [&](LLVMArrayType containerType) { |
| 895 | return isCompatible(containerType.getElementType()); |
| 896 | }) |
| 897 | .Case< |
| 898 | BFloat16Type, |
| 899 | Float16Type, |
| 900 | Float32Type, |
| 901 | Float64Type, |
| 902 | Float80Type, |
| 903 | Float128Type, |
| 904 | LLVMByteType, |
| 905 | LLVMLabelType, |
| 906 | LLVMMetadataType, |
| 907 | LLVMPPCFP128Type, |
| 908 | LLVMVoidType, |
| 909 | LLVMX86AMXType, |
| 910 | TokenType |
| 911 | >(caseFn: [](Type) { return true; }) |
| 912 | // clang-format on |
| 913 | .Case<PtrLikeTypeInterface>( |
| 914 | caseFn: [](Type type) { return isCompatiblePtrType(type); }) |
| 915 | .Default(defaultResult: false); |
| 916 | |
| 917 | if (!result) |
| 918 | compatibleTypes.erase(V: type); |
| 919 | |
| 920 | return result; |
| 921 | } |
| 922 | |
| 923 | bool LLVMDialect::isCompatibleType(Type type) { |
| 924 | if (auto *llvmDialect = |
| 925 | type.getContext()->getLoadedDialect<LLVM::LLVMDialect>()) |
| 926 | return isCompatibleImpl(type, compatibleTypes&: llvmDialect->compatibleTypes.get()); |
| 927 | |
| 928 | DenseSet<Type> localCompatibleTypes; |
| 929 | return isCompatibleImpl(type, compatibleTypes&: localCompatibleTypes); |
| 930 | } |
| 931 | |
| 932 | bool mlir::LLVM::isCompatibleType(Type type) { |
| 933 | return LLVMDialect::isCompatibleType(type); |
| 934 | } |
| 935 | |
| 936 | bool mlir::LLVM::isLoadableType(Type type) { |
| 937 | return /*LLVM_PrimitiveType*/ ( |
| 938 | LLVM::isCompatibleOuterType(type) && |
| 939 | !isa<LLVM::LLVMVoidType, LLVM::LLVMFunctionType>(Val: type)) && |
| 940 | /*LLVM_OpaqueStruct*/ |
| 941 | !(isa<LLVM::LLVMStructType>(Val: type) && |
| 942 | cast<LLVM::LLVMStructType>(Val&: type).isOpaque()) && |
| 943 | /*LLVM_AnyTargetExt*/ |
| 944 | !(isa<LLVM::LLVMTargetExtType>(Val: type) && |
| 945 | !cast<LLVM::LLVMTargetExtType>(Val&: type).supportsMemOps()); |
| 946 | } |
| 947 | |
| 948 | bool mlir::LLVM::isCompatibleFloatingPointType(Type type) { |
| 949 | return llvm::isa<BFloat16Type, Float16Type, Float32Type, Float64Type, |
| 950 | Float80Type, Float128Type, LLVMPPCFP128Type>(Val: type); |
| 951 | } |
| 952 | |
| 953 | bool mlir::LLVM::isCompatibleVectorType(Type type) { |
| 954 | if (auto vecType = llvm::dyn_cast<VectorType>(Val&: type)) { |
| 955 | if (vecType.getRank() != 1) |
| 956 | return false; |
| 957 | Type elementType = vecType.getElementType(); |
| 958 | if (auto intType = llvm::dyn_cast<IntegerType>(Val&: elementType)) |
| 959 | return intType.isSignless(); |
| 960 | return llvm::isa<BFloat16Type, Float16Type, Float32Type, Float64Type, |
| 961 | Float80Type, Float128Type, LLVMByteType, LLVMPointerType>( |
| 962 | Val: elementType) || |
| 963 | isCompatiblePtrType(type: elementType); |
| 964 | } |
| 965 | return false; |
| 966 | } |
| 967 | |
| 968 | llvm::ElementCount mlir::LLVM::getVectorNumElements(Type type) { |
| 969 | auto vecTy = dyn_cast<VectorType>(Val&: type); |
| 970 | assert(vecTy && "incompatible with LLVM vector type" ); |
| 971 | if (vecTy.isScalable()) |
| 972 | return llvm::ElementCount::getScalable(MinVal: vecTy.getNumElements()); |
| 973 | return llvm::ElementCount::getFixed(MinVal: vecTy.getNumElements()); |
| 974 | } |
| 975 | |
| 976 | bool mlir::LLVM::isScalableVectorType(Type vectorType) { |
| 977 | assert(llvm::isa<VectorType>(vectorType) && |
| 978 | "expected LLVM-compatible vector type" ); |
| 979 | return llvm::cast<VectorType>(Val&: vectorType).isScalable(); |
| 980 | } |
| 981 | |
| 982 | Type mlir::LLVM::getVectorType(Type elementType, unsigned numElements, |
| 983 | bool isScalable) { |
| 984 | assert(VectorType::isValidElementType(elementType) && |
| 985 | "incompatible element type" ); |
| 986 | return VectorType::get(shape: numElements, elementType, scalableDims: {isScalable}); |
| 987 | } |
| 988 | |
| 989 | Type mlir::LLVM::getVectorType(Type elementType, |
| 990 | const llvm::ElementCount &numElements) { |
| 991 | if (numElements.isScalable()) |
| 992 | return getVectorType(elementType, numElements: numElements.getKnownMinValue(), |
| 993 | /*isScalable=*/true); |
| 994 | return getVectorType(elementType, numElements: numElements.getFixedValue(), |
| 995 | /*isScalable=*/false); |
| 996 | } |
| 997 | |
| 998 | llvm::TypeSize mlir::LLVM::getPrimitiveTypeSizeInBits(Type type) { |
| 999 | assert(isCompatibleType(type) && |
| 1000 | "expected a type compatible with the LLVM dialect" ); |
| 1001 | |
| 1002 | return llvm::TypeSwitch<Type, llvm::TypeSize>(type) |
| 1003 | .Case<BFloat16Type, Float16Type>( |
| 1004 | caseFn: [](Type) { return llvm::TypeSize::getFixed(ExactSize: 16); }) |
| 1005 | .Case<Float32Type>(caseFn: [](Type) { return llvm::TypeSize::getFixed(ExactSize: 32); }) |
| 1006 | .Case<Float64Type>(caseFn: [](Type) { return llvm::TypeSize::getFixed(ExactSize: 64); }) |
| 1007 | .Case<Float80Type>(caseFn: [](Type) { return llvm::TypeSize::getFixed(ExactSize: 80); }) |
| 1008 | .Case<Float128Type>(caseFn: [](Type) { return llvm::TypeSize::getFixed(ExactSize: 128); }) |
| 1009 | .Case(caseFn: [](IntegerType intTy) { |
| 1010 | return llvm::TypeSize::getFixed(ExactSize: intTy.getWidth()); |
| 1011 | }) |
| 1012 | .Case(caseFn: [](LLVMByteType byteTy) { |
| 1013 | return llvm::TypeSize::getFixed(ExactSize: byteTy.getBitWidth()); |
| 1014 | }) |
| 1015 | .Case<LLVMPPCFP128Type>( |
| 1016 | caseFn: [](Type) { return llvm::TypeSize::getFixed(ExactSize: 128); }) |
| 1017 | .Case(caseFn: [](VectorType t) { |
| 1018 | assert(isCompatibleVectorType(t) && |
| 1019 | "unexpected incompatible with LLVM vector type" ); |
| 1020 | llvm::TypeSize elementSize = |
| 1021 | getPrimitiveTypeSizeInBits(type: t.getElementType()); |
| 1022 | return llvm::TypeSize(elementSize.getFixedValue() * t.getNumElements(), |
| 1023 | elementSize.isScalable()); |
| 1024 | }) |
| 1025 | .Default(defaultFn: [](Type ty) { |
| 1026 | assert( |
| 1027 | (llvm::isa<LLVMVoidType, LLVMLabelType, LLVMMetadataType, TokenType, |
| 1028 | LLVMStructType, LLVMArrayType, LLVMPointerType, |
| 1029 | LLVMFunctionType, LLVMTargetExtType>(ty)) && |
| 1030 | "unexpected missing support for primitive type" ); |
| 1031 | return llvm::TypeSize::getFixed(ExactSize: 0); |
| 1032 | }); |
| 1033 | } |
| 1034 | |
| 1035 | //===----------------------------------------------------------------------===// |
| 1036 | // LLVMDialect |
| 1037 | //===----------------------------------------------------------------------===// |
| 1038 | |
| 1039 | void LLVMDialect::registerTypes() { |
| 1040 | addTypes< |
| 1041 | #define GET_TYPEDEF_LIST |
| 1042 | #include "mlir/Dialect/LLVMIR/LLVMTypes.cpp.inc" |
| 1043 | |
| 1044 | >(); |
| 1045 | } |
| 1046 | |
| 1047 | Type LLVMDialect::parseType(DialectAsmParser &parser) const { |
| 1048 | return detail::parseType(parser); |
| 1049 | } |
| 1050 | |
| 1051 | void LLVMDialect::printType(Type type, DialectAsmPrinter &os) const { |
| 1052 | return detail::printType(type, printer&: os); |
| 1053 | } |
| 1054 | |