[KnownFPClass] add applyInputSemantics to handle NSZ and the input denormal mode (#242439)
Adds `KnownFPClass::applyInputSemantics` to correctly handle NSZ and the
input denormal mode. It will apply the input denormal mode via
`KnownFPClass::applyInputDenormalMode` first, then it will treat any
+0.0 or -0.0 (and subnormals flushed to zero) as a zero of either sign.
[KnownFPClass] Improve fadd/fsub deductions around infinity classes (#239064)
Part of https://github.com/llvm/llvm-project/issues/242935
Improved `fadd`/`fsub` deductions for ruling out infinity. For example,
`1.0 - FLT_MAX` is not negative infinity. Additionally, we don't need to
worry if one of the inputs is infinity when ruling out finite classes,
because adding or subtracting infinity will only result in infinity or
NaN.
Also adds test coverage for `fadd`/`fsub` NaN deductions.
AI Disclosure:
I used Claude Opus 5 to help write the tests and to help analyze the
correctness of the changes.
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.
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
clang: also check alloc_align and assume_aligned claims in return (#239729)
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 limit
[4 lines not shown]
[CIR][CodeGen] Assert static member calls are unexpected in EmitCXXMemberCallExpr (#237420)
### Description
In Clang AST, calls to static member functions (whether written as
`S::func()`, `s.func()`, or `p->func()`) are lowered by Sema into
ordinary `CallExpr` nodes rather than `CXXMemberCallExpr`.
Previously:
- CIR's `CIRGenFunction::emitCXXMemberCallExpr` had an `errorNYI` for
static methods, giving the incorrect impression that static member
function calls were unimplemented (leading to #237402).
- Classic Clang's `CodeGenFunction::EmitCXXMemberCallExpr`
(`CGExprCXX.cpp:200`) retained an `if (MD->isStatic())` defensive
branch.
Per [ClangIR's code duplication
guidelines](https://github.com/llvm/llvm-project/blob/main/clang/docs/CIR/CodeDuplication.md),
this patch replaces the `errorNYI` in CIR and the defensive check in
[4 lines not shown]
[SCEV] Use m_scev_MinMax in applyDivisibilityOnMinMaxExpr (NFC). (#243233)
The lambda checking for a min/max expression with a constant first
operand and one other operand is what m_scev_MinMax with m_SCEVConstant
matches. Use the matcher and take the kind from the expression. The
assertion that the constant is non-negative directly followed the check
for it, remove it. Also pass Divisor by const reference.
Part of an effort to clean up the loop guards logic.
PR: https://github.com/llvm/llvm-project/pull/243233
[libc] Fix FILE mutex, buffering (#238618)
This change fixes some small issues found in FILE while building
programs against LLVM-libc. Specifically:
1) Linux pipes sometimes give less data than was requested, but the FILE
is expected to retry until it gets a blank read.
2) FILE mutexes are expected to be reentrant.
This PR fixes these minor issues and adds appropriate tests to catch
this behavior in future.
Assisted-by: Automated tooling, human reviewed.
[InstCombine] Allow multi-use icmp in (add (..) (sext (icmp ne A, 0))) fold. (#243186)
The fold to umax(A, 1) is profitable even if the icmp has other users:
it replaces the two adds and the sext with a single umax and the icmp
stays alive for its other users.
This shows up after LoopVectorize, where the trip count
(A + 1 - umin(A, 1)) shares umin(A, 1) with the runtime check bounds.
Triggers in ClamAV and also SPEC2017:
https://github.com/dtcxzyw/llvm-opt-benchmark-nightly/pull/2051
PR: https://github.com/llvm/llvm-project/pull/243186
[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-
[libc][math] Add multiply_sub, add_multiply, and sub_multiply to multiply_add.h.
- Add fputil::multiply_sub (x * y - z), fputil::add_multiply (x + y * z), and
fputil::sub_multiply (x - y * z) alongside fputil::multiply_add (x * y + z).
- Use template-based hardware FMA dispatch and size-aware inline overloads so
explicit multiply_add<T> calls also use hardware FMA when available.
- Add NumberPair<T> add/sub overloads in double_double.h, and matching
multiply_sub, add_multiply, and sub_multiply overloads for DoubleDouble and
DyadicFloat.
- Add unit tests in libc/test/src/__support/FPUtil/multiply_add_test.cpp.
Import math/gecode from wip, update to version 6.6.0.
Gecode is an open source C++ toolkit for developing constraint-based
systems and applications. Gecode provides a constraint solver with
state-of-the-art performance while being modular and extensible.
[NVPTX] Don't crash on image handles that are not params or globals (#238504)
NVPTXReplaceImageHandles hits llvm_unreachable when a texture, sampler
or surface handle is defined by anything other than a param load,
texsurf_handles or a copy, e.g. a handle returned by a call, merged by a
phi or computed with arithmetic. Release builds go on to crash in the
AsmPrinter.
Such handles have no symbol to replace them with, so leave them in a
register, as is already done for CUDA param loads. This needs bindless
textures and surfaces, and PTX only allows texture and sampler handles
in registers in unified texture mode, so report an error on sm_2x and
for texture and sampler handles in independent texture mode instead of
emitting invalid PTX.
---------
Co-authored-by: mmalavika <mmalavika at nvidia.com>
Revert "[llvm-diff] Compare TBAA and alias scope metadata" (#243239)
Reverts llvm/llvm-project#238976
Looks like the new test broke because of a mid-air collision with
080bb047270d99b468db8ca9603ff1b320124eff.
[bazel][libc] Allow generated headers to be filtered by entrypoint (#243168)
For https://github.com/llvm/llvm-project/issues/242727. This PR splits
the `libc_generated_header` rule into two steps. Instead of directly
generating a header, we introduce two new rules: `libc_hdrgen_spec` and
`libc_generated_headers`. The former describes all the inputs to a
`hdrgen` invocation for a header without producing a file, and the
latter generates the header from the spec while letting you specify
`--entrypoints=`.
This is nice because it allows rules to share definitions for how
headers are produced while specifying their own sets of `entrypoints`.
---
**Note:** this is not exactly equivalent to how the CMake build works.
In this PR, LLVM-libc function objects are still compiled against
[unfiltered
headers](https://github.com/llvm/llvm-project/blob/cbfac21cd384f6c7c480c2d0026ecedfe2d619e7/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl#L46),
[23 lines not shown]