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"
17
18namespace cir {
19
20// TODO(cir): This might be replaced by a CIRDataLayout interface which can
21// provide the same functionalities.
23 // This is starting with the minimum functionality needed for code that is
24 // being upstreamed. Additional methods and members will be added as needed.
25 bool bigEndian = false;
26
27public:
28 mlir::DataLayout layout;
29
30 /// Constructs a DataLayout the module's data layout attribute.
31 CIRDataLayout(mlir::ModuleOp modOp);
32
33 /// Parse a data layout string (with fallback to default values).
34 void reset(mlir::DataLayoutSpecInterface spec);
35
36 bool isBigEndian() const { return bigEndian; }
37
38 /// Internal helper method that returns requested alignment for type.
39 llvm::Align getAlignment(mlir::Type ty, bool useABIAlign) const;
40
41 llvm::Align getABITypeAlign(mlir::Type ty) const {
42 return getAlignment(ty, true);
43 }
44
45 /// Returns the maximum number of bytes that may be overwritten by
46 /// storing the specified type.
47 ///
48 /// If Ty is a scalable vector type, the scalable property will be set and
49 /// the runtime size will be a positive integer multiple of the base size.
50 ///
51 /// For example, returns 5 for i36 and 10 for x86_fp80.
52 llvm::TypeSize getTypeStoreSize(mlir::Type ty) const {
53 llvm::TypeSize baseSize = getTypeSizeInBits(ty);
54 return {llvm::divideCeil(baseSize.getKnownMinValue(), 8),
55 baseSize.isScalable()};
56 }
57
58 /// Returns the offset in bytes between successive objects of the
59 /// specified type, including alignment padding.
60 ///
61 /// If Ty is a scalable vector type, the scalable property will be set and
62 /// the runtime size will be a positive integer multiple of the base size.
63 ///
64 /// This is the amount that alloca reserves for this type. For example,
65 /// returns 12 or 16 for x86_fp80, depending on alignment.
66 llvm::TypeSize getTypeAllocSize(mlir::Type ty) const {
67 // Round up to the next alignment boundary.
68 return llvm::alignTo(getTypeStoreSize(ty), getABITypeAlign(ty).value());
69 }
70
71 /// Returns the offset in bits between successive objects of the
72 /// specified type, including alignment padding; always a multiple of 8.
73 ///
74 /// If Ty is a scalable vector type, the scalable property will be set and
75 /// the runtime size will be a positive integer multiple of the base size.
76 ///
77 /// This is the amount that alloca reserves for this type. For example,
78 /// returns 96 or 128 for x86_fp80, depending on alignment.
79 llvm::TypeSize getTypeAllocSizeInBits(mlir::Type ty) const {
80 return 8 * getTypeAllocSize(ty);
81 }
82
83 llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const;
84};
85
86} // namespace cir
87
88#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 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
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.