[clang] [CIR] Fix speculative vtable emission check for inline virtual functions (PR #245845)

Konstantinos Parasyris via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 18:03:23 PST 2026


https://github.com/koparasy created https://github.com/llvm/llvm-project/pull/245845

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.

>From b5626dd27a3073822cbf411478a539972bcd655f Mon Sep 17 00:00:00 2001
From: Konstantinos Parasyris <konstantinos.parasyris at intel.com>
Date: Tue, 24 Nov 2026 17:59:23 -0800
Subject: [PATCH] [CIR] Fix speculative vtable emission check for inline
 virtual functions

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.
---
 clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp         |  2 +-
 clang/test/CodeGenCXX/vtable-available-externally.cpp | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
index e75a3319d82447..905adbfe77581d 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 66aeb63cf99096..ccd84360bb1972 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>



More information about the cfe-commits mailing list