Merge pull request #12000 from moiseev/arkit+metal-4

[swift-4.0-branch][overlay] Metal, MetalKit, and ARKit changes.
diff --git a/stdlib/public/SDK/ARKit/ARKit.swift b/stdlib/public/SDK/ARKit/ARKit.swift
index 47863e6..a2baa6a 100644
--- a/stdlib/public/SDK/ARKit/ARKit.swift
+++ b/stdlib/public/SDK/ARKit/ARKit.swift
@@ -78,3 +78,30 @@
         return Array(buffer)
     }
 }
+
+@available(iOS, introduced: 11.0)
+extension ARFaceGeometry {
+    /**
+     The mesh vertices of the geometry.
+     */
+    @nonobjc public var vertices: [vector_float3] {
+        let buffer = UnsafeBufferPointer(start: __vertices, count: Int(__vertexCount))
+        return Array(buffer)
+    }
+
+    /**
+     The texture coordinates of the geometry.
+     */
+    @nonobjc public var textureCoordinates: [vector_float2] {
+        let buffer = UnsafeBufferPointer(start: __textureCoordinates, count: Int(__textureCoordinateCount))
+        return Array(buffer)
+    }
+
+    /**
+     The triangle indices of the geometry.
+     */
+    @nonobjc public var triangleIndices: [Int16] {
+        let buffer = UnsafeBufferPointer(start: __triangleIndices, count: Int(triangleCount * 3))
+        return Array(buffer)
+    }
+}
diff --git a/stdlib/public/SDK/Accelerate/CMakeLists.txt b/stdlib/public/SDK/Accelerate/CMakeLists.txt
index 2ad8736..fe4cdfa 100644
--- a/stdlib/public/SDK/Accelerate/CMakeLists.txt
+++ b/stdlib/public/SDK/Accelerate/CMakeLists.txt
@@ -5,9 +5,10 @@
   LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
   TARGET_SDKS OSX IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR
   SWIFT_COMPILE_FLAGS ${STDLIB_SIL_SERIALIZE_ALL} -parse-stdlib
-  SWIFT_MODULE_DEPENDS_OSX Darwin CoreFoundation CoreGraphics Dispatch Foundation IOKit ObjectiveC os XPC # auto-updated
-  SWIFT_MODULE_DEPENDS_IOS Darwin CoreFoundation CoreGraphics Dispatch Foundation ObjectiveC os # auto-updated
-  SWIFT_MODULE_DEPENDS_TVOS Darwin CoreFoundation CoreGraphics Dispatch Foundation ObjectiveC os # auto-updated
+  SWIFT_MODULE_DEPENDS_OSX Darwin CoreFoundation CoreGraphics Dispatch Foundation IOKit Metal ObjectiveC XPC # auto-updated
+    os
+  SWIFT_MODULE_DEPENDS_IOS Darwin CoreFoundation CoreGraphics Dispatch Foundation Metal ObjectiveC os # auto-updated
+  SWIFT_MODULE_DEPENDS_TVOS Darwin CoreFoundation CoreGraphics Dispatch Foundation Metal ObjectiveC os # auto-updated
   SWIFT_MODULE_DEPENDS_WATCHOS Darwin CoreFoundation CoreGraphics Dispatch ObjectiveC os # auto-updated
 
   DEPLOYMENT_VERSION_OSX ${SWIFTLIB_DEPLOYMENT_VERSION_SIMD_OSX}
diff --git a/stdlib/public/SDK/CMakeLists.txt b/stdlib/public/SDK/CMakeLists.txt
index eb0cb2c..aad77ac 100644
--- a/stdlib/public/SDK/CMakeLists.txt
+++ b/stdlib/public/SDK/CMakeLists.txt
@@ -10,7 +10,7 @@
 
 list(APPEND SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS "${STDLIB_SIL_SERIALIZE_WITNESS_TABLES}")
 
-set(all_overlays "Accelerate;AppKit;ARKit;AssetsLibrary;AVFoundation;CallKit;CloudKit;Contacts;CoreAudio;CoreData;CoreFoundation;CoreGraphics;CoreImage;CoreLocation;CoreMedia;CryptoTokenKit;Dispatch;Foundation;GameplayKit;GLKit;HomeKit;IOKit;Intents;MapKit;MediaPlayer;ModelIO;ObjectiveC;OpenCL;os;Photos;QuartzCore;SafariServices;SceneKit;simd;SpriteKit;UIKit;Vision;WatchKit;XCTest;XPC")
+set(all_overlays "Accelerate;AppKit;ARKit;AssetsLibrary;AVFoundation;CallKit;CloudKit;Contacts;CoreAudio;CoreData;CoreFoundation;CoreGraphics;CoreImage;CoreLocation;CoreMedia;CryptoTokenKit;Dispatch;Foundation;GameplayKit;GLKit;HomeKit;IOKit;Intents;MapKit;MediaPlayer;Metal;MetalKit;ModelIO;ObjectiveC;OpenCL;os;Photos;QuartzCore;SafariServices;SceneKit;simd;SpriteKit;UIKit;Vision;WatchKit;XCTest;XPC")
 
 if(DEFINED SWIFT_OVERLAY_TARGETS)
   set(overlays_to_build ${SWIFT_OVERLAY_TARGETS})
