OSDN Git Service

[llvm-exegesis] Run unit tests on more platforms.
authorClement Courbet <courbet@google.com>
Fri, 13 Apr 2018 12:20:30 +0000 (12:20 +0000)
committerClement Courbet <courbet@google.com>
Fri, 13 Apr 2018 12:20:30 +0000 (12:20 +0000)
Summary:
 - Target-independent tests are run all the time.
 - Tests that codegen X86 code are run when X86 is in build targets.
 - Tests that run X86 jitted code are run only on X86 hosts.

Reviewers: gchatelet

Subscribers: mgorny, llvm-commits, tschuett

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330008 91177308-0d34-0410-b5e6-96231b3b80d8

unittests/tools/CMakeLists.txt
unittests/tools/llvm-exegesis/CMakeLists.txt
unittests/tools/llvm-exegesis/X86/CMakeLists.txt [new file with mode: 0644]
unittests/tools/llvm-exegesis/X86/InMemoryAssemblerTest.cpp [moved from unittests/tools/llvm-exegesis/InMemoryAssemblerTest.cpp with 78% similarity]
unittests/tools/llvm-exegesis/X86/InstructionSnippetGeneratorTest.cpp [moved from unittests/tools/llvm-exegesis/InstructionSnippetGeneratorTest.cpp with 98% similarity]

index 7f8cf36..e7c7dca 100644 (file)
@@ -2,10 +2,9 @@ if(LLVM_TARGETS_TO_BUILD MATCHES "X86")
   add_subdirectory(
     llvm-cfi-verify
   )
-  if(LLVM_HOST_TRIPLE MATCHES "x86_64")
-    add_subdirectory(
-      llvm-exegesis
-    )
-  endif()
 endif()
 
+add_subdirectory(
+  llvm-exegesis
+)
+
index a6b1d87..a08d569 100644 (file)
@@ -1,6 +1,4 @@
 include_directories(
-  ${LLVM_MAIN_SRC_DIR}/lib/Target/X86
-  ${LLVM_BINARY_DIR}/lib/Target/X86
   ${LLVM_MAIN_SRC_DIR}/tools/llvm-exegesis/lib
   )
 
@@ -10,13 +8,10 @@ set(LLVM_LINK_COMPONENTS
   Object
   Support
   Symbolize
-  native
   )
 
 add_llvm_unittest(LLVMExegesisTests
   BenchmarkResultTest.cpp
-  InMemoryAssemblerTest.cpp
-  InstructionSnippetGeneratorTest.cpp
   OperandGraphTest.cpp
   PerfHelperTest.cpp
   )
@@ -25,3 +20,9 @@ target_link_libraries(LLVMExegesisTests PRIVATE LLVMExegesis)
 if(LLVM_ENABLE_LIBPFM AND HAVE_LIBPFM)
   target_link_libraries(LLVMExegesisTests PRIVATE pfm)
 endif()
+
+if(LLVM_TARGETS_TO_BUILD MATCHES "X86")
+  add_subdirectory(
+    X86
+  )
+endif()
diff --git a/unittests/tools/llvm-exegesis/X86/CMakeLists.txt b/unittests/tools/llvm-exegesis/X86/CMakeLists.txt
new file mode 100644 (file)
index 0000000..4d76f1a
--- /dev/null
@@ -0,0 +1,21 @@
+include_directories(
+  ${LLVM_MAIN_SRC_DIR}/lib/Target/X86
+  ${LLVM_BINARY_DIR}/lib/Target/X86
+  ${LLVM_MAIN_SRC_DIR}/tools/llvm-exegesis/lib
+  )
+
+set(LLVM_LINK_COMPONENTS
+  MC
+  MCParser
+  Object
+  Support
+  Symbolize
+  X86
+  )
+
+add_llvm_unittest(LLVMExegesisX86Tests
+  InMemoryAssemblerTest.cpp
+  InstructionSnippetGeneratorTest.cpp
+  )
+target_link_libraries(LLVMExegesisX86Tests PRIVATE LLVMExegesis)
+
@@ -39,8 +39,10 @@ protected:
         CpuName(llvm::sys::getHostCPUName().str()) {}
 
   static void SetUpTestCase() {
-    llvm::InitializeNativeTarget();
-    llvm::InitializeNativeTargetAsmPrinter();
+    LLVMInitializeX86TargetInfo();
+    LLVMInitializeX86TargetMC();
+    LLVMInitializeX86Target();
+    LLVMInitializeX86AsmPrinter();
   }
 
   std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() {
@@ -54,12 +56,26 @@ protected:
             TT, CpuName, "", Options, llvm::Reloc::Model::Static)));
   }
 
