[llvm] 2a739f2 - [ORC][ORC-RT] Register type metadata from __swift5_types MachO section

Ben Langmuir via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 12 16:40:06 PST 2021


Author: Ben Langmuir
Date: 2021-11-12T16:39:59-08:00
New Revision: 2a739f2789c3549aec3d77807e452dc22e4ef9f9

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

LOG: [ORC][ORC-RT] Register type metadata from __swift5_types MachO section

Similar to how the other swift sections are registered by the ORC
runtime's macho platform, add the __swift5_types section, which contains
type metadata. Add a simple test that demonstrates that the swift
runtime recognized the registered types.

rdar://85358530

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

Added: 
    compiler-rt/test/orc/TestCases/Darwin/x86-64/trivial-swift-types-section.S

Modified: 
    compiler-rt/lib/orc/macho_platform.cpp
    compiler-rt/test/lit.common.cfg.py
    llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
    llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/orc/macho_platform.cpp b/compiler-rt/lib/orc/macho_platform.cpp
index d8f9d5b8ad92..32770219e65c 100644
--- a/compiler-rt/lib/orc/macho_platform.cpp
+++ b/compiler-rt/lib/orc/macho_platform.cpp
@@ -50,6 +50,7 @@ extern "C" SEL sel_registerName(const char *) ORC_RT_WEAK_IMPORT;
 // Swift types.
 class ProtocolRecord;
 class ProtocolConformanceRecord;
