From 5fa8186b8dcc0be77f4ab64b1ef46ad919315b54 Mon Sep 17 00:00:00 2001 From: Daniel Malea Date: Fri, 28 Jun 2013 20:37:20 +0000 Subject: Adding tests for DebugIR pass - lit tests verify that each line of input LLVM IR gets a !dbg node and a corresponding entry of metadata that contains the line number - unit tests verify that DebugIR works as advertised in the interface - refactored some useful IR generation functionality from the MCJIT unit tests so it can be reused git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@185212 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ExecutionEngine/MCJIT/MCJITTestBase.h | 77 +++--- unittests/Transforms/CMakeLists.txt | 1 + unittests/Transforms/DebugIR/CMakeLists.txt | 7 + unittests/Transforms/DebugIR/DebugIR.cpp | 296 ++++++++++++++++++++++++ unittests/Transforms/DebugIR/Makefile | 15 ++ unittests/Transforms/Makefile | 2 +- 6 files changed, 363 insertions(+), 35 deletions(-) create mode 100644 unittests/Transforms/DebugIR/CMakeLists.txt create mode 100644 unittests/Transforms/DebugIR/DebugIR.cpp create mode 100644 unittests/Transforms/DebugIR/Makefile (limited to 'unittests') diff --git a/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h b/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h index 8edc12a14f..9766a79ae5 100644 --- a/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h +++ b/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h @@ -30,42 +30,19 @@ namespace llvm { -class MCJITTestBase : public MCJITTestAPICommon { +/// Helper class that can build very simple Modules +class TrivialModuleBuilder { protected: + LLVMContext Context; + IRBuilder<> Builder; + std::string BuilderTriple; - MCJITTestBase() - : OptLevel(CodeGenOpt::None) - , RelocModel(Reloc::Default) - , CodeModel(CodeModel::Default) - , MArch("") - , Builder(Context) - , MM(new SectionMemoryManager) - { - // The architectures below are known to be compatible with MCJIT as they - // are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be - // kept in sync. - SupportedArchs.push_back(Triple::aarch64); - SupportedArchs.push_back(Triple::arm); - SupportedArchs.push_back(Triple::x86); - SupportedArchs.push_back(Triple::x86_64); - - // Some architectures have sub-architectures in which tests will fail, like - // ARM. These two vectors will define if they do have sub-archs (to avoid - // extra work for those who don't), and if so, if they are listed to work - HasSubArchs.push_back(Triple::arm); - SupportedSubArchs.push_back("armv6"); - SupportedSubArchs.push_back("armv7"); - - // The operating systems below are known to be incompatible with MCJIT as - // they are copied from the test/ExecutionEngine/MCJIT/lit.local.cfg and - // should be kept in sync. - UnsupportedOSs.push_back(Triple::Cygwin); - UnsupportedOSs.push_back(Triple::Darwin); - } + TrivialModuleBuilder(const std::string &Triple) + : Builder(Context), BuilderTriple(Triple) {} - Module *createEmptyModule(StringRef Name) { + Module *createEmptyModule(StringRef Name = StringRef()) { Module * M = new Module(Name, Context); - M->setTargetTriple(Triple::normalize(HostTriple)); + M->setTargetTriple(Triple::normalize(BuilderTriple)); return M; } @@ -161,6 +138,40 @@ protected: name); return Global; } +}; + +class MCJITTestBase : public MCJITTestAPICommon, public TrivialModuleBuilder { +protected: + + MCJITTestBase() + : TrivialModuleBuilder(HostTriple) + , OptLevel(CodeGenOpt::None) + , RelocModel(Reloc::Default) + , CodeModel(CodeModel::Default) + , MArch("") + , MM(new SectionMemoryManager) + { + // The architectures below are known to be compatible with MCJIT as they + // are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be + // kept in sync. + SupportedArchs.push_back(Triple::aarch64); + SupportedArchs.push_back(Triple::arm); + SupportedArchs.push_back(Triple::x86); + SupportedArchs.push_back(Triple::x86_64); + + // Some architectures have sub-architectures in which tests will fail, like + // ARM. These two vectors will define if they do have sub-archs (to avoid + // extra work for those who don't), and if so, if they are listed to work + HasSubArchs.push_back(Triple::arm); + SupportedSubArchs.push_back("armv6"); + SupportedSubArchs.push_back("armv7"); + + // The operating systems below are known to be incompatible with MCJIT as + // they are copied from the test/ExecutionEngine/MCJIT/lit.local.cfg and + // should be kept in sync. + UnsupportedOSs.push_back(Triple::Cygwin); + UnsupportedOSs.push_back(Triple::Darwin); + } void createJIT(Module *M) { @@ -186,14 +197,12 @@ protected: assert(TheJIT.get() != NULL && "error creating MCJIT with EngineBuilder"); } - LLVMContext Context; CodeGenOpt::Level OptLevel; Reloc::Model RelocModel; CodeModel::Model CodeModel; StringRef MArch; SmallVector MAttrs; OwningPtr TheJIT; - IRBuilder<> Builder; RTDyldMemoryManager *MM; OwningPtr M; diff --git a/unittests/Transforms/CMakeLists.txt b/unittests/Transforms/CMakeLists.txt index e3ce185e0d..8ec56f10d9 100644 --- a/unittests/Transforms/CMakeLists.txt +++ b/unittests/Transforms/CMakeLists.txt @@ -1 +1,2 @@ +add_subdirectory(DebugIR) add_subdirectory(Utils) diff --git a/unittests/Transforms/DebugIR/CMakeLists.txt b/unittests/Transforms/DebugIR/CMakeLists.txt new file mode 100644 index 0000000000..4b471939ef --- /dev/null +++ b/unittests/Transforms/DebugIR/CMakeLists.txt @@ -0,0 +1,7 @@ +set(LLVM_LINK_COMPONENTS + Instrumentation + ) + +add_llvm_unittest(DebugIRTests + DebugIR.cpp + ) diff --git a/unittests/Transforms/DebugIR/DebugIR.cpp b/unittests/Transforms/DebugIR/DebugIR.cpp new file mode 100644 index 0000000000..978662a79d --- /dev/null +++ b/unittests/Transforms/DebugIR/DebugIR.cpp @@ -0,0 +1,296 @@ +//===- DebugIR.cpp - Unit tests for the DebugIR pass ----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// The tests in this file verify the DebugIR pass that generates debug metadata +// for LLVM IR. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/Triple.h" +#include "llvm/Config/config.h" +#include "llvm/DebugInfo.h" +#include "llvm/DIBuilder.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/Host.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" +#include "llvm/Transforms/Instrumentation.h" + +#include "../lib/Transforms/Instrumentation/DebugIR.h" + +// These tests do not depend on MCJIT, but we use the TrivialModuleBuilder +// helper class to construct some trivial Modules. +#include "../unittests/ExecutionEngine/MCJIT/MCJITTestBase.h" + +#include + +#include "gtest/gtest.h" + +using namespace llvm; +using namespace std; + +namespace { + +/// Insert a mock CUDescriptor with the specified producer +void insertCUDescriptor(Module *M, StringRef File, StringRef Dir, + StringRef Producer) { + DIBuilder B(*M); + B.createCompileUnit(dwarf::DW_LANG_C99, File, Dir, Producer, false, "", 0); + B.finalize(); +} + +/// Attempts to remove file at Path and returns true if it existed, or false if +/// it did not. +bool removeIfExists(StringRef Path) { + bool existed = false; + sys::fs::remove(Path, existed); + return existed; +} + +class TestDebugIR : public ::testing::Test, public TrivialModuleBuilder { +protected: + TestDebugIR() + : TrivialModuleBuilder(sys::getProcessTriple()) +#ifdef HAVE_GETCWD + , + cwd(get_current_dir_name()) +#else + , + cwd(0) +#endif + { + } + + ~TestDebugIR() { free(cwd); } + + /// Returns a concatenated path string consisting of Dir and Filename + string getPath(const string &Dir, const string &Filename) { + SmallVector Path; + sys::path::append(Path, Dir, Filename); + Path.resize(Dir.size() + Filename.size() + 2); + Path[Dir.size() + Filename.size() + 1] = '\0'; + return string(Path.data()); + } + + LLVMContext Context; + char *cwd; + OwningPtr M; + OwningPtr D; +}; + +// Test empty named Module that is not supposed to be output to disk. +TEST_F(TestDebugIR, EmptyNamedModuleNoWrite) { + string name = "/mock/path/to/empty_module.ll"; + M.reset(createEmptyModule(name)); + D.reset(static_cast(llvm::createDebugIRPass())); + string Path; + D->runOnModule(*M, Path); + + // verify DebugIR was able to correctly parse the file name from module ID + ASSERT_EQ(Path, name); + + // verify DebugIR did not generate a file + ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path; +} + +// Test a non-empty unnamed module that is output to an autogenerated file name. +TEST_F(TestDebugIR, NonEmptyUnnamedModuleWriteToAutogeneratedFile) { + M.reset(createEmptyModule()); + insertAddFunction(M.get()); + D.reset(static_cast(llvm::createDebugIRPass(true, true))); + + string Path; + D->runOnModule(*M, Path); + + // verify DebugIR generated a file, and clean it up + ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path; +} + +// Test not specifying a name in the module -- DebugIR should generate a name +// and write the file contents. +TEST_F(TestDebugIR, EmptyModuleWriteAnonymousFile) { + M.reset(createEmptyModule()); + D.reset(static_cast(llvm::createDebugIRPass(false, false))); + + string Path; + D->runOnModule(*M, Path); + + // verify DebugIR generated a file and clean it up + ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path; +} + +#ifdef HAVE_GETCWD // These tests require get_current_dir_name() + +// Test empty named Module that is to be output to path specified at Module +// construction. +TEST_F(TestDebugIR, EmptyNamedModuleWriteFile) { + string Filename("NamedFile1"); + string ExpectedPath(getPath(cwd, Filename)); + + M.reset(createEmptyModule(ExpectedPath)); + D.reset(static_cast(llvm::createDebugIRPass(true, true))); + + string Path; + D->runOnModule(*M, Path); + + // verify DebugIR was able to correctly parse the file name from module ID + ASSERT_EQ(ExpectedPath, Path); + + // verify DebugIR generated a file, and clean it up + ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path; +} + +// Test an empty unnamed module generates an output file whose path is specified +// at DebugIR construction. +TEST_F(TestDebugIR, EmptyUnnamedModuleWriteNamedFile) { + string Filename("NamedFile2"); + + M.reset(createEmptyModule()); + D.reset(static_cast(llvm::createDebugIRPass( + false, false, StringRef(cwd), StringRef(Filename)))); + string Path; + D->runOnModule(*M, Path); + + string ExpectedPath(getPath(cwd, Filename)); + ASSERT_EQ(ExpectedPath, Path); + + // verify DebugIR generated a file, and clean it up + ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path; +} + +// Test an empty named module generates an output file at the path specified +// during DebugIR construction. +TEST_F(TestDebugIR, EmptyNamedModuleWriteNamedFile) { + string Filename("NamedFile3"); + + string UnexpectedPath(getPath(cwd, "UnexpectedFilename")); + M.reset(createEmptyModule(UnexpectedPath)); + + D.reset(static_cast(llvm::createDebugIRPass( + false, false, StringRef(cwd), StringRef(Filename)))); + string Path; + D->runOnModule(*M, Path); + + string ExpectedPath(getPath(cwd, Filename)); + ASSERT_EQ(ExpectedPath, Path); + + // verify DebugIR generated a file, and clean it up + ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path; + + // verify DebugIR did not generate a file at the path specified at Module + // construction. + ASSERT_FALSE(removeIfExists(UnexpectedPath)) << "Unexpected file " << Path; +} + +// Test a non-empty named module that is not supposed to be output to disk +TEST_F(TestDebugIR, NonEmptyNamedModuleNoWrite) { + string Filename("NamedFile4"); + string ExpectedPath(getPath(cwd, Filename)); + + M.reset(createEmptyModule(ExpectedPath)); + insertAddFunction(M.get()); + + D.reset(static_cast(llvm::createDebugIRPass())); + + string Path; + D->runOnModule(*M, Path); + ASSERT_EQ(ExpectedPath, Path); + + // verify DebugIR did not generate a file + ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path; +} + +// Test a non-empty named module that is output to disk. +TEST_F(TestDebugIR, NonEmptyNamedModuleWriteFile) { + string Filename("NamedFile5"); + string ExpectedPath(getPath(cwd, Filename)); + + M.reset(createEmptyModule(ExpectedPath)); + insertAddFunction(M.get()); + + D.reset(static_cast(llvm::createDebugIRPass(true, true))); + + string Path; + D->runOnModule(*M, Path); + ASSERT_EQ(ExpectedPath, Path); + + // verify DebugIR generated a file, and clean it up + ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path; +} + +// Test a non-empty unnamed module is output to a path specified at DebugIR +// construction. +TEST_F(TestDebugIR, NonEmptyUnnamedModuleWriteToNamedFile) { + string Filename("NamedFile6"); + + M.reset(createEmptyModule()); + insertAddFunction(M.get()); + + D.reset(static_cast( + llvm::createDebugIRPass(true, true, cwd, Filename))); + string Path; + D->runOnModule(*M, Path); + + string ExpectedPath(getPath(cwd, Filename)); + ASSERT_EQ(ExpectedPath, Path); + + // verify DebugIR generated a file, and clean it up + ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path; +} + +// Test that information inside existing debug metadata is retained +TEST_F(TestDebugIR, ExistingMetadataRetained) { + string Filename("NamedFile7"); + string ExpectedPath(getPath(cwd, Filename)); + + M.reset(createEmptyModule(ExpectedPath)); + insertAddFunction(M.get()); + + StringRef Producer("TestProducer"); + insertCUDescriptor(M.get(), Filename, cwd, Producer); + + DebugInfoFinder Finder; + Finder.processModule(*M); + ASSERT_EQ((unsigned)1, Finder.compile_unit_count()); + D.reset(static_cast(llvm::createDebugIRPass())); + + string Path; + D->runOnModule(*M, Path); + ASSERT_EQ(ExpectedPath, Path); + + // verify DebugIR did not generate a file + ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path; + + DICompileUnit CU(*Finder.compile_unit_begin()); + + // Verify original CU information is retained + ASSERT_EQ(Filename, CU.getFilename()); + ASSERT_EQ(cwd, CU.getDirectory()); + ASSERT_EQ(Producer, CU.getProducer()); +} + +#endif // HAVE_GETCWD + +#ifdef GTEST_HAS_DEATH_TEST + +// Test a non-empty unnamed module that is not supposed to be output to disk +// NOTE: this test is expected to die with LLVM_ERROR, and such depends on +// google test's "death test" mode. +TEST_F(TestDebugIR, NonEmptyUnnamedModuleNoWrite) { + M.reset(createEmptyModule(StringRef())); + insertAddFunction(M.get()); + D.reset(static_cast(llvm::createDebugIRPass())); + + // No name in module or on DebugIR construction ==> DebugIR should assert + EXPECT_DEATH(D->runOnModule(*M), + "DebugIR unable to determine file name in input."); +} + +#endif // GTEST_HAS_DEATH_TEST +} diff --git a/unittests/Transforms/DebugIR/Makefile b/unittests/Transforms/DebugIR/Makefile new file mode 100644 index 0000000000..9ace8c33c4 --- /dev/null +++ b/unittests/Transforms/DebugIR/Makefile @@ -0,0 +1,15 @@ +##===- unittests/Transforms/Utils/Makefile -----------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../../.. +TESTNAME = DebugIR +LINK_COMPONENTS := Instrumentation + +include $(LEVEL)/Makefile.config +include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest diff --git a/unittests/Transforms/Makefile b/unittests/Transforms/Makefile index 599b18a057..d5cca397b6 100644 --- a/unittests/Transforms/Makefile +++ b/unittests/Transforms/Makefile @@ -9,7 +9,7 @@ LEVEL = ../.. -PARALLEL_DIRS = Utils +PARALLEL_DIRS = DebugIR Utils include $(LEVEL)/Makefile.common -- cgit v1.2.3