diff --git a/stdlib/public/SDK/CoreImage/CMakeLists.txt b/stdlib/public/SDK/CoreImage/CMakeLists.txt
index 0d6deee..2fe1a7c 100644
--- a/stdlib/public/SDK/CoreImage/CMakeLists.txt
+++ b/stdlib/public/SDK/CoreImage/CMakeLists.txt
@@ -7,9 +7,9 @@
   SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
   LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
   TARGET_SDKS OSX IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR
-  SWIFT_MODULE_DEPENDS_OSX Darwin CoreFoundation CoreGraphics Dispatch Foundation IOKit ObjectiveC XPC # auto-updated
-  SWIFT_MODULE_DEPENDS_IOS Darwin CoreFoundation CoreGraphics Dispatch Foundation ObjectiveC # auto-updated
-  SWIFT_MODULE_DEPENDS_TVOS Darwin CoreFoundation CoreGraphics Dispatch Foundation ObjectiveC # auto-updated
+  SWIFT_MODULE_DEPENDS_OSX Darwin CoreFoundation CoreGraphics Dispatch Foundation IOKit Metal ObjectiveC XPC # auto-updated
+  SWIFT_MODULE_DEPENDS_IOS Darwin CoreFoundation CoreGraphics Dispatch Foundation Metal ObjectiveC # auto-updated
+  SWIFT_MODULE_DEPENDS_TVOS Darwin CoreFoundation CoreGraphics Dispatch Foundation Metal ObjectiveC # auto-updated
   FRAMEWORK_DEPENDS_OSX QuartzCore
   FRAMEWORK_DEPENDS_IOS_TVOS CoreImage
 
diff --git a/stdlib/public/SDK/CoreMedia/CMakeLists.txt b/stdlib/public/SDK/CoreMedia/CMakeLists.txt
index c848124..4a6f013 100644
--- a/stdlib/public/SDK/CoreMedia/CMakeLists.txt
+++ b/stdlib/public/SDK/CoreMedia/CMakeLists.txt
@@ -8,9 +8,9 @@
   SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
   LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
   TARGET_SDKS OSX IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR
-  SWIFT_MODULE_DEPENDS_OSX Darwin AppKit CoreAudio CoreData CoreGraphics CoreImage Dispatch Foundation IOKit ObjectiveC QuartzCore XPC # auto-updated
-  SWIFT_MODULE_DEPENDS_IOS Darwin CoreAudio CoreGraphics Dispatch Foundation ObjectiveC # auto-updated
-  SWIFT_MODULE_DEPENDS_TVOS Darwin CoreAudio CoreGraphics Dispatch Foundation ObjectiveC # auto-updated
+  SWIFT_MODULE_DEPENDS_OSX Darwin AppKit CoreAudio CoreData CoreGraphics CoreImage Dispatch Foundation IOKit Metal ObjectiveC QuartzCore XPC # auto-updated
+  SWIFT_MODULE_DEPENDS_IOS Darwin CoreAudio CoreGraphics Dispatch Foundation Metal ObjectiveC # auto-updated
+  SWIFT_MODULE_DEPENDS_TVOS Darwin CoreAudio CoreGraphics Dispatch Foundation Metal ObjectiveC # auto-updated
   FRAMEWORK_DEPENDS CoreMedia
 
   DEPLOYMENT_VERSION_OSX ${SWIFTLIB_DEPLOYMENT_VERSION_COREMEDIA_OSX}
diff --git a/stdlib/public/SDK/MediaPlayer/CMakeLists.txt b/stdlib/public/SDK/MediaPlayer/CMakeLists.txt
index 8334da5..8d28d4d 100644
--- a/stdlib/public/SDK/MediaPlayer/CMakeLists.txt
+++ b/stdlib/public/SDK/MediaPlayer/CMakeLists.txt
@@ -8,7 +8,7 @@
   LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
   TARGET_SDKS IOS IOS_SIMULATOR
 
-  SWIFT_MODULE_DEPENDS_IOS Darwin AVFoundation CoreAudio CoreFoundation CoreGraphics CoreImage CoreMedia Dispatch Foundation ObjectiveC QuartzCore simd UIKit os CoreData # auto-updated
+  SWIFT_MODULE_DEPENDS_IOS Darwin AVFoundation CoreAudio CoreFoundation CoreGraphics CoreImage CoreMedia Dispatch Foundation Metal ObjectiveC QuartzCore simd UIKit os CoreData # auto-updated
   FRAMEWORK_DEPENDS_WEAK MediaPlayer
 
   DEPLOYMENT_VERSION_IOS ${SWIFTLIB_DEPLOYMENT_VERSION_MEDIAPLAYER_IOS}
diff --git a/stdlib/public/SDK/Metal/CMakeLists.txt b/stdlib/public/SDK/Metal/CMakeLists.txt
new file mode 100644
index 0000000..f3fe951
--- /dev/null
+++ b/stdlib/public/SDK/Metal/CMakeLists.txt
@@ -0,0 +1,22 @@
+cmake_minimum_required(VERSION 3.4.3)
+include("../../../../cmake/modules/StandaloneOverlay.cmake")
+
+add_swift_library(swiftMetal ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
+    Metal.swift
+
+  SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
+  LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
+  TARGET_SDKS OSX IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR
+  SWIFT_MODULE_DEPENDS_OSX Darwin CoreFoundation CoreGraphics Dispatch Foundation IOKit ObjectiveC XPC # auto-updated
+    os
+  SWIFT_MODULE_DEPENDS_IOS Darwin CoreFoundation Dispatch Foundation ObjectiveC # auto-updated
+    os
+  SWIFT_MODULE_DEPENDS_TVOS Darwin CoreFoundation Dispatch Foundation ObjectiveC # auto-updated
+    os
+
+  FRAMEWORK_DEPENDS_WEAK Metal
+
+  DEPLOYMENT_VERSION_OSX ${SWIFTLIB_DEPLOYMENT_VERSION_METAL_OSX}
+  DEPLOYMENT_VERSION_IOS ${SWIFTLIB_DEPLOYMENT_VERSION_METAL_IOS}
+  DEPLOYMENT_VERSION_TVOS ${SWIFTLIB_DEPLOYMENT_VERSION_METAL_TVOS}
+)
diff --git a/stdlib/public/SDK/Metal/Metal.swift b/stdlib/public/SDK/Metal/Metal.swift
new file mode 100644
index 0000000..802e902
--- /dev/null
+++ b/stdlib/public/SDK/Metal/Metal.swift
@@ -0,0 +1,231 @@
+//===----------------------------------------------------------------------===//
+//
+// This source file is part of the Swift.org open source project
+//
+// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
+// Licensed under Apache License v2.0 with Runtime Library Exception
+//
+// See https://swift.org/LICENSE.txt for license information
+// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
+//
+//===----------------------------------------------------------------------===//
+
+@_exported import Metal // Clang module
+
+@available(swift 4)
+@available(macOS 10.11, iOS 8.0, tvOS 8.0, *)
+extension MTLBlitCommandEncoder {
+    
+    public func fill(buffer: MTLBuffer, range: Range<Int>, value: UInt8) {
+        fill(buffer: buffer, range: NSMakeRange(range.lowerBound, range.count), value: value)
+    }
+}
+
+@available(swift 4)
+@available(macOS 10.11, iOS 8.0, tvOS 8.0, *)
+extension MTLBuffer {
+    
+#if os(OSX)
+    @available(macOS, introduced: 10.11)
+    public func didModifyRange(_ range: Range<Int>) {
+        didModifyRange(NSMakeRange(range.lowerBound, range.count))
+    }
+#endif
+    
+    @available(macOS 10.12, iOS 10.0, tvOS 10.0, *)
+    public func addDebugMarker(_ marker: String, range: Range<Int>) {
+        addDebugMarker(marker, range: NSMakeRange(range.lowerBound, range.count))
+    }
+}
+
+@available(swift 4)
+@available(macOS 10.11, iOS 8.0, tvOS 8.0, *)
+extension MTLComputeCommandEncoder {
+    
+    @available(macOS 10.13, iOS 11.0, tvOS 11.0, *)
+    public func useResources(_ resources: [MTLResource], usage: MTLResourceUsage) {
+        useResources(resources, count: resources.count, usage: usage)
+    }
+    
+    @available(macOS 10.13, iOS 11.0, tvOS 11.0, *)
+    public func useHeaps(_ heaps: [MTLHeap]) {
+        useHeaps(heaps, count: heaps.count)
+    }
+    
+    public func setBuffers(_ buffers: [MTLBuffer?], offsets: [Int], range: Range<Int>) {
+        setBuffers(buffers, offsets: offsets, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    public func setTextures(_ textures: [MTLTexture?], range: Range<Int>) {
+        setTextures(textures, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    public func setSamplerStates(_ samplers: [MTLSamplerState?], range: Range<Int>) {
+        setSamplerStates(samplers, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    public func setSamplerStates(_ samplers: [MTLSamplerState?], lodMinClamps: [Float], lodMaxClamps: [Float], range: Range<Int>) {
+        setSamplerStates(samplers, lodMinClamps: lodMinClamps, lodMaxClamps: lodMaxClamps, with: NSMakeRange(range.lowerBound, range.count))
+    }
+}
+
+@available(swift 4)
+@available(macOS 10.11, iOS 8.0, tvOS 8.0, *)
+extension MTLDevice {
+    
+    @available(macOS 10.13, iOS 11.0, tvOS 11.0, *)
+    public func getDefaultSamplePositions(sampleCount: Int) -> [MTLSamplePosition] {
+        var positions = [MTLSamplePosition](repeating: MTLSamplePosition(x: 0,y: 0), count: sampleCount)
+        getDefaultSamplePositions(&positions, count: sampleCount)
+        return positions
+    }
+}
+
+#if os(OSX)
+@available(swift 4)
+@available(macOS 10.13, *)
+public func MTLCopyAllDevicesWithObserver(handler: @escaping MTLDeviceNotificationHandler) -> (devices:[MTLDevice], observer:NSObject) {
+    var resultTuple: (devices:[MTLDevice], observer:NSObject)
+    resultTuple.observer = NSObject()
+    resultTuple.devices = MTLCopyAllDevicesWithObserver(AutoreleasingUnsafeMutablePointer<NSObjectProtocol?>(&resultTuple.observer), handler)
+    return resultTuple
+}
+#endif
+
+@available(swift 4)
+@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)
+extension MTLFunctionConstantValues {
+    
+    public func setConstantValues(_ values: UnsafeRawPointer, type: MTLDataType, range: Range<Int>) {
+        setConstantValues(values, type: type, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+}
+
+@available(swift 4)
+@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)
+extension MTLArgumentEncoder {
+    
+    public func setBuffers(_ buffers: [MTLBuffer?], offsets: [Int], range: Range<Int>) {
+        setBuffers(buffers, offsets: offsets, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    public func setTextures(_ textures: [MTLTexture?], range: Range<Int>) {
+        setTextures(textures, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    public func setSamplerStates(_ samplers: [MTLSamplerState?], range: Range<Int>) {
+        setSamplerStates(samplers, with: NSMakeRange(range.lowerBound, range.count))
+    }
+}
+
+@available(swift 4)
+@available(macOS 10.11, iOS 8.0, tvOS 8.0, *)
+extension MTLRenderCommandEncoder {
+    
+    @available(macOS 10.13, iOS 11.0, tvOS 11.0, *)
+    public func useResources(_ resources: [MTLResource], usage: MTLResourceUsage) {
+        useResources(resources, count: resources.count, usage: usage)
+    }
+    
+    @available(macOS 10.13, iOS 11.0, tvOS 11.0, *)
+    public func useHeaps(_ heaps: [MTLHeap]) {
+        useHeaps(heaps, count: heaps.count)
+    }
+    
+#if os(OSX)
+    @available(macOS 10.13, *)
+    public func setViewports(_ viewports: [MTLViewport]) {
+        setViewports(viewports, count: viewports.count)
+    }
+    
+    @available(macOS 10.13, *)
+    public func setScissorRects(_ scissorRects: [MTLScissorRect]) {
+        setScissorRects(scissorRects, count: scissorRects.count)
+    }
+#endif
+    
+    public func setVertexBuffers(_ buffers: [MTLBuffer?], offsets: [Int], range: Range<Int>) {
+        setVertexBuffers(buffers, offsets: offsets, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    public func setVertexTextures(_ textures: [MTLTexture?], range: Range<Int>) {
+        setVertexTextures(textures, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    public func setVertexSamplerStates(_ samplers: [MTLSamplerState?], range: Range<Int>) {
+        setVertexSamplerStates(samplers, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    public func setVertexSamplerStates(_ samplers: [MTLSamplerState?], lodMinClamps: [Float], lodMaxClamps: [Float], range: Range<Int>) {
+        setVertexSamplerStates(samplers, lodMinClamps: lodMinClamps, lodMaxClamps: lodMaxClamps, with: NSMakeRange(range.lowerBound, range.count))
+    }
+
+    public func setFragmentBuffers(_ buffers: [MTLBuffer?], offsets: [Int], range: Range<Int>) {
+        setFragmentBuffers(buffers, offsets: offsets, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    public func setFragmentTextures(_ textures: [MTLTexture?], range: Range<Int>) {
+        setFragmentTextures(textures, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    public func setFragmentSamplerStates(_ samplers: [MTLSamplerState?], range: Range<Int>) {
+        setFragmentSamplerStates(samplers, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    public func setFragmentSamplerStates(_ samplers: [MTLSamplerState?], lodMinClamps: [Float], lodMaxClamps: [Float], range: Range<Int>) {
+        setFragmentSamplerStates(samplers, lodMinClamps: lodMinClamps, lodMaxClamps: lodMaxClamps, with: NSMakeRange(range.lowerBound, range.count))
+    }
+
+#if os(iOS)
+
+    @available(iOS 11.0, *)
+    public func setTileBuffers(_ buffers: [MTLBuffer?], offsets: [Int], range: Range<Int>) {
+        __setTileBuffers(buffers, offsets: offsets, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    @available(iOS 11.0, *)
+    public func setTileTextures(_ textures: [MTLTexture?], range: Range<Int>) {
+        __setTileTextures(textures, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    @available(iOS 11.0, *)
+    public func setTileSamplerStates(_ samplers: [MTLSamplerState?], range: Range<Int>) {
+        __setTileSamplerStates(samplers, with: NSMakeRange(range.lowerBound, range.count))
+    }
+    
+    @available(iOS 11.0, *)
+    public func setTileSamplerStates(_ samplers: [MTLSamplerState?], lodMinClamps: [Float], lodMaxClamps: [Float], range: Range<Int>) {
+        __setTileSamplerStates(samplers, lodMinClamps: lodMinClamps, lodMaxClamps: lodMaxClamps, with: NSMakeRange(range.lowerBound, range.count))
+    }
+#endif
+}
+
+@available(swift 4)
+@available(macOS 10.11, iOS 8.0, tvOS 8.0, *)
+extension MTLRenderPassDescriptor {
+    
+    @available(macOS 10.13, iOS 11.0, tvOS 11.0, *)
+    public func setSamplePositions(_ positions: [MTLSamplePosition]) {
+        setSamplePositions(positions, count: positions.count)
+    }
+    
+    @available(macOS 10.13, iOS 11.0, tvOS 11.0, *)
+    public func getSamplePositions() -> [MTLSamplePosition] {
+        let numPositions = getSamplePositions(nil, count: 0)
+        var positions = [MTLSamplePosition](repeating: MTLSamplePosition(x: 0,y: 0), count: numPositions)
+        getSamplePositions(&positions, count: numPositions)
+        return positions
+    }
+    
+}
+
+@available(swift 4)
+@available(macOS 10.11, iOS 8.0, tvOS 8.0, *)
+extension MTLTexture {
+    
+    @available(macOS 10.11, iOS 9.0, tvOS 9.0, *)
+    public func makeTextureView(pixelFormat: MTLPixelFormat, textureType: MTLTextureType, levels levelRange: Range<Int>, slices sliceRange: Range<Int>) -> MTLTexture? {
+        return makeTextureView(pixelFormat: pixelFormat, textureType: textureType, levels: NSMakeRange(levelRange.lowerBound, levelRange.count), slices: NSMakeRange(sliceRange.lowerBound, sliceRange.count))
+    }
+}
diff --git a/stdlib/public/SDK/MetalKit/CMakeLists.txt b/stdlib/public/SDK/MetalKit/CMakeLists.txt
new file mode 100644
index 0000000..b0a2013
--- /dev/null
+++ b/stdlib/public/SDK/MetalKit/CMakeLists.txt
@@ -0,0 +1,20 @@
+cmake_minimum_required(VERSION 3.4.3)
+include("../../../../cmake/modules/StandaloneOverlay.cmake")
+
+add_swift_library(swiftMetalKit ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
+    MetalKit.swift
+
+  SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
+  LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
+  TARGET_SDKS OSX IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR
+  SWIFT_MODULE_DEPENDS_OSX Darwin AppKit CoreData CoreFoundation CoreGraphics CoreImage Dispatch Foundation IOKit Metal ModelIO ObjectiveC QuartzCore simd XPC # auto-updated
+  SWIFT_MODULE_DEPENDS_IOS Darwin CoreFoundation CoreGraphics CoreImage Dispatch Foundation Metal ModelIO ObjectiveC QuartzCore simd UIKit # auto-updated
+  SWIFT_MODULE_DEPENDS_TVOS Darwin CoreFoundation CoreGraphics CoreImage Dispatch Foundation Metal ModelIO ObjectiveC QuartzCore simd UIKit # auto-updated
+  
+  FRAMEWORK_DEPENDS_WEAK MetalKit
+  FRAMEWORK_DEPENDS_WEAK Metal
+
+  DEPLOYMENT_VERSION_OSX ${SWIFTLIB_DEPLOYMENT_VERSION_METALKIT_OSX}
+  DEPLOYMENT_VERSION_IOS ${SWIFTLIB_DEPLOYMENT_VERSION_METALKIT_IOS}
+  DEPLOYMENT_VERSION_TVOS ${SWIFTLIB_DEPLOYMENT_VERSION_METALKIT_TVOS}
+)
diff --git a/stdlib/public/SDK/MetalKit/MetalKit.swift b/stdlib/public/SDK/MetalKit/MetalKit.swift
new file mode 100644
index 0000000..cbbba22
--- /dev/null
+++ b/stdlib/public/SDK/MetalKit/MetalKit.swift
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// This source file is part of the Swift.org open source project
+//
+// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
+// Licensed under Apache License v2.0 with Runtime Library Exception
+//
+// See https://swift.org/LICENSE.txt for license information
+// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
+//
+//===----------------------------------------------------------------------===//
+
+@_exported import MetalKit // Clang module
+
+@available(swift 4)
+@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)
+extension MTKMesh {
+    public class func newMeshes(asset: MDLAsset, device: MTLDevice) throws -> (modelIOMeshes: [MDLMesh], metalKitMeshes: [MTKMesh]) {
+        var resultTuple : (modelIOMeshes: [MDLMesh], metalKitMeshes: [MTKMesh])
+        resultTuple.modelIOMeshes = [MDLMesh]()
+        resultTuple.metalKitMeshes = try MTKMesh.newMeshes(from: asset, device: device, sourceMeshes: AutoreleasingUnsafeMutablePointer<NSArray?>(&resultTuple.modelIOMeshes))
+        return resultTuple
+    }
+}
+
+@available(swift 4)
+@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)
+public func MTKModelIOVertexDescriptorFromMetalWithError(_ metalDescriptor: MTLVertexDescriptor) throws -> MDLVertexDescriptor {
+    var error: NSError? = nil
+    let result = MTKModelIOVertexDescriptorFromMetalWithError(metalDescriptor, &error)
+    if let error = error {
+        throw error
+    }
+    return result
+}
+
+@available(swift 4)
+@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)
+public func MTKMetalVertexDescriptorFromModelIOWithError(_ modelIODescriptor: MDLVertexDescriptor) throws -> MTLVertexDescriptor? {
+    var error: NSError? = nil
+    let result = MTKMetalVertexDescriptorFromModelIOWithError(modelIODescriptor, &error)
+    if let error = error {
+        throw error
+    }
+    return result
+}
diff --git a/stdlib/public/SDK/QuartzCore/CMakeLists.txt b/stdlib/public/SDK/QuartzCore/CMakeLists.txt
index 0e08583..43b495e 100644
--- a/stdlib/public/SDK/QuartzCore/CMakeLists.txt
+++ b/stdlib/public/SDK/QuartzCore/CMakeLists.txt
@@ -7,9 +7,9 @@
   SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
   LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
   TARGET_SDKS OSX IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR
-  SWIFT_MODULE_DEPENDS_OSX Darwin CoreFoundation CoreGraphics CoreImage Dispatch Foundation IOKit ObjectiveC XPC # auto-updated
-  SWIFT_MODULE_DEPENDS_IOS Darwin CoreFoundation CoreGraphics Dispatch Foundation ObjectiveC # auto-updated
-  SWIFT_MODULE_DEPENDS_TVOS Darwin CoreFoundation CoreGraphics Dispatch Foundation ObjectiveC # auto-updated
+  SWIFT_MODULE_DEPENDS_OSX Darwin CoreFoundation CoreGraphics CoreImage Dispatch Foundation IOKit Metal ObjectiveC XPC # auto-updated
+  SWIFT_MODULE_DEPENDS_IOS Darwin CoreFoundation CoreGraphics Dispatch Foundation Metal ObjectiveC # auto-updated
+  SWIFT_MODULE_DEPENDS_TVOS Darwin CoreFoundation CoreGraphics Dispatch Foundation Metal ObjectiveC # auto-updated
   FRAMEWORK_DEPENDS QuartzCore
 
   DEPLOYMENT_VERSION_OSX ${SWIFTLIB_DEPLOYMENT_VERSION_QUARTZCORE_OSX}
diff --git a/test/stdlib/Metal.swift b/test/stdlib/Metal.swift
new file mode 100644
index 0000000..cbb2b19
--- /dev/null
+++ b/test/stdlib/Metal.swift
@@ -0,0 +1,243 @@
+// RUN: rm -rf %t ; mkdir -p %t
+// RUN: %target-build-swift %s -o %t/a.out4 -swift-version 4 && %target-run %t/a.out4
+// REQUIRES: objc_interop
+// UNSUPPORTED: OS=watchos
+
+import StdlibUnittest
+
+import Metal
+
+var MetalTests = TestSuite("Metal")
+
+if #available(OSX 10.13, iOS 11.0, tvOS 11.0, *) {
+
+  // Call each overlay to ensure nothing explodes
+
+  MetalTests.test("MTLArgumentEncoder") {
+    func apiAvailabilityTest() {
+      /* Setup */
+
+      let device = MTLCreateSystemDefaultDevice()!
+      let buf = device.makeBuffer(
+        length: 64, options: MTLResourceOptions.storageModeShared)!
+      let texDesc = MTLTextureDescriptor()
+      texDesc.usage = MTLTextureUsage.renderTarget
+      let tex = device.makeTexture(descriptor: texDesc)!
+      let smplr = device.makeSamplerState(descriptor: MTLSamplerDescriptor())
+
+      var arguments = [MTLArgumentDescriptor]()
+      arguments.append(MTLArgumentDescriptor())
+      arguments.append(MTLArgumentDescriptor())
+      arguments.append(MTLArgumentDescriptor())
+      arguments[0].dataType = MTLDataType.pointer
+      arguments[0].index = 0
+      arguments[1].dataType = MTLDataType.texture
+      arguments[1].index = 1
+      arguments[2].dataType = MTLDataType.sampler
+      arguments[2].index = 2
+
+      /* Call APIs */
+
+      let argEncoder = device.makeArgumentEncoder(arguments: arguments)!
+      argEncoder.setArgumentBuffer(buf, offset: 0)
+      argEncoder.setBuffers([buf], offsets: [0], range: 0..<1)
+      argEncoder.setTextures([tex], range: 1..<2)
+      argEncoder.setSamplerStates([smplr], range: 2..<3)
+    }
+  }
+
+  MetalTests.test("MTLBlitCommandEncoder") {
+    func apiAvailabilityTest() {
+
+      /* Setup */
+
+      let device = MTLCreateSystemDefaultDevice()!
+      let queue = device.makeCommandQueue()!
+      let cmdBuf = queue.makeCommandBuffer()!
+      let bltCmdEncdr = cmdBuf.makeBlitCommandEncoder()!
+
+      /* Call APIs */
+
+      let buf = device.makeBuffer(length: 4, options: MTLResourceOptions())!
+      bltCmdEncdr.fill(buffer: buf, range: 0..<buf.length, value: 0)
+      bltCmdEncdr.endEncoding()
+    }
+  }
+
+  MetalTests.test("MTLBuffer") {
+    func apiAvailabilityTest() {
+
+      /* Setup */
+
+      let device = MTLCreateSystemDefaultDevice()!
+      #if os(OSX)
+        let options = MTLResourceOptions.storageModeManaged
+      #else
+        let options = MTLResourceOptions.storageModePrivate
+      #endif
+      let buf = device.makeBuffer(length: 4, options: options)!
+
+      /* Call APIs */
+
+      #if os(OSX)
+        buf.didModifyRange(0..<4)
+      #endif
+      buf.addDebugMarker("test marker", range: 0..<4)
+    }
+  }
+
+  MetalTests.test("MTLComputeCommandEncoder") {
+    func apiAvailabilityTest(heapDesc: MTLHeapDescriptor) {
+
+      /* Setup */
+
+      let device = MTLCreateSystemDefaultDevice()!
+      let queue = device.makeCommandQueue()!
+      let cmdBuf = queue.makeCommandBuffer()!
+
+      #if os(OSX)
+        let options = MTLResourceOptions.storageModeManaged
+      #else
+        let options = MTLResourceOptions.storageModePrivate
+      #endif
+      let buf = device.makeBuffer(length: 4, options: options)!
+      let tex = device.makeTexture(descriptor: MTLTextureDescriptor())!
+      heapDesc.size = 4
+      let heap = device.makeHeap(descriptor: heapDesc)!
+      let smplr = device.makeSamplerState(descriptor: MTLSamplerDescriptor())
+
+      /* Call APIs */
+      
+      let encoder = cmdBuf.makeComputeCommandEncoder()!
+      encoder.useResources([buf], usage: MTLResourceUsage.read)
+      encoder.useHeaps([heap])
+      encoder.setBuffers([buf], offsets: [0], range: 0..<1)
+      encoder.setTextures([tex], range: 0..<1)
+      encoder.setSamplerStates([smplr], range: 0..<1)
+      encoder.setSamplerStates(
+        [smplr], lodMinClamps: [0], lodMaxClamps: [0], range: 0..<1)
+      encoder.endEncoding()
+    }
+  }
+
+  MetalTests.test("MTLDevice") {
+    func apiAvailabilityTest() {
+
+      /* Setup */
+
+      let device = MTLCreateSystemDefaultDevice()!
+
+      /* Call APIs */
+
+      var samplePositions : [MTLSamplePosition]
+      if (device.supportsTextureSampleCount(2)) {
+        samplePositions = device.getDefaultSamplePositions(sampleCount: 2)
+      }
+      else if (device.supportsTextureSampleCount(4)) {
+        samplePositions = device.getDefaultSamplePositions(sampleCount: 4)
+      }
+      else {
+        expectUnreachable("device unexpectedly does not support sample count 2 or 4")
+      }
+    }
+  }
+
+  MetalTests.test("MTLFunctionConstantValues") {
+    func apiAvailabilityTest() {
+
+      /* Call APIs */
+
+      let vals = MTLFunctionConstantValues()
+      vals.setConstantValues([0], type: MTLDataType.float, range: 0..<1)
+    }
+  }
+
+  MetalTests.test("MTLRenderCommandEncoder") {
+    func apiAvailabilityTest(heapDesc: MTLHeapDescriptor) {
+
+      /* Setup */
+      let device = MTLCreateSystemDefaultDevice()!
+      let queue = device.makeCommandQueue()!
+      let cmdBuf = queue.makeCommandBuffer()!
+
+      #if os(OSX)
+        let options = MTLResourceOptions.storageModeManaged
+      #else
+        let options = MTLResourceOptions.storageModePrivate
+      #endif
+      let buf = device.makeBuffer(length: 4, options: options)!
+      let texDesc = MTLTextureDescriptor()
+      texDesc.usage = MTLTextureUsage.renderTarget
+      let tex = device.makeTexture(descriptor: texDesc)!
+      heapDesc.size = 4
+      let heap = device.makeHeap(descriptor: heapDesc)!
+      let smplr = device.makeSamplerState(descriptor: MTLSamplerDescriptor())
+      let rpDesc = MTLRenderPassDescriptor()
+      rpDesc.colorAttachments[0].texture = tex
+
+      /* Call APIs */
+
+      let encoder = cmdBuf.makeRenderCommandEncoder(descriptor: rpDesc)!
+      encoder.useResources([buf], usage: MTLResourceUsage.read)
+      encoder.useHeaps([heap])
+      #if os(OSX)
+        encoder.setViewports([MTLViewport()])
+        encoder.setScissorRects([MTLScissorRect(x:0, y:0, width:1, height:1)])
+      #endif
+      encoder.setVertexBuffers([buf], offsets: [0], range: 0..<1)
+      encoder.setVertexTextures([tex], range: 0..<1)
+      encoder.setVertexSamplerStates([smplr], range: 0..<1)
+      encoder.setVertexSamplerStates(
+        [smplr], lodMinClamps: [0], lodMaxClamps: [0], range: 0..<1)
+      encoder.setFragmentBuffers([buf], offsets: [0], range: 0..<1)
+      encoder.setFragmentTextures([tex], range: 0..<1)
+      encoder.setFragmentSamplerStates([smplr], range: 0..<1)
+      encoder.setFragmentSamplerStates(
+        [smplr], lodMinClamps: [0], lodMaxClamps: [0], range: 0..<1)
+     #if os(iOS)
+         encoder.setTileBuffers([buf], offsets: [0], range: 0..<1)
+         encoder.setTileTextures([tex], range: 0..<1)
+         encoder.setTileSamplerStates([smplr], range: 0..<1)
+         encoder.setTileSamplerStates(
+           [smplr], lodMinClamps: [0], lodMaxClamps: [0], range: 0..<1)
+     #endif
+      encoder.endEncoding()
+    }
+  }
+
+  MetalTests.test("MTLRenderPassDescriptor") {
+    func apiAvailabilityTest() {
+
+      /* Setup */
+
+      let rpDesc = MTLRenderPassDescriptor()
+
+      /* Call APIs */
+
+      rpDesc.setSamplePositions(
+        [MTLSamplePosition(x:0.25,y:0.75), MTLSamplePosition(x:0.75, y:0.25)])
+      _ = rpDesc.getSamplePositions()
+    }
+  }
+
+  MetalTests.test("MTLTexture") {
+    func apiAvailabilityTest() {
+      /* Setup */
+
+      let device = MTLCreateSystemDefaultDevice()!
+      let texDesc = MTLTextureDescriptor()
+      texDesc.usage = MTLTextureUsage.renderTarget
+      let tex = device.makeTexture(descriptor: texDesc)!
+
+      /* Call APIs */
+
+      let _ = tex.makeTextureView(
+        pixelFormat: texDesc.pixelFormat,
+        textureType: texDesc.textureType,
+        levels: 0..<1,
+        slices: 0..<1)
+    }
+  }
+}
+
+runAllTests()
diff --git a/test/stdlib/MetalKit.swift b/test/stdlib/MetalKit.swift
new file mode 100644
index 0000000..223121d
--- /dev/null
+++ b/test/stdlib/MetalKit.swift
@@ -0,0 +1,44 @@
+// RUN: rm -rf %t ; mkdir -p %t
+// RUN: %target-build-swift %s -o %t/a.out4 -swift-version 4 && %target-run %t/a.out4
+// REQUIRES: objc_interop
+// UNSUPPORTED: OS=watchos
+
+import StdlibUnittest
+
+import Metal
+import MetalKit
+
+var MetalKitTests = TestSuite("MetalKit")
+
+// Call each overlay to ensure nothing explodes
+
+if #available(macOS 10.12, iOS 10.0, tvOS 10.0, *) {
+  MetalKitTests.test("Globals") {
+
+    do {
+      let _ = try MTKModelIOVertexDescriptorFromMetalWithError(MTLVertexDescriptor())
+    } catch _ {
+      expectUnreachable("MTKModelIOVertexDescriptorFromMetalWithError has thrown an unexpected error")
+    }
+
+    do {
+      let _ = try MTKMetalVertexDescriptorFromModelIOWithError(MDLVertexDescriptor())
+    } catch _ {
+      expectUnreachable("MTKMetalVertexDescriptorFromModelIOWithError has thrown an unexpected error")
+    }
+  }
+}
+
+if #available(macOS 10.11, iOS 9.0, tvOS 9.0, *) {
+  MetalKitTests.test("MTKMesh") {
+    func apiAvailabilityTest(device: MTLDevice) {
+      do {
+        let _ = try MTKMesh.newMeshes(asset: MDLAsset(), device: device)
+      } catch _ {
+        expectUnreachable("MTKMesh.newMeshes has thrown an unexpected error")
+      }
+    }
+  }
+}
+
+runAllTests()