Repository navigation
[CIR] Fix speculative vtable emission check for inline virtual functions - #245845
Conversation
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.
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clangir Author: Konstantinos Parasyris (koparasy) ChangeshasAnyUnusedVirtualInlineFunction 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:
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>
|
| 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)); |
There was a problem hiding this comment.
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 %} |
There was a problem hiding this comment.
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.
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.