Skip to content

[CIR] Fix speculative vtable emission check for inline virtual functions - #245845

Merged
koparasy merged 2 commits into
llvm:mainfrom
koparasy:cir/speculative-vtable-inline-check
Dec 1, 2026
Merged

koparasy merged 2 commits into
llvm:mainfrom
koparasy:cir/speculative-vtable-inline-check

Conversation

@koparasy

Copy link
Copy Markdown
Contributor

hasAnyUnusedVirtualInlineFunction looked up each inline virtual function as a cir::GlobalOp, which never matches a function, so every class appeared to have an unemitted inline virtual and canSpeculativelyEmitVTable always returned false. As a result CIR never emitted available_externally vtables at -O1 and above, preventing LLVM from devirtualizing calls whose target is known after inlining a constructor. Look the entry up as a cir::FuncOp, matching classic codegen.

hasAnyUnusedVirtualInlineFunction looked up each inline virtual function as a
cir::GlobalOp, which never matches a function, so every class appeared to have
an unemitted inline virtual and canSpeculativelyEmitVTable always returned
false. As a result CIR never emitted available_externally vtables at -O1 and
above, preventing LLVM from devirtualizing calls whose target is known after
inlining a constructor. Look the entry up as a cir::FuncOp, matching classic
codegen.
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Nov 25, 2026
@llvmorg-github-actions

llvmorg-github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clangir

Author: Konstantinos Parasyris (koparasy)

Changes

hasAnyUnusedVirtualInlineFunction looked up each inline virtual function as a cir::GlobalOp, which never matches a function, so every class appeared to have an unemitted inline virtual and canSpeculativelyEmitVTable always returned false. As a result CIR never emitted available_externally vtables at -O1 and above, preventing LLVM from devirtualizing calls whose target is known after inlining a constructor. Look the entry up as a cir::FuncOp, matching classic codegen.


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

2 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp (+1-1)
  • (modified) clang/test/CodeGenCXX/vtable-available-externally.cpp (+11)
diff --git a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
index e75a3319d8244..905adbfe77581 100644
--- a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
@@ -2139,7 +2139,7 @@ bool CIRGenItaniumCXXABI::hasAnyUnusedVirtualInlineFunction(
 
     StringRef name = cgm.getMangledName(
         vtableComponent.getGlobalDecl(/*HasVectorDeletingDtors=*/false));
-    auto entry = dyn_cast_or_null<cir::GlobalOp>(cgm.getGlobalValue(name));
+    auto entry = dyn_cast_or_null<cir::FuncOp>(cgm.getGlobalValue(name));
     // This checks if virtual inline function has already been emitted.
     // Note that it is possible that this inline function would be emitted
     // after trying to emit vtable speculatively. Because of this we do
diff --git a/clang/test/CodeGenCXX/vtable-available-externally.cpp b/clang/test/CodeGenCXX/vtable-available-externally.cpp
index 66aeb63cf9909..ccd84360bb197 100644
--- a/clang/test/CodeGenCXX/vtable-available-externally.cpp
+++ b/clang/test/CodeGenCXX/vtable-available-externally.cpp
@@ -15,6 +15,17 @@
 // RUN: FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST16 %s < %t.opt
 // RUN: FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST17 %s < %t.opt
 // RUN: FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-FORCE-EMIT %s < %t.vtable
+// RUN: %if cir-enabled %{ %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -std=c++98 -O2 -disable-llvm-passes -fclangir -emit-llvm -o %t.cir.opt %}
+// RUN: %if cir-enabled %{ FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST8 %s < %t.cir.opt %}
+// RUN: %if cir-enabled %{ FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST9 %s < %t.cir.opt %}
+// RUN: %if cir-enabled %{ FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST10 %s < %t.cir.opt %}
+// RUN: %if cir-enabled %{ FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST11 %s < %t.cir.opt %}
+// RUN: %if cir-enabled %{ FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST12 %s < %t.cir.opt %}
+// RUN: %if cir-enabled %{ FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST13 %s < %t.cir.opt %}
+// RUN: %if cir-enabled %{ FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST14 %s < %t.cir.opt %}
+// RUN: %if cir-enabled %{ FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST15 %s < %t.cir.opt %}
+// RUN: %if cir-enabled %{ FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST16 %s < %t.cir.opt %}
+// RUN: %if cir-enabled %{ FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST17 %s < %t.cir.opt %}
 
 
 #include <typeinfo>

@xlauko xlauko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM.

StringRef name = cgm.getMangledName(
vtableComponent.getGlobalDecl(/*HasVectorDeletingDtors=*/false));
auto entry = dyn_cast_or_null<cir::GlobalOp>(cgm.getGlobalValue(name));
auto entry = dyn_cast_or_null<cir::FuncOp>(cgm.getGlobalValue(name));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: cgm.lookupFuncOp(name) already does this lookup and cast.

// RUN: FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST16 %s < %t.opt
// RUN: FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-TEST17 %s < %t.opt
// RUN: FileCheck -allow-deprecated-dag-overlap --check-prefix=CHECK-FORCE-EMIT %s < %t.vtable
// RUN: %if cir-enabled %{ %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -std=c++98 -O2 -disable-llvm-passes -fclangir -emit-llvm -o %t.cir.opt %}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TEST1 and TEST5 (the -O0 %t run) also pass with -fclangir; could they get a CIR run too? TEST2 doesn't yet (@_ZTSN5Test21AE isn't emitted) and FORCE-EMIT hits the "strict vtable pointers for vbase" NYI, so a short comment on why those stay classic-only would help.

@koparasy
koparasy merged commit 8746851 into llvm:main Dec 1, 2026
9 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.

2 participants