[clang-format] Fix a crash in UseBreakAfter on an unmatched brace (#240744)
With `BinPack: UseBreakAfter`, the loop that adds breaks between
arguments skips nested brackets by jumping to their `MatchingParen`.
When a nested bracket isn't closed on the same line, for example because
a `;` in an inactive `#else` branch splits the line, `MatchingParen` is
null and the loop dereferences it.
Stop the loop in that case. The tokens after the unclosed bracket are
nested inside it, so they aren't arguments of the outer bracket anyway.
Fixes #233301
AI-assisted; I reviewed and tested the change.
[ConstraintElim] Split signed min/max across their operands when checking a compare
The existing constraint systems only record the bounds that hold for both
operands of a min/max (smax(a, b) >= a and >= b, smin(a, b) <= a and <= b). The
other direction is a disjunction, which cannot be recorded as a fact. When a
compare of signed decompositions involves a min/max result, split it into its
operands: the decomposition is monotonic in that result, so it is the max or min
of its values on each side, and the relation holds for all or for either side
respectively.
https://alive2.llvm.org/ce/z/zsGkJ-
[CIR] Keep volatile and atomic return loads before cleanups (#242713)
When FlattenCFG routes a `cir.return` through a cleanup, it sinks a load
of an entry-block alloca that feeds the return into the block after the
cleanup. For a volatile or atomic load this moved the access past the
cleanup code. Such a load is now spilled to a temporary before the
cleanup, like any other return operand.
Assisted-by: Claude Code (claude-opus-5-5).
[AArch64] Simplify Apple runtime unroll calculation(NFC) (#242836)
- Name magic numbers, as constexpr
- Name line remainder calculations to make the semantics clear (includes
CSE for 2 calcs of InstsInLastLine)
- Use for instead of while and trim the first duplicate iteration
- Make SizeWithUC induction variable instead repeating a multiply
[libc][test] Bound mlockall memory locking and skip under sanitizers (#242736)
Shadow-memory sanitizers map terabytes of shadow memory, which is why
sanitizer_common intercepts mlockall as a no-op. Because
LIBC_NAMESPACE::mlockall invokes SYS_mlockall directly, it bypasses
sanitizer interceptors; calling mlockall(MCL_CURRENT) when
RLIMIT_MEMLOCK is unlimited or CAP_IPC_LOCK is held attempts to
populate and wire-lock the shadow region, exhausting physical RAM.
Define LIBC_HAS_SANITIZER in sanitizer.h for shadow-memory sanitizers
(ASan, MSan, TSan, and HWASan) and skip LlvmLibcMlockTest.MLockAll
under sanitizers. For unprivileged non-sanitizer builds (without
CAP_IPC_LOCK), also cap soft RLIMIT_MEMLOCK to 64 MiB inside MLockAll
so mlockall fails fast with ENOMEM if multi-gigabyte virtual arenas are
mapped on hosts with RLIM_INFINITY.
Assisted-by: Gemini
[GlobalMerge] Add -global-merge-reorder-only option
In reorder-only mode the pass makes the same grouping decisions but, instead
of merging, only places the affected globals next to each other so each keeps
its own symbol. Off by default.
rdar://192416322
[libc][math] Add do not optimize macros and guard some double-double functions.
Guard some double-double computations against -(a * b) -> a * (-b) optimizations from compilers.
hwpmc: call the thread-create hook for Linux and kernel threads
linux_clone_thread() and kthread_add() did not call the
PMC_FN_THR_CREATE hook. Their new threads had no hwpmc thread
descriptor. If the process had a sampling PMC, the first switch-in
of such a thread hit KASSERT(pt != NULL) in pmc_process_csw_in().
An unprivileged user could panic an INVARIANTS kernel with
"pmcstat -P" on a threaded Linux program.
Call the hook in both functions, as thread_create() does.
kern_kthread.c did not include opt_hwpmc_hooks.h, so its hwpmc code
never compiled. Include it, and use newtd in kthread_add1().
Remove the exit log call from kthread_exit(), because thread_exit()
already logs the exit.
Reported by: llm-postcr
Reviewed by: kib, mhorne
Fixes: 6161b98c9935 ("hwpmc: Implement per-thread counters for PMC sampling")
Differential Revision: https://reviews.freebsd.org/D61524
[CIR][SystemZ][NFC] Add SystemZTargetCIRGenInfo (#237828)
Adds implementation for SystemZ TargetCIRGenInfo (following the
structure used in #215424). getTargetCIRGenInfo() had no SystemZ case so
it fell through to x86_64 target info.
The only hook in SystemZTargetCodeGenInfo in classic code gen that has a
CIR equivalent is setTargetAttributes. setTargetAttributes adds the
s390x-visible-vector-ABI module flag which CIR doesn't emit yet. This is
my intended next PR. For now, I add a MissingFeatures marker.
The other classic hook testFPKind has no counterpart in TargetCIRGenInfo
yet.
z/OS uses XPLINK ABI which has its own target info in classic codegen.
For now, z/OS uses the SystemZ ELF target info as a placeholder. I
marked it with targetCIRGenInfoOS() for follow up.
NFC.
[4 lines not shown]
[flang][parser] Parse the !DIR$ ALIGN compiler directive (#236444)
Parse a per-variable alignment directive in two spellings, `!DIR$
ALIGN(n) :: name-list` and the Intel-compatible `!DIR$ ATTRIBUTES ALIGN
: n :: name-list`, into one new `CompilerDirective::Align` node holding
the value and the names. Unparsing always emits the first spelling,
which becomes the canonical form (module files will use it). A bare
`!DIR$ ALIGN` or a token that merely starts with `ALIGN` still falls
through to the generic name/value directive parser, and `!DEC$` lines
remain comments.
Nothing acts on the directive yet. To make sure no revision silently
accepts and ignores it, name resolution warns that `!DIR$ ALIGN` is
recognized but not yet honored; semantics (applicability checks, value
validation, module files) and lowering follow in separate patches, and
the lowering patch removes the warning.
This is the first front-end patch of the directive proposed in the RFC
https://discourse.llvm.org/t/rfc-a-directive-to-set-the-alignment-of-variables-in-flang/92619;
[2 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
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.
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
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.)
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]
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
[AMDGPU] Use bundle-aware iterator in matchVccMask (#243188)
The backward scan from the S_AND started at A.getReverseIterator(),
which is a not bundle-aware and can step into a preceding BUNDLE,
triggering the MachineInstrBundleIterator assertion. So, we shall start
from a MachineBasicBlock::reverse_iterator instead.
Fixes https://lab.llvm.org/buildbot/#/builders/10/builds/39523
(#241405).
Assisted-by: Cursor (Claude Opus 5.5)