clang 24.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
28 unsigned programAddrSpace = 0;
29
30 /// TargetInfo::getBitIntMaxAlign(), in bits, from the module's
31 /// #cir.bitint_spec entry.
32 uint64_t bitIntMaxAlignBits = 0;
33
34 /// TargetInfo::getMaxVectorAlign(), in bits, from the module's
35 /// #cir.vector_spec entry, or 0 when there is no entry.
36 uint64_t vectorMaxAlignBits = 0;
37
38 /// The module whose cir.record_layouts entries give record alignments.
39 mlir::ModuleOp module;
40
41public:
42 mlir::DataLayout layout;
43
44 /// Constructs a DataLayout the module's data layout attribute.
45 CIRDataLayout(mlir::ModuleOp modOp);
46
47 /// Parse a data layout string (with fallback to default values).
48 void reset(mlir::DataLayoutSpecInterface spec);
49
50 bool isBigEndian() const { return bigEndian; }
51
52 unsigned getProgramAddressSpace() const { return programAddrSpace; }
53
54 /// Internal helper method that returns requested alignment for type.
55 llvm::Align getAlignment(mlir::Type ty, bool useABIAlign) const;
56
57 llvm::Align getABITypeAlign(mlir::Type ty) const {
58 return getAlignment(ty, true);
59 }
60
61 /// Returns the alignment, in bytes, that the source language requires for
62 /// an object of type \p ty. ABI classification, coercion temporaries, and
63 /// accesses with no explicit alignment use it. The ABI alignment of \p ty is
64 /// the alignment of the memory representation \p ty lowers to, which record
65 /// layout and array strides use.
66 uint64_t getTypeLanguageAlignment(mlir::Type ty) const;
67
68 /// Returns the maximum number of bytes that may be overwritten by
69 /// storing the specified type.
70 ///
71 /// If Ty is a scalable vector type, the scalable property will be set and
72 /// the runtime size will be a positive integer multiple of the base size.
73 ///
74 /// For example, returns 5 for i36 and 10 for x86_fp80.
75 llvm::TypeSize getTypeStoreSize(mlir::Type ty) const {
76 llvm::TypeSize baseSize = getTypeSizeInBits(ty);
77 return {llvm::divideCeil(baseSize.getKnownMinValue(), 8),
78 baseSize.isScalable()};
79 }
80
81 /// Returns the maximum number of bits that may be overwritten by
82 /// storing the specified type; always a multiple of 8.
83 ///
84 /// If Ty is a scalable vector type, the scalable property will be set and
85 /// the runtime size will be a positive integer multiple of the base size.
86 ///
87 /// For example, returns 40 for i36 and 80 for x86_fp80.
88 llvm::TypeSize getTypeStoreSizeInBits(mlir::Type ty) const {
89 llvm::TypeSize baseSize = getTypeSizeInBits(ty);
90 uint64_t alignedSizeInBits =
91 llvm::alignToPowerOf2(baseSize.getKnownMinValue(), 8);
92 return {alignedSizeInBits, baseSize.isScalable()};
93 }
94
95 /// Returns the offset in bytes between successive objects of the
96 /// specified type, including alignment padding.
97 ///
98 /// If Ty is a scalable vector type, the scalable property will be set and
99 /// the runtime size will be a positive integer multiple of the base size.
100 ///
101 /// This is the amount that alloca reserves for this type. For example,
102 /// returns 12 or 16 for x86_fp80, depending on alignment.
103 llvm::TypeSize getTypeAllocSize(mlir::Type ty) const {
104 // Round up to the next alignment boundary.
105 return llvm::alignTo(getTypeStoreSize(ty), getABITypeAlign(ty).value());
106 }
107
108 /// Returns the offset in bits between successive objects of the
109 /// specified type, including alignment padding; always a multiple of 8.
110 ///
111 /// If Ty is a scalable vector type, the scalable property will be set and
112 /// the runtime size will be a positive integer multiple of the base size.
113 ///
114 /// This is the amount that alloca reserves for this type. For example,
115 /// returns 96 or 128 for x86_fp80, depending on alignment.
116 llvm::TypeSize getTypeAllocSizeInBits(mlir::Type ty) const {
117 return 8 * getTypeAllocSize(ty);
118 }
119
120 llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const;
121
122 llvm::TypeSize getPointerTypeSizeInBits(mlir::Type ty) const {
123 assert(mlir::isa<cir::PointerType>(ty) &&
124 "This should only be called with a pointer type");
125 return layout.getTypeSizeInBits(ty);
126 }
127
128 mlir::Type getIntPtrType(mlir::Type ty) const {
129 assert(mlir::isa<cir::PointerType>(ty) && "Expected pointer type");
130 return cir::IntType::get(ty.getContext(), getPointerTypeSizeInBits(ty),
131 false);
132 }
133
134 /// Returns true if no extra padding bits are needed when storing the
135 /// specified type.
136 ///
137 /// For example, returns false for i19 that has a 24-bit store size.
138 bool typeSizeEqualsStoreSize(mlir::Type ty) const {
140 }
141
142 mlir::ptr::MemorySpaceAttrInterface
143 getAllocaAddrSpace(mlir::MLIRContext *ctx) {
144 auto allocaASAttr = mlir::dyn_cast_if_present<mlir::IntegerAttr>(
145 layout.getAllocaMemorySpace());
146 if (!allocaASAttr)
147 return {};
148 return cir::TargetAddressSpaceAttr::get(ctx, allocaASAttr.getUInt());
149 }
150
151 mlir::ptr::MemorySpaceAttrInterface
152 getDefaultGlobalsAddrSpace(mlir::MLIRContext *ctx) const {
153 auto globalsASAttr = mlir::dyn_cast_if_present<mlir::IntegerAttr>(
154 layout.getGlobalMemorySpace());
155 if (!globalsASAttr || globalsASAttr.getUInt() == 0)
156 return {};
157 return cir::TargetAddressSpaceAttr::get(ctx, globalsASAttr.getUInt());
158 }
159};
160
161} // namespace cir
162
163#endif // CLANG_CIR_DIALECT_IR_CIRDATALAYOUT_H
mlir::ptr::MemorySpaceAttrInterface getAllocaAddrSpace(mlir::MLIRContext *ctx)
uint64_t getTypeLanguageAlignment(mlir::Type ty) const
Returns the alignment, in bytes, that the source language requires for an object of type ty.
mlir::DataLayout layout
mlir::ptr::MemorySpaceAttrInterface getDefaultGlobalsAddrSpace(mlir::MLIRContext *ctx) const
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...
llvm::TypeSize getTypeStoreSizeInBits(mlir::Type ty) const
Returns the maximum number of bits that may be overwritten by storing the specified type; always a mu...
unsigned getProgramAddressSpace() const
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...
bool typeSizeEqualsStoreSize(mlir::Type ty) const
Returns true if no extra padding bits are needed when storing the specified type.
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.
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t