1. Jun 25, 2020
    • Maksim Panchenko's avatar
      [BOLT] Do not emit duplicate org symbols · ffaba224
      Maksim Panchenko authored
      Summary:
      When adding symbols for patched functions, we may end up emitting
      multiple symbols per function if the function has multiple names (e.g.
      after identical code folding by the linker).
      
      (cherry picked from FBD22294112)
      ffaba224
  2. Jun 27, 2020
    • Maksim Panchenko's avatar
      [BOLT] Add static binary support · 250ca408
      Maksim Panchenko authored
      Summary:
      Accept binaries without dynamic section/segment as a valid input.
      
      Modify the check for invalid debug info "executables" that are result of
      running "objcopy --only-keep-debug". Instead of checking for an empty
      dynamic segment, check that ".text" is mapped into a valid segment.
      
      Move SegmentMapInfo inside BinaryContext.
      
      Fixes facebookincubator/BOLT#91
      
      Temporarily removing issue*.test tests that use yaml2obj and operate on
      fake binaries.
      
      (cherry picked from FBD22271481)
      250ca408
  3. Jun 26, 2020
    • Maksim Panchenko's avatar
      [perf2bolt] Relax rules for aggregation in strict mode · 94230a2c
      Maksim Panchenko authored
      Summary:
      While aggregating perf.data events, even in strict mode, there is no
      need to process all functions since we are not generating an output
      binary. However, it's still important to convert data for as many
      functions as possible, even for ones with unknown internal control flow.
      
      (cherry picked from FBD22248390)
      94230a2c
  4. Jun 24, 2020
    • Maksim Panchenko's avatar
      [BOLT] Ignore duplicate relocations · 4aaa8892
      Maksim Panchenko authored
      Summary:
      lld linker may emit static relocations against addresses that also have
      dynamic relocations associated with them. When this happens, BOLT fails
      to validate the extracted value at the address.
      
      Read dynamic relocations in the binary and ignore static relocations at
      addresses that have a duplicate dynamic relocation.
      
      (cherry picked from FBD22192345)
      4aaa8892
  5. Jun 23, 2020
    • Maksim Panchenko's avatar
      [BOLT] Add '-force-patch' to forcefully patch old entries · 13baf47a
      Maksim Panchenko authored
      Summary:
      The option is useful for debugging.
      
      Also, print personality function when dumping a function.
      
      (cherry picked from FBD22169482)
      13baf47a
    • Maksim Panchenko's avatar
      [BOLT] Fix getNewValueForSymbol() · 4946b881
      Maksim Panchenko authored
      Summary:
      getNewValueForSymbol() uses orc::RTDyldObjectLinkingLayer::findSymbol()
      to resolve symbol values. The latter will always return JITSymbol,
      even if there was no symbol defined. The address for the undefined
      symbol will be zero, but some symbols could legally be resolved to zero
      too.
      
      We need to distinguish between real zero-valued symbols and symbols that
      were not emitted and are not visible by orc::RTDyldObjectLinkingLayer.
      If zero address is returned by ORC, check for a binary data with the
      same name and use its address for the symbol resolution.
      
      (cherry picked from FBD22175269)
      4946b881
    • Maksim Panchenko's avatar
      [BOLT] Allow to overwrite -use-old-text option · ae296ea6
      Maksim Panchenko authored
      (cherry picked from FBD22169409)
      ae296ea6
    • Maksim Panchenko's avatar
      [BOLT] Ignore functions that failed validation · 12b7987d
      Maksim Panchenko authored
      Summary:
      If a function failed internal calls validation, we can ignore it and
      keep processing the binary.
      
      (cherry picked from FBD22169381)
      12b7987d
    • Maksim Panchenko's avatar
      [BOLT] Create entry points for internal refs from external code · efce443e
      Maksim Panchenko authored
      Summary:
      If we detect an internal function reference from code outside of the
      function, then create an entry point at that location.
      
      (cherry picked from FBD22169337)
      efce443e
    • Maksim Panchenko's avatar
      [BOLT] Fixes for scanExternalRefs() · 0403adde
      Maksim Panchenko authored
      Summary:
      In my previous commit, I've accidentally reverted the condition while
      evaluating a branch target.
      
      Also, do not emit instruction for relocation purposes in
      scanExternalRefs() if there was no TargetSymbol set and we have not
      produced new relocations.
      
      (cherry picked from FBD22169317)
      0403adde
    • Maksim Panchenko's avatar
      [BOLT] Properly register symbols at secondary entry points · 8374e8e3
      Maksim Panchenko authored
      Summary:
      We may end up with a secondary entry symbol set to zero if there was no
      symbol in the input file at the entry point address, and if we skipped
      the function emission, e.g. if it was ignored. In that case, the symbol
      should be properly initialized with a proper address.
      
      (cherry picked from FBD22169167)
      8374e8e3
  6. Jun 19, 2020
  7. 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
  8. 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
  9. Jun 13, 2020
  10. Jun 16, 2020
  11. 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
  12. 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
  13. 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
  14. 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
  15. May 26, 2020
  16. 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
  17. 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
  18. May 15, 2020
  19. 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
  20. 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
  21. May 07, 2020
  22. 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
  23. 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
  24. May 02, 2020
  25. Apr 27, 2020
  26. Apr 28, 2020
  27. 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
  28. Apr 25, 2020
  29. Apr 24, 2020
  30. 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