clang: also check alloc_align and assume_aligned claims in return
Prior to this patch, we only check alloc_align and assume_aligned claims
in the caller. We are about to thread alloc_align information down into
LLVM, which would then have a chance to defeat the sanitizer checks by
performing optimizations. To avoid that, we also check alignment claims
in the called function prior to return.
Per review feedback on D121629:
- Also verify the alignment required by `AssumeAlignedAttr` in
`EmitReturnValueCheck`.
- Add a new `ReturnAlignmentAssumption` (`return_alignment_assumption`)
sanitizer handler in clang and compiler-rt that accepts the dynamic
return statement `SourceLocation` pointer loaded from `ReturnLocation`.
- Only emit the sanitizer check (and not a redundant `llvm.assume`) in
`EmitReturnValueCheck`.
This patch is originally from 2023 and was rebased and revised with
assistance from an LLM. I have manually reviewed the diffs to the
[5 lines not shown]
[NFC] clang: simplify emitAlignmentAssumptionCheck
Move the alignment check condition (`ptrtoint`, optional offset subtraction,
mask, and `icmp eq 0`) into `CodeGenFunction::emitAlignmentAssumptionCheck`
and emit the sanitizer check before `Builder.CreateAlignmentAssumption`.
This removes the need to pass `TheCheck` and `Assumption` into
`emitAlignmentAssumptionCheck` (or temporarily unlink and re-insert
`Assumption`) and prepares `emitAlignmentAssumptionCheck` to be called
directly without an accompanying `@llvm.assume`.
This patch is new since this line of work was originally authored in
2023. It was largely written by an LLM in response to review feedback
for the next patch, but I hand-reviewed the content and it looks
reasonable enough (with the same disclaimer as other patches that I
know nothing about clang.)
CGCall: emit allocptr attributes for operator delete
With this change, we're almost ready to drop the entire table out of
MemoryBuiltins.cpp and rely solely on attributes to detect allocator
functions.
Per review feedback on D123085, emit `allocptr` in
`CodeGenModule::ConstructAttributeList` so that both the function
declaration and all calls to replaceable global `operator delete` /
`operator delete[]` receive the attribute.
This patch is originally from 2023 and was rebased and revised with
assistance from an LLM. I have manually reviewed the diffs to the
limit of my abilities (I don't do much in LLVM, and this is my first
time touching clang in a meaningful way) and they appear sensible. The
commit messages are entirely written by me.
Differential Revision: https://reviews.llvm.org/D123085
SimplifyLibCalls: preserve allocation attributes in optimizeNew
When `LibCallSimplifier::optimizeNew` rewrites an `operator new` call into a
`__hot_cold_t` allocation call, forward the allocation attributes
(`allockind`, `allocsize`, `"alloc-family"`, and parameter `allocalign`) from
the original call onto the synthesized call instruction alongside its metadata.
This is a new patch since 2023 (unlike most of the rest of the stack)
and allows the next patch to remove more specialized C++ knowledge
from the LLVM layer, instead leaving it in clang where it seems more
sensible.
This patch was authored with the assistance of an LLM and was reviewed
by me before posting.
clang: emit allocalign to LLVM for alloc_align attributes
LLVM is growing knowledge of this, so we should inform it when we have
the information available. As a bonus, since we now check return
alignments in the callee as of an earlier patch of mine, we can now drop
the redundant llvm.assume() that this previously emitted.
This patch is originally from 2023 and was rebased and revised with
assistance from an LLM. I have manually reviewed the diffs to the
limit of my abilities (I don't do much in LLVM, and this is my first
time touching clang in a meaningful way) and they appear sensible. The
commit messages are entirely written by me.
Differential Revision: https://reviews.llvm.org/D119271
CGExprCXX: emit family and allockind attributes
This allows us to remove the special knowledge about C++ allocation from
LLVM's MemoryBuiltins.cpp, instead trusting attributes to communicate
the important properties.
Annoyingly, clang has to grow a little bit of hard-coded knowledge about
mangled symbol names in order to actually get the most-basic-matching
::operator::new when working out the alloc-family attribute. I spent
some time trying to do this "right" by looking up the available
operators new and finding the best one, but that's super tricky and this
has the virtue of working and being pretty obviously correct when
compared to the old code.
Happily, clang now emits allocator family attributes for custom
allocators like the `__hot_cold_t` overloads in `new_hot_cold.cpp`
(which `isReplaceableGlobalAllocationFunction` in clang recognizes),
allowing us to remove that special knowledge from LLVM.
[7 lines not shown]
CodeGen: hand assume_aligned tests to the care of update_cc_test_checks
My previous change already landed upstream for the two alloc_align
tests. Since a later patch's review feedback requested also checking
AssumeAlignedAttr in EmitReturnValueCheck, convert the two
corresponding assume_aligned tests (and refresh the alloc_align tests)
using update_cc_test_checks.py first so that the functional changes in
the next patch are obvious.
This patch is originally from 2023 and was rebased and revised with
assistance from an LLM. I have manually reviewed the diffs to the
limit of my abilities (I don't do much in LLVM, and this is my first
time touching clang in a meaningful way) and they appear sensible. The
commit messages are entirely written by me.
Differential Revision: https://reviews.llvm.org/D152400
[SimplifyCFG] Fix incorrect merging of `invoke`s (#237431)
After #230789 was merged, it exposed a preexisting miscompile in the
pipeline where `SimplifyCFG` would incorrectly merge `invoke`
instructions resulting in the output of the incorrect `invoke`
instruction being used. One instance where this occurs is in loops. To
fix this, we only merge invokes where:
* The only uses of the `invoke`s are phi nodes
* The incoming edges of all of the phi node's usages of the `invoke`'s
result is the same `BasicBlock` parent of the `invoke`.
godbolt showing existing miscompile: https://godbolt.org/z/56a1ossEq
Assisted-by: Claude
[SystemZ][z/OS] Improve the HLASM output
HLASM uses a fixed block format, however, in USS this is not fully needed,
and trailing spaces can be omitted. Thus, the padding of lines (which also
did not cover all lines) is removed. Also, there are more limititations on
the continuation character. For example, a closing parenthesis cannot be used
as continuation character if it is part of the current statement. The solution
is to limit the continuation character only to characters valid in an ordinary
symbol, and use a `X` in all other cases.
[lldb] Preserve module specs for embedded objects (#239789)
`SymbolVendorELF` built a new `ModuleSpec` from the object file's path,
UUID and symbol file, dropping the module's object offset, size and
data. `SymbolLocatorDefault::LocateExecutableObjectFile` then called
`ObjectFile::GetModuleSpecifications(file, 0, 0)`, which parses the
backing file from offset zero. For modules that are slices of a larger
file (for example GPU device images inside a core file), every module
re-parsed the whole container, and the result could never match the
slice anyway.
Co-authored-by: Bar Soloveychik <barsolo at fb.com>
[MachineLICM] Track peak register pressure when hoisting
The pressure check in MachineLICM only looks at the estimates saved in
BackTrace. These record pressure at block boundaries, so they can miss an
earlier peak in the current BB or an ancestor BB. If pressure falls before the
candidate, the check can allow it to move even though its def then needs to stay
live across that earlier peak. This can push pressure beyond the limit and cause
spills.
This PR adds PeakPressure alongside RegPressure and saves the previous peak with
the existing boundary estimate in BackTrace. The pressure check considers the
current BB's peak and both saved estimates. Normal hoists adjust the current and
saved peaks, and scope exit restores the previous peak. Preheader scanning skips
peak updates and seeds the starting pressure from its final estimate.
Fixes LCOMPILER-3233.
This PR was assisted by AI but I reviewed all the changes.
[NFC][InstCombine] Add a test for call metadata when dropping the kcfi operand bundle (#242424)
InstCombine drops the "kcfi" operand bundle from calls that have become
direct. The test shows the current behavior: the call's metadata (!prof,
!callsite, !memprof) is dropped along with the bundle.
This is a pre-commit test for https://github.com/llvm/llvm-project/pull/242427
[CodeGen] Declare the remaining RegisterCodeGenFlags options in TableGen (#242412)
Move remaining options into CodeGenToolOptions and remove
codegen::RegisterCodeGenFlags.
Remove the getters without outside readers: getRelocModel, getCodeModel,
etc.
LLM-aided
[dsymutil] Undo prefix mapping before resolving paths for ODR uniquing
The classic linker resolves the declaration file of a type with
realpath() to tell apart types with the same name, line and size. For
paths remapped by compilation caching, which don't exist on disk,
realpath() fails and only the file name is left, so different types
declared in headers with the same name in different directories were
merged. Undo the prefix mapping from compilation-prefix-map.json first.
[dsymutil] Support compilation-prefix-map.json from compilation caching
Compilation caching can prefix map the paths that the compiler sees, so
that cache keys don't depend on where the build happens, for example
/path/to/project becomes /^src. The build system records the mapping in
a compilation-prefix-map.json file in a parent directory of the object
files, as a JSON object from each remapped prefix to the original one.
dsymutil didn't support it, so it used the remapped paths from the
debug info as is: it failed to copy the Swift interfaces that the debug
info refers to, didn't recognize interfaces from the SDK or the
toolchain that it should skip, and wrote the remapped paths into the
dSYM.
Load the compilation-prefix-map.json closest to each object file, and
use it to find Swift interfaces and Clang modules, and to restore the
original paths in the path attributes and line tables of the linked
debug info. Clang modules use the map of the object file that refers to
them. -object-prefix-map still only applies to looking up Clang
modules.
[CIR] Wire SPIRV targets into the call-convention lowering pass (#241531)
Add a SPIR-V target to `CallConvLowering`
(`-cir-call-conv-lowering=target=spirv`), classified by
`SPIRVTargetInfo` in the LLVM ABI library.
Ensures the lowering pass correctly follow SPIR-V-specific
classification rules implemented by #224699:
- A kernel pointer argument in the default address space is passed as a
`CrossWorkgroup` (addrspace 1) pointer and cast back in the kernel.
- In device compilations, a kernel aggregate argument is passed `byval`
in addrspace 0.
- A record that cannot be copied is returned through an `sret` pointer
in the default address space.
Everything else, including the remaining kernel arguments, follows the
default classification rules `DefaultTargetInfo`.
The classifier gets the same inputs classic codegen passes under
`-fexperimental-abi-lowering`: the target address space of
[16 lines not shown]
[bazel] Default WORKSPACE_ROOT to "." in main repository (#241744)
When `llvm-project-overlay` is evaluated as the root repository rather
than an external repository, `ctx.label.workspace_root` is `""`, causing
flags such as `-I$(WORKSPACE_ROOT)/clang/lib/Basic` to expand to an
absolute root path (`-I/clang/lib/Basic`). Fall back to `"."` when
`ctx.label.workspace_root` is empty while preserving external-repository
expansion unchanged.
Assisted-by: Gemini
CodeGen: Allocate LiveIntervals ranges with BumpPtrAllocator
LiveInterval and register unit LiveRange objects were individually
heap allocated and freed. Allocate them from a BumpPtrAllocator owned
by LiveIntervals, with recyclers so removed intervals and register
unit ranges reuse their storage. The arena is reset in clear(),
keeping the first slab across functions.
Reduces llc instructions by ~0.3% on the 40 largest CTMark TUs for
x86_64 and arm64. The impact is larger for amdgpu.
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
[bazel] Load cc_library from @rules_cc in vulkan_sdk.bzl (#241739)
Explicitly load `cc_library` from `@rules_cc//cc:cc_library.bzl` in the
`BUILD` files synthesized by `vulkan_sdk_setup` so the repository builds
cleanly when native C++ rules are not autoloaded (Bazel 8/9
`--incompatible_autoload_externally=`).
Assisted-by: Gemini
AArch64: Use inline register class syntax in MIR tests
Avoid using the registers section and delete it when no longer needed.
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
[OpenMP][OMPTarget][test] Rename, strengthen and extend host-pointer FROM tests
All tests are now attach_host_ptr_from_<construct>_<case>.c:
old new change
repointed_kept.c from_basic.c check copied s.x too
restore_deferred_from_mapper.c from_exit_data_mapper_deferred.c re-point s.p, check s.x
update_strided.c from_update_strided.c fresh nowait values
from_target_overlap.c new: overlapping FROMs
from_update_mapper.c new: update via mapper
from_update_strided_2d.c new: 2-D strided update
The deferred-mapper test now re-points s.p, so it fails without the
runtime change; before, it expected the attach-time value and passed either
way. Tests whose checks assume separate host and device storage are marked
UNSUPPORTED: apu, as strided_update_multidim_gap.c is.
Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>