| //===--- CIRDataLayoutSpec.cpp - DLTI data layout for CIR modules ---------===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| // | |
| // This file attaches the DLTI data-layout spec to a CIR module. | |
| // | |
| //===----------------------------------------------------------------------===// | |
| #include "clang/CIR/CIRDataLayoutSpec.h" | |
| #include "mlir/Dialect/DLTI/DLTI.h" | |
| #include "mlir/IR/BuiltinOps.h" | |
| #include "mlir/Target/LLVMIR/Import.h" | |
| #include "clang/CIR/Dialect/IR/CIRAttrs.h" | |
| #include "clang/CIR/Dialect/IR/CIRTypes.h" | |
| #include "clang/CIR/MissingFeatures.h" | |
| #include "llvm/IR/DataLayout.h" | |
| void cir::setMLIRDataLayout(mlir::ModuleOp mod, const llvm::DataLayout &dl, | |
| unsigned bitIntMaxAlign, unsigned maxVectorAlign) { | |
| mlir::MLIRContext *mlirContext = mod.getContext(); | |
| mlir::DataLayoutSpecInterface dlSpec = | |
| mlir::translateDataLayout(dl, mlirContext); | |
| // Append the !cir.ptr-keyed #cir.ptr_spec entry. | |
| // TODO(cir): only the default address space is recorded. | |
| assert(!cir::MissingFeatures::dataLayoutPtrHandlingBasedOnLangAS()); | |
| constexpr unsigned kBitsInByte = 8; | |
| unsigned ptrSizeBits = dl.getPointerSizeInBits(/*AS=*/0); | |
| unsigned ptrAbiBits = | |
| dl.getPointerABIAlignment(/*AS=*/0).value() * kBitsInByte; | |
| unsigned ptrPrefBits = | |
| dl.getPointerPrefAlignment(/*AS=*/0).value() * kBitsInByte; | |
| unsigned ptrIndexBits = dl.getIndexSizeInBits(/*AS=*/0); | |
| auto ptrKey = cir::PointerType::get(cir::VoidType::get(mlirContext)); | |
| auto ptrSpec = cir::PtrSpecAttr::get(mlirContext, ptrSizeBits, ptrAbiBits, | |
| ptrPrefBits, ptrIndexBits); | |
| llvm::SmallVector<mlir::DataLayoutEntryInterface> entries( | |
| dlSpec.getEntries().begin(), dlSpec.getEntries().end()); | |
| entries.push_back(mlir::DataLayoutEntryAttr::get(ptrKey, ptrSpec)); | |
| // The key names the _BitInt class. max_align is | |
| // TargetInfo::getBitIntMaxAlign(), in bits. | |
| auto bitIntKey = cir::IntType::getBitIntMaxAlignKey(mlirContext); | |
| auto bitIntSpec = cir::BitIntSpecAttr::get(mlirContext, bitIntMaxAlign); | |
| entries.push_back(mlir::DataLayoutEntryAttr::get(bitIntKey, bitIntSpec)); | |
| // TargetInfo::getMaxVectorAlign() is 0 when the target imposes no cap. | |
| // The key names the vector class. max_align is in bits. | |
| if (maxVectorAlign) { | |
| auto vectorKey = cir::VectorType::getMaxAlignKey(mlirContext); | |
| auto vectorSpec = cir::VectorSpecAttr::get(mlirContext, maxVectorAlign); | |
| entries.push_back(mlir::DataLayoutEntryAttr::get(vectorKey, vectorSpec)); | |
| } | |
| mod->setAttr(mlir::DLTIDialect::kDataLayoutAttrName, | |
| mlir::DataLayoutSpecAttr::get(mlirContext, entries)); | |
| } |