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

Ngoc Huynh

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

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.”

Share the news now

Source : http://www.infoworld.com