xref: /llvm-project/llvm/unittests/Transforms/Utils/ProfDataUtilTest.cpp (revision 4d5906e0bf27b7aea451cf60e0d38a01f9b96085)
16047deb7SPaul Kirth //===----- ProfDataUtils.cpp - Unit tests for ProfDataUtils ---------------===//
26047deb7SPaul Kirth //
36047deb7SPaul Kirth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
46047deb7SPaul Kirth // See https://llvm.org/LICENSE.txt for license information.
56047deb7SPaul Kirth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66047deb7SPaul Kirth //
76047deb7SPaul Kirth //===----------------------------------------------------------------------===//
86047deb7SPaul Kirth 
96047deb7SPaul Kirth #include "llvm/AsmParser/Parser.h"
106047deb7SPaul Kirth #include "llvm/IR/BasicBlock.h"
11*4d5906e0SBjorn Pettersson #include "llvm/IR/Function.h"
12*4d5906e0SBjorn Pettersson #include "llvm/IR/Instructions.h"
136047deb7SPaul Kirth #include "llvm/IR/LLVMContext.h"
14*4d5906e0SBjorn Pettersson #include "llvm/IR/Module.h"
156047deb7SPaul Kirth #include "llvm/IR/ProfDataUtils.h"
166047deb7SPaul Kirth #include "llvm/Support/SourceMgr.h"
176047deb7SPaul Kirth #include "gtest/gtest.h"
186047deb7SPaul Kirth 
196047deb7SPaul Kirth using namespace llvm;
206047deb7SPaul Kirth 
parseIR(LLVMContext & C,const char * IR)216047deb7SPaul Kirth static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
226047deb7SPaul Kirth   SMDiagnostic Err;
236047deb7SPaul Kirth   std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
246047deb7SPaul Kirth   if (!Mod)
256047deb7SPaul Kirth     Err.print("ProfDataUtilsTests", errs());
266047deb7SPaul Kirth   return Mod;
276047deb7SPaul Kirth }
286047deb7SPaul Kirth 
TEST(ProfDataUtils,extractWeights)296047deb7SPaul Kirth TEST(ProfDataUtils, extractWeights) {
306047deb7SPaul Kirth   LLVMContext C;
316047deb7SPaul Kirth   std::unique_ptr<Module> M = parseIR(C, R"IR(
326047deb7SPaul Kirth define void @foo(i1 %cond0) {
336047deb7SPaul Kirth entry:
346047deb7SPaul Kirth   br i1 %cond0, label %bb0, label %bb1, !prof !1
356047deb7SPaul Kirth bb0:
366047deb7SPaul Kirth  %0 = mul i32 1, 2
376047deb7SPaul Kirth  br label %bb1
386047deb7SPaul Kirth bb1:
396047deb7SPaul Kirth   ret void
406047deb7SPaul Kirth }
416047deb7SPaul Kirth 
426047deb7SPaul Kirth !1 = !{!"branch_weights", i32 1, i32 100000}
436047deb7SPaul Kirth )IR");
446047deb7SPaul Kirth   Function *F = M->getFunction("foo");
456047deb7SPaul Kirth   auto &Entry = F->getEntryBlock();
466047deb7SPaul Kirth   auto &I = Entry.front();
476047deb7SPaul Kirth   auto *Branch = dyn_cast<BranchInst>(&I);
486047deb7SPaul Kirth   EXPECT_NE(nullptr, Branch);
496047deb7SPaul Kirth   auto *ProfileData = Branch->getMetadata(LLVMContext::MD_prof);
506047deb7SPaul Kirth   EXPECT_NE(ProfileData, nullptr);
516047deb7SPaul Kirth   EXPECT_TRUE(hasProfMD(I));
526047deb7SPaul Kirth   SmallVector<uint32_t> Weights;
536047deb7SPaul Kirth   EXPECT_TRUE(extractBranchWeights(ProfileData, Weights));
546047deb7SPaul Kirth   EXPECT_EQ(Weights[0], 1U);
556047deb7SPaul Kirth   EXPECT_EQ(Weights[1], 100000U);
566047deb7SPaul Kirth   EXPECT_EQ(Weights.size(), 2U);
576047deb7SPaul Kirth }
586047deb7SPaul Kirth 
TEST(ProfDataUtils,NoWeights)596047deb7SPaul Kirth TEST(ProfDataUtils, NoWeights) {
606047deb7SPaul Kirth   LLVMContext C;
616047deb7SPaul Kirth   std::unique_ptr<Module> M = parseIR(C, R"IR(
626047deb7SPaul Kirth define void @foo(i1 %cond0) {
636047deb7SPaul Kirth entry:
646047deb7SPaul Kirth   br i1 %cond0, label %bb0, label %bb1
656047deb7SPaul Kirth bb0:
666047deb7SPaul Kirth  %0 = mul i32 1, 2
676047deb7SPaul Kirth  br label %bb1
686047deb7SPaul Kirth bb1:
696047deb7SPaul Kirth   ret void
706047deb7SPaul Kirth }
716047deb7SPaul Kirth )IR");
726047deb7SPaul Kirth   Function *F = M->getFunction("foo");
736047deb7SPaul Kirth   auto &Entry = F->getEntryBlock();
746047deb7SPaul Kirth   auto &I = Entry.front();
756047deb7SPaul Kirth   auto *Branch = dyn_cast<BranchInst>(&I);
766047deb7SPaul Kirth   EXPECT_NE(nullptr, Branch);
776047deb7SPaul Kirth   auto *ProfileData = Branch->getMetadata(LLVMContext::MD_prof);
786047deb7SPaul Kirth   EXPECT_EQ(ProfileData, nullptr);
796047deb7SPaul Kirth   EXPECT_FALSE(hasProfMD(I));
806047deb7SPaul Kirth   SmallVector<uint32_t> Weights;
816047deb7SPaul Kirth   EXPECT_FALSE(extractBranchWeights(ProfileData, Weights));
826047deb7SPaul Kirth   EXPECT_EQ(Weights.size(), 0U);
836047deb7SPaul Kirth }
84