| 1 | //===--- CIRDataLayoutSpec.cpp - DLTI data layout for CIR modules ---------===// |
| 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 attaches the DLTI data-layout spec to a CIR module. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/CIR/CIRDataLayoutSpec.h" |
| 14 | |
| 15 | #include "mlir/Dialect/DLTI/DLTI.h" |
| 16 | #include "mlir/IR/BuiltinOps.h" |
| 17 | #include "mlir/Target/LLVMIR/Import.h" |
| 18 | |
| 19 | #include "clang/CIR/Dialect/IR/CIRAttrs.h" |
| 20 | #include "clang/CIR/Dialect/IR/CIRTypes.h" |
| 21 | #include "clang/CIR/MissingFeatures.h" |
| 22 | #include "llvm/IR/DataLayout.h" |
| 23 | |
| 24 | void cir::setMLIRDataLayout(mlir::ModuleOp mod, const llvm::DataLayout &dl, |
| 25 | unsigned bitIntMaxAlign, unsigned maxVectorAlign) { |
| 26 | mlir::MLIRContext *mlirContext = mod.getContext(); |
| 27 | mlir::DataLayoutSpecInterface dlSpec = |
| 28 | mlir::translateDataLayout(dataLayout: dl, context: mlirContext); |
| 29 | |
| 30 | // Append the !cir.ptr-keyed #cir.ptr_spec entry. |
| 31 | // TODO(cir): only the default address space is recorded. |
| 32 | assert(!cir::MissingFeatures::dataLayoutPtrHandlingBasedOnLangAS()); |
| 33 | constexpr unsigned kBitsInByte = 8; |
| 34 | unsigned ptrSizeBits = dl.getPointerSizeInBits(/*AS=*/0); |
| 35 | unsigned ptrAbiBits = |
| 36 | dl.getPointerABIAlignment(/*AS=*/0).value() * kBitsInByte; |
| 37 | unsigned ptrPrefBits = |
| 38 | dl.getPointerPrefAlignment(/*AS=*/0).value() * kBitsInByte; |
| 39 | unsigned ptrIndexBits = dl.getIndexSizeInBits(/*AS=*/0); |
| 40 | auto ptrKey = cir::PointerType::get(pointee: cir::VoidType::get(ctx: mlirContext)); |
| 41 | auto ptrSpec = cir::PtrSpecAttr::get(context: mlirContext, size: ptrSizeBits, abi: ptrAbiBits, |
| 42 | preferred: ptrPrefBits, index: ptrIndexBits); |
| 43 | llvm::SmallVector<mlir::DataLayoutEntryInterface> entries( |
| 44 | dlSpec.getEntries().begin(), dlSpec.getEntries().end()); |
| 45 | entries.push_back(Elt: mlir::DataLayoutEntryAttr::get(key: ptrKey, value: ptrSpec)); |
| 46 | |
| 47 | // The key names the _BitInt class. max_align is |
| 48 | // TargetInfo::getBitIntMaxAlign(), in bits. |
| 49 | auto bitIntKey = cir::IntType::getBitIntMaxAlignKey(ctx: mlirContext); |
| 50 | auto bitIntSpec = cir::BitIntSpecAttr::get(context: mlirContext, max_align: bitIntMaxAlign); |
| 51 | entries.push_back(Elt: mlir::DataLayoutEntryAttr::get(key: bitIntKey, value: bitIntSpec)); |
| 52 | |
| 53 | // TargetInfo::getMaxVectorAlign() is 0 when the target imposes no cap. |
| 54 | // The key names the vector class. max_align is in bits. |
| 55 | if (maxVectorAlign) { |
| 56 | auto vectorKey = cir::VectorType::getMaxAlignKey(ctx: mlirContext); |
| 57 | auto vectorSpec = cir::VectorSpecAttr::get(context: mlirContext, max_align: maxVectorAlign); |
| 58 | entries.push_back(Elt: mlir::DataLayoutEntryAttr::get(key: vectorKey, value: vectorSpec)); |
| 59 | } |
| 60 | |
| 61 | mod->setAttr(name: mlir::DLTIDialect::kDataLayoutAttrName, |
| 62 | value: mlir::DataLayoutSpecAttr::get(context: mlirContext, entries)); |
| 63 | } |
| 64 | |