[compiler-rt] e0b6c99 - Re-apply "[ORC-RT] Add unit test infrastructure, extensible_rtti..."

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Tue May 11 10:29:05 PDT 2021


Author: Lang Hames
Date: 2021-05-11T10:28:33-07:00
New Revision: e0b6c99288bf1798ccc80aa0c5c7940c17665e69

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

LOG: Re-apply "[ORC-RT] Add unit test infrastructure, extensible_rtti..."

This reapplies 6d263b6f1c9 (which was reverted in 1c7c6f2b106) with a fix for a
CMake issue.

Added: 
    compiler-rt/lib/orc/extensible_rtti.cpp
    compiler-rt/lib/orc/extensible_rtti.h
    compiler-rt/lib/orc/unittests/CMakeLists.txt
    compiler-rt/lib/orc/unittests/extensible_rtti_test.cpp
    compiler-rt/lib/orc/unittests/orc_unit_test_main.cpp

Modified: 
    compiler-rt/cmake/config-ix.cmake
    compiler-rt/lib/orc/CMakeLists.txt

Removed: 
    compiler-rt/lib/orc/placeholder.cpp


################################################################################
diff  --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake
index d6ce36709ca59..eea34b60895f6 100644
--- a/compiler-rt/cmake/config-ix.cmake
+++ b/compiler-rt/cmake/config-ix.cmake
@@ -343,7 +343,7 @@ set(ALL_XRAY_SUPPORTED_ARCH ${X86_64} ${ARM32} ${ARM64} ${MIPS32} ${MIPS64} powe
 endif()
 set(ALL_SHADOWCALLSTACK_SUPPORTED_ARCH ${ARM64})
 
-if (APPLE)
+if (UNIX)
 set(ALL_ORC_SUPPORTED_ARCH ${X86_64})
 endif()
 

diff  --git a/compiler-rt/lib/orc/CMakeLists.txt b/compiler-rt/lib/orc/CMakeLists.txt
index 4007a46ca4661..3c7d5876cd327 100644
--- a/compiler-rt/lib/orc/CMakeLists.txt
+++ b/compiler-rt/lib/orc/CMakeLists.txt
@@ -2,7 +2,7 @@
 
 # ORC runtime library implementation files.
 set(ORC_SOURCES
-  placeholder.cpp
+  extensible_rtti.cpp
   )
 
 # Implementation files for all ORC architectures.
@@ -12,6 +12,7 @@ set(x86_64_SOURCES
 
 set(ORC_IMPL_HEADERS
 # Implementation headers will go here.
+  extensible_rtti.h
 )
 
 # Create list of all source files for
@@ -70,7 +71,7 @@ else() # not Apple
       ARCHS ${arch}
       SOURCES ${ORC_SOURCES} ${${arch}_SOURCES}
       ADDITIONAL_HEADERS ${ORC_IMPL_HEADERS}
-      CFLAGS $ORC_CFLAGS}
+      CFLAGS ${ORC_CFLAGS}
       DEPS ${ORC_DEPS})
 
     # Common ORC archive for instrumented binaries.
@@ -82,3 +83,7 @@ else() # not Apple
      PARENT_TARGET orc)
   endforeach()
 endif() # not Apple
+
+if(COMPILER_RT_INCLUDE_TESTS)
+  add_subdirectory(unittests)
+endif()

diff  --git a/compiler-rt/lib/orc/extensible_rtti.cpp b/compiler-rt/lib/orc/extensible_rtti.cpp
new file mode 100644
index 0000000000000..c6951a449a3d9
--- /dev/null
+++ b/compiler-rt/lib/orc/extensible_rtti.cpp
@@ -0,0 +1,24 @@
+//===- extensible_rtti.cpp ------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of the ORC runtime support library.
+//
+// Note:
+//   This source file was adapted from lib/Support/ExtensibleRTTI.cpp, however
+// the data structures are not shared and the code need not be kept in sync.
+//
+//===----------------------------------------------------------------------===//
+
+#include "extensible_rtti.h"
+
+namespace __orc_rt {
+
+char RTTIRoot::ID = 0;
+void RTTIRoot::anchor() {}
+
+} // end namespace __orc_rt

