Skip to content

Commit

Permalink
[CIR] Track type sizes with module attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
AdUhTkJm committed Feb 24, 2025
1 parent 60126e9 commit 8f5faa7
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 16 deletions.
73 changes: 73 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,79 @@ def CIR_OptInfoAttr : CIR_Attr<"OptInfo", "opt_info"> {
let genVerifyDecl = 1;
}

//===----------------------------------------------------------------------===//
// TypeSizesInfoAttr
//===----------------------------------------------------------------------===//

def CIR_TypeSizesInfoAttr : CIR_Attr<"TypeSizesInfo", "type_sizes_info"> {
let summary = "the size of types in bits";
let description = [{
The `cir.type_sizes` attribute is attached to a module, recording lengths
of various types if their names don't include it.

It is worth noticing that size_t and pointers are considered to have the
same length in Clang IR.

Float and double types are represented by cir::SingleType and cir::
DoubleType respectively, whose constructos don't need the type size as an
argument. So their lengths are not stored here.

Examples:

```mlir
// sizeof(int) == 4, sizeof(size_t) == 8
module attributes {
cir.type_sizes = #cir.type_sizes<
char = 8,
int = 32,
size_t = 64
>
} {}
```
}];

let parameters = (ins "unsigned":$char_size,
"unsigned":$int_size,
"unsigned":$size_type_size);

let assemblyFormat = [{
`<`
`char` `=` $char_size `,`
`int` `=` $int_size `,`
`size_t` `=` $size_type_size
`>`
}];

let extraClassDeclaration = [{
unsigned getPointerSize() const { return getSizeTypeSize(); }

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

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

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

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

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

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


//===----------------------------------------------------------------------===//
// BoolAttr
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 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,7 @@ 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 getTypeSizesInfoAttrName() { return "cir.type_sizes_info"; }
static llvm::StringRef getSOBAttrName() { return "cir.sob"; }
static llvm::StringRef getLangAttrName() { return "cir.lang"; }
static llvm::StringRef getTripleAttrName() { return "cir.triple"; }
Expand Down
29 changes: 15 additions & 14 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
openMPRuntime(new CIRGenOpenMPRuntime(*this)),
cudaRuntime(new CIRGenCUDARuntime(*this)) {

unsigned charSize = astContext.getTargetInfo().getCharWidth();
unsigned intSize = astContext.getTargetInfo().getIntWidth();
unsigned sizeTypeSize = astContext.getTargetInfo().getMaxPointerWidth();

auto typeSizeInfo = cir::TypeSizesInfoAttr::get(&mlirContext, charSize,
intSize, sizeTypeSize);
theModule->setAttr(cir::CIRDialect::getTypeSizesInfoAttrName(), typeSizeInfo);

// Initialize CIR signed integer types cache.
SInt8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/true);
SInt16Ty = cir::IntType::get(&getMLIRContext(), 16, /*isSigned=*/true);
Expand Down Expand Up @@ -146,19 +154,13 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
astContext.getTargetInfo().getPointerAlign(LangAS::Default))
.getQuantity();
SizeSizeInBytes =
astContext
.toCharUnitsFromBits(astContext.getTargetInfo().getMaxPointerWidth())
astContext.toCharUnitsFromBits(typeSizeInfo.getSizeTypeSize())
.getQuantity();
// TODO: IntAlignInBytes
UCharTy = cir::IntType::get(&getMLIRContext(),
astContext.getTargetInfo().getCharWidth(),
/*isSigned=*/false);
UIntTy = cir::IntType::get(&getMLIRContext(),
astContext.getTargetInfo().getIntWidth(),
/*isSigned=*/false);
UIntPtrTy = cir::IntType::get(&getMLIRContext(),
astContext.getTargetInfo().getMaxPointerWidth(),
/*isSigned=*/false);
UCharTy = typeSizeInfo.getUCharType(&getMLIRContext());
UIntTy = typeSizeInfo.getUIntType(&getMLIRContext());
// In CIRGenTypeCache, UIntPtrTy and SizeType are fields of the same union
UIntPtrTy = typeSizeInfo.getSizeType(&getMLIRContext());
UInt8PtrTy = builder.getPointerTo(UInt8Ty);
UInt8PtrPtrTy = builder.getPointerTo(UInt8PtrTy);
AllocaInt8PtrTy = UInt8PtrTy;
Expand All @@ -167,9 +169,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
// TODO: ConstGlobalsPtrTy
CIRAllocaAddressSpace = getTargetCIRGenInfo().getCIRAllocaAddressSpace();

PtrDiffTy = cir::IntType::get(&getMLIRContext(),
astContext.getTargetInfo().getMaxPointerWidth(),
/*isSigned=*/true);
PtrDiffTy = typeSizeInfo.getPtrDiffType(&getMLIRContext());

if (langOpts.OpenCL) {
createOpenCLRuntime();
Expand Down Expand Up @@ -197,6 +197,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
cir::LangAttr::get(&mlirContext, lang));
theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
builder.getStringAttr(getTriple().str()));

if (CGO.OptimizationLevel > 0 || CGO.OptimizeSize > 0)
theModule->setAttr(cir::CIRDialect::getOptInfoAttrName(),
cir::OptInfoAttr::get(&mlirContext,
Expand Down
1 change: 1 addition & 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
2 changes: 1 addition & 1 deletion clang/test/CIR/CodeGen/attribute-annotate-multiple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void foo(int i) __attribute__((annotate("withargfunc", "os", 23 )));
void bar() __attribute__((annotate("withargfunc", "os", 22))) {
}

// BEFORE: module @{{.*}}attribute-annotate-multiple.cpp" attributes {cir.lang =
// BEFORE: module @{{.*}}attribute-annotate-multiple.cpp" attributes {{{.*}}cir.lang =

// BEFORE: cir.global external @a = #cir.ptr<null> : !cir.ptr<!cir.double>
// BEFORE-SAME: [#cir.annotation<name = "withargs", args = ["21", 12 : i32]>]
Expand Down
6 changes: 6 additions & 0 deletions clang/test/CIR/CodeGen/dlti.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ void foo() {}

// CHECK: module @"{{.*}}dlti.c" attributes {
// CHECK-DAG: cir.sob = #cir.signed_overflow_behavior<undefined>,
// CHECK-DAG: cir.type_sizes_info =
// CHECK-DAG: #cir.type_sizes_info<
// CHECK-DAG: char = 8,
// CHECK-DAG: int = {{16|32}},
// CHECK-DAG: size_t = {{32|64}}
// CHECK-DAG: >
// CHECK-DAG: dlti.dl_spec =
// CHECK-DAG: #dlti.dl_spec<
// CHECK-DAG: i16 = dense<16> : vector<2xi64>,
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CIR/CodeGen/sourcelocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int s0(int a, int b) {
// CIR: #loc6 = loc("{{.*}}sourcelocation.cpp":6:19)
// CIR: #loc21 = loc(fused[#loc3, #loc4])
// CIR: #loc22 = loc(fused[#loc5, #loc6])
// CIR: module @"{{.*}}sourcelocation.cpp" attributes {cir.lang = #cir.lang<cxx>, cir.sob = #cir.signed_overflow_behavior<undefined>
// CIR: module @"{{.*}}sourcelocation.cpp" attributes {{{.*}}cir.lang = #cir.lang<cxx>, {{.*}}cir.sob = #cir.signed_overflow_behavior<undefined>
// CIR: cir.func @_Z2s0ii(%arg0: !s32i loc(fused[#loc3, #loc4]), %arg1: !s32i loc(fused[#loc5, #loc6])) -> !s32i
// CIR: %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init] {alignment = 4 : i64} loc(#loc21)
// CIR: %1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init] {alignment = 4 : i64} loc(#loc22)
Expand Down

0 comments on commit 8f5faa7

Please sign in to comment.