Skip to content

Commit 3e393d9

Browse files
authored
[CIR] Upstream enum support (#136807)
1 parent 2f69111 commit 3e393d9

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ struct MissingFeatures {
179179
static bool msabi() { return false; }
180180
static bool typeChecks() { return false; }
181181
static bool lambdaFieldToName() { return false; }
182+
static bool updateCompletedType() { return false; }
182183
static bool targetSpecificCXXABI() { return false; }
183184
static bool moduleNameHash() { return false; }
184185
static bool setDSOLocal() { return false; }

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ void CIRGenModule::emitTopLevelDecl(Decl *decl) {
787787
case Decl::OpenACCDeclare:
788788
emitGlobalOpenACCDecl(cast<OpenACCDeclareDecl>(decl));
789789
break;
790-
790+
case Decl::Enum:
791791
case Decl::UsingDirective: // using namespace X; [C++]
792792
case Decl::Typedef:
793793
case Decl::TypeAlias: // using foo = bar; [C++11]

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,19 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
420420
break;
421421
}
422422

423+
case Type::Enum: {
424+
// TODO(cir): Implement updateCompletedType for enums.
425+
assert(!cir::MissingFeatures::updateCompletedType());
426+
const EnumDecl *ED = cast<EnumType>(ty)->getDecl();
427+
if (auto integerType = ED->getIntegerType(); !integerType.isNull())
428+
return convertType(integerType);
429+
// Return a placeholder 'i32' type. This can be changed later when the
430+
// type is defined (see UpdateCompletedType), but is likely to be the
431+
// "right" answer.
432+
resultType = cgm.UInt32Ty;
433+
break;
434+
}
435+
423436
case Type::FunctionNoProto:
424437
case Type::FunctionProto:
425438
resultType = convertFunctionTypeInternal(type);

clang/test/CIR/CodeGen/basic.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,29 @@ size_type max_size(void) {
253253

254254
// OGCG: define{{.*}} i64 @max_size()
255255
// OGCG: ret i64 2305843009213693951
256+
// CHECK: cir.store %5, %0 : !u64i, !cir.ptr<!u64i>
257+
// CHECK: %6 = cir.load %0 : !cir.ptr<!u64i>, !u64i
258+
// CHECK: cir.return %6 : !u64i
259+
// CHECK: }
260+
261+
enum A {
262+
A_one,
263+
A_two
264+
};
265+
enum A a;
266+
267+
// CHECK: cir.global external @a = #cir.int<0> : !u32i
268+
269+
enum B : int;
270+
enum B b;
271+
272+
// CHECK: cir.global external @b = #cir.int<0> : !u32i
273+
274+
275+
enum C : int {
276+
C_one,
277+
C_two
278+
};
279+
enum C c;
280+
281+
// CHECK: cir.global external @c = #cir.int<0> : !u32i

clang/test/CIR/CodeGen/basic.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ size_type max_size() {
102102
// CHECK: %3 = cir.cast(integral, %2 : !s32i), !u64i
103103
// CHECK: %4 = cir.const #cir.int<8> : !u64i
104104
// CHECK: %5 = cir.binop(div, %3, %4) : !u64i
105+
// CHECK: cir.store %5, %0 : !u64i, !cir.ptr<!u64i>
106+
// CHECK: %6 = cir.load %0 : !cir.ptr<!u64i>, !u64i
107+
// CHECK: cir.return %6 : !u64i
108+
// CHECK: }
105109

106110
void ref_arg(int &x) {
107111
int y = x;
@@ -141,3 +145,29 @@ void ref_local(short x) {
141145
// CHECK: %[[Y_REF_ADDR:.*]] = cir.alloca !cir.ptr<!s16i>, !cir.ptr<!cir.ptr<!s16i>>, ["y", init, const] {alignment = 8 : i64}
142146
// CHECK: cir.store %[[ARG]], %[[X_ADDR]] : !s16i, !cir.ptr<!s16i>
143147
// CHECK: cir.store %[[X_ADDR]], %[[Y_REF_ADDR]] : !cir.ptr<!s16i>, !cir.ptr<!cir.ptr<!s16i>>
148+
149+
enum A {
150+
A_one,
151+
A_two
152+
};
153+
enum A a;
154+
155+
// CHECK: cir.global external @a = #cir.int<0> : !u32i
156+
157+
enum B : int;
158+
enum B b;
159+
160+
// CHECK: cir.global external @b = #cir.int<0> : !s32i
161+
162+
enum C : int {
163+
C_one,
164+
C_two
165+
};
166+
enum C c;
167+
168+
// CHECK: cir.global external @c = #cir.int<0> : !s32i
169+
170+
enum class D : int;
171+
enum D d;
172+
173+
// CHECK: cir.global external @d = #cir.int<0> : !s32i

0 commit comments

Comments
 (0)