Skip to content

Header modules: expose module names beside module files so a toolchain can load them by name #925

Open

Description

@lpulley

With use_header_modules, a compile receives -fmodule-file=<file> for every module in its dependency closure, through the module_files variable. Clang loads a module file passed that way before parsing, whether or not the unit includes one of its headers, so every unit pays the load time of every module in its closure. Clang's -fmodule-file=<name>=<file> form loads a module file only when the unit includes one of its headers, but a toolchain cannot emit it: module_files holds the files alone, and nothing in rules_cc pairs a module file with its module name. The name is the module map's (get_module_map_name), and the file's owner label is not always that name: a separate module is <name>.sep, and compile() may be called with a name other than the target's.

Repro: https://gist.github.com/lpulley/4165df8f53c1cd0ab87a2f7c699f19a2. //:unit includes small.h only, with //:big in its closure. strace -f -e trace=openat on its compile shows big.pcm opened. With the patch below applied and the toolchain emitting the name form, only small.pcm is opened.

We use a patch (in the gist) that builds _transitive_module_names / _transitive_pic_module_names depsets of <name>=<path> strings in _merge_compilation_contexts, beside the module file depsets, passes the depset to the compile action as a module_file_names variable, declares the variable in cc/toolchains/variables, and adds the fields to the two functions that rebuild CcCompilationContextInfo. A toolchain then iterates over the variable to emit -fmodule-file={module_file_names}.

One consequence: by-name loading maps an include to a module only through a module map clang has parsed, where eager loading reads the embedded map of every loaded module file. rules_cc passes the direct dependencies' maps alone, so a module reached through a dependency that compiles no module would be absorbed textually. The patch therefore also adds every dependency's module map to the compilation context's non-code inputs, transitively; each generated map names its dependencies' maps with extern module, so clang reaches the closure from the direct maps and no flags are added. I can open a PR for this if the approach is acceptable.

Activity

  1. fmeum commented on Oct 6, 2026

    @fmeum
    Collaborator

    This is very interesting, I didn't know about the alternate form of this argument. I would like to take a look at your PR.

    cc @trybka

  2. lpulley commented on Oct 6, 2026

    @lpulley
    Author

    Sure; I made this draft: #928

  3. lpulley commented on Oct 7, 2026

    @lpulley
    Author

    Note

    Authored by Claude

    Bazel 9.3.0 changes part of this. With bazelbuild/bazel#30396, a compile without include scanning now lists only its direct dependencies' module files instead of every module in its closure. That makes the command line much shorter, but two cases still load too much or too little:

    • A large module that is a direct dependency but is not included (boost here, about 450 MB) is still loaded eagerly.
    • A module reached only through a dependency that compiles no module is not listed at all, so clang cannot find it by name.

    So the request is now narrower: provide <module name>=<module file> pairs for the transitive module set next to the existing file list, as in #928, so a toolchain can pass -fmodule-file=<name>=<file> and let clang load only what the unit includes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions