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 abiOrPref) 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 llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const;
72};
73
74} // namespace cir
75
76#endif // CLANG_CIR_DIALECT_IR_CIRDATALAYOUT_H
llvm::Align getAlignment(mlir::Type ty, bool abiOrPref) const
Internal helper method that returns requested alignment for type.
mlir::DataLayout layout
Definition: CIRDataLayout.h:28
void reset(mlir::DataLayoutSpecInterface spec)
Parse a data layout string (with fallback to default values).
llvm::TypeSize getTypeAllocSize(mlir::Type ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: CIRDataLayout.h:66
bool isBigEndian() const
Definition: CIRDataLayout.h:36
llvm::Align getABITypeAlign(mlir::Type ty) const
Definition: CIRDataLayout.h:41
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.
Definition: CIRDataLayout.h:52
Definition: ABIArgInfo.h:22