gnzlbg
gnzlbg

Reputation: 7415

Is it possible to compile a single C++ translation unit in parallel?

Is it possible to compile a single C++ translation unit in parallel?

If so, how? (specifically how to do it with e.g. clang or gcc)

Otherwise, why not?

Upvotes: 1

Views: 1126

Answers (2)

MSalters
MSalters

Reputation: 179907

Probably in theory, but pointless in practice.

The preprocessor thread can emit a sequence of tokens to be compiled, and the actual compilation thread can pick those up as they are produced. Similarly, the linker thread can be fed compiled functions as they are produced, because it can start before the last function is known.

Peephole optimization can also be done in parallel, pretty much by definition. But that needs to alternate with other optimization steps such as inlining, which is a bit harder to do in parallel.

But as the comments indicated, any real program has more translation units than you have cores. You'd waste time by having to synchronize two threads for a single TU.

Linking is of course an entirely different matter.

Upvotes: 1

Zan Lynx
Zan Lynx

Reputation: 54325

I very much doubt that it is possible to compile in parallel.

The C and C++ languages depend on the order of evaluation. A #define higher in the file might change the meaning of everything that follows it. In C++ an operator might call a function or do another operation, depending on the existence of an operator override function. In fact, the existence or non-existence of a symbol name might affect if it is interpreted as a variable or a type.

The simple parts of parsing that might be done in parallel without reference to symbol tables are so easy to do that threading them hardly makes sense. And the hard parts are inherently serialized.

A language might be designed to allow parallel compilation in a single unit, but it wouldn't be C.

Upvotes: 2

Related Questions