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

    <tr>
        <th>Summary</th>
        <td>
            bolt: --instrument merges files not correctly and gets overwritten from previous profile 
        </td>
    </tr>

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

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

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

<pre>
    Hey,

As already mentioned here some time ago, https://github.com/ClangBuiltLinux/tc-build/issues/155#issuecomment-1106831385 the bug seems still present in the main branch.

If instrumenting a binary with:

```
llvm-bolt \
        --instrument \
        --instrumentation-file=/tmp/bolt.fdata \
        ${BINARY} \
        -o ${BINARY}.inst
```

The output of the profile does not get correctly merged automatically, only the latest process seems to profiled correctly. 
For example if running a workload with the instrumented binary will result into a profile which has very less branches.

example:

```
BOLT-INFO: shared object or position-independent executable detected
BOLT-INFO: Target architecture: x86_64
BOLT-INFO: BOLT version: f595b51f502b2f3c97d30e826784159438bde9c4
BOLT-INFO: first alloc address is 0x0
BOLT-INFO: enabling relocation mode
BOLT-INFO: enabling lite mode
BOLT-WARNING: Failed to analyze 2424 relocations
BOLT-INFO: pre-processing profile using branch profile reader
BOLT-INFO: forcing -jump-tables=move as PIC jump table was detected in function _ZN4llvm3sys4path11is_absoluteERKNS_5TwineENS1_5StyleE
BOLT-INFO: 1315 out of 96706 functions in the binary (1.4%) have non-empty execution profile
BOLT-INFO: 3 functions with profile could not be optimized
BOLT-INFO: profile for 1 objects was ignored
BOLT-INFO: the input contains 176 (dynamic count : 5974) opportunities for macro-fusion optimization. Will fix instances on a hot path.
BOLT-INFO: 13166 instructions were shortened
BOLT-INFO: removed 11 empty blocks
BOLT-INFO: ICF folded 316 out of 96983 functions in 4 passes. 0 functions had jump tables.
BOLT-INFO: Removing all identical functions will save 94.04 KB of code space. Folded functions were called 3316 times based on profile.
BOLT-INFO: basic block reordering modified layout of 330 (0.34%) functions
BOLT-INFO: UCE removed 1 blocks and 7 bytes of code.
BOLT-INFO: splitting separates 560689 hot bytes from 346146 cold bytes (61.83% of split functions is hot).
BOLT-INFO: 3 Functions were reordered by LoopInversionPass
BOLT-INFO: hfsort+ reduced the number of chains from 1002 to 552
BOLT-INFO: program-wide dynostats after all optimizations before SCTC and FOP:

              553848 : executed forward branches
               64210 : taken forward branches
              191942 : executed backward branches
              100756 : taken backward branches
               59984 : executed unconditional branches
              251734 : all function calls
              103854 : indirect calls
               96734 : PLT calls
             5738078 : executed instructions
             1228106 : executed load instructions
              632336 : executed store instructions
                 988 : taken jump table branches
                   0 : taken unknown indirect branches
              805774 : total branches
              224950 : taken branches
              580824 : non-taken conditional branches
              164966 : taken conditional branches
              745790 : all conditional branches

              525170 : executed forward branches (-5.2%)
               25848 : taken forward branches (-59.7%)
              220620 : executed backward branches (+14.9%)
               95176 : taken backward branches (-5.5%)
               30714 : executed unconditional branches (-48.8%)
              251734 : all function calls (=)
              103854 : indirect calls (=)
               96734 : PLT calls (=)
             5676670 : executed instructions (-1.1%)
             1228106 : executed load instructions (=)
              632336 : executed store instructions (=)
                 988 : taken jump table branches (=)
                   0 : taken unknown indirect branches (=)
              776504 : total branches (-3.6%)
              151738 : taken branches (-32.5%)
              624766 : non-taken conditional branches (+7.6%)
              121024 : taken conditional branches (-26.6%)
              745790 : all conditional branches (=)

BOLT-INFO: SCTC: patched 4 tail calls (4 forward) tail calls (0 backward) from a total of 4 while removing 0 double jumps and removing 4 basic blocks totalling 20 bytes of code. CTCs total execution count is 36 and the number of times CTCs are taken is 8.
BOLT-INFO: padding code to 0x5000000 to accommodate hot text
BOLT-INFO: setting __hot_start to 0x4e00000
BOLT-INFO: setting __hot_end to 0x4e956f4
```

running the same workload with following options:

```
llvm-bolt \
        --instrument \
        --instrumentation-file-append-pid \
        --instrumentation-file=~/data/bolt.fdata \
        ${BINARY} \
        -o ${BINARY}.inst
```
Will result into a massive amount of profiles which uses alot of space. After compiling clang with the instrumented binary results into a over 100GB of fdata files.
After merging them with `merge-fdata` and using the profile with llvm-bolt shows following stats:

```
BOLT-INFO: shared object or position-independent executable detected
BOLT-INFO: Target architecture: x86_64
BOLT-INFO: BOLT version: f595b51f502b2f3c97d30e826784159438bde9c4
BOLT-INFO: first alloc address is 0x0
BOLT-INFO: enabling relocation mode
BOLT-INFO: enabling lite mode
BOLT-WARNING: Failed to analyze 2424 relocations
BOLT-INFO: pre-processing profile using branch profile reader
BOLT-INFO: forcing -jump-tables=move as PIC jump table was detected in function _ZN5clang4Decl19castFromDeclContextEPKNS_11DeclContextE
BOLT-WARNING: 7 collisions detected while hashing binary objects. Use -v=1 to see the list.
BOLT-INFO: 16889 out of 96706 functions in the binary (17.5%) have non-empty execution profile
BOLT-INFO: 495 functions with profile could not be optimized
BOLT-WARNING: 9 (0.1% of all profiled) functions have invalid (possibly stale) profile. Use -report-stale to see the list.
BOLT-WARNING: 695817 out of 2165380220666 samples in the binary (0.0%) belong to functions with invalid (possibly stale) profile.
BOLT-INFO: profile for 1 objects was ignored
BOLT-INFO: the input contains 9616 (dynamic count : 19121657995) opportunities for macro-fusion optimization. Will fix instances on a hot path.
BOLT-INFO: 782089 instructions were shortened
BOLT-INFO: removed 1458 empty blocks
BOLT-INFO: ICF folded 1687 out of 96983 functions in 5 passes. 2 functions had jump tables.
BOLT-INFO: Removing all identical functions will save 192.87 KB of code space. Folded functions were called 6708861534 times based on profile.
BOLT-INFO: basic block reordering modified layout of 9503 (9.97%) functions
BOLT-INFO: UCE removed 1 blocks and 7 bytes of code.
BOLT-INFO: splitting separates 11629561 hot bytes from 11335899 cold bytes (50.64% of split functions is hot).
BOLT-INFO: 223 Functions were reordered by LoopInversionPass
BOLT-INFO: hfsort+ reduced the number of chains from 15697 to 7487
BOLT-INFO: program-wide dynostats after all optimizations before SCTC and FOP:

       1345852442580 : executed forward branches
        177695950568 : taken forward branches
        279784019546 : executed backward branches
        165552345783 : taken backward branches
         80707139554 : executed unconditional branches
        111711292644 : all function calls
         38436135059 : indirect calls
         20540549655 : PLT calls
      10555075375899 : executed instructions
       2627819906958 : executed load instructions
       1279424679765 : executed store instructions
         16846108018 : taken jump table branches
                   0 : taken unknown indirect branches
       1706343601680 : total branches
        423955435905 : taken branches
       1282388165775 : non-taken conditional branches
        343248296351 : taken conditional branches
       1625636462126 : all conditional branches

       1280317632631 : executed forward branches (-4.9%)
         78974541817 : taken forward branches (-55.6%)
        345318829495 : executed backward branches (+23.4%)
        155608698163 : taken backward branches (-6.0%)
         60656090585 : executed unconditional branches (-24.8%)
        111711292644 : all function calls (=)
         38436135059 : indirect calls (=)
         20540549655 : PLT calls (=)
      10505269253372 : executed instructions (-0.5%)
       2627819906958 : executed load instructions (=)
       1279424679765 : executed store instructions (=)
         16846108018 : taken jump table branches (=)
                   0 : taken unknown indirect branches (=)
       1686292552711 : total branches (-1.2%)
        295239330565 : taken branches (-30.4%)
       1391053222146 : non-taken conditional branches (+8.5%)
        234583239980 : taken conditional branches (-31.7%)
       1625636462126 : all conditional branches (=)

BOLT-INFO: SCTC: patched 78 tail calls (70 forward) tail calls (8 backward) from a total of 78 while removing 12 double jumps and removing 65 basic blocks totalling 325 bytes of code. CTCs total execution count is 54542385 and the number of times CTCs are taken is 51777916.
BOLT-INFO: setting __hot_start to 0x4e00000
```

Think the discussion at https://github.com/ClangBuiltLinux/tc-build/issues/155 could be also helpful since this bug is there a bit discussed.

The last tests ive run was with commit https://github.com/llvm/llvm-project/commit/96f6ec5090c2f7a1e4804693cbb84c29c574b3de and also their seems everything equal:

Regards.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWktz20YS_jXUZYqoeWAeOOhgyVZWlZTjsp1K7V5UA2AoIgYBLgawpPz6dA9AimRAkHStvZewVBRBTPf0e7o_Iq3zl-t_uZcZv53RtzP6pn9_44ktG2fzF7JyVVvUlcvJ0jWO-HrlSFvAm32sgYos23btZ-LNjN_B32PRLrs0yuoVX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