Repository navigation
core: a misaligned atomic traps precisely (address-misaligned) instead of touching memory - #569
Merged
Merged
Conversation
tinebp
added a commit
that referenced
this pull request
Dec 3, 2026
…#560) Bug fixes to master found while integrating the Vulkan graphics stack. Each fix carries its trigger test. - F10 common: util.h's __assert macro is VX_ASSERT, so EL9 (glibc 2.34) debug builds compile again; the uses #574 added are renamed too. - F3 trap_order: master orders a trap entry behind an earlier mcause write since #574 (the same XREG_2 slot); this adds the trigger test, tests/regression/trap_order and the trap-simd CI case. - F1 core: a workgroup barrier also drains the warp's memory requests, including those waiting at the local memory's block inputs (two LSU blocks at >= 32 warps let a peer read the old value). RTL: lsu_store_empty reaches VX_wctl_unit through VX_warp_ctl_if. SimX: the SFU gates BAR on the warp's pending level and the LSU's drained level (the ports #574 added) and on Core::mem_req_drained (switch wires, coalescer or lane adapter, local-memory inputs, MMU). A barrier arrival stalls its warp until it executes. Tests: bar_vis (bar-vis, -shared, -bank, -lmem-input(-timed), model_parity-bar-vis). - F2 lsu, simx: a fence goes out on every lane and holds the block's input until answered. Tests: fence_mp cases, vm-release. - F6 amo: compare-and-swap counters from a nonzero base, global and local memory (cas_counter, cas_counter_lmem); drops the stale note that the RTL has no local-memory AMO path. - F7 simx/cache: LLC and passthrough atomics are timed as VX_cache_amo does (parity-amo-contended(-l2), parity-amo-scatter). - F8 cache: a write-back bank admits against an exact bound of the writebacks it can still owe (#557 follow-ups): sgemv-nt16 back from #557's +2.3%, and the range assert no longer fires on a 4-core write-back L2 with two sectors per line (cache:wb_drain-l2-1). The misaligned-atomic trap (F5) is not here: #569 replaces it. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
…d of touching memory An atomic (AMO, LR, SC, compare-and-swap) whose active lane names an address not aligned to its size now raises the RISC-V address-misaligned exception: store/AMO address misaligned (6), load address misaligned (4) for LR, mepc the atomic, mtval read-only zero. On master the RTL asserts in simulation (on hardware it writes through a misaligned word) and SimX aborts. The trap is precise: no instruction after the atomic runs before the handler, and the handler may return to mepc + 4. It uses the pipeline's extension points only; no shared stage changes (coding guidelines §13). - Decode marks every atomic wstall (EXT_A only), as it does a branch. - The uop sequencer (VX_uop_amo) puts an alignment check ahead of every atomic: check [release fence] atomic [acquire fence]. The check is an ALU branch-port uop on rs1 (INST_BR_AMOCHK, INST_BR_LRCHK, two spare branch codes) that targets PC + 4 and reads the trap pseudo-register, as ECALL does, so an older mtvec write has landed when it traps. - VX_alu_int (EXT_A only) raises is_trap with cause 6 or 4 and dest = the atomic's PC when an active lane is misaligned. The check's SIMD slices and packets reach a block back to back, so one flop holds a misaligned lane of an earlier one until the last. The branch port's existing trap path writes mepc, mcause and mstatus and redirects the stalled warp; its br_hit releases the warp's fetch. - VX_lsu_block (EXT_A only) decides the same from rs1 on its own: from the packet holding the first misaligned lane on, the atomic's packets retire through the no-response path writing nothing (as a fence's skipped packets do); packets ahead of it have gone to memory. A faulted end-of-instruction packet waits for those packets' responses (fault_hold), so the next packet does not start in a slot still counting them. - SimX in lockstep: decode marks the atomic macro wstall; LsuUopGen emits the check (BrType::AMOCHK) as uop 0; the sequencer gives a wstall uop its macro's fetch release; AluUnit holds a misaligned lane per block and calls Scheduler::raise_trap; LsuUnit drops the atomic from the faulted packet on and holds behind a partly faulted one. - No VX_types change (the causes are local literals, as the ALU's other trap causes), so the resolved config and every perf golden are unchanged. Cost: the atomic stalls its warp's fetch until the check resolves, as a branch does. x32 cycles, control = master 3fb83fd with this commit's amo test text, so only the hardware and model differ: case simx rtlsim amoadd 5875 -> 8211 (+39.8%) 5888 -> 8224 (+39.7%) contended 7505 -> 8721 (+16.2%) 7522 -> 8738 (+16.2%) contended-l2 37487 -> 33539 (-10.5%) 37491 -> 37641 (+0.4%) zacas-simd 20239 -> 22129 (+9.3%) 21523 -> 21337 (-0.9%) amo list w/o trap 58579 -> 61244 (+4.5%) 62238 -> 66286 (+6.5%) sgemm 113803 -> 113803 (0.0%) 113814 -> 113814 (0.0%) With EXT_A off (the default) nothing changes: Beacon (v3.0.3) core and lmem with A off are identical to master on U55C (LUT 120383, FF 137138, 15 levels) and ASIC (723387 cells, 94018 FF, 30 stages). With A on: U55C core LUT 127131 -> 127084, FF 140274 -> 140277, 15 -> 15 levels; ASIC core 745720 -> 745233 cells, FF 95842 -> 95845, 30 -> 30 stages. Trigger test: tests/regression/amo amo_misaligned (amoadd.w, lr.w, sc.w, amocas.w at misaligned addresses; lane 1 alone misaligned; a misaligned lane opening the second LSU packet and the second SIMD slice; an aligned atomic), checking mcause, mepc ^ mtval, rd, every word of memory, and that the instruction after the atomic runs exactly once, after the handler. CI cases parity-amo-misaligned and misaligned-simd-packets (8 threads, SIMD 4, ALU and LSU lanes 2); the base, zacas, wb, threads8 and mc amo cases run it too. On master the RTL asserts "misaligned memory access" and SimX aborts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tinebp
force-pushed
the
fix/amo-misaligned-trap
branch
from
December 3, 2026 11:21
fe58030 to
8944ea8
Compare
tinebp
added a commit
that referenced
this pull request
Dec 3, 2026
…hain v3.0.4 Toolchain v3.0.4 (#566) moved the Beacon version and some instruction counts, so every golden is re-recorded on the final tree of the #560..#569 merge series, by the gates' own --update-baseline(s): - logic_gate U55C (16 entries) and beacon_asic (14): no build deeper or slower than its previous golden; LUT flat except raster +1%. - perf_gate (51 x32 entries): geomean +0.52% cycles. - dxa/wgmma (accepted): wgmma-dxa-mcast-nt4 +6.7%, copy-nt16/-nt4 +3.6% (+8 instructions), wgmma-dxa-mcast-nt16 +2.0%, wgmma-sparse +1.5%, the rest of dxa and wgmma under +1%. - core:sgemm2-wide +4.74% (313459 -> 328310, instructions equal): from #560, whose barrier and fence now drain the warp's memory requests (a correctness fix). rtlsim before #560 313459, after 328310; SimX moves the same way (312426 -> 324923). Later merges leave it unchanged (#569: 328310; #568 equal on SimX). - sgemmx +0.4%, sgemm2-fp16 +0.3%, softmax -0.2% (-6,144 instructions); all other entries within +/-0.1%. Not re-recorded (skipped for time): fpga_gate, asic_gate, and the opt-in gpu logic_gate entries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tinebp
added a commit
that referenced
this pull request
Dec 3, 2026
…E_ENABLE at FLEN 64) (#568) VX_CFG_EXT_LDST_WIDE_ENABLE required FLEN 32, where vx_flw.d/.q are macro-ops of 2 or 4 flw uops. With 64-bit FP registers a vx_flw.d/.q is now one LSU instruction, as NVIDIA's LDS.128 and AMD's DS_READ_B128 are: each lane reads its 8 or 16 bytes in one memory access and one writeback writes the 2 or 4 registers, each word NaN-boxed in its own register. The address must be naturally aligned (8 / 16 B). vx_fsw.d/.q stay fsw uops. - RTL: decode carries the group size; the scoreboard reserves the aligned group; LSU lanes are 16 B with one addressed word in the low half; the LMEM engine reads whole lanes for a quad; VX_lsu_dcache_narrow (new) sends a quad to the D-cache in two half-lane passes; result/commit/writeback carry the upper 64 bits. - SimX: the same model (IntrLsuArgs::wide, group scoreboard, one writeback, local memory serving a quad lane's two banks in one pass, two half-lane coalescer passes). - Everything is under the derived LSU_WIDE_ENABLE (the extension on at FLEN 64); every other configuration elaborates master's logic. - Profiles: on in the NVIDIA and AMD root profiles (derived ones inherit); tinygpu unchanged. a100, one core, SimX: sgemm -n64 -18.4%, sgemm2 -n64 -t4 -24.0% cycles. - Tests: tests/ldst_wide covers FLEN 64 (NaN-boxing, junk upper halves, end of the read-only words); parity-ldst-wide-x64 is a 64-bit model_parity case (testcase.py lets a model_parity case name its own xlen). Rebased onto master after #565, #566 and #569: the LSU alignment assertion keeps #569's fault exemption on both the wide and the normal path. On the final tree: parity-ldst-wide-x64 instructions equal, cycle gap 0.01%. CI goldens, re-recorded on this tree with toolchain v3.0.4 (#566 moved some instruction counts and the Beacon version) by the gates' own update flags: - logic_gate U55C (16 entries) and beacon_asic (14 entries): no build deeper or slower than its previous golden; LUT flat except raster +1%. - perf_gate (51 x32 entries): geomean +0.52% cycles; dxa/wgmma up to +6.7% (accepted); core:sgemm2-wide +4.74% from #560's barrier/fence memory drain (a correctness fix; SimX moves the same way); everything else within +/-0.4%. - parity on the paths where the merged PRs meet (atomics, fences, ldst-wide, ldst-matrix, lmem, barriers, traps): 13 of 14 pass. isa:parity-amo-scatter is 5.52% (> 5%) on master since #569: the RTL uop sequencer spends a start cycle on every macro-op, which SimX does not model, and #569 made every atomic a macro-op. Fixed separately. fpga_gate, asic_gate and the gpu logic_gate entries were not re-recorded (skipped for time). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tinebp
added a commit
that referenced
this pull request
Dec 4, 2026
…uop_amo (VX_CFG_EXT_AMO64_ENABLE) A 32-bit hart with VX_CFG_EXT_AMO64_ENABLE runs the .D read-modify-write atomics and AMOCAS.D on even-aligned register pairs, the pairing Zacas defines for amocas.d on RV32. The knob is off by default. Five registers in and two out exceed one instruction's operand slots, so the atomic expander VX_uop_amo (SimX LsuUopGen) emits the atomic as check [release fence] CMP AMO HI [acquire fence]: - CMP reads rd/rd+1 and stages them in the LSU as the 64-bit comparand; reading rd+1 also orders the burst behind an older pending write of rd+1. - AMO sends one thread per request on the thread's even/odd lane pair. The dcache coalescer fuses the pair into one 8-byte atomic; local memory serves it in one pass on two banks with a 64-bit read-modify-write; the LLC uses a 64-bit AMO ALU. Its response retires twice: the old low word into rd, the old high word into rd+1. - HI reserves rd+1 and is dropped at the LSU. rd+1 is released by whichever of HI and the last high-word response reaches the LSU last, so the release can never come before the reservation, and the response path never waits on HI. - A misaligned pair atomic is trapped by the check uop and dropped by the LSU like a word atomic; the LSU keeps one fault bit per staging entry so HI retires writing nothing exactly for the packets the AMO dropped. This is #564 reworked onto master's misaligned-atomic design (#569): #564 expanded pair atomics in VX_uop_packld and detected faults in the dispatcher, neither of which exists on master any more. Timing (Beacon against c6deb1c): the default build has no deeper register (core +279 LUTs for the decode checks). With AMO64 on, core 15 -> 16 levels and cache 13 -> 16. For that, the LSU picks a pair's operands within its own lane pair rather than through a NUM_LANES-wide mux, computes the "last request" flag one request ahead, and sets the high word's address bit instead of adding 1. The 64-bit AMO ALU builds its sum and compares from two 32-bit halves; this also applies to RV64's LLC/local-memory AMO ALU, which is logically unchanged. The 32-bit ALU keeps its form. Default-build changes: - Decode on RV32 now raises the illegal-instruction trap for the RV64 LD, LWU and SD encodings, FLD/FSD when FLEN is 32, LR.D/SC.D, and odd register pairs (new VX_TRAP_CAUSE_ILLEGAL_INSTR; MISA bit 32+17 for AMO64). - SimX no longer reserves x0 for a uop whose destination is x0, as RTL never does. This changes SimX timing for word atomics with rd = x0. - VX_config.toml/VX_types.toml gain the AMO64 knob, so every perf baseline's config_hash moves. The 51 baselines get their new config_hash from perf_baseline.config_hash (the same function reproduces master's hashes on a master build); cycles and instruction counts are unchanged, and the PR's rtlsim perf_gate cells check them exactly. Tests: tests/regression/amo gains the amo64_* cases (arith, bits, CAS, WAR on rd+1, store-then-atomic order, x0 pairs, local memory streams, illegal encodings, misaligned pairs across packets, slices and warps); ci/testcases adds the amo64 SimX/RTL legs and parity-amo64, -lmem and -misaligned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tinebp
added a commit
that referenced
this pull request
Dec 4, 2026
…uop_amo (VX_CFG_EXT_AMO64_ENABLE) (#564) A 32-bit hart with VX_CFG_EXT_AMO64_ENABLE runs the .D read-modify-write atomics and AMOCAS.D on even-aligned register pairs, the pairing Zacas defines for amocas.d on RV32. The knob is off by default. Five registers in and two out exceed one instruction's operand slots, so the atomic expander VX_uop_amo (SimX LsuUopGen) emits the atomic as check [release fence] CMP AMO HI [acquire fence]: - CMP reads rd/rd+1 and stages them in the LSU as the 64-bit comparand; reading rd+1 also orders the burst behind an older pending write of rd+1. - AMO sends one thread per request on the thread's even/odd lane pair. The dcache coalescer fuses the pair into one 8-byte atomic; local memory serves it in one pass on two banks with a 64-bit read-modify-write; the LLC uses a 64-bit AMO ALU. Its response retires twice: the old low word into rd, the old high word into rd+1. - HI reserves rd+1 and is dropped at the LSU. rd+1 is released by whichever of HI and the last high-word response reaches the LSU last, so the release can never come before the reservation, and the response path never waits on HI. - A misaligned pair atomic is trapped by the check uop and dropped by the LSU like a word atomic; the LSU keeps one fault bit per staging entry so HI retires writing nothing exactly for the packets the AMO dropped. This is #564 reworked onto master's misaligned-atomic design (#569): #564 expanded pair atomics in VX_uop_packld and detected faults in the dispatcher, neither of which exists on master any more. Timing (Beacon against c6deb1c): the default build has no deeper register (core +279 LUTs for the decode checks). With AMO64 on, core 15 -> 16 levels and cache 13 -> 16. For that, the LSU picks a pair's operands within its own lane pair rather than through a NUM_LANES-wide mux, computes the "last request" flag one request ahead, and sets the high word's address bit instead of adding 1. The 64-bit AMO ALU builds its sum and compares from two 32-bit halves; this also applies to RV64's LLC/local-memory AMO ALU, which is logically unchanged. The 32-bit ALU keeps its form. Default-build changes: - Decode on RV32 now raises the illegal-instruction trap for the RV64 LD, LWU and SD encodings, FLD/FSD when FLEN is 32, LR.D/SC.D, and odd register pairs (new VX_TRAP_CAUSE_ILLEGAL_INSTR; MISA bit 32+17 for AMO64). - SimX no longer reserves x0 for a uop whose destination is x0, as RTL never does. This changes SimX timing for word atomics with rd = x0. - VX_config.toml/VX_types.toml gain the AMO64 knob, so every perf baseline's config_hash moves. The 51 baselines get their new config_hash from perf_baseline.config_hash (the same function reproduces master's hashes on a master build); cycles and instruction counts are unchanged, and the PR's rtlsim perf_gate cells check them exactly. Tests: tests/regression/amo gains the amo64_* cases (arith, bits, CAS, WAR on rd+1, store-then-atomic order, x0 pairs, local memory streams, illegal encodings, misaligned pairs across packets, slices and warps); ci/testcases adds the amo64 SimX/RTL legs and parity-amo64, -lmem and -misaligned. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Base: master 3fb83fd. Head: fe58030 (1 commit).
Depends on: nothing. It is #560's F5 (the misaligned-atomic trap), split out of #560 and redesigned at the pipeline's extension points, as the #560 review asked (P1).
Merge order: independent of #560 at the code level. feat/amo64 (#564) stacks on it. A version on top of #560 is on branch rev2/amo-trap-b560 (e1371cf4b): same change, the test files merged with #560's amo tests.
The bug
An atomic (AMO, LR, SC, compare-and-swap) with an active lane whose address is not aligned to its size:
RISC-V says it raises address-misaligned: store/AMO (mcause 6), or load (4) for LR.
The fix: a precise trap, at the extension points only
The warp traps with mcause 6 (4 for LR), mepc = the atomic, mtval read-only zero. No instruction after the atomic runs before the handler, and the handler may return to mepc + 4.
VX_uop_amo) expands every atomic:check [release fence] atomic [acquire fence].INST_BR_AMOCHK,INST_BR_LRCHK).is_trap(extension point 4).VX_alu_int(EXT_A only) raises it when an active lane is misaligned.br_hitreleases the warp's fetch.VX_lsu_block, EXT_A only), from rs1: no fault bit travels inop_args.fault_hold, from Bug fixes to master found while integrating the Vulkan graphics stack #560's F5).BrType::AMOCHK); the sequencer gives a wstall uop its macro's fetch release;AluUnitholds a misaligned lane per block and callsScheduler::raise_trap;LsuUnitdrops the atomic from the faulted packet on.Semantics, stated: with one LSU packet per warp (the default shape) a misaligned atomic performs no access. With several, the lanes of packets ahead of the first misaligned one perform their atomics. F5 used SIMD slices as the unit instead; the test checks the packet rule.
Cost
The atomic stalls its warp's fetch until the check resolves, as a branch does.
x32 cycles, control = master 3fb83fd with this commit's amo test text, so only the hardware and model differ (meas/pctl_, meas/pb_):
On top of #560 (branch rev2/amo-trap-b560 e1371cf4b; control = rev1/b560's tree with this commit's amo test text). #560 adds scatter and cas_counter and fixes SimX's LLC atomic timing (F7). meas/p5ctl_s2, p5b_s2, p5ctl_r, p5b_r:
Against the two F5 forms (measured on F8 in #560, so read the percentages, not the cycles):
This change costs about what the precise form cost: amoadd +39.7% here against +38.0% there (rtlsim), contended +16.2% against +15.4%.
Three forms considered (§13.4); the maintainer chose P
The maintainer chose P, this PR (2026-12-01): precise and with no shared-stage change, at the measured cost below.
EXT_A is off by default (
VX_config.toml,VX_CFG_EXT_A_ENABLE = false), so the default configuration is unaffected by any of the three: every change is behind EXT_A.is_trap, ALU/LSU under EXT_Asched_amo_t, PC override, dispatcher fault marking (9 shared stages)INST_ARGS_BITS+1 (gated later under EXT_A)csrw mtvecbefore the next branch); a warp that exits with no branch in between loses it, and the pending state stays with that warp slotD was built and measured for this comparison only (branch rev2/amo-trap-deferred b9286af28, not proposed as is). x32, control = master with D's test text (meas/pDc_, pD_):
D's own test (the atomic followed only by a jump, the handler returning to the continuation) passes: SimX 4403 / RTL 4268 cycles, SIMD-4 shape RTL 6850. This PR's precise test fails on D, on both models: the late trap lands after the test restores mtvec, so the warp traps to address 0 (SimX "no trap handler (mtvec=0)", RTL fetch assertion; meas/dprec).
What remains of D's cost: the check uop (one ALU issue per atomic) and the uop sequencer's first-uop bubble, now paid by every atomic.
Area and depth (Beacon, CI's v3.0.3 bundle, against master 3fb83fd)
Units that moved with A on (U55C):
uop_amoLUT 832 → 720,uop_sequencer3152 → 3168,lsu_block907 → 939 (+1 FF),alu_int2966 → 2978 (+1 FF), its response buffer +1 FF. No path got deeper.(The A-off rows use measurement-only entries, core without
-DVX_CFG_EXT_A_ENABLE; the ASICcoreentry builds with A on.)Pipeline isolation (docs/coding_guidelines_verilog.md §13)
No shared stage (§13.1) changes, in RTL or SimX. The classifier marks the PR R3 for isa-localparams (the two branch codes), isa-decoder (
BrType::AMOCHK) and test-coverage, not pipeline-stage.It does change two shared units, the ALU and the LSU, for the atomics extension. The rules for that (§13.1) are those of a shared stage, so the six §13.4 items:
is_trap); only the LSU sees the atomic's packets. The ALU part decides the trap; the LSU part keeps the atomic from performing.INST_ARGS_BITSin every configuration. This change costs +39.7% on amoadd (rtlsim) and 0.0% off atomics, with no shared-stage change. The precise F5 cost about the same (+38.0% amoadd, rtlsim).VX_CFG_EXT_A_ENABLEor folds to a constant), and SimX runs no new code; non-atomic cycles are unchanged (sgemm 0.00% on both simulators).Changes to master code
All atomic behaviour needs EXT_A, which is off by default.
hw/rtl/VX_gpu_pkg.sv(1 hunk):INST_BR_AMOCHK,INST_BR_LRCHKon the spare branch codes 0001 and 0011.hw/rtl/VX_trace_pkg.sv(1): their trace names.hw/rtl/core/VX_decode.sv(1, under EXT_A): an atomic is wstall.hw/rtl/core/VX_uop_sequencer.sv(1, under EXT_A): every atomic goes throughVX_uop_amo, which now owns theDCACHE_SHARED_LLCfence rule.hw/rtl/core/VX_uop_amo.sv(2): the check uop.hw/rtl/core/VX_alu_int.sv(4): the check, under EXT_A; with A offchk_trap_ris a constant 0.hw/rtl/core/VX_lsu_block.sv(7): the fault, under EXT_A; the bypass, hold and header mask fold away with A off (req_fault= 0).types.h(2),decode.cpp(2),sequencer.cpp(1: a wstall uop takes its macro's fetch release; no existing uop is wstall),alu_unit.{h,cpp}(2, 5),lsu_unit.{h,cpp}(3, 6).tests/regression/amo(common.h,kernel.cpp,testcases.h: the newamo_misalignedcase),ci/testcases/isa/amo.yaml(2 cases, touches).atomic_memory_operations.md,lsu_pipeline_design.md,trap_and_exception_foundation.md.Verification
Default-behaviour impact
None. Every change needs EXT_A; with A off Beacon core equals master and sgemm's cycles are unchanged.
Known gaps (not fixed here)
🤖 Generated with Claude Code