Skip to content

noexcept changes - #56

Closed
auroraeosrose wants to merge 3 commits into
meta:masterfrom
auroraeosrose:noexcept_macro
Closed

auroraeosrose wants to merge 3 commits into
meta:masterfrom
auroraeosrose:noexcept_macro

Conversation

@auroraeosrose

Copy link
Copy Markdown

noexcept gets two macros, one for noexcept(value) and one for regular
noexcept - both are replaced with throw() which isn't an exact
replacement but should be "good enough"

noexcept gets two macros, one for noexcept(value) and one for regular
noexcept - both are replaced with throw() which isn't an exact
replacement but should be "good enough"
so we need to remove this "protect you from yourself" hack
@ghost

ghost commented Apr 7, 2015

Copy link
Copy Markdown

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

@Gownta

Gownta commented Apr 18, 2015

Copy link
Copy Markdown
Contributor

Apologies for the delay in dealing with this pull request. I'm in the middle of going through all of our still-open pull requests.

The philosophy of folly is to be on the cutting edge, so we keep it relatively up-to-date with the current language spec. So unfortunately we can't accept this pull request.

@Gownta Gownta closed this Apr 18, 2015
@Gownta Gownta mentioned this pull request Apr 18, 2015
meta-codesync Bot pushed a commit that referenced this pull request Aug 27, 2026
Summary:
On by default wherever `svmatch` is available.

`F14Chunk::tagMatchIter` computes the match as an SVE predicate via `svmatch_u8`
and then throws it away, rebuilding the same 4-bit-spaced mask the plain-NEON
path uses so it can hand back a `SparseMaskIter`. On Neoverse V2 the generated
hot path is:

```
match  p1.b, p0/z, z1.b, z0.b     // sets NZCV itself
b.eq   <no-match>                 // svptest_any is free, folds into MATCH flags
mov    z1.b, p1/z, #17            -.
shrn   v1.8b, v1.8h, #4            | five instructions purely to rebuild
fmov   x3, d1                      | the index the predicate already held
rbit   x3, x3                      |
clz    x6, x3                     -'
```

`BRKB` sets the active lanes preceding the first match, so `CNTP` of that is the
index directly. That replaces the five-instruction chain with two, and keeps the
free `b.eq` early-out off MATCH's own flags.

After the optimization, hot path becomes:
```
match   p1.b, p0/z, z1.b, z0.b
b.eq   <no-match>
brkb    p2.b, p0/z, p1.b // -> lanes before first match
cntp    x3,   p0,  p2.b // -> first-match index i
```

`svbool_t` is a sizeless type, so it cannot be a class member or travel back
inside a `std::pair` — a predicate-native `SparseMaskIter` is not expressible.
Instead this adds `F14Chunk::forEachTagMatch`, which inverts the loop so the
predicate stays in a register. Everything except the SVE2 build gets a
`FOLLY_ALWAYS_INLINE` wrapper over the existing `tagMatchIter`, so the other
backends are unchanged. `tagMatchIter` had exactly two callers, `findImpl` and
`findMatching`, and both are rewritten to take a callback.

`FOLLY_F14_SVE_PREDICATE_NATIVE_ACTIVE` is `FOLLY_NEON &&
FOLLY_ARM_FEATURE_NEON_SVE_BRIDGE && FOLLY_ARM_FEATURE_SVE2` — exactly the
condition the `svmatch` `tagMatchIter` this replaces already compiles under — so
it turns on automatically wherever that path exists and is inert everywhere
else. `svmatch` is the only SVE2-only intrinsic in the sequence — `BRKB`,
`BRKA`, `CNTP` and predicate `BIC` are all SVE1 — so SVE2 is the correct gate.
There is no build knob: the two configurations where `FOLLY_ARM_FEATURE_SVE2` is
set but the body still cannot compile — an SVE2 target whose toolchain has no
`arm_neon_sve_bridge.h`, and `__CUDACC__`, where `Portability.h` forces
`FOLLY_NEON` to 0 — keep the generic wrapper because the macro tests those two
terms as well.

