Did/does Swift require changes to LLVM-assembly?

Background: I am learning about compilers by writing one in swift.
The easiest approach is a VM. Another is emitting LLVM-assembly.

Thoughts: LLVM is written in c++. All tutorials use a bunch of tools or are geared towards c++. I want to stick with swift and write everything myself to better understand the fundamentals. Though converting assembly to architecture specific instructions is a step too far. Life is too short for that. :frowning:

LLVM-assembly looks like it could be written by hand. So it’s something I could target with my toy compiler. Right?

Question: did or still does Swift influence LLVM-assembly? Or is LLVM-assembly, the language, something that is finished? Take actors for example. That will require suspension points, threads, concurrency and so on. Would that require changes to the assembly language or the LLVM backend?

I guess what I am really asking is if I know LLVM assembly, will I ever have to care about the steps afterwards to get an app with all bells and whistles compiled and working?

PS. I am sure the backend is fascinating in itself. No offence intended. Just getting to fully understand the frontend will take me years due to time constraints.

Swift uses a two stage approach: it generates its own intermediate language (called SIL), which it can do optimizations on that rely on the specifics of Swift (e.g. LLVM has no idea what reference counting is, so the ARC optimizer operates on SIL, which does know about reference counting). Then it converts the SIL to LLVM IR and hands that off to LLVM to deal with.

The main thing I can think of that the concurrency work might depend on is LLVM coroutines (I don't know if we're using them, but it would be a logical possibility), but that work was in progress independent of Swift: https://llvm.org/docs/Coroutines.html

1 Like

You should be able to call the LLVM C API from Swift. If you search for that, you'll find some documentation and tutorials. Any particular implementation of Swift will depend on some version of LLVM, but that's a small concern relative to the task.

1 Like

Writing a compiler in Swift using LLVM doesn’t really require you to know anything about how Swift itself generates LLVM IR.

If you’re interested in using LLVM from Swift, there’s a package you can use that wraps the C bindings in a Swift API: https://github.com/llvm-swift/LLVMSwift

1 Like

@harlanhaskins also wrote a programming language using LLVMSwift, I used this as an example when I learn compiler: https://github.com/trill-lang/trill

Thanks everyone for the replies and the links.

Terms of Service

Privacy Policy

Cookie Policy