Serdar Yegulalp
Senior Writer

LLVM coroutines come to C++, await Swift and Rust

news
Mar 14, 20172 mins

The open source compiler framework can now produce optimized code for languages that have this threadlike feature, but Swift and Rust are still lagging

wait sign
Credit: Pete Smithies

LLVM, the open source compiler framework that’s used as part of the toolchain for languages like Rust and Swift, was bumped up to version 4.0 this week.

The most eye-catching addition is support for coroutines, a construction found in many modern programming languages that can be used as a less cumbersome (albeit slightly less powerful) alternative to threading.

Previously, a language with coroutines that compiled in LLVM would have had to implement coroutines by hand. LLVM 4.0 adds support for representing coroutines directly in the compiler via new instructions in its intermediate representation (IR).

Right now, coroutine support is experimental and has to be enabled explicitly in LLVM to work. It also must be enabled from the language side; recompiling existing code in LLVM 4.0 won’t automatically add coroutine instructions.

To that end, the next step will be for LLVM-backed languages to leverage coroutine support in the compiler. Rust, one of the most prominent projects using LLVM today, is a likely candidate.

Ironically, Rust doesn’t have coroutines as a native language feature—not for lack of LLVM support, but due to ongoing discussion of the best way for Rust to officially support such a feature. A few existing implementations might be used as a base to build such items, but anything official is still a ways off.

With Swift, there’s also discussion about adding coroutines and other concurrency features, although that’s slated to land in the next major version of the language. Chris Lattner, Swift’s original chief developer, has stated any such functionality would be considered “alongside whatever async/concurrency approach we tackle (likely in Swift 4).”

For those curious about how LLVM accomplishes this internally, Microsoft Visual C++ Team member Gor Nishanov provided details in a deck of slides from a presentation the 2016 LLVM Developers’ Meeting. The notes are highly technical, but they make it clear that coroutines have been integrated into LLVM tightly, with the compiler aware of coroutines throughout each stage of the optimization process.

“Though coroutine support in LLVM is motivated primarily by the desire to support C++ Coroutines,” read the presentation summary, “the LLVM coroutine representation is language neutral and can be used to support coroutines in other languages as well.”

Serdar Yegulalp

Serdar Yegulalp is a senior writer at InfoWorld. A veteran technology journalist, Serdar has been writing about computers, operating systems, databases, programming, and other information technology topics for 30 years. Before joining InfoWorld in 2013, Serdar wrote for Windows Magazine, InformationWeek, Byte, and a slew of other publications. At InfoWorld, Serdar has covered software development, devops, containerization, machine learning, and artificial intelligence, winning several B2B journalism awards including a 2024 Neal Award and a 2025 Azbee Award for best instructional content and best how-to article, respectively. He currently focuses on software development tools and technologies and major programming languages including Python, Rust, Go, Zig, and Wasm. Tune into his weekly Dev with Serdar videos for programming tips and techniques and close looks at programming libraries and tools.

More from this author