<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/60996>60996</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Modules] Disappointing O(n^2) scaling of compile times with modules and optimizations
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          davidstone
      </td>
    </tr>
</table>

<pre>
    Given the following set of translation units

```cpp
export module a;

#define REPEAT8(x) \
        (x), \
        (x), \
        (x), \
        (x), \
        (x), \
        (x), \
        (x), \
        (x)


export unsigned a() {
        unsigned x = 0;
        REPEAT8(REPEAT8(REPEAT8(REPEAT8(REPEAT8(REPEAT8(++x))))));
        return x;
}
```

```cpp
export module b;

import a;

export unsigned b() {
        return a();
}
```

```cpp
export module c;

import b;

unsigned c() {
        return b();
}
```

Compiling with optimizations turned on (say, `-O3`) takes a few seconds for `a`, which is reasonable (`a` is designed to be expensive to compile and optimize in this example). But then it takes a few seconds for `b`, and a few seconds for `c`. My expectation was that the compiler would compile and optimize the function `a` once, when compiling `module a`, and then importing modules would be fast. The net effect effect of this problem is that after converting a program to use modules (and not using any module partitions), the last few modules are each taking about 30 minutes to compile, as each high-level module is optimizing almost my entire program serially.

This is effectively quadratic scaling of compile times -- `a` does all the work of `a`, `b` does all the work of `a` + `b`, `c` does all the work of `a` + `b` + `c`, etc.

I understand how we got to this point -- we want the generation of the .pcm file to happen as quickly as possible, because then any downstream consumer that doesn't care about optimizations can get access to that data quickly (just precompiling `b` took about 300 milliseconds on my machine, and precompiling `c` took about 15 milliseconds). However, this seems to sell out the common case of compiling your program.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVk2P2zYQ_TX0ZbCGTNmydfAhu862PQQpivwBihpZzFKkwqEsu7--IPXhD2yCLtBDAcGSyeHwvTePIwkidTSIe7Z5Z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