Skip to content

Commit

Permalink
[CIR] Track size_t and int size with module attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
AdUhTkJm committed Feb 22, 2025
1 parent c301b4a commit e915a6c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
51 changes: 51 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,57 @@ def CIR_OptInfoAttr : CIR_Attr<"OptInfo", "opt_info"> {
let genVerifyDecl = 1;
}

//===----------------------------------------------------------------------===//
// SizeTypeSizeAttr
//===----------------------------------------------------------------------===//

def CIR_SizeTypeSizeAttr : CIR_Attr<"SizeTypeSize", "size_type_size"> {
let summary = "the size of size_t in bits";
let description = [{
Attached to a module, to share information between CIR generation and
later passes.

Examples:

```mlir
// sizeof(size_t) == 8
module attributes {cir.size_type_size = #cir.size_type_size<64>} {}
```
}];

let parameters = (ins "unsigned":$size);

let assemblyFormat = [{
`<` $size `>`
}];
}

//===----------------------------------------------------------------------===//
// IntSizeAttr
//===----------------------------------------------------------------------===//

def CIR_IntSizeAttr : CIR_Attr<"IntSize", "int_size"> {
let summary = "the size of int in bits";
let description = [{
Attached to a module, to share information between CIR generation and
later passes.

Examples:

```mlir
// sizeof(int) == 4
module attributes {cir.int_size = #cir.int_size<32>} {}
```
}];

let parameters = (ins "unsigned":$size);

let assemblyFormat = [{
`<` $size `>`
}];
}


//===----------------------------------------------------------------------===//
// BoolAttr
//===----------------------------------------------------------------------===//
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class CIRDataLayout {
// The StructType -> StructLayout map.
mutable void *LayoutMap = nullptr;

int intSize;
int sizeTypeSize;

public:
mlir::DataLayout layout;

Expand Down Expand Up @@ -106,6 +109,10 @@ class CIRDataLayout {
cir::IntType::get(Ty.getContext(), getPointerTypeSizeInBits(Ty), false);
return IntTy;
}

mlir::Type getIntType(mlir::MLIRContext *ctx) const;
mlir::Type getSizeType(mlir::MLIRContext *ctx) const;
mlir::Type getPtrDiffType(mlir::MLIRContext *ctx) const;
};

/// Used to lazily calculate structure layout information for a target machine,
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def CIR_Dialect : Dialect {
// Names of CIR parameter attributes.
static llvm::StringRef getSExtAttrName() { return "cir.signext"; }
static llvm::StringRef getZExtAttrName() { return "cir.zeroext"; }
static llvm::StringRef getSizeTypeSizeAttrName() { return "cir.size_type_size"; }
static llvm::StringRef getIntSizeAttrName() { return "cir.int_size"; }
static llvm::StringRef getSOBAttrName() { return "cir.sob"; }
static llvm::StringRef getLangAttrName() { return "cir.lang"; }
static llvm::StringRef getTripleAttrName() { return "cir.triple"; }
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
cir::LangAttr::get(&mlirContext, lang));
theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
builder.getStringAttr(getTriple().str()));
theModule->setAttr(
cir::CIRDialect::getSizeTypeSizeAttrName(),
cir::SizeTypeSizeAttr::get(
&mlirContext, astContext.getTargetInfo().getMaxPointerWidth()));
theModule->setAttr(
cir::CIRDialect::getIntSizeAttrName(),
cir::IntSizeAttr::get(&mlirContext,
astContext.getTargetInfo().getIntWidth()));
if (CGO.OptimizationLevel > 0 || CGO.OptimizeSize > 0)
theModule->setAttr(cir::CIRDialect::getOptInfoAttrName(),
cir::OptInfoAttr::get(&mlirContext,
Expand Down
21 changes: 21 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/MissingFeatures.h"
#include "llvm/IR/DataLayout.h"
Expand Down Expand Up @@ -112,6 +113,14 @@ class StructLayoutMap {

CIRDataLayout::CIRDataLayout(mlir::ModuleOp modOp) : layout{modOp} {
reset(modOp.getDataLayoutSpec());

auto intSizeAttr = mlir::cast<cir::IntSizeAttr>(
modOp->getAttr(cir::CIRDialect::getIntSizeAttrName()));
intSize = intSizeAttr.getSize();

auto sizeTypeSizeAttr = mlir::cast<cir::SizeTypeSizeAttr>(
modOp->getAttr(cir::CIRDialect::getSizeTypeSizeAttrName()));
sizeTypeSize = sizeTypeSizeAttr.getSize();
}

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

return llvm::TypeSize::getFixed(layout.getTypeSizeInBits(Ty));
}

mlir::Type CIRDataLayout::getPtrDiffType(mlir::MLIRContext *ctx) const {
return cir::IntType::get(ctx, sizeTypeSize, /*signed=*/true);
}

mlir::Type CIRDataLayout::getSizeType(mlir::MLIRContext *ctx) const {
return cir::IntType::get(ctx, sizeTypeSize, /*signed=*/false);
}

mlir::Type CIRDataLayout::getIntType(mlir::MLIRContext *ctx) const {
return cir::IntType::get(ctx, intSize, /*signed=*/true);
}
2 changes: 2 additions & 0 deletions clang/test/CIR/CodeGen/dlti.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ void foo() {}

// CHECK: module @"{{.*}}dlti.c" attributes {
// CHECK-DAG: cir.sob = #cir.signed_overflow_behavior<undefined>,
// CHECK-DAG: cir.int_size = #cir.int_size<{{16|32}}>,
// CHECK-DAG: cir.size_type_size = #cir.size_type_size<{{32|64}}>,
// CHECK-DAG: dlti.dl_spec =
// CHECK-DAG: #dlti.dl_spec<
// CHECK-DAG: i16 = dense<16> : vector<2xi64>,
Expand Down

0 comments on commit e915a6c

Please sign in to comment.