[SPIR-V] Set register class on entry block i32 constants (#245610)
Without it the vreg gets the generic ID class and later i32 constant
lookups reuse it as an iID operand
This verifier failure caused the revert of switching to NewPM in #239489
[LoongArch] Lower insertion of masks into zero vectors to [x]vmsk*
[x]vmsk* already zero-extends the mask of each 128-bit lane to the lane, so
there is no need to move the mask to GPR and back:
```
xvmskltz.b $xr0, $xr0
xvpickve2gr.wu $a0, $xr0, 0
xvpickve2gr.wu $a1, $xr0, 4
bstrins.d $a0, $a1, 31, 16
bstrpick.d $a1, $a0, 15, 0
srli.d $a0, $a0, 16
xvrepli.b $xr0, 0
xvinsgr2vr.d $xr0, $a1, 0
xvinsgr2vr.d $xr0, $a0, 2
```
becomes:
[5 lines not shown]
[LoongArch] Compute known bits for [X]VMSK* nodes
The bits above the mask are known to be zero, so redundant zero-extensions
can be removed.
This turns the inversion of a mask into a XOR with a constant, which doesn't
fit in xori if the mask has more than 12 bits. Invert the mask in the vector
instead, or use NOR and BSTRPICK if that isn't possible. Likewise, compare
the inverted mask with zero instead of the mask with all ones.
```
vmskltz.b $vr0, $vr0
vpickve2gr.hu $a0, $vr0, 0
nor $a0, $a0, $zero
bstrpick.d $a0, $a0, 15, 0
```
becomes:
[9 lines not shown]
[clang-tidy] readability-enum-initial-value: fix self-reference detection (#228401)
[clang-tidy] readability-enum-initial-value: fix self-reference
detection
Fix `AllowReferencedInitialValues` failing to recognize references to
another enumerator in unscoped enums when the reference is implicitly
converted to the enum's underlying type.
Add a regression test for this case.
---
LLVM already supports enumerators that explicitly refer to another
enumerator through AllowReferencedInitialValues.
For example, the existing implementation supports:
E_a,
E_b,
E_last = E_b,
[35 lines not shown]
[RISCV] Predicate AMO patterns on Zaamo instead of A (#246235)
The AMO patterns were guarded by `+a` and the LR/SC pseudo patterns by
the absence of `+zaamo`. With `+zaamo,+zalrsc` but no `+a`, neither
matches
and `atomicrmw` fails with "Cannot select".
Guard them with `HasStdExtZaamo`, the predicate the `amo*` instructions
are defined under. The new `-a,+zaamo,+zalrsc` test runs share the `+a`
check prefixes.
Fixes #246093.
Assisted-by: TRAE CLI (GLM-5.4)
[LV] Add test command for divergent main/epilogue interleaved group widening decisions. nfc (#246022)
The new test command uses a scalar main vector loop (VF=1, UF=4) and an
epilogue vector loop with VF=2. The interleave group analysis finds a
load group with a gap that requires a scalar epilogue. Neither plan
widens it as an interleaved access (for VF=2 the cost model prefers
scalarization), but the epilogue vector loop is still forced to require
a scalar epilogue while the main loop does not.
This discrepancy in scalar epilogue requirements between the main and
epilogue loops causes an incorrect min-iteration check.
See #245054.
[CIR] Return the dominating store to the return slot directly
Classic codegen returns the stored value directly when the only store to
the return value slot dominates the return (findDominatingStoreToReturnValue
in CGCall.cpp), and then usually erases the slot. ClangIR always lowered the
slot to alloca, store, load and ret, so the LLVM IR of nearly every
scalar-returning function differed from classic codegen at -O0.
Add cir-forward-return-store to the pre-lowering pipeline, after FlattenCFG,
so -emit-cir output is unchanged and the ABI-lowered return is already in
place. It follows the classic rules: one cir.return of a value (classic has
one return block); a single store whose block is reached by following single
predecessors up from the return, or, if the slot has other uses, a store
right before the load; no volatile or atomic accesses; no record, array or
complex slots. The return takes the store's location and the slot is erased
once unused.
Assisted-by: Claude Code (claude-opus-5-5).
[llvm-size] Refactor output formatting into SizePrinter classes (NFC)
Introduce a class-based SizePrinter abstraction (SummarySizePrinter,
BerkeleySizePrinter, SysVSizePrinter, DarwinSizePrinter) to encapsulate
format-specific state and output logic instead of branching on
OutputFormat across printObject and printObjectSectionSizes.
Move per-run state (HeaderPrinted and totals) from file-scope globals
into printer instances, preparing llvm-size for cleanly adding
additional output formats such as GNU (-G / --format=gnu) and JSON.
Assisted-by: Gemini
[CIR] Mark LoweringPrepare init functions strictfp
Classic codegen's StartFunction makes a function that has no
FunctionDecl `strictfp` when the TU-wide default FP environment is
constrained: FP exceptions are not ignored or the rounding mode is
dynamic. Of those functions ClangIR creates `_GLOBAL__sub_I_*`,
`_GLOBAL__I_*`, the C++20 module initializer, `__cxx_global_var_init*`,
`__cxx_global_array_dtor*` and `__tls_init`. It marked only
`__cxx_global_var_init*`, through a `strictfp` attribute on `cir.global`,
so the other functions and the calls in them were not `strictfp`.
Classic also marks the `__dtor_*` stubs, `_GLOBAL__D_a` and
`__GLOBAL_cleanup_*`, which ClangIR does not emit.
LoweringPrepare creates these functions after CIRGen, so it reads the
TU-wide default from `#cir.lowering_options`. `no_trapping_math` already
records whether FP exceptions are ignored, and the new `rounding_math`
field mirrors `LangOptions::RoundingMath`. It is omitted when false, so
the module attribute prints as before without `-frounding-math`.
`buildRuntimeFunction` adds `strictfp` together with the other TU-wide
[8 lines not shown]
[VPlan] Move extract-lane(0, canWideIV) combine before unrolling
Some extract-lane combines can't be performed before unrolling, e.g. the one that combines it to Instruction::ExtractElement can end up extracting from a different part. But the extract-lane(0, canWideIV) can because at index 0 you're always extracting from the first part. Do this before unrolling, which improves codegen of unrolled code.
[VPlan] Remove header masks whose tail lanes aren't used
Replace a use of header-mask && x with x if the user, transitively
through freeze, logical-and, not and mask.beforefirst(freeze ...), only
reaches masks that are and'ed with the header mask again. The header
mask is a prefix and lane i of mask.beforefirst only depends on lanes
0..i, so the lanes outside of the header mask are never used.
This avoids the i1 vp.merge that EVL tail folding needs for the inner
header mask, e.g. in the early exit mask.beforefirst(freeze(header-mask
&& c)) and in switch default edges.
[VPlan] Allow tail folding early exits with side effects
Continues the work in #179595.
This allows us to tail fold e.g. the following loop:
```c
extern char a[1024], b[1024], c[1024], y[1024];
void two_guards(void) {
for (int i = 0; i < 1000; i++) {
if (a[i] == 0)
break;
if (b[i] > 3) {
if (c[i] < 9)
y[i] = 1;
}
}
}
```
[30 lines not shown]
[VPlan] Support tail folding uncountable early exits
Enables the first step towards #179595. After #237254, the MaskedHandleExitInVectorLoop early exit style uses the CFG to handle masking for early exit loops. This allows it to compose with tail folding which is also handled via the CFG in #176143.
This allows us to tail fold the following loop:
```c
extern char x[256];
int f(char * restrict y) {
for (int i = 0; i < 256; i++) {
if (x[i] == 0)
return i;
}
return -1;
}
```
Into this on RISC-V:
[18 lines not shown]
[VPlan] Use vputils::findCanonicalIVIncrement in more places. NFC
In a future patch we want to enable more scenarios with early exits and tail folding. In these scenarios the terminator will no longer be a BranchOnCount/BranchOnCond but a BranchOnTwoConds, so a bunch of canonical IV matchers break. Use vputils::findCanonicalIVIncrement instead since it searches the users of the canonical IV instead of looking at the terminator.
[VPlan] Allow early exit loops with side effects by masking in the vector loop.
When all blocks with side effects are dominated by every early exit, we can vectorize the loop if we mask off any lanes past the exit. We can do this with control flow:
```mermaid
flowchart TD
header --> exiting
exiting -->| exit mask | sideeffects --> latch --> latch.split
exiting -->| !exit mask | latch.split
```
For every early exiting block we add a branch to the latch on `!mask.beforefirst(exitcond)`. This is implemented in UncountableExitStyle::ReadOnly which is now renamed to MaskedHandleExitInVectorLoop.
This is a complementary exit style to MaskedHandleExitInScalarLoop. If any of the side effects aren't dominated by the early exit it still falls back to the scalar loop style, which is why none of the existing early exit side effect tests changed. Masking in loop is profitable for RISC-V and I'm assuming it's also profitable for AArch64 too, but if not then we can add a TTI hook for a preferred exit style.
[VPlan] Preserve SSA in early exit loops
MaskedCond is used to logically-and the condition to early exit with the current block in-mask. However because we use it in the latch block where the condition isn't defined on all paths, we have to workaround it not being in SSA in the verifier.
This fixes this by using reconstructSSA added in #212209 to preserve SSA in both the exit conditions and live outs in the exit blocks. It also means we get the right exit condition without MaskedCond and allows us to remove the MaskedCond exceptions in the verifier. MaskedCond is still kept since a followup PR will still use it.
One subtle point is that we need to freeze any branch conditions. Any lanes after and including the early exiting lane might contain poison and we can have branches on those poison lanes that didn't we didn't previously branch on in the scalar loop.
There's no miscompile on main today because we coincidentally don't keep SSA so exit conditions are read directly without being polluted by poison stemming from a branch past the early exit. But in this PR the exit conditions are polluted and we can end up with poison in the condition operand of a select, which causes an undefined result for AnyOf even if it freezes its operand. The poison in the branch will also cause problems in future if we decide to preserve uniform control flow and emit branches instead of blends. The exit_cond_merged_after_poison_branch test illustrates this.
[VPlan] Simplify BLEND X/M, false, ..., false -> M && X
If an i1 blend has false for all but one incoming value, then we can simplify this to the one non-false value. Blends by construction never have any overlapping mask values, so we don't need to worry about the order as the masks can't "shortcut" each other.
[VPlan] Move extract-lane(0, canWideIV) combine before unrolling
Some extract-lane combines can't be performed before unrolling, e.g. the one that combines it to Instruction::ExtractElement can end up extracting from a different part. But the extract-lane(0, canWideIV) can because at index 0 you're always extracting from the first part. Do this before unrolling, which improves codegen of unrolled code.
[CIR] Mark calls in strictfp functions strictfp
LangRef requires every call in a function that uses constrained FP
intrinsics to be `strictfp`, at the call site or on the callee, and
LLVM checks the call, not its caller, before it folds or simplifies a
libcall. Classic codegen gets the attribute from IRBuilder's
constrained mode. ClangIR marked only the function, so at -O1 LLVM
folded `erf(1.0)` to a constant under `FENV_ACCESS ON`.
After the conversion to the LLVM dialect, the lowering now sets
`strictfp` on every `llvm.call`, `llvm.invoke`, `llvm.inline_asm` and
`llvm.call_intrinsic` in a `strictfp` function. That covers direct and
indirect calls, invokes, inline assembly, constrained intrinsics, and
the calls that LoweringPrepare and EH lowering create. CIR does not
change, `strictfp` stays a function attribute. The marks are part of
the LLVM dialect, so they survive any route to LLVM IR.
Calls from named intrinsic ops such as `llvm.intr.fabs` and
`llvm.intr.memcpy` stay unmarked, because the LLVM dialect has no
[9 lines not shown]
[mlir][LLVM] Add strictfp to call, invoke, inline_asm and call_intrinsic
In a function that uses constrained floating-point intrinsics, LLVM
requires every call to be `strictfp`, at the call site or on the callee,
and its folders check the call before they fold or simplify a libcall.
The LLVM dialect could mark only the function, through `passthrough`, so
a frontend lowering to it had no way to mark its calls.
This adds a `strictfp` unit attribute to `llvm.call`, `llvm.invoke`,
`llvm.inline_asm` and `llvm.call_intrinsic`, the ops that model
call-site function attributes, like `convergent` and `nobuiltin`. Export
turns it into the call-site `strictfp` attribute and import reads it
back. Calls of the constrained intrinsic ops already get the attribute
from IRBuilder. Other named intrinsic ops have no call-site attributes.
Assisted-by: Claude Code (Opus 5.5).
[flang] Fix value separators for CHARACTER array output (#205303)
Fixes #192936
Undelimited character values in namelist output might not be readable as
namelist input (F'2018 13.11.4.2 NOTE 1). Emit a warning for namelist
WRITE/PRINT when a group item has CHARACTER data and DELIM= is absent or
'NONE'. DELIM='NONE' runtime output is unchanged, and a runtime test is
added for DELIM='QUOTE' namelist output and read-back.
The warning is enabled by default since the affected users are the ones
who would not opt in; it can be disabled with
-Wno-undelimited-namelist-character.