How to enable InMemoryModule cache?

In the constructor of CompilerInstance, I found there is an argument called SharedModuleCache with type: InMemoryModuleCache. And the comment of InMemoryModuleCache tells that it is a cache for modules for use across a compilation, which looks good. However, in every construction of CompilerInstance, I can’t find the use of SharedModuleCache . It looks like it is unused. Or do I take an oversight?

Thanks

Not sure I understood the question. This line shows if you don’t pass a InMemoryModuleCache to the ctor, the default value(nullptr) will be used, and then it’s constructed in the init list.

Yeah, I mean if all the construction of CompilerInstance would make the value of SharedModuleCache to nullptr, it is meaningless. My key point is that I can’t find callsites of constructor of CompilerInstance whose value of SharedModuleCache is not nullptr.

I still don’t understand. Why is it meaningless? CompilerInstance serves as a helper class for constructing stuff that a compiler needs. The default nullptr is just a short-cut way to init ModuleCache as new InMemoryModuleCache.

If what you want is to find a use-case that a non-nullptr ModuleCache is being used, this line is an example.

I still don’t understand. Why is it meaningless? CompilerInstance serves as a helper class for constructing stuff that a compiler needs. The default nullptr is just a short-cut way to init ModuleCache as new InMemoryModuleCache .

If all the construction of CompilerInstance wouldn’t use SharedModuleCache, in other words, SharedModuleCache shouldn’t be an argument since the value is nullptr all the way.

If what you want is to find a use-case that a non-nullptr ModuleCache is being used, this line is an example.

Yeah, I want to find a use-case. And the you mentioned is not the example. Since it is used in CompilerInstance, and the ModuleCache used here is still created by CompilerInstance itself. I want to find a use-case which would create CompilerInstace with an existing InMemoryModuleCache.

https://clang.llvm.org/doxygen/CompilerInstance_8cpp_source.html#l01205

BTW, the first InMemoryModuleCache always needs to be created somewhere. I don’t think there is any problem to create it in the ctor of CompilerInstance.

Yeah, I found this. And I noticed that the we get the ModuleCache from another compiler instance.

Maybe I got the problem we had. Let me give an example to make things clear:

clang++ -std=c++20 --precompile A.cppm -o A.pcm
clang++ -std=c++20 B.cpp -fprebuilt-module-path=. -c

Then according to the comment, I thought when we execute the second line, we could read the module from the memory instead of the disk.

The comment of InMemoryModuleCache is here: llvm-project/InMemoryModuleCache.h at d480f968ad8b56d3ee4a6b6df5532d485b0ad01e · llvm/llvm-project · GitHub

1 Like

Makes sense to me now. These two invocations are running through isolated frontend actions and you’d like to check whether/how the ModuleCache is shared.