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, including the CIR-native
10// pointer and _BitInt entries, to a CIR module.
11//
12//===----------------------------------------------------------------------===//
13
15
16#include "mlir/Dialect/DLTI/DLTI.h"
17#include "mlir/IR/BuiltinOps.h"
18#include "mlir/Target/LLVMIR/Import.h"
19
23#include "llvm/IR/DataLayout.h"
24
25void cir::setMLIRDataLayout(mlir::ModuleOp mod, const llvm::DataLayout &dl,
26 unsigned bitIntMaxAlign) {
27 mlir::MLIRContext *mlirContext = mod.getContext();
28 mlir::DataLayoutSpecInterface dlSpec =
29 mlir::translateDataLayout(dl, mlirContext);
30
31 // Append the !cir.ptr-keyed #cir.ptr_spec entry.
32 // TODO(cir): only the default address space is recorded.
34 constexpr unsigned kBitsInByte = 8;
35 unsigned ptrSizeBits = dl.getPointerSizeInBits(/*AS=*/0);
36 unsigned ptrAbiBits =
37 dl.getPointerABIAlignment(/*AS=*/0).value() * kBitsInByte;
38 unsigned ptrPrefBits =
39 dl.getPointerPrefAlignment(/*AS=*/0).value() * kBitsInByte;
40 unsigned ptrIndexBits = dl.getIndexSizeInBits(/*AS=*/0);
41 auto ptrKey = cir::PointerType::get(cir::VoidType::get(mlirContext));
42 auto ptrSpec = cir::PtrSpecAttr::get(mlirContext, ptrSizeBits, ptrAbiBits,
43 ptrPrefBits, ptrIndexBits);
45 dlSpec.getEntries().begin(), dlSpec.getEntries().end());
46 entries.push_back(mlir::DataLayoutEntryAttr::get(ptrKey, ptrSpec));
47
48 // The key names the _BitInt class. max_align is
49 // TargetInfo::getBitIntMaxAlign(), in bits.
50 auto bitIntKey = cir::IntType::getBitIntMaxAlignKey(mlirContext);
51 auto bitIntSpec = cir::BitIntSpecAttr::get(mlirContext, bitIntMaxAlign);
52 entries.push_back(mlir::DataLayoutEntryAttr::get(bitIntKey, bitIntSpec));
53
54 mod->setAttr(mlir::DLTIDialect::kDataLayoutAttrName,
55 mlir::DataLayoutSpecAttr::get(mlirContext, entries));
56}
void setMLIRDataLayout(mlir::ModuleOp mod, const llvm::DataLayout &dl, unsigned bitIntMaxAlign)
Translate dl into a DLTI data-layout spec and attach it to mod.
static bool dataLayoutPtrHandlingBasedOnLangAS()