[PATCH] D58893: Modules: Invalidate out-of-date PCMs as they're discovered

Duncan P. N. Exon Smith via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 3 23:20:12 PST 2019


dexonsmith created this revision.
dexonsmith added reviewers: Bigcheese, bruno, rsmith, vsapsai.
Herald added a subscriber: jdoerfert.
dexonsmith added parent revisions: D58890: Modules: Rename MemoryBufferCache to InMemoryModuleCache, D58891: Modules: Add -Rmodule-import.

Leverage the InMemoryModuleCache to invalidate a module the first time
it fails to import (and to lock a module as soon as it's built or
imported successfully).  For implicit module builds, this optimizes
importing deep graphs where the leaf module is out-of-date; see example
near the end of the commit message.

Previously the cache finalized ("locked in") all modules imported so far
when starting a new module build.  This was sufficient to prevent
loading two versions of the same module, but was somewhat arbitrary and
hard to reason about.

Now the cache explicitly tracks module state, where each module must be
one of:

- Unknown: module not in the cache (yet).
- Tentative: module in the cache, but not yet fully imported.
- ToBuild: module found on disk could not be imported; need to build.
- Final: module in the cache has been successfully built or imported.

Preventing repeated failed imports avoids variation in builds based on
shifting filesystem state.  Now it's guaranteed that a module is loaded
from disk exactly once.  It now seems safe to remove
FileManager::invalidateCache, but I'm leaving that for a later commit.

The new, precise logic uncovered a pre-existing problem in the cache:
the map key is the module filename, and different contexts use different
filenames for the same PCM file.  (In particular, the test
Modules/relative-import-path.c does not build without this commit.
r223577 started using a relative path to describe a module's base
directory when importing it within another module.  As a result, the
module cache sees an absolute path when (a) building the module or
importing it at the top-level, and a relative path when (b) importing
the module underneath another one.)

The "obvious" fix is to resolve paths using FileManager::getVirtualFile
and change the map key for the cache to a FileEntry, but some contexts
(particularly related to ASTUnit) have a shorter lifetime for their
FileManager than the InMemoryModuleCache.  This is worth pursuing
further in a later commit; perhaps by tying together the FileManager and
InMemoryModuleCache lifetime, or moving the in-memory PCM storage into a
VFS layer.

For now, use the PCM's base directory as-written for constructing the
filename to check the ModuleCache.

Example
=======

To understand the build optimization, first consider the build of a
module graph TU -> A -> B -> C -> D with an empty cache:

  TU builds A'
     A' builds B'
        B' builds C'
           C' builds D'
              imports D'
        B' imports C'
           imports D'
     A' imports B'
        imports C'
        imports D'
  TU imports A'
     imports B'
     imports C'
     imports D'

If we build TU again, where A, B, C, and D are in the cache and D is
out-of-date, we would previously get this build:

  TU imports A
     imports B
     imports C
     imports D (out-of-date)
  TU builds A'
     A' imports B
        imports C
        imports D (out-of-date)
        builds B'
        B' imports C
           imports D (out-of-date)
           builds C'
           C' imports D (out-of-date)
              builds D'
              imports D'
        B' imports C'
           imports D'
     A' imports B'
        imports C'
        imports D'
   TU imports A'
      imports B'
      imports C'
      imports D'

After this commit, we'll immediateley invalidate A, B, C, and D when we
first observe that D is out-of-date, giving this build:

  TU imports A
     imports B
     imports C
     imports D (out-of-date)
  TU builds A' // The same graph as an empty cache.
     A' builds B'
        B' builds C'
           C' builds D'
              imports D'
        B' imports C'
           imports D'
     A' imports B'
        imports C'
        imports D'
  TU imports A'
     imports B'
     imports C'
     imports D'

The new build matches what we'd naively expect, pretty closely matching
the original build with the empty cache.

rdar://problem/48545366


https://reviews.llvm.org/D58893

Files:
  clang/include/clang/Serialization/ASTReader.h
  clang/include/clang/Serialization/InMemoryModuleCache.h
  clang/lib/Frontend/CompilerInstance.cpp
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/lib/Serialization/InMemoryModuleCache.cpp
  clang/lib/Serialization/ModuleManager.cpp
  clang/test/Modules/Inputs/implicit-invalidate-chain/A.h
  clang/test/Modules/Inputs/implicit-invalidate-chain/B.h
  clang/test/Modules/Inputs/implicit-invalidate-chain/C.h
  clang/test/Modules/Inputs/implicit-invalidate-chain/module.modulemap
  clang/test/Modules/Inputs/relative-import-path/A.h
  clang/test/Modules/Inputs/relative-import-path/B.h
  clang/test/Modules/Inputs/relative-import-path/C.h
  clang/test/Modules/Inputs/relative-import-path/module.modulemap
  clang/test/Modules/implicit-invalidate-chain.c
  clang/test/Modules/relative-import-path.c
  clang/unittests/Serialization/InMemoryModuleCacheTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58893.189122.patch
Type: text/x-patch
Size: 29123 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190304/2bd9bd1c/attachment-0001.bin>


More information about the cfe-commits mailing list