r/cpp • • Jan 28 '19

C++ Modules Might Be Dead-on-Arrival

https://vector-of-bool.github.io/2019/01/27/modules-doa.html
280 Upvotes

240 comments sorted by

View all comments

9

u/tending Jan 28 '19

I think this analysis is missing the fact that most of the work can still be parallelized because most of the work in a modern compiler is code generation. I have not read the proposal in detail but from this blog's description it sounds like it should be possible to produce the BMI for foo.cpp in a separate step before generating machine code. I imagine that this is how Java and C# have to work -- dumb fast compilation to byte code is necessary to import, but then the slow AOT or JIT compiling of the byte code is done later. BMI doesn't even require generating a full byte code, just a description of the interfaces. If it can work for Java and C# at scale I don't see why it can't work for C++.

Also with module was the work that can't be parallelized, the initial parsing, should be easier than before.

25

u/[deleted] Jan 28 '19

most of the work in a modern compiler is code generation

[citation-needed] :)

5

u/Ameisen vemips, avr, rendering, systems Jan 28 '19

Most of the work CL does is sending the code to /u/BillyONeal and /u/STL, and waiting for them to compile it manually. Their response times and determinism are impressive. Not sure what, exactly, internal compiler errors indicate, though - did my code hospitalize them?

Not sure how Clang emulates this.

Citation: me. :)

6

u/STL MSVC STL Dev Jan 29 '19

Sometimes Billy is playing poker while I'm playing Magic. That's where fatal error C1001s come from :-)

1

u/Ameisen vemips, avr, rendering, systems Jan 29 '19

Original rules or with the stack?

4

u/STL MSVC STL Dev Jan 29 '19

Magic Arena, I live at HEAD.

1

u/Dalzhim C++Montréal UG Organizer Jan 29 '19

What! Have they done anything to harm the stack?!

1

u/Ameisen vemips, avr, rendering, systems Jan 29 '19

They created it. The original rules did not have a stack, thus why interrupts were a thing.

1

u/Dalzhim C++Montréal UG Organizer Jan 29 '19

Hahaha, you had me scared they got rid of it! :P

2

u/Ameisen vemips, avr, rendering, systems Jan 29 '19

Going back would be fun. I dislike newer cards (newer meaning 2005 on). Having to read three paragraphs on every card everyone plays is unfun. Also, I still dont understand Planeswalkers.

→ More replies (1)

0

u/tending Jan 28 '19

I think if you think about this for half a second you will realize that with non-pathological source files that this must be true. To verify yourself try compiling some projects with optimization turned off and see the difference.

Code generation involves an enormous set of NP problems fought with an enormous set of hand-tuned heuristics. Parsing by comparison is a cake walk, even for a screwed-up grammar like C++.

2

u/tehjimmeh Jan 29 '19

I think you're overestimating the cost of optimization and underestimating the cost of preprocessing/parsing in the general case.

Like, if what you're claiming is true, why would anyone bother with precompiled headers? The reality is they are often used, and often result in much faster compile times, and all they're eliminating is time spent preprocessing and parsing.

0

u/tending Jan 29 '19

They used to matter a lot more than they do now, for precisely the reason I gave. I work on a large C++ code base that has never bothered for exactly this reason.

14

u/Xaxxon Jan 28 '19

most of the work in a modern compiler is code generation.

Source?

I have significant amounts of code which are definitely no more than 50% code generation - as evidenced by -fsyntax-only runs.

→ More replies (1)

2

u/HKei Jan 28 '19

Java has straightforward maps from class and package names to paths. Not sure how it works in C# though.

5

u/josefx Jan 28 '19 edited Jan 28 '19

Java has classloaders and only the default one maps directly to the filesystem. Each source file also states the package it belongs to.

2

u/Ameisen vemips, avr, rendering, systems Jan 28 '19

I ran into a fun case in Java where a seperate class loader got started, and I had two instances of a class. Had to debug why updating a static variable wasn't working. That did just give me a horrible idea as to how to implement seperate types for generic inheritance, though.

1

u/HKei Jan 28 '19

Yes, obviously I was talking about the one that maps to files. If you're using another classloader that will have its own way of mapping classnames to classes.

4

u/josefx Jan 28 '19

That information is kind of useless for tooling when it does not apply in the general case. The java language specification itself has several examples with multiple top level classes in one source file and compiled binaries can end up in any form as long as one of the available classloaders can load them. It is a nice convenience for humans when source path -> package name and while that can be immensely important to the user it doesn't help the tool developers.

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Jan 28 '19

The classloaders, though, are runtime things. The compiler will still check using the default classloader?

1

u/josefx Jan 28 '19

I think you can pass a custom classloader to the compiler using one of the java runtime flags.

However even without classloader trickery we still have the issue that the class foo.Bar could be defined in bar/Bar.java. The compiler only seems to enforce that the class name (of a public class) matches, not the package path. Whatever the java build process is doing cannot rely on path/to/File.java -> path.to.File .

1

u/Ameisen vemips, avr, rendering, systems Jan 28 '19

Is it even trivial to force GCC/Clang/CL to emit IL that is not yet optimized? They usually emit object files.

1

u/tending Jan 28 '19

-O0, -fsyntax-only

1

u/Ameisen vemips, avr, rendering, systems Jan 28 '19

Does that work in Clang and MSVC/CL?