[llvm] 0750757 - [Attributor] Unittest for Attributor

Luofan Chen via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 14 19:13:29 PDT 2020


Author: Luofan Chen
Date: 2020-07-15T10:12:39+08:00
New Revision: 0750757e80c2dbca87b77f57c9994c76964fa92d

URL: https://github.com/llvm/llvm-project/commit/0750757e80c2dbca87b77f57c9994c76964fa92d
DIFF: https://github.com/llvm/llvm-project/commit/0750757e80c2dbca87b77f57c9994c76964fa92d.diff

LOG: [Attributor] Unittest for Attributor

Summary: This patch introduces basic unittest interface for the Attributor and a simple test case for casting.

Reviewers: jdoerfert, sstefan1, uenoku, homerdin, baziotis

Reviewed By: jdoerfert

Subscribers: mgorny, uenoku, kuter, okura, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D83754

Added: 
    llvm/unittests/Transforms/IPO/AttributorTest.cpp
    llvm/unittests/Transforms/IPO/AttributorTestBase.h

Modified: 
    llvm/unittests/Transforms/IPO/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/Transforms/IPO/AttributorTest.cpp b/llvm/unittests/Transforms/IPO/AttributorTest.cpp
new file mode 100644
index 000000000000..8c1682c01f30
--- /dev/null
+++ b/llvm/unittests/Transforms/IPO/AttributorTest.cpp
@@ -0,0 +1,59 @@
+//===- AttributorTest.cpp - Attributor unit tests ------------------------===//
+//
+// 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/Transforms/IPO/Attributor.h"
+#include "AttributorTestBase.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Analysis/CGSCCPassManager.h"
+#include "llvm/Analysis/CallGraphSCCPass.h"
+#include "llvm/Analysis/LoopAnalysisManager.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/Support/Allocator.h"
+#include "llvm/Testing/Support/Error.h"
+#include "llvm/Transforms/Utils/CallGraphUpdater.h"
+#include "gtest/gtest.h"
+#include <memory>
+
+namespace llvm {
+
+TEST_F(AttributorTestBase, TestCast) {
+  const char *ModuleString = R"(
+    define i32 @foo(i32 %a, i32 %b) {
+    entry:
+      %c = add i32 %a, %b
+      ret i32 %c
+    }
+  )";
+
+  Module &M = parseModule(ModuleString);
+
+  SetVector<Function *> Functions;
+  AnalysisGetter AG;
+  for (Function &F : M)
+    Functions.insert(&F);
+
+  CallGraphUpdater CGUpdater;
+  BumpPtrAllocator Allocator;
+  InformationCache InfoCache(M, AG, Allocator, nullptr);
+  Attributor A(Functions, InfoCache, CGUpdater);
+
+  Function *F = M.getFunction("foo");
+
+  AbstractAttribute *AA = (AbstractAttribute *)&(
+      A.getOrCreateAAFor<AAIsDead>(IRPosition::function(*F)));
+
+  EXPECT_TRUE(AA);
+
+  const auto *SFail = dyn_cast<AAAlign>(AA);
+  const auto *SSucc = dyn_cast<AAIsDead>(AA);
+
+  ASSERT_EQ(SFail, nullptr);
+  ASSERT_TRUE(SSucc);
+}
+
+} // namespace llvm
\ No newline at end of file

diff  --git a/llvm/unittests/Transforms/IPO/AttributorTestBase.h b/llvm/unittests/Transforms/IPO/AttributorTestBase.h
new file mode 100644
index 000000000000..d12a2b481af0
--- /dev/null
+++ b/llvm/unittests/Transforms/IPO/AttributorTestBase.h
@@ -0,0 +1,47 @@
+//===- llvm/unittest/Transforms/IPO/AttributorTestBase.h -----------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// This file defines an AttributorTestBase class, which provides helpers to
+/// parse a LLVM IR string and create Attributor
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_UNITTESTS_TRANSFORMS_ATTRIBUTOR_TESTBASE_H
+#define LLVM_UNITTESTS_TRANSFORMS_ATTRIBUTOR_TESTBASE_H
+
+#include "llvm/Analysis/CGSCCPassManager.h"
+#include "llvm/Analysis/CallGraphSCCPass.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/Support/Allocator.h"
+#include "llvm/Testing/Support/Error.h"
+#include "llvm/Transforms/IPO/Attributor.h"
+#include "llvm/Transforms/Utils/CallGraphUpdater.h"
+#include "gtest/gtest.h"
+#include <memory>
+
+namespace llvm {
+
+/// Helper class to create a module from assembly string and an Attributor
+class AttributorTestBase : public testing::Test {
+protected:
+  std::unique_ptr<LLVMContext> Ctx;
+  std::unique_ptr<Module> M;
+
+  AttributorTestBase() : Ctx(new LLVMContext) {}
+
+  Module &parseModule(const char *ModuleString) {
+    SMDiagnostic Err;
+    M = parseAssemblyString(ModuleString, Err, *Ctx);
+    EXPECT_TRUE(M);
+    return *M;
+  }
+};
+
+} // namespace llvm
+
+#endif
\ No newline at end of file

diff  --git a/llvm/unittests/Transforms/IPO/CMakeLists.txt b/llvm/unittests/Transforms/IPO/CMakeLists.txt
index ee33a5fcd1b3..8399b925512b 100644
--- a/llvm/unittests/Transforms/IPO/CMakeLists.txt
+++ b/llvm/unittests/Transforms/IPO/CMakeLists.txt
@@ -7,4 +7,5 @@ set(LLVM_LINK_COMPONENTS
 add_llvm_unittest(IPOTests
   LowerTypeTests.cpp
   WholeProgramDevirt.cpp
+  AttributorTest.cpp
   )


        


More information about the llvm-commits mailing list