diff  --git a/compiler-rt/lib/orc/extensible_rtti.h b/compiler-rt/lib/orc/extensible_rtti.h
new file mode 100644
index 0000000000000..72f68242e7c45
--- /dev/null
+++ b/compiler-rt/lib/orc/extensible_rtti.h
@@ -0,0 +1,145 @@
+//===------ extensible_rtti.h - Extensible RTTI for ORC RT ------*- C++ -*-===//
+//
+// 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
+//
+// Provides an extensible RTTI mechanism, that can be used regardless of whether
+// the runtime is built with -frtti or not. This is predominantly used to
+// support error handling.
+//
+// The RTTIRoot class defines methods for comparing type ids. Implementations
+// of these methods can be injected into new classes using the RTTIExtends
+// class template.
+//
+// E.g.
+//
+//   @code{.cpp}
+//   class MyBaseClass : public RTTIExtends<MyBaseClass, RTTIRoot> {
+//   public:
+//     static char ID;
+//     virtual void foo() = 0;
+//   };
+//
+//   class MyDerivedClass1 : public RTTIExtends<MyDerivedClass1, MyBaseClass> {
+//   public:
+//     static char ID;
+//     void foo() override {}
+//   };
+//
+//   class MyDerivedClass2 : public RTTIExtends<MyDerivedClass2, MyBaseClass> {
+//   public:
+//     static char ID;
+//     void foo() override {}
+//   };
+//
+//   char MyBaseClass::ID = 0;
+//   char MyDerivedClass1::ID = 0;
+//   char MyDerivedClass2:: ID = 0;
+//
+//   void fn() {
+//     std::unique_ptr<MyBaseClass> B = std::make_unique<MyDerivedClass1>();
+//     outs() << isa<MyBaseClass>(B) << "\n"; // Outputs "1".
+//     outs() << isa<MyDerivedClass1>(B) << "\n"; // Outputs "1".
+//     outs() << isa<MyDerivedClass2>(B) << "\n"; // Outputs "0'.
+//   }
+//
+//   @endcode
+//
+// Note:
+//   This header was adapted from llvm/Support/ExtensibleRTTI.h, however the
+// data structures are not shared and the code need not be kept in sync.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef ORC_RT_EXTENSIBLE_RTTI_H
+#define ORC_RT_EXTENSIBLE_RTTI_H
+
+namespace __orc_rt {
+
+template <typename ThisT, typename ParentT> class RTTIExtends;
+
+/// Base class for the extensible RTTI hierarchy.
+///
+/// This class defines virtual methods, dynamicClassID and isA, that enable
+/// type comparisons.
+class RTTIRoot {
+public:
+  virtual ~RTTIRoot() = default;
+
+  /// Returns the class ID for this type.
+  static const void *classID() { return &ID; }
+
+  /// Returns the class ID for the dynamic type of this RTTIRoot instance.
+  virtual const void *dynamicClassID() const = 0;
+
+  /// Returns true if this class's ID matches the given class ID.
+  virtual bool isA(const void *const ClassID) const {
+    return ClassID == classID();
+  }
+
+  /// Check whether this instance is a subclass of QueryT.
+  template <typename QueryT> bool isA() const { return isA(QueryT::classID()); }
+
+  static bool classof(const RTTIRoot *R) { return R->isA<RTTIRoot>(); }
+
+private:
+  virtual void anchor();
+
+  static char ID;
+};
+
+/// Inheritance utility for extensible RTTI.
+///
+/// Supports single inheritance only: A class can only have one
+/// ExtensibleRTTI-parent (i.e. a parent for which the isa<> test will work),
+/// though it can have many non-ExtensibleRTTI parents.
+///
+/// RTTIExtents uses CRTP so the first template argument to RTTIExtends is the
+/// newly introduced type, and the *second* argument is the parent class.
+///
+/// class MyType : public RTTIExtends<MyType, RTTIRoot> {
+/// public:
+///   static char ID;
+/// };
+///
+/// class MyDerivedType : public RTTIExtends<MyDerivedType, MyType> {
+/// public:
+///   static char ID;
+/// };
+///
+template <typename ThisT, typename ParentT> class RTTIExtends : public ParentT {
+public:
+  // Inherit constructors and isA methods from ParentT.
+  using ParentT::isA;
+  using ParentT::ParentT;
+
+  static char ID;
+
+  static const void *classID() { return &ThisT::ID; }
+
+  const void *dynamicClassID() const override { return &ThisT::ID; }
+
+  bool isA(const void *const ClassID) const override {
+    return ClassID == classID() || ParentT::isA(ClassID);
+  }
+
+  static bool classof(const RTTIRoot *R) { return R->isA<ThisT>(); }
+};
+
+template <typename ThisT, typename ParentT>
+char RTTIExtends<ThisT, ParentT>::ID = 0;
+
+/// Returns true if the given value is an instance of the template type
+/// parameter.
+template <typename To, typename From> bool isa(const From &Value) {
+  return To::classof(&Value);
+}
+
+} // end namespace __orc_rt
+
+#endif // ORC_RT_EXTENSIBLE_RTTI_H