+  bool IsSupportedTarget() const {
+    return llvm::StringRef(TT).startswith_lower("x86_64");
+  }
+
 private:
   const std::string TT;
   const std::string CpuName;
 };
 
-TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunction) {
+// Used to skip tests on unsupported architectures and operating systems.
+// To skip a test, add this macro at the top of a test-case.
+#define SKIP_UNSUPPORTED_PLATFORM \
+  do \
+    if (!IsSupportedTarget()) \
+      return; \
+  while(0)
+
+
+TEST_F(MachineFunctionGeneratorTest, JitFunction) {
+  SKIP_UNSUPPORTED_PLATFORM;
   JitFunctionContext Context(createTargetMachine());
   JitFunction Function(std::move(Context), {});
   ASSERT_THAT(Function.getFunctionBytes().str(), ElementsAre(0xc3));
@@ -68,7 +84,8 @@ TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunction) {
   // Function();
 }
 
-TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunctionXOR32rr) {
+TEST_F(MachineFunctionGeneratorTest, JitFunctionXOR32rr) {
+  SKIP_UNSUPPORTED_PLATFORM;
   JitFunctionContext Context(createTargetMachine());
   JitFunction Function(
       std::move(Context),
@@ -77,7 +94,8 @@ TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunctionXOR32rr) {
   // Function();
 }
 
-TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunctionMOV64ri) {
+TEST_F(MachineFunctionGeneratorTest, JitFunctionMOV64ri) {
+  SKIP_UNSUPPORTED_PLATFORM;
   JitFunctionContext Context(createTargetMachine());
   JitFunction Function(std::move(Context),
                        {MCInstBuilder(MOV64ri32).addReg(RAX).addImm(42)});
@@ -86,7 +104,8 @@ TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunctionMOV64ri) {
   // Function();
 }
 
-TEST_F(MachineFunctionGeneratorTest, DISABLED_JitFunctionMOV32ri) {
+TEST_F(MachineFunctionGeneratorTest, JitFunctionMOV32ri) {
+  SKIP_UNSUPPORTED_PLATFORM;
   JitFunctionContext Context(createTargetMachine());
   JitFunction Function(std::move(Context),
                        {MCInstBuilder(MOV32ri).addReg(EAX).addImm(42)});
@@ -55,12 +55,15 @@ using llvm::X86::RAX;
 class MCInstrDescViewTest : public ::testing::Test {
 protected:
   MCInstrDescViewTest()
-      : TheTriple(llvm::sys::getProcessTriple()),
-        CpuName(llvm::sys::getHostCPUName().str()) {}
+      : TheTriple("x86_64") {}
 
-  void SetUp() override {
-    llvm::InitializeNativeTarget();
+  static void SetUpTestCase() {
+    LLVMInitializeX86TargetInfo();
+    LLVMInitializeX86TargetMC();
+    LLVMInitializeX86Target();
+  }
 
+  void SetUp() override {
     std::string Error;
     const auto *Target = llvm::TargetRegistry::lookupTarget(TheTriple, Error);
     InstrInfo.reset(Target->createMCInstrInfo());
@@ -68,7 +71,6 @@ protected:
   }
 
   const std::string TheTriple;
-  const std::string CpuName;
   std::unique_ptr<const llvm::MCInstrInfo> InstrInfo;
   std::unique_ptr<const llvm::MCRegisterInfo> RegInfo;
 };