Commit Graph

1481 Commits

Author SHA1 Message Date
Fangrui Song 89fab98e88 [DebugInfo] llvm::Optional => std::optional
https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-05 00:09:22 +00:00
Fangrui Song b0df70403d [Target] llvm::Optional => std::optional
The updated functions are mostly internal with a few exceptions (virtual functions in
TargetInstrInfo.h, TargetRegisterInfo.h).
To minimize changes to LLVMCodeGen, GlobalISel files are skipped.

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-04 22:43:14 +00:00
Fangrui Song f4c16c4473 [MC] llvm::Optional => std::optional
https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-04 21:36:08 +00:00
Fangrui Song ea47ccc78f [BOLT] Fix after DebugInfoMetadata change 0ca43d4488 2022-12-04 18:57:52 +00:00
Kazu Hirata e324a80fab [BOLT] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-02 23:12:38 -08:00
Kazu Hirata 1028b165ee [BOLT] Fix a build error
This patch fixes:

  bolt/lib/Profile/DataAggregator.cpp:264:66: error: no viable
  conversion from 'Optional<llvm::StringRef>[3]' to
  'ArrayRef<std::optional<StringRef>>'
2022-12-01 15:48:03 -08:00
Kazu Hirata 04b59e7af9 [BOLT] Fix unused function warnings
This patch fixes:

  bolt/lib/Passes/CallGraph.cpp:27:15: error: unused function
  'hash_int64_fallback' [-Werror,-Wunused-function]

  bolt/lib/Passes/CallGraph.cpp:40:15: error: unused function
  'hash_int64' [-Werror,-Wunused-function]
2022-11-29 11:13:14 -08:00
Guillaume Chatelet 702126aec5 [NFC] Add helper method to ensure min alignment on MCSection
Follow up on D138653.

Differential Revision: https://reviews.llvm.org/D138686
2022-11-28 10:00:34 +00:00
Guillaume Chatelet 6c09ea3fdd [Alignment][NFC] Use Align in MCStreamer::emitValueToAlignment
Differential Revision: https://reviews.llvm.org/D138674
2022-11-24 16:09:44 +00:00
Guillaume Chatelet 4f17734175 [Alignment][NFC] Use Align in MCStreamer::emitCodeAlignment
This patch makes code less readable but it will clean itself after all functions are converted.

Differential Revision: https://reviews.llvm.org/D138665
2022-11-24 14:51:46 +00:00
Guillaume Chatelet e647b4f519 [reland][Alignment][NFC] Use the Align type in MCSection
Differential Revision: https://reviews.llvm.org/D138653
2022-11-24 13:19:18 +00:00
Kazu Hirata 34bcadc38c Use std::nullopt_t instead of NoneType (NFC)
This patch replaces those occurrences of NoneType that would trigger
an error if the definition of NoneType were missing in None.h.

To keep this patch focused, I am deliberately not replacing None with
std::nullopt in this patch or updating comments.  They will be
addressed in subsequent patches.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716

Differential Revision: https://reviews.llvm.org/D138539
2022-11-23 14:16:04 -08:00
Nico Weber 4ffc6f3b83 [bolt] Stop setting config.llvm_plugin_ext in lit.site.cfg.py.in
config.llvm_plugin_ext is used by lit to set the %pluginext
substitution. bolt's tests don't use %pluginext, so they don't
need to set config.llvm_plugin_ext.

Differential Revision: https://reviews.llvm.org/D138325
2022-11-22 20:29:14 -05:00
Alexander Yermolovich 4ff1bc2f53 [BOLT][DWARF] Re-enable DWARF5 for asm-func-debug tests
Now that BOLT supports DWARF5 re-enabling it for these two tests. This is update
to https://reviews.llvm.org/D125366

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D138338
2022-11-22 16:30:33 -08:00
Nico Weber e8ce5f1ec9 [bolt] Use llvm::sys::RWMutex instead of std::shared_timed_mutex
This has the following advantages:
- std::shared_timed_mutex is macOS 10.12+ only. llvm::sys::RWMutex
  automatically switches to a different implementation internally
  when targeting older macOS versions.
- bolt only needs std::shared_mutex, not std::shared_timed_mutex.
  llvm::sys::RWMutex automatically uses std::shared_mutex internally
  where available.

