aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/MCContext.cpp
AgeCommit message (Collapse)AuthorFilesLines
2024-06-11Reland "[MC][NFC] Make ELFUniquingMap a StringMap (#95006)" (#95030)aengelke1-10/+37
This avoids std::map, which is slow, and uses a StringMap. Section name, group name, linked-to name and unique id are encoded into the key for fast lookup. This gives a measurable performance boost for applications that compile many small object files (e.g., functions in JIT compilers). --- Now also the second case works properly. That's what happens when you do that last refactoring without re-running all tests... sorry.
2024-06-10[MC] Don't evaluate name of unnamed symbols (#95021)aengelke1-5/+4
There's only one way to create unnamed symbols (createTempSymbol). Previously, the name was evaluated unconditionally, but often unnecessarily. Avoid this. Also the parameter names in the header were wrong, fix these.
2024-06-10Revert "[MC] Make ELFUniquingMap a StringMap" (#95023)aengelke1-37/+10
Reverts llvm/llvm-project#95006 Seems like there's some bug where the section name is empty in the `if (!Section.isSingleStringRef())`. Revert for now to get builds back to green.
2024-06-10[MC][NFC] Make ELFUniquingMap a StringMap (#95006)aengelke1-10/+37
This avoid std::map, which is slow, and uses a StringMap. Section name, group name, linked-to name and unique id are encoded into the key for fast lookup. This gives a measurable performance boost (>3%) for applications that compile many small object files (e.g., functions in JIT compilers).
2024-06-08[MC] Simplify Sec.getFragmentList().insert(Sec.begin(), F). NFCFangrui Song1-5/+5
Decrease the uses of getFragmentList() to make it easier to change the fragment list representation.
2024-05-14[MC] Make ELFEntrySizeMap a DenseMap (#91728)aengelke1-4/+8
There is no need for an ordered std::map and also no need to duplicate the section name, which is owned by the ELFSectionKey. Therefore, use a DenseMap instead and don't copy the string. As a further, minor performance optimization, avoid the hash table lookup in isELFGenericMergeableSection when the section name was just added. This slightly improves compilation performance in our application, where we occasionally compile many small object files.
2024-04-02[WebAssembly] Allocate MCSymbolWasm data on MCContext (#85866)Tim Neumann1-0/+5
Fixes #85578, a use-after-free caused by some `MCSymbolWasm` data being freed too early. Previously, `WebAssemblyAsmParser` owned the data that is moved to `MCContext` by this PR, which caused problems when handling module ASM, because the ASM parser was destroyed after parsing the module ASM, but the symbols persisted. The added test passes locally with an LLVM build with AddressSanitizer enabled. Implementation notes: * I've called the added method <code>allocate<b><i>Generic</i></b>String</code> and added the second paragraph of its documentation to maybe guide people a bit on when to use this method (based on my (limited) understanding of the `MCContext` class). We could also just call it `allocateString` and remove that second paragraph. * The added `createWasmSignature` method does not support taking the return and parameter types as arguments: Specifying them afterwards is barely any longer and prevents them from being accidentally specified in the wrong order. * This removes a _"TODO: Do the uniquing of Signatures here instead of ObjectFileWriter?"_ since the field it's attached to is also removed. Let me know if you think that TODO should be preserved somewhere.
2024-01-31[llvm] Use StringRef::starts_with (NFC)Kazu Hirata1-1/+1
2023-12-19[SystemZ][z/OS] Add exception handling for XPLINK (#74638)Yusra Syeda1-4/+10
Adds emitting the exception table and the EH registers for XPLINK. --------- Co-authored-by: Yusra Syeda <yusra.syeda@ibm.com>
2023-12-11[llvm] Use StringRef::{starts,ends}_with (NFC) (#74956)Kazu Hirata1-5/+5
This patch replaces uses of StringRef::{starts,ends}with with StringRef::{starts,ends}_with for consistency with std::{string,string_view}::{starts,ends}_with in C++20. I'm planning to deprecate and eventually remove StringRef::{starts,ends}with.
2023-11-11[llvm] Stop including llvm/ADT/DenseMapInfo.h (NFC)Kazu Hirata1-1/+0
Identified with clangd.
2023-08-09[MC] Restore default section classification to SectionKind::getReadOnly()Fangrui Song1-1/+1
D133456 changed the default `Kind = SectionKind::getReadOnly();` to `SectionKind::getText()` to work around a longstanding issue in producing R_RISCV_{ADD,SUB}* relocations. D155357 properly fixed the issue and the workaround is no longer needed. getReadonly() actually matches BFD better as BFD doesn't set SEC_CODE to a not-well-known section. To the best of my knowledge, this change is un-testable after D155357 removed a `getKind().isText()` use.
2023-06-20[XRay] Make xray_fn_idx entries PC-relativeFangrui Song1-1/+5
As mentioned by commit c5d38924dc6688c15b3fa133abeb3626e8f0767c (Apr 2020), PC-relative entries avoid dynamic relocations and can therefore make the section read-only. This is similar to D78082 and D78590. We cannot commit to support compiler/runtime built at different versions, so just don't play with versions. For Mach-O support (incomplete yet), we use non-temporary `lxray_fn_idx[0-9]+` symbols. Label differences are represented as a pair of UNSIGNED and SUBTRACTOR relocations. The SUBTRACTOR external relocation requires r_extern==1 (needs to reference a symbol table entry) which can be satisfied by `lxray_fn_idx[0-9]+`. A `lxray_fn_idx[0-9]+` symbol also serves as the atom for this dead-strippable section (follow-up to commit b9a134aa629de23a1dcf4be32e946e4e308fc64d). Differential Revision: https://reviews.llvm.org/D152661
2023-06-07Reland "D144999 [MC][MachO]Only emits compact-unwind format for "canonical" ↵Vy Nguyen1-0/+6
personality symbols. For the rest, use DWARFs." Reasons for rolling forward: - the crash reported from Chromium was fixed in D151824 (not related to this patch at all) - since D152824 was committed, it should now be safe to roll this forward. New change: - add an additional _ in name check This reverts commit 4980eead4d0b4666d53dad07afb091375b3a13a0.
2023-06-06[Triple] Add triple for UEFIprabhukr1-1/+1
Target triple to support "x86_64-unknown-uefi" Reviewed By: phosek Differential Revision: https://reviews.llvm.org/D131594
2023-05-23[Coverity] Constant variable guards dead code.Luo, Yuanke1-3/+0
2023-05-19Revert "[RFC][MC][MachO]Only emits compact-unwind format for "canonical" ↵Nico Weber1-6/+0
personality symbols. For the rest, use DWARFs." This reverts commit 09aaf53a05e3786eea374f3ce57574225036412d. Causes toolchain asserts building libc++ for x86_64, see https://reviews.llvm.org/D144999#4356215
2023-05-18[RFC][MC][MachO]Only emits compact-unwind format for "canonical" personality ↵Vy Nguyen1-0/+6
symbols. For the rest, use DWARFs. Details: https://github.com/rust-lang/rust/issues/102754 The MachO format uses 2 bits to encode these personality funtions, with 0 reserved for "no-personality". This means we can only have up to 3 personality. There are already three popular personalities: __gxx_personality_v0, __gcc_personality_v0, and __objc_personality_v0. As a result, any system that needs custom-personality will run into a problem. This patch implemented jyknight's proposal to simply force DWARFs for all non-canonical personality functions. Differential Revision: https://reviews.llvm.org/D144999
2023-04-25-fdebug-prefix-map=: make the last win when multiple prefixes matchFangrui Song1-2/+2
For `clang -c -g -fdebug-prefix-map=a/b=y -fdebug-prefix-map=a=x a/b/c.c`, we apply the longest prefix substitution, but GCC has always been picking the last applicable option (`a=x`, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109591). I feel that GCC's behavior is reasonable given the convention that the last value wins for the same option. Before D49466, Clang appeared to apply the shortest prefix substitution, which likely made the least sense. Reviewed By: #debug-info, scott.linder Differential Revision: https://reviews.llvm.org/D148975
2023-03-21[MCContext] Use `const Twine &` in symbol creation methods. NFCpaperchalice1-18/+15
All of these methods will invoke `getOrCreateSymbol(const Twine &Name)`, using `Twine` here makes these methods more flexible. Differential Revision: https://reviews.llvm.org/D145923
2022-12-27MC: Add .data. and .rodata. prefixes to MCContext section classificationDave Marchevsky1-0/+2
Commit 463da422f019 ("MC: make section classification a bit more thorough") changed MCContext::getELFSection section classification logic to default to SectionKind::getText (previously default was SectionKind::getReadOnly) and added some matching based on section name to determine internal section classification. The BPF runtime implements global variables using 'BPF map' datastructures, specifically the arraymap BPF map type. Global variables in a section are placed in a single arraymap value at predictable byte offsets. Variables in different sections are placed in separate arraymaps, so in this example: #define SEC(name) __attribute__((section(name))) SEC(".data.A") u32 one; SEC(".data.A") u32 two; SEC(".data.B") u32 three; SEC(".data.B") u32 four; variables one and two would correspond to some byte offsets (probably 0 and 4) in one arraymap, while three and four would be in a separate arraymap. Variables of a bpf_spin_lock type are considered to protect next-generation BPF datastructure types in the same arraymap value and there can only be a single bpf_spin_lock variable per arraymap value - and thus per section. As a result it's necessary to keep bpf_spin_locks and the datastructures they guard in separate data sections. Before the aforementioned commit, a section whose name starts with ".data." - like ".data.A" - would be classified as SectionKind::getReadOnly, whereas after it is SectionKind::getText. If 4-byte padding is required in such a section due to alignment of some symbol within it, classification of the section as SectionKind::getText will result in compilation of those variables to BPF backend failing with an error like "unable to write nop sequence of 4 bytes". This is due to nop instruction emitted in BPFAsmBackend::writeNopData being 8 bytes, so the function fails since it cannot emit a 4-byte nop instruction. Let's follow the pattern of matching section names starting with ".bss." and ".tbss." prefixes resulting in proper classification of the section as data by adding similar matches for ".data." and ".rodata." prefixes. This will bring padding behavior for these sections back to what it was before that commit and fix the crash. Differential Revision: https://reviews.llvm.org/D138477
2022-12-16std::optional::value => operator*/operator->Fangrui Song1-7/+5
value() has undesired exception checking semantics and calls __throw_bad_optional_access in libc++. Moreover, the API is unavailable without _LIBCPP_NO_EXCEPTIONS on older Mach-O platforms (see _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS). This commit fixes LLVMAnalysis and its dependencies.
2022-12-05Remove unused #include "llvm/ADT/Optional.h"Fangrui Song1-1/+0
2022-12-04[MC] llvm::Optional => std::optionalFangrui Song1-13/+12
https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-04DebugInfoMetadata: convert Optional to std::optionalKrzysztof Parzyszek1-1/+2
2022-12-02[llvm] Use std::nullopt instead of None (NFC)Kazu Hirata1-2/+3
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-11-22[XCOFF] set fragment for XMC_PR csects.esmeyi1-0/+8
Summary: -xcoff-traceback-table is a default option on AIX regardless of optimization and debug levels. An error of relocation for paired relocatable term is not yet supported in XCOFFObjectWriter::recordRelocation occurred when both of the -xcoff-traceback-table and -function-sections are enabled. The root cause is that we missed to calculate the symbols difference as absolute value before adding fixups when symbol_A without the fragment set is the csect itself and symbol_B is in it. This patch only sets the fragment for XMC_PR csects because we don't have other cases that hit this problem yet. Reviewed By: DiggerLin, hubert.reinterpretcast Differential Revision: https://reviews.llvm.org/D137230
2022-10-28Move getenv for AS_SECURE_LOG_FILE to clangBen Langmuir1-7/+1
Avoid calling getenv in the MC layer and let the clang driver do it so that it is reflected in the command-line as an -mllvm option. rdar://101558354 Differential Revision: https://reviews.llvm.org/D136888
2022-09-22MC: make section classification a bit more thoroughSaleem Abdulrasool1-1/+30
This does *NOT* change the emitted section flags in any way. This only impacts the internal classification of sections. Extend the section classification in LLVM for ELF targets. This has one important change: we now classify sections as text by default rather than readonly. This matches the behaviour for GAS better. Ensure that any section that has a writable attribute set is not treated as readonly. We also special case any section named `.debug_` which is reserved for DWARF as metadata. In the case none of the attributes are set (or because no attributes were provided), consult the section name for classification. We match the well known names and classify the section accordingly. Any remaining section is now classified as text. This change allows us to classify sections in the MC layer more precisely which is needed for subsequent changes for handling target specific behaviour. Re-apply the change that was reverted with additional changes to classify section prefixes appropriately and differentiate the TLS sections, addressing the FIXME and post-commit review comments by @MaskRay. Differential Revision: https://reviews.llvm.org/D133456 Reviewed By: @MaskRay
2022-08-24Revert "MC: make section classification a bit more thorough"Matthias Braun1-17/+1
This reverts commit 73a9dfcee24df959b59a46d75dcbdc0bcfb50fe6. We started to hit assertions when compiling & assembling separately. See comments in https://reviews.llvm.org/D131270
2022-08-19MC: make section classification a bit more thoroughSaleem Abdulrasool1-1/+17
This does *NOT* change the emitted section flags in any way. This only impacts the internal classification of sections. Extend the section classification in LLVM for ELF targets. This has one important change: we now classify sections as text by default rather than readonly. This matches the behaviour for GAS better. Ensure that any section that has a writable attribute set is not treated as readonly. We also special case any section named `.debug_` which is reserved for DWARF as metadata. In the case none of the attributes are set (or because no attributes were provided), consult the section name for classification. We match the well known names and classify the section accordingly. Any remaining section is now classified as text. This change allows us to classify sections in the MC layer more precisely which is needed for subsequent changes for handling target specific behaviour. Differential Revision: https://reviews.llvm.org/D131270 Reviewed By: @echristo
2022-08-14[DebugInfo] -fdebug-prefix-map: handle '#line "file"' for asm sourceFangrui Song1-1/+7
`getContext().setMCLineTableRootFile` (from D62074) sets `RootFile.Name` to `FirstCppHashFilename`. `RootFile.Name` is not processed by -fdebug-prefix-map and will go to DW_TAG_compile_unit's DT_AT_name and DW_TAG_label's DW_AT_decl_file. Remap `RootFile.Name`. Fix another issue reported by https://github.com/llvm/llvm-project/issues/56609 Reviewed By: #debug-info, dblaikie, raj.khem Differential Revision: https://reviews.llvm.org/D131848
2022-08-12[MCDwarf] Respect -fdebug-prefix-map= for generated assembly debug info ↵Fangrui Song1-16/+15
(DWARF v5) For generated assembly debug info, MCDwarfLineTableHeader::CompilationDir is an unmapped path set in MCContext::setGenDwarfRootFile. Remap it. A relative destination path of -fdebug-prefix-map= exposes a llvm-dwarfdump bug which joins relative DW_AT_comp_dir and directories[0]. Fix https://github.com/llvm/llvm-project/issues/56609 Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D131749
2022-07-24[MC] Remove unused renameELFSectionFangrui Song1-18/+0
2022-07-13[llvm] Use value instead of getValue (NFC)Kazu Hirata1-2/+2
2022-06-26[llvm] Use Optional::has_value instead of Optional::hasValue (NFC)Kazu Hirata1-2/+2
This patch replaces x.hasValue() with x.has_value() where x is not contextually convertible to bool.
2022-06-25Revert "Don't use Optional::hasValue (NFC)"Kazu Hirata1-7/+9
This reverts commit aa8feeefd3ac6c78ee8f67bf033976fc7d68bc6d.
2022-06-25Don't use Optional::hasValue (NFC)Kazu Hirata1-9/+7
2022-06-17[DirectX][MC] Add MC support for DXContainerChris Bieneman1-0/+26
DXContainer files resemble traditional object files in that they are comprised of parts which resemble sections. Adding DXContainer as an object file format in the MC layer will allow emitting DXContainer objects through the normal object emission pipeline. Differential Revision: https://reviews.llvm.org/D127165
2022-06-12[MC][re-land] Omit DWARF unwind info if compact unwind is present where eligibleJez Ng1-0/+6
This reverts commit d941d597837d9e1405086f008c9bd6a71e7263c9. Differential Revision: https://reviews.llvm.org/D122258
2022-06-12Revert "[MC] Omit DWARF unwind info if compact unwind is present where eligible"Jez Ng1-6/+0
This reverts commit ef501bf85d8c869248e51371f0e74bcec0e7b229.
2022-06-12[MC] Omit DWARF unwind info if compact unwind is present where eligibleJez Ng1-0/+6
Previously, omitting unnecessary DWARF unwinds was only done in two cases: * For Darwin + aarch64, if no DWARF unwind info is needed for all the functions in a TU, then the `__eh_frame` section would be omitted entirely. If any one function needed DWARF unwind, then MC would emit DWARF unwind entries for all the functions in the TU. * For watchOS, MC would omit DWARF unwind on a per-function basis, as long as compact unwind was available for that function. This diff makes it so that we omit DWARF unwind on a per-function basis for Darwin + aarch64 as well. In addition, we introduce the flag `--emit-dwarf-unwind=` which can toggle between `always`, `no-compact-unwind` (only emit DWARF when CU cannot be emitted for a given function), and the target platform `default`. `no-compact-unwind` is particularly useful for newer x86_64 platforms: we don't want to omit DWARF unwind for x86_64 in general due to possible backwards compat issues, but we should make it possible for people to opt into this behavior if they are only targeting newer platforms. **Motivation:** I'm working on adding support for `__eh_frame` to LLD, but I'm concerned that we would suffer a perf hit. Processing compact unwind is already expensive, and that's a simpler format than EH frames. Given that MC currently produces one EH frame entry for every compact unwind entry, I don't think processing them will be cheap. I tried to do something clever on LLD's end to drop the unnecessary EH frames at parse time, but this made the code significantly more complex. So I'm looking at fixing this at the MC level instead. **Addendum:** It turns out that there was a latent bug in the X86 backend when `OmitDwarfIfHaveCompactUnwind` is naively enabled, which is not too surprising given that this combination has not been heretofore used. For functions that have unwind info that cannot be encoded with CU, MC would end up dropping both the compact unwind entry (OK; existing behavior) as well as the DWARF entries (not OK). This diff fixes things so that we emit the DWARF entry, as well as a CU entry with encoding `UNWIND_X86_MODE_DWARF` -- this basically tells the unwinder to look for the DWARF entry. I'm not 100% sure the `UNWIND_X86_MODE_DWARF` CU entry is necessary, this was the simplest fix. ld64 seems to be able to handle both the absence and presence of this CU entry. Ultimately ld64 (and LLD) will synthesize `UNWIND_X86_MODE_DWARF` if it is absent, so there is no impact to the final binary size. Reviewed By: davide, lhames Differential Revision: https://reviews.llvm.org/D122258
2022-05-18[SystemZ][z/OS] Add the PPA1 to SystemZAsmPrinterYusra Syeda1-2/+5
Differential Revision: https://reviews.llvm.org/D125725
2022-04-20[SPIR-V](3/6) Add MC layer, object file support, and InstPrinterIlia Diachkov1-0/+23
The patch adds SPIRV-specific MC layer implementation, SPIRV object file support and SPIRVInstPrinter. Differential Revision: https://reviews.llvm.org/D116462 Authors: Aleksandr Bezzubikov, Lewis Crawford, Ilia Diachkov, Michal Paszkowski, Andrey Tretyakov, Konrad Trifunovic Co-authored-by: Aleksandr Bezzubikov <zuban32s@gmail.com> Co-authored-by: Ilia Diachkov <iliya.diyachkov@intel.com> Co-authored-by: Michal Paszkowski <michal.paszkowski@outlook.com> Co-authored-by: Andrey Tretyakov <andrey1.tretyakov@intel.com> Co-authored-by: Konrad Trifunovic <konrad.trifunovic@intel.com>
2022-03-29Add DXContainerChris Bieneman1-0/+5
DXIL is wrapped in a container format defined by the DirectX 11 specification. Codebases differ in calling this format either DXBC or DXILContainer. Since eventually we want to add support for DXBC as a target architecture and the format is used by DXBC and DXIL, I've termed it DXContainer here. Most of the changes in this patch are just adding cases to switch statements to address warnings. Reviewed By: pete Differential Revision: https://reviews.llvm.org/D122062
2022-03-28Apply clang-tidy fixes for readability-redundant-smartptr-get in ↵Kazu Hirata1-2/+2
MCContext.cpp (NFC)
2022-03-11[MC] Destroy WasmAllocator in MCContext::resetFangrui Song1-0/+1
To not leave lingering getWasmSection instances.
2022-02-09Cleanup LLVMMC headersserge-sans-paille1-2/+6
There's a few relevant forward declarations in there that may require downstream adding explicit includes: llvm/MC/MCContext.h no longer includes llvm/BinaryFormat/ELF.h, llvm/MC/MCSubtargetInfo.h, llvm/MC/MCTargetOptions.h llvm/MC/MCObjectStreamer.h no longer include llvm/MC/MCAssembler.h llvm/MC/MCAssembler.h no longer includes llvm/MC/MCFixup.h, llvm/MC/MCFragment.h Counting preprocessed lines required to rebuild llvm-project on my setup: before: 1052436830 after: 1049293745 Which is significant and backs up the change in addition to the usual benefits of decreasing coupling between headers and compilation units. Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup Differential Revision: https://reviews.llvm.org/D119244
2022-02-05[AIX][PowerPC][PGO] Generate .ref for some PGO sectionsWael Yehia1-0/+6
For PGO on AIX, when we switch to the linux-style PGO variable access (via _start and _stop labels), we need the compiler to generate a .ref assembly for each of the three csects: - __llvm_prf_data[RW] - __llvm_prf_names[RO] - __llvm_prf_vnds[RW] We insert the .ref inside the __llvm_prf_cnts[RW] csect so that if it's live then the 3 csects are live. For example, for a testcase with at least one function definition, when compiled with -fprofile-generate we should generate: .csect __llvm_prf_cnts[RW],3 .ref __llvm_prf_data[RW] <<============ needs to be inserted .ref __llvm_prf_names[RO] <<=========== the __llvm_prf_vnds is not always present, so we reference it only when it's present. Reviewed By: sfertile, daltenty Differential Revision: https://reviews.llvm.org/D116607
2022-01-28Emit swift5 reflection section data in dsym bundle generated by dsymutil in ↵Shubham Sandeep Rastogi1-4/+4
the Dwarf section. Add support for Swift reflection metadata to dsymutil. This patch adds support for copying Swift reflection metadata (__swift5_.* sections) from .o files to into the symbol-rich binary in the output .dSYM. The functionality is automatically enabled only if a .o file has reflection metadata sections and the binary doesn't. When copying dsymutil moves the section from the __TEXT segment to the __DWARF segment. rdar://76973336 Differential Revision: https://reviews.llvm.org/D115007