Skip to content

Conversation

moar55
Copy link
Contributor

@moar55 moar55 commented Oct 15, 2025

This commit adds the RTTI support for enum in the vtable.

Issue #163601

@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Oct 15, 2025
@moar55 moar55 changed the title #163601: Add name for enum type in vtable [CIR] #163601: Add name for enum type in vtable Oct 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 15, 2025

@llvm/pr-subscribers-clangir

Author: Omar Hossam (moar55)

Changes

This commit adds the RTTI support for enum in the vtable.


Full diff: https://github.com/llvm/llvm-project/pull/163612.diff

2 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp (+1-2)
  • (modified) clang/test/CIR/CodeGen/throws.cpp (+49)
diff --git a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
index 9bb1fe1d49a25..276675cbca5f5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
@@ -952,8 +952,7 @@ const char *vTableClassNameForType(const CIRGenModule &cgm, const Type *ty) {
     break;
 
   case Type::Enum:
-    cgm.errorNYI("VTableClassNameForType: Enum");
-    break;
+    return "_ZTVN10__cxxabiv116__enum_type_infoE";
 
   case Type::Record: {
     const CXXRecordDecl *rd =
diff --git a/clang/test/CIR/CodeGen/throws.cpp b/clang/test/CIR/CodeGen/throws.cpp
index 4255d436d4a32..f8aa965b1a4aa 100644
--- a/clang/test/CIR/CodeGen/throws.cpp
+++ b/clang/test/CIR/CodeGen/throws.cpp
@@ -144,3 +144,52 @@ void throw_complex_expr() {
 // OGCG: store float 0x3FF19999A0000000, ptr %[[EXCEPTION_REAL]], align 16
 // OGCG: store float 0x40019999A0000000, ptr %[[EXCEPTION_IMAG]], align 4
 // OGCG: call void @__cxa_throw(ptr %[[EXCEPTION_ADDR]], ptr @_ZTICf, ptr null)
+
+
+void throw_enum_expr() {
+  enum Test {
+    TestA,
+    TestB
+  };
+  throw Test::TestA;
+}
+
+//CIR: %[[EXCEPTION_ADDR:.*]]  = cir.alloc.exception 4 -> !cir.ptr<!u32i>
+//CIR: %[[EXCEPTION_VALUE:.*]] = cir.const #cir.int<0> : !u32i
+//CIR: cir.store{{.*}} %[[EXCEPTION_VALUE]], %[[EXCEPTION_ADDR]] : !u32i, !cir.ptr<!u32i>
+//CIR: cir.throw %[[EXCEPTION_ADDR]] : !cir.ptr<!u32i>, @_ZTIZ15throw_enum_exprvE4Test
+//CIR: cir.unreachable
+
+//LLVM: %[[EXCEPTION_ADDR:.*]] = call ptr @__cxa_allocate_exception(i64 4)
+//LLVM: store i32 0, ptr %[[EXCEPTION_ADDR]], align 16
+//LLVM: call void @__cxa_throw(ptr %[[EXCEPTION_ADDR]], ptr @_ZTIZ15throw_enum_exprvE4Test, ptr null)
+//LLVM: unreachable
+
+//OGCG: %[[EXCEPTION_ADDR:.*]]  = call ptr @__cxa_allocate_exception(i64 4)
+//OGCG: store i32 0, ptr %[[EXCEPTION_ADDR]], align 16
+//OGCG: call void @__cxa_throw(ptr %[[EXCEPTION_ADDR]], ptr @_ZTIZ15throw_enum_exprvE4Test, ptr null)
+//OGCG: unreachable
+
+void throw_enum_class_expr() {
+  enum class Test {
+    TestA,
+    TestB
+  };
+  throw Test::TestA;
+}
+
+//CIR: %[[EXCEPTION_ADDR:.*]]  = cir.alloc.exception 4 -> !cir.ptr<!s32i>
+//CIR: %[[EXCEPTION_VALUE:.*]] = cir.const #cir.int<0> : !s32i
+//CIR: cir.store{{.*}} %[[EXCEPTION_VALUE]], %[[EXCEPTION_ADDR]] : !s32i, !cir.ptr<!s32i>
+//CIR: cir.throw %[[EXCEPTION_ADDR]] : !cir.ptr<!s32i>, @_ZTIZ21throw_enum_class_exprvE4Test
+//CIR: cir.unreachable
+
+//LLVM: %[[EXCEPTION_ADDR:.*]] = call ptr @__cxa_allocate_exception(i64 4)
+//LLVM: store i32 0, ptr %[[EXCEPTION_ADDR]], align 16
+//LLVM: call void @__cxa_throw(ptr %[[EXCEPTION_ADDR]], ptr @_ZTIZ21throw_enum_class_exprvE4Test, ptr null)
+//LLVM: unreachable
+
+//OGCG: %[[EXCEPTION_ADDR:.*]]  = call ptr @__cxa_allocate_exception(i64 4)
+//OGCG: store i32 0, ptr %[[EXCEPTION_ADDR]], align 16
+//OGCG: call void @__cxa_throw(ptr %[[EXCEPTION_ADDR]], ptr @_ZTIZ21throw_enum_class_exprvE4Test, ptr null)
+//OGCG: unreachable

@llvmbot
Copy link
Member

llvmbot commented Oct 15, 2025

@llvm/pr-subscribers-clang

Author: Omar Hossam (moar55)

Changes

This commit adds the RTTI support for enum in the vtable.


Full diff: https://github.com/llvm/llvm-project/pull/163612.diff

2 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp (+1-2)
  • (modified) clang/test/CIR/CodeGen/throws.cpp (+49)
diff --git a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
index 9bb1fe1d49a25..276675cbca5f5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
@@ -952,8 +952,7 @@ const char *vTableClassNameForType(const CIRGenModule &cgm, const Type *ty) {
     break;
 
   case Type::Enum:
-    cgm.errorNYI("VTableClassNameForType: Enum");
-    break;
+    return "_ZTVN10__cxxabiv116__enum_type_infoE";
 
   case Type::Record: {
     const CXXRecordDecl *rd =
diff --git a/clang/test/CIR/CodeGen/throws.cpp b/clang/test/CIR/CodeGen/throws.cpp
index 4255d436d4a32..f8aa965b1a4aa 100644
--- a/clang/test/CIR/CodeGen/throws.cpp
+++ b/clang/test/CIR/CodeGen/throws.cpp
@@ -144,3 +144,52 @@ void throw_complex_expr() {
 // OGCG: store float 0x3FF19999A0000000, ptr %[[EXCEPTION_REAL]], align 16
 // OGCG: store float 0x40019999A0000000, ptr %[[EXCEPTION_IMAG]], align 4
 // OGCG: call void @__cxa_throw(ptr %[[EXCEPTION_ADDR]], ptr @_ZTICf, ptr null)
+
+
+void throw_enum_expr() {
+  enum Test {
+    TestA,
+    TestB
+  };
+  throw Test::TestA;
+}
+
+//CIR: %[[EXCEPTION_ADDR:.*]]  = cir.alloc.exception 4 -> !cir.ptr<!u32i>
+//CIR: %[[EXCEPTION_VALUE:.*]] = cir.const #cir.int<0> : !u32i
+//CIR: cir.store{{.*}} %[[EXCEPTION_VALUE]], %[[EXCEPTION_ADDR]] : !u32i, !cir.ptr<!u32i>
+//CIR: cir.throw %[[EXCEPTION_ADDR]] : !cir.ptr<!u32i>, @_ZTIZ15throw_enum_exprvE4Test
+//CIR: cir.unreachable
+
+//LLVM: %[[EXCEPTION_ADDR:.*]] = call ptr @__cxa_allocate_exception(i64 4)
+//LLVM: store i32 0, ptr %[[EXCEPTION_ADDR]], align 16
+//LLVM: call void @__cxa_throw(ptr %[[EXCEPTION_ADDR]], ptr @_ZTIZ15throw_enum_exprvE4Test, ptr null)
+//LLVM: unreachable
+
+//OGCG: %[[EXCEPTION_ADDR:.*]]  = call ptr @__cxa_allocate_exception(i64 4)
+//OGCG: store i32 0, ptr %[[EXCEPTION_ADDR]], align 16
+//OGCG: call void @__cxa_throw(ptr %[[EXCEPTION_ADDR]], ptr @_ZTIZ15throw_enum_exprvE4Test, ptr null)
+//OGCG: unreachable
+
+void throw_enum_class_expr() {
+  enum class Test {
+    TestA,
+    TestB
+  };
+  throw Test::TestA;
+}
+
+//CIR: %[[EXCEPTION_ADDR:.*]]  = cir.alloc.exception 4 -> !cir.ptr<!s32i>
+//CIR: %[[EXCEPTION_VALUE:.*]] = cir.const #cir.int<0> : !s32i
+//CIR: cir.store{{.*}} %[[EXCEPTION_VALUE]], %[[EXCEPTION_ADDR]] : !s32i, !cir.ptr<!s32i>
+//CIR: cir.throw %[[EXCEPTION_ADDR]] : !cir.ptr<!s32i>, @_ZTIZ21throw_enum_class_exprvE4Test
+//CIR: cir.unreachable
+
+//LLVM: %[[EXCEPTION_ADDR:.*]] = call ptr @__cxa_allocate_exception(i64 4)
+//LLVM: store i32 0, ptr %[[EXCEPTION_ADDR]], align 16
+//LLVM: call void @__cxa_throw(ptr %[[EXCEPTION_ADDR]], ptr @_ZTIZ21throw_enum_class_exprvE4Test, ptr null)
+//LLVM: unreachable
+
+//OGCG: %[[EXCEPTION_ADDR:.*]]  = call ptr @__cxa_allocate_exception(i64 4)
+//OGCG: store i32 0, ptr %[[EXCEPTION_ADDR]], align 16
+//OGCG: call void @__cxa_throw(ptr %[[EXCEPTION_ADDR]], ptr @_ZTIZ21throw_enum_class_exprvE4Test, ptr null)
+//OGCG: unreachable

@moar55 moar55 changed the title [CIR] #163601: Add name for enum type in vtable [CIR]: Add name for enum type in vtable Oct 15, 2025
@moar55 moar55 changed the title [CIR]: Add name for enum type in vtable [CIR] Add name for enum type in vtable Oct 15, 2025
Copy link
Member

@AmrDeveloper AmrDeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this

// OGCG: store float 0x3FF19999A0000000, ptr %[[EXCEPTION_REAL]], align 16
// OGCG: store float 0x40019999A0000000, ptr %[[EXCEPTION_IMAG]], align 4
// OGCG: call void @__cxa_throw(ptr %[[EXCEPTION_ADDR]], ptr @_ZTICf, ptr null)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this extra line

throw Test::TestA;
}

//CIR: %[[EXCEPTION_ADDR:.*]] = cir.alloc.exception 4 -> !cir.ptr<!u32i>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//CIR: %[[EXCEPTION_ADDR:.*]] = cir.alloc.exception 4 -> !cir.ptr<!u32i>
//CIR: %[[EXCEPTION_ADDR:.*]] = cir.alloc.exception 4 -> !cir.ptr<!u32i>

throw Test::TestA;
}

//CIR: %[[EXCEPTION_ADDR:.*]] = cir.alloc.exception 4 -> !cir.ptr<!s32i>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//CIR: %[[EXCEPTION_ADDR:.*]] = cir.alloc.exception 4 -> !cir.ptr<!s32i>
//CIR: %[[EXCEPTION_ADDR:.*]] = cir.alloc.exception 4 -> !cir.ptr<!s32i>

throw Test::TestA;
}

//CIR: %[[EXCEPTION_ADDR:.*]] = cir.alloc.exception 4 -> !cir.ptr<!u32i>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the same format of the test

//CIR : -> // CIR:

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once @AmrDeveloper concerns are addressed!

@moar55 moar55 force-pushed the support-enum-vtable-clangir branch from a21a9c2 to 10e2373 Compare October 17, 2025 07:47
Copy link
Member

@AmrDeveloper AmrDeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks!

@AmrDeveloper AmrDeveloper merged commit d6191b8 into llvm:main Oct 17, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants