Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are clang's compiled module pcm files for?

I'm looking at the driver test cases for clang modules: https://github.com/llvm-mirror/clang/blob/master/test/Driver/modules.cpp

It includes steps to produce .pcm.o files. I'm wondering what they are for.

Given a c++20 module

// a-m.cc
module;
#include <iostream>
export module a;
export void do_a() { std::cout << "A\n"; }

You can compile it using

clang++ -std=c++20 -x c++-module --precompile a-m.cc -o a.pcm

which produces the precompiled module file a.pcm.

But there are also steps to compile .pcm files to .o files.

From the driver tests:

clang++ -std=c++20 a.pcm -S -o a.pcm.o

How are the .pcm.o files meant to be used?

If I write a main program

// main.cc
import a;

int main() {
    do_a();
    return 0;
}

Compile with

clang++ -std=c++20 -c main.cc -fmodule-file=a=a.pcm

And then try to link with the .pcm.o, I get

clang++ main.o a.pcm.o
/usr/bin/ld:a.pcm.o: file format not recognized; treating as linker script
/usr/bin/ld:a.pcm.o:2: syntax error
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)

Note: you can compile a-m.cc

clang++ std=c++20 -c a-m.cc -o a.o

and link with a.o, but what are the .pcm.o files for? and can they be used to avoid compiling a-m.cc again after you've precompiled it?

like image 649
Ryan Burn Avatar asked Nov 07 '22 02:11

Ryan Burn


1 Answers

The pcm-files contains "half baked" information about a module. The file extension does not have to be pcm and i think Visual Studio for example uses .ifc as extension. (And a difference with Visual Studio is that it outputs the ifc-file and compiles the object file (.obj) at the same time, while clang does this in two separate steps)

The pcm file is storing the information about the module file in a format so that the compiler can easily import it or compile it to other object files.

The command from your link surprises me, I think that -S in the example is compiling to assembly, so i am surprised that it is listed in that clang repo that you linked to.

clang++ -std=c++20 a.pcm -S -o a.pcm.o # To mee this looks wrong (assembly output)

Try changing it to use -c instead

clang++ -std=c++20 a.pcm -c -o a.pcm.o # This is how you compile pcm-files to object files
like image 143
Lasersköld Avatar answered Nov 15 '22 05:11

Lasersköld