clang 22.0.0git
CIRDataLayout.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// Provides an LLVM-like API wrapper to DLTI and MLIR layout queries. This
9// makes it easier to port some of LLVM codegen layout logic to CIR.
10//===----------------------------------------------------------------------===//
11
12#ifndef CLANG_CIR_DIALECT_IR_CIRDATALAYOUT_H
13#define CLANG_CIR_DIALECT_IR_CIRDATALAYOUT_H
14
15#include "mlir/Dialect/DLTI/DLTI.h"
16#include "mlir/IR/BuiltinOps.h"
18
19namespace cir {
20
21// TODO(cir): This might be replaced by a CIRDataLayout interface which can
22// provide the same functionalities.
24 // This is starting with the minimum functionality needed for code that is
25 // being upstreamed. Additional methods and members will be added as needed.
26 bool bigEndian = false;
27
28public:
29 mlir::DataLayout layout;
30
31 /// Constructs a DataLayout the module's data layout attribute.
32 CIRDataLayout(mlir::ModuleOp modOp);
33
34 /// Parse a data layout string (with fallback to default values).
35 void reset(mlir::DataLayoutSpecInterface spec);
36
37 bool isBigEndian() const { return bigEndian; }
38
39 /// Internal helper method that returns requested alignment for type.
40 llvm::Align getAlignment(mlir::Type ty, bool useABIAlign) const;
41
42 llvm::Align getABITypeAlign(mlir::Type ty) const {
43 return getAlignment(ty, true);
44 }
45
46 /// Returns the maximum number of bytes that may be overwritten by
47 /// storing the specified type.
48 ///
49 /// If Ty is a scalable vector type, the scalable property will be set and
50 /// the runtime size will be a positive integer multiple of the base size.
51 ///
52 /// For example, returns 5 for i36 and 10 for x86_fp80.
53 llvm::TypeSize getTypeStoreSize(mlir::Type ty) const {
54 llvm::TypeSize baseSize = getTypeSizeInBits(ty);
55 return {llvm::divideCeil(baseSize.getKnownMinValue(), 8),
56 baseSize.isScalable()};
57 }
58
59 /// Returns the offset in bytes between successive objects of the
60 /// specified type, including alignment padding.
61 ///
62 /// If Ty is a scalable vector type, the scalable property will be set and
63 /// the runtime size will be a positive integer multiple of the base size.
64 ///
65 /// This is the amount that alloca reserves for this type. For example,
66 /// returns 12 or 16 for x86_fp80, depending on alignment.
67 llvm::TypeSize getTypeAllocSize(mlir::Type ty) const {
68 // Round up to the next alignment boundary.
69 return llvm::alignTo(getTypeStoreSize(ty), getABITypeAlign(ty).value());
70 }
71
72 /// Returns the offset in bits between successive objects of the
73 /// specified type, including alignment padding; always a multiple of 8.
74 ///
75 /// If Ty is a scalable vector type, the scalable property will be set and
76 /// the runtime size will be a positive integer multiple of the base size.
77 ///
78 /// This is the amount that alloca reserves for this type. For example,
79 /// returns 96 or 128 for x86_fp80, depending on alignment.
80 llvm::TypeSize getTypeAllocSizeInBits(mlir::Type ty) const {
81 return 8 * getTypeAllocSize(ty);
82 }
83
84 llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const;
85
86 llvm::TypeSize getPointerTypeSizeInBits(mlir::Type ty) const {
87 assert(mlir::isa<cir::PointerType>(ty) &&
88 "This should only be called with a pointer type");
89 return layout.getTypeSizeInBits(ty);
90 }
91
92 mlir::Type getIntPtrType(mlir::Type ty) const {
93 assert(mlir::isa<cir::PointerType>(ty) && "Expected pointer type");
94 return cir::IntType::get(ty.getContext(), getPointerTypeSizeInBits(ty),
95 false);
96 }
97};
98
99} // namespace cir
100
101#endif // CLANG_CIR_DIALECT_IR_CIRDATALAYOUT_H
mlir::DataLayout layout
void reset(mlir::DataLayoutSpecInterface spec)
Parse a data layout string (with fallback to default values).
CIRDataLayout(mlir::ModuleOp modOp)
Constructs a DataLayout the module's data layout attribute.
llvm::TypeSize getPointerTypeSizeInBits(mlir::Type ty) const
llvm::TypeSize getTypeAllocSize(mlir::Type ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
bool isBigEndian() const
llvm::Align getABITypeAlign(mlir::Type ty) const
mlir::Type getIntPtrType(mlir::Type ty) const
llvm::Align getAlignment(mlir::Type ty, bool useABIAlign) const
Internal helper method that returns requested alignment for type.
llvm::TypeSize getTypeAllocSizeInBits(mlir::Type ty) const
Returns the offset in bits between successive objects of the specified type, including alignment padd...
llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const
llvm::TypeSize getTypeStoreSize(mlir::Type ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.