Skip to content

Commit ffd24af

Browse files
[clang][gmodules] Use CASID to encode splitDwarfFilename
Teach clang to encode CASID as splitDwarfFilename for gmodule when clang caching is enabled. This allows the outputs from compiler do not contain paths to the clang module files, thus allows distributed caching without the need of a unified clang module cache directory path.
1 parent db0d747 commit ffd24af

35 files changed

+685
-147
lines changed

clang/include/clang/Basic/ASTSourceDescriptor.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,23 @@ class ASTSourceDescriptor {
3030
StringRef Path;
3131
StringRef ASTFile;
3232
ASTFileSignature Signature;
33+
StringRef CASID;
3334
Module *ClangModule = nullptr;
3435

3536
public:
3637
ASTSourceDescriptor() = default;
3738
ASTSourceDescriptor(StringRef Name, StringRef Path, StringRef ASTFile,
38-
ASTFileSignature Signature)
39+
ASTFileSignature Signature, StringRef CASID)
3940
: PCHModuleName(std::move(Name)), Path(std::move(Path)),
40-
ASTFile(std::move(ASTFile)), Signature(Signature) {}
41+
ASTFile(std::move(ASTFile)), Signature(Signature), CASID(CASID) {}
4142
ASTSourceDescriptor(Module &M);
4243

4344
std::string getModuleName() const;
4445
StringRef getPath() const { return Path; }
4546
StringRef getASTFile() const { return ASTFile; }
4647
ASTFileSignature getSignature() const { return Signature; }
4748
Module *getModuleOrNull() const { return ClangModule; }
49+
StringRef getCASID() const { return CASID; }
4850
};
4951

5052
} // namespace clang

clang/include/clang/Basic/DiagnosticCASKinds.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ def err_cas_depscan_daemon_connection: Error<
2323
def err_cas_depscan_failed: Error<
2424
"CAS-based dependency scan failed: %0">, DefaultFatal;
2525
def err_cas_store: Error<"failed to store to CAS: %0">, DefaultFatal;
26-
def err_cas_unloadable_module : Error<
27-
"module file '%0' not found: unloadable module cache key %1: %2">, DefaultFatal;
26+
def err_cas_unloadable_module
27+
: Error<"module file '%0' not found: unloadable %select{casid|module cache "
28+
"key}1 %2: %3">,
29+
DefaultFatal;
2830
def err_cas_missing_module : Error<
2931
"module file '%0' not found: missing module cache key %1: %2">, DefaultFatal;
3032
def err_cas_missing_root_id : Error<

clang/include/clang/Basic/Module.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ class alignas(8) Module {
274274
/// The \c ActionCache key for this module, if any.
275275
std::optional<std::string> ModuleCacheKey;
276276

277+
/// The \c CASID for the loaded module, if any.
278+
std::optional<std::string> CASID;
279+
277280
/// The top-level headers associated with this module.
278281
llvm::SmallSetVector<FileEntryRef, 2> TopHeaders;
279282

@@ -773,6 +776,15 @@ class alignas(8) Module {
773776
getTopLevelModule()->ModuleCacheKey = std::move(Key);
774777
}
775778

779+
std::optional<StringRef> getCASID() const {
780+
return getTopLevelModule()->CASID;
781+
}
782+
783+
void setCASID(std::string ID) {
784+
assert(!getCASID() || *getCASID() == ID);
785+
getTopLevelModule()->CASID = std::move(ID);
786+
}
787+
776788
/// Retrieve the umbrella directory as written.
777789
std::optional<DirectoryName> getUmbrellaDirAsWritten() const {
778790
if (const auto *Dir = std::get_if<DirectoryEntryRef>(&Umbrella))

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,8 +1031,8 @@ class CompilerInstance : public ModuleLoader {
10311031
/// "-fmodule-file-cache-key", or an imported pcm file. Used in diagnostics.
10321032
///
10331033
/// \returns true on failure.
1034-
bool addCachedModuleFile(StringRef Path, StringRef CacheKey,
1035-
StringRef Provider);
1034+
bool addCachedModuleFile(StringRef Path, StringRef CASID, StringRef Provider,
1035+
bool IsKey);
10361036

10371037
ModuleCache &getModuleCache() const { return *ModCache; }
10381038

clang/include/clang/Serialization/ASTReader.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ class ASTReaderListener {
287287
return false;
288288
}
289289

290+
/// Called for each module CASID.
291+
///
292+
/// \returns true to indicate the key cannot be loaded.
293+
virtual bool readModuleCASID(StringRef ModuleName, StringRef Filename,
294+
StringRef CASID) {
295+
return false;
296+
}
297+
290298
/// Indicates that a particular module file extension has been read.
291299
virtual void readModuleFileExtension(
292300
const ModuleFileExtensionMetadata &Metadata) {}
@@ -343,6 +351,8 @@ class ChainedASTReaderListener : public ASTReaderListener {
343351
bool readIncludeTreeID(StringRef ID, bool Complain) override;
344352
bool readModuleCacheKey(StringRef ModuleName, StringRef Filename,
345353
StringRef CacheKey) override;
354+
bool readModuleCASID(StringRef ModuleName, StringRef Filename,
355+
StringRef CASID) override;
346356
void readModuleFileExtension(
347357
const ModuleFileExtensionMetadata &Metadata) override;
348358
};

clang/include/clang/Serialization/InMemoryModuleCache.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class InMemoryModuleCache : public llvm::RefCountedBase<InMemoryModuleCache> {
3030
struct PCM {
3131
std::unique_ptr<llvm::MemoryBuffer> Buffer;
3232

33+
std::string CASID;
34+
3335
/// Track whether this PCM is known to be good (either built or
3436
/// successfully imported by a CompilerInstance/ASTReader using this
3537
/// cache).
@@ -38,6 +40,9 @@ class InMemoryModuleCache : public llvm::RefCountedBase<InMemoryModuleCache> {
3840
PCM() = default;
3941
PCM(std::unique_ptr<llvm::MemoryBuffer> Buffer)
4042
: Buffer(std::move(Buffer)) {}
43+
44+
PCM(std::unique_ptr<llvm::MemoryBuffer> Buffer, llvm::StringRef CASID)
45+
: Buffer(std::move(Buffer)), CASID(CASID.str()) {}
4146
};
4247

4348
/// Cache of buffers.
@@ -64,7 +69,8 @@ class InMemoryModuleCache : public llvm::RefCountedBase<InMemoryModuleCache> {
6469
/// \post state is Tentative
6570
/// \return a reference to the buffer as a convenience.
6671
llvm::MemoryBuffer &addPCM(llvm::StringRef Filename,
67-
std::unique_ptr<llvm::MemoryBuffer> Buffer);
72+
std::unique_ptr<llvm::MemoryBuffer> Buffer,
73+
llvm::StringRef CASID = "");
6874

6975
/// Store a just-built PCM under the Filename.
7076
///
@@ -90,6 +96,9 @@ class InMemoryModuleCache : public llvm::RefCountedBase<InMemoryModuleCache> {
9096
/// Get a pointer to the pCM if it exists; else nullptr.
9197
llvm::MemoryBuffer *lookupPCM(llvm::StringRef Filename) const;
9298

99+
/// Get the PCM if it exits; else nullptr.
100+
const PCM *lookup(llvm::StringRef Filename) const;
101+
93102
/// Check whether the PCM is final and has been shown to work.
94103
///
95104
/// \return true iff state is Final.

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ class ModuleFile {
147147
/// The \c ActionCache key for this module, or empty.
148148
std::string ModuleCacheKey;
149149

150+
/// The \c CASID for the module, or empty.
151+
std::string CASID;
152+
150153
/// The CAS filesystem root ID for implicit modules built with the dependency
151154
/// scanner, or empty.
152155
std::string CASFileSystemRootID;

clang/lib/Basic/ASTSourceDescriptor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ ASTSourceDescriptor::ASTSourceDescriptor(Module &M)
2121
Path = M.Directory->getName();
2222
if (auto File = M.getASTFile())
2323
ASTFile = File->getName();
24+
if (M.getCASID())
25+
CASID = *M.getCASID();
2426
}
2527

2628
std::string ASTSourceDescriptor::getModuleName() const {

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3401,6 +3401,11 @@ llvm::DIModule *CGDebugInfo::getOrCreateModuleRef(ASTSourceDescriptor Mod,
34013401
PCM = Mod.getPath();
34023402
}
34033403
llvm::sys::path::append(PCM, Mod.getASTFile());
3404+
3405+
// If module file has CASID, use CASID as PCM path.
3406+
if (!Mod.getCASID().empty())
3407+
PCM = Mod.getCASID();
3408+
34043409
DIB.createCompileUnit(
34053410
TheCU->getSourceLanguage(),
34063411
// TODO: Support "Source" from external AST providers?

clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ class PCHContainerGenerator : public ASTConsumer {
191191
// Prepare CGDebugInfo to emit debug info for a clang module.
192192
auto *DI = Builder->getModuleDebugInfo();
193193
StringRef ModuleName = llvm::sys::path::filename(MainFileName);
194-
DI->setPCHDescriptor(
195-
{ModuleName, "", OutputFileName, ASTFileSignature::createDISentinel()});
194+
DI->setPCHDescriptor({ModuleName, "", OutputFileName,
195+
ASTFileSignature::createDISentinel(), /*CASID=*/""});
196196
DI->setModuleMap(MMap);
197197
}
198198

0 commit comments

Comments
 (0)