Skip to content

Commit e915a6c

Browse files
committed
[CIR] Track size_t and int size with module attributes
1 parent c301b4a commit e915a6c

File tree

6 files changed

+91
-0
lines changed

6 files changed

+91
-0
lines changed

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,57 @@ def CIR_OptInfoAttr : CIR_Attr<"OptInfo", "opt_info"> {
122122
let genVerifyDecl = 1;
123123
}
124124

125+
//===----------------------------------------------------------------------===//
126+
// SizeTypeSizeAttr
127+
//===----------------------------------------------------------------------===//
128+
129+
def CIR_SizeTypeSizeAttr : CIR_Attr<"SizeTypeSize", "size_type_size"> {
130+
let summary = "the size of size_t in bits";
131+
let description = [{
132+
Attached to a module, to share information between CIR generation and
133+
later passes.
134+
135+
Examples:
136+
137+
```mlir
138+
// sizeof(size_t) == 8
139+
module attributes {cir.size_type_size = #cir.size_type_size<64>} {}
140+
```
141+
}];
142+
143+
let parameters = (ins "unsigned":$size);
144+
145+
let assemblyFormat = [{
146+
`<` $size `>`
147+
}];
148+
}
149+
150+
//===----------------------------------------------------------------------===//
151+
// IntSizeAttr
152+
//===----------------------------------------------------------------------===//
153+
154+
def CIR_IntSizeAttr : CIR_Attr<"IntSize", "int_size"> {
155+
let summary = "the size of int in bits";
156+
let description = [{
157+
Attached to a module, to share information between CIR generation and
158+
later passes.
159+
160+
Examples:
161+
162+
```mlir
163+
// sizeof(int) == 4
164+
module attributes {cir.int_size = #cir.int_size<32>} {}
165+
```
166+
}];
167+
168+
let parameters = (ins "unsigned":$size);
169+
170+
let assemblyFormat = [{
171+
`<` $size `>`
172+
}];
173+
}
174+
175+
125176
//===----------------------------------------------------------------------===//
126177
// BoolAttr
127178
//===----------------------------------------------------------------------===//

clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class CIRDataLayout {
3535
// The StructType -> StructLayout map.
3636
mutable void *LayoutMap = nullptr;
3737

38+
int intSize;
39+
int sizeTypeSize;
40+
3841
public:
3942
mlir::DataLayout layout;
4043

@@ -106,6 +109,10 @@ class CIRDataLayout {
106109
cir::IntType::get(Ty.getContext(), getPointerTypeSizeInBits(Ty), false);
107110
return IntTy;
108111
}
112+
113+
mlir::Type getIntType(mlir::MLIRContext *ctx) const;
114+
mlir::Type getSizeType(mlir::MLIRContext *ctx) const;
115+
mlir::Type getPtrDiffType(mlir::MLIRContext *ctx) const;
109116
};
110117

111118
/// Used to lazily calculate structure layout information for a target machine,

clang/include/clang/CIR/Dialect/IR/CIRDialect.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def CIR_Dialect : Dialect {
3434
// Names of CIR parameter attributes.
3535
static llvm::StringRef getSExtAttrName() { return "cir.signext"; }
3636
static llvm::StringRef getZExtAttrName() { return "cir.zeroext"; }
37+
static llvm::StringRef getSizeTypeSizeAttrName() { return "cir.size_type_size"; }
38+
static llvm::StringRef getIntSizeAttrName() { return "cir.int_size"; }
3739
static llvm::StringRef getSOBAttrName() { return "cir.sob"; }
3840
static llvm::StringRef getLangAttrName() { return "cir.lang"; }
3941
static llvm::StringRef getTripleAttrName() { return "cir.triple"; }

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
197197
cir::LangAttr::get(&mlirContext, lang));
198198
theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
199199
builder.getStringAttr(getTriple().str()));
200+
theModule->setAttr(
201+
cir::CIRDialect::getSizeTypeSizeAttrName(),
202+
cir::SizeTypeSizeAttr::get(
203+
&mlirContext, astContext.getTargetInfo().getMaxPointerWidth()));
204+
theModule->setAttr(
205+
cir::CIRDialect::getIntSizeAttrName(),
206+
cir::IntSizeAttr::get(&mlirContext,
207+
astContext.getTargetInfo().getIntWidth()));
200208
if (CGO.OptimizationLevel > 0 || CGO.OptimizeSize > 0)
201209
theModule->setAttr(cir::CIRDialect::getOptInfoAttrName(),
202210
cir::OptInfoAttr::get(&mlirContext,

clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
2+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
23
#include "clang/CIR/Dialect/IR/CIRTypes.h"
34
#include "clang/CIR/MissingFeatures.h"
45
#include "llvm/IR/DataLayout.h"
@@ -112,6 +113,14 @@ class StructLayoutMap {
112113

113114
CIRDataLayout::CIRDataLayout(mlir::ModuleOp modOp) : layout{modOp} {
114115
reset(modOp.getDataLayoutSpec());
116+
117+
auto intSizeAttr = mlir::cast<cir::IntSizeAttr>(
118+
modOp->getAttr(cir::CIRDialect::getIntSizeAttrName()));
119+
intSize = intSizeAttr.getSize();
120+
121+
auto sizeTypeSizeAttr = mlir::cast<cir::SizeTypeSizeAttr>(
122+
modOp->getAttr(cir::CIRDialect::getSizeTypeSizeAttrName()));
123+
sizeTypeSize = sizeTypeSizeAttr.getSize();
115124
}
116125

117126
void CIRDataLayout::reset(mlir::DataLayoutSpecInterface spec) {
@@ -220,3 +229,15 @@ llvm::TypeSize CIRDataLayout::getTypeSizeInBits(mlir::Type Ty) const {
220229

221230
return llvm::TypeSize::getFixed(layout.getTypeSizeInBits(Ty));
222231
}
232+
233+
mlir::Type CIRDataLayout::getPtrDiffType(mlir::MLIRContext *ctx) const {
234+
return cir::IntType::get(ctx, sizeTypeSize, /*signed=*/true);
235+
}
236+
237+
mlir::Type CIRDataLayout::getSizeType(mlir::MLIRContext *ctx) const {
238+
return cir::IntType::get(ctx, sizeTypeSize, /*signed=*/false);
239+
}
240+
241+
mlir::Type CIRDataLayout::getIntType(mlir::MLIRContext *ctx) const {
242+
return cir::IntType::get(ctx, intSize, /*signed=*/true);
243+
}

clang/test/CIR/CodeGen/dlti.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ void foo() {}
55

66
// CHECK: module @"{{.*}}dlti.c" attributes {
77
// CHECK-DAG: cir.sob = #cir.signed_overflow_behavior<undefined>,
8+
// CHECK-DAG: cir.int_size = #cir.int_size<{{16|32}}>,
9+
// CHECK-DAG: cir.size_type_size = #cir.size_type_size<{{32|64}}>,
810
// CHECK-DAG: dlti.dl_spec =
911
// CHECK-DAG: #dlti.dl_spec<
1012
// CHECK-DAG: i16 = dense<16> : vector<2xi64>,

0 commit comments

Comments
 (0)