aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/MCObjectStreamer.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-04-12MCObjectStreamer: Refine absoluteSymbolDiff conditionFangrui Song1-12/+14
The function is called to test the fast path - when Lo/Hi are within the same fragment. This is unsafe - Lo/Hi at the begin and end of a relaxable fragment should not evaluate to a constant. However, we don't have tests that exercise the code path. Nevertheless, make the check safer and remove the now unnecessary isRISCV check (from https://reviews.llvm.org/D103539).
2025-04-05[MC] Replace getSymA()->getSymbol() with getAddSym. NFCFangrui Song1-5/+5
We will replace the MCSymbolRefExpr member in MCValue with MCSymbol. This change reduces dependence on MCSymbolRefExpr.
2025-04-05[MC] Replace getSymA()->getSymbol() with getAddSym. NFCFangrui Song1-4/+2
We will replace the MCSymbolRefExpr member in MCValue with MCSymbol. This change reduces dependence on MCSymbolRefExpr.
2025-03-23MCValue: Simplify code with getSubSymFangrui Song1-2/+2
MCValue::SymB is a MCSymbolRefExpr *, which might become MCSymbol * in the future. Simplify some code that uses MCValue::SymB.
2025-03-15[MC] evaluateAsRelocatableImpl: remove the Fixup argumentFangrui Song1-2/+2
Follow-up to d6fbffa23c84e622735b3e880fd800985c1c0072 . This commit updates all call sites and removes the argument from the function.
2025-03-11Reland: [MC] output inlined-at debug info (#106230) (#130306)Yaxun (Sam) Liu1-2/+3
Reland https://github.com/llvm/llvm-project/pull/106230 The original PR was reverted due to compilation time regression. This PR fixed that by adding a condition OutStreamer->isVerboseAsm() to the generation of extra inlined-at debug info, so that it does not affect normal compilation time. Currently MC print source location of instructions in comments in assembly when debug info is available, however, it does not include inlined-at locations when a function is inlined. For example, function foo is defined in header file a.h and is called multiple times in b.cpp. If foo is inlined, current assembly will only show its instructions with their line numbers in a.h. With inlined-at locations, the assembly will also show where foo is called in b.cpp. This patch adds inlined-at locations to the comments by using DebugLoc::print. It makes the printed source location info consistent with those printed by machine passes.
2025-03-07Revert "[MC] output inlined-at debug info (#106230)"Nikita Popov1-3/+2
This reverts commit f3dc358953a13caf7521fc615a08f6317930351c. This causes a large compile-time regression: https://llvm-compile-time-tracker.com/compare.php?from=267403442264959f6b06e227ff450c385f4b3ef2&to=f3dc358953a13caf7521fc615a08f6317930351c&stat=instructions:u
2025-03-06[MC] output inlined-at debug info (#106230)Yaxun (Sam) Liu1-2/+3
Currently MC print source location of instructions in comments in assembly when debug info is available, however, it does not include inlined-at locations when a function is inlined. For example, function foo is defined in header file a.h and is called multiple times in b.cpp. If foo is inlined, current assembly will only show its instructions with their line numbers in a.h. With inlined-at locations, the assembly will also show where foo is called in b.cpp. This patch adds inlined-at locations to the comments by using DebugLoc::print. It makes the printed source location info consistent with those printed by machine passes.
2025-03-02[MC] Move MIPS-specific gprel/tprel/dtprel from MCStreamer to MipsTargetStreamerFangrui Song1-48/+0
https://reviews.llvm.org/D23669 inappropriately added MIPS-specific dtprel/tprel directives to MCStreamer. In addition, llvm-mc -filetype=null parsing these directives will crash. This patch moves these functions to MipsTargetStreamer and fixes -filetype=null. gprel32 and gprel64, called by AsmPrinter, are moved to MCTargetStreamer.
2024-12-21[MC] Decrease direct access to getContents()Fangrui Song1-8/+8
to make it easier to store fragment contents out-of-line. Loosely based on Alexis Engelke's prototype.
2024-09-20[MC] Add .loc_label instruction (#99710)alx321-6/+10
As discussed in [the RFC](https://discourse.llvm.org/t/rfc-extending-llvm-mc-loc-directive-with-labeling-support/79608) we need a way to create labels in the assembler-generated line section in order to support the future addition of the [DW_AT_LLVM_stmt_sequence](https://discourse.llvm.org/t/rfc-new-dwarf-attribute-for-symbolication-of-merged-functions/79434) attribute. We have a similar precedent for such behavior with the [.cfi_label](https://github.com/llvm/llvm-project/pull/97922) instruction - so we add the `.loc_label THE_LABEL_NAME` instruction which: - Terminates the current line sequence in the line section - Creates a new label with the specified label name in the `.debug_line` section
2024-07-22MCObjectStreamer: Remove an unneeded getBackendPtr testFangrui Song1-2/+2
All of `MCAsmBackend`, `MCCodeEmitter`, and `MCObjectWriter` must be non-null.
2024-07-22MCAssembler: Move FileNames and CompilerVersion to MCObjectWriterFangrui Song1-3/+6
2024-07-04[MC][ELF] Emit instructions directly into fragment (#94950)Alexis Engelke1-4/+2
Avoid needless copying of instructions and fixups and directly emit into the fragment small vectors. This (optionally, second commit) also removes the single use of the MCCompactEncodedInstFragment to simplify code.
2024-06-27[MC] Remove nullable getCurrentSectionOnly use from AsmParserFangrui Song1-6/+0
We will implement getCurrentSectionOnly with `CurFrag->getParent()`, which is non-null. Eliminate a nullable use.
2024-06-27[MC] Replace one nullable getCurrentSectionOnly with CurFragFangrui Song1-1/+2
We will implement getCurrentSectionOnly with `CurFrag->getParent()`, which is non-null. Eliminate a nullable use.
2024-06-27[MC] switchSectionNoPrint: update CurFragFangrui Song1-0/+5
To ensure that CurFrag->Parent and SectionStack.back() are in sync.
2024-06-27[MC] Remove unneeded nullness check from getOrCreateDataFragment. NFCFangrui Song1-1/+1
`getCurrentFragment()` no longer returns null after 7423bf78eb53d81ce0c7b3a38e39a56341ca2a89.
2024-06-27[MC] Cache current fragment in MCStreamerFangrui Song1-1/+3
This eliminates indirection through `getCurrentSectionOnly()->curFragList()->Tail`.
2024-06-27[MC] Ensure subsections have a MCDataFragmentFangrui Song1-3/+15
Similar to 21fac2d1d060b0f9b11a746718e58d4cd1ee97e5 for sections. This makes it feasible to cache the current fragment in MCStreamer.
2024-06-27[MC,X86] emitInstruction: remove virtual function calls due to Intel JCC ErratumFangrui Song1-2/+0
https://reviews.llvm.org/D70157 (for Intel Jump Conditional Code Erratum) introduced two virtual function calls in the generic MCObjectStreamer::emitInstruction, which added some overhead. This patch removes the virtual function overhead: * Define `llvm::X86_MC::emitInstruction` that calls `emitInstruction{Begin,End}`. * Define {X86ELFStreamer,X86WinCOFFStreamer}::emitInstruction to call `llvm::X86_MC::emitInstruction` Pull Request: https://github.com/llvm/llvm-project/pull/96835
2024-06-22[MC] Remove unused MCObjectStreamer::CurSubsectionIdx. NFCFangrui Song1-2/+1
2024-06-22[MC] Change Subsection parameters from const MCExpr * to uint32_tFangrui Song1-8/+3
Follow-up to 05ba5c0648ae5e80d5afce270495bf3b1eef9af4. uint32_t is preferred over const MCExpr * in the section stack uses because it should only be evaluated once. Change the paramter type to match.
2024-06-22[MC] MCSectionSubPair: replace const MCExpr * with uint32_tFangrui Song1-14/+5
2024-06-22[MC] Remove remnant code related to pending labelsFangrui Song1-2/+0
2024-06-22[MC] Remove pending labelsFangrui Song1-98/+4
This commit removes the complexity introduced by pending labels in https://reviews.llvm.org/D5915 by using a simpler approach. D5915 aimed to ensure padding placement before `.Ltmp0` for the following code, but at the cost of expensive per-instruction `flushPendingLabels`. ``` // similar to llvm/test/MC/X86/AlignedBundling/labeloffset.s .bundle_lock align_to_end calll .L0$pb .bundle_unlock .L0$pb: popl %eax .Ltmp0: //// padding should be inserted before this label instead of after addl $_GLOBAL_OFFSET_TABLE_+(.Ltmp0-.L0$pb), %eax ``` (D5915 was adjusted by https://reviews.llvm.org/D8072 and https://reviews.llvm.org/D71368) This patch achieves the same goal by setting the offset of the empty MCDataFragment (`Prev`) in `layoutBundle`. This eliminates the need for pending labels and simplifies the code. llvm/test/MC/MachO/pending-labels.s (D71368): relocation symbols are changed, but the result is still supported by linkers.
2024-06-21[MC] emitLabelAtPos: change parameter to MCDataFragment &. NFCFangrui Song1-12/+3
emitLabelAtPos is only called by ARMELFStreamer with MCDataFragment.
2024-06-21[MC] Remove the Parent parameter from MCFragment ctor callers. NFCFangrui Song1-2/+1
2024-06-14[MC] Aligned bundling: remove special handling for RelaxAllFangrui Song1-7/+3
When both aligned bundling and RelaxAll are enabled, bundle padding is directly written into fragments (https://reviews.llvm.org/D8072). (The original motivation was memory usage, which has been achieved from different angles with recent assembler improvement). The code presents challenges with the work to replace fragment representation (e.g. #94950 #95077). This patch removes the special handling. RelaxAll still works but the behavior seems slightly different as revealed by 2 changed tests. However, most `-mc-relax-all` tests are unchanged. RelaxAll used to be the default for clang -O0. This mode has significant code size drawbacks and newer Clang doesn't use it (#90013). --- flushPendingLabels: The FOffset parameter can be removed: pending labels will be assigned to the incoming fragment at offset 0. Pull Request: https://github.com/llvm/llvm-project/pull/95188
2024-06-14[MC] Add MCFragment allocation helpersFangrui Song1-11/+18
`allocFragment` might be changed to a placement new when the allocation strategy changes. `allocInitialFragment` is to deduplicate the following pattern ``` auto *F = new MCDataFragment(); Result->addFragment(*F); F->setParent(Result); ``` Pull Request: https://github.com/llvm/llvm-project/pull/95197
2024-06-12[MC] Restore setOffset(0) in emitLabelFangrui Song1-1/+1
For bolt/test/runtime/X86/exceptions-pic.test, llvm-bolt seems to call emitLabel twice and the assert will fail. Work around it after 2cc4bc132cbcc76c5552cbc128830943ea596b3e
2024-06-12MCFragment: Initialize Offset to 0Fangrui Song1-1/+1
After 9d0754ada5dbbc0c009bcc2f7824488419cc5530 ("[MC] Relax fragments eagerly") removes the assert of Offset, it is no longer useful to initialize the member to -1. Now the symbol value estimate is more precise, which leads to slight behavior change to layout-interdependency.s.
2024-06-11[MC] Replace fragment ilist with singly-linked listsFangrui Song1-9/+2
Fragments are allocated with `operator new` and stored in an ilist with Prev/Next/Parent pointers. A more efficient representation would be an array of fragments without the overhead of Prev/Next pointers. As the first step, replace ilist with singly-linked lists. * `getPrevNode` uses have been eliminated by previous changes. * The last use of the `Prev` pointer remains: for each subsection, there is an insertion point and the current insertion point is stored at `CurInsertionPoint`. * `HexagonAsmBackend::finishLayout` needs a backward iterator. Save all fragments within `Frags`. Hexagon programs are usually small, and the performance does not matter that much. To eliminate `Prev`, change the subsection representation to singly-linked lists for subsections and a pointer to the active singly-linked list. The fragments from all subsections will be chained together at layout time. Since fragment lists are disconnected before layout time, we can remove `MCFragment::SubsectionNumber` (https://reviews.llvm.org/D69411). The current implementation of `AttemptToFoldSymbolOffsetDifference` requires future improvement for robustness. Pull Request: https://github.com/llvm/llvm-project/pull/95077
2024-06-10[MC] Remove getFragmentList uses. NFCFangrui Song1-1/+1
2024-06-10[MC] Maintain MCRelaxAll after reset() (#94945)aengelke1-1/+4
RelaxAll was set in the constructor, but cleared after reset().
2024-05-19[MC] Make UseAssemblerInfoForParsing mostly trueFangrui Song1-3/+0
Commit 6c0665e22174d474050e85ca367424f6e02476be (https://reviews.llvm.org/D45164) enabled certain constant expression evaluation for `MCObjectStreamer` at parse time (e.g. `.if` directives, see llvm/test/MC/AsmParser/assembler-expressions.s). `getUseAssemblerInfoForParsing` was added to make `clang -c` handling inline assembly similar to `MCAsmStreamer` (e.g. `llvm-mc -filetype=asm`), where such expression folding (related to `AttemptToFoldSymbolOffsetDifference`) is unavailable. I believe this is overly conservative. We can make some parse-time expression folding work for `clang -c` even if `clang -S` would still report an error, a MCAsmStreamer issue (we cannot print `.if` directives) that should not restrict the functionality of MCObjectStreamer. ``` % cat b.cc asm(R"( .pushsection .text,"ax" .globl _start; _start: ret .if . -_start == 1 ret .endif .popsection )"); % gcc -S b.cc && gcc -c b.cc % clang -S -fno-integrated-as b.cc # succeeded % clang -c b.cc # succeeded with this patch % clang -S b.cc # still failed <inline asm>:4:5: error: expected absolute expression 4 | .if . -_start == 1 | ^ 1 error generated. ``` However, removing `getUseAssemblerInfoForParsing` would make MCDwarfFrameEmitter::Emit (for .eh_frame FDE) slow (~4% compile time regression for sqlite3.c amalgamation) due to expensive `AttemptToFoldSymbolOffsetDifference`. For now, make `UseAssemblerInfoForParsing` false in MCDwarfFrameEmitter::Emit. Close #62520 Link: https://discourse.llvm.org/t/rfc-clang-assembly-object-equivalence-for-files-with-inline-assembly/78841 Pull Request: https://github.com/llvm/llvm-project/pull/91082
2024-05-16Revert "[MC] Remove UseAssemblerInfoForParsing"Nikita Popov1-1/+8
This reverts commit 03c53c69a367008da689f0d2940e2197eb4a955c. This causes very large compile-time regressions in some cases, e.g. sqlite3 at O0 regresses by 5%.
2024-05-15[MC] Remove UseAssemblerInfoForParsingFangrui Song1-8/+1
Commit 6c0665e22174d474050e85ca367424f6e02476be (https://reviews.llvm.org/D45164) enabled certain constant expression evaluation for `MCObjectStreamer` at parse time (e.g. `.if` directives, see llvm/test/MC/AsmParser/assembler-expressions.s). `getUseAssemblerInfoForParsing` was added to make `clang -c` handling inline assembly similar to `MCAsmStreamer` (e.g. `llvm-mc -filetype=asm`), where such expression folding (related to `AttemptToFoldSymbolOffsetDifference`) is unavailable. I believe this is overly conservative. We can make some parse-time expression folding work for `clang -c` even if `clang -S` would still report an error, a MCAsmStreamer issue (we cannot print `.if` directives) that should not restrict the functionality of MCObjectStreamer. ``` % cat b.cc asm(R"( .pushsection .text,"ax" .globl _start; _start: ret .if . -_start == 1 ret .endif .popsection )"); % gcc -S b.cc && gcc -c b.cc % clang -S -fno-integrated-as b.cc # succeeded % clang -c b.cc # succeeded with this patch % clang -S b.cc # still failed <inline asm>:4:5: error: expected absolute expression 4 | .if . -_start == 1 | ^ 1 error generated. ``` Close #62520 Link: https://discourse.llvm.org/t/rfc-clang-assembly-object-equivalence-for-files-with-inline-assembly/78841 Pull Request: https://github.com/llvm/llvm-project/pull/91082
2024-04-25[MC] Move setRelaxAll() calls to MCObjectStreamerFangrui Song1-0/+2
Related to clean-up opportunities discussed at #90013.
2024-02-06[MC]: Fix typo in MCObjectStreamer.cpp (#80856)Vinayak Dev1-1/+1
Fixes a typo in llvm/lib/MC/MCObjectStreamer.cpp introduced in #80162
2024-02-06[XCOFF] Add compiler version to an auxiliary symbol table entry (#80162)stephenpeckham1-1/+3
C_FILE symbols. To match the behavior of the assembler and the legacy compiler, this includes using the generic ".file" name for the C_FILE symbol and generating the actual file name in an auxiliary entry.
2024-02-05[NFC] Fix typo (#80703)stephenpeckham1-1/+1
2024-01-18Revert "[BOLT] Fix unconditional output of boltedcollection in merge-fdata ↵Amir Ayupov1-5/+0
(#78653)" This reverts commit 82bc33ea3f1a539be50ed46919dc53fc6b685da9. Accidentally pushed unrelated changes.
2024-01-18[BOLT] Fix unconditional output of boltedcollection in merge-fdata (#78653)Amir Ayupov1-0/+5
Fix the bug where merge-fdata unconditionally outputs boltedcollection line, regardless of whether input files have it set. Test Plan: Added bolt/test/X86/merge-fdata-nobat-mode.test which fails without this fix.
2023-12-07[MC] .reloc: register used symbolsFangrui Song1-2/+3
When `sym` in `.reloc ., BFD_RELOC_NONE, sym` is not referenced elsewhere, `sym` is not in the symbol table and the relocation references the null symbol. Visit the expression to fix the issue.
2023-06-29[RISCV] Make linker-relaxable instructions terminate MCDataFragmentFangrui Song1-0/+5
`MCExpr::evaluateAsAbsolute` has a longstanding bug. When the MCAssembler is non-null and the MCAsmLayout is null, it may incorrectly fold A-B even if A and B are separated by a linker-relaxable instruction. This behavior can suppress some ADD/SUB relocations and lead to wrong results if the linker performs relaxation. To fix the bug, ensure that linker-relaxable instructions only appear at the end of an MCDataFragment, thereby making them terminate the fragment. When computing A-B, suppress folding if A and B are separated by a linker-relaxable instruction. * `.subsection` now correctly give errors for non-foldable expressions. * gen-dwarf.s will pass even if we add back the .debug_line or .eh_frame/.debug_frame code from D150004 * This will fix suppressed relocation when we add R_RISCV_SET_ULEB128/R_RISCV_SUB_ULEB128. In the future, we should investigate the desired behavior for `MCExpr::evaluateAsAbsolute` when both MCAssembler and MCAsmLayout are non-null. (Note: MCRelaxableFragment is only for assembler-relaxation. If we ever need linker-relaxable MCRelaxableFragment, we would need to adjust RISCVMCExpr.cpp (D58943/D73211).) Depends on D153096 Differential Revision: https://reviews.llvm.org/D153097
2023-06-26[MC] Add SMLoc to MCCFIInstructionFangrui Song1-6/+7
to help debug and report better diagnostics for functions like relaxDwarfCallFrameFragment (D153167). In MCStreamer, some emitCFI* functions already take a SMLoc argument. Add a SMLoc argument to the remaining functions that generate a MCCFIInstruction.
2023-06-15[MC] Improve .subsection diagnosticFangrui Song1-1/+2
2023-06-15[MC] Remove an unneeded special case from MCObjectStreamer::flushPendingLabelsFangrui Song1-6/+3
We always pass a non-null F to flushPendingLabels. Wait a bit before changing the parameter to use a reference.
2023-06-14[MC] Properly report errors for .subsectionFangrui Song1-4/+9
For the out-of-range error, MCConstantExpr doesn't have a location, so we can only show "<unknown>:0:". Also, allow subsection numbers up to 2147483647, which is the maximum value GNU assembler supports. (GNU assembler also supports negative numbers.)