5847 Commits

Author SHA1 Message Date
Reid Kleckner
bb7242477c
[MC] Use StringTable to reduce dynamic relocations (#144202)
Dynamic relocations are expensive on ELF/Linux platforms because they
are applied in userspace on process startup. Therefore, it is worth
optimizing them to make PIE and PIC dylib builds faster. In +asserts
builds (non-NDEBUG), nikic identified these schedule class name string
pointers as the leading source of dynamic relocations. [1]

This change uses llvm::StringTable and the StringToOffsetTable TableGen
helper to turn the string pointers into 32-bit offsets into a separate
character array.

The number of dynamic relocations is reduced by ~60%:
❯ llvm-readelf --dyn-relocations lib/libLLVM.so | wc -l
381376 # before
155156 # after

The test suite time is modestly affected, but I'm running on a shared
noisy workstation VM with a ton of cores:
https://gist.github.com/rnk/f38882c2fe2e63d0eb58b8fffeab69de
Testing Time: 100.88s   # before
Testing Time: 78.50s. # after
Testing Time: 96.25s.  # before again

I haven't used any fancy hyperfine/denoising tools, but I think the
result is clearly visible and we should ship it.

[1] https://gist.github.com/nikic/554f0a544ca15d5219788f1030f78c5a
2025-06-25 05:23:11 -07:00
Matt Arsenault
b991b21874
Triple: Add BridgeOS to isOSDarwin (#145636)
This fixes a TODO and avoids a special case. Also required
hacking up a few cases to avoid asserting in codegen; it's not
confidence inspiring that there is only one codegen test using
a bridgeos triple and its specifically for the exp10 libcall
names.

This also changes the behavior, losing an extra leading _ in the
emitted name matching the other apple outputs. I have no idea if
this is right or not. IMO it's someone from apple's problem to fix
it and add appropriate test coverage, or we can rip all references
to BridgeOS out from upstream.
2025-06-25 20:06:51 +09:00
joaosaffran
770d0b028a
[DirectX] Fix order of v2::DescriptorRange (#145555)
As pointed in #145438, the order of elements in `v2::DescriptorRange` is
wrong according to the root signature file format. This changes the
order and updates the code and test to continue to pass.

Closes: #145438

---------

Co-authored-by: joaosaffran <joao.saffran@microsoft.com>
2025-06-24 14:27:58 -07:00
Fangrui Song
290fc1ea11
MC,AsmPrinter: Report redefinition error instead of crashing in more cases
* Fix the crash for `.equiv b, undef; b:` (.equiv equates a symbol to an expression and reports an error if the symbol was already defined).
* Remove redundant isVariable check from emitFunctionEntryLabel

Pull Request: https://github.com/llvm/llvm-project/pull/145460
2025-06-23 23:00:03 -07:00
Haohai Wen
9cc9efc483
[lld][COFF] Remove duplicate strtab entries (#141197)
String table size is too big for large binary when symbol table is
enabled. Some strings in strtab is same so it can be reused.

This patch revives 9ffeaaa authored by mstorsjo with the prioritized
string table builder to fix debug section name issue (see 4d2eda2
for more details).

---------

Co-authored-by: Wen Haohai <whh108@live.com>
Co-authored-by: James Henderson <James.Henderson@sony.com>
2025-06-21 13:44:10 +08:00
Fangrui Song
30350afd02 MCSpecifierExpr: Remove unused virtual functions
... now that all targets using MCSpecifierExpr have migrated to
XXXMCAsmInfo::printExpr/evaluateAsRelocatableImpl.
2025-06-16 20:58:10 -07:00
Fangrui Song
f8e0518120 MC: Adjust -show-inst output for MCExpr
This dump feature does not pass MCAsmInfo to the printer function.
When we remove MCSpecifierExpr subclasses (and the printImpl overrides),
we will not be able to print target-specific specifier strings.
Just print a textual representation.
2025-06-15 21:46:09 -07:00
Fangrui Song
72de33a406 MC: Add MCAsmInfo::evaluateAsRelocatableImpl and replace VEMCExpr with MCSpecifierExpr
Expressions with specifier can only be folded during relocation
generatin. At parse time the `MCAssembler *` argument might be null, and
targets should not rely on the evaluateAsRelocatable result.

Therefore, we can move evaluateAsRelocatableImpl from MCSpecifierExpr to
MCAsmInfo, so that targets do not need to inherit from MCSpecifierExpr.
2025-06-15 11:52:43 -07:00
Fangrui Song
254a92d49a MC: Add MCSpecifierExpr::create
as a target-agnostic implementation to replace target-specific
XXXMCExpr::create.
2025-06-15 11:41:33 -07:00
Fangrui Song
d4c7d0be1f MCObjectStreamer: Replace getAssemblerPtr with getAssembler
In general getAssemblerPtr should only be called by MCParse.
Revert some changes from https://reviews.llvm.org/D45164?id=143128
2025-06-13 19:12:12 -07:00
Fangrui Song
3b09a3d5ae MC,SPARC: Replace SparcMCExpr with MCSpecifierExpr
Add a hook printSpecifierExpr so that targets can implement
relocation specifier printing without inheriting from MCSpecifierExpr.
2025-06-13 09:36:38 -07:00
Fangrui Song
18b67a7a10 MC: Add MCAsmInfo::printExpr to replace MCExpr::print
* Make relocation specifier code closer (MCAsmInfo defines specifiers).
* MCExpr::print has an optional MCAsmInfo argument, which is
  error-prone when omitted.
* Enable MCSpecifierExpr
2025-06-13 08:58:19 -07:00
Fangrui Song
28bda77843
Introduce MCAsmInfo::UsesSetToEquateSymbol and prefer = to .set
Introduce MCAsmInfo::UsesSetToEquateSymbol to control the preferred
syntax for symbol equating. We now favor the more readable and common
`symbol = expression` syntax over `.set`. This aligns with pre- https://reviews.llvm.org/D44256 behavior.

On Apple platforms, this resolves a clang -S vs -c behavior difference (resolves #104623).

For targets whose = support is unconfirmed, UsesSetToEquateSymbol is set to false.
This also minimizes test updates.

Pull Request: https://github.com/llvm/llvm-project/pull/142289
2025-06-11 22:19:31 -07:00
Fangrui Song
ce270b495d MCExpr: Move isSymbolUsedInExpression workaround to AMDGPU
This function was a workaround used to detect cyclic dependency
(properly resolved by 343428c666f9293ae260bbcf79130562b830b268).
We do not want backends to use it. However, #112251 exposed it to MCExpr
to be reused by AMDGPU. Keep the workaround within AMDGPU to prevent
other backends from accidentally relying on it.
2025-06-08 00:02:27 -07:00
Fangrui Song
49a706d8ef MCSpecifierExpr: Make dtor defaulted 2025-06-07 23:25:29 -07:00
Fangrui Song
97a32f2ad9 MC: Add MCSpecifierExpr to unify target MCExprs
Many targets define MCTargetExpr subclasses just to encode an expression
with a relocation specifier. Create a generic MCSpecifierExpr to be
inherited instead. Migrate M68k and SPARC as examples.
2025-06-07 11:33:40 -07:00
Ivan Kosarev
bcf8ded849
[MC] Remove dead code. (#114798) 2025-06-05 22:47:36 -07:00
Kazu Hirata
34c011d544
[llvm] Use *Map::try_emplace (NFC) (#143002)
- try_emplace(Key) is shorter than insert(std::make_pair(Key, 0)).
- try_emplace performs value initialization without value parameters.
- We overwrite values on successful insertion anyway.
2025-06-05 16:14:31 -07:00
alx32
aab79c41b2
[DebugInfo] Fix issue with debug line table offsets for empty functions (#142253)
This patch addresses an issue where an anonymous DWARF line table symbol
could be inadvertently defined multiple times, leading to an "symbol ''
is already defined" error during assembly or object file emission. This
issue happens for empty functions when
`-emit-func-debug-line-table-offsets` is enabled.

The root cause is the creation of the "end sequence" entry for a DWARF
line table. This entry was sometimes created by copying the last
existing line table entry. If this last entry was a special one (created
for the purpose of marking the position in the line table stream and
having an anonymous symbol attached), the copied end-sequence entry
would also incorrectly reference this same anonymous symbol.
Consequently, when the line table was finally emitted, the DWARF
emission logic would attempt to emit a label for this anonymous symbol
twice, triggering the redefinition error.

The fix ensures that when an end-sequence line table entry is created,
it does not inherit any special stream label from the entry it might
have been based on, thereby preventing the duplicate label emission.
2025-06-03 07:33:51 -07:00
Fangrui Song
742ecfc13e [MC] Relax MCFillFragment and compute fragment offsets eagerly
This builds on top of commit 9d0754ada5dbbc0c009bcc2f7824488419cc5530
("[MC] Relax fragments eagerly") and relaxes fragments eagerly to
eliminate MCSection::HasLayout and `getFragmentOffset` overhead.
Relands 1a47f3f3db66589c11f8ddacfeaecc03fb80c510

Builds with many text sections (e.g. full LTO) shall observe a decrease
in compile time.

---

In addition, ensure `.fill` and `.space` directives with expressions are
re-evaluated during fragment relaxation, as their sizes may change.
Continue iteration to prevent stale, incorrect sizes.
This change has to be coupled with the fragment algorithm change
as otherwise the test test/MC/ELF/layout-interdependency.s would not
converge.

Fixes #123402 and resolves the root cause of #100283, building on error
postponing from commit 38b12d4a7c219b46d1cb52580cbacbdb931262f2.

For AArch64/label-arithmetic-diags-elf.s, the extra iteration
reports a .fill error early and suppresses the fixup/relocation errors.
Just split the tests.
2025-06-02 00:29:27 -07:00
Fangrui Song
7786ac077a MCContext::reset: clear RelSecnames & MacroMap
When MCContext is used for the second compile of
llvm/test/MC/ELF/twice.ll, ensure that .rel.text and .rel.eh_frame
strings do not come from the previous compilation copies.
2025-06-01 21:36:55 -07:00
Fangrui Song
2a673078b2 MC: Clear some members in reset 2025-06-01 21:20:25 -07:00
Fangrui Song
38b12d4a7c MCAssembler: Postpone errors in layout iteration
.org and .fill errors reported in a layout iteration might go away in
the final layout. Related to #100283
2025-06-01 18:27:26 -07:00
Fangrui Song
9ff30d4f1c MCParse: Disallow @ specifier in symbol equating
Relocation specifiers are attached to an instruction and cannot be used
in equating. GAS rejects `a = b@plt`. For now, handle just
MCSymbolRefExpr.
2025-06-01 15:20:16 -07:00
Fangrui Song
9297af1c41 MCExpr: Simplify and optimize equated symbol evaluation
Sym.isInSection() calls findAssociatedFragment, which traverses the
expression tree. This can be replaced by calling evaluateAsRelocatableImpl
first and then inspecting the MCValue result.
2025-06-01 14:42:41 -07:00
Fangrui Song
eedc72b45e MCSection: Replace DummyFragment with the Subsections[0] head fragment
The dummy fragment is primarily used by MCAsmStreamer::emitLabel to
track the defined state. We can replace it with an arbitrary fragment.

Remove MCDummyFragment introduced for https://github.com/llvm/llvm-project/issues/24860
2025-06-01 01:12:06 -07:00
joaosaffran
c7cbaef1e9
[DirectX] Adding support for static samplers in yaml2obj/obj2yaml (#139963)
- Adds support for static samplers ins dxcontainer binary format.
- Adds writing logic to mcdxbc
- adds reading logic to Object
- adds tests
Closes: [126636](https://github.com/llvm/llvm-project/issues/126636)

---------

Co-authored-by: joaosaffran <joao.saffran@microsoft.com>
2025-05-29 14:02:15 -07:00
joaosaffran
8e19573682
[DirectX] adding support to read/write descriptor table data using obj2yaml/yaml2obj (#138315)
Closes:
https://github.com/orgs/llvm/projects/4/views/22?sliceBy%5Bvalue%5D=joaosaffran&pane=issue&itemId=97332852&issue=llvm%7Cllvm-project%7C126635

---------

Co-authored-by: joaosaffran <joao.saffran@microsoft.com>
2025-05-29 12:21:20 -07:00
Fangrui Song
ab0931b638 MC: Set MCSection::LinkerRelaxable for linker-relaxable MCAlignFragment
Commit bb03cdcb441fd68da9d1ebb7d5f39f73667cd39c caused a Linux kernel
regression https://github.com/ClangBuiltLinux/linux/issues/2091

When a section contains linker-relaxable MCAlignmentFragment but no
linker-relaxable instructions, the RISCVAsmBackend::isPCRelFixupResolved
code path should be taken as well. The #76552 condition in the fragment
walk code will make the fixup unresolvable, leading to a relocation.
2025-05-28 22:46:37 -07:00
joaosaffran
654c4bc286
[NFC] Updating RTS0 namespace to contain all elements related to it's representation (#141173)
As requested in a previous PR, this change moves all structs related to
RTS0 to RTS0 namespace.

---------

Co-authored-by: joaosaffran <joao.saffran@microsoft.com>
2025-05-27 16:46:01 -07:00
Fangrui Song
e015626f18 MC: Allow .set to reassign non-MCConstantExpr expressions
GNU Assembler supports symbol reassignment via .set, .equ, or =.
However, LLVM's integrated assembler only allows reassignment for
MCConstantExpr cases, as it struggles with scenarios like:

```
.data
.set x, 0
.long x         // reference the first instance
x = .-.data
.long x         // reference the second instance
.set x,.-.data
.long x         // reference the third instance
```

Between two assignments binds, we cannot ensure that a reference binds
to the earlier assignment. We use MCSymbol::IsUsed and other conditions
to reject potentially unsafe reassignments, but certain MCConstantExpr
uses could be unsafe as well.

This patch enables reassignment by cloning the symbol upon reassignment
and updating the symbol table. Existing references to the original
symbol remain unchanged, and the original symbol is excluded from the
emitted symbol table.
2025-05-26 21:58:18 -07:00
Fangrui Song
76ee2d34f7 MCParser: Error when .set reassigns a non-redefinable variable
The conditions in parseAssignmentExpression are conservative. We should
also report an error when a non-redefiniable variable (e.g. .equiv
followed by .set; .weakref followed by .set).

Make MCAsmStreamer::emitLabel call setOffset to make the behavior
similar to MCObjectStreamer. `isUndefined()` can now be replaced with
`isUnset()`.

Additionally, fix an AMDGPU API user (tested by a few tests including
MC/AMDGPU/hsa-v4.s)
2025-05-26 20:19:52 -07:00
Fangrui Song
343428c666 MC: Detect cyclic dependency for variable symbols
We report cyclic dependency errors for variable symbols and rely on
isSymbolUsedInExpression in parseAssignmentExpression at parse time,
which does not catch all setVariableValue cases (e.g. cyclic .weakref).
Instead, add a bit to MCSymbol and check it when walking the variable
value MCExpr. When a cycle is detected when we have a final layout,
report an error and set the variable to a constant to avoid duplicate
errors.

isSymbolUsedInExpression is considered deprecated, but it is still used
by AMDGPU (#112251).
2025-05-26 15:08:11 -07:00
Fangrui Song
27b6ba449b MC: Improve error reporting for equated symbols and undefined labels
Currently, the code path is likely only reachable with super edge-case scenario,
but will be more reachable with the upcoming parseAssignmentExpression improvement
to address a pile of hacks.
2025-05-26 13:05:20 -07:00
Fangrui Song
9909cf5303 llvm-ml: Rework the alias directive to not use ELF-intended VK_WEAKREF
Set `WeakExternal` to disable the the expansion in MCExpr.cpp:canExpand.
2025-05-25 22:11:09 -07:00
Fangrui Song
fe518e77d3 ELFObjectWriter: Simplify STT_SECTION adjustment 2025-05-25 21:41:02 -07:00
Fangrui Song
0004c37c1c [llvm-ml] Restore VK_WEAKREF special case to fix alias.asm 2025-05-25 21:33:09 -07:00
Fangrui Song
95756e67c2 MC: Rework .weakref
Use a variable symbol without any specifier instead of VK_WEAKREF.
Add code in ELFObjectWriter::executePostLayoutBinding to check
whether the target should be made an undefined weak symbol.

This change fixes several issues:

* Unreferenced `.weakref alias, target` no longer creates an undefined `target`.
* When `alias` is already defined, report an error instead of crashing.

.weakref is specific to ELF. llvm-ml has reused the VK_WEAKREF name for
a different concept. wasm incorrectly copied the ELF implementation.
Remove it.
2025-05-25 21:09:55 -07:00
Fangrui Song
9513284f25 WinCOFFObjectWriter: Simplify code with member MCAssembler *
Similar to b65760bc7fcdee8179bf1e57fce3786737528dd8 for ELF.
2025-05-25 13:29:45 -07:00
Fangrui Song
843e362318 MachObjectWriter: Simplify Asm.getContext().reportError
Similar to b65760bc7fcdee8179bf1e57fce3786737528dd8 for ELF.
2025-05-25 13:06:14 -07:00
Fangrui Song
64390b9181 MC: Remove MCAssembler argument from addFileName 2025-05-25 12:52:35 -07:00
Fangrui Song
1193f62f7c MachObjectWriter: Remove the MCAssembler argument from getSymbolAddress 2025-05-25 12:38:08 -07:00
Fangrui Song
cb7d68a77b MCParser: Replace deprecated alias MCAsmLexer with AsmLexer 2025-05-25 12:08:40 -07:00
Fangrui Song
a0901a2f87 Replace #include MCAsmLexer.h with AsmLexer.h
MCAsmLexer.h has been made a forwarder header since #134207
2025-05-25 11:57:29 -07:00
Fangrui Song
e3e949cf5b MCELFObjectTargetWriter::needsRelocateWithSymbol: Remove MCSymbol argument
Replace MCSymbol argument with MCValue::AddSym. The minor difference in
.weakref handling is negligible, as our implementation may not fully
align with GAS, and .weakref is not used in practice.
2025-05-24 23:55:26 -07:00
Fangrui Song
63adf07555 ELFObjectWriter: Move Thumb-specific condition to ARMELFObjectWriter 2025-05-24 22:31:39 -07:00
Fangrui Song
d89084cf97 MCAssembler: Add reportError to simplify getContext().reportError 2025-05-24 22:00:55 -07:00
Fangrui Song
fe32806d67 ELFObjectWriter: Remove the MCContext argument from getRelocType
Additionally, swap MCFixup/MCValue order to match addReloc/recordRelocation.
2025-05-24 21:48:11 -07:00
Fangrui Song
b65760bc7f MCObjectTargetWriter: Add getContext/reportError and use it for ELF
Prepare for removing MCContext from getRelocType functions.
2025-05-24 20:43:48 -07:00
Fangrui Song
7687261637 ELFObjectWriter: Remove MCAssembler * arguments 2025-05-24 20:01:00 -07:00