aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ProfileData/InstrProfTest.cpp
diff options
context:
space:
mode:
authorMingming Liu <mingmingl@google.com>2024-05-09 10:41:23 -0700
committerGitHub <noreply@github.com>2024-05-09 10:41:23 -0700
commit98c1ba460a697110c64f6d1dd362dcf7088a13ca (patch)
treeec2a7a98293fc4930ee7f4ca1c212ba2f7f6bb4e /llvm/unittests/ProfileData/InstrProfTest.cpp
parentb3a835e129ed8a67cf393f9ee26989b36a3eff1c (diff)
downloadllvm-98c1ba460a697110c64f6d1dd362dcf7088a13ca.zip
llvm-98c1ba460a697110c64f6d1dd362dcf7088a13ca.tar.gz
llvm-98c1ba460a697110c64f6d1dd362dcf7088a13ca.tar.bz2
[InstrProf] Add vtables with type metadata into symtab (#81051)
The indirect-call-promotion pass will look up the vtable to find out the virtual function [1], and add vtable-derived information in icall candidate [2] for cost-benefit analysis. [1] https://github.com/llvm/llvm-project/pull/81442/files#diff-a95d1ac8a0da69713fcb3346135d4b219f0a73920318d2549495620ea215191bR395-R416 [2] https://github.com/llvm/llvm-project/pull/81442/files#diff-a95d1ac8a0da69713fcb3346135d4b219f0a73920318d2549495620ea215191bR195-R199
Diffstat (limited to 'llvm/unittests/ProfileData/InstrProfTest.cpp')
-rw-r--r--llvm/unittests/ProfileData/InstrProfTest.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/llvm/unittests/ProfileData/InstrProfTest.cpp b/llvm/unittests/ProfileData/InstrProfTest.cpp
index 402de64..8f2c5ae 100644
--- a/llvm/unittests/ProfileData/InstrProfTest.cpp
+++ b/llvm/unittests/ProfileData/InstrProfTest.cpp
@@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
@@ -1730,6 +1732,34 @@ TEST(SymtabTest, instr_prof_symtab_module_test) {
Function::Create(FTy, Function::WeakODRLinkage, "Wblah", M.get());
Function::Create(FTy, Function::WeakODRLinkage, "Wbar", M.get());
+ // [ptr, ptr, ptr]
+ ArrayType *VTableArrayType = ArrayType::get(
+ PointerType::get(Ctx, M->getDataLayout().getDefaultGlobalsAddressSpace()),
+ 3);
+ Constant *Int32TyNull =
+ llvm::ConstantExpr::getNullValue(PointerType::getUnqual(Ctx));
+ SmallVector<llvm::Type *, 1> tys = {VTableArrayType};
+ StructType *VTableType = llvm::StructType::get(Ctx, tys);
+
+ // Create two vtables in the module, one with external linkage and the other
+ // with local linkage.
+ for (auto [Name, Linkage] :
+ {std::pair{"ExternalGV", GlobalValue::ExternalLinkage},
+ {"LocalGV", GlobalValue::InternalLinkage}}) {
+ llvm::Twine FuncName(Name, StringRef("VFunc"));
+ Function *VFunc = Function::Create(FTy, Linkage, FuncName, M.get());
+ GlobalVariable *GV = new llvm::GlobalVariable(
+ *M, VTableType, /* isConstant= */ true, Linkage,
+ llvm::ConstantStruct::get(
+ VTableType,
+ {llvm::ConstantArray::get(VTableArrayType,
+ {Int32TyNull, Int32TyNull, VFunc})}),
+ Name);
+ // Add type metadata for the test data, since vtables with type metadata
+ // are added to symtab.
+ GV->addTypeMetadata(16, MDString::get(Ctx, Name));
+ }
+
InstrProfSymtab ProfSymtab;
EXPECT_THAT_ERROR(ProfSymtab.create(*M), Succeeded());
@@ -1751,6 +1781,23 @@ TEST(SymtabTest, instr_prof_symtab_module_test) {
EXPECT_EQ(PGOName, PGOFuncName);
EXPECT_THAT(PGOFuncName.str(), EndsWith(Funcs[I].str()));
}
+
+ StringRef VTables[] = {"ExternalGV", "LocalGV"};
+ for (auto [VTableName, PGOName] : {std::pair{"ExternalGV", "ExternalGV"},
+ {"LocalGV", "MyModule.cpp;LocalGV"}}) {
+ GlobalVariable *GV =
+ M->getGlobalVariable(VTableName, /* AllowInternal=*/true);
+
+ // Test that ProfSymtab returns the expected name given a hash.
+ std::string IRPGOName = getPGOName(*GV);
+ EXPECT_STREQ(IRPGOName.c_str(), PGOName);
+ uint64_t GUID = IndexedInstrProf::ComputeHash(IRPGOName);
+ EXPECT_EQ(IRPGOName, ProfSymtab.getFuncOrVarName(GUID));
+ EXPECT_EQ(VTableName, getParsedIRPGOName(IRPGOName).second);
+
+ // Test that ProfSymtab returns the expected global variable
+ EXPECT_EQ(GV, ProfSymtab.getGlobalVariable(GUID));
+ }
}
// Testing symtab serialization and creator/deserialization interface