Merge pull request #20699 from slavapestov/overly-lazy-witness-table-emission

IRGen: Force emission of lazy witness table when conformance descriptor is referenced
diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp
index 7f77f0e..238cac9 100644
--- a/lib/IRGen/GenDecl.cpp
+++ b/lib/IRGen/GenDecl.cpp
@@ -3539,6 +3539,8 @@
 llvm::Constant *IRGenModule::getAddrOfProtocolConformanceDescriptor(
                                 const RootProtocolConformance *conformance,
                                 ConstantInit definition) {
+  IRGen.addLazyWitnessTable(conformance);
+
   auto entity = LinkEntity::forProtocolConformanceDescriptor(conformance);
   return getAddrOfLLVMVariable(entity, definition,
                                DebugTypeInfo());
diff --git a/test/IRGen/lazy_conformances.swift b/test/IRGen/lazy_conformances.swift
new file mode 100644
index 0000000..7f7889d
--- /dev/null
+++ b/test/IRGen/lazy_conformances.swift
@@ -0,0 +1,41 @@
+// RUN: %target-swift-frontend -parse-as-library -O %s -emit-ir | %FileCheck %s
+
+// CHECK: @"$s17lazy_conformances12MyCollectionVyxGSKAASKRzrlMc" = hidden constant {
+// CHECK: @"$s17lazy_conformances12MyCollectionVyxGSTAAMc" = hidden constant {
+
+struct MyCollection<Base : Collection> : Collection {
+  typealias Index = Base.Index
+  typealias Element = Base.Element
+
+  var startIndex: Index {
+    fatalError()
+  }
+
+  var endIndex: Index {
+    fatalError()
+  }
+
+  func index(after i: Index) -> Index {
+    fatalError()
+  }
+
+  func formIndex(after i: inout Index) {
+    fatalError()
+  }
+
+  subscript(position: Index) -> Element {
+    fatalError()
+  }
+}
+
+extension MyCollection : BidirectionalCollection
+  where Base : BidirectionalCollection
+{
+  func index(before i: Index) -> Index {
+    fatalError()
+  }
+
+  func formIndex(before i: inout Index) {
+    fatalError()
+  }
+}