-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Macros] Add support for wasm macros #73031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 69 commits
e5c191d
e5342e7
d5d1cb7
f776c28
43fc233
961fd54
a771631
3f8ef81
23426e0
9fe505f
89158e2
f084e25
5ef86da
02f4324
bddde66
ca22e2d
6a9bbe7
e598064
7d1510e
37b8a61
da74f38
7a18da2
1076b16
805c659
4527842
f9dbbb1
6511672
dd481fe
4ce4749
482871c
771acb0
2460e98
150f3d2
34ed5b0
94d525e
a99f6b3
c5dc6bd
6375faf
826a80f
f053859
6c0b40c
d72e165
564f844
a670f32
aaaaae3
7b16cbd
7d9d015
ea81405
b472f4f
3baf627
eee48e0
4fca102
d465d36
c4efcee
b3f53c2
b629484
af2b87e
bbb916c
77c273f
b56e28a
94f0ad7
14b5b79
af68825
92aef4e
9b1e4ee
f93df4d
95f254c
ca27b45
caa1a8c
2ee417f
fa52ecd
f2f2ea2
c0a75e1
011a0a5
d2d3b94
e2a455a
b307eb4
782956a
8a88dc5
fcb838d
469d84a
cc41cf1
7499295
e89fd6a
2b47724
4285cf1
56b6cae
96363e1
913041b
6cd389e
c3f4276
4341058
393933a
9c5e7fc
5890c3e
0f39ec8
48dd97f
c3f41c7
ee82b85
1dcdd7b
6e56934
dd13597
5770553
94f870c
ed54400
d568d67
75bd7ca
c1e928d
0d2c140
7683e83
e0427b2
be04533
1852df9
e49c980
052058a
f860528
28a9efa
19eef9a
db325e8
54eb483
224ac09
dc8cd3c
6817517
f56ec6a
1626ad8
4204eef
1cf1e20
6277098
0da4f22
3991b60
636fa0e
f915c05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Package.resolved |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
if (SWIFT_BUILD_SWIFT_SYNTAX) | ||
# override the remote SwiftSystem and ArgParser dependencies in WasmKit | ||
FetchContent_Declare(SwiftSystem SOURCE_DIR "${PROJECT_SOURCE_DIR}/../swift-system") | ||
FetchContent_Declare(ArgumentParser SOURCE_DIR "${PROJECT_SOURCE_DIR}/../swift-argument-parser") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might end up having to do something more bespoke here to get things building on all of the platforms, but let's try this to start! |
||
|
||
FetchContent_Declare(WasmKit SOURCE_DIR "${PROJECT_SOURCE_DIR}/../wasmkit") | ||
FetchContent_MakeAvailable(WasmKit) | ||
|
||
add_pure_swift_host_tool(swift-plugin-server | ||
Sources/swift-plugin-server/swift-plugin-server.swift | ||
Sources/swift-plugin-server/WasmEngine.swift | ||
Sources/swift-plugin-server/WasmKitEngine.swift | ||
Sources/swift-plugin-server/WasmMessageHandler.swift | ||
SWIFT_COMPONENT | ||
compiler | ||
SWIFT_DEPENDENCIES | ||
SwiftCompilerPluginMessageHandling | ||
SwiftLibraryPluginProvider | ||
WasmKitWASI | ||
) | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import WASI | ||
import WasmTypes | ||
import SystemPackage | ||
|
||
typealias WasmFunction = ([UInt32]) throws -> [UInt32] | ||
|
||
protocol WasmEngine { | ||
init(path: FilePath, imports: WASIBridgeToHost) throws | ||
|
||
func function(named name: String) throws -> WasmFunction? | ||
} | ||
|
||
typealias DefaultWasmPlugin = WasmEnginePlugin<DefaultWasmEngine> | ||
|
||
// a WasmPlugin implementation that delegates to a WasmEngine | ||
struct WasmEnginePlugin<Engine: WasmEngine>: WasmPlugin { | ||
private let hostToPlugin: FileDescriptor | ||
private let pluginToHost: FileDescriptor | ||
private let pumpFunction: WasmFunction | ||
let engine: Engine | ||
|
||
init(path: FilePath) throws { | ||
let hostToPluginPipes = try FileDescriptor.pipe() | ||
let pluginToHostPipes = try FileDescriptor.pipe() | ||
self.hostToPlugin = hostToPluginPipes.writeEnd | ||
self.pluginToHost = pluginToHostPipes.readEnd | ||
|
||
let bridge = try WASIBridgeToHost( | ||
stdin: hostToPluginPipes.readEnd, | ||
stdout: pluginToHostPipes.writeEnd, | ||
stderr: .standardError | ||
) | ||
engine = try Engine(path: path, imports: bridge) | ||
|
||
let exportName = "swift_wasm_macro_v1_pump" | ||
guard let pump = try engine.function(named: exportName) else { | ||
throw WasmEngineError(message: "Wasm plugin has an unknown ABI (could not find '\(exportName)')") | ||
} | ||
self.pumpFunction = pump | ||
|
||
guard let start = try engine.function(named: "_start") else { | ||
throw WasmEngineError(message: "Wasm plugin does not have a '_start' entrypoint") | ||
} | ||
_ = try start([]) | ||
} | ||
|
||
func handleMessage(_ json: [UInt8]) throws -> [UInt8] { | ||
try withUnsafeBytes(of: UInt64(json.count).littleEndian) { | ||
_ = try hostToPlugin.writeAll($0) | ||
} | ||
try hostToPlugin.writeAll(json) | ||
|
||
_ = try pumpFunction([]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why we're passing in and returning unused There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is vestigial from when we were passing and returning JSON via function arguments instead of stdio; will remove the arguments. |
||
|
||
let lengthRaw = try withUnsafeTemporaryAllocation(of: UInt8.self, capacity: 8) { buffer in | ||
let lengthCount = try pluginToHost.read(into: UnsafeMutableRawBufferPointer(buffer)) | ||
guard lengthCount == 8 else { | ||
throw WasmEngineError(message: "Wasm plugin sent invalid response") | ||
} | ||
return buffer.withMemoryRebound(to: UInt64.self, \.baseAddress!.pointee) | ||
} | ||
let length = Int(UInt64(littleEndian: lengthRaw)) | ||
return try [UInt8](unsafeUninitializedCapacity: length) { buffer, size in | ||
let received = try pluginToHost.read(into: UnsafeMutableRawBufferPointer(buffer)) | ||
guard received == length else { | ||
throw WasmEngineError(message: "Wasm plugin sent truncated response") | ||
} | ||
size = received | ||
} | ||
} | ||
} | ||
|
||
struct WasmEngineError: Error, CustomStringConvertible { | ||
let description: String | ||
|
||
init(message: String) { | ||
self.description = message | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not familiar with this part of the codebase — should I be recording
val.ServerPath
as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think we need to record
.wasm
files under the search path