+class TypeMetadataRecord;
 
 extern "C" void
 swift_registerProtocols(const ProtocolRecord *begin,
@@ -59,6 +60,10 @@ extern "C" void swift_registerProtocolConformances(
     const ProtocolConformanceRecord *begin,
     const ProtocolConformanceRecord *end) ORC_RT_WEAK_IMPORT;
 
+extern "C" void swift_registerTypeMetadataRecords(
+    const TypeMetadataRecord *begin,
+    const TypeMetadataRecord *end) ORC_RT_WEAK_IMPORT;
+
 namespace {
 
 Error validatePointerSectionExtent(const char *SectionName,
@@ -172,6 +177,21 @@ Error registerSwift5ProtocolConformances(
   return Error::success();
 }
 
+Error registerSwift5Types(const std::vector<ExecutorAddrRange> &Sections,
+                          const MachOJITDylibInitializers &MOJDIs) {
+
+  if (ORC_RT_UNLIKELY(!Sections.empty() && !swift_registerTypeMetadataRecords))
+    return make_error<StringError>(
+        "swift_registerTypeMetadataRecords is not available");
+
+  for (const auto &Section : Sections)
+    swift_registerTypeMetadataRecords(
+        Section.Start.toPtr<const TypeMetadataRecord *>(),
+        Section.End.toPtr<const TypeMetadataRecord *>());
+
+  return Error::success();
+}
+
 Error runModInits(const std::vector<ExecutorAddrRange> &ModInitsSections,
                   const MachOJITDylibInitializers &MOJDIs) {
 
@@ -261,6 +281,7 @@ class MachOPlatformRuntimeState {
        {"__DATA,__objc_classlist", registerObjCClasses},
        {"__TEXT,__swift5_protos", registerSwift5Protocols},
        {"__TEXT,__swift5_proto", registerSwift5ProtocolConformances},
+       {"__TEXT,__swift5_types", registerSwift5Types},
        {"__DATA,__mod_init_func", runModInits}};
 
   // FIXME: Move to thread-state.

diff  --git a/compiler-rt/test/lit.common.cfg.py b/compiler-rt/test/lit.common.cfg.py
index a5ef10faac5c..c4ef8e3c42a8 100644
--- a/compiler-rt/test/lit.common.cfg.py
+++ b/compiler-rt/test/lit.common.cfg.py
@@ -400,6 +400,8 @@ def get_ios_commands_dir():
     if osx_version >= (10, 11):
       config.available_features.add('osx-autointerception')
       config.available_features.add('osx-ld64-live_support')
+    if osx_version >= (10, 15):
+      config.available_features.add('osx-swift-runtime')
   except subprocess.CalledProcessError:
     pass
 

diff  --git a/compiler-rt/test/orc/TestCases/Darwin/x86-64/trivial-swift-types-section.S b/compiler-rt/test/orc/TestCases/Darwin/x86-64/trivial-swift-types-section.S
new file mode 100644
index 000000000000..106bb5e9b8ed
--- /dev/null
+++ b/compiler-rt/test/orc/TestCases/Darwin/x86-64/trivial-swift-types-section.S
@@ -0,0 +1,94 @@
+# RUN: %clang -c -o %t %s
+# RUN: %llvm_jitlink -dlopen /usr/lib/swift/libswiftCore.dylib %t
+
+# REQUIRES: osx-swift-runtime
+
+# Check that _typeByName is able to find a type registered by metadata in
+# the __swift5_types section.
+
+  .section  __TEXT,__text,regular,pure_instructions
+  .globl  _main
+  .p2align  4, 0x90
+_main:
+  .cfi_startproc
+  pushq %rbp
+  .cfi_def_cfa_offset 16
+  .cfi_offset %rbp, -16
+  movq  %rsp, %rbp
+  .cfi_def_cfa_register %rbp
+  # Constant String for "4Test3FooV"
+  movabsq $8018152761824990260, %rdi
+  movabsq $-1585267068834392465, %rsi
+  callq _$ss11_typeByNameyypXpSgSSF
+  xorl  %edi, %edi
+  testq %rax, %rax
+  sete  %dil
+  callq _exit
+  .cfi_endproc
+
+  .private_extern _$s4Test3FooVMa
+  .globl  _$s4Test3FooVMa
+  .p2align  4, 0x90
+_$s4Test3FooVMa:
+  leaq  _$s4Test3FooVMf+8(%rip), %rax
+  xorl  %edx, %edx
+  retq
+
+  .section  __TEXT,__const
+l___unnamed_1:
+  .asciz  "Test"
+
+  .private_extern _$s4TestMXM
+  .globl  _$s4TestMXM
+  .weak_definition  _$s4TestMXM
+  .p2align  2
+_$s4TestMXM:
+  .long 0
+  .long 0
+  .long (l___unnamed_1-_$s4TestMXM)-8
+
+l___unnamed_2:
+  .asciz  "Foo"
+
+  .private_extern _$s4Test3FooVMn
+  .globl  _$s4Test3FooVMn
+  .p2align  2
+_$s4Test3FooVMn:
+  .long 81
+  .long (_$s4TestMXM-_$s4Test3FooVMn)-4
+  .long (l___unnamed_2-_$s4Test3FooVMn)-8
+  .long (_$s4Test3FooVMa-_$s4Test3FooVMn)-12
+  .long (_$s4Test3FooVMF-_$s4Test3FooVMn)-16
+  .long 0
+  .long 2
+
+  .section  __DATA,__const
+  .p2align  3
+_$s4Test3FooVMf:
+  .quad _$sytWV
+  .quad 512
+  .quad _$s4Test3FooVMn
+
+  .private_extern "_symbolic _____ 4Test3FooV"
+  .section  __TEXT,__swift5_typeref,regular,no_dead_strip
+  .globl  "_symbolic _____ 4Test3FooV"
+  .weak_definition  "_symbolic _____ 4Test3FooV"
+  .p2align  1
+"_symbolic _____ 4Test3FooV":
+  .byte 1
+  .long (_$s4Test3FooVMn-"_symbolic _____ 4Test3FooV")-1
+  .byte 0
+
+  .section  __TEXT,__swift5_fieldmd,regular,no_dead_strip
+  .p2align  2
+_$s4Test3FooVMF:
+  .long "_symbolic _____ 4Test3FooV"-_$s4Test3FooVMF
+  .long 0
+  .short  0
+  .short  12
+  .long 0
+
+  .section  __TEXT,__swift5_types,regular,no_dead_strip
+  .p2align  2
+l_type_metadata_table:
+  .long _$s4Test3FooVMn-l_type_metadata_table

diff  --git a/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp b/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
index ad92d82e029c..46c915dfea9e 100644
--- a/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
@@ -136,13 +136,14 @@ StringRef ObjCImageInfoSectionName = "__DATA,__objc_image_info";
 StringRef ObjCSelRefsSectionName = "__DATA,__objc_selrefs";
 StringRef Swift5ProtoSectionName = "__TEXT,__swift5_proto";
 StringRef Swift5ProtosSectionName = "__TEXT,__swift5_protos";
+StringRef Swift5TypesSectionName = "__TEXT,__swift5_types";
 StringRef ThreadBSSSectionName = "__DATA,__thread_bss";
 StringRef ThreadDataSectionName = "__DATA,__thread_data";
 StringRef ThreadVarsSectionName = "__DATA,__thread_vars";
 
 StringRef InitSectionNames[] = {
     ModInitFuncSectionName, ObjCSelRefsSectionName, ObjCClassListSectionName,
-    Swift5ProtosSectionName, Swift5ProtoSectionName};
+    Swift5ProtosSectionName, Swift5ProtoSectionName, Swift5TypesSectionName};
 
 } // end anonymous namespace
 

diff  --git a/llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp b/llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp
index 88f6dc1c3ea8..92da6723c372 100644
--- a/llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp
@@ -77,6 +77,7 @@ class LinkGraphMaterializationUnit : public MaterializationUnit {
           Sec.getName() == "__DATA,__objc_classlist" ||
           Sec.getName() == "__TEXT,__swift5_protos" ||
           Sec.getName() == "__TEXT,__swift5_proto" ||
+          Sec.getName() == "__TEXT,__swift5_types" ||
           Sec.getName() == "__DATA,__mod_init_func")
         return true;
     return false;


        


More information about the llvm-commits mailing list