1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
//===--- CtxProfAnalysisTest.cpp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/CtxProfAnalysis.h"
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/CGSCCPassManager.h"
#include "llvm/Analysis/LoopAnalysisManager.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Analysis.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassInstrumentation.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
class CtxProfAnalysisTest : public testing::Test {
static constexpr auto *IR = R"IR(
declare void @bar()
define private void @foo(i32 %a, ptr %fct) #0 !guid !0 {
%t = icmp eq i32 %a, 0
br i1 %t, label %yes, label %no
yes:
call void %fct(i32 %a)
br label %exit
no:
call void @bar()
br label %exit
exit:
ret void
}
define void @an_entrypoint(i32 %a) {
%t = icmp eq i32 %a, 0
br i1 %t, label %yes, label %no
yes:
call void @foo(i32 1, ptr null)
ret void
no:
ret void
}
define void @another_entrypoint_no_callees(i32 %a) {
%t = icmp eq i32 %a, 0
br i1 %t, label %yes, label %no
yes:
ret void
no:
ret void
}
define void @inlineasm() {
call void asm "nop", ""()
ret void
}
attributes #0 = { noinline }
!0 = !{ i64 11872291593386833696 }
)IR";
protected:
LLVMContext C;
PassBuilder PB;
ModuleAnalysisManager MAM;
FunctionAnalysisManager FAM;
CGSCCAnalysisManager CGAM;
LoopAnalysisManager LAM;
std::unique_ptr<Module> M;
void SetUp() override {
SMDiagnostic Err;
M = parseAssemblyString(IR, Err, C);
ASSERT_TRUE(!!M);
}
public:
CtxProfAnalysisTest() {
PB.registerModuleAnalyses(MAM);
PB.registerCGSCCAnalyses(CGAM);
PB.registerFunctionAnalyses(FAM);
PB.registerLoopAnalyses(LAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
}
};
TEST_F(CtxProfAnalysisTest, GetCallsiteIDTest) {
ModulePassManager MPM;
MPM.addPass(PGOInstrumentationGen(PGOInstrumentationType::CTXPROF));
EXPECT_FALSE(MPM.run(*M, MAM).areAllPreserved());
auto *F = M->getFunction("foo");
ASSERT_NE(F, nullptr);
std::vector<uint32_t> InsValues;
for (auto &BB : *F)
for (auto &I : BB)
if (auto *CB = dyn_cast<CallBase>(&I)) {
// Skip instrumentation inserted intrinsics.
if (CB->getCalledFunction() && CB->getCalledFunction()->isIntrinsic())
continue;
auto *Ins = CtxProfAnalysis::getCallsiteInstrumentation(*CB);
ASSERT_NE(Ins, nullptr);
InsValues.push_back(Ins->getIndex()->getZExtValue());
}
EXPECT_THAT(InsValues, testing::ElementsAre(0, 1));
}
TEST_F(CtxProfAnalysisTest, GetCallsiteIDInlineAsmTest) {
ModulePassManager MPM;
MPM.addPass(PGOInstrumentationGen(PGOInstrumentationType::CTXPROF));
EXPECT_FALSE(MPM.run(*M, MAM).areAllPreserved());
auto *F = M->getFunction("inlineasm");
ASSERT_NE(F, nullptr);
std::vector<const Instruction *> InsValues;
for (auto &BB : *F)
for (auto &I : BB)
if (auto *CB = dyn_cast<CallBase>(&I)) {
// Skip instrumentation inserted intrinsics.
if (CB->getCalledFunction() && CB->getCalledFunction()->isIntrinsic())
continue;
auto *Ins = CtxProfAnalysis::getCallsiteInstrumentation(*CB);
InsValues.push_back(Ins);
}
EXPECT_THAT(InsValues, testing::ElementsAre(nullptr));
}
TEST_F(CtxProfAnalysisTest, GetCallsiteIDNegativeTest) {
auto *F = M->getFunction("foo");
ASSERT_NE(F, nullptr);
CallBase *FirstCall = nullptr;
for (auto &BB : *F)
for (auto &I : BB)
if (auto *CB = dyn_cast<CallBase>(&I)) {
if (CB->isIndirectCall() || !CB->getCalledFunction()->isIntrinsic()) {
FirstCall = CB;
break;
}
}
ASSERT_NE(FirstCall, nullptr);
auto *IndIns = CtxProfAnalysis::getCallsiteInstrumentation(*FirstCall);
EXPECT_EQ(IndIns, nullptr);
}
TEST_F(CtxProfAnalysisTest, GetBBIDTest) {
ModulePassManager MPM;
MPM.addPass(PGOInstrumentationGen(PGOInstrumentationType::CTXPROF));
EXPECT_FALSE(MPM.run(*M, MAM).areAllPreserved());
auto *F = M->getFunction("foo");
ASSERT_NE(F, nullptr);
std::map<std::string, int> BBNameAndID;
for (auto &BB : *F) {
auto *Ins = CtxProfAnalysis::getBBInstrumentation(BB);
if (Ins)
BBNameAndID[BB.getName().str()] =
static_cast<int>(Ins->getIndex()->getZExtValue());
else
BBNameAndID[BB.getName().str()] = -1;
}
EXPECT_THAT(BBNameAndID,
testing::UnorderedElementsAre(
testing::Pair("", 0), testing::Pair("yes", 1),
testing::Pair("no", -1), testing::Pair("exit", -1)));
}
} // namespace
|