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

    <tr>
        <th>Summary</th>
        <td>
            [clang] Enabling `-ftime-report` pollutes the output of `-stats-file`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    This has been an issue for a long time (at least Clang 19 since I checked)

If we run Clang with `-Xclang=-stats-file`, we get the following (expected) output:

```sh
$ bin/clang -O0  ~/hello_world.cpp -S -o /dev/null -Xclang=-stats-file=/tmp/stats.txt
$ cat /tmp/stats.txt
{
 "asm-printer.EmittedInsts": 38,
        "dagcombine.NodesCombined": 1,
 "dwarf-eh-prepare.NumCleanupLandingPadsRemaining": 1,
 "dwarf-eh-prepare.NumUnwind": 1,
        "file-search.NumIncluded": 564,
        "file-search.NumMultiIncludeFileOptzn": 288,
 "isel.NumDAGBlocks": 5,
        "isel.NumDAGIselRetries": 55,
 "isel.NumEntryBlocks": 1,
        "isel.NumFastIselBlocks": 2,
 "isel.NumFastIselFailures": 11,
        "isel.NumFastIselSuccess": 13,
 "isel.NumFastIselSuccessIndependent": 7,
 "isel.NumFastIselSuccessTarget": 6,
        "mcexpr.MCExprEvaluate": 24,
        "prologepilog.NumBytesStackSpace": 552,
 "prologepilog.NumFuncSeen": 1,
        "regalloc.NumCoalesced": 17,
 "regalloc.NumLoads": 1,
        "regalloc.NumStores": 1,
 "source-manager.MaxUsedSLocBytes": 3848014,
 "stackmaps.NumStackMapFuncSkipped": 1,
 "stackmaps.NumStackMapFuncVisited": 1
}
```

However, if we have both `-ftime-report` **and** `-stats-file`, the timers from `-ftime-report` contaminates the output of the stats file:

```sh
$ bin/clang -O0 ~/hello_world.cpp -S -o /dev/null -Xclang=-stats-file=/tmp/stats.txt -ftime-report 2>/dev/null
$ cat /tmp/stats.txt
{
# ...
 "time.pass.AlwaysInlinerPass.wall": 7.1525573730468750e-06,
 "time.pass.AlwaysInlinerPass.user": 0.0000000000000000e+00,
 "time.pass.AlwaysInlinerPass.sys": 7.0000000000000617e-06,
 "time.pass.CoroConditionalWrapper.wall": 5.2452087402343750e-06,
 "time.pass.CoroConditionalWrapper.user": 0.0000000000000000e+00,
 "time.pass.CoroConditionalWrapper.sys": 4.9999999999980616e-06,
# ...etc
}
```

The reason for this is that `llvm::PrintStatisticsJSON(...)` [prints all the timers](https://github.com/llvm/llvm-project/blob/334e05b4b78a5de6efdcb23f208f3453a5b364a0/llvm/lib/Support/Statistic.cpp#L223) when it should only print the statistics counter.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVkFv4zYT_TX0hbAgkZIsH3xwnOj7UiS7i_Vu21sxokYSG5oUSCpO9tDfXlB2bGcd76ZFBQM26XlvZjicpwHnZKsRFyS7IoyBAv2tA5MQxkh2PYHBd8YuDtuTytTPiy-ddLQDRytETUFT6dyAtDGWAlVGt9TLDVLCCvBUIThPVwp0S5M5dVILpLdUdCgesCZsTuIliZe3Dd0itYPem26l7yjJ4-nvIqwJv546D95NG6mQ5DFhqwBo0VPfBd9Kma3UbfCKTz0KP5JTM_h-8IQvd24Ccvy4LqxYSiupCStHJ3T6Mab0L8LKDpUyf2yNVXUk-p5O13RqKGFljY-ElXpQir4dGb8mrPSbnrBy3I38k997EuDp23_Orki8pOH83WbaW6k92uhmI73H-lY770I5-JLygrBVMN0_hLEaWmE2ldQYfTA1utVuUe8RyR4QLLdgmyl2095iDxajD8NmpRD00N-BrqVuP0HtPuMGpJa6fR_BV72V-ntnx-jCkUwdghVdsL7VQg31IbgsT3-GuB-Ul3tYKRV-7P03vYezojgGJx2qALhe_u9KGfHwcmTZmYcTy1uH6jN6K_Fgnp1T3mhvn1-Rnif6YluC84H1lTk753yxK0GqwR7cJz9nXg9CoDsA-GXuveWt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