diff  --git a/compiler-rt/lib/orc/placeholder.cpp b/compiler-rt/lib/orc/placeholder.cpp
deleted file mode 100644
index c832f6007a7d1..0000000000000
--- a/compiler-rt/lib/orc/placeholder.cpp
+++ /dev/null
@@ -1 +0,0 @@
-void placeholder(void) {}

diff  --git a/compiler-rt/lib/orc/unittests/CMakeLists.txt b/compiler-rt/lib/orc/unittests/CMakeLists.txt
new file mode 100644
index 0000000000000..b9884b49e7429
--- /dev/null
+++ b/compiler-rt/lib/orc/unittests/CMakeLists.txt
@@ -0,0 +1,101 @@
+include(CompilerRTCompile)
+
+include_directories(..)
+
+add_custom_target(OrcRTUnitTests)
+set_target_properties(OrcRTUnitTests PROPERTIES FOLDER "OrcRT unittests")
+
+set(ORC_UNITTEST_CFLAGS
+  ${ORC_CFLAGS}
+  ${COMPILER_RT_UNITTEST_CFLAGS}
+  ${COMPILER_RT_GTEST_CFLAGS}
+  -I${COMPILER_RT_SOURCE_DIR}/lib/orc
+  )
+
+# We add the include directories one at a time in our CFLAGS.
+foreach (DIR ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})
+  list(APPEND ORC_UNITTEST_CFLAGS -I${DIR})
+endforeach()
+
+function(add_orc_lib library)
+  add_library(${library} STATIC ${ARGN})
+  set_target_properties(${library} PROPERTIES
+    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+    FOLDER "Compiler-RT Runtime tests")
+endfunction()
+
+function(get_orc_lib_for_arch arch lib)
+  if(APPLE)
+    set(tgt_name "RTOrc.test.osx")
+  else()
+    set(tgt_name "RTOrc.test.${arch}")
+  endif()
+  set(${lib} "${tgt_name}" PARENT_SCOPE)
+endfunction()
+
+set(ORC_TEST_ARCH ${ORC_SUPPORTED_ARCH})
+set(ORC_UNITTEST_LINK_FLAGS
+  ${COMPILER_RT_UNITTEST_LINK_FLAGS}
+  ${CMAKE_THREAD_LIBS_INIT}
+  )
+
+if(APPLE)
+  darwin_filter_host_archs(ORC_SUPPORTED_ARCH ORC_TEST_ARCH)
+  list(APPEND ORC_UNITTEST_CFLAGS ${DARWIN_osx_CFLAGS})
+  list(APPEND ORC_UNITTEST_LINK_FLAGS ${DARWIN_osx_LINK_FLAGS})
+else()
+  append_list_if(COMPILER_RT_HAS_LIBM -lm ORC_UNITTEST_LINK_FLAGS)
+  append_list_if(COMPILER_RT_HAS_LIBRT -lrt ORC_UNITTEST_LINK_FLAGS)
+  append_list_if(COMPILER_RT_HAS_LIBDL -ldl ORC_UNITTEST_LINK_FLAGS)
+  append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread ORC_UNITTEST_LINK_FLAGS)
+  append_list_if(COMPILER_RT_HAS_LIBEXECINFO -lexecinfo ORC_UNITTEST_LINK_FLAGS)
+endif()
+
+foreach(lib ${SANITIZER_TEST_CXX_LIBRARIES})
+  list(APPEND ORC_UNITTEST_LINK_FLAGS -l${lib})
+endforeach()
+
+set(ORC_DEPS gtest orc)
+# ORC uses C++ standard library headers.
+if (TARGET cxx-headers OR HAVE_LIBCXX)
+  set(ORC_DEPS cxx-headers)
+endif()
+
+macro(add_orc_unittest testname)
+  cmake_parse_arguments(TEST "" "" "SOURCES;HEADERS" ${ARGN})
+  if(UNIX)
+    foreach(arch ${ORC_TEST_ARCH})
+      set(TEST_OBJECTS)
+      get_orc_lib_for_arch(${arch} ORC_RUNTIME_LIBS)
+      generate_compiler_rt_tests(TEST_OBJECTS
+        OrcRTUnitTests "${testname}-${arch}-Test" "${arch}"
+        SOURCES ${TEST_SOURCES} ${COMPILER_RT_GTEST_SOURCE}
+        RUNTIME "${ORC_RUNTIME_LIBS}"
+        COMPILE_DEPS ${TEST_HEADERS}
+        DEPS ${ORC_DEPS}
+        CFLAGS ${ORC_UNITTEST_CFLAGS}
+        LINK_FLAGS ${ORC_UNITTEST_LINK_FLAGS})
+    endforeach()
+  endif()
+endmacro()
+
+set(UNITTEST_SOURCES
+  extensible_rtti_test.cpp
+  orc_unit_test_main.cpp
+  )
+
+if (COMPILER_RT_CAN_EXECUTE_TESTS)
+
+  if (APPLE)
+    add_orc_lib("RTOrc.test.osx"
+      $<TARGET_OBJECTS:RTOrc.osx>)
+  else()
+    foreach(arch ${ORC_SUPPORTED_ARCH})
+      add_orc_lib("RTOrc.test.${arch}"
+        $<TARGET_OBJECTS:RTOrc.${arch}>)
+    endforeach()
+  endif()
+
+  add_orc_unittest(OrcUnitTest SOURCES ${UNITTEST_SOURCES})
+
+endif()

diff  --git a/compiler-rt/lib/orc/unittests/extensible_rtti_test.cpp b/compiler-rt/lib/orc/unittests/extensible_rtti_test.cpp
new file mode 100644
index 0000000000000..feca1ec1d18ce
--- /dev/null
+++ b/compiler-rt/lib/orc/unittests/extensible_rtti_test.cpp
@@ -0,0 +1,54 @@
+//===-- extensible_rtti_test.cpp ------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of the ORC runtime.
+//
+// Note:
+//   This unit test was adapted from
+//   llvm/unittests/Support/ExtensibleRTTITest.cpp
+//
+//===----------------------------------------------------------------------===//
+
+#include "extensible_rtti.h"
+#include "gtest/gtest.h"
+
+using namespace __orc_rt;
+
+namespace {
+
+class MyBase : public RTTIExtends<MyBase, RTTIRoot> {};
+
+class MyDerivedA : public RTTIExtends<MyDerivedA, MyBase> {};
+
+class MyDerivedB : public RTTIExtends<MyDerivedB, MyBase> {};
+
+} // end anonymous namespace
+
+TEST(ExtensibleRTTITest, BaseCheck) {
+  MyBase MB;
+  MyDerivedA MDA;
+  MyDerivedB MDB;
+
+  // Check MB properties.
+  EXPECT_TRUE(isa<RTTIRoot>(MB));
+  EXPECT_TRUE(isa<MyBase>(MB));
+  EXPECT_FALSE(isa<MyDerivedA>(MB));
+  EXPECT_FALSE(isa<MyDerivedB>(MB));
+
+  // Check MDA properties.
+  EXPECT_TRUE(isa<RTTIRoot>(MDA));
+  EXPECT_TRUE(isa<MyBase>(MDA));
+  EXPECT_TRUE(isa<MyDerivedA>(MDA));
+  EXPECT_FALSE(isa<MyDerivedB>(MDA));
+
+  // Check MDB properties.
+  EXPECT_TRUE(isa<RTTIRoot>(MDB));
+  EXPECT_TRUE(isa<MyBase>(MDB));
+  EXPECT_FALSE(isa<MyDerivedA>(MDB));
+  EXPECT_TRUE(isa<MyDerivedB>(MDB));
+}

diff  --git a/compiler-rt/lib/orc/unittests/orc_unit_test_main.cpp b/compiler-rt/lib/orc/unittests/orc_unit_test_main.cpp
new file mode 100644
index 0000000000000..d02101704d654
--- /dev/null
+++ b/compiler-rt/lib/orc/unittests/orc_unit_test_main.cpp
@@ -0,0 +1,18 @@
+//===-- orc_unit_test_main.cpp --------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of the ORC runtime.
+//
+//===----------------------------------------------------------------------===//
+#include "gtest/gtest.h"
+
+int main(int argc, char **argv) {
+  testing::GTEST_FLAG(death_test_style) = "threadsafe";
+  testing::InitGoogleTest(&argc, argv);
+  return RUN_ALL_TESTS();
+}


        


More information about the llvm-commits mailing list