diff --git a/llvm/unittests/Transforms/IPO/AttributorTest.cpp b/llvm/unittests/Transforms/IPO/AttributorTest.cpp new file mode 100644 --- /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 + +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 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(IRPosition::function(*F))); + + EXPECT_TRUE(AA); + + const auto *SFail = dyn_cast(AA); + const auto *SSucc = dyn_cast(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 --- /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 + +namespace llvm { + +/// Helper class to create a module from assembly string and an Attributor +class AttributorTestBase : public testing::Test { +protected: + std::unique_ptr Ctx; + std::unique_ptr 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 --- a/llvm/unittests/Transforms/IPO/CMakeLists.txt +++ b/llvm/unittests/Transforms/IPO/CMakeLists.txt @@ -7,4 +7,5 @@ add_llvm_unittest(IPOTests LowerTypeTests.cpp WholeProgramDevirt.cpp + AttributorTest.cpp )