clang 24.0.0git
CIRDataLayoutSpec.cpp
Go to the documentation of this file.
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
14
15#include "mlir/Dialect/DLTI/DLTI.h"
16#include "mlir/IR/BuiltinOps.h"
17#include "mlir/Target/LLVMIR/Import.h"
18
22#include "llvm/IR/DataLayout.h"
23
24void 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(dl, mlirContext);
29
30 // Append the !cir.ptr-keyed #cir.ptr_spec entry.
31 // TODO(cir): only the default address space is recorded.
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(cir::VoidType::get(mlirContext));
41 auto ptrSpec = cir::PtrSpecAttr::get(mlirContext, ptrSizeBits, ptrAbiBits,
42 ptrPrefBits, ptrIndexBits);
44 dlSpec.getEntries().begin(), dlSpec.getEntries().end());
45 entries.push_back(mlir::DataLayoutEntryAttr::get(ptrKey, ptrSpec));
46
47 // The key names the _BitInt class. max_align is
48 // TargetInfo::getBitIntMaxAlign(), in bits.
49 auto bitIntKey = cir::IntType::getBitIntMaxAlignKey(mlirContext);
50 auto bitIntSpec = cir::BitIntSpecAttr::get(mlirContext, bitIntMaxAlign);
51 entries.push_back(mlir::DataLayoutEntryAttr::get(bitIntKey, 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(mlirContext);
57 auto vectorSpec = cir::VectorSpecAttr::get(mlirContext, maxVectorAlign);
58 entries.push_back(mlir::DataLayoutEntryAttr::get(vectorKey, vectorSpec));
59 }
60
61 mod->setAttr(mlir::DLTIDialect::kDataLayoutAttrName,
62 mlir::DataLayoutSpecAttr::get(mlirContext, entries));
63}
void setMLIRDataLayout(mlir::ModuleOp mod, const llvm::DataLayout &dl, unsigned bitIntMaxAlign, unsigned maxVectorAlign)
Translate dl into a DLTI data-layout spec and attach it to mod.
static bool dataLayoutPtrHandlingBasedOnLangAS()