== Generated code ==

Full annotated probe loop for `F14ValueMap<K, std::array<uint8_t, 1>>` `Find`,
with `findImpl` inlined, `fbcode//mode/opt`, Grace (Neoverse V2). The two
excerpts above are taken from these. The source being compiled is one iteration
of:

```
ChunkPtr chunk = chunks_ + (index & chunkMask);
if (prefetch == Prefetch::ENABLED && Chunk::kChunkStride > 64)
  prefetchAddr(chunk->itemAddr(8));
ItemIter found{};
if (chunk->forEachTagMatch(needleV, [&](unsigned i) {
      if (FOLLY_LIKELY(this->keyMatchesItem(key, chunk->item(i)))) {
        found = ItemIter{chunk, i}; return true;
      }
      return false;
    })) { return found; }
if (FOLLY_LIKELY(chunk->outboundOverflowCount() == 0)) break;
index += step;
```

=== `uint64_t` key ===

Chunk stride 256 B, item 16 B. `keyMatchesItem` is a `cmp`, so there is no call
in the loop and nothing is ever spilled.

Before, `322d80 .. 322ddc`, 24 instructions:

```
  322d4c:  whilelo p0.b, wzr, w9         ; governing predicate, hoisted
  322d54:  mov x12, #0x7fffffffffffffff  ; mask constant, hoisted
loop:
  322d80:  and x2, x0, x1                ; index & chunkMask
  322d84:  lsl x3, x2, #8                ;   * sizeof(Chunk) = 256
  322d88:  add x2, x18, x3               ; chunk
  322d8c:  prfm pldl1keep, [x2, #144]    ; prefetchAddr(chunk->itemAddr(8))
  322d90:  ld1b {z1.b}, p0/z, [x18, x3]  ; tags_[0..15]
  322d94:  match p1.b, p0/z, z1.b, z0.b  ; svmatch_u8 -- also sets NZCV
  322d98:  b.eq 322dcc                   ; svptest_any() == false (free)
  322d9c:  mov z1.b, p1/z, #17           ; -. 0x11 per matching lane
  322da0:  shrn v1.8b, v1.8h, #4         ;  | narrow to 4 bits/lane
  322da4:  fmov x3, d1                   ;  | predicate -> GPR, the long way
  322da8:  rbit x3, x3                   ;  |
  322dac:  clz x4, x3                    ; -' x4 = first match index * 4
  322db0:  add x5, x2, x4, lsl #2        ; item = chunk + i*16
  322db4:  ldr x6, [x5, #16]             ; keyMatchesItem
  322db8:  cmp x15, x6
  322dbc:  b.eq 322de4                   ; hit -> ItemIter{chunk, i}
  322dc0:  lsr x4, x12, x4               ; SparseMaskIter advance; needs the
  322dc4:  ands x3, x4, x3               ;   hoisted 0x7fff.. constant
  322dc8:  b.ne 322dac                   ; next tag match
  322dcc:  ldrb w2, [x2, #15]            ; outboundOverflowCount()
  322dd0:  cbz w2, 322e44                ; == 0 -> break
  322dd4:  subs x17, x17, #0x1
  322dd8:  add x0, x16, x0               ; index += step
  322ddc:  b.ne 322d80
```

After, `3229c8 .. 322a1c`, 22 instructions:

