Merge pull request #13613 from aschwaighofer/fix_unowned_refcount_insertion

When inserting retain/releases for sil_unowned types use the right in…
diff --git a/lib/SILOptimizer/Utils/Local.cpp b/lib/SILOptimizer/Utils/Local.cpp
index 9469e5d..0b49b82 100644
--- a/lib/SILOptimizer/Utils/Local.cpp
+++ b/lib/SILOptimizer/Utils/Local.cpp
@@ -54,8 +54,12 @@
 
   // If Ptr is refcounted itself, create the strong_retain and
   // return.
-  if (Ptr->getType().isReferenceCounted(B.getModule()))
-    return B.createStrongRetain(Loc, Ptr, B.getDefaultAtomicity());
+  if (Ptr->getType().isReferenceCounted(B.getModule())) {
+    if (Ptr->getType().is<UnownedStorageType>())
+      return B.createUnownedRetain(Loc, Ptr, B.getDefaultAtomicity());
+    else
+      return B.createStrongRetain(Loc, Ptr, B.getDefaultAtomicity());
+  }
 
   // Otherwise, create the retain_value.
   return B.createRetainValue(Loc, Ptr, B.getDefaultAtomicity());
@@ -74,8 +78,12 @@
   auto Loc = RegularLocation(SourceLoc());
 
   // If Ptr has reference semantics itself, create a strong_release.
-  if (Ptr->getType().isReferenceCounted(B.getModule()))
-    return B.createStrongRelease(Loc, Ptr, B.getDefaultAtomicity());
+  if (Ptr->getType().isReferenceCounted(B.getModule())) {
+    if (Ptr->getType().is<UnownedStorageType>())
+      return B.createUnownedRelease(Loc, Ptr, B.getDefaultAtomicity());
+    else
+      return B.createStrongRelease(Loc, Ptr, B.getDefaultAtomicity());
+  }
 
   // Otherwise create a release value.
   return B.createReleaseValue(Loc, Ptr, B.getDefaultAtomicity());
diff --git a/test/SILOptimizer/mandatory_inlining.swift b/test/SILOptimizer/mandatory_inlining.swift
index c1e3e96..d9f28e7 100644
--- a/test/SILOptimizer/mandatory_inlining.swift
+++ b/test/SILOptimizer/mandatory_inlining.swift
@@ -160,3 +160,17 @@
   _ = class_constrained_generic(c)
   // CHECK: return
 }
+
+// Make sure we don't crash.
+@_transparent
+public func mydo(_ what: @autoclosure () -> ()) {
+  what()
+}
+public class A {
+  public func bar() {}
+  public func foo(_ act: (@escaping () ->()) -> ()) {
+    act { [unowned self] in
+      mydo( self.bar() )
+    }
+  }
+}