1. Jun 19, 2020
  2. Jun 15, 2020
    • Maksim Panchenko's avatar
      [BOLT] Support for lite mode with relocations · 0ce0bce9
      Maksim Panchenko authored
      Summary:
      Add '-lite' support for relocations for improved processing time,
      memory consumption, and more resilient processing of binaries with
      embedded assembly code.
      
      In lite relocation mode, BOLT will skip full processing of functions
      without a profile. It will run scanExternalRefs() on such functions
      to discover external references and to create internal relocations
      to update references to optimized functions.
      
      Note that we could have relied on the compiler/linker to provide
      relocations for function references. However, there's no assurance
      that all such references are reported. E.g., the compiler can resolve
      inter-procedural references internally, leaving no relocations
      for the linker.
      
      The scan process takes about <10 seconds per 100MB of code on modern
      hardware. It's a reasonable overhead to live with considering the
      flexibility it provides.
      
      If BOLT fails to scan or disassemble a function, .e.g., due to a data
      object embedded in code, or an unsupported instruction, it enables a
      patching mode to guarantee that the failed function will call
      optimized/moved versions of functions. The patching happens at original
      function entry points.
      
      '-skip=<func1,func2,...>' option now can be used to skip processing of
      arbitrary functions in the relocation mode.
      
      With '-use-old-text' or '-strict' we require all functions to be
      processed. As such, it is incompatible with '-lite' option,
      and '-skip' option will only disable optimizations of listed
      functions, not their disassembly and emission.
      
      (cherry picked from FBD22040717)
      0ce0bce9
  3. Jun 17, 2020
    • Xun Li's avatar
      Be more flexible when locating runtime libs · e22378d2
      Xun Li authored
      Summary:
      In some cases we install bolt binary into one level deeper in bin/, such as bin/install/, we need to go back one more level to find lib directory.
      
      (cherry picked from FBD22070974)
      e22378d2
  4. Jun 13, 2020
  5. Jun 16, 2020
  6. Jun 11, 2020
    • takh's avatar
      Generate heatmap for linux kernel · 48b71ad2
      takh authored
      Summary:
      This diff handles several challenges related to heatmap generation for Linux kernel (vmlinux elf file):
      - If the input binary elf file contains the section `__ksymtab`, this diff assumes that this is the linux kernel `vmlinux` file and enables an extra flag `LinuxKernelMode`
      - In `LinuxKernelMode`, we only support heat map generation right now, therefore it ensures that current BOLT mode is heat map generation. Otherwise, it exits with error.
      - For some Linux symbol and section combinations, BOLT may not be able to find section for symbol (specially symbols that specifies the end of some section). For such cases, we show an warning message without exiting which was the previous behavior.
      - Linux kernel elf file does not contain dynamic section, therefore, we don't exit when no dynamic section is found for linux kernel binary.
      - Current `ParseMMap` logic does not work with linux kernel. MMap entries for linux kernel uses `PERF_RECORD_MMAP` format instead of typical `PERF_RECORD_MMAP2` format. Since linux kernel address mapping is absolute (same as specified in the ELF file), we avoid calling `ParseMMap` in linux kernel mode.
      - Linux kernel entries are registered with PID -1, therefore `BinaryMMapInfo` lookup is not required for linux kernel entries. Similarly, `adjustLBR` is also not required.
      - Default max address in linux kernel mode is highest unsigned 64-bit integer instead of current 4GBs.
      - Added another new parameter for heatmap, `MinAddress`, in case of Linux kernel mode which is `KernelBaseAddress`, otherwise, it is 0. While registering Heatmap sample counts from LBR entries, any address lower than this `MinAddress` is ignored.
      - `IgnoreInterruptLBR` is disabled in linux kernel mode to ensure that kernel entries are processed
      
      Currently, linux kernel heat map also include heat map for Linux kernel modules that are not part of vmlinux elf file. This is intentional to identify other potential optimization opportunities. If reviewers think, those modules should be omitted, I will disable those modules based on highest end address of a vmlinux elf section.
      
      (cherry picked from FBD21992765)
      48b71ad2
  7. Jun 10, 2020
    • Maksim Panchenko's avatar
      [BOLT] Update section index for symbols from unemitted functions · 2d524fd5
      Maksim Panchenko authored
      Summary:
      Under some conditions, e.g. while running in lite mode or when a
      function is non-simple, BOLT may decide not to emit function code and
      hence there's no need to update the symbol. However, since we change
      section table, the corresponding section index may need an update.
      
      Also, update section index for ICF symbols.
      
      (cherry picked from FBD21970017)
      2d524fd5
  8. May 03, 2020
    • Xun Li's avatar
      Adding automatic huge page support · 9bd71615
      Xun Li authored
      Summary:
      This patch enables automated hugify for Bolt.
      When running Bolt against a binary with -hugify specified, Bolt will inject a call to a runtime library function at the entry of the binary. The runtime library calls madvise to map the hot code region into a 2M huge page. We support both new kernel with THP support and old kernels. For kernels with THP support we simply make a madvise call, while for old kernels, we first copy the code out, remap the memory with huge page, and then copy the code back.
      With this change, we no longer need to manually call into hugify_self and precompile it with --hot-text. Instead, we could simply combine --hugify option with existing optimizations, and at runtime it will automatically move hot code into 2M pages.
      
      Some details around the changes made:
      1. Add an command line option to support --hugify. --hugify will automatically turn on --hot-text to get the proper hot code symbols. However, running with both --hugify and --hot-text is not allowed, since --hot-text is used on binaries that has precompiled call to hugify_self, which contradicts with the purpose of --hugify.
      2. Moved the common utility functions out of instr.cpp to common.h, which will also be used by hugify.cpp. Added a few new system calls definitions.
      3. Added a new class that inherits RuntimeLibrary, and implemented the necessary emit and link logic for hugify.
      4. Added a simple test for hugify.
      
      (cherry picked from FBD21384529)
      9bd71615
  9. May 22, 2020
    • Xun Li's avatar
      Refactor runtime library · 00892a5f
      Xun Li authored
      Summary:
      As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
      
      This patch does the following:
      1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
      2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
      3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
      4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
      5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
      6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
      7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
      
      (cherry picked from FBD21694762)
      00892a5f
  10. May 26, 2020
  11. May 22, 2020
    • Xun Li's avatar
      Use shuffle instead of random_shuffle · 2b65b3aa
      Xun Li authored
      Summary: random_shuffle is deprecated in C++14.
      
      (cherry picked from FBD21698180)
      2b65b3aa
    • Xun Li's avatar
      Remove const call to take_front · 8a680745
      Xun Li authored
      Summary:
      take_front() is a const member of StringRef. Calling it does nothing.
      This suggests that this line of code is useless, deleting it.
      But it's good to double check, what was the original intention here?
      
      (cherry picked from FBD21697637)
      8a680745
  12. May 08, 2020
    • Maksim Panchenko's avatar
      [BOLT] Refactor profile-handling code · 87291711
      Maksim Panchenko authored
      Summary:
      This diff handles several issues related to profile reading and
      handling:
        * Unifies interface used by 3 profile readers in ProfileReaderBase.
        * Adds automatic detection of the profile file contents.
        * Removes reader-specific fields from BinaryFunction and BinaryData.
          All the information is stored in instruction annotations.
        * Removes implicit memory dependencies in annotations on profile
          reader instance.
        * Adds lite mode support to YAML reader.
        * Moves profile reading code out of BinaryFunction.
      
      (cherry picked from FBD21601411)
      87291711
  13. May 15, 2020
  14. May 07, 2020
    • Rafael Auler's avatar
      [BOLT] Add option to tag version · f91d121e
      Rafael Auler authored
      Summary:
      Add a dummy option in BOLT to allow us to write any string in
      the bolt info section. This is accomplished since we record the complete
      argv vector. This string used to tag this binary with any ID that can
      later be associated with a specific BOLT invocation.
      
      (cherry picked from FBD21441902)
      f91d121e
  15. May 06, 2020
    • Maksim Panchenko's avatar
      [BOLT] Change .debug_line emission for non-simple functions · 689447bf
      Maksim Panchenko authored
      Summary:
      We use a special routine to emit line info for functions that we do not
      overwrite. The resulting DWARF was not quite efficient as we were
      advancing addresses using a larger than needed opcodes. Since there were
      only a few functions that we didn't emit/overwrite, it was not a big
      issue.
      
      However, in lite mode the majority of functions are not overwritten and
      as a result, the inefficiency in debug line encoding got exposed and
      binaries were getting larger than expected .debug_line sections.
      
      Fix it by using more conventional line table opcodes for address
      advancing.
      
      (cherry picked from FBD21423074)
      689447bf
  16. May 07, 2020
  17. May 05, 2020
    • Xun Li's avatar
      Check runtime lib format within archiver · 7b61bdf8
      Xun Li authored
      Summary: We only support linking ELF runtime library right now. If the library is an archiver, we check that each individual library inside the archiver is an ELF library.
      
      (cherry picked from FBD21388672)
      7b61bdf8
  18. May 04, 2020
    • Maksim Panchenko's avatar
      [BOLT] Introduce lite processing mode without relocations · 924d0bdb
      Maksim Panchenko authored
      Summary:
      When optimizing a binary without relocations, we can skip processing
      functions without profile (cold functions). By skipping processing of
      cold functions, we reduce the processing time and memory. We call
      such mode a lite mode, and it is enabled by default.
      
      Some processing is still done for functions without profile even in lite
      mode. scanExternalRefs() function is used to detect secondary entry
      points to functions that are not marked in the symbol table.
      
      Note that the no-relocation requirement is a temporary limitation
      of the lite mode.
      
      (cherry picked from FBD21366567)
      924d0bdb
    • Maksim Panchenko's avatar
      [BOLT] Introduce isIgnored() function attribute · 04c5d4fc
      Maksim Panchenko authored
      Summary:
      Whenever a function is not meant for processing, e.g. when the user
      requests to optimize only a subset of functions, mark the function as
      ignored. Use this attribute instead of opts::shouldProcess().
      
      (cherry picked from FBD21374806)
      04c5d4fc
  19. May 02, 2020
  20. Apr 27, 2020
  21. Apr 28, 2020
  22. Apr 20, 2020
    • Maksim Panchenko's avatar
      [BOLT] Change symbol handling for secondary function entries · 5296b6d1
      Maksim Panchenko authored
      Summary:
      Some functions could be called at an address inside their function body.
      Typically, these functions are written in assembly as C/C++ does not
      have a multi-entry function concept. The addresses inside a function
      body that could be referenced from outside are called secondary entry
      points.
      
      In BOLT we support processing functions with secondary/multiple entry
      points. We used to mark basic blocks representing those entry points
      with a special flag. There was only one problem - each basic block has
      exactly one MCSymbol associated with it, and for the most efficient
      processing we prefer that symbol to be local/temporary. However, in
      certain scenarios, e.g. when running in non-relocation mode, we need
      the entry symbol to be global/non-temporary.
      
      We could create global symbols for secondary points ahead of time when
      the entry point is marked in the symbol table. But not all such entries
      are properly marked. This means that potentially we could discover an
      entry point only after disassembling the code that references it, and
      it could happen after a local label was already created at the same
      location together with all its references. Replacing the local symbol
      and updating the references turned out to be an error-prone process.
      
      This diff takes a different approach. All basic blocks are created with
      permanently local symbols. Whenever there's a need to add a secondary
      entry point, we create an extra global symbol or use an existing one
      at that location. Containing BinaryFunction maps a local symbol of a
      basic block to the global symbol representing a secondary entry point.
      This way we can tell if the basic block is a secondary entry point,
      and we emit both symbols for all secondary entry points. Since secondary
      entry points are quite rare, the overhead of this approach is minimal.
      
      Note that the same location could be referenced via local symbol from
      inside a function and via global entry point symbol from outside.
      This is true for both primary and secondary entry points.
      
      (cherry picked from FBD21150193)
      5296b6d1
  23. Apr 25, 2020
  24. Apr 24, 2020
  25. Apr 23, 2020
    • Maksim Panchenko's avatar
      [BOLT] Option to fail if invalid profile detected · 0ea98d1f
      Maksim Panchenko authored
      Summary:
      Add an option to fail processing of the input binary if the profile
      is not accurate:
      
        -stale-threshold=<uint>
          - maximum percentage of stale functions to tolerate (default: 100)
      
      Default (100) means never to fail.
      
      A function profile is considered stale if any branch in its profile
      has invalid source or destination.
      
      Use `-stale-threshold=0` to fail if any staleness is detected in the
      profile.
      
      (cherry picked from FBD21189036)
      0ea98d1f
  26. Apr 20, 2020
  27. Apr 16, 2020
    • Maksim Panchenko's avatar
      [BOLT] Emit ICF symbols for large functions · 10245b5c
      Maksim Panchenko authored
      Summary:
      In non-relocation mode, make sure we emit extra symbols for a folded
      function even if the function was not overwritten due to its large
      size.
      
      (cherry picked from FBD21080467)
      10245b5c
    • Maksim Panchenko's avatar
      [BOLT] Fix .eh_frame update with ICF in non-relocation mode · 606532bd
      Maksim Panchenko authored
      Summary:
      In a rare case, we may fold a function and fail to emit it in
      non-relocation mode due to a function size increase. At the same time,
      the function that the original function was folded into could have been
      successfully emitted, e.g. because it was split in the presence of a
      profile information.
      
      Later, because the function was not emitted, we have to use its original
      .eh_frame entry in the preserved .eh_frame section. However, that entry
      is no longer referencing the original function, but the function that
      the original was folded into. This happens since the original symbol gets
      emitted at the other function location. As a result, .eh_frame entry for
      the folded function is missing.
      
      To prevent incorrect update of the original .eh_frame, create
      relocations against absolute values. This guarantees preservation of the
      section contents while updating pc-relative references.
      
      (cherry picked from FBD21061130)
      606532bd
  28. Nov 12, 2019
    • Maksim Panchenko's avatar
      [BOLT] Speedup RTDyld external symbol resolution · 1be7a825
      Maksim Panchenko authored
      Summary:
      RuntimeDyldImpl::resolveExternalSymbols() some time ago used to call
      getSymbolAddress() while in the second loop. That call could have
      modified the contents of ExternalSymbolRelocations that the loop was
      iterating over. Thus the code was written in a way that erased the
      processed entry on every loop iteration and reset the map
      iterator. With large number of entries in ExternalSymbolRelocations the
      loop code becomes a performance bottleneck.
      
      Since getSymbolAddress() is no longer used, the
      ExternalSymbolRelocations could be iterated in a straightforward way and
      the map cleared before the function exit.
      
      (cherry picked from FBD21057058)
      1be7a825
  29. Apr 07, 2020
  30. Apr 08, 2020
    • Maksim Panchenko's avatar
      [BOLT] Further speedup ICF · 401fa5b4
      Maksim Panchenko authored
      Summary:
      Further speedup ICF by applying stricter rules for congruent functions.
      While checking symbolic operands in congruent functions, consider
      operands congruent only if they are equal or reference functions
      with identical hashes, i.e. potentially foldable functions.
      Note that jump table operands are handled as a special case.
      
      (cherry picked from FBD20912054)
      401fa5b4
  31. Apr 07, 2020
    • Maksim Panchenko's avatar
      [BOLT] Speedup ICF by better function hashing · ee0371ad
      Maksim Panchenko authored
      Summary:
      Too many hash collisions may cause ICF to run slowly.
      
      We used to hash BinaryFunction only looking at instruction opcodes,
      ignoring instruction operands. With many almost identical functions,
      such approach may lead to long ICF processing time. By including
      operands into the hash, we reduce the number of collisions and
      improve the runtime often by a factor of 2 or more.
      
      (cherry picked from FBD20888957)
      ee0371ad
  32. Apr 05, 2020
    • Maksim Panchenko's avatar
      [BOLT] Fix ICF non-determinism in non-relocation mode · abda7dc6
      Maksim Panchenko authored
      Summary:
      ICF may fold functions in arbitrary order when running multi-threaded.
      This is fine in relocation mode as we end up with just one function
      holding all function symbols.
      
      However, in non-relocation mode we keep all function bodies, and if we
      keep merging profiles in non-deterministic order, we end up with
      functions with non deterministic profiles. The fix for non-relocation
      mode is to not merge profiles as the factual new profile could be
      different from the merged one since both function instances are
      potentially callable.
      
      Additionally, emit extra symbols for ICF functions in non-relocation
      mode to make it possible to track the folding.
      
      (cherry picked from FBD20889866)
      abda7dc6
  33. Mar 31, 2020
    • Maksim Panchenko's avatar
      [BOLT] Verify exceptions action table equivalence in ICF · b08d82d9
      Maksim Panchenko authored
      Summary:
      Some functions may have exactly the same code and exception handlers.
      However, their action tables could be different leading to mismatching
      semantics. We should verify their equivalence while running ICF.
      
      (cherry picked from FBD20889035)
      b08d82d9
  34. Mar 25, 2020