```
  322998:  whilelo p0.b, wzr, w9         ; governing predicate, hoisted
loop:
  3229c8:  and x1, x18, x0               ; index & chunkMask
  3229cc:  lsl x2, x1, #8                ;   * sizeof(Chunk) = 256
  3229d0:  add x1, x17, x2               ; chunk
  3229d4:  prfm pldl1keep, [x1, #144]    ; prefetchAddr(chunk->itemAddr(8))
  3229d8:  ld1b {z1.b}, p0/z, [x17, x2]  ; tags_[0..15]
  3229dc:  match p1.b, p0/z, z1.b, z0.b  ; svmatch_u8 -- also sets NZCV
  3229e0:  b.eq 322a0c                   ; svptest_any() == false (free)
  3229e4:  brkb p2.b, p0/z, p1.b         ; lanes strictly before first match
  3229e8:  cntp x2, p0, p2.b             ; count them = the index
  3229ec:  ubfiz x2, x2, #4, #32         ;   * 16
  3229f0:  add x2, x1, x2                ; item = chunk + i*16
  3229f4:  ldr x3, [x2, #16]             ; keyMatchesItem
  3229f8:  cmp x14, x3
  3229fc:  b.eq 322a24                   ; hit -> ItemIter{chunk, i}
  322a00:  brka p2.b, p0/z, p1.b         ; lanes up to AND INCLUDING it
  322a04:  bics p1.b, p0/z, p1.b, p2.b   ; clear it; BICS sets the flags
  322a08:  b.ne 3229e4                   ; next tag match
  322a0c:  ldrb w1, [x1, #15]            ; outboundOverflowCount()
  322a10:  cbz w1, 322a84                ; == 0 -> break
  322a14:  subs x16, x16, #0x1
  322a18:  add x18, x15, x18             ; index += step
  322a1c:  b.ne 3229c8
```

`match` to item-address goes 6 -> 4 instructions, and the loop-invariant
`0x7fffffffffffffff` that `SparseMaskIter` needs disappears, freeing a register
across the whole loop.

=== `std::string` key ===

Chunk stride 576 B, item 40 B. Here `keyMatchesItem` is a size compare plus an
out-of-line `bcmp`, and that call is what makes the two arms diverge: under the
base AAPCS64 a normal callee clobbers all of `p0`-`p15`, so any live predicate
has to go to the stack. That is what `addvl sp, sp, #-2` reserves, and what
`str p` / `ldr p` move.

LLVM clones the probe loop here — `cbnz x21, <main loop>` at the foot of the
hash block dispatches on key length, so there is a second, call-free copy for
the empty-key case (30 -> 27 instructions, never executed by this benchmark).
The 48-instruction string-hash block that precedes the loop is byte-identical
between the two arms. Only the main loop is shown.

Before, `3235e0 .. 3236c0`, 57 instructions:

