Hi all,
Remember the 2019 discussion about "Swift as Syntactic Sugar for MLIR" Swift as syntactic sugar for MLIR ? I've been exploring this vision using modern Swift features that didn't exist back then.
Background: From Passenger to Driver
Some of you might know my previous work on TaylorTorch, where I added automatic differentiation to LibTorch for Swift. While that project works, Swift is ultimately just a passenger - we could call LibTorch's APIs, even add differentiation protocols, but how tensors were optimized, compiled, or executed was entirely at LibTorch's discretion. Swift had no say in the compilation pipeline.
What I Built
SwiftIR is a Swift framework for building, manipulating, and executing MLIR code with full type safety. Instead of wrapping someone else's compiler, Swift now owns the compilation pipeline - from IR generation through optimization to hardware execution.
Key achievements:
-
Swift controls the entire pipeline - Not just wrapping, but owning compilation
-
OpenXLA/PJRT integration working - Same battle-tested runtime powering JAX/TensorFlow
-
20+ working examples (CNNs, ResNets) executing on CPU via XLA compilation
-
SPIR-V generation in progress - MLIR opens doors to Vulkan/Metal compute shaders
-
Type-safe MLIR construction - catch errors at compile-time, not runtime
-
Multiple abstraction levels: string-based MLIR, type-safe DSL, and macro vision
Quick Example
```swift
import SwiftIR
let tensorType = TensorType(shape: [4])
let module = StableHLOModule(name: "add_module") {
StableHLOFunction(
name: "add",
parameters: [
Parameter(name: "a", type: tensorType),
Parameter(name: "b", type: tensorType)
],
returnType: tensorType
) {
Add("a", "b", type: tensorType)
}
}
let mlir = module.build() // Generate MLIR from DSL
let client = try PJRTClient.createCPU()
let result = try client.compile(mlir).execute(
inputs: [Tensor([1,2,3,4]), Tensor([5,6,7,8])]
)
print(result) // [6, 8, 10, 12]
```
Unlike TaylorTorch where we worked within LibTorch's constraints, SwiftIR gives Swift developers direct control over the entire stack - we decide the optimizations, the lowering strategies, and the execution model.
Looking for Feedback
This is a research project exploring what's possible when Swift owns its ML stack. Coming from TaylorTorch, where Swift was limited by C++ design decisions, I'm excited to explore what we can achieve with full control. I'd love to hear:
-
Is OpenXLA integration valuable for Swift developers?
-
Interest in SPIR-V/GPU compute beyond ML?
-
What limitations in wrapped C++ frameworks have you hit?
-
Anyone interested in collaborating on the macro system or GPU backends?
-
Thoughts on the API design and the "levels of abstraction" approach?
-
Should Swift aim to own more of its ML/compute stack?
GitHub: https://github.com/pedronahum/SwiftIR 20+ working examples included (all using OpenXLA's runtime).
Best,
Pedro N.