Skip to content

Commit a8543dc

Browse files
committed
[CIR] Introduce type aliases for records
This introduces MLIR aliases for ClangIR record types.
1 parent 2ce97fd commit a8543dc

File tree

6 files changed

+38
-29
lines changed

6 files changed

+38
-29
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ struct CIROpAsmDialectInterface : public OpAsmDialectInterface {
3333
using OpAsmDialectInterface::OpAsmDialectInterface;
3434

3535
AliasResult getAlias(Type type, raw_ostream &os) const final {
36+
if (auto recordType = dyn_cast<cir::RecordType>(type)) {
37+
StringAttr nameAttr = recordType.getName();
38+
if (!nameAttr)
39+
os << "ty_anon_" << recordType.getKindAsStr();
40+
else
41+
os << "ty_" << nameAttr.getValue();
42+
return AliasResult::OverridableAlias;
43+
}
3644
if (auto intType = dyn_cast<cir::IntType>(type)) {
3745
// We only provide alias for standard integer types (i.e. integer types
3846
// whose width is a power of 2 and at least 8).

clang/test/CIR/CodeGen/struct.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77

88
// For LLVM IR checks, the structs are defined before the variables, so these
99
// checks are at the top.
10+
// CIR-DAG: !ty_IncompleteS = !cir.record<struct "IncompleteS" incomplete>
11+
// CIR-DAG: !ty_CompleteS = !cir.record<struct "CompleteS" {!s32i, !s8i}>
12+
// CIR-DAG: !ty_OuterS = !cir.record<struct "OuterS" {!ty_InnerS, !s32i}>
13+
// CIR-DAG: !ty_InnerS = !cir.record<struct "InnerS" {!s32i, !s8i}>
14+
// CIR-DAG: !ty_PackedS = !cir.record<struct "PackedS" packed {!s32i, !s8i}>
15+
// CIR-DAG: !ty_PackedAndPaddedS = !cir.record<struct "PackedAndPaddedS" packed padded {!s32i, !s8i, !u8i}>
16+
1017
// LLVM: %struct.CompleteS = type { i32, i8 }
1118
// LLVM: %struct.OuterS = type { %struct.InnerS, i32 }
1219
// LLVM: %struct.InnerS = type { i32, i8 }
1320
// LLVM: %struct.PackedS = type <{ i32, i8 }>
1421
// LLVM: %struct.PackedAndPaddedS = type <{ i32, i8, i8 }>
22+
1523
// OGCG: %struct.CompleteS = type { i32, i8 }
1624
// OGCG: %struct.OuterS = type { %struct.InnerS, i32 }
1725
// OGCG: %struct.InnerS = type { i32, i8 }
@@ -20,8 +28,7 @@
2028

2129
struct IncompleteS *p;
2230

23-
// CIR: cir.global external @p = #cir.ptr<null> : !cir.ptr<!cir.record<struct
24-
// CIR-SAME: "IncompleteS" incomplete>>
31+
// CIR: cir.global external @p = #cir.ptr<null> : !cir.ptr<!ty_IncompleteS>
2532
// LLVM: @p = dso_local global ptr null
2633
// OGCG: @p = global ptr null, align 8
2734

@@ -30,8 +37,7 @@ struct CompleteS {
3037
char b;
3138
} cs;
3239

33-
// CIR: cir.global external @cs = #cir.zero : !cir.record<struct
34-
// CIR-SAME: "CompleteS" {!s32i, !s8i}>
40+
// CIR: cir.global external @cs = #cir.zero : !ty_CompleteS
3541
// LLVM: @cs = dso_local global %struct.CompleteS zeroinitializer
3642
// OGCG: @cs = global %struct.CompleteS zeroinitializer, align 4
3743

@@ -47,8 +53,7 @@ struct OuterS {
4753

4854
struct OuterS os;
4955

50-
// CIR: cir.global external @os = #cir.zero : !cir.record<struct
51-
// CIR-SAME: "OuterS" {!cir.record<struct "InnerS" {!s32i, !s8i}>, !s32i}>
56+
// CIR: cir.global external @os = #cir.zero : !ty_OuterS
5257
// LLVM: @os = dso_local global %struct.OuterS zeroinitializer
5358
// OGCG: @os = global %struct.OuterS zeroinitializer, align 4
5459

@@ -60,8 +65,7 @@ struct PackedS {
6065
char a1;
6166
} ps;
6267

63-
// CIR: cir.global external @ps = #cir.zero : !cir.record<struct "PackedS"
64-
// CIR-SAME: packed {!s32i, !s8i}>
68+
// CIR: cir.global external @ps = #cir.zero : !ty_PackedS
6569
// LLVM: @ps = dso_local global %struct.PackedS zeroinitializer
6670
// OGCG: @ps = global %struct.PackedS zeroinitializer, align 1
6771

@@ -70,8 +74,7 @@ struct PackedAndPaddedS {
7074
char b1;
7175
} __attribute__((aligned(2))) pps;
7276

73-
// CIR: cir.global external @pps = #cir.zero : !cir.record<struct
74-
// CIR-SAME: "PackedAndPaddedS" packed padded {!s32i, !s8i, !u8i}>
77+
// CIR: cir.global external @pps = #cir.zero : !ty_PackedAndPaddedS
7578
// LLVM: @pps = dso_local global %struct.PackedAndPaddedS zeroinitializer
7679
// OGCG: @pps = global %struct.PackedAndPaddedS zeroinitializer, align 2
7780

@@ -82,9 +85,7 @@ void f(void) {
8285
}
8386

8487
// CIR: cir.func @f()
85-
// CIR-NEXT: cir.alloca !cir.ptr<!cir.record<struct "IncompleteS" incomplete>>,
86-
// CIR-SAME: !cir.ptr<!cir.ptr<!cir.record<struct
87-
// CIR-SAME: "IncompleteS" incomplete>>>, ["p"]
88+
// CIR-NEXT: cir.alloca !cir.ptr<!ty_IncompleteS>, !cir.ptr<!cir.ptr<!ty_IncompleteS>>, ["p"] {alignment = 8 : i64}
8889
// CIR-NEXT: cir.return
8990

9091
// LLVM: define void @f()
@@ -101,9 +102,7 @@ void f2(void) {
101102
}
102103

103104
// CIR: cir.func @f2()
104-
// CIR-NEXT: cir.alloca !cir.record<struct "CompleteS" {!s32i, !s8i}>,
105-
// CIR-SAME: !cir.ptr<!cir.record<struct "CompleteS" {!s32i, !s8i}>>,
106-
// CIR-SAME: ["s"] {alignment = 4 : i64}
105+
// CIR-NEXT: cir.alloca !ty_CompleteS, !cir.ptr<!ty_CompleteS>, ["s"] {alignment = 4 : i64}
107106
// CIR-NEXT: cir.return
108107

109108
// LLVM: define void @f2()

clang/test/CIR/CodeGen/struct.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
struct IncompleteS;
99
IncompleteS *p;
1010

11-
// CIR: cir.global external @p = #cir.ptr<null> : !cir.ptr<!cir.record<struct "IncompleteS" incomplete>>
11+
// CIR: cir.global external @p = #cir.ptr<null> : !cir.ptr<!ty_IncompleteS>
1212
// LLVM: @p = dso_local global ptr null
1313
// OGCG: @p = global ptr null, align 8
1414

@@ -17,8 +17,7 @@ void f(void) {
1717
}
1818

1919
// CIR: cir.func @f()
20-
// CIR-NEXT: cir.alloca !cir.ptr<!cir.record<struct "IncompleteS" incomplete>>,
21-
// CIR-SAME: !cir.ptr<!cir.ptr<!cir.record<struct "IncompleteS" incomplete>>>, ["p"]
20+
// CIR-NEXT: cir.alloca !cir.ptr<!ty_IncompleteS>, !cir.ptr<!cir.ptr<!ty_IncompleteS>>, ["p"]
2221
// CIR-NEXT: cir.return
2322

2423
// LLVM: define void @f()

clang/test/CIR/CodeGen/typedef.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ void local_typedef(void) {
1111
}
1212

1313
// CIR: cir.func @local_typedef()
14-
// CIR: cir.alloca !cir.record<struct "Struct" {!s32i}>,
15-
// CIR-SAME: !cir.ptr<!cir.record<struct "Struct" {!s32i}>>, ["s"]
16-
// CIR-SAME: {alignment = 4 : i64}
14+
// CIR: cir.alloca !ty_Struct, !cir.ptr<!ty_Struct>, ["s"] {alignment = 4 : i64}
1715
// CIR: cir.return
1816

1917
// LLVM: %struct.Struct = type { i32 }

clang/test/CIR/CodeGen/union.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
union IncompleteU *p;
99

10-
// CIR: cir.global external @p = #cir.ptr<null> : !cir.ptr<!cir.record<union "IncompleteU" incomplete>>
10+
// CIR: cir.global external @p = #cir.ptr<null> : !cir.ptr<!ty_IncompleteU>
1111
// LLVM: @p = dso_local global ptr null
1212
// OGCG: @p = global ptr null, align 8
1313

@@ -16,8 +16,7 @@ void f(void) {
1616
}
1717

1818
// CIR: cir.func @f()
19-
// CIR-NEXT: cir.alloca !cir.ptr<!cir.record<union "IncompleteU" incomplete>>,
20-
// CIR-SAME: !cir.ptr<!cir.ptr<!cir.record<union "IncompleteU" incomplete>>>, ["p"]
19+
// CIR-NEXT: cir.alloca !cir.ptr<!ty_IncompleteU>, !cir.ptr<!cir.ptr<!ty_IncompleteU>>, ["p"]
2120
// CIR-NEXT: cir.return
2221

2322
// LLVM: define void @f()

clang/test/CIR/IR/struct.cir

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
// RUN: cir-opt %s | FileCheck %s
22

3+
!ty_S = !cir.record<struct "S" incomplete>
4+
!ty_U = !cir.record<union "U" incomplete>
5+
6+
// CHECK: !ty_S = !cir.record<struct "S" incomplete>
7+
// CHECK: !ty_U = !cir.record<union "U" incomplete>
8+
39
module {
4-
cir.global external @p1 = #cir.ptr<null> : !cir.ptr<!cir.record<struct "S" incomplete>>
5-
cir.global external @p2 = #cir.ptr<null> : !cir.ptr<!cir.record<union "U" incomplete>>
10+
cir.global external @p1 = #cir.ptr<null> : !cir.ptr<!ty_S>
11+
cir.global external @p2 = #cir.ptr<null> : !cir.ptr<!ty_U>
612
}
713

8-
// CHECK: cir.global external @p1 = #cir.ptr<null> : !cir.ptr<!cir.record<struct "S" incomplete>>
9-
// CHECK: cir.global external @p2 = #cir.ptr<null> : !cir.ptr<!cir.record<union "U" incomplete>>
14+
// CHECK: cir.global external @p1 = #cir.ptr<null> : !cir.ptr<!ty_S>
15+
// CHECK: cir.global external @p2 = #cir.ptr<null> : !cir.ptr<!ty_U>

0 commit comments

Comments
 (0)