```
  323508:  addvl sp, sp, #-2             ; VL-scaled SVE spill area
  3235ac:  str p1, [x29, #-1, mul vl]    ; governing predicate spilled ONCE
loop:
  3235e0:  and x8, x28, x19
  3235e4:  mul x8, x8, x6                ; * 576
  3235e8:  add x25, x26, x8
  3235ec:  prfm pldl1keep, [x25, #336]   ; prefetchAddr(chunk->itemAddr(8))
  3235f0:  ld1b {z0.b}, p1/z, [x26, x8]  ; tags_[0..15]
  3235f4:  match p0.b, p1/z, z0.b, z1.b  ; svmatch_u8
  3235f8:  b.eq 3236b0                   ; no match
  3235fc:  mov z0.b, p0/z, #17           ; -. rebuild the nibble mask
  323600:  str w23, [sp, #44]
  323604:  str z1, [x29, #-2, mul vl]    ; needle vector spilled
  323608:  stp x12, x10, [sp, #24]
  32360c:  shrn v0.8b, v0.8h, #4         ;  |
  323610:  fmov x8, d0                   ;  |
  323614:  rbit x8, x8                   ;  |
  323618:  str x8, [sp, #56]             ; GPR spill: the reversed mask
  32361c:  clz x8, x8                    ;  -' index * 4
  323620:  mov w9, #0x28
  323624:  str x8, [sp, #48]             ; GPR spill: the index * 4
  323628:  lsr x8, x8, #2
  32362c:  umaddl x24, w8, w9, x25       ; item = chunk + i*40
  323630:  ldr x8, [x24, #24]            ; keyMatchesItem: size first
  323634:  cmp x21, x8
  323638:  b.ne 323690                   ; size mismatch -> next tag match
  32363c:  ldr x1, [x24, #16]
  323640:  mov x0, x22
  323644:  mov x2, x21
  323648:  mov x23, x22                  ; shuffle to keep x30/x22 alive
  32364c:  mov x22, x30                  ;   across the call
  323650:  bl bcmp                       ; CALL
  323654:  mov x3, #0xbfd6
  323658:  mov x1, #0xacc9
  32365c:  ldr p1, [x29, #-1, mul vl]    ; predicate refill
  323660:  movk x3, #0x7d8f, lsl #16
  323664:  movk x1, #0x962e, lsl #16
  323668:  mov x30, x22                  ; undo the shuffle
  32366c:  movk x3, #0x6556, lsl #32
  323670:  movk x1, #0x4b93, lsl #32
  323674:  mov x22, x23
  323678:  mov w6, #0x240
  32367c:  mov x5, #0xffffffffffffffff
  323680:  mov w4, #0x1
  323684:  movk x3, #0x4227, lsl #48
  323688:  movk x1, #0x8bb8, lsl #48
  32368c:  cbz w0, 323898                ; bcmp == 0 -> hit
  323690:  ldp x10, x9, [sp, #48]        ; refill index*4 and mask
  323694:  mov x8, #0x7fffffffffffffff
  323698:  ldr z1, [x29, #-2, mul vl]    ; refill needle
  32369c:  ldr w23, [sp, #44]
  3236a0:  lsr x8, x8, x10
  3236a4:  ldp x12, x10, [sp, #24]
  3236a8:  and x8, x8, x9
  3236ac:  cbnz x8, 323618               ; next tag match
  3236b0:  ldrb w8, [x25, #15]           ; outboundOverflowCount()
  3236b4:  cbz w8, 3238b8                ; == 0 -> break
  3236b8:  subs x12, x12, #0x1
  3236bc:  add x28, x28, x10             ; index += step
  3236c0:  b.ne 3235e0
```

After, `323220 .. 3232d8`, 47 instructions:

```
  323148:  addvl sp, sp, #-2             ; VL-scaled SVE spill area
  3231ec:  str p2, [x29, #-1, mul vl]    ; governing predicate spilled ONCE
loop:
  323220:  and x8, x28, x19
  323224:  mul x8, x8, x6                ; * 576
  323228:  add x27, x26, x8
  32322c:  prfm pldl1keep, [x27, #336]   ; prefetchAddr(chunk->itemAddr(8))
  323230:  ld1b {z0.b}, p2/z, [x26, x8]  ; tags_[0..15]
  323234:  match p1.b, p2/z, z0.b, z1.b  ; svmatch_u8
  323238:  b.eq 3232c8                   ; no match
  32323c:  str w23, [sp, #44]
  323240:  str z1, [x29, #-2, mul vl]    ; needle vector spilled
  323244:  stp x12, x10, [sp, #24]
  323248:  brkb p0.b, p2/z, p1.b         ; lanes before the first match
  32324c:  mov w9, #0x28
  323250:  str p1, [x29, #-2, mul vl]    ; THE NEW SPILL: match predicate
  323254:  cntp x8, p2, p0.b             ; count them = the index
  323258:  umaddl x23, w8, w9, x27       ; item = chunk + i*40
  32325c:  ldr x8, [x23, #24]            ; keyMatchesItem: size first
  323260:  cmp x21, x8
  323264:  b.ne 3232ac                   ; size mismatch -> next tag match
  323268:  ldr x1, [x23, #16]
  32326c:  mov x0, x22
  323270:  mov x2, x21                   ; no x30/x22 shuffle needed
  323274:  bl bcmp                       ; CALL
  323278:  mov x3, #0xbfd6
  32327c:  mov x1, #0xacc9
  323280:  ldr p2, [x29, #-1, mul vl]    ; governing predicate refill
  323284:  movk x3, #0x7d8f, lsl #16
  323288:  movk x1, #0x962e, lsl #16
  32328c:  mov w6, #0x240
  323290:  movk x3, #0x6556, lsl #32
  323294:  movk x1, #0x4b93, lsl #32
  323298:  mov x5, #0xffffffffffffffff
  32329c:  mov w4, #0x1
  3232a0:  movk x3, #0x4227, lsl #48
  3232a4:  movk x1, #0x8bb8, lsl #48
  3232a8:  cbz w0, 32349c                ; bcmp == 0 -> hit
  3232ac:  ldr p1, [x29, #-2, mul vl]    ; match predicate refill
  3232b0:  ldr z1, [x29, #-2, mul vl]    ; needle refill
  3232b4:  ldr w23, [sp, #44]
  3232b8:  ldp x12, x10, [sp, #24]
  3232bc:  brka p0.b, p2/z, p1.b         ; lanes up to AND INCLUDING it
  3232c0:  bics p1.b, p2/z, p1.b, p0.b   ; clear it; BICS sets the flags
  3232c4:  b.ne 323248                   ; next tag match
  3232c8:  ldrb w8, [x27, #15]           ; outboundOverflowCount()
  3232cc:  cbz w8, 3234bc                ; == 0 -> break
  3232d0:  subs x12, x12, #0x1
  3232d4:  add x28, x28, x10             ; index += step
  3232d8:  b.ne 323220
```

The trade is in one place. Before spills two GPRs (`[sp, #56]` the reversed
mask, `[sp, #48]` the index) and refills them with an `ldp`; after spills one
predicate and refills it with one `ldr`. Because the predicate carries the
iteration state directly, the five-instruction mask rebuild, the
`0x7fffffffffffffff` constant, and the four `mov`s shuffling `x22`/`x23`/`x30`
around the call all go away.

`str z1, [x29, #-2, mul vl]` and `str p1, [x29, #-2, mul vl]` read like the
second stomps the first; they do not. `mul vl` scales by VL for Z registers and
by VL/8 for P registers, so at VL=128 the two slots are `x29-32 .. x29-16` and
`x29-4 .. x29-2`.

=== Counts ===

| region | before | after | delta |
| --- | --- | --- | --- |
| `uint64_t` probe loop | 24 | 22 | -2 |
| -- match -> item address | 6 | 4 | -2 |
| -- next-match advance | 3 | 3 | 0 |
| `std::string` probe loop | 57 | 47 | **-10** |
| -- match -> `bl bcmp` | 24 | 17 | -7 |
| -- `bcmp` return -> loop-back | 28 | 25 | -3 |
| `std::string` empty-key clone | 30 | 27 | -3 |

`addvl sp, sp, #-2` is unchanged in both `std::string` arms (same SVE spill
area), while the fixed frame shrinks `0x40` -> `0x30` — exactly the 16 bytes of
GPR mask spills that go away.

For a *second* tag match in the same chunk the new path is 10 instructions
(`brkb` .. `b.ne`) against the old 8 (`clz` .. `b.ne`), because `brkb`+`cntp`
are re-executed where the old code re-entered at `clz`. False tag matches are
~1/255 per key, well below the first-match saving.

Depends on D117044647, which gates the pre-existing `svmatch` sites on SVE2 —
without it an SVE1 target cannot compile this file at all.

Result on Grace (Neoverse V2), `hash_maps_bench` find, ~5-6% faster.

Reviewed By: yfeldblum

Differential Revision: D117005509

fbshipit-source-id: 30f4dda50935e9bde72dab323408560a2c43b064
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants