[llvm] [PGO] Test for inheritance relationships for `instrprof` intrinsics (PR #89485)

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 19 20:43:16 PDT 2024


https://github.com/mtrofin updated https://github.com/llvm/llvm-project/pull/89485

>From 59c16b5d9bb98c0a34ee0c15ccde0f416d58db3a Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Fri, 19 Apr 2024 21:51:27 +0200
Subject: [PATCH 1/2] [PGO] Test for inheritance relationships for `instrprof`
 intrinsics

Following up PR #83511, added a test to cover the inheritance graph for these intrinsics.
---
 llvm/unittests/IR/IntrinsicsTest.cpp | 88 ++++++++++++++++++++++++++--
 1 file changed, 82 insertions(+), 6 deletions(-)

diff --git a/llvm/unittests/IR/IntrinsicsTest.cpp b/llvm/unittests/IR/IntrinsicsTest.cpp
index a500346b66a5e4..715e7b9c6cdcd6 100644
--- a/llvm/unittests/IR/IntrinsicsTest.cpp
+++ b/llvm/unittests/IR/IntrinsicsTest.cpp
@@ -6,22 +6,57 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/IR/Intrinsics.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/IR/Constant.h"
+#include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/SourceMgr.h"
 #include "gtest/gtest.h"
+#include <algorithm>
+#include <cstdarg>
 
 using namespace llvm;
 
 namespace {
 
 static const char *const NameTable1[] = {
-  "llvm.foo",
-  "llvm.foo.a",
-  "llvm.foo.b",
-  "llvm.foo.b.a",
-  "llvm.foo.c",
+    "llvm.foo", "llvm.foo.a", "llvm.foo.b", "llvm.foo.b.a", "llvm.foo.c",
 };
 
-TEST(IntrinNameLookup, Basic) {
+class IntrinsicsTest : public ::testing::Test {
+  LLVMContext Context;
+  std::unique_ptr<Module> M;
+  BasicBlock *BB = nullptr;
+
+  void TearDown() override { M.reset(); }
+
+  void SetUp() override {
+    M = std::make_unique<Module>("Test", Context);
+    auto F = M->getOrInsertFunction(
+        "test", FunctionType::get(Type::getVoidTy(Context), false));
+    BB = BasicBlock::Create(Context, "", cast<Function>(F.getCallee()));
+    EXPECT_NE(BB, nullptr);
+  }
+
+public:
+  Instruction *makeIntrinsic(Intrinsic::ID ID) const {
+    IRBuilder<> Builder(BB);
+    SmallVector<Value*, 4> ProcessedArgs;
+    auto *Decl = Intrinsic::getDeclaration(M.get(), ID);
+    for (auto *Ty : Decl->getFunctionType()->params()) {
+      auto *Val = Constant::getNullValue(Ty);
+      ProcessedArgs.push_back(Val);
+    }
+    return Builder.CreateCall(Decl, ProcessedArgs);
+  }
+  template <typename T> void checkIsa(const Instruction &I) {
+    EXPECT_TRUE(isa<T>(I));
+  }
+};
+
+TEST(IntrinsicNameLookup, Basic) {
   int I = Intrinsic::lookupLLVMIntrinsicByName(NameTable1, "llvm.foo");
   EXPECT_EQ(0, I);
   I = Intrinsic::lookupLLVMIntrinsicByName(NameTable1, "llvm.foo.f64");
@@ -36,4 +71,45 @@ TEST(IntrinNameLookup, Basic) {
   EXPECT_EQ(4, I);
 }
 
+TEST_F(IntrinsicsTest, InstrProfInheritance) {
+  auto isInstrProfInstBase = [](const Instruction &I) {
+    return isa<InstrProfInstBase>(I);
+  };
+#define __ISA(TYPE, PARENT)                                                    \
+  auto is##TYPE = [&](const Instruction &I) -> bool {                          \
+    return isa<TYPE>(I) && is##PARENT(I);                                      \
+  }
+  __ISA(InstrProfCntrInstBase, InstrProfInstBase);
+  __ISA(InstrProfMCDCCondBitmapUpdate, InstrProfInstBase);
+  __ISA(InstrProfCoverInst, InstrProfCntrInstBase);
+  __ISA(InstrProfIncrementInst, InstrProfCntrInstBase);
+  __ISA(InstrProfIncrementInstStep, InstrProfIncrementInst);
+  __ISA(InstrProfTimestampInst, InstrProfCntrInstBase);
+  __ISA(InstrProfValueProfileInst, InstrProfCntrInstBase);
+  __ISA(InstrProfMCDCBitmapInstBase, InstrProfInstBase);
+  __ISA(InstrProfMCDCBitmapParameters, InstrProfMCDCBitmapInstBase);
+  __ISA(InstrProfMCDCTVBitmapUpdate, InstrProfMCDCBitmapInstBase);
+#undef __ISA
+
+  std::vector<
+      std::pair<Intrinsic::ID, std::function<bool(const Instruction &)>>>
+      LeafIDs = {
+          {Intrinsic::instrprof_cover, isInstrProfCoverInst},
+          {Intrinsic::instrprof_increment, isInstrProfIncrementInst},
+          {Intrinsic::instrprof_increment_step, isInstrProfIncrementInstStep},
+          {Intrinsic::instrprof_mcdc_condbitmap_update,
+           isInstrProfMCDCCondBitmapUpdate},
+          {Intrinsic::instrprof_mcdc_parameters,
+           isInstrProfMCDCBitmapParameters},
+          {Intrinsic::instrprof_mcdc_tvbitmap_update,
+           isInstrProfMCDCTVBitmapUpdate},
+          {Intrinsic::instrprof_timestamp, isInstrProfTimestampInst},
+          {Intrinsic::instrprof_value_profile, isInstrProfValueProfileInst}};
+  for (const auto &[ID, Checker] : LeafIDs) {
+    auto *Intr = makeIntrinsic(ID);
+    EXPECT_TRUE(Checker(*Intr));
+  }
+
+#undef RULES
+}
 } // end namespace

>From b33c64b0dabc0660e5892e35c9a94af36e03c954 Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Sat, 20 Apr 2024 05:42:49 +0200
Subject: [PATCH 2/2] yanked unused includes

---
 llvm/unittests/IR/IntrinsicsTest.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/llvm/unittests/IR/IntrinsicsTest.cpp b/llvm/unittests/IR/IntrinsicsTest.cpp
index 715e7b9c6cdcd6..30f9d14ffc16c0 100644
--- a/llvm/unittests/IR/IntrinsicsTest.cpp
+++ b/llvm/unittests/IR/IntrinsicsTest.cpp
@@ -12,9 +12,7 @@
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Module.h"
-#include "llvm/Support/SourceMgr.h"
 #include "gtest/gtest.h"
-#include <algorithm>
 #include <cstdarg>
 
 using namespace llvm;



More information about the llvm-commits mailing list