std::shared_mutex and RWMutex have the same API, so no code changes
other than types and includes are needed.

Differential Revision: https://reviews.llvm.org/D138423
2022-11-21 19:24:32 -05:00
Kazu Hirata 1fa870b1bd Use None consistently (NFC)
This patch replaces NoneType() and NoneType::None with None in
preparation for migration from llvm::Optional to std::optional.

In the std::optional world, we are not guranteed to be able to
default-construct std::nullopt_t or peek what's inside it, so neither
NoneType() nor NoneType::None has a corresponding expression in the
std::optional world.

Once we consistently use None, we should even be able to replace the
contents of llvm/include/llvm/ADT/None.h with something like:

  using NoneType = std::nullopt_t;
  inline constexpr std::nullopt_t None = std::nullopt;

to ease the migration from llvm::Optional to std::optional.

Differential Revision: https://reviews.llvm.org/D138376
2022-11-20 00:24:40 -08:00
Nico Weber f65e8c3c51 [bolt] Fix std::prev()-past-begin in veneer handling code
matchLinkerVeneer() returns 3 if `Instruction` and the last
two instructions in `[Instructions.begin, Instructions.end())`
match the pattern

    ADRP  x16, imm
    ADD   x16, x16, imm
    BR    x16

BinaryContext.cpp used to use

    --Count;
    for (auto It = std::prev(Instructions.end()); Count != 0;
         It = std::prev(It), --Count) {
      ...use It...
    }

to walk these instructions. The first `--Count` skips the
instruction that's in `Instruction` instead of in `Instructions`.
The loop then walks over `Instructions`.

However, on the last iteration, this calls `std::prev()` on an
iterator that points at the container's begin(), which can blow
up.

Instead, use rbegin(), which sidesteps this issue.

Fixes test/AArch64/veneer-gold.s on a macOS host.
With this, check-bolt passes on macOS.

Differential Revision: https://reviews.llvm.org/D138313
2022-11-18 14:42:08 -05:00
Nico Weber 685f671e1d [bolt] Pass %cflags in test/X86/is-strip.s
Without this, clang builds a binary for macOS on a macOS host, and
then the linker complains that it doesn't know the -q flag.

Differential Revision: https://reviews.llvm.org/D138306
2022-11-18 12:09:07 -05:00
Nico Weber 59b9673b45 [bolt] Make test/X86/end-symbol.test not use "tac"
"tac" doesn't exist on macOS, so this makes the test pass there.

