Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

//===----------------------------------------------------------------------===//
// TypeSizeInfoAttr
//===----------------------------------------------------------------------===//

def CIR_TypeSizeInfoAttr : CIR_Attr<"TypeSizeInfo", "type_size_info"> {
let summary = "the size of types in bits";
let description = [{
The `cir.type_size` 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_size = #cir.type_size<
char = 8,
int = 32,
size_t = 64
>
} {}
```
}];

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

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

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

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, getSizeTSize(), /*signed=*/false);
}

mlir::Type getPtrDiffType(mlir::MLIRContext *ctx) const {
return cir::IntType::get(ctx, getSizeTSize(), /*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 getTypeSizeInfoAttrName() { return "cir.type_size_info"; }
static llvm::StringRef getSOBAttrName() { return "cir.sob"; }
static llvm::StringRef getLangAttrName() { return "cir.lang"; }
static llvm::StringRef getTripleAttrName() { return "cir.triple"; }
Expand Down
30 changes: 15 additions & 15 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 sizeTSize = astContext.getTargetInfo().getMaxPointerWidth();

auto typeSizeInfo =
cir::TypeSizeInfoAttr::get(&mlirContext, charSize, intSize, sizeTSize);
theModule->setAttr(cir::CIRDialect::getTypeSizeInfoAttrName(), 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,12 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
astContext.getTargetInfo().getPointerAlign(LangAS::Default))
.getQuantity();
SizeSizeInBytes =
astContext
.toCharUnitsFromBits(astContext.getTargetInfo().getMaxPointerWidth())
.getQuantity();
astContext.toCharUnitsFromBits(typeSizeInfo.getSizeTSize()).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 +168,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 +196,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
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_size_info =
// CHECK-DAG: #cir.type_size_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