Differential Revision: https://reviews.llvm.org/D138305
2022-11-18 12:08:33 -05:00
Nico Weber 187729ce9f [bolt] Clean up lit site cfg files a bit
* Stop setting config.python_executable in Unit/lit.site.cfg.py.in.
  All other projects only set this in the main lit config, not in the
  one for unit tests. (Unit tests don't spawn Python.)
* Set config.python_executable to Python3_EXECUTABLE in main
  lit.site.cfg.py.in instead of PYTHON_EXECUTABLE. All other files
  did this in c4c3883b00.
* Stop setting enable_abi_breaking_checks, enable_backtrace, enable_shared.
  Nothing in bolt's tests (or in lit) reads them.

Differential Revision: https://reviews.llvm.org/D138299
2022-11-18 12:07:10 -05:00
Nico Weber d731d6df64 [bolt] add missing space in "llvm-bolt -help" output 2022-11-18 09:47:11 -05:00
Vladislav Khmelevsky 750d17bb72 [BOLT][runtime] Change fPIE flag to fPIC
Since instrumentation could be used on libraries we need to use fPIC,
not fPIE flag.

Differential Revision: https://reviews.llvm.org/D138099
2022-11-17 11:50:43 +04:00
Amir Ayupov dd8fd6437e [BOLT-TESTS] Follow-up to D131919
googletest was moved to third-party. Update path in BOLT's CMakeCache.

Reviewed By: #bolt, maksfb

Differential Revision: https://reviews.llvm.org/D138066
2022-11-15 14:58:50 -08:00
revunov.denis@huawei.com c92ff2a3c4 [BOLT][NFC] Fix possible use-after-free
If NewName twine has reference to the old name, then after
Section.Name = NewName.str(); this reference is invalidated,
so we cannot use NewName.str() anymore.

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D137616
2022-11-14 13:30:22 +00:00
Rafael Auler 3698994492 [BOLT] Always move JTs in jump-table=move
We should always move jump tables when requested. Previously,
we were not moving jump tables of non-simple functions in relocation
mode. That caused a bug detailed in the attached test case: in PIC
jump tables, we force jump tables to be moved, but if they are not
moved because the function is not simple, we could incorrectly update
original entries in .rodata, corrupting it under special circumstances
(see testcase).

Reviewed By: #bolt, maksfb

Differential Revision: https://reviews.llvm.org/D137357
2022-11-04 13:20:11 -07:00
Rafael Auler 687ce3dec1 [BOLT][Hugify] Fix apple builds
Fix placement of ifdefs in hugify.cpp after D129107 landed.
2022-11-04 13:10:29 -07:00
Alexey Moksyakov 1fb186198a adds huge pages support of PIE/no-PIE binaries
This patch adds the huge pages support (-hugify) for PIE/no-PIE
binaries. Also returned functionality to support the kernels < 5.10
where there is a problem in a dynamic loader with the alignment of
pages addresses.

Differential Revision: https://reviews.llvm.org/D129107
2022-11-04 15:14:21 +03:00
serge-sans-paille f71d32a0ee
Honor LLVM_LIBDIR_SUFFIX
Some distribution install libraries under lib64. LLVM supports this
through LLVM_LIBDIR_SUFFIX, have bolt do the same.

Differential Revision: https://reviews.llvm.org/D137039
2022-11-01 23:54:06 +01:00
Daniel Thornburgh 75cdab6dc2 [llvm-objdump] Add --no-print-imm-hex to tests depending on it.
This prepares for an upcoming change to make --print-imm-hex the default
behavior of llvm-objdump. These tests were updated in a semi-automatic
fashion.

See D136972 for details.
2022-10-29 15:40:26 -07:00
Hongtao Yu d5a963ab8b [PseudoProbe] Replace relocation with offset for entry probe.
Currently pseudo probe encoding for a function is like:
	- For the first probe, a relocation from it to its physical position in the code body
	- For subsequent probes, an incremental offset from the current probe to the previous probe

The relocation could potentially cause relocation overflow during link time. I'm now replacing it with an offset from the first probe to the function start address.

A source function could be lowered into multiple binary functions due to outlining (e.g, coro-split). Since those binary function have independent link-time layout, to really avoid relocations from .pseudo_probe sections to .text sections, the offset to replace with should really be the offset from the probe's enclosing binary function, rather than from the entry of the source function. This requires some changes to previous section-based emission scheme which now switches to be function-based. The assembly form of pseudo probe directive is also changed correspondingly, i.e, reflecting the binary function name.

Most of the source functions end up with only one binary function. For those don't, a sentinel probe is emitted for each of the binary functions with a different name from the source. The sentinel probe indicates the binary function name to differentiate subsequent probes from the ones from a different binary function. For examples, given source function

```
Foo() {
  …
  Probe 1
  …
  Probe 2
}
```

If it is transformed into two binary functions:

```
Foo:
   …

Foo.outlined:
   …
```

The encoding for the two binary functions will be separate:

```

GUID of Foo
  Probe 1

GUID of Foo
  Sentinel probe of Foo.outlined
  Probe 2
```

Then probe1 will be decoded against binary `Foo`'s address, and Probe 2 will be decoded against `Foo.outlined`. The sentinel probe of `Foo.outlined` makes sure there's not accidental relocation from `Foo.outlined`'s probes to `Foo`'s entry address.

On the BOLT side, to be minimal intrusive, the pseudo probe re-encoding sticks with the old encoding format. This is fine since unlike linker, Bolt processes the pseudo probe section as a whole and it is free from relocation overflow issues.

The change is downwards compatible as long as there's no mixed use of the old encoding and the new encoding.

Reviewed By: wenlei, maksfb

Differential Revision: https://reviews.llvm.org/D135912
Differential Revision: https://reviews.llvm.org/D135914
Differential Revision: https://reviews.llvm.org/D136394
2022-10-27 13:28:22 -07:00
Amir Ayupov 1464e30704 [BOLT][TEST] Add pseudoprobe-decoding tests
Upstream internal tests, leveraging llvm-profgen binaries.

Reviewed By: hoy

Differential Revision: https://reviews.llvm.org/D136729
2022-10-25 21:33:59 -07:00
Maksim Panchenko 20204db503 [BOLT] Add mold-style PLT support
mold linker creates symbols for PLT entries and that caught BOLT by
surprise. Add the support for marked PLT entries.

Fixes: #58498

Reviewed By: yota9

Differential Revision: https://reviews.llvm.org/D136655
2022-10-25 11:03:52 -07:00
Rafael Auler c0d954a068 [BOLT] Ignore duplicate global symbols
We noticed some binaries with duplicated global symbol
entries (same name, address and size). Ignore them as it is possibly a
bug in the linker, and continue processing, unless the symbol has a
different size or address.

Reviewed By: #bolt, maksfb

Differential Revision: https://reviews.llvm.org/D136122
2022-10-19 11:52:06 -07:00
Alexander Yermolovich fcd7717ddf [BOLT][DWARF] Add support for DW_FORM_addr for DW_AT_call_return_pc
GCC 12 produces DW_FORM_addr for DW_AT_call_return_pc. Added support for that.
Fixes facebookincubator/BOLT#307

Reviewed By: maksfb

Differential Revision: https://reviews.llvm.org/D136204
2022-10-19 10:44:09 -07:00
Maksim Panchenko 28d70d3f1e [BOLT][NFC] Refactor EFMM initialization
Move EFMM initialization code to emitAndLink(), where EFMM is used.

Reviewed By: yavtuk

Differential Revision: https://reviews.llvm.org/D136205
2022-10-18 20:31:10 -07:00
Maksim Panchenko bcc4c90954 [BOLT] Fix instruction encoding validation
Always use non-symbolizing disassembler for instruction encoding
validation as symbols will be treated as undefined/zeros be the encoder
and causing byte sequence mismatches.

Reviewed By: Amir

Differential Revision: https://reviews.llvm.org/D136118
2022-10-18 13:50:00 -07:00
Maksim Panchenko dc8035bddd [BOLT][NFCI] Avoid calling registerName() twice
Calling registerName() for the same symbol twice, even with a different
size, has no effect other than the lookup overhead. Avoid the
redundancy.

Fixes facebookincubator/BOLT#299

Reviewed By: Amir

Differential Revision: https://reviews.llvm.org/D136115
2022-10-17 16:16:31 -07:00
Maksim Panchenko 4d3a0cade2 [BOLT] Section-handling refactoring/overhaul
Simplify the logic of handling sections in BOLT. This change brings more
direct and predictable mapping of BinarySection instances to sections in
the input and output files.

* Only sections from the input binary will have a non-null SectionRef.
  When a new section is created as a copy of the input section,
  its SectionRef is reset to null.

* RewriteInstance::getOutputSectionName() is removed as the section name
  in the output file is now defined by BinarySection::getOutputName().

* Querying BinaryContext for sections by name uses their original name.
  E.g., getUniqueSectionByName(".rodata") will return the original
  section even if the new .rodata section was created.

* Input file sections (with relocations applied) are emitted via MC with
  ".bolt.org" prefix. However, their name in the output binary is
  unchanged unless a new section with the same name is created.

* New sections are emitted internally with ".bolt.new" prefix if there's
  a name conflict with an input file section. Their original name is
  preserved in the output file.

* Section header string table is properly populated with section names
  that are actually used. Previously we used to include discarded
  section names as well.

* Fix the problem when dynamic relocations were propagated to a new
  section with a name that matched a section in the input binary.
  E.g., the new .rodata with jump tables had dynamic relocations from
  the original .rodata.

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D135494
2022-10-13 23:10:39 -07:00
Rafael Auler 4f158995b9 [BOLT] Add pass to fix ambiguous memory references
This adds a round of checks to memory references, looking for
incorrect references to jump table objects. Fix them by replacing the
jump table reference with another object reference + offset.

This solves bugs related to regular data references in code
accidentally being bound to a jump table, and this reference being
updated to a new (incorrect) location because we moved this jump
table.

Fixes #55004

Reviewed By: #bolt, maksfb

Differential Revision: https://reviews.llvm.org/D134098
2022-10-12 18:39:50 -07:00
Rafael Auler 8d1fc45dc3 [BOLT][NFC] Refactor creation of symbol+addend references
Put code that creates references to symbol+addend behind MCPlusBuilder.
Will use this later in validate memory references pass.

Reviewed By: #bolt, maksfb, yota9

Differential Revision: https://reviews.llvm.org/D134097
2022-10-12 18:39:26 -07:00
Maksim Panchenko 978f11c8e8 [BOLT][TEST] Fix section order test
.bss section emitted by llvm-bolt (e.g. with instrumentation) is not a
real BSS section, i.e. it takes space in the output file. Hence the
order with respect to .data is not defined. Remove .bss from the test
and fix the buildbot failure.

Reviewed By: Amir

Differential Revision: https://reviews.llvm.org/D135475
2022-10-07 14:38:49 -07:00
Rafael Auler 696b8ea05f [BOLT] Testcase to repro dyn reloc bug
Add a new testcase that shows a bug in BOLT when writing out
dynamic relocations. This is currently marked as XFAIL as we work on
solving it. This bug happens when the current strategy fails to
recognize that the original dynamic relocation in the input should
reference the original .bolt.org.rodata section instead of the new one
.rodata created by BOLT after moving jump tables. This bug started
happening after 729d29e167.

Reviewed By: Amir

Differential Revision: https://reviews.llvm.org/D125941
2022-10-07 11:27:23 -07:00
Maksim Panchenko 5fca9c5763 [BOLT] Change order of new sections
While the order of new sections in the output binary was deterministic
in the past (i.e. there was no run-to-run variation), it wasn't always
rational as we used size to define the precedence of allocatable
sections within "code" or "data" groups (probably unintentionally).
Fix that by defining stricter section-ordering rules.

Other than the order of sections, this should be NFC.

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D135235
2022-10-07 11:20:42 -07:00
Maksim Panchenko 0b213c9090 [BOLT] Fix writing out unmarked .eh_frame section
When BOLT updates .eh_frame section, it concatenates newly-generated
contents (from CFI directives) with the original .eh_frame that has
relocations applied to it. However, if no new content is generated,
the original .eh_frame has to be left intact. In that case, BOLT was
still writing out the relocatable copy of the original .eh_frame section
to the new segment, even though this copy was never used and was not
even marked in the section header table.

Detect the scenario above and skip allocating extra space for .eh_frame.

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D135223
2022-10-07 11:19:51 -07:00
Maksim Panchenko c683e281cd [BOLT] Properly set _end symbol
To properly set the "_end" symbol, we need to track the last allocatable
address. Simply emitting "_end" at the end of some section is not
sufficient since the order of section allocation is unknown during the
emission step.

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D135121
2022-10-07 11:19:14 -07:00
Maksim Panchenko 3e097fab5a [BOLT][NFC] Remove text section assertion
We can emit a binary without a new text section. Hence, the text section
assertion is not needed.

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D135120
2022-10-07 11:18:37 -07:00
Sriraman Tallam 90f2c768e5 Unittest to skip padding between buildid and filenames.
Differential Revision: https://reviews.llvm.org/D130563
2022-09-30 12:44:26 -07:00
Gabriel Ravier 9966b3e728 [BOLT] Fixed some typos
I went over the output of the following mess of a command:

`(ulimit -m 2000000; ulimit -v 2000000; git ls-files -z | parallel --xargs -0 cat | aspell list --mode=none --ignore-case | grep -E '^[A-Za-z][a-z]*$' | sort | uniq -c | sort -n | grep -vE '.{25}' | aspell pipe -W3 | grep : | cut -d' ' -f2 | less)`

and proceeded to spend a few days looking at it to find probable typos
and fixed a few hundred of them in all of the llvm project (note, the
ones I found are not anywhere near all of them, but it seems like a
good start).

Reviewed By: Amir, maksfb

Differential Revision: https://reviews.llvm.org/D130824
2022-09-30 17:07:04 +02:00
Amir Ayupov 90d87dbf4b [BOLT] Report BB reordering %-age vs profiled and total number of functions
Reviewed By: spupyrev

Differential Revision: https://reviews.llvm.org/D134819
2022-09-29 12:35:45 +02:00
Rafael Auler ba9cc6537c [PERF2BOLT] Fix unittest failure
Fix failure caused by commit e549ac072b "Do not issue parsing error on
weird build ids".
2022-09-28 16:01:57 -07:00