From f05df5fb76fb6d8dfb02a3c58319400800d1171e Mon Sep 17 00:00:00 2001 From: tinebp Date: Thu, 3 Dec 2026 20:25:16 -0800 Subject: [PATCH] 64-bit atomics on a 32-bit hart: register-pair .D atomics through VX_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 c6deb1c41): 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 --- VX_config.toml | 8 +- VX_types.toml | 1 + ci/baselines/perf/core.json | 34 +- ci/baselines/perf/dxa.json | 22 +- ci/baselines/perf/graphics.json | 18 +- ci/baselines/perf/raytracing.json | 6 +- ci/baselines/perf/tensor.json | 12 +- ci/baselines/perf/tensor_mx.json | 2 +- ci/baselines/perf/tensor_sp.json | 2 +- ci/baselines/perf/tensor_wg.json | 6 +- ci/testcases/isa/amo.yaml | 82 +++- docs/designs/atomic_memory_operations.md | 119 ++++- hw/rtl/VX_gpu_pkg.sv | 26 +- hw/rtl/cache/VX_amo_alu.sv | 46 +- hw/rtl/cache/VX_cache_amo.sv | 23 +- hw/rtl/core/VX_decode.sv | 63 ++- hw/rtl/core/VX_lsu_block.sv | 396 +++++++++++++++- hw/rtl/core/VX_lsu_mem_path.sv | 14 +- hw/rtl/core/VX_mem_unit.sv | 5 + hw/rtl/core/VX_uop_amo.sv | 65 ++- hw/rtl/mem/VX_lmem_bank.sv | 34 +- hw/rtl/mem/VX_lmem_unit.sv | 172 ++++++- sim/simx/decode.cpp | 67 ++- sim/simx/lsu_unit.cpp | 329 +++++++++++-- sim/simx/lsu_unit.h | 44 ++ sim/simx/mem/local_mem.cpp | 28 +- sim/simx/sequencer.cpp | 6 +- sim/simx/types.h | 9 +- sw/runtime/include/vortex2.h | 2 + tests/regression/amo/common.h | 49 ++ tests/regression/amo/kernel.cpp | 572 +++++++++++++++++++++++ tests/regression/amo/main.cpp | 6 +- tests/regression/amo/testcases.h | 519 ++++++++++++++++++++ 33 files changed, 2614 insertions(+), 173 deletions(-) diff --git a/VX_config.toml b/VX_config.toml index db2a13d3ce..d90b25841c 100644 --- a/VX_config.toml +++ b/VX_config.toml @@ -32,6 +32,12 @@ VX_CFG_EXT_A_ENABLE = false # an atomic, it does not replace the set. Off by default because it widens the # memory request with a comparand operand. VX_CFG_EXT_ZACAS_ENABLE = false +# 64-bit atomics on a 32-bit hart. The .D encodings of the read-modify-write +# atomics, and of Zacas amocas.d, operate on even-aligned register pairs +# (rd/rd+1, rs2/rs2+1), the pairing Zacas defines for amocas.d on RV32. LR.D and +# SC.D stay illegal. Requires the A extension and XLEN=32 (a 64-bit hart has the +# .D forms natively). Without it, a .D atomic on a 32-bit hart traps as illegal. +VX_CFG_EXT_AMO64_ENABLE = false VX_CFG_EXT_ZICOND_ENABLE = true # Zfhmin: half-precision load/store, move and fp16<->fp32/fp64 conversion (no @@ -557,7 +563,7 @@ VX_CFG_NUM_OCACHES = "expr: max(1, up($VX_CFG_NUM_OM_CORES / 4))" VX_CFG_MISA_STD = "expr: ($VX_CFG_EXT_A_ENABLED << 0) | (0 << 1) | ($VX_CFG_EXT_C_ENABLED << 2) | ($VX_CFG_EXT_D_ENABLED << 3) | (0 << 4) | ($VX_CFG_EXT_F_ENABLED << 5) | (0 << 6) | (0 << 7) | (1 << 8) | (0 << 9) | (0 << 10) | (0 << 11) | ($VX_CFG_EXT_M_ENABLED << 12) | (0 << 13) | (0 << 14) | (0 << 15) | (0 << 16) | (0 << 17) | (0 << 18) | (0 << 19) | (1 << 20) | (0 << 21) | (0 << 22) | (1 << 23) | (0 << 24) | (0 << 25)" # Custom extensions signature -VX_CFG_MISA_EXT = "expr: ($VX_CFG_ICACHE_ENABLED << 0) | ($VX_CFG_DCACHE_ENABLED << 1) | ($VX_CFG_L2_ENABLED << 2) | ($VX_CFG_L3_ENABLED << 3) | ($VX_CFG_LMEM_ENABLED << 4) | ($VX_CFG_EXT_ZICOND_ENABLED << 5) | ($VX_CFG_EXT_TCU_ENABLED << 9) | ($VX_CFG_EXT_DXA_ENABLED << 10) | ($VX_CFG_EXT_TEX_ENABLED << 6) | ($VX_CFG_EXT_RASTER_ENABLED << 7) | ($VX_CFG_EXT_OM_ENABLED << 8) | ($VX_CFG_EXT_RTU_ENABLED << 11) | ($VX_CFG_EXT_TFU_ENABLED << 12) | ($VX_CFG_EXT_ZFHMIN_ENABLED << 13) | ($VX_CFG_EXT_LDST_WIDE_ENABLED << 14) | ($VX_CFG_EXT_LDST_MATRIX_ENABLED << 15)" +VX_CFG_MISA_EXT = "expr: ($VX_CFG_ICACHE_ENABLED << 0) | ($VX_CFG_DCACHE_ENABLED << 1) | ($VX_CFG_L2_ENABLED << 2) | ($VX_CFG_L3_ENABLED << 3) | ($VX_CFG_LMEM_ENABLED << 4) | ($VX_CFG_EXT_ZICOND_ENABLED << 5) | ($VX_CFG_EXT_TCU_ENABLED << 9) | ($VX_CFG_EXT_DXA_ENABLED << 10) | ($VX_CFG_EXT_TEX_ENABLED << 6) | ($VX_CFG_EXT_RASTER_ENABLED << 7) | ($VX_CFG_EXT_OM_ENABLED << 8) | ($VX_CFG_EXT_RTU_ENABLED << 11) | ($VX_CFG_EXT_TFU_ENABLED << 12) | ($VX_CFG_EXT_ZFHMIN_ENABLED << 13) | ($VX_CFG_EXT_LDST_WIDE_ENABLED << 14) | ($VX_CFG_EXT_LDST_MATRIX_ENABLED << 15) | ($VX_CFG_EXT_AMO64_ENABLED << 17)" [toolchain] ASIC = false diff --git a/VX_types.toml b/VX_types.toml index 76111bdb07..58dc2b81be 100644 --- a/VX_types.toml +++ b/VX_types.toml @@ -532,6 +532,7 @@ VX_RT_CB_TYPE_PROC = 2 [traps] # RISC-V M-mode synchronous trap causes. 0..15 are standard. +VX_TRAP_CAUSE_ILLEGAL_INSTR = 2 VX_TRAP_CAUSE_BREAKPOINT = 3 VX_TRAP_CAUSE_ECALL_MMODE = 11 diff --git a/ci/baselines/perf/core.json b/ci/baselines/perf/core.json index a4fed9d6c6..9074f934e5 100644 --- a/ci/baselines/perf/core.json +++ b/ci/baselines/perf/core.json @@ -6,7 +6,7 @@ }, "app": "raycast", "args": "", - "config_hash": "6a8db2da81885bd3", + "config_hash": "ec2eab37ae6610df", "configs": "-DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -17,7 +17,7 @@ }, "app": "raycast", "args": "", - "config_hash": "a7365739bd29ae7f", + "config_hash": "85c93eaa65ab0e29", "configs": "-DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -28,7 +28,7 @@ }, "app": "sgemm", "args": "-n128", - "config_hash": "a94d5512bae0d0c4", + "config_hash": "54fdc586ad40dfe6", "configs": "-DVX_CFG_NUM_WARPS=8 -DVX_CFG_NUM_CORES=2 -DVX_CFG_NUM_THREADS=16 -DVX_CFG_L2_ENABLE", "driver": "rtlsim" }, @@ -39,7 +39,7 @@ }, "app": "sgemm", "args": "-n128", - "config_hash": "ed82856d2fbdb4fa", + "config_hash": "99a2d875b0cc052f", "configs": "-DVX_CFG_NUM_WARPS=8 -DVX_CFG_NUM_CORES=2 -DVX_CFG_NUM_THREADS=4 -DVX_CFG_L2_ENABLE", "driver": "rtlsim" }, @@ -50,7 +50,7 @@ }, "app": "sgemm", "args": "-n128", - "config_hash": "207c97cf1edf224d", + "config_hash": "790857eb58ef4a38", "configs": "-DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -61,7 +61,7 @@ }, "app": "sgemm", "args": "-n128", - "config_hash": "ddaf766d29f0c9e2", + "config_hash": "b073425087f8afcc", "configs": "-DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -72,7 +72,7 @@ }, "app": "sgemm2", "args": "-n64 -t4", - "config_hash": "e3a289024245088f", + "config_hash": "4cfad78ae90d8abf", "configs": "-DVX_CFG_EXT_LDST_WIDE_ENABLE -DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -83,7 +83,7 @@ }, "app": "sgemmx", "args": "-n128", - "config_hash": "b33624dacfde5ab3", + "config_hash": "5719767a72ddb58b", "configs": "-DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -94,7 +94,7 @@ }, "app": "sgemmx", "args": "-n128", - "config_hash": "c2faf5453d1010f0", + "config_hash": "dd17b407ca109069", "configs": "-DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -105,7 +105,7 @@ }, "app": "sgemv", "args": "-m512 -n512", - "config_hash": "ba5846fb3e38ff61", + "config_hash": "f1cdab89df261a6f", "configs": "-DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -116,7 +116,7 @@ }, "app": "sgemv", "args": "-m512 -n512", - "config_hash": "ff7c79ffef364ae9", + "config_hash": "781c061099be02ba", "configs": "-DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -127,7 +127,7 @@ }, "app": "softmax", "args": "-n64", - "config_hash": "5c38536ce941bce9", + "config_hash": "43c01c10adef3247", "configs": "-DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -138,7 +138,7 @@ }, "app": "softmax", "args": "-n64", - "config_hash": "647c47cbc33e0b1e", + "config_hash": "af19feb4ba83c536", "configs": "-DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -149,7 +149,7 @@ }, "app": "stencil3d", "args": "-n32", - "config_hash": "e962be6c4a705bd0", + "config_hash": "40175d4e0e384496", "configs": "-DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -160,7 +160,7 @@ }, "app": "stencil3d", "args": "-n32", - "config_hash": "44671bcb89d71094", + "config_hash": "f1f7e8066c503382", "configs": "-DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -171,7 +171,7 @@ }, "app": "vecadd", "args": "-n16384", - "config_hash": "3c46a73d97d7dde8", + "config_hash": "86ae627525ff48fa", "configs": "-DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -182,7 +182,7 @@ }, "app": "vecadd", "args": "-n16384", - "config_hash": "67f76328e947f037", + "config_hash": "3086b96b5e90ed6e", "configs": "-DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" } diff --git a/ci/baselines/perf/dxa.json b/ci/baselines/perf/dxa.json index 9c42109c5f..a7fc5f8dfc 100644 --- a/ci/baselines/perf/dxa.json +++ b/ci/baselines/perf/dxa.json @@ -6,7 +6,7 @@ }, "app": "dxa_copy_mcast", "args": "", - "config_hash": "a98f143d348178b8", + "config_hash": "9433b1d6d8b7cff7", "configs": "-DVX_CFG_EXT_DXA_ENABLE", "driver": "rtlsim" }, @@ -17,7 +17,7 @@ }, "app": "dxa_copy", "args": "-d1", - "config_hash": "55c5155d6ab46f83", + "config_hash": "3339bf7c49b86d9d", "configs": "-DVX_CFG_EXT_DXA_ENABLE -DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -28,7 +28,7 @@ }, "app": "dxa_copy", "args": "-d1", - "config_hash": "ee5d9d68a6bee766", + "config_hash": "5afa5abaffe6e15f", "configs": "-DVX_CFG_EXT_DXA_ENABLE -DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -39,7 +39,7 @@ }, "app": "sgemm2_dxa_mcast", "args": "-n128", - "config_hash": "82c50e47ea6ea51c", + "config_hash": "5193116f1e881efa", "configs": "-DVX_CFG_EXT_DXA_ENABLE -DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -50,7 +50,7 @@ }, "app": "sgemm2_dxa_mcast", "args": "-n128", - "config_hash": "8f2e3ded05181df5", + "config_hash": "d1e0db531b6ccd94", "configs": "-DVX_CFG_EXT_DXA_ENABLE -DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -61,7 +61,7 @@ }, "app": "sgemm2_dxa", "args": "-n128 -t4 -m1", - "config_hash": "a94548f6a341be69", + "config_hash": "93e0fad8e2d2f051", "configs": "-DVX_CFG_EXT_DXA_ENABLE -DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -72,7 +72,7 @@ }, "app": "sgemm2_dxa", "args": "-n128 -t4 -m1", - "config_hash": "b647693500d03cdb", + "config_hash": "f52684ed135aa09d", "configs": "-DVX_CFG_EXT_DXA_ENABLE -DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -83,7 +83,7 @@ }, "app": "sgemm_tcu_wg_dxa_mcast", "args": "-m 128 -n 128 -k 64", - "config_hash": "7506c2b313ddfb40", + "config_hash": "0464cd431eaff687", "configs": "-DVX_CFG_EXT_TCU_ENABLE -DVX_CFG_EXT_DXA_ENABLE -DVX_CFG_NUM_ALU_BLOCKS=1 -DVX_CFG_NUM_LSU_BLOCKS=1 -DVX_CFG_NUM_FPU_BLOCKS=1 -DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -94,7 +94,7 @@ }, "app": "sgemm_tcu_wg_dxa_mcast", "args": "-m 128 -n 128 -k 64", - "config_hash": "a7d7a7c19cc97d7f", + "config_hash": "757603f7261ebf58", "configs": "-DVX_CFG_EXT_TCU_ENABLE -DVX_CFG_EXT_DXA_ENABLE -DVX_CFG_NUM_ALU_BLOCKS=1 -DVX_CFG_NUM_LSU_BLOCKS=1 -DVX_CFG_NUM_FPU_BLOCKS=1 -DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -105,7 +105,7 @@ }, "app": "sgemm_tcu_wg_dxa", "args": "-m 128 -n 128 -k 64", - "config_hash": "0dfd4e6b3f65442b", + "config_hash": "7dafaa4d0f8c031b", "configs": "-DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_EXT_TCU_ENABLE -DVX_CFG_TCU_WGMMA_ENABLE -DVX_CFG_EXT_DXA_ENABLE -DWGMMA_NRC=16 -DVX_CFG_NUM_ALU_BLOCKS=1 -DVX_CFG_NUM_LSU_BLOCKS=1 -DVX_CFG_NUM_FPU_BLOCKS=1 -DVX_CFG_NUM_WARPS=8 -DVX_CFG_NUM_THREADS=32", "driver": "rtlsim" }, @@ -116,7 +116,7 @@ }, "app": "sgemm_tcu_wg_sp_dxa", "args": "-m 128 -n 128 -k 128", - "config_hash": "30f77e58ea21b51b", + "config_hash": "fe21095a1de0fe13", "configs": "-DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_EXT_TCU_ENABLE -DVX_CFG_TCU_WGMMA_ENABLE -DVX_CFG_TCU_SPARSE_ENABLE -DVX_CFG_EXT_DXA_ENABLE -DWGMMA_NRC=32 -DVX_CFG_NUM_ALU_BLOCKS=1 -DVX_CFG_NUM_LSU_BLOCKS=1 -DVX_CFG_NUM_FPU_BLOCKS=1 -DVX_CFG_NUM_WARPS=8 -DVX_CFG_NUM_THREADS=32", "driver": "rtlsim" } diff --git a/ci/baselines/perf/graphics.json b/ci/baselines/perf/graphics.json index d52d41eda8..74fe3d288f 100644 --- a/ci/baselines/perf/graphics.json +++ b/ci/baselines/perf/graphics.json @@ -6,7 +6,7 @@ }, "app": "gfx_draw3d", "args": "-w 128 -h 128 -t box.cgltrace -r box_ref_128.png", - "config_hash": "c548118c41bcef23", + "config_hash": "20cf2992755b938e", "configs": "-DVX_CFG_EXT_TEX_ENABLE -DVX_CFG_EXT_RASTER_ENABLE -DVX_CFG_EXT_OM_ENABLE -DVX_CFG_NUM_CORES=2 -DVX_CFG_NUM_THREADS=4 -DVX_CFG_L2_ENABLE", "driver": "rtlsim" }, @@ -17,7 +17,7 @@ }, "app": "gfx_draw3d", "args": "-w 128 -h 128 -t box.cgltrace -r box_ref_128.png", - "config_hash": "48890b8c1ad7fe04", + "config_hash": "6514e2762ab6355d", "configs": "-DVX_CFG_EXT_TEX_ENABLE -DVX_CFG_EXT_RASTER_ENABLE -DVX_CFG_EXT_OM_ENABLE -DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -28,7 +28,7 @@ }, "app": "gfx_draw3d", "args": "-w 128 -h 128 -t box.cgltrace -r box_ref_128.png", - "config_hash": "b08776e5f96f6b97", + "config_hash": "1a9b4fb1be09e582", "configs": "-DVX_CFG_EXT_TEX_ENABLE -DVX_CFG_EXT_RASTER_ENABLE -DVX_CFG_EXT_OM_ENABLE -DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -39,7 +39,7 @@ }, "app": "gfx_om", "args": "-w 128 -h 128 -r whitebox_128.png", - "config_hash": "32307cceb33dc7a6", + "config_hash": "d5dbc198569d3dae", "configs": "-DVX_CFG_EXT_OM_ENABLE -DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -50,7 +50,7 @@ }, "app": "gfx_om", "args": "-w 128 -h 128 -r whitebox_128.png", - "config_hash": "ce07b69d9a182c02", + "config_hash": "ef58a124aacc1f5b", "configs": "-DVX_CFG_EXT_OM_ENABLE -DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -61,7 +61,7 @@ }, "app": "gfx_raster", "args": "-w 128 -h 128 -t triangle.cgltrace -r triangle_ref_128.png", - "config_hash": "366fbc3590ee481f", + "config_hash": "7a09c8c5f0f1a6c3", "configs": "-DVX_CFG_EXT_RASTER_ENABLE -DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -72,7 +72,7 @@ }, "app": "gfx_raster", "args": "-w 128 -h 128 -t triangle.cgltrace -r triangle_ref_128.png", - "config_hash": "8c3f6ea5a04a0d52", + "config_hash": "f055c2feba08a052", "configs": "-DVX_CFG_EXT_RASTER_ENABLE -DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" }, @@ -83,7 +83,7 @@ }, "app": "gfx_tex", "args": "-i toad.png -r toad_ref_f0.png -f 0 -g 0", - "config_hash": "370db7ba23efdbb2", + "config_hash": "0390595d10311ba5", "configs": "-DVX_CFG_EXT_TEX_ENABLE -DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -94,7 +94,7 @@ }, "app": "gfx_tex", "args": "-i toad.png -r toad_ref_f0.png -f 0 -g 0", - "config_hash": "a680ecaa56a2c2fd", + "config_hash": "5ac2eac1fba71bcf", "configs": "-DVX_CFG_EXT_TEX_ENABLE -DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" } diff --git a/ci/baselines/perf/raytracing.json b/ci/baselines/perf/raytracing.json index db66430680..d260cde7a8 100644 --- a/ci/baselines/perf/raytracing.json +++ b/ci/baselines/perf/raytracing.json @@ -6,7 +6,7 @@ }, "app": "rt_raycast", "args": "", - "config_hash": "9fa9d930efb7a892", + "config_hash": "cfcebadf97f11e01", "configs": "-DVX_CFG_EXT_RTU_ENABLE -DVX_CFG_NUM_CORES=2 -DVX_CFG_NUM_THREADS=4 -DVX_CFG_L2_ENABLE", "driver": "rtlsim" }, @@ -17,7 +17,7 @@ }, "app": "rt_raycast", "args": "", - "config_hash": "bf5c79f92d7a2d0b", + "config_hash": "a05eb1131e4e7969", "configs": "-DVX_CFG_EXT_RTU_ENABLE -DVX_CFG_NUM_THREADS=16", "driver": "rtlsim" }, @@ -28,7 +28,7 @@ }, "app": "rt_raycast", "args": "", - "config_hash": "6eca2ad296cbc4c8", + "config_hash": "9b6e3b30f136d6b1", "configs": "-DVX_CFG_EXT_RTU_ENABLE -DVX_CFG_NUM_THREADS=4", "driver": "rtlsim" } diff --git a/ci/baselines/perf/tensor.json b/ci/baselines/perf/tensor.json index 660bb540f9..e39e79eb7d 100644 --- a/ci/baselines/perf/tensor.json +++ b/ci/baselines/perf/tensor.json @@ -6,7 +6,7 @@ }, "app": "sgemm_tcu", "args": "-m 128 -n 128 -k 128", - "config_hash": "395b9f89b1613b5f", + "config_hash": "d04059704dadce77", "configs": "-DVX_CFG_NUM_THREADS=8 -DVX_CFG_NUM_WARPS=8 -DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_EXT_TCU_ENABLE -DITYPE=fp16 -DOTYPE=fp32 -DVX_CFG_NUM_ALU_BLOCKS=1 -DVX_CFG_NUM_LSU_BLOCKS=1 -DVX_CFG_NUM_FPU_BLOCKS=1", "driver": "rtlsim" }, @@ -17,7 +17,7 @@ }, "app": "sgemm_tcu", "args": "-m 128 -n 128 -k 128", - "config_hash": "14c25c4ef390468d", + "config_hash": "bb241072fb023603", "configs": "-DVX_CFG_NUM_WARPS=8 -DVX_CFG_EXT_TCU_ENABLE -DITYPE=fp16 -DOTYPE=fp32 -DVX_CFG_NUM_CORES=2 -DVX_CFG_NUM_THREADS=16 -DVX_CFG_L2_ENABLE", "driver": "rtlsim" }, @@ -28,7 +28,7 @@ }, "app": "sgemm_tcu", "args": "-m 128 -n 128 -k 128", - "config_hash": "3a5afbca2316def1", + "config_hash": "2ed1de9701affdc7", "configs": "-DVX_CFG_NUM_WARPS=8 -DVX_CFG_EXT_TCU_ENABLE -DITYPE=fp16 -DOTYPE=fp32 -DVX_CFG_NUM_CORES=2 -DVX_CFG_NUM_THREADS=4 -DVX_CFG_L2_ENABLE", "driver": "rtlsim" }, @@ -39,7 +39,7 @@ }, "app": "sgemm_tcu", "args": "-m 128 -n 128 -k 128", - "config_hash": "fd128828360dcf27", + "config_hash": "d43de8a0b557b418", "configs": "-DVX_CFG_NUM_THREADS=8 -DVX_CFG_EXT_TCU_ENABLE -DITYPE=fp16 -DOTYPE=fp32", "driver": "rtlsim" }, @@ -50,7 +50,7 @@ }, "app": "sgemm2_tcu", "args": "-m 128 -n 128 -k 64", - "config_hash": "9934ecd51fa79038", + "config_hash": "7019934b481f08ab", "configs": "-DVX_CFG_NUM_THREADS=8 -DVX_CFG_NUM_WARPS=8 -DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_EXT_TCU_ENABLE -DITYPE=fp16 -DOTYPE=fp32 -DVX_CFG_NUM_ALU_BLOCKS=1 -DVX_CFG_NUM_LSU_BLOCKS=1 -DVX_CFG_NUM_FPU_BLOCKS=1", "driver": "rtlsim" }, @@ -61,7 +61,7 @@ }, "app": "sgemm_tcu", "args": "-m 128 -n 128 -k 128", - "config_hash": "328c614d0141deb8", + "config_hash": "5c6c2ebe50d1ccb0", "configs": "-DVX_CFG_NUM_THREADS=8 -DVX_CFG_EXT_TCU_ENABLE -DITYPE=fp16 -DOTYPE=fp32 -DVX_CFG_EXT_LDST_MATRIX_ENABLE", "driver": "rtlsim" } diff --git a/ci/baselines/perf/tensor_mx.json b/ci/baselines/perf/tensor_mx.json index e8acc30a31..7567073823 100644 --- a/ci/baselines/perf/tensor_mx.json +++ b/ci/baselines/perf/tensor_mx.json @@ -6,7 +6,7 @@ }, "app": "sgemm_tcu_mx", "args": "-m 128 -n 128 -k32", - "config_hash": "af77d4a5f0a8576f", + "config_hash": "2caf5882ec6a263d", "configs": "-DVX_CFG_NUM_THREADS=8 -DVX_CFG_EXT_TCU_ENABLE -DVX_CFG_TCU_MX_ENABLE -DVX_CFG_TCU_FP8_ENABLE -DITYPE=mxfp8 -DOTYPE=fp32 -DVX_CFG_TCU_TYPE_DPI", "driver": "rtlsim" } diff --git a/ci/baselines/perf/tensor_sp.json b/ci/baselines/perf/tensor_sp.json index 7e3e05593e..00e5012fe8 100644 --- a/ci/baselines/perf/tensor_sp.json +++ b/ci/baselines/perf/tensor_sp.json @@ -6,7 +6,7 @@ }, "app": "sgemm_tcu_sp", "args": "-m 128 -n 128 -k 128", - "config_hash": "e7354a7a2bc00238", + "config_hash": "6c9466af6f3aeb4f", "configs": "-DVX_CFG_NUM_THREADS=8 -DVX_CFG_EXT_TCU_ENABLE -DVX_CFG_TCU_SPARSE_ENABLE", "driver": "rtlsim" } diff --git a/ci/baselines/perf/tensor_wg.json b/ci/baselines/perf/tensor_wg.json index 1c5354893e..34ec779594 100644 --- a/ci/baselines/perf/tensor_wg.json +++ b/ci/baselines/perf/tensor_wg.json @@ -6,7 +6,7 @@ }, "app": "sgemm_tcu_wg", "args": "-m64 -n64 -k64", - "config_hash": "4d4bf375520e05f4", + "config_hash": "f7f5b3294f817e23", "configs": "-DVX_CFG_NUM_THREADS=8 -DVX_CFG_NUM_WARPS=8 -DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_EXT_TCU_ENABLE -DVX_CFG_TCU_WGMMA_ENABLE -DVX_CFG_NUM_ALU_BLOCKS=1 -DVX_CFG_NUM_LSU_BLOCKS=1 -DVX_CFG_NUM_FPU_BLOCKS=1 -DVX_CFG_TCU_FEDP2K -DITYPE=fp16 -DOTYPE=fp32 -DWGMMA_NRC=8 -DWGMMA_RS", "driver": "rtlsim" }, @@ -17,7 +17,7 @@ }, "app": "sgemm_tcu_wg", "args": "-m 128 -n 128 -k 128", - "config_hash": "9eda8c6f5a4af670", + "config_hash": "4aba179670ab8950", "configs": "-DVX_CFG_NUM_THREADS=8 -DVX_CFG_NUM_WARPS=8 -DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_EXT_TCU_ENABLE -DVX_CFG_TCU_WGMMA_ENABLE -DITYPE=fp16 -DOTYPE=fp32 -DWGMMA_NRC=32 -DWGMMA_SS -DVX_CFG_NUM_ALU_BLOCKS=1 -DVX_CFG_NUM_LSU_BLOCKS=1 -DVX_CFG_NUM_FPU_BLOCKS=1", "driver": "rtlsim" }, @@ -28,7 +28,7 @@ }, "app": "sgemm_tcu_wg_sp", "args": "-m 128 -n 128 -k 128", - "config_hash": "279fd043db54e2d1", + "config_hash": "5924ef0de1af3c4e", "configs": "-DVX_CFG_NUM_THREADS=8 -DVX_CFG_NUM_WARPS=8 -DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_EXT_TCU_ENABLE -DVX_CFG_TCU_WGMMA_ENABLE -DVX_CFG_TCU_SPARSE_ENABLE -DWGMMA_NRC=32 -DVX_CFG_NUM_ALU_BLOCKS=1 -DVX_CFG_NUM_LSU_BLOCKS=1 -DVX_CFG_NUM_FPU_BLOCKS=1", "driver": "rtlsim" } diff --git a/ci/testcases/isa/amo.yaml b/ci/testcases/isa/amo.yaml index dbf1896f98..51d502fa67 100644 --- a/ci/testcases/isa/amo.yaml +++ b/ci/testcases/isa/amo.yaml @@ -7,7 +7,7 @@ defaults: configs: "-DVX_CFG_EXT_A_ENABLE" xlen: [32, 64] tier: smoke - touches: [hw/rtl/cache, hw/rtl/mem, hw/rtl/core/VX_decode.sv, hw/rtl/core/VX_lsu_block.sv, hw/rtl/core/VX_uop_amo.sv, hw/rtl/core/VX_alu_int.sv, sim/simx/amo, sim/simx/mem, sim/simx/lsu_unit.cpp, sim/simx/alu_unit.cpp] + touches: [hw/rtl/cache, hw/rtl/mem, hw/rtl/core/VX_decode.sv, hw/rtl/core/VX_lsu_block.sv, hw/rtl/core/VX_uop_amo.sv, hw/rtl/core/VX_alu_int.sv, sim/simx/amo, sim/simx/mem, sim/simx/lsu_unit.cpp, sim/simx/alu_unit.cpp, sim/simx/decode.cpp] tests: # RISC-V 'A' ISA conformance (rv{XLEN}ua-p-{amo*,lrsc}); shares the A-built sim. - id: isa-conformance @@ -26,6 +26,33 @@ tests: app: amo drivers: [simx, rtlsim] configs+: "-DVX_CFG_EXT_ZACAS_ENABLE" + # 64-bit atomics on a 32-bit hart: every .D atomic on register pairs, + # contended, on global and local memory, and the .D encodings that must trap. + # (Without AMO64, the base and zacas specs above run the trap case alone.) + - id: amo64 + app: amo + drivers: [simx, rtlsim] + xlen: [32] + configs+: "-DVX_CFG_EXT_ZACAS_ENABLE -DVX_CFG_EXT_AMO64_ENABLE" + # Two LSU blocks, four operand collectors and two SIMD packets per warp: the + # comparand stage per issue slot and collector, pair requests across packets, + # and the operand arbiter keeping an instruction's packets together (a + # three-operand compare-and-swap stalls between them on bank conflicts). + - id: amo64-simd + app: amo + drivers: [simx, rtlsim] + xlen: [32] + configs+: "-DVX_CFG_EXT_ZACAS_ENABLE -DVX_CFG_EXT_AMO64_ENABLE -DVX_CFG_NUM_WARPS=32 -DVX_CFG_SIMD_WIDTH=4" + shape: {threads: 8} + args: "-n8 -t cas_ladder -t cas_lmem -t cas_counter -t cas_counter_lmem -t amo64_arith -t amo64_arith_lmem -t amo64_bits -t amo64_bits_lmem -t amo64_cas_counter -t amo64_cas_counter_lmem -t amo64_cas_ladder -t amo64_illegal -t ldst64_illegal -t amo64_war -t amo64_war_lmem -t amo64_store_amo -t amo64_store_amo_lmem -t amo64_x0 -t amo_misaligned -t amo64_misaligned -t amo_line_burst" + # One LSU block for two issue slots, with the pair atomics' ordering cases. + - id: amo64-simd-lsu1 + app: amo + drivers: [simx, rtlsim] + xlen: [32] + configs+: "-DVX_CFG_EXT_ZACAS_ENABLE -DVX_CFG_EXT_AMO64_ENABLE -DVX_CFG_NUM_WARPS=32 -DVX_CFG_SIMD_WIDTH=4 -DVX_CFG_NUM_LSU_BLOCKS=1" + shape: {threads: 8} + args: "-n8 -t amo64_arith -t amo64_arith_lmem -t amo64_cas_ladder -t amo64_war -t amo64_war_lmem -t amo64_store_amo -t amo64_store_amo_lmem -t amo64_x0 -t amo_misaligned -t amo64_misaligned" # Two LSU packets per warp (8 threads on 4 LSU lanes): an LR/SC reservation # belongs to a warp thread, so the second packet's lanes must not alias the # first packet's. @@ -35,6 +62,50 @@ tests: configs+: "-DVX_CFG_NUM_LSU_LANES=4" shape: {threads: 8} args: "-n8 -t lrsc_counter -t lrsc_counter_aqrl -t lrsc_lmem" + # Four cores sharing an L2: the dcache passes the fused 8-byte atomic to the LLC. + - id: amo64-mc-l2 + app: amo + drivers: [simx, rtlsim] + xlen: [32] + configs+: "-DVX_CFG_EXT_ZACAS_ENABLE -DVX_CFG_EXT_AMO64_ENABLE" + shape: {cores: 4, l2cache: true} + args: "-n8 -t amo64_arith -t amo64_arith_lmem -t amo64_bits -t amo64_bits_lmem -t amo64_cas_counter -t amo64_cas_counter_lmem -t amo64_cas_ladder" + tier: full + # SimX timing of the pair atomics (one thread per request, two retirements per + # response, the local-memory wide engine) against the RTL. No LR/SC and no + # compare-and-swap retry loop, so retired instructions match exactly. + - id: parity-amo64 + check: model_parity + via: blackbox + app: amo + xlen: [32] + configs+: "-DVX_CFG_EXT_ZACAS_ENABLE -DVX_CFG_EXT_AMO64_ENABLE" + args: "-n32 -t amo64_arith -t amo64_arith_lmem -t amo64_bits -t amo64_bits_lmem -t amo64_cas_ladder -t amo64_illegal" + tier: full + # The local memory's pair atomics alone: one pass on two banks, then the banks + # held three cycles (a word atomic two) while the 64-bit result is registered + # and written to both. amo64_lmem_stream issues pair atomics back to back + # into each hart's own words, with no retry loop, so retired instructions + # match exactly. + - id: parity-amo64-lmem + check: model_parity + via: blackbox + app: amo + xlen: [32] + configs+: "-DVX_CFG_EXT_ZACAS_ENABLE -DVX_CFG_EXT_AMO64_ENABLE" + args: "-n64 -t amo64_lmem_stream" + touches: [hw/rtl/mem/VX_lmem_unit.sv, sim/simx/mem/local_mem.cpp] + # SimX timing of a misaligned pair atomic against the RTL: the alignment + # check traps, the LSU drops the atomic's packets from the misaligned one on, + # and the high-half uop retires exactly where its atomic did. + - id: parity-amo64-misaligned + check: model_parity + via: blackbox + app: amo + xlen: [32] + configs+: "-DVX_CFG_EXT_ZACAS_ENABLE -DVX_CFG_EXT_AMO64_ENABLE" + args: "-n32 -t amo64_misaligned" + tier: full # Every lane of every warp hammering one word: the LLC bank admits no core # request from an atomic's admission until its writeback re-enters the bank # (the commit occupancy), which paces contended atomics at one per five cycles. @@ -83,6 +154,15 @@ tests: configs+: "-DVX_CFG_EXT_ZACAS_ENABLE -DVX_CFG_SIMD_WIDTH=4 -DVX_CFG_NUM_ALU_LANES=2 -DVX_CFG_NUM_LSU_LANES=2" shape: {threads: 8} args: "-n8 -t amo_misaligned" + # The same for register-pair atomics: the high-half uop of each packet + # retires its old high words exactly where the packet's atomic went. + - id: amo64-misaligned-simd-packets + app: amo + drivers: [simx, rtlsim] + xlen: [32] + configs+: "-DVX_CFG_EXT_ZACAS_ENABLE -DVX_CFG_EXT_AMO64_ENABLE -DVX_CFG_SIMD_WIDTH=4 -DVX_CFG_NUM_ALU_LANES=2 -DVX_CFG_NUM_LSU_LANES=2" + shape: {threads: 8} + args: "-n8 -t amo64_misaligned -t amo64_arith -t amo64_arith_lmem" # Four operand collectors and two SIMD packets per warp (32 warps x 8 threads, # SIMD 4): a three-operand compare-and-swap stalls between its packets on # bank conflicts, and the operand arbiter must keep an instruction's packets diff --git a/docs/designs/atomic_memory_operations.md b/docs/designs/atomic_memory_operations.md index c5d6810031..80af2cda8f 100644 --- a/docs/designs/atomic_memory_operations.md +++ b/docs/designs/atomic_memory_operations.md @@ -86,7 +86,8 @@ so the operation field stays four bits wide. is the `funct5` bits and is otherwise unused — so the comparand reaches the LSU as `rs3_data` with no new operand path. -A misaligned atomic (`.W` not on 4 bytes, `.D` not on 8) is never split: it +A misaligned atomic (`.W` not on 4 bytes, `.D`, native or register pair, not +on 8) is never split: it raises the RISC-V address-misaligned exception, load (`mcause` 4) for `LR`, store/AMO (6) for every other atomic, with `mepc` the atomic. `mtval` stays read-only zero. The trap is precise: no instruction after the atomic runs @@ -107,6 +108,62 @@ before the handler, and the handler may return to `mepc + 4`. The stall costs an atomic a branch's fetch bubble. +**Refused encodings trap.** An encoding the build does not implement raises +the illegal-instruction trap (`mcause` 2, through the same ALU trap path as +`ECALL`): a `funct5` with no operation, `AMOCAS` without Zacas, a `funct3` +other than `.W`/`.D`, and on a 32-bit hart every `.D` atomic unless AMO64 is +built in. Under AMO64, `LR.D`/`SC.D` and a `.D` atomic naming an odd `rd` or +`rs2` still trap. A 32-bit hart also refuses the RV64 load and store encodings +(`LD`, `LWU`, `SD`, and `FLD`/`FSD` when `FLEN` is 32), so on it an LSU op of +64-bit width is only ever a register-pair atomic. + +### 2.1 64-bit atomics on a 32-bit hart (AMO64) + +With `VX_CFG_EXT_AMO64_ENABLE` a 32-bit hart runs the `.D` read-modify-write +atomics and `AMOCAS.D` on even-aligned register pairs — the pairing Zacas +defines for `amocas.d` on RV32: `rs2`/`rs2+1` is the operand (low word in the +even register), `rd`/`rd+1` receives the old value and, for compare-and-swap, +carries the comparand in. `x0` as a pair base names `x0` for both halves. + +Five registers in and two out exceed one instruction's three source slots and +one destination, so the atomic expander +([`VX_uop_amo.sv`](../../hw/rtl/core/VX_uop_amo.sv), SimX `LsuUopGen`) makes +its atomic a burst of three uops: + + check [release fence] CMP AMO HI [acquire fence] + +| uop | reads | writes | does | +|---|---|---|---| +| CMP | `rd`, `rd+1` | — | the LSU stages the 64-bit comparand (used by `AMOCAS.D`); retires like a store | +| AMO | `rs1`, `rs2`, `rs2+1` | `rd` | the atomic; retires the old low word | +| HI | — | `rd+1` | reserves `rd+1`; the LSU drops it and retires the old high word into `rd+1` from the AMO's response | + +CMP leads every pair atomic, not only compare-and-swap: AMO's own register +check covers `rd` alone, and an older still-pending write of `rd+1` (a load +that missed) would otherwise land after HI's reservation, clobbering the high +word and leaving `rd+1` reserved forever. Reading `rd+1` in CMP orders the burst +behind it. + +The burst holds the issue slot's lock (`fu_lock` on CMP, `fu_unlock` on HI). +The LSU keeps one staging entry per (issue slot within the block, operand +collector, packet): a warp's uops leave its collector in order and the lock +keeps another warp of the slot from issuing inside the burst, so the entry +cannot be overwritten in between. The entry holds the staged comparand and +whether the AMO's packet faulted: a misaligned pair atomic is checked and +dropped like a word atomic, and its HI uop, which otherwise only waits for the +AMO's response, retires writing nothing exactly for the packets the AMO +dropped. Packets ahead of the misaligned one went to memory, and their lanes +get both old words. + +HI reserves `rd+1` when it issues, behind the AMO, so the AMO's last high-word +response can reach the LSU first. `rd+1` is released by whichever of the two +arrives last. If HI arrives first, it marks the AMO's packet slot (recorded in +the staging entry) and the response retires as usual. If the response arrives +first, it writes `rd+1` without ending the instruction and marks the staging +entry; HI then retires writing nothing, which releases `rd+1`. The memory +response path never waits on HI. AMO64 needs FLEN 32: the LSU recognizes a +pair atomic by its 8-byte operation type, which an `FLD` would otherwise share. + --- ## 3. The LSU sideband @@ -118,7 +175,8 @@ per lane into the lane's memory attribute | field | width | meaning | |---|---|---| | `hart_id` | `NC_BITS + NW_BITS + NT_BITS` | `{core, warp, thread}` — the reservation owner | -| `amo_cmp` | `XLEN` | compare-and-swap comparand; present only with Zacas | +| `amo_cmp` | `XLEN`, 64 with AMO64 | compare-and-swap comparand; present only with Zacas | +| `amo_wide` | 1 | AMO64 only: the lane is half of a 64-bit atomic on a 32-bit hart | | `amo_unsigned` | 1 | unsigned `MIN`/`MAX` | | `amo_op` | 4 | `amo_op_e` | | `amo_valid` | 1 | the lane is an atomic; sits at `MEM_ATTR_AMO_OFFS` | @@ -141,7 +199,23 @@ Load-bearing properties: `hw/rtl/libs/` contain no atomic logic. The coalescer is told only `in_req_no_merge` ([`VX_mem_unit.sv`](../../hw/rtl/core/VX_mem_unit.sv)): an atomic lane is never merged, because two read-modify-writes to one word - are two operations, not one. + are two operations, not one — except the two halves of an AMO64 atomic. + +**AMO64 requests.** A 32-bit hart's LSU word is four bytes, so a 64-bit atomic +rides two lanes, as a 64-bit access does on NVIDIA's 32-bit banks: the LSU +issues one thread per request on the even/odd lane pair holding the thread's +own lane (the low word on the even lane), both marked `amo_wide` and carrying +the thread's attribute and 64-bit comparand. Staying on the thread's lane pair +keeps the atomic behind the thread's older accesses to the same word, which +travel the same lane, through the coalescer group and the cache port. The dcache coalescer fuses the pair into one 8-byte request (its +byte enables make the commit point a `.D`), and splits the old value back onto +the two lanes. The LSU retires each response twice, the low word into `rd`, +then the high word into `rd+1`. One thread per request keeps two threads' +atomics to one word from ever being fused; the alignment field of the request +tag, unused by an 8-byte-aligned pair, names the thread. Wider issue (several +pairs per request) is not implemented, and the local memory relies on it: its +wide path serves exactly one pair per pass (a runtime assertion in +`VX_lmem_unit`). A virtual-memory build checks an atomic's permissions as a **write**: the request arrives with `rw = 0`, so the MMU takes the write intent from the @@ -180,10 +254,10 @@ Two parameters keep the datapath no wider than the build needs: - `DATA_WIDTH` — the operand width, the cache word capped at 64. A 32-bit word can only carry `.W`, so the adder and comparators are built 32-bit. - `ARITH_WIDTH` — the width of every operation **except** compare-and-swap. - A 32-bit hart issues 64-bit operands only for the Zacas pair, so its adder - and comparators shrink to 32 bits while the equality compare stays full - width. The bank asserts that no `.D` arithmetic atomic reaches a narrowed - build. + A 32-bit hart without AMO64 issues no 64-bit atomic, so its adder and + comparators shrink to 32 bits (the equality compare stays full width); the + bank asserts that no `.D` atomic reaches such a build. With AMO64 the + arithmetic is 64-bit. **A failed compare-and-swap still stores.** On a mismatch the old value is written back unchanged rather than the store being suppressed. The commit @@ -368,6 +442,29 @@ A plain store reaches the reservation stage the same way an atomic does, one cycle after acceptance, so a plain store to a reserved word breaks the reservation. +**Wide atomics (AMO64).** The two words of a 64-bit atomic on a 32-bit hart sit +in adjacent banks on one row, and arrive as one warp request whose two lanes +(the low word on the even lane) are marked `amo_wide`. The engine serves a +request whole and in order, after the issuing block's older requests, and a +store or atomic only once the read engine has drained, so the atomic is +already behind every older access to its words; its one pass gives the two +lanes their two banks. The banks do their atomics word by word, so the pair's +read-modify-write is done apart, in `VX_lmem_unit`: + +| cycle | banks | wide read-modify-write | +|---|---|---| +| 1 — read | both words are read; the reads are the response, on both lanes | the reservation stage of each bank sees an atomic: a reservation on either word breaks | +| 2 — capture | — | both words are registered | +| 3 — compute | — | the 64-bit result (`VX_amo_alu`, 64-bit) is registered, keeping the adder off the bank write path | +| 4 — write back | the even bank takes the low word, the odd bank the high word | — | + +No pass and no DMA access is issued in the three cycles after a wide atomic's +pass, one more than after a word atomic's. The performance counters count a +wide atomic as its two lanes, both reads (atomics carry `rw = 0`). + +Without AMO64 the attribute carries no wide flag and the wide path is not +built: the banks and their timing are those of a build without it. + --- ## 8. SimX model @@ -380,6 +477,7 @@ reservation. | LLC commit | `VX_cache_amo`, commit role | `CacheBank::commitAmo()` | | above the LLC | `VX_cache_amo`, passthrough role | `AmoProbe` request type | | local memory | 3-cycle in-place RMW | same occupancy, modeled as a per-bank busy count | +| AMO64 pair | the thread's lane pair, fused by the coalescer; at local memory one pass on two banks and a 64-bit read-modify-write | one 8-byte request on the thread's lane; at local memory one pass, the banks held one cycle longer than for a word atomic, as the RTL does | | reservations | bounded line-indexed stations with hold credit | one reservation **per hart** | Two of these differences matter when mirroring logic between the models: @@ -407,6 +505,7 @@ Two of these differences matter when mirroring logic between the models: |---|---|---| | `VX_CFG_EXT_A_ENABLE` | false | the A extension; sets `MISA` bit 0 | | `VX_CFG_EXT_ZACAS_ENABLE` | false | compare-and-swap; requires A, widens every memory request by `XLEN` | +| `VX_CFG_EXT_AMO64_ENABLE` | false | 64-bit atomics on a 32-bit hart (§2.1); requires A, `XLEN` 32, `FLEN` 32, an even number of LSU lanes, a dcache word of at least 8 bytes and, with local memory, at least two local-memory banks; `AMOCAS.D` also needs Zacas | | `VX_CFG_AMO_RS_SIZE` | `max(NUM_WARPS, NUM_CORES × NUM_WARPS / L2_NUM_BANKS)` | reservation stations per LLC bank | `AMO_ENABLE` is a **parameter** on the cache and local-memory modules, driven @@ -449,8 +548,14 @@ is the LLC. | 13, 14 | `amoadd_lmem`, `lrsc_lmem` | cases 0 and 7 on local memory | | 15, 16 | `cas_ladder`, `cas_lmem` | every hart races compare-and-swap for each slot; compiled only with Zacas | | 17, 18 | `cas_counter`, `cas_counter_lmem` | compare-and-swap increment from a nonzero base | +| `amo64_*` | `arith`, `bits` (global and local), `cas_counter`, `cas_counter_lmem`, `cas_ladder` | every 64-bit atomic, contended; operands that make each half matter, and every returned old value checked; on a 64-bit hart, or a 32-bit one with AMO64 | +| `amo64_illegal` | a 32-bit hart's refused `.D` encodings | `LR.D`, an odd `rd` or `rs2` pair (AMO64) or any `.D` atomic (without), trap with `mcause` 2 and leave memory alone | +| `amo64_war`, `amo64_war_lmem` | a load into `rd+1` that misses, then `amoadd.d` | the atomic waits for the older write of `rd+1`; every old value decodable and the counter exact | +| `amo64_store_amo`, `amo64_store_amo_lmem` | a thread stores 0 then adds 1 to its own word | the atomic stays behind the thread's store (lane pair, the local memory's in-order engine) | +| `amo64_x0` | `x0` as a pair's destination | the old value is discarded and `x0` stays zero | | `amoadd_scatter`, `amo_line_burst` | atomics into more lines than the dcache holds; groups of harts adding into distinct words of cold lines | the counts, with missing atomics replaying back to back into the LLC | | last | `amo_misaligned` | `amoadd.w`, `lr.w`, `sc.w`, `amocas.w` at misaligned addresses; a warp whose lane 1 alone is misaligned; a misaligned lane opening the second LSU packet, and the second SIMD slice: `mcause`, `mepc` (and `mtval` 0), the instruction after the atomic running once and only after the handler, `rd` and every word of memory | +| `amo64_misaligned` | `amoadd.d` word aligned but not doubleword aligned: every lane, a misaligned lane opening the second LSU packet, and the second SIMD slice; misaligned and aligned warps side by side | `mcause`, `mepc`, the instruction after the atomic running once, `rd` and `rd+1`, and every word of memory: only the lanes ahead of the misaligned packet or slice, and the aligned warps, perform; AMO64 only | The `amo` suite in [`ci/testcases/isa/amo.yaml`](../../ci/testcases/isa/amo.yaml) runs them on both simulators at both `XLEN`s, with and without Zacas, on a diff --git a/hw/rtl/VX_gpu_pkg.sv b/hw/rtl/VX_gpu_pkg.sv index 1b3d5d594c..0a52d44343 100644 --- a/hw/rtl/VX_gpu_pkg.sv +++ b/hw/rtl/VX_gpu_pkg.sv @@ -274,10 +274,27 @@ package VX_gpu_pkg; // word already carries the swap value, so the comparand travels here. It // must reach wherever the read-modify-write commits, which is the local // bank for shared memory and the last-level bank for global memory. + // A 64-bit atomic on a 32-bit hart (AMO64) carries its operand on two + // adjacent lanes, low word first, that the dcache coalescer fuses into one + // 8-byte request and the local memory serves as one; amo_wide marks both + // lanes. Its comparand is 64 bits, so the sideband widens with it. +`ifdef VX_CFG_EXT_AMO64_ENABLE + localparam AMO_CMP_BITS = 64; + `PACKAGE_ASSERT(`VX_CFG_XLEN == 32) + `PACKAGE_ASSERT(`VX_CFG_EXT_A_ENABLED != 0) + // The LSU tells a pair atomic by its 8-byte operation type, which on a + // 32-bit hart an FLD would otherwise share. + `PACKAGE_ASSERT(`VX_CFG_FLEN == 32) +`else + localparam AMO_CMP_BITS = `VX_CFG_XLEN; +`endif typedef struct packed { logic [HART_ID_WIDTH-1:0] hart_id; `ifdef VX_CFG_EXT_ZACAS_ENABLE - logic [`VX_CFG_XLEN-1:0] amo_cmp; + logic [AMO_CMP_BITS-1:0] amo_cmp; + `endif + `ifdef VX_CFG_EXT_AMO64_ENABLE + logic amo_wide; `endif logic amo_unsigned; amo_op_e amo_op; @@ -1509,8 +1526,13 @@ package VX_gpu_pkg; // (the expander clears it), so the LSU stays a general-purpose unit. // Spends two of the struct's spare padding bits -- INST_ARGS_BITS is // unchanged (the PACKAGE_ASSERT below proves it). + // amo_pair marks the companion uops of a register-pair atomic (AMO64, + // VX_uop_amo): with is_store, the comparand stage (rd/rd+1 ride rs2/rs3 + // and are held for the atomic); without, the high-half uop that reserves + // rd+1 and retires with the atomic's response. Neither sends a memory + // request. It spends the struct's last spare bit. typedef struct packed { - logic [(INST_ARGS_BITS-1-1-12-2-1-1-$bits(amo_op_e)-2-2)-1:0] __padding; + logic amo_pair; logic [1:0] export_mask; logic amo_valid; logic amo_unsigned; diff --git a/hw/rtl/cache/VX_amo_alu.sv b/hw/rtl/cache/VX_amo_alu.sv index ae09fb7674..9d62f743e7 100644 --- a/hw/rtl/cache/VX_amo_alu.sv +++ b/hw/rtl/cache/VX_amo_alu.sv @@ -23,9 +23,9 @@ // at 64): a 32-bit-word cache can only carry .W atomics, so the adder and // comparators are built 32-bit rather than 64-bit. width selects .W vs .D and // is only meaningful when DATA_WIDTH > 32. ARITH_WIDTH bounds every op except -// compare-and-swap: a 32-bit hart issues 64-bit operands only for the Zacas -// pair, so its adder and comparators shrink to 32 bits while the CAS equality -// keeps the full width. +// compare-and-swap: a 32-bit hart without AMO64 issues no 64-bit atomic, so +// its adder and comparators shrink to 32 bits while the CAS equality keeps the +// full width. module VX_amo_alu import VX_gpu_pkg::*; #( parameter DATA_WIDTH = 64, parameter ARITH_WIDTH = DATA_WIDTH @@ -67,20 +67,50 @@ module VX_amo_alu import VX_gpu_pkg::*; #( wire signed [ARW-1:0] a_s = arith_w ? {{(ARW-32){a_v[31]}}, a_v[31:0]} : a_v; wire signed [ARW-1:0] b_s = arith_w ? {{(ARW-32){b_v[31]}}, b_v[31:0]} : b_v; + // A 64-bit sum and comparison are built from two 32-bit halves side by + // side, so neither waits on a 64-bit carry chain: the high half's sum is + // selected by the low half's carry, and the high half's comparison decides + // unless its halves are equal. + wire [ARW-1:0] sum; + wire lt_u, lt_s; + // MAX keeps the old value (else rhs); where the two are equal it returns either + wire max_old_u, max_old_s; + if (ARW > 32) begin : g_split + wire [32:0] lo_sum = {1'b0, a_u[31:0]} + {1'b0, b_u[31:0]}; + wire [ARW-33:0] hi_sum0 = a_u[ARW-1:32] + b_u[ARW-1:32]; + wire [ARW-33:0] hi_sum1 = a_u[ARW-1:32] + b_u[ARW-1:32] + (ARW-32)'(1); + assign sum = {(lo_sum[32] ? hi_sum1 : hi_sum0), lo_sum[31:0]}; + wire lo_lt = (a_u[31:0] < b_u[31:0]); + wire [ARW-33:0] a_hu = a_u[ARW-1:32]; + wire [ARW-33:0] b_hu = b_u[ARW-1:32]; + wire signed [ARW-33:0] a_hs = a_s[ARW-1:32]; + wire signed [ARW-33:0] b_hs = b_s[ARW-1:32]; + assign lt_u = (a_hu < b_hu) || ((a_hu == b_hu) && lo_lt); + assign lt_s = (a_hs < b_hs) || ((a_hs == b_hs) && lo_lt); + assign max_old_u = ~lt_u; + assign max_old_s = ~lt_s; + end else begin : g_whole + assign sum = a_u + b_u; + assign lt_u = (a_u < b_u); + assign lt_s = (a_s < b_s); + assign max_old_u = (a_u > b_u); + assign max_old_s = (a_s > b_s); + end + reg [ARW-1:0] arith_res; always @(*) begin case (op) AMO_OP_LR: arith_res = a_u; AMO_OP_SC: arith_res = b_u; AMO_OP_SWAP: arith_res = b_u; - AMO_OP_ADD: arith_res = a_u + b_u; + AMO_OP_ADD: arith_res = sum; AMO_OP_AND: arith_res = a_u & b_u; AMO_OP_OR: arith_res = a_u | b_u; AMO_OP_XOR: arith_res = a_u ^ b_u; - AMO_OP_MIN: arith_res = is_unsigned ? ((a_u < b_u) ? a_u : b_u) - : ((a_s < b_s) ? a_s : b_s); - AMO_OP_MAX: arith_res = is_unsigned ? ((a_u > b_u) ? a_u : b_u) - : ((a_s > b_s) ? a_s : b_s); + AMO_OP_MIN: arith_res = is_unsigned ? (lt_u ? a_u : b_u) + : (lt_s ? a_s : b_s); + AMO_OP_MAX: arith_res = is_unsigned ? (max_old_u ? a_u : b_u) + : (max_old_s ? a_s : b_s); default: arith_res = a_u; endcase if (arith_w) arith_res = {{(ARW-32){1'b0}}, arith_res[31:0]}; diff --git a/hw/rtl/cache/VX_cache_amo.sv b/hw/rtl/cache/VX_cache_amo.sv index 72c67ed10d..e82b0e7590 100644 --- a/hw/rtl/cache/VX_cache_amo.sv +++ b/hw/rtl/cache/VX_cache_amo.sv @@ -202,6 +202,17 @@ module VX_cache_amo import VX_gpu_pkg::*; #( localparam BIT_OFF_BITS = `CLOG2(WORD_WIDTH); localparam LANE_OFF_BITS = `CLOG2(LANE_WIDTH); localparam AMO_OLD_BITS = (LANE_WIDTH < 64) ? LANE_WIDTH : 64; + `ifdef VX_CFG_EXT_AMO64_ENABLE + // the wide flag only steers the coalescer; the width is the byteen's + `UNUSED_VAR (amo_st1.amo_wide) + `endif + // A 32-bit hart issues 64-bit atomics only with AMO64 (register + // pairs); otherwise the arithmetic is register-wide. + `ifdef VX_CFG_EXT_AMO64_ENABLE + localparam AMO_ARITH_BITS = AMO_OLD_BITS; + `else + localparam AMO_ARITH_BITS = `MIN(`VX_CFG_XLEN, AMO_OLD_BITS); + `endif // Writeback queue: a completed AMO pushes its result here instead of // overwriting a still-draining writeback. The head (slot 0) drains @@ -549,7 +560,7 @@ module VX_cache_amo import VX_gpu_pkg::*; #( .NUM_RES_ENTRIES (NUM_RES_ENTRIES), .LINE_ADDR_BITS (LINE_ADDR_BITS), .DATA_WIDTH (AMO_OLD_BITS), // 32-bit word cache -> 32-bit RMW datapath - .ARITH_WIDTH (`MIN(`VX_CFG_XLEN, AMO_OLD_BITS)) // only the Zacas pair is wider than a register + .ARITH_WIDTH (AMO_ARITH_BITS) ) amo_unit ( .clk (clk), .reset (reset), @@ -892,11 +903,11 @@ module VX_cache_amo import VX_gpu_pkg::*; #( || (&lane_byteen_st1) || (lane_byteen_st1 == LANE_SIZE'(4'hF)) || ((LANE_SIZE == 8) && (lane_byteen_st1 == LANE_SIZE'(8'hF0))), ("%t: AMO operand is not a naturally aligned word or doubleword (addr=0x%0h)", $time, addr_st1)) - // The narrowed arithmetic width rests on the ISA: on a 32-bit hart - // only the Zacas pair carries a 64-bit operand. - if (`MIN(`VX_CFG_XLEN, AMO_OLD_BITS) < AMO_OLD_BITS) begin : g_arith_width_guard - `RUNTIME_ASSERT (~(do_store_st1 && (width_st1 == 2'd3) && (amo_st1.amo_op != AMO_OP_CAS)), - ("%t: non-CAS 64-bit AMO on a 32-bit hart (addr=0x%0h)", $time, addr_st1)) + // The narrowed arithmetic width rests on the ISA: without AMO64 a + // 32-bit hart refuses every .D atomic, so none reaches the cache. + if (AMO_ARITH_BITS < AMO_OLD_BITS) begin : g_arith_width_guard + `RUNTIME_ASSERT (~(do_store_st1 && (width_st1 == 2'd3)), + ("%t: 64-bit AMO on a 32-bit hart without AMO64 (addr=0x%0h)", $time, addr_st1)) end assign wb_pending = wb_pending_r; assign wb_addr = wb_addr_r; diff --git a/hw/rtl/core/VX_decode.sv b/hw/rtl/core/VX_decode.sv index 7f684ff055..f5ab2e30c7 100644 --- a/hw/rtl/core/VX_decode.sv +++ b/hw/rtl/core/VX_decode.sv @@ -275,10 +275,14 @@ module VX_decode import `ifdef VX_CFG_EXT_A_ENABLE // Map RVA funct5 (instr[31:27]) to amo_op_e + amo_unsigned bit. // MINU/MAXU collapse into MIN/MAX + amo_unsigned=1. + // amo_legal clears for a funct5 with no operation (amocas without Zacas + // included); the width check below adds the funct3 and register rules. amo_op_e amo_type; reg amo_unsigned; + reg amo_legal; always @(*) begin amo_unsigned = 1'b0; + amo_legal = 1'b1; case (funct5) 5'h00: amo_type = AMO_OP_ADD; 5'h01: amo_type = AMO_OP_SWAP; @@ -294,9 +298,25 @@ module VX_decode import `ifdef VX_CFG_EXT_ZACAS_ENABLE 5'h05: amo_type = AMO_OP_CAS; `endif - default: amo_type = AMO_OP_LR; + default: begin amo_type = AMO_OP_LR; amo_legal = 1'b0; end endcase end + // From funct5, not amo_type: the case's output is too deep for the illegal + // flag. They differ only where amo_legal already clears. + wire amo_is_lrsc = (funct5 == 5'h02) || (funct5 == 5'h03); +`ifdef VX_CFG_XLEN_64 + wire amo_width_ok = (funct3 == 3'h2) || (funct3 == 3'h3); + `UNUSED_VAR (amo_is_lrsc) +`elsif VX_CFG_EXT_AMO64_ENABLE + // A .D atomic on a 32-bit hart names even-aligned register pairs (the + // VX_uop_amo expander splits it); LR.D and SC.D have no pair form. + wire amo_width_ok = (funct3 == 3'h2) + || ((funct3 == 3'h3) && ~amo_is_lrsc && ~rd[0] && ~rs2[0]); +`else + wire amo_width_ok = (funct3 == 3'h2); + `UNUSED_VAR (amo_is_lrsc) +`endif + wire amo_ok = amo_legal && amo_width_ok; `endif `ifdef VX_CFG_EXT_C_ENABLE @@ -305,6 +325,40 @@ module VX_decode import wire decode_is_rvc = 1'b0; `endif + // A 32-bit hart has no 64-bit integer access (ld, lwu, sd), and its + // floating-point loads and stores are FLEN wide. +`ifdef VX_CFG_XLEN_32 + // flw/fsw, fld/fsd with a 64-bit FLEN, and Zfhmin's flh/fsh + // Written as gates of the funct3 bits: compare nodes would sit in series + // with the opcode decode on the illegal flag's path. + wire ls_fp_ok = ~funct3[2] && ((funct3[1] && ~funct3[0]) + || ((`VX_CFG_FLEN == 64) && funct3[1] && funct3[0]) + || (`VX_CFG_EXT_ZFHMIN_ENABLED && ~funct3[1] && funct3[0])); + // integer loads lb/lh/lw/lbu/lhu (0, 1, 2, 4, 5); stores sb/sh/sw (0, 1, 2) + wire ld_ok = opcode[2] ? ls_fp_ok : ~(funct3[1] && (funct3[0] || funct3[2])); + wire st_ok = opcode[2] ? ls_fp_ok : ~(funct3[2] || (funct3[1] && funct3[0])); +`else + wire ld_ok = 1'b1; + wire st_ok = 1'b1; +`endif + + // A load, store or AMO encoding the decoder refuses. It is decided from the + // raw fields beside the opcode case, whose arms decode unconditionally, so + // the width checks stay off the arms' data path. +`ifdef VX_CFG_EXT_F_ENABLE + wire is_load = (opcode == INST_L) || (opcode == INST_FL); + wire is_store = (opcode == INST_S) || (opcode == INST_FS); +`else + wire is_load = (opcode == INST_L); + wire is_store = (opcode == INST_S); +`endif +`ifdef VX_CFG_EXT_A_ENABLE + wire amo_illegal = (opcode == INST_AMO) && ~amo_ok; +`else + wire amo_illegal = 1'b0; +`endif + wire lsa_illegal = (is_load && ~ld_ok) || (is_store && ~st_ok) || amo_illegal; + fence_op_t fence_op; always @(*) begin @@ -326,6 +380,10 @@ module VX_decode import // loads/stores/fences/packed-loads don't propagate 'x through op_args.lsu.amo_valid = 1'b0; `endif + `ifdef VX_CFG_EXT_AMO64_ENABLE + // the pair-uop marker is set only by the expander + op_args.lsu.amo_pair = 1'b0; + `endif `ifdef VX_CFG_EXT_OM_ENABLE // Same reason: op_args defaults to 'x, and a stale export_mask makes // VX_gfx_uops expand an ORDINARY store into an aperture record -- @@ -1144,10 +1202,11 @@ module VX_decode import // An illegal instruction, or an F/D instruction while mstatus.FS is // Off, becomes a trap uop: no operands, no writeback. - trap_uop = illegal || (is_fp_inst && fs_off_w); + trap_uop = illegal || lsa_illegal || (is_fp_inst && fs_off_w); if (trap_uop) begin ex_type = EX_ALU; op_type = INST_OP_BITS'(INST_BR_ILLEGAL); + op_args = '0; op_args.br.xtype = ALU_TYPE_BRANCH; op_args.br.use_PC = 1; op_args.br.use_imm = 1; diff --git a/hw/rtl/core/VX_lsu_block.sv b/hw/rtl/core/VX_lsu_block.sv index ead2f74e1c..8aaeb24f34 100644 --- a/hw/rtl/core/VX_lsu_block.sv +++ b/hw/rtl/core/VX_lsu_block.sv @@ -96,6 +96,34 @@ module VX_lsu_block import VX_gpu_pkg::*; #( `UNUSED_VAR (execute_if.data.rs3_data) `endif + // Register-pair atomics of a 32-bit hart (AMO64; VX_uop_amo splits them): + // - the AMO (a .D atomic) issues one thread per request, its 64-bit + // operand on the even/odd lane pair holding the thread's own lane (low + // word on the even lane), which the dcache coalescer fuses into one + // 8-byte atomic and the local memory serves as one. Staying on the + // thread's lane pair keeps the atomic behind the thread's older accesses + // to the same word, which travel the same lane. Its response retires + // twice, the old low word into rd, then the old high word into rd+1 as + // the HI uop's retirement; + // - the CMP uop stages the rd pair here (the compare-and-swap comparand) + // and retires like a store; + // - the HI uop only reserved rd+1 and is dropped, except where its AMO's + // packet faulted: there it retires writing nothing. + // On a 32-bit hart op_type LD is only ever a pair atomic: the decoder + // refuses the RV64 load and store encodings, and AMO64 needs FLEN 32, so + // there is no FLD. +`ifdef VX_CFG_EXT_AMO64_ENABLE + wire req_is_pair = execute_if.data.op_args.lsu.amo_valid + && (execute_if.data.op_type == INST_OP_BITS'(INST_LSU_LD)); + wire req_is_pair_cmp = execute_if.data.op_args.lsu.amo_pair && execute_if.data.op_args.lsu.is_store; + wire req_is_pair_hi = execute_if.data.op_args.lsu.amo_pair && ~execute_if.data.op_args.lsu.is_store; + `STATIC_ASSERT (((NUM_LANES % 2) == 0), ("AMO64 needs even/odd LSU lane pairs")) +`else + wire req_is_pair = 1'b0; + wire req_is_pair_cmp = 1'b0; + wire req_is_pair_hi = 1'b0; +`endif + // full address calculation — per-lane AGU (all address forms live in // VX_lsu_agu; this slice contains no address arithmetic). @@ -116,6 +144,47 @@ module VX_lsu_block import VX_gpu_pkg::*; #( ); end + // Pair atomic: one thread per request, lowest pending lane first. + wire [NUM_LANES-1:0] pair_pending; + wire [NUM_LANES-1:0] pair_sel_onehot; + wire [`UP(`CLOG2(NUM_LANES))-1:0] pair_sel; + wire pair_first, pair_last; +`ifdef VX_CFG_EXT_AMO64_ENABLE + function automatic logic at_most_one(input logic [NUM_LANES-1:0] mask); + logic seen, multi; + seen = 1'b0; + multi = 1'b0; + for (integer i = 0; i < NUM_LANES; ++i) begin + multi = multi | (seen & mask[i]); + seen = seen | mask[i]; + end + return ~multi; + endfunction + + reg [NUM_LANES-1:0] pair_issued_r; + // whether the packet's next request is its last, known from the previous + // one, so the request's ready does not wait on the lane selection + reg pair_first_r, pair_last_r; + assign pair_pending = execute_if.data.header.tmask & ~pair_issued_r; + VX_priority_encoder #( + .N (NUM_LANES) + ) pair_sel_enc ( + .data_in (pair_pending), + .onehot_out (pair_sel_onehot), + .index_out (pair_sel), + `UNUSED_PIN (valid_out) + ); + assign pair_first = pair_first_r; + assign pair_last = pair_first_r ? at_most_one(execute_if.data.header.tmask) : pair_last_r; +`else + assign pair_pending = '0; + assign pair_sel_onehot = '0; + assign pair_sel = '0; + assign pair_first = 1'b1; + assign pair_last = 1'b1; + `UNUSED_VAR ({pair_pending, pair_sel_onehot, pair_sel}) +`endif + // The address windows are classified on the address before the stack // interleave, which keeps the interleave off the request's attribute // path. The interleave maps the stack window onto itself, so this holds @@ -205,7 +274,12 @@ module VX_lsu_block import VX_gpu_pkg::*; #( `ifdef VX_CFG_EXT_ZACAS_ENABLE // Compare-and-swap comparand. The decoder routes rd into the third // operand slot for it; every other atomic leaves the slot unread. - assign lane_amo.amo_cmp = execute_if.data.rs3_data[i]; + assign lane_amo.amo_cmp = AMO_CMP_BITS'(execute_if.data.rs3_data[i]); + `endif + `ifdef VX_CFG_EXT_AMO64_ENABLE + // on every lane, whether the pair's or masked: the coalescer reads its + // merge decision from lane 0's attribute + assign lane_amo.amo_wide = req_is_pair; `endif // make_hart_id(cid, wid, tid) — packed concatenation, low bits = tid, // the warp thread: the packet's first thread plus the lane. LR/SC @@ -225,6 +299,95 @@ module VX_lsu_block import VX_gpu_pkg::*; #( assign mem_req_attr[i] = mem_req_attr_struct[i]; end + // The even/odd lanes a pair request occupies are the selected thread's + // own, so each lane pair takes its operands from one of its two lanes and + // no operand crosses lane pairs. + localparam NUM_PAIRS = `UP(NUM_LANES / 2); + wire [NUM_LANES-1:0] pair_lanes; + // the selected thread is its lane pair's odd lane + wire [NUM_PAIRS-1:0] pair_odd; + for (genvar i = 0; i < NUM_LANES; ++i) begin : g_pair_lanes + assign pair_lanes[i] = req_is_pair && (pair_sel_onehot[i & ~1] || pair_sel_onehot[(i | 1) % NUM_LANES]); + end + for (genvar p = 0; p < NUM_PAIRS; ++p) begin : g_pair_odd + assign pair_odd[p] = pair_sel_onehot[(2 * p + 1) % NUM_LANES]; + end + // The selected thread's attribute rides both lanes of its pair, carrying + // the staged 64-bit comparand. + wire [NUM_PAIRS-1:0][MEM_ATTR_WIDTH-1:0] pair_attr; +`ifdef VX_CFG_EXT_AMO64_ENABLE + // One staging entry (comparand, fault) per (issue slot within this block, + // operand collector, packet). A warp's uops leave its collector in order + // and the CMP..HI burst holds the slot's issue lock, so no other pair + // atomic of the same slot and collector can land inside the burst. + localparam BLK_BITS = `CLOG2(`VX_CFG_NUM_LSU_BLOCKS); + localparam STG_WBITS = ISSUE_ISW_BITS + NUM_OPCS_BITS - BLK_BITS; + localparam STG_ADDRW = STG_WBITS + PID_BITS; + localparam STG_SIZE = 1 << STG_ADDRW; + // the warp id's low bits are {collector, issue slot}, the slot's low bits + // the LSU block + `STATIC_ASSERT (((ISSUE_ISW_BITS + NUM_OPCS_BITS) <= NW_BITS) && (BLK_BITS <= ISSUE_ISW_BITS), + ("the staging index does not fit the warp id")) + wire [`UP(STG_ADDRW)-1:0] stg_addr; + if (STG_ADDRW != 0) begin : g_stg_addr + wire [`UP(STG_WBITS)-1:0] stg_wsel; + if (STG_WBITS != 0) begin : g_wsel + assign stg_wsel = execute_if.data.header.wid[BLK_BITS +: STG_WBITS]; + end else begin : g_no_wsel + assign stg_wsel = '0; + end + if (PID_BITS != 0) begin : g_stg_pid + assign stg_addr = STG_ADDRW'({stg_wsel, execute_if.data.header.pid[PID_BITS-1:0]}); + end else begin : g_stg_no_pid + assign stg_addr = STG_ADDRW'(stg_wsel); + end + end else begin : g_stg_addr0 + assign stg_addr = '0; + end + wire [NUM_PAIRS-1:0][63:0] pair_cmp; +`ifdef VX_CFG_EXT_ZACAS_ENABLE + wire [NUM_LANES-1:0][63:0] stg_wdata; + wire [NUM_LANES-1:0][63:0] stg_rdata; + for (genvar i = 0; i < NUM_LANES; ++i) begin : g_stg_wdata + assign stg_wdata[i] = {execute_if.data.rs3_data[i], execute_if.data.rs2_data[i]}; + end + VX_dp_ram #( + .DATAW (NUM_LANES * 64), + .SIZE (STG_SIZE), + .OUT_REG (0), + .LUTRAM (1) + ) pair_cmp_store ( + .clk (clk), + .reset (reset), + .read (1'b1), + .write (execute_if.valid && execute_if.ready && req_is_pair_cmp), + .wren (1'b1), + .waddr (stg_addr), + .wdata (stg_wdata), + .raddr (stg_addr), + .rdata (stg_rdata) + ); + for (genvar p = 0; p < NUM_PAIRS; ++p) begin : g_pair_cmp + assign pair_cmp[p] = stg_rdata[2 * p + 32'(pair_odd[p])]; + end +`else + assign pair_cmp = '0; + `UNUSED_VAR (pair_cmp) +`endif + for (genvar p = 0; p < NUM_PAIRS; ++p) begin : g_pair_attr + mem_bus_attr_t pair_attr_struct; + always @(*) begin + pair_attr_struct = mem_req_attr_struct[2 * p + 32'(pair_odd[p])]; + `ifdef VX_CFG_EXT_ZACAS_ENABLE + pair_attr_struct.amo.amo_cmp = pair_cmp[p]; + `endif + end + assign pair_attr[p] = pair_attr_struct; + end +`else + assign pair_attr = '0; +`endif + // schedule memory request wire mem_req_valid; @@ -261,8 +424,14 @@ module VX_lsu_block import VX_gpu_pkg::*; #( // access size performs no access: its warp traps through the atomic's // alignment check (VX_uop_amo), and this packet and the instruction's // later ones retire writing nothing. The instruction's earlier packets - // have gone to memory. - wire req_fault; + // have gone to memory. A register-pair atomic's HI uop, which otherwise + // retires with the atomic's response, retires writing nothing exactly + // where its atomic did: the atomic records each packet's fault for it. + wire req_fault, hi_fault; + // A pair atomic's HI uop whose rd+1 the atomic's response has already + // written retires writing nothing, which releases rd+1 (see the response). + wire hi_late; + wire hi_retire = hi_fault || hi_late; `ifdef VX_CFG_EXT_A_ENABLE wire req_is_d = (inst_lsu_wsize(execute_if.data.op_type) == 2'd3); wire [NUM_LANES-1:0] req_lane_mis; @@ -279,8 +448,26 @@ module VX_lsu_block import VX_gpu_pkg::*; #( fault_open_r <= req_fault && ~execute_if.data.header.eop; end end +`ifdef VX_CFG_EXT_AMO64_ENABLE + reg [STG_SIZE-1:0] pair_fault_r; + always @(posedge clk) begin + if (reset) begin + pair_fault_r <= '0; + end else if (execute_if.valid && execute_if.ready && req_is_pair) begin + pair_fault_r[stg_addr] <= req_fault; + end + end + assign hi_fault = req_is_pair_hi && pair_fault_r[stg_addr]; + reg [STG_SIZE-1:0] rsp_early_r; + assign hi_late = req_is_pair_hi && execute_if.data.header.eop && rsp_early_r[stg_addr]; +`else + assign hi_fault = 1'b0; + assign hi_late = 1'b0; +`endif `else assign req_fault = 1'b0; + assign hi_fault = 1'b0; + assign hi_late = 1'b0; `endif // fence handling @@ -308,13 +495,16 @@ module VX_lsu_block import VX_gpu_pkg::*; #( // slot its end-of-packet request would have acquired, so the next // instruction's packet could otherwise start in a slot still counting // them, and the instruction's end would retire ahead of their data. - wire fault_hold = req_fault && execute_if.data.header.eop && pkt_open; + wire fault_hold = (req_fault || hi_fault) && execute_if.data.header.eop && pkt_open; // retires through the no-response path without a memory request - wire req_bypass = req_skip || req_fault; + wire req_bypass = req_skip || req_is_pair_cmp || req_fault || hi_retire; wire no_rsp_buf_enable = (mem_req_rw && ~execute_if.data.header.wb) || req_bypass; + // a pair atomic holds the packet until its last thread's request + wire req_done = ~req_is_pair || pair_last; assign mem_req_valid = execute_if.valid && ~req_bypass + && ~req_is_pair_hi && ~(no_rsp_buf_enable && ~no_rsp_buf_ready) && ~fence_lock; @@ -324,16 +514,36 @@ module VX_lsu_block import VX_gpu_pkg::*; #( && ~fault_hold && ~fence_lock; - assign execute_if.ready = (mem_req_ready || req_bypass) - && ~(no_rsp_buf_enable && ~no_rsp_buf_ready) + assign execute_if.ready = ((req_is_pair_hi && ~hi_retire) + || (((mem_req_ready && req_done) || req_bypass) + && ~(no_rsp_buf_enable && ~no_rsp_buf_ready))) && ~fault_hold && ~fence_lock; // A fence is sent on every lane, whatever the thread mask: the block's // older requests travel on all of them, and each copy only follows those // on its own lane. - assign mem_req_mask = req_is_fence ? {NUM_LANES{1'b1}} + assign mem_req_mask = req_is_pair ? pair_lanes + : req_is_fence ? {NUM_LANES{1'b1}} : execute_if.data.header.tmask; + +`ifdef VX_CFG_EXT_AMO64_ENABLE + always @(posedge clk) begin + if (reset) begin + pair_issued_r <= '0; + pair_first_r <= 1'b1; + pair_last_r <= 1'b0; + end else if (mem_req_fire && req_is_pair) begin + pair_issued_r <= pair_last ? '0 : (pair_issued_r | pair_sel_onehot); + pair_first_r <= pair_last; + pair_last_r <= at_most_one(pair_pending & ~pair_sel_onehot); + end + end +`endif + + // a pair atomic's requests carry one packet's sop/eop between them + wire req_sop = execute_if.data.header.sop && (~req_is_pair || pair_first); + wire req_eop = execute_if.data.header.eop && req_done; `ifdef VX_CFG_EXT_A_ENABLE // AMO MemReq.rw is unconditionally 0: a missing line under SC must // miss-and-return-failure, not write-and-succeed. @@ -348,8 +558,17 @@ module VX_lsu_block import VX_gpu_pkg::*; #( wire [NUM_LANES-1:0][REQ_ASHIFT-1:0] req_align; for (genvar i = 0; i < NUM_LANES; ++i) begin : g_mem_req_addr + localparam PAIR_LO = i & ~1; + localparam PAIR_HI = (PAIR_LO + 1) % NUM_LANES; + // the pair's 8-byte address + wire [LSU_ADDR_WIDTH-2:0] pair_addr = pair_odd[i / 2] ? full_addr[PAIR_HI][`VX_CFG_MEM_ADDR_WIDTH-1:REQ_ASHIFT+1] + : full_addr[PAIR_LO][`VX_CFG_MEM_ADDR_WIDTH-1:REQ_ASHIFT+1]; assign req_align[i] = full_addr[i][REQ_ASHIFT-1:0]; - assign mem_req_addr[i] = full_addr[i][`VX_CFG_MEM_ADDR_WIDTH-1:REQ_ASHIFT]; + // the even lane carries the low word, the odd lane the high word; a + // pair that goes to memory is 8-byte aligned (a misaligned one + // faults), so the high word's address only sets the low bit + assign mem_req_addr[i] = pair_lanes[i] ? {pair_addr, 1'(i % 2)} + : full_addr[i][`VX_CFG_MEM_ADDR_WIDTH-1:REQ_ASHIFT]; end // byte enable formatting @@ -418,11 +637,11 @@ module VX_lsu_block import VX_gpu_pkg::*; #( for (genvar i = 0; i < NUM_LANES; ++i) begin : g_missalign wire lsu_req_fire = execute_if.valid && execute_if.ready; `ifdef LSU_WIDE_ENABLE - `RUNTIME_ASSERT((~lsu_req_fire || ~execute_if.data.header.tmask[i] || req_is_fence || req_fault || (full_addr[i] % (1 << req_wsize)) == 0), + `RUNTIME_ASSERT((~lsu_req_fire || ~execute_if.data.header.tmask[i] || req_is_fence || req_fault || req_is_pair_cmp || req_is_pair_hi || (full_addr[i] % (1 << req_wsize)) == 0), ("%t: misaligned memory access, wid=%0d, PC=0x%0h, addr=0x%0h, wsize=%0d! (#%0d)", $time, execute_if.data.header.wid, to_fullPC(execute_if.data.header.PC), full_addr[i], req_wsize, execute_if.data.header.uuid)) `else - `RUNTIME_ASSERT((~lsu_req_fire || ~execute_if.data.header.tmask[i] || req_is_fence || req_fault || (full_addr[i] % (1 << inst_lsu_wsize(execute_if.data.op_type))) == 0), + `RUNTIME_ASSERT((~lsu_req_fire || ~execute_if.data.header.tmask[i] || req_is_fence || req_fault || req_is_pair_cmp || req_is_pair_hi || (full_addr[i] % (1 << inst_lsu_wsize(execute_if.data.op_type))) == 0), ("%t: misaligned memory access, wid=%0d, PC=0x%0h, addr=0x%0h, wsize=%0d! (#%0d)", $time, execute_if.data.header.wid, to_fullPC(execute_if.data.header.PC), full_addr[i], inst_lsu_wsize(execute_if.data.op_type), execute_if.data.header.uuid)) `endif @@ -464,20 +683,38 @@ module VX_lsu_block import VX_gpu_pkg::*; #( `endif default:; endcase + if (pair_lanes[i]) begin + if (pair_odd[i / 2]) begin + mem_req_data[i] = ((i % 2) == 0) ? execute_if.data.rs2_data[(i | 1) % NUM_LANES] + : execute_if.data.rs3_data[(i | 1) % NUM_LANES]; + end else begin + mem_req_data[i] = ((i % 2) == 0) ? execute_if.data.rs2_data[i & ~1] + : execute_if.data.rs3_data[i & ~1]; + end + end end end `endif // multi-packet load responses could return out-of-order. // we should track and flag SOP and EOP responses. + // A pair atomic sends one request per thread, so it needs the tracking + // even for a single packet, and its count spans every thread. +`ifdef VX_CFG_EXT_AMO64_ENABLE + localparam PKT_TRACK = 1; + localparam PKT_CTR_BITS = `CLOG2(`VX_CFG_NUM_THREADS + 1); +`else + localparam PKT_TRACK = (PID_BITS != 0); + localparam PKT_CTR_BITS = PID_BITS + 1; +`endif - if (PID_BITS != 0) begin : g_pid - reg [`VX_CFG_LSU_PENDING_SIZE-1:0][PID_BITS:0] pkt_ctr; + if (PKT_TRACK) begin : g_pid + reg [`VX_CFG_LSU_PENDING_SIZE-1:0][PKT_CTR_BITS-1:0] pkt_ctr; reg [`VX_CFG_LSU_PENDING_SIZE-1:0] pkt_sop, pkt_eop; wire mem_req_rd_fire = mem_req_fire && ~mem_req_rw; - wire mem_req_rd_sop_fire = mem_req_rd_fire && execute_if.data.header.sop; - wire mem_req_rd_eop_fire = mem_req_rd_fire && execute_if.data.header.eop; + wire mem_req_rd_sop_fire = mem_req_rd_fire && req_sop; + wire mem_req_rd_eop_fire = mem_req_rd_fire && req_eop; wire mem_rsp_eop_fire = mem_rsp_fire && mem_rsp_eop; wire mem_rsp_eop_pkt_fire= mem_rsp_fire && mem_rsp_eop_pkt; wire full; @@ -517,10 +754,10 @@ module VX_lsu_block import VX_gpu_pkg::*; #( end if (~rw_collision) begin if (mem_req_rd_fire) begin - pkt_ctr[pkt_waddr] <= pkt_ctr[pkt_waddr] + PID_BITS'(1); + pkt_ctr[pkt_waddr] <= pkt_ctr[pkt_waddr] + PKT_CTR_BITS'(1); end if (mem_rsp_eop_fire) begin - pkt_ctr[pkt_raddr] <= pkt_ctr[pkt_raddr] - PID_BITS'(1); + pkt_ctr[pkt_raddr] <= pkt_ctr[pkt_raddr] - PKT_CTR_BITS'(1); end end end @@ -543,13 +780,24 @@ module VX_lsu_block import VX_gpu_pkg::*; #( assign mem_rsp_sop_pkt = mem_rsp_sop; assign mem_rsp_eop_pkt = mem_rsp_eop; `UNUSED_VAR (pkt_raddr) + `UNUSED_VAR ({req_sop, req_eop}) + end + + // A pair request's lanes are 8-byte aligned, so its alignment field + // carries the thread the request serves instead. + localparam SEL_W = `UP(`CLOG2(NUM_LANES)); + wire [NUM_LANES-1:0][REQ_ASHIFT-1:0] req_align_tag = req_is_pair ? (NUM_LANES * REQ_ASHIFT)'(pair_sel) : req_align; + + wire [NUM_LANES-1:0][MEM_ATTR_WIDTH-1:0] mem_req_attr_out; + for (genvar i = 0; i < NUM_LANES; ++i) begin : g_mem_req_attr_out + assign mem_req_attr_out[i] = pair_lanes[i] ? pair_attr[i / 2] : mem_req_attr[i]; end // pack memory request tag assign mem_req_tag = { execute_if.data.header, execute_if.data.op_type, - req_align, + req_align_tag, pkt_waddr, req_is_fence }; @@ -587,7 +835,7 @@ module VX_lsu_block import VX_gpu_pkg::*; #( assign client_if.req_data.mask = mem_req_mask; assign client_if.req_data.byteen = mem_req_byteen; assign client_if.req_data.addr = mem_req_addr; - assign client_if.req_data.attr = mem_req_attr; + assign client_if.req_data.attr = mem_req_attr_out; assign client_if.req_data.data = mem_req_data; assign client_if.req_data.tag = mem_req_tag; assign mem_req_ready = client_if.req_ready; @@ -614,6 +862,95 @@ module VX_lsu_block import VX_gpu_pkg::*; #( rsp_is_fence } = mem_rsp_tag; + wire [SEL_W-1:0] rsp_pair_sel = SEL_W'(rsp_align); + wire [SEL_W-1:0] rsp_pair_lo = rsp_pair_sel & ~SEL_W'(1); + + // A pair response retires in two beats: the old low word into rd, then the + // old high word into rd+1, which is the HI uop's retirement. + wire rsp_is_pair; + wire rsp_hi; + // the instruction's last high-word beat leaves rd+1 reserved + wire rsp_defer; + wire rsp_buf_ready; +`ifdef VX_CFG_EXT_AMO64_ENABLE + reg rsp_hi_r; + assign rsp_is_pair = (rsp_op_type == INST_LSU_BITS'(INST_LSU_LD)); + assign rsp_hi = rsp_hi_r; + always @(posedge clk) begin + if (reset) begin + rsp_hi_r <= 1'b0; + end else if (mem_rsp_valid && rsp_buf_ready && rsp_is_pair) begin + rsp_hi_r <= ~rsp_hi_r; + end + end + `RUNTIME_ASSERT(~(mem_rsp_valid && rsp_is_pair) || (mem_rsp_mask[rsp_pair_lo +: 2] == 2'b11), + ("%t: pair atomic response without both halves (mask=%b)", $time, mem_rsp_mask)) + + // rd+1 is reserved by the HI uop when it issues, behind the atomic, so the + // atomic's last high-word beat can come back first. rd+1 is released by + // whichever of the two reaches this block last: + // - HI first: it marks the atomic's packet slot, which the atomic left in + // their staging entry and holds until that beat retires; the beat then + // retires as usual; + // - the beat first: it writes rd+1 but does not end the instruction, and + // marks the staging entry; HI then retires writing nothing (hi_late). + // A later pair atomic of the entry reaches this block after HI, so each + // mark has one owner. + wire [`UP(STG_ADDRW)-1:0] rsp_stg_addr; + if (STG_ADDRW != 0) begin : g_rsp_stg_addr + wire [`UP(STG_WBITS)-1:0] stg_wsel; + if (STG_WBITS != 0) begin : g_wsel + assign stg_wsel = rsp_hdr.wid[BLK_BITS +: STG_WBITS]; + end else begin : g_no_wsel + assign stg_wsel = '0; + end + if (PID_BITS != 0) begin : g_stg_pid + assign rsp_stg_addr = STG_ADDRW'({stg_wsel, rsp_hdr.pid[PID_BITS-1:0]}); + end else begin : g_stg_no_pid + assign rsp_stg_addr = STG_ADDRW'(stg_wsel); + end + end else begin : g_rsp_stg_addr0 + assign rsp_stg_addr = '0; + end + + reg [STG_SIZE-1:0][LSUQ_SIZEW-1:0] stg_pkt_r; + reg [`VX_CFG_LSU_PENDING_SIZE-1:0] hi_seen_r; + wire hi_fire = execute_if.valid && execute_if.ready && req_is_pair_hi && execute_if.data.header.eop; + wire hi_mark = hi_fire && ~hi_retire; + wire [LSUQ_SIZEW-1:0] hi_slot = stg_pkt_r[stg_addr]; + wire rsp_hi_last = rsp_is_pair && rsp_hi && mem_rsp_eop_pkt; + assign rsp_defer = rsp_hi_last && ~(hi_seen_r[pkt_raddr] || (hi_mark && (hi_slot == pkt_raddr))); + always @(posedge clk) begin + if (mem_req_fire && req_is_pair && req_eop) begin + stg_pkt_r[stg_addr] <= pkt_waddr; + end + end + always @(posedge clk) begin + if (reset) begin + hi_seen_r <= '0; + rsp_early_r <= '0; + end else begin + if (hi_mark) begin + hi_seen_r[hi_slot] <= 1'b1; + end + if (hi_fire && hi_late) begin + rsp_early_r[stg_addr] <= 1'b0; + end + if (mem_rsp_fire && rsp_hi_last) begin + hi_seen_r[pkt_raddr] <= 1'b0; + if (rsp_defer) begin + rsp_early_r[rsp_stg_addr] <= 1'b1; + end + end + end + end +`else + assign rsp_is_pair = 1'b0; + assign rsp_hi = 1'b0; + assign rsp_defer = 1'b0; +`endif + assign mem_rsp_ready = rsp_buf_ready && (~rsp_is_pair || rsp_hi); + // load response formatting reg [NUM_LANES-1:0][`VX_CFG_XLEN-1:0] rsp_data; @@ -681,11 +1018,22 @@ module VX_lsu_block import VX_gpu_pkg::*; #( // result header_t rsp_hdr2; + reg [NUM_LANES-1:0][LSU_RESULT_DATAW-1:0] rsp_result2; + wire [RV_REGS_BITS-1:0] rsp_rd_idx = get_reg_idx(rsp_hdr.rd); always @(*) begin rsp_hdr2 = rsp_hdr; rsp_hdr2.tmask = mem_rsp_mask; rsp_hdr2.sop = mem_rsp_sop_pkt; - rsp_hdr2.eop = mem_rsp_eop_pkt; + rsp_hdr2.eop = mem_rsp_eop_pkt && ~rsp_defer; + rsp_result2 = rsp_result; + if (rsp_is_pair) begin + rsp_hdr2.tmask = NUM_LANES'(1) << rsp_pair_sel; + rsp_result2[rsp_pair_sel] = LSU_RESULT_DATAW'(rsp_hi ? `VX_CFG_XLEN'(mem_rsp_data[rsp_pair_lo + 1]) + : `VX_CFG_XLEN'(mem_rsp_data[rsp_pair_lo])); + if (rsp_hi && (rsp_rd_idx != 0)) begin + rsp_hdr2.rd = make_reg_num(REG_TYPE_I, rsp_rd_idx | RV_REGS_BITS'(1)); + end + end end VX_elastic_buffer #( @@ -695,18 +1043,18 @@ module VX_lsu_block import VX_gpu_pkg::*; #( .clk (clk), .reset (reset), .valid_in (mem_rsp_valid), - .ready_in (mem_rsp_ready), - .data_in ({rsp_hdr2, rsp_result}), + .ready_in (rsp_buf_ready), + .data_in ({rsp_hdr2, rsp_result2}), .data_out (result_rsp_if.data), .valid_out (result_rsp_if.valid), .ready_out (result_rsp_if.ready) ); - // a faulted atomic retires writing nothing + // a faulted atomic, or a late HI uop, retires writing nothing header_t no_rsp_hdr; always @(*) begin no_rsp_hdr = execute_if.data.header; - if (req_fault) begin + if (req_fault || hi_retire) begin no_rsp_hdr.tmask = '0; end end diff --git a/hw/rtl/core/VX_lsu_mem_path.sv b/hw/rtl/core/VX_lsu_mem_path.sv index dbda3e683a..754e43bb2f 100644 --- a/hw/rtl/core/VX_lsu_mem_path.sv +++ b/hw/rtl/core/VX_lsu_mem_path.sv @@ -191,6 +191,18 @@ module VX_lsu_mem_path import VX_gpu_pkg::*; #( if ((`VX_CFG_NUM_LSU_LANES > 1) && (DCACHE_WORD_SIZE > LSU_MEM_WORD_SIZE)) begin : g_coalescing + // An atomic lane is never merged with another, except that the two + // lanes of a 64-bit atomic on a 32-bit hart (AMO64) fuse into the + // one 8-byte operation they encode. + mem_bus_attr_t lane0_attr; + assign lane0_attr = mem_bus_attr_t'(lsu_dcache_word_if.req_data.user[0]); + `ifdef VX_CFG_EXT_AMO64_ENABLE + wire req_no_merge = lane0_attr.amo.amo_valid && ~lane0_attr.amo.amo_wide; + `else + wire req_no_merge = lane0_attr.amo.amo_valid; + `endif + `UNUSED_VAR (lane0_attr) + VX_mem_coalescer #( .INSTANCE_ID (`SFORMATF(("%s-coalescer", INSTANCE_ID))), .NUM_REQS (`VX_CFG_NUM_LSU_LANES), @@ -221,7 +233,7 @@ module VX_lsu_mem_path import VX_gpu_pkg::*; #( .in_req_byteen (lsu_dcache_word_if.req_data.byteen), .in_req_addr (lsu_dcache_word_if.req_data.addr), .in_req_user (lsu_dcache_word_if.req_data.user), - .in_req_no_merge(lsu_dcache_word_if.req_data.user[0][MEM_ATTR_AMO_OFFS]), + .in_req_no_merge(req_no_merge), .in_req_data (lsu_dcache_word_if.req_data.data), .in_req_tag (lsu_dcache_word_if.req_data.tag), .in_req_ready (lsu_dcache_word_if.req_ready), diff --git a/hw/rtl/core/VX_mem_unit.sv b/hw/rtl/core/VX_mem_unit.sv index db02ef7bff..d4d35a5186 100644 --- a/hw/rtl/core/VX_mem_unit.sv +++ b/hw/rtl/core/VX_mem_unit.sv @@ -232,6 +232,11 @@ module VX_mem_unit import VX_gpu_pkg::*; #( end assign busy = (| flush_req_valid); +`ifdef VX_CFG_EXT_AMO64_ENABLE + `STATIC_ASSERT ((`VX_CFG_NUM_LSU_LANES > 1) && (DCACHE_WORD_SIZE >= 8), + ("AMO64 needs the dcache coalescer to fuse a wide atomic's two lanes")) +`endif + // A block's D-cache request carries a clear tag LSB, the flush arbiter's // select bit; the block takes the cache response directly and drops the // flush token's. diff --git a/hw/rtl/core/VX_uop_amo.sv b/hw/rtl/core/VX_uop_amo.sv index 038f90da54..c4bcca39b9 100644 --- a/hw/rtl/core/VX_uop_amo.sv +++ b/hw/rtl/core/VX_uop_amo.sv @@ -30,6 +30,25 @@ // (later reads see what the atomic observed), unless the dcache is itself the // point of coherence. The fences are plain LSU fence ops carrying the acquire // or release fence operation; they read and write no register. +// +// A .D atomic on a 32-bit hart (AMO64) is a register-pair atomic: it reads +// rs1, the rs2 pair and the rd pair (the compare-and-swap comparand), and +// writes the rd pair, more than one uop's three source slots and one +// destination. Its atomic becomes a locked burst: +// +// check [release fence] CMP AMO HI [acquire fence] +// +// CMP : reads rd, rd+1 as rs2/rs3; the LSU holds them as the comparand +// AMO : reads rs1, rs2, rs2+1 as rs3; writes rd (old low word) +// HI : writes rd+1; reserves it until the LSU retires the atomic's old +// high word into it +// +// CMP leads every pair atomic, not only compare-and-swap: reading rd+1 makes +// the burst wait for an older write of rd+1 to land, which AMO's own register +// check (rd only) does not cover and which would otherwise race the atomic's +// high word into rd+1. The LSU holds one comparand per issue slot and operand +// collector, so the lock keeps another warp of the slot from issuing between +// CMP and HI. x0 as a pair base names x0 for both halves. module VX_uop_amo import VX_gpu_pkg::*; ( input clk, @@ -47,14 +66,28 @@ module VX_uop_amo import VX_gpu_pkg::*; ( wire aq = ibuf_in.op_args.lsu.amo_aq && !DCACHE_SHARED_LLC; wire rl = ibuf_in.op_args.lsu.amo_rl && !DCACHE_SHARED_LLC; - assign uop_count = UOP_CTR_W'(2) + UOP_CTR_W'(aq) + UOP_CTR_W'(rl); + wire is_lr = (ibuf_in.op_args.lsu.amo_op == AMO_OP_LR); + wire is_d = (inst_lsu_wsize(INST_LSU_BITS'(ibuf_in.op_type)) == 2'd3); +`ifdef VX_CFG_EXT_AMO64_ENABLE + wire is_pair = is_d; +`else + wire is_pair = 1'b0; +`endif + // the atomic's uops: one, or CMP, AMO and HI + wire [UOP_CTR_W-1:0] amo_len = is_pair ? UOP_CTR_W'(3) : UOP_CTR_W'(1); + assign uop_count = UOP_CTR_W'(1) + amo_len + UOP_CTR_W'(aq) + UOP_CTR_W'(rl); wire [UOP_CTR_W-1:0] amo_idx = UOP_CTR_W'(1) + UOP_CTR_W'(rl); wire is_chk = (uop_idx == '0); wire is_rel = rl && (uop_idx == UOP_CTR_W'(1)); - wire is_acq = aq && (uop_idx == amo_idx + UOP_CTR_W'(1)); - wire is_lr = (ibuf_in.op_args.lsu.amo_op == AMO_OP_LR); - wire is_d = (inst_lsu_wsize(INST_LSU_BITS'(ibuf_in.op_type)) == 2'd3); + wire is_acq = aq && (uop_idx == amo_idx + amo_len); + wire is_cmp = is_pair && (uop_idx == amo_idx); + wire is_hi = is_pair && (uop_idx == amo_idx + UOP_CTR_W'(2)); + + wire [RV_REGS_BITS-1:0] rd_idx = get_reg_idx(ibuf_in.rd); + wire [RV_REGS_BITS-1:0] rs2_idx = get_reg_idx(ibuf_in.rs2); + wire [NUM_REGS_BITS-1:0] rd_hi = (rd_idx != 0) ? make_reg_num(REG_TYPE_I, rd_idx | RV_REGS_BITS'(1)) : '0; + wire [NUM_REGS_BITS-1:0] rs2_hi = (rs2_idx != 0) ? make_reg_num(REG_TYPE_I, rs2_idx | RV_REGS_BITS'(1)) : '0; ibuffer_t ibuf_r; always_comb begin @@ -81,6 +114,30 @@ module VX_uop_amo import VX_gpu_pkg::*; ( ibuf_r.rd_xregs = '0; ibuf_r.wr_xregs = '0; ibuf_r.used_rs = '0; + end else if (is_cmp) begin + ibuf_r.op_args.lsu.amo_valid = 1'b0; + ibuf_r.op_args.lsu.amo_pair = 1'b1; + ibuf_r.op_args.lsu.is_store = 1'b1; + ibuf_r.wb = 1'b0; + ibuf_r.rd = '0; + ibuf_r.rs2 = ibuf_in.rd; + ibuf_r.rs3 = rd_hi; + ibuf_r.used_rs = 3'b110; + ibuf_r.fu_lock = 1'b1; + ibuf_r.fu_unlock = 1'b0; + end else if (is_hi) begin + ibuf_r.op_args.lsu.amo_valid = 1'b0; + ibuf_r.op_args.lsu.amo_pair = 1'b1; + ibuf_r.op_args.lsu.is_store = 1'b0; + ibuf_r.rd = rd_hi; + ibuf_r.used_rs = '0; + ibuf_r.fu_lock = 1'b0; + ibuf_r.fu_unlock = 1'b1; + end else if (is_pair) begin + ibuf_r.rs3 = rs2_hi; + ibuf_r.used_rs = 3'b111; + ibuf_r.fu_lock = 1'b0; + ibuf_r.fu_unlock = 1'b0; end end assign ibuf_out = ibuf_r; diff --git a/hw/rtl/mem/VX_lmem_bank.sv b/hw/rtl/mem/VX_lmem_bank.sv index cc5e150811..5068a15b3b 100644 --- a/hw/rtl/mem/VX_lmem_bank.sv +++ b/hw/rtl/mem/VX_lmem_bank.sv @@ -60,11 +60,17 @@ module VX_lmem_bank import VX_gpu_pkg::*; #( // the read engine's pass input wire b_valid, input wire [BANK_ADDR_WIDTH-1:0] b_addr, - output wire [WORD_WIDTH-1:0] b_rdata + output wire [WORD_WIDTH-1:0] b_rdata, + + // a wide atomic's pass, and its write-back to this bank + input wire wide_pass, + input wire wide_wr, + input wire [BANK_ADDR_WIDTH-1:0] wide_row, + input wire [WORD_WIDTH-1:0] wide_wdata ); localparam AMO_OP_WIDTH = $bits(amo_op_e); `ifdef VX_CFG_EXT_ZACAS_ENABLE - localparam AMO_CMP_WIDTH = `VX_CFG_XLEN; + localparam AMO_CMP_WIDTH = AMO_CMP_BITS; `endif // Without the DMA port no DMA access ever reaches the bank. @@ -130,8 +136,11 @@ module VX_lmem_bank import VX_gpu_pkg::*; #( wire [AMO_OP_WIDTH-1:0] req_amo_op = a_attr[1 +: AMO_OP_WIDTH]; wire req_amo_unsigned = a_attr[0]; + // the wide-atomic bit above the op is VX_lmem_unit's (wide_pass) + `UNUSED_VAR (a_attr) - wire amo_accept = lsu_active && a_amo; + // a wide atomic's lane leaves the read-modify-write to the unit + wire amo_accept = lsu_active && a_amo && ~wide_pass; // Reservation commit stage. Every accepted request that can touch a // reservation -- an atomic, or a plain store -- reaches it one cycle @@ -209,7 +218,7 @@ module VX_lmem_bank import VX_gpu_pkg::*; #( // A load-reserved leaves the word alone, and a failed // store-conditional must not store; every other atomic writes its // computed value back. - assign amo_wb_store = amo_wb_valid_r && ~amo_wb_is_lr_r && ~amo_wb_sc_fail_r; + assign amo_wb_store = (amo_wb_valid_r && ~amo_wb_is_lr_r && ~amo_wb_sc_fail_r) || wide_wr; always @(posedge clk) begin if (reset) begin @@ -246,21 +255,23 @@ module VX_lmem_bank import VX_gpu_pkg::*; #( // shift. The addressed lane is recovered from the byteen: an atomic // is naturally aligned, so a .W to the upper lane never covers byte // 0 and a .D covers the full word. - wire [63:0] amo_old_lane; - wire [63:0] amo_rhs_lane; - wire [1:0] amo_width; + wire [63:0] amo_old_lane; + wire [63:0] amo_rhs_lane; + wire [1:0] amo_width; + wire [WORD_WIDTH-1:0] amo_lane_data; if (WORD_WIDTH == 64) begin : g_amo_lane wire lane_hi = ~amo_wb_byteen_r[0]; assign amo_width = (&amo_wb_byteen_r) ? 2'd3 : 2'd2; assign amo_old_lane = lane_hi ? {32'h0, amo_old_r[63:32]} : amo_old_r; assign amo_rhs_lane = lane_hi ? {32'h0, amo_wb_rhs_r[63:32]} : amo_wb_rhs_r; - assign amo_wb_data = lane_hi ? {amo_new_word[31:0], 32'h0} : amo_new_word; + assign amo_lane_data = lane_hi ? {amo_new_word[31:0], 32'h0} : amo_new_word; end else begin : g_amo_word assign amo_width = 2'd2; assign amo_old_lane = 64'(amo_old_r); assign amo_rhs_lane = 64'(amo_wb_rhs_r); - assign amo_wb_data = amo_new_word[WORD_WIDTH-1:0]; + assign amo_lane_data = amo_new_word[WORD_WIDTH-1:0]; end + assign amo_wb_data = wide_wr ? wide_wdata : amo_lane_data; // VX_CFG_AMO_RS_SIZE sizes ONE shared LLC bank's contention; deployed // per lmem bank it multiplies by NUM_LMEM_BANKS x NUM_CORES. After // the holder-credit protocol a hot word maps to exactly one station, @@ -310,8 +321,8 @@ module VX_lmem_bank import VX_gpu_pkg::*; #( `UNUSED_VAR (amo_new_word[63:WORD_WIDTH]) end - assign amo_wb_addr = amo_wb_addr_r; - assign amo_wb_byteen = amo_wb_byteen_r; + assign amo_wb_addr = wide_wr ? wide_row : amo_wb_addr_r; + assign amo_wb_byteen = wide_wr ? {WORD_SIZE{1'b1}} : amo_wb_byteen_r; end else begin : g_no_amo assign amo_wb_store = 1'b0; @@ -323,6 +334,7 @@ module VX_lmem_bank import VX_gpu_pkg::*; #( assign sc_lane_hi = 1'b0; `UNUSED_VAR ({a_amo, a_attr}) `UNUSED_VAR (req_hart_id) + `UNUSED_VAR ({wide_pass, wide_wr, wide_row, wide_wdata}) `ifdef VX_CFG_EXT_ZACAS_ENABLE `UNUSED_VAR (req_amo_cmp) `endif diff --git a/hw/rtl/mem/VX_lmem_unit.sv b/hw/rtl/mem/VX_lmem_unit.sv index a990750b95..7d5734ac2d 100644 --- a/hw/rtl/mem/VX_lmem_unit.sv +++ b/hw/rtl/mem/VX_lmem_unit.sv @@ -32,6 +32,14 @@ // request is seen, and it is accepted in the cycle that follows. An atomic owns // its bank for three cycles (read, capture, write back), so no pass or DMA access // is issued in the two cycles after an atomic pass. +// +// A 64-bit atomic of a 32-bit hart (AMO64) comes as one request whose two lanes +// hold the low word, on an even word, and the high word: one pass gives them two +// adjacent banks on one row and reads both old words as the response. The +// banks' own atomic stage works a word at a time, so the pair's 64-bit result is +// computed apart, registered to keep the 64-bit ALU off the SRAM write path, and +// written to both banks a cycle later than a word atomic's, so nothing reaches +// the banks in the three cycles after a wide atomic's pass. module VX_lmem_unit import VX_gpu_pkg::*; #( parameter `STRING INSTANCE_ID = "", @@ -90,15 +98,21 @@ module VX_lmem_unit import VX_gpu_pkg::*; #( // Compare-and-swap adds a comparand: the request's data word already // carries the swap value, so the third operand needs its own room. `ifdef VX_CFG_EXT_ZACAS_ENABLE - localparam AMO_CMP_WIDTH = `VX_CFG_XLEN; + localparam AMO_CMP_WIDTH = AMO_CMP_BITS; `else localparam AMO_CMP_WIDTH = 0; +`endif +`ifdef VX_CFG_EXT_AMO64_ENABLE + localparam AMO_WIDE_ENABLE = (AMO_ENABLE != 0) ? 1 : 0; +`else + localparam AMO_WIDE_ENABLE = 0; `endif // per lane: the hart id that holds a reservation, and the comparand localparam LANE_SB_WIDTH = AMO_ENABLE ? (HART_ID_WIDTH + AMO_CMP_WIDTH) : 0; localparam PAYLOAD_WIDTH = WORD_WIDTH + WORD_SIZE + LANE_SB_WIDTH; - // common to all lanes: the atomic op and its signedness - localparam ATTR_WIDTH = AMO_OP_WIDTH + 1; + // common to all lanes: the atomic op, its signedness, and whether it is a + // 64-bit atomic of a 32-bit hart + localparam ATTR_WIDTH = AMO_OP_WIDTH + 1 + AMO_WIDE_ENABLE; // A lane of several words accesses the word it addresses, in its first // word, or reads all of its aligned words: a request's lanes do one or the // other, and the engines serve a whole lane's words in one pass. @@ -131,7 +145,11 @@ module VX_lmem_unit import VX_gpu_pkg::*; #( assign in_rw[k] = lsu_mem_if[k].req_data.rw; assign in_amo[k] = AMO_ENABLE ? attr0.amo.amo_valid : 1'b0; assign in_read[k] = ~in_rw[k] && ~in_amo[k]; + `ifdef VX_CFG_EXT_AMO64_ENABLE + assign in_attr[k] = AMO_ENABLE ? {attr0.amo.amo_wide, attr0.amo.amo_op, attr0.amo.amo_unsigned} : '0; + `else assign in_attr[k] = AMO_ENABLE ? {attr0.amo.amo_op, attr0.amo.amo_unsigned} : '0; + `endif assign in_mask[k] = lsu_mem_if[k].req_data.mask; assign in_tag[k] = lsu_mem_if[k].req_data.tag; assign in_whole[k] = (LANE_WORDS > 1) && lsu_mem_if[k].req_data.byteen[0][LANE_SIZE-1]; @@ -507,6 +525,12 @@ module VX_lmem_unit import VX_gpu_pkg::*; #( // DMA and atomic slots + // a wide atomic's pass reaching the banks, and its write-back to them + wire wide_pass; + wire [NUM_BANKS-1:0] wide_wr; + wire [BANK_ADDR_WIDTH-1:0] wide_row; + wire [NUM_BANKS-1:0][WORD_WIDTH-1:0] wide_wdata; + reg [1:0] amo_stall_r; wire amo_stall = (amo_stall_r != 2'd0); always @(posedge clk) begin @@ -514,7 +538,7 @@ module VX_lmem_unit import VX_gpu_pkg::*; #( amo_stall_r <= 2'd0; end else if (a_pass_amo) begin amo_stall_r <= 2'd2; - end else if (amo_stall) begin + end else if (amo_stall && ~wide_pass) begin amo_stall_r <= amo_stall_r - 2'd1; end end @@ -590,6 +614,140 @@ module VX_lmem_unit import VX_gpu_pkg::*; #( `UNUSED_VAR (per_bank_rdata_a) end + if (AMO_WIDE_ENABLE != 0) begin : g_wide_amo + localparam NUM_PAIRS = NUM_BANKS / 2; + localparam PAIR_DATAW = BANK_ADDR_WIDTH + 2 * WORD_WIDTH; + + `STATIC_ASSERT (NUM_BANKS >= 2, ("wide atomics need two banks for one word pair")) + `STATIC_ASSERT (WORD_WIDTH == 32, ("wide atomics are made of two 32-bit words")) + + assign wide_pass = (| a_bank_valid) && a_bank_amo && a_bank_attr[ATTR_WIDTH-1]; + + // the pass's pair of banks, by its even bank + wire [NUM_PAIRS-1:0] pass_pair; + wire [NUM_PAIRS-1:0][PAIR_DATAW-1:0] pair_req; + wire [NUM_PAIRS-1:0][63:0] pair_rdata; + for (genvar j = 0; j < NUM_PAIRS; ++j) begin : g_pairs + assign pass_pair[j] = a_bank_valid[2*j]; + assign pair_req[j] = {a_bank_addr[2*j], + a_bank_payload[2*j+1][WORD_WIDTH-1:0], + a_bank_payload[2*j][WORD_WIDTH-1:0]}; + assign pair_rdata[j] = {per_bank_rdata_a[2*j+1], per_bank_rdata_a[2*j]}; + end + + // The LSU sends a register pair as a request of its own thread, so a + // pass carries at most one pair: the one-hot selects below would OR + // two pairs' operands together and write one result to both. + `RUNTIME_ASSERT(~wide_pass || $onehot0(pass_pair), + ("%t: *** %s wide atomic pass with more than one register pair: 0x%0h", $time, INSTANCE_ID, pass_pair)) + + wire [BANK_ADDR_WIDTH-1:0] pass_row; + wire [63:0] pass_rhs; + VX_onehot_mux #( + .DATAW (PAIR_DATAW), + .N (NUM_PAIRS) + ) pair_req_mux ( + .data_in (pair_req), + .sel_in (pass_pair), + .data_out ({pass_row, pass_rhs}) + ); + + wire [63:0] pass_cmp; + `ifdef VX_CFG_EXT_ZACAS_ENABLE + // both lanes carry the comparand + wire [NUM_PAIRS-1:0][63:0] pair_cmp; + for (genvar j = 0; j < NUM_PAIRS; ++j) begin : g_pair_cmp + assign pair_cmp[j] = a_bank_payload[2*j][WORD_WIDTH + WORD_SIZE +: AMO_CMP_WIDTH]; + end + VX_onehot_mux #( + .DATAW (64), + .N (NUM_PAIRS) + ) pair_cmp_mux ( + .data_in (pair_cmp), + .sel_in (pass_pair), + .data_out (pass_cmp) + ); + `else + assign pass_cmp = '0; + `endif + + reg rd_valid_r; + reg alu_valid_r; + reg wb_valid_r; + reg [NUM_PAIRS-1:0] pair_r; + reg [BANK_ADDR_WIDTH-1:0] row_r; + reg [AMO_OP_WIDTH-1:0] op_r; + reg unsigned_r; + reg [63:0] rhs_r; + reg [63:0] cmp_r; + reg [63:0] old_r; + reg [63:0] new_r; + + wire [63:0] old_word; + VX_onehot_mux #( + .DATAW (64), + .N (NUM_PAIRS) + ) pair_rdata_mux ( + .data_in (pair_rdata), + .sel_in (pair_r), + .data_out (old_word) + ); + + wire [63:0] new_word; + wire [63:0] ret_word; + VX_amo_alu #( + .DATA_WIDTH (64) + ) amo_alu ( + .op (amo_op_e'(op_r)), + .is_unsigned (unsigned_r), + .width (2'd3), + .old_word (old_r), + .rhs (rhs_r), + .cmp (cmp_r), + .new_word (new_word), + .ret_word (ret_word) + ); + `UNUSED_VAR (ret_word) + + always @(posedge clk) begin + if (reset) begin + rd_valid_r <= 1'b0; + alu_valid_r <= 1'b0; + wb_valid_r <= 1'b0; + end else begin + rd_valid_r <= wide_pass; + alu_valid_r <= rd_valid_r; + wb_valid_r <= alu_valid_r; + end + if (wide_pass) begin + pair_r <= pass_pair; + row_r <= pass_row; + op_r <= a_bank_attr[1 +: AMO_OP_WIDTH]; + unsigned_r <= a_bank_attr[0]; + rhs_r <= pass_rhs; + cmp_r <= pass_cmp; + end + if (rd_valid_r) begin + old_r <= old_word; + end + if (alu_valid_r) begin + new_r <= new_word; + end + end + + for (genvar i = 0; i < NUM_BANKS; ++i) begin : g_wide_wr + assign wide_wr[i] = wb_valid_r && pair_r[i / 2]; + assign wide_wdata[i] = ((i % 2) != 0) ? new_r[63:32] : new_r[31:0]; + end + assign wide_row = row_r; + + end else begin : g_no_wide_amo + assign wide_pass = 1'b0; + assign wide_wr = '0; + assign wide_row = '0; + assign wide_wdata = '0; + end + // banks for (genvar i = 0; i < NUM_BANKS; ++i) begin : g_data_store @@ -620,7 +778,11 @@ module VX_lmem_unit import VX_gpu_pkg::*; #( .a_rdata (a_bank_rdata[i]), .b_valid (b_bank_valid[i]), .b_addr (b_bank_addr[i]), - .b_rdata (b_bank_rdata[i]) + .b_rdata (b_bank_rdata[i]), + .wide_pass (wide_pass), + .wide_wr (wide_wr[i]), + .wide_row (wide_row), + .wide_wdata (wide_wdata[i]) ); end diff --git a/sim/simx/decode.cpp b/sim/simx/decode.cpp index e11670c56b..22a2ef391f 100644 --- a/sim/simx/decode.cpp +++ b/sim/simx/decode.cpp @@ -395,6 +395,8 @@ static op_string_t op_string(const Instr &instr) { } } case LsuType::FENCE: return {"FENCE", ""}; + case LsuType::AMO_CMP: return {"AMO_CMP.D", ""}; + case LsuType::AMO_HI: return {"AMO_HI.D", ""}; default: std::abort(); } @@ -801,9 +803,25 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid, bool fs_off) { case Opcode::FL: case Opcode::S: case Opcode::FS: { - instr->set_fu_type(FUType::LSU); bool is_float = (op == Opcode::FL || op == Opcode::FS); bool is_load = (op == Opcode::L || op == Opcode::FL); + // A 32-bit hart has no 64-bit integer access (ld, lwu, sd), and its + // floating-point loads and stores are FLEN wide, or half wide with Zfhmin. + if (VX_CFG_XLEN == 32) { + bool legal; + if (is_float) { + legal = (funct3 == 2) || (VX_CFG_FLEN == 64 && funct3 == 3) + || (VX_CFG_EXT_ZFHMIN_ENABLED && funct3 == 1); + } else if (is_load) { + legal = (funct3 <= 2) || (funct3 == 4) || (funct3 == 5); + } else { + legal = (funct3 <= 2); + } + if (!legal) { + return this->illegal(uuid, is_rvc); + } + } + instr->set_fu_type(FUType::LSU); { instr->set_src_reg(0, rs1, RegType::Integer); uint32_t imm12 = 0; @@ -844,25 +862,40 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid, bool fs_off) { instr->set_args(args); } break; case Opcode::AMO: { - instr->set_fu_type(FUType::LSU); uint32_t aq = (code >> shift_aq) & mask_aq; uint32_t rl = (code >> shift_rl) & mask_rl; + bool legal = true; + AmoType amo_type = AmoType::LR; switch (funct5) { - case 0x00: instr->set_op_type(AmoType::AMOADD); break; - case 0x01: instr->set_op_type(AmoType::AMOSWAP); break; - case 0x02: instr->set_op_type(AmoType::LR); break; - case 0x03: instr->set_op_type(AmoType::SC); break; - case 0x04: instr->set_op_type(AmoType::AMOXOR); break; - case 0x08: instr->set_op_type(AmoType::AMOOR); break; - case 0x0c: instr->set_op_type(AmoType::AMOAND); break; - case 0x10: instr->set_op_type(AmoType::AMOMIN); break; - case 0x14: instr->set_op_type(AmoType::AMOMAX); break; - case 0x18: instr->set_op_type(AmoType::AMOMINU); break; - case 0x1c: instr->set_op_type(AmoType::AMOMAXU); break; - case 0x05: instr->set_op_type(AmoType::AMOCAS); break; - default: - std::abort(); + case 0x00: amo_type = AmoType::AMOADD; break; + case 0x01: amo_type = AmoType::AMOSWAP; break; + case 0x02: amo_type = AmoType::LR; break; + case 0x03: amo_type = AmoType::SC; break; + case 0x04: amo_type = AmoType::AMOXOR; break; + case 0x08: amo_type = AmoType::AMOOR; break; + case 0x0c: amo_type = AmoType::AMOAND; break; + case 0x10: amo_type = AmoType::AMOMIN; break; + case 0x14: amo_type = AmoType::AMOMAX; break; + case 0x18: amo_type = AmoType::AMOMINU; break; + case 0x1c: amo_type = AmoType::AMOMAXU; break; + case 0x05: amo_type = AmoType::AMOCAS; legal = VX_CFG_EXT_ZACAS_ENABLED; break; + default: legal = false; break; + } + const bool is_lrsc = (amo_type == AmoType::LR || amo_type == AmoType::SC); + // A 64-bit atomic on a 32-bit hart names even-aligned register pairs. LR.D + // and SC.D have no pair form. + const bool is_pair = (VX_CFG_XLEN == 32) && (funct3 == 3); + if (!VX_CFG_EXT_A_ENABLED || (funct3 != 2 && funct3 != 3)) { + legal = false; + } + if (is_pair && !(VX_CFG_EXT_AMO64_ENABLED && !is_lrsc && (rd % 2) == 0 && (rs2 % 2) == 0)) { + legal = false; } + if (!legal) { + return this->illegal(uuid, is_rvc); + } + instr->set_fu_type(FUType::LSU); + instr->set_op_type(amo_type); instr->set_args(IntrAmoArgs{funct3, aq, rl}); // An atomic expands into its alignment check and, with .aq/.rl, the fences // around it (LsuUopGen). The warp fetches on once the check resolves. @@ -871,7 +904,7 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid, bool fs_off) { instr->set_dest_reg(rd, RegType::Integer); instr->set_src_reg(0, rs1, RegType::Integer); instr->set_src_reg(1, rs2, RegType::Integer); - if (funct5 == 0x05) { + if (amo_type == AmoType::AMOCAS) { // Compare-and-swap reads rd as well as writing it: rd carries the // comparand in, the loaded word out. It has to join the source set or // the operand collector supplies a stale comparand and the swap decides diff --git a/sim/simx/lsu_unit.cpp b/sim/simx/lsu_unit.cpp index 49ded56dc5..d8e01e0a5f 100644 --- a/sim/simx/lsu_unit.cpp +++ b/sim/simx/lsu_unit.cpp @@ -34,14 +34,19 @@ using namespace vortex; // An atomic: check [release fence] atomic [acquire fence]. The fences order a // .aq/.rl atomic with the hart's other accesses, unless the dcache is itself -// the point of coherence. +// the point of coherence. On a 32-bit hart a 64-bit atomic is a three-uop +// register-pair burst (amo_pair_uop). static bool amo_fenced(uint32_t bit) { return bit && !DCACHE_SHARED_LLC; } +static uint32_t amo_body_count(const IntrAmoArgs& amo) { + return (VX_CFG_XLEN == 32 && amo.width == 3) ? 3 : 1; +} + uint32_t LsuUopGen::uop_count(const Instr& instr) { if (auto amo = std::get_if(&instr.get_args())) { - return 2 + amo_fenced(amo->aq) + amo_fenced(amo->rl); + return 1 + amo_body_count(*amo) + amo_fenced(amo->aq) + amo_fenced(amo->rl); } // PACKLB.F: width=0 (LB), 4 byte-elements // PACKLH.F: width=1 (LH), 2 halfword-elements @@ -61,6 +66,59 @@ uint32_t LsuUopGen::uop_count(const Instr& instr) { return (args.width == 0) ? 4 : 2; } +// A 64-bit atomic on a 32-bit hart reads rs1, the rs2 pair and the rd pair +// (the compare-and-swap comparand), and writes the rd pair: more than one +// instruction's three source slots and one destination. It issues as a locked +// burst: +// AMO_CMP : reads rd, rd+1 -> the LSU stages them as the comparand +// AMO : reads rs1, rs2, rs2+1; writes rd (the old low word) +// AMO_HI : writes rd+1 (the old high word) +// AMO_CMP leads every pair atomic: reading rd+1 orders the burst after an older +// write of rd+1, which the atomic's high word would otherwise race. AMO_HI +// reserves rd+1 at issue; the LSU retires it with the high half of the AMO's +// response. x0 as a pair base names x0 for both halves. +static Instr::Ptr amo_pair_uop(PoolAllocator& pool, + const Instr& macro_instr, + uint32_t uop_index, + uint64_t uop_uuid) { + auto amo_type = std::get(macro_instr.get_op_type()); + uint32_t role = uop_index; // 0: CMP, 1: AMO, 2: HI + uint32_t rd = macro_instr.get_dest_reg().idx; + uint32_t rs1 = macro_instr.get_src_reg(0).idx; + uint32_t rs2 = macro_instr.get_src_reg(1).idx; + uint32_t rd_hi = rd ? (rd + 1) : 0; + uint32_t rs2_hi = rs2 ? (rs2 + 1) : 0; + + auto uop = std::allocate_shared(pool, uop_uuid, FUType::LSU); + uop->set_parent_uuid(macro_instr.get_uuid()); + switch (role) { + case 0: + uop->set_op_type(LsuType::AMO_CMP); + uop->set_args(IntrLsuArgs{3, 0, 0}); + uop->set_src_reg(1, rd, RegType::Integer); + uop->set_src_reg(2, rd_hi, RegType::Integer); + break; + case 1: + uop->set_op_type(amo_type); + uop->set_args(macro_instr.get_args()); + uop->set_dest_reg(rd, RegType::Integer); + uop->set_src_reg(0, rs1, RegType::Integer); + uop->set_src_reg(1, rs2, RegType::Integer); + uop->set_src_reg(2, rs2_hi, RegType::Integer); + break; + default: + uop->set_op_type(LsuType::AMO_HI); + uop->set_args(IntrLsuArgs{3, 0, 0}); + uop->set_dest_reg(rd_hi, RegType::Integer); + break; + } + // The comparand is staged per issue slot and operand collector, so no other + // warp of the slot may issue inside the burst. + uop->set_fu_lock(role == 0); + uop->set_fu_unlock(role == 2); + return uop; +} + Instr::Ptr LsuUopGen::get(const Instr& macro_instr, uint32_t uop_index) { uint64_t parent_uuid = macro_instr.get_uuid(); uint64_t uop_uuid = make_uop_uuid(parent_uuid, uop_index); @@ -81,9 +139,13 @@ Instr::Ptr LsuUopGen::get(const Instr& macro_instr, uint32_t uop_index) { chk_instr->set_wstall(true); return chk_instr; } + uint32_t amo_index = 1 + amo_fenced(amo->rl); + uint32_t body = amo_body_count(*amo); + if (body > 1 && uop_index >= amo_index && uop_index < amo_index + body) { + return amo_pair_uop(pool_, macro_instr, uop_index - amo_index, uop_uuid); + } auto uop_instr = std::allocate_shared(pool_, uop_uuid, FUType::LSU); uop_instr->set_parent_uuid(parent_uuid); - uint32_t amo_index = 1 + amo_fenced(amo->rl); if (uop_index == amo_index) { uop_instr->set_op_type(macro_instr.get_op_type()); uop_instr->set_args(*amo); @@ -248,6 +310,179 @@ void LsuUnit::on_reset() { drained_sent_ = true; } +bool LsuUnit::is_pair_amo(const instr_trace_t* trace) { + auto* amo_tag = std::get_if(&trace->op_type); + if (amo_tag == nullptr || VX_CFG_XLEN != 32) { + return false; + } + auto amo_args = std::get(trace->instr_ptr->get_args()); + return (amo_args.width == 3); +} + +static uint64_t sext_word(uint32_t value) { + return (uint64_t)(int64_t)(int32_t)value; +} + +// AMO_HI retires the old high words of the threads whose atomic went to +// memory; a faulted thread's lane writes nothing. +static void retire_pair_hi(instr_trace_t* hi_trace, const std::vector& hi, const ThreadMask& hi_mask) { + hi_trace->tmask &= hi_mask; + for (uint32_t t = 0; t < VX_CFG_NUM_THREADS; ++t) { + if (hi_trace->tmask.test(t)) { + hi_trace->dst_data.at(t).u64 = sext_word(hi.at(t)); + } + } +} + +// The pair atomic claims the state its AMO_CMP opened: the oldest of its warp +// and packet not yet claimed. Its lanes left after the alignment check +// (check_amo) are the ones its AMO_HI retires. +LsuUnit::pair_state_t* LsuUnit::claim_pair(uint32_t b, const instr_trace_t* trace) { + auto& state = states_.at(b); + auto it = state.pairs.begin(); + while (it != state.pairs.end() + && !(it->wid == trace->wid && it->pid == trace->pid && !it->has_amo)) { + ++it; + } + if (it == state.pairs.end()) { + std::cout << "Error: pair AMO without its AMO_CMP: " << *trace << std::endl; + std::abort(); + } + it->has_amo = true; + it->hi.assign(VX_CFG_NUM_THREADS, 0); + it->hi_mask = trace->tmask; + return &*it; +} + +// The companion uops of a register-pair atomic. AMO_CMP stages the comparand +// and retires like a store; AMO_HI retires the old high words once the AMO has +// returned every beat, and parks with the pair until then. Returns false on +// output backpressure. +bool LsuUnit::process_pair_uop(uint32_t b, instr_trace_t* trace) { + auto& state = states_.at(b); + // it commits without a response, through the no-response buffer + auto& output = no_rsp_bufs_.at(b); + auto lsu_type = std::get(trace->op_type); + if (lsu_type == LsuType::AMO_CMP) { + if (output.full()) { + return false; + } + pair_state_t ps; + ps.wid = trace->wid; + ps.pid = trace->pid; + ps.cmp.assign(VX_CFG_NUM_THREADS, 0); + for (uint32_t t = 0; t < VX_CFG_NUM_THREADS; ++t) { + if (!trace->tmask.test(t)) { + continue; + } + ps.cmp.at(t) = (uint64_t)(uint32_t)trace->src_data[1][t].u + | ((uint64_t)(uint32_t)trace->src_data[2][t].u << 32); + } + state.pairs.push_back(std::move(ps)); + DT(3, this->name() << " amo-cmp: " << *trace); + output.push(trace, 1); + return true; + } + assert(lsu_type == LsuType::AMO_HI); + auto it = state.pairs.begin(); + while (it != state.pairs.end() + && !(it->wid == trace->wid && it->pid == trace->pid && it->has_amo && !it->has_hi)) { + ++it; + } + if (it == state.pairs.end()) { + std::cout << "Error: AMO_HI without its AMO: " << *trace << std::endl; + std::abort(); + } + if (it->issued && it->beats == 0) { + if (output.full()) { + return false; + } + retire_pair_hi(trace, it->hi, it->hi_mask); + DT(3, this->name() << " amo-hi: " << *trace); + output.push(trace, 1); + state.pairs.erase(it); + } else { + it->has_hi = true; + it->hi_trace = trace; + } + return true; +} + +// A pair response retires as two beats, as the hardware's does: the low half +// into the AMO's rd, then the high half into rd+1. The high beat is AMO_HI's +// retirement when it completes the AMO and AMO_HI has arrived, otherwise a +// writeback-less commit-port slot. +bool LsuUnit::process_pair_response(uint32_t b) { + auto& state = states_.at(b); + auto& lsu_rsp_in = mem_rsp_in[b]; + auto& lsu_rsp = lsu_rsp_in.peek(); + auto& entry = state.pending_reqs.at(lsu_rsp.tag); + auto& output = rsp_bufs_.at(b); + if (output.full()) { + return false; + } + if (!state.rsp_hi_phase) { + auto trace = entry.trace; + auto ps = entry.pair; + state.rsp_pair = ps; + DT(3, this->name() << " mem-rsp: " << lsu_rsp); + for (uint32_t lane = 0; lane < lsu_rsp.mask.size(); ++lane) { + if (!lsu_rsp.mask.test(lane)) { + continue; + } + const auto& lane_info = entry.lanes.at(lane); + uint32_t off = lane_info.addr & (VX_CFG_MEM_BLOCK_SIZE - 1); + uint64_t old = 0; + std::memcpy(&old, lsu_rsp.data.at(lane)->data() + off, 8); + trace->dst_data.at(lane_info.tid).u64 = sext_word((uint32_t)old); + ps->hi.at(lane_info.tid) = (uint32_t)(old >> 32); + } + if (ps->issued && (ps->beats == 1)) { + // the AMO's last beat: the AMO retires, and its trace is not + // touched again + state.rsp_hi_beat = nullptr; + output.push(trace, 1); + } else { + auto beat_alloc = core_->trace_pool().allocate(1); + auto beat = new (beat_alloc) instr_trace_t(*trace); + beat->wb = false; + beat->eop = false; + auto hi_alloc = core_->trace_pool().allocate(1); + state.rsp_hi_beat = new (hi_alloc) instr_trace_t(*beat); + output.push(beat, 1); + } + state.rsp_hi_phase = true; + return true; + } + auto ps = state.rsp_pair; + if (state.rsp_hi_beat) { + output.push(state.rsp_hi_beat, 1); + state.rsp_hi_beat = nullptr; + ps->beats -= 1; + } else if (ps->hi_trace) { + retire_pair_hi(ps->hi_trace, ps->hi, ps->hi_mask); + DT(3, this->name() << " amo-hi: " << *ps->hi_trace); + output.push(ps->hi_trace, 1); + for (auto it = state.pairs.begin(); it != state.pairs.end(); ++it) { + if (&*it == ps) { + state.pairs.erase(it); + break; + } + } + } else { + // AMO_HI has not reached the unit yet; it retires on arrival + ps->beats = 0; + } + state.rsp_pair = nullptr; + entry.count -= lsu_rsp.mask.count(); + assert(entry.count == 0); + state.pending_reqs.release(lsu_rsp.tag); + pending_loads_ -= lsu_rsp.mask.count(); + state.rsp_hi_phase = false; + lsu_rsp_in.pop(); + return true; +} + // Work besides a packet in one of its ports: a trace queued, in flight or // awaiting a response, a fence engaged, or a load outstanding (a tick // accumulates their latency). @@ -268,7 +503,8 @@ bool LsuUnit::busy() const { auto& state = states_.at(b); if (!state.req_queue.empty() || !state.pending_reqs.empty() || state.staged_reads != 0 || !state.meta_reqs.empty() || state.fence.locked() || state.fence_token - || state.head_staged || state.remain_addrs != 0 || !state.rsp_tracks.empty()) { + || state.head_staged || state.remain_addrs != 0 || !state.rsp_tracks.empty() + || !state.pairs.empty() || state.rsp_hi_phase) { return true; } } @@ -288,6 +524,8 @@ void LsuUnit::compute_addrs(uint32_t b, instr_trace_t* trace) { // Compare-and-swap alone reads a third operand: the comparand, which the // instruction takes from rd. Decode adds rd to the source set for it. const bool is_cas = is_amo && (*amo_tag == AmoType::AMOCAS); + pair_state_t* pair = is_pair_amo(trace) ? this->claim_pair(b, trace) : nullptr; + state.head_pair = pair; uint32_t num_threads = VX_CFG_NUM_THREADS; uint32_t data_bytes; bool is_write; @@ -361,6 +599,12 @@ void LsuUnit::compute_addrs(uint32_t b, instr_trace_t* trace) { if (is_cas) { e.amo_cmp = trace->src_data[2][t].u64; } + if (pair) { + // the operand is the rs2 pair; the comparand was staged by AMO_CMP + e.data = (uint64_t)(uint32_t)rs2_data[t].u + | ((uint64_t)(uint32_t)trace->src_data[2][t].u << 32); + e.amo_cmp = is_cas ? pair->cmp.at(t) : 0; + } } state.addr_list.push_back(e); } @@ -421,6 +665,10 @@ void LsuUnit::process_response_step(uint32_t b) { auto& state = states_.at(b); auto& lsu_rsp = lsu_rsp_in.peek(); auto& entry = state.pending_reqs.at(lsu_rsp.tag); + if (entry.pair) { + this->process_pair_response(b); + return; + } #ifdef TCU_META_ENABLE // TCU metadata response: forward the fragment with the client's tag @@ -798,9 +1046,19 @@ void LsuUnit::process_request_step(uint32_t b) { std::abort(); } #endif + if (lsu_tag && (*lsu_tag == LsuType::AMO_CMP || *lsu_tag == LsuType::AMO_HI)) { + if (this->process_pair_uop(b, trace)) { + state.pop_req(); + } + return; + } const bool is_amo = (amo_tag != nullptr); const bool is_fence = (lsu_tag && *lsu_tag == LsuType::FENCE); const bool is_write = (lsu_tag && *lsu_tag == LsuType::STORE); + // A register-pair atomic issues one thread per beat: the hardware carries + // its 64-bit operand on two adjacent lanes that the coalescer fuses. + const bool is_pair = is_pair_amo(trace); + const uint32_t beat_lanes = is_pair ? 1 : VX_CFG_NUM_LSU_LANES; if (is_fence) { // Engage fence lock; the next call will try to release it once @@ -824,6 +1082,11 @@ void LsuUnit::process_request_step(uint32_t b) { --state.staged_reads; state.amo_full = false; state.amo_checked = nullptr; + if (is_pair) { + // no beat goes: its AMO_HI retires on arrival, writing nothing + auto pair = this->claim_pair(b, trace); + pair->issued = true; + } no_rsp_bufs_.at(b).push(trace, 1); state.pop_req(); return; @@ -856,7 +1119,7 @@ void LsuUnit::process_request_step(uint32_t b) { // Skip beats whose tid group is entirely inactive — nothing to issue. while (state.remain_addrs != 0) { uint32_t t0 = state.addr_list.size() - state.remain_addrs; - uint32_t n = std::min(VX_CFG_NUM_LSU_LANES, state.remain_addrs); + uint32_t n = std::min(beat_lanes, state.remain_addrs); bool any = false; for (uint32_t i = 0; i < n; ++i) { if (state.addr_list.at(t0 + i).size != 0) { @@ -883,7 +1146,7 @@ void LsuUnit::process_request_step(uint32_t b) { if (state.remain_addrs != 0) { // check backpressure at the switch buffers this beat addresses uint32_t beat_t0 = state.addr_list.size() - state.remain_addrs; - uint32_t beat_len = std::min(VX_CFG_NUM_LSU_LANES, state.remain_addrs); + uint32_t beat_len = std::min(beat_lanes, state.remain_addrs); bool to_dc = false, to_lmem = false; for (uint32_t i = 0; i < beat_len; ++i) { auto& entry = state.addr_list.at(beat_t0 + i); @@ -909,18 +1172,21 @@ void LsuUnit::process_request_step(uint32_t b) { lsu_req.flags.amo_unsigned = amo_is_unsigned(*amo_tag); } uint32_t t0 = state.addr_list.size() - state.remain_addrs; - uint32_t beat_n = std::min(VX_CFG_NUM_LSU_LANES, state.remain_addrs); + uint32_t beat_n = std::min(beat_lanes, state.remain_addrs); std::vector lane_entries(VX_CFG_NUM_LSU_LANES); for (uint32_t i = 0; i < beat_n; ++i) { auto& entry = state.addr_list.at(t0 + i); if (entry.size == 0) continue; // inactive tid: keep the lane hole (tid-stable mapping) + // A pair atomic rides its thread's own lane, which keeps it behind + // the thread's older accesses to the same word. + const uint32_t lane = is_pair ? (entry.tid % VX_CFG_NUM_LSU_LANES) : i; // Address goes downstream as VA. The per-core dcache MMU // (under VX_CFG_VM_ENABLE) substitutes PA before the request reaches // the cache; with VM off, the address is already the PA. - lsu_req.mask.set(i); - lsu_req.addrs.at(i) = entry.addr; - lane_entries.at(i) = entry; + lsu_req.mask.set(lane); + lsu_req.addrs.at(lane) = entry.addr; + lane_entries.at(lane) = entry; if (is_write || is_amo) { // Package the lane's value into a per-lane block + byteen. // Stores: store data. AMOs: rhs (rs2). The cache extracts @@ -931,20 +1197,20 @@ void LsuUnit::process_request_step(uint32_t b) { for (uint32_t bo = 0; bo < entry.size; ++bo) { (*block)[off + bo] = uint8_t((entry.data >> (8 * bo)) & 0xff); } - lsu_req.data.at(i) = block; - lsu_req.byteen.at(i) = ((1ull << entry.size) - 1) << off; + lsu_req.data.at(lane) = block; + lsu_req.byteen.at(lane) = ((1ull << entry.size) - 1) << off; } #ifdef LSU_WIDE_ENABLE else if (entry.size > XLENB) { // a wide load's byte enables tell the memory it spans two words uint32_t off = entry.addr & (VX_CFG_MEM_BLOCK_SIZE - 1); - lsu_req.byteen.at(i) = ((1ull << entry.size) - 1) << off; + lsu_req.byteen.at(lane) = ((1ull << entry.size) - 1) << off; } #endif - lsu_req.amo_cmp.at(i) = entry.amo_cmp; + lsu_req.amo_cmp.at(lane) = entry.amo_cmp; // The lane's original thread index rides along so the adapter can // recover hart_id = make_hart_id(cid, wid, tids[i]). - lsu_req.tids.at(i) = entry.tid; + lsu_req.tids.at(lane) = entry.tid; } state.remain_addrs -= beat_n; @@ -970,19 +1236,30 @@ void LsuUnit::process_request_step(uint32_t b) { --state.staged_reads; state.head_staged = false; } - if (state.open_track == kNoTrack) { - state.open_track = state.rsp_tracks.allocate({}); + pending_req_t pending{trace, count, is_eop, std::move(lane_entries), entry_args, true}; + if (state.head_pair) { + // a register-pair atomic retires through process_pair_response, + // beat by beat, not through the response tracking + pending.track = kNoTrack; + } else { + if (state.open_track == kNoTrack) { + state.open_track = state.rsp_tracks.allocate({}); + } + uint32_t track_id = state.open_track; + auto& track = state.rsp_tracks.at(track_id); + ++track.outstanding; + if (trace->eop && is_eop) { + track.last_issued = true; + state.open_track = kNoTrack; + } + pending.track = track_id; } - uint32_t track_id = state.open_track; - auto& track = state.rsp_tracks.at(track_id); - ++track.outstanding; - if (trace->eop && is_eop) { - track.last_issued = true; - state.open_track = kNoTrack; + pending.pair = state.head_pair; + tag = state.pending_reqs.allocate(std::move(pending)); + if (state.head_pair) { + state.head_pair->beats += 1; + state.head_pair->issued = (state.remain_addrs == 0); } - pending_req_t entry{trace, count, is_eop, std::move(lane_entries), entry_args, true}; - entry.track = track_id; - tag = state.pending_reqs.allocate(std::move(entry)); } lsu_req.tag = tag; lsu_req.cid = trace->cid; diff --git a/sim/simx/lsu_unit.h b/sim/simx/lsu_unit.h index 8edab33c42..80c3f08632 100644 --- a/sim/simx/lsu_unit.h +++ b/sim/simx/lsu_unit.h @@ -15,6 +15,7 @@ #include #include +#include #include #include #include "func_unit.h" @@ -111,6 +112,9 @@ class LsuUnit : public FuncUnit { // Returns true when all LSU blocks have no in-flight requests. bool drained() const; + // A 64-bit atomic on a 32-bit hart: a register-pair atomic. + static bool is_pair_amo(const instr_trace_t* trace); + void on_reset(); void on_tick(); bool busy() const; @@ -150,6 +154,31 @@ class LsuUnit : public FuncUnit { uint32_t tid; }; + // A register-pair atomic in flight. A warp's pair uops reach its block in + // program order, so each state is matched by (warp, packet) to the oldest + // one still waiting for that uop: AMO_CMP opens it with the comparand, the + // AMO claims it and fills `hi` from its responses, and AMO_HI retires the + // high words once every beat of the AMO has returned. + struct pair_state_t { + uint32_t wid; + int pid; + std::vector cmp; + std::vector hi; + // the threads whose atomic went to memory (check_amo): AMO_HI retires + // only these, and writes nothing for the faulted ones + ThreadMask hi_mask{VX_CFG_NUM_THREADS}; + bool has_amo = false; + uint32_t beats = 0; + bool issued = false; + bool has_hi = false; + instr_trace_t* hi_trace = nullptr; + }; + + // Register-pair atomic support (64-bit atomics on a 32-bit hart). + pair_state_t* claim_pair(uint32_t b, const instr_trace_t* trace); + bool process_pair_uop(uint32_t b, instr_trace_t* trace); + bool process_pair_response(uint32_t b); + struct pending_req_t { instr_trace_t* trace; uint32_t count; @@ -163,6 +192,8 @@ class LsuUnit : public FuncUnit { // TcuRspOut with client_tag restored instead of written to registers. bool is_tcu = false; uint32_t client_tag = 0; + // Register-pair atomic beat: one thread's 64-bit operation. + pair_state_t* pair = nullptr; // A fence's flush token: its response only releases the fence. bool is_flush = false; }; @@ -214,6 +245,14 @@ class LsuUnit : public FuncUnit { // A partially faulted atomic whose requests are all sent: nothing // passes it until they are answered, as its faulted end waits for them. instr_trace_t* fault_hold = nullptr; + std::list pairs; + // A pair response retires in two beats, low half then high half; the + // response stays at the head of the stream between them. + bool rsp_hi_phase = false; + pair_state_t* rsp_pair = nullptr; + // the pair state of the trace at the head of req_queue + pair_state_t* head_pair = nullptr; + instr_trace_t* rsp_hi_beat = nullptr; void push_req(instr_trace_t* trace) { this->req_queue.push(trace); @@ -242,6 +281,11 @@ class LsuUnit : public FuncUnit { this->amo_partial = false; this->amo_full = false; this->fault_hold = nullptr; + this->pairs.clear(); + this->rsp_hi_phase = false; + this->rsp_hi_beat = nullptr; + this->rsp_pair = nullptr; + this->head_pair = nullptr; this->rsp_tracks.clear(); this->open_track = kNoTrack; } diff --git a/sim/simx/mem/local_mem.cpp b/sim/simx/mem/local_mem.cpp index 36c9bb3135..5b85179419 100644 --- a/sim/simx/mem/local_mem.cpp +++ b/sim/simx/mem/local_mem.cpp @@ -56,6 +56,10 @@ class LocalMem::Impl { // An atomic reads its word, registers it, then writes the new value back: // the two cycles after its pass the banks take nothing else. static constexpr uint32_t AMO_STALL_CYCLES = 2; + // A 64-bit atomic of a 32-bit hart reads its two words from adjacent banks + // in one pass, but registers its 64-bit result before writing both back, + // one cycle later. + static constexpr uint32_t AMO_WIDE_STALL_CYCLES = 3; #endif struct Request { @@ -192,6 +196,13 @@ class LocalMem::Impl { return served; } +#if VX_CFG_EXT_A_ENABLED + // A 64-bit atomic of a 32-bit hart covers the words of two adjacent banks. + bool is_wide_amo(const LsuReq& req, uint32_t i) const { + return req.is_amo() && ((uint32_t)__builtin_popcountll(req.byteen.at(i)) > (1u << lg2_word_)); + } +#endif + // Do one lane's bank access and return the word the lane gets back, if any. std::shared_ptr access(const LsuReq& req, uint32_t i) { uint64_t la = to_local_addr(req.addrs.at(i)); @@ -206,6 +217,7 @@ class LocalMem::Impl { const uint64_t byteen = req.byteen.at(i); const uint32_t wbytes = byteen ? (uint32_t)__builtin_popcountll(byteen) : 4u; const uint8_t width = (wbytes >= 8) ? 3 : 2; + const bool is_wide = this->is_wide_amo(req, i); const bool is_lr = (req.op == MemOp::AMO_LR); const bool is_sc = (req.op == MemOp::AMO_SC); const bool sc_fail = is_sc && !amo_unit_.check(hart_id, la); @@ -238,9 +250,13 @@ class LocalMem::Impl { amo_store_word(sbuf, 0, width, rmw.new_word); ram_.write(sbuf, la, wbytes); amo_unit_.invalidate(la, hart_id); + // a 64-bit atomic on a 32-bit hart writes the words of two banks + if (is_wide) { + amo_unit_.invalidate(la + (1u << lg2_word_), hart_id); + } } - // an atomic responds, so it counts as a read - ++perf_stats_.reads; + // an atomic responds, so it counts as a read, of each bank it reads + perf_stats_.reads += is_wide ? 2 : 1; return rsp_data; } #endif @@ -342,6 +358,9 @@ class LocalMem::Impl { return false; } auto served = this->select_pass(req, e.pending); +#if VX_CFG_EXT_A_ENABLED + bool wide = false; +#endif LsuRsp rsp(config_.num_lanes); rsp.tag = req.tag; rsp.cid = req.cid; @@ -351,6 +370,9 @@ class LocalMem::Impl { continue; } auto data = this->access(req, i); +#if VX_CFG_EXT_A_ENABLED + wide = wide || this->is_wide_amo(req, i); +#endif if (has_rsp) { rsp.mask.set(i); rsp.data.at(i) = data; @@ -363,7 +385,7 @@ class LocalMem::Impl { } #if VX_CFG_EXT_A_ENABLED if (req.is_amo()) { - stall_cycles_ = AMO_STALL_CYCLES; + stall_cycles_ = wide ? AMO_WIDE_STALL_CYCLES : AMO_STALL_CYCLES; } #endif if (e.pending.none()) { diff --git a/sim/simx/sequencer.cpp b/sim/simx/sequencer.cpp index 2763303546..7f2b8e96a0 100644 --- a/sim/simx/sequencer.cpp +++ b/sim/simx/sequencer.cpp @@ -119,7 +119,11 @@ instr_trace_t* Sequencer::get(instr_trace_t* trace) { for (uint32_t i = 0; i < NUM_SRC_REGS; ++i) { uop_trace->src_regs[i] = uop_instr->get_src_reg(i); } - uop_trace->wb = (uop_instr->get_dest_reg().type != RegType::None); + // As for whole instructions (Core::issue): writes to x0 are silent, so a + // uop naming x0 (a pair atomic's discarded old value) reserves nothing. + auto uop_dst = uop_instr->get_dest_reg(); + uop_trace->wb = (uop_dst.type != RegType::None) + && !(uop_dst.type == RegType::Integer && uop_dst.idx == 0); // A uop that stalls fetch takes over its macro's fetch stall: the warp // fetches on as that uop resolves, not once the macro's last uop issues. if (uop_instr->is_wstall()) { diff --git a/sim/simx/types.h b/sim/simx/types.h index 31d18c3138..f7dd653fad 100644 --- a/sim/simx/types.h +++ b/sim/simx/types.h @@ -396,10 +396,15 @@ inline std::ostream &operator<<(std::ostream &os, const MdvType& type) { /////////////////////////////////////////////////////////////////////////////// +// AMO_CMP and AMO_HI are the companion uops of a register-pair atomic on a +// 32-bit hart: AMO_CMP stages the rd pair (the compare-and-swap comparand), +// AMO_HI retires the upper half of the old value into rd+1. enum class LsuType { LOAD, STORE, - FENCE + FENCE, + AMO_CMP, + AMO_HI }; // With 64-bit FP registers a wide FP load (vx_flw.d/.q) is one LSU @@ -425,6 +430,8 @@ inline std::ostream &operator<<(std::ostream &os, const LsuType& type) { case LsuType::LOAD: os << "LOAD"; break; case LsuType::STORE: os << "STORE"; break; case LsuType::FENCE: os << "FENCE"; break; + case LsuType::AMO_CMP: os << "AMO_CMP"; break; + case LsuType::AMO_HI: os << "AMO_HI"; break; default: assert(false); } diff --git a/sw/runtime/include/vortex2.h b/sw/runtime/include/vortex2.h index 49e4051d10..611c04e0f9 100644 --- a/sw/runtime/include/vortex2.h +++ b/sw/runtime/include/vortex2.h @@ -114,6 +114,8 @@ typedef struct vx_kernel* vx_kernel_h; #define VX_ISA_EXT_ZFHMIN (1ull << (32 + 13)) #define VX_ISA_EXT_LDST_WIDE (1ull << (32 + 14)) #define VX_ISA_EXT_LDST_MATRIX (1ull << (32 + 15)) +// 64-bit atomics on a 32-bit hart (register pairs, VX_CFG_EXT_AMO64_ENABLE). +#define VX_ISA_EXT_AMO64 (1ull << (32 + 17)) // ============================================================================ // Device memory access flags (vx_buffer_create / vx_buffer_access) diff --git a/tests/regression/amo/common.h b/tests/regression/amo/common.h index 70177a3f08..07372d62c5 100644 --- a/tests/regression/amo/common.h +++ b/tests/regression/amo/common.h @@ -17,6 +17,13 @@ // swap uses is ever zero. #define CAS_COUNTER_BASE 0x5a000000u +// 64-bit atomics: native on a 64-bit hart, register pairs on a 32-bit hart +// with the AMO64 extension. +#define AMO64_TESTS ((VX_CFG_XLEN == 64) || VX_CFG_EXT_AMO64_ENABLED) + +// ldst64_illegal: the destination and store data a refused LD/LWU/SD holds. +#define LDST64_SENTINEL 0x3c3c3c3cu + // amoadd_scatter: AMO_SCATTER_LINES lines past the per-hart region, more than // a dcache holds, so most atomics miss. #define AMO_SCATTER_LINES 1024u @@ -31,6 +38,48 @@ static inline uint32_t amo_burst_line(uint32_t i, uint32_t g) { return (i * 37u + g * 101u + 11u) % AMO_SCATTER_LINES; } +// 64-bit counters start below 2^32 so the low word wraps and the carry into the +// high word is exercised. The add step moves both halves by one, which keeps +// every intermediate value decodable without a division: after k steps the low +// word is base + k and the high word is k, plus one once the low word wrapped. +#define AMO64_ADD_BASE 0x00000000fffffff0ull +#define AMO64_ADD_STEP 0x0000000100000001ull +#define AMO64_CAS_BASE 0x00000000ffffff00ull +#define AMO64_XOR_INIT 0x0123456789abcdefull +#define AMO64_SWAP_INIT 0x5a5a5a5aa5a5a5a5ull +// Marks a group leader's published record in the shared region. +#define AMO64_LMEM_MARK 0x4c4d454d00000000ull + +// min/max operands: harts pair up on the high word, so equal high words are +// decided by the low word, and every other pair flips the sign bit, so signed +// and unsigned order disagree. The low word (h + 1) identifies the hart. +static inline uint64_t amo64_minmax_value(uint32_t h) { + uint32_t g = h >> 1; + uint32_t hi = (g * 0x9e3779b1u) ^ ((g & 1) ? 0x80000000u : 0u); + return ((uint64_t)hi << 32) | (h + 1); +} + +// Bit operands: one bit in each half, so a lost half shows in the result. +static inline uint64_t amo64_bit_value(uint32_t h) { + return (1ull << (h % 32)) | (1ull << (32 + ((h * 7 + 3) % 32))); +} + +// Exchange operand: both halves name the hart, so a torn old value is caught. +static inline uint64_t amo64_swap_value(uint32_t h) { + return ((uint64_t)(h + 1) << 32) | ((h + 1) * 0x01010101u); +} + +// The word hart h stores on iteration i of amo64_x0: its low word carries into +// the high one when AMO64_ADD_STEP is added. +static inline uint64_t amo64_x0_value(uint32_t h, uint32_t i) { + return ((uint64_t)(h + 1 + i) << 32) | (0xfffffff0u + (h & 7)); +} + +// Compare-and-swap ladder claim; zero is the unclaimed marker. +static inline uint64_t amo64_claim_value(uint32_t h) { + return ((uint64_t)(h + 1) << 32) | (h + 1); +} + // Initial word of the local-memory swap chain in the AMO op sweep. #define AMO_OPS_SWAP_INIT 0x5EED5EEDu diff --git a/tests/regression/amo/kernel.cpp b/tests/regression/amo/kernel.cpp index 2f87ae6c8e..c35f08c514 100644 --- a/tests/regression/amo/kernel.cpp +++ b/tests/regression/amo/kernel.cpp @@ -522,6 +522,453 @@ void kernel_cas_counter_lmem(kernel_arg_t* __UNIFORM__ arg) { } #endif +#if AMO64_TESTS +// 64-bit atomic primitives. A 64-bit hart has the .D forms natively; a 32-bit +// hart names even-aligned register pairs ("R"), which the assembler only spells +// for amocas.d, so the read-modify-write forms are emitted as raw encodings +// (funct7 = funct5 << 2). +#if VX_CFG_XLEN == 32 +#define AMO64_RMW(name, funct7) \ + static inline uint64_t name(volatile uint64_t* p, uint64_t v) { \ + uint64_t old; \ + asm volatile (".insn r 0x2f, 3, " #funct7 ", %0, %1, %2" \ + : "=R"(old) : "r"(p), "R"(v) : "memory"); \ + return old; \ + } +AMO64_RMW(amo64_add, 0x00) +AMO64_RMW(amo64_swap, 0x04) +AMO64_RMW(amo64_xor, 0x10) +AMO64_RMW(amo64_or, 0x20) +AMO64_RMW(amo64_and, 0x30) +AMO64_RMW(amo64_min, 0x40) +AMO64_RMW(amo64_max, 0x50) +AMO64_RMW(amo64_minu, 0x60) +AMO64_RMW(amo64_maxu, 0x70) +#if VX_CFG_EXT_ZACAS_ENABLED +// Returns the old value; the swap landed when it equals `expected`. +static inline uint64_t amo64_cas(volatile uint64_t* p, uint64_t expected, uint64_t v) { + asm volatile ("amocas.d %0, %2, (%1)" + : "+R"(expected) : "r"(p), "R"(v) : "memory"); + return expected; +} +#endif +#else +#define AMO64_RMW(name, insn) \ + static inline uint64_t name(volatile uint64_t* p, uint64_t v) { \ + uint64_t old; \ + asm volatile (insn " %0, %2, (%1)" \ + : "=r"(old) : "r"(p), "r"(v) : "memory"); \ + return old; \ + } +AMO64_RMW(amo64_add, "amoadd.d") +AMO64_RMW(amo64_swap, "amoswap.d") +AMO64_RMW(amo64_xor, "amoxor.d") +AMO64_RMW(amo64_or, "amoor.d") +AMO64_RMW(amo64_and, "amoand.d") +AMO64_RMW(amo64_min, "amomin.d") +AMO64_RMW(amo64_max, "amomax.d") +AMO64_RMW(amo64_minu, "amominu.d") +AMO64_RMW(amo64_maxu, "amomaxu.d") +#if VX_CFG_EXT_ZACAS_ENABLED +static inline uint64_t amo64_cas(volatile uint64_t* p, uint64_t expected, uint64_t v) { + asm volatile ("amocas.d %0, %2, (%1)" + : "+r"(expected) : "r"(p), "r"(v) : "memory"); + return expected; +} +#endif +#endif + +// A value an add counter can hold after fewer than `total` steps from +// AMO64_ADD_BASE (see common.h). +static inline bool amo64_add_valid(uint64_t v, uint32_t total) { + uint32_t k = (uint32_t)v - (uint32_t)AMO64_ADD_BASE; + uint32_t hi = k + ((k >= (uint32_t)(0u - (uint32_t)AMO64_ADD_BASE)) ? 1u : 0u); + return (k < total) && ((uint32_t)(v >> 32) == hi); +} + +static inline bool amo64_minmax_valid(uint64_t v, uint64_t init, uint32_t n) { + uint32_t k = (uint32_t)v - 1; + return (v == init) || ((k < n) && (amo64_minmax_value(k) == v)); +} + +static inline bool amo64_swap_valid(uint64_t v, uint32_t n) { + uint32_t k = (uint32_t)(v >> 32) - 1; + return (v == AMO64_SWAP_INIT) || ((k < n) && (amo64_swap_value(k) == v)); +} + +// Arithmetic atomics over five words: add, signed max/min, unsigned max/min. +// Every hart hammers every word; each returned old value must be one the word +// can hold, and the count of those that are not is the hart's error count. +static uint32_t amo64_arith(volatile uint64_t* w, uint32_t hid, uint32_t n, + uint32_t add_total, uint32_t iters) { + const uint64_t v = amo64_minmax_value(hid); + uint32_t bad = 0; + for (uint32_t i = 0; i < iters; ++i) { + bad += !amo64_add_valid(amo64_add(&w[0], AMO64_ADD_STEP), add_total); + bad += !amo64_minmax_valid(amo64_max(&w[1], v), (uint64_t)INT64_MIN, n); + bad += !amo64_minmax_valid(amo64_min(&w[2], v), (uint64_t)INT64_MAX, n); + bad += !amo64_minmax_valid(amo64_maxu(&w[3], v), 0, n); + bad += !amo64_minmax_valid(amo64_minu(&w[4], v), UINT64_MAX, n); + } + return bad; +} + +static void amo64_arith_init(volatile uint64_t* w) { + w[0] = AMO64_ADD_BASE; + w[1] = (uint64_t)INT64_MIN; + w[2] = (uint64_t)INT64_MAX; + w[3] = 0; + w[4] = UINT64_MAX; +} + +// Bitwise atomics and exchange over four words: or, and, xor, swap. +static uint32_t amo64_bits(volatile uint64_t* w, uint32_t hid, uint32_t n, uint32_t iters) { + const uint64_t b = amo64_bit_value(hid); + const uint64_t s = amo64_swap_value(hid); + uint32_t bad = 0; + for (uint32_t i = 0; i < iters; ++i) { + amo64_or(&w[0], b); + amo64_and(&w[1], ~b); + amo64_xor(&w[2], b); + bad += !amo64_swap_valid(amo64_swap(&w[3], s), n); + } + // an odd number of xors per hart, so the result is the xor of every operand + if ((iters & 1) == 0) { + amo64_xor(&w[2], b); + } + return bad; +} + +static void amo64_bits_init(volatile uint64_t* w) { + w[0] = 0; + w[1] = UINT64_MAX; + w[2] = AMO64_XOR_INIT; + w[3] = AMO64_SWAP_INIT; +} + +// 64-bit arithmetic atomics on global memory, every hart on the same words. +void kernel_amo64_arith(kernel_arg_t* __UNIFORM__ arg) { + auto w = (volatile uint64_t*)arg->shared_addr; + auto per_hart = (uint32_t*)arg->per_hart_addr; + const uint32_t hid = hart_id(); + per_hart[hid] = amo64_arith(w, hid, arg->num_harts, arg->num_harts * arg->iters, arg->iters); +} + +// The same on local memory. The host cannot read it, so each group's leader +// publishes the group's final words, tagged with the group size, into its own +// line of the shared region. +void kernel_amo64_arith_lmem(kernel_arg_t* __UNIFORM__ arg) { + auto w = (volatile uint64_t*)__local_mem(); + auto per_hart = (uint32_t*)arg->per_hart_addr; + auto out = (volatile uint64_t*)arg->shared_addr; + const uint32_t hid = hart_id(); + if (threadIdx.x == 0) { + amo64_arith_init(w); + } + __syncthreads(); + per_hart[hid] = amo64_arith(w, hid, arg->num_harts, blockDim.x * arg->iters, arg->iters); + __syncthreads(); + if (threadIdx.x == 0) { + for (uint32_t i = 0; i < 5; ++i) { + out[hid * 8 + i] = w[i]; + } + out[hid * 8 + 7] = AMO64_LMEM_MARK | blockDim.x; + } +} + +void kernel_amo64_bits(kernel_arg_t* __UNIFORM__ arg) { + auto w = (volatile uint64_t*)arg->shared_addr; + auto per_hart = (uint32_t*)arg->per_hart_addr; + const uint32_t hid = hart_id(); + per_hart[hid] = amo64_bits(w, hid, arg->num_harts, arg->iters); +} + +void kernel_amo64_bits_lmem(kernel_arg_t* __UNIFORM__ arg) { + auto w = (volatile uint64_t*)__local_mem(); + auto per_hart = (uint32_t*)arg->per_hart_addr; + auto out = (volatile uint64_t*)arg->shared_addr; + const uint32_t hid = hart_id(); + if (threadIdx.x == 0) { + amo64_bits_init(w); + } + __syncthreads(); + per_hart[hid] = amo64_bits(w, hid, arg->num_harts, arg->iters); + __syncthreads(); + if (threadIdx.x == 0) { + for (uint32_t i = 0; i < 4; ++i) { + out[hid * 8 + i] = w[i]; + } + out[hid * 8 + 7] = AMO64_LMEM_MARK | blockDim.x; + } +} + +#if VX_CFG_EXT_ZACAS_ENABLED +// Compare-and-swap increment of a 64-bit counter whose low word wraps. The +// initial read is two loads on a 32-bit hart and may be torn; the swap then +// fails, returns the word it saw, and the retry carries on from there. +static void amo64_cas_count(volatile uint64_t* counter, uint32_t iters) { + for (uint32_t i = 0; i < iters; ++i) { + uint64_t expected = *counter; + for (;;) { + uint64_t seen = amo64_cas(counter, expected, expected + 1); + if (seen == expected) { + break; + } + expected = seen; + } + } +} + +void kernel_amo64_cas_counter(kernel_arg_t* __UNIFORM__ arg) { + amo64_cas_count((volatile uint64_t*)arg->shared_addr, arg->iters); +} + +void kernel_amo64_cas_counter_lmem(kernel_arg_t* __UNIFORM__ arg) { + auto counter = (volatile uint64_t*)__local_mem(); + auto per_hart = (uint32_t*)arg->per_hart_addr; + if (threadIdx.x == 0) { + *counter = AMO64_CAS_BASE; + } + __syncthreads(); + amo64_cas_count(counter, arg->iters); + __syncthreads(); + if (threadIdx.x == 0) { + uint64_t moved = *counter - AMO64_CAS_BASE; + // a count that does not fit the low word means the high word went wrong + per_hart[hart_id()] = (moved >> 32) ? 0xffffffffu : (uint32_t)moved; + } +} + +// Every hart races for each slot of a 64-bit ladder; the per-hart win counts +// check the returned old value, which must equal the comparand exactly once +// per slot. +void kernel_amo64_cas_ladder(kernel_arg_t* __UNIFORM__ arg) { + auto slots_p = (volatile uint64_t*)arg->shared_addr; + auto per_hart = (uint32_t*)arg->per_hart_addr; + const uint32_t hid = hart_id(); + const uint32_t slots = (arg->iters < CAS_LADDER_SLOTS) ? arg->iters : CAS_LADDER_SLOTS; + uint32_t wins = 0; + for (uint32_t i = 0; i < slots; ++i) { + if (amo64_cas(&slots_p[i], 0, amo64_claim_value(hid)) == 0) { + ++wins; + } + } + per_hart[hid] = wins; +} +#endif +#endif + +// --- 64-bit atomic ordering hazards (register pairs on a 32-bit hart) ------- +#if AMO64_TESTS && (VX_CFG_XLEN == 32) +// Write-after-write on rd+1. A 64-bit atomic writes rd and rd+1 with its old +// value. An older, still-pending write of rd+1 -- a slow load that missed in +// the cache -- must land before the atomic's high word, or it clobbers the +// high word or leaves rd+1 reserved forever (a hang). Here a9 (== the amoadd.d's rd+1, a8/a9 the rd pair) is +// written by a slow load one instruction before the atomic, so the atomic's +// old high word and a9 collide. The harness verifies both the final counter +// and that every returned old value was decodable. +static uint32_t amo64_war(volatile uint64_t* w, volatile uint32_t* slow, + uint32_t total, uint32_t iters) { + uint32_t bad = 0; + for (uint32_t i = 0; i < iters; ++i) { + register uint64_t acc asm("a6"); // rd pair a6/a7 + register uintptr_t wp asm("a4") = (uintptr_t)w; + register uint64_t step asm("a2") = AMO64_ADD_STEP; // operand pair a2/a3 + register uintptr_t sp asm("a5") = (uintptr_t)slow; + // a7 (== the amoadd.d's rd+1) is written by a cache-missing load one + // instruction before the atomic. + asm volatile ("lw a7, 0(a5)\n" + ".insn r 0x2f, 3, 0x00, a6, a4, a2" + : "=r"(acc) + : "r"(wp), "r"(step), "r"(sp) + : "a7", "memory"); + bad += !amo64_add_valid(acc, total); + } + return bad; +} + +// Write-after-write on rd+1 on global memory, every hart on the same counter. The per-hart slow word +// lives in the hart's own 64B line of the shared region (offset 16), clear of +// the counter in hart 0's line. +void kernel_amo64_war(kernel_arg_t* __UNIFORM__ arg) { + auto w = (volatile uint64_t*)arg->shared_addr; + auto per_hart = (uint32_t*)arg->per_hart_addr; + auto slow = (volatile uint32_t*)arg->shared_addr; + const uint32_t hid = hart_id(); + per_hart[hid] = amo64_war(w, &slow[hid * 16 + 4], arg->num_harts * arg->iters, arg->iters); +} + +// Write-after-write on rd+1 with the atomic on local memory: its response +// returns long before the older global load that missed. +void kernel_amo64_war_lmem(kernel_arg_t* __UNIFORM__ arg) { + auto w = (volatile uint64_t*)__local_mem(); + auto slow = (volatile uint32_t*)arg->per_hart_addr; + auto per_hart = (uint32_t*)arg->per_hart_addr; + auto out = (volatile uint64_t*)arg->shared_addr; + const uint32_t hid = hart_id(); + if (threadIdx.x == 0) { + w[0] = AMO64_ADD_BASE; + } + __syncthreads(); + // the slow word is the hart's own result slot, zero until it is written below + uint32_t bad = amo64_war(w, &slow[hid], blockDim.x * arg->iters, arg->iters); + per_hart[hid] = bad; + __syncthreads(); + if (threadIdx.x == 0) { + out[hid * 8] = w[0]; + out[hid * 8 + 7] = AMO64_LMEM_MARK | blockDim.x; + } +} + +// A thread's own store must precede its later atomic to the same word. Each +// thread stores 0 to its private 64-bit word, then does a 64-bit atomic add of +// 1 to it; the result must be 1. Bank contention (every thread on its own word +// in the same local-memory banks) opens the window, and threads past the first +// lane pair are the ones it reaches. +void kernel_amo64_store_amo_lmem(kernel_arg_t* __UNIFORM__ arg) { + auto base = (volatile uint64_t*)__local_mem(); + auto per_hart = (uint32_t*)arg->per_hart_addr; + const uint32_t hid = hart_id(); + volatile uint64_t* w = &base[threadIdx.x]; + uint32_t bad = 0; + for (uint32_t i = 0; i < arg->iters; ++i) { + *w = 0; + uint64_t old = amo64_add(w, 1); + bad += (old != 0) + ((*w) != 1); + } + per_hart[hid] = bad; +} + +// A pair atomic whose old value is discarded names x0 as rd, which names x0 for +// both halves: a store as amoswap.d into the zero pair, then an add whose old +// value is dropped. Nothing may wait on (or write) the destination, and the +// word must read back as the sum. +void kernel_amo64_x0(kernel_arg_t* __UNIFORM__ arg) { + auto shared = (volatile uint64_t*)arg->shared_addr; + auto per_hart = (uint32_t*)arg->per_hart_addr; + const uint32_t hid = hart_id(); + volatile uint64_t* w = &shared[hid * 8]; + uint32_t bad = 0; + for (uint32_t i = 0; i < arg->iters; ++i) { + register uintptr_t wp asm("a4") = (uintptr_t)w; + register uint64_t val asm("a2") = amo64_x0_value(hid, i); // pair a2/a3 + register uint64_t step asm("a6") = AMO64_ADD_STEP; // pair a6/a7 + asm volatile (".insn r 0x2f, 3, 0x04, x0, a4, a2\n" // amoswap.d x0, a2, (a4) + ".insn r 0x2f, 3, 0x00, x0, a4, a6" // amoadd.d x0, a6, (a4) + :: "r"(wp), "r"(val), "r"(step) : "memory"); + bad += (*w != amo64_x0_value(hid, i) + AMO64_ADD_STEP); + } + per_hart[hid] = bad; +} + +// Store-then-atomic order on global memory. A warp's threads take words one +// bank stride apart, so the warp's store request queues on one dcache bank +// lane by lane while the thread's later atomic finds a free port. +#define AMO64_BANK_STRIDE (VX_CFG_DCACHE_NUM_BANKS * VX_CFG_L1_LINE_SIZE / 8) +void kernel_amo64_store_amo(kernel_arg_t* __UNIFORM__ arg) { + auto base = (volatile uint64_t*)arg->shared_addr; + auto per_hart = (uint32_t*)arg->per_hart_addr; + const uint32_t hid = hart_id(); + // a multiple of the bank stride that leaves room for every warp's word + const uint32_t warps = arg->num_harts / VX_CFG_NUM_THREADS; + uint32_t stride = AMO64_BANK_STRIDE; + while (stride < warps) { + stride += AMO64_BANK_STRIDE; + } + volatile uint64_t* w = &base[(hid % VX_CFG_NUM_THREADS) * stride + + hid / VX_CFG_NUM_THREADS]; + uint32_t bad = 0; + for (uint32_t i = 0; i < arg->iters; ++i) { + *w = 0; + uint64_t old = amo64_add(w, 1); + bad += (old != 0) + ((*w) != 1); + } + per_hart[hid] = bad; +} +#endif + +#if VX_CFG_XLEN == 32 +// Encodings a 32-bit hart must refuse with an illegal-instruction trap: LR.D, +// which has no register-pair form, and a .D atomic naming an odd register (with +// AMO64) or any .D atomic (without it). The handler records mcause in t1 and +// resumes after the instruction; the word the instructions name must not change. +extern "C" void amo64_illegal_handler(); +asm (".align 4\n" + "amo64_illegal_handler:\n" + " csrr t0, mepc\n" + " addi t0, t0, 4\n" + " csrw mepc, t0\n" + " csrr t1, mcause\n" + " mret\n"); + +void kernel_amo64_illegal(kernel_arg_t* __UNIFORM__ arg) { + auto per_hart = (uint32_t*)arg->per_hart_addr; + uintptr_t saved_mtvec; + // mtvec is one per core and a block fills its core: every warp reads the + // old value before any installs the handler, and restores it only once all + // have trapped. + asm volatile ("csrr %0, mtvec" : "=r"(saved_mtvec)); + __syncthreads(); + asm volatile ("csrw mtvec, %0" :: "r"(&amo64_illegal_handler)); + register uintptr_t addr asm("a4") = (uintptr_t)arg->shared_addr; + // LR.D has no register-pair form. + register uint32_t cause_lrd asm("t1") = 0; + asm volatile (".insn r 0x2f, 3, 0x08, a2, a4, x0" // lr.d + : "+r"(cause_lrd) : "r"(addr) : "t0", "a2", "a3", "memory"); + uint32_t c_lrd = cause_lrd; +#if VX_CFG_EXT_AMO64_ENABLED + // With AMO64 a .D atomic naming an odd register in either pair is illegal. + register uint32_t cause_ord asm("t1") = 0; + asm volatile (".insn r 0x2f, 3, 0x00, a3, a4, a6" // amoadd.d, odd rd + : "+r"(cause_ord) : "r"(addr) : "t0", "a3", "memory"); + register uint32_t cause_ors asm("t1") = 0; + asm volatile (".insn r 0x2f, 3, 0x00, a2, a4, a5" // amoadd.d, odd rs2 + : "+r"(cause_ors) : "r"(addr) : "t0", "a2", "a3", "memory"); + uint32_t c2 = cause_ord, c3 = cause_ors; +#else + // Without AMO64 every .D atomic is illegal. + register uint32_t cause_addd asm("t1") = 0; + asm volatile (".insn r 0x2f, 3, 0x00, a2, a4, a6" // amoadd.d + : "+r"(cause_addd) : "r"(addr) : "t0", "a2", "a3", "memory"); + uint32_t c2 = cause_addd, c3 = cause_addd; +#endif + __syncthreads(); + asm volatile ("csrw mtvec, %0" :: "r"(saved_mtvec)); + per_hart[hart_id()] = c_lrd | (c2 << 8) | (c3 << 16); +} + +// A 32-bit hart has no LD, LWU or SD: each traps, and neither writes its rd nor +// touches memory. Bits 24/25 flag an LD/LWU destination that changed. +void kernel_ldst64_illegal(kernel_arg_t* __UNIFORM__ arg) { + auto per_hart = (uint32_t*)arg->per_hart_addr; + uintptr_t saved_mtvec; + // mtvec is one per core: as in amo64_illegal, every warp reads the old value + // before any installs the handler, and restores it only once all have trapped. + asm volatile ("csrr %0, mtvec" : "=r"(saved_mtvec)); + __syncthreads(); + asm volatile ("csrw mtvec, %0" :: "r"(&amo64_illegal_handler)); + register uintptr_t addr asm("a4") = (uintptr_t)arg->shared_addr; + register uint32_t cause_ld asm("t1") = 0; + register uint32_t rd_ld asm("a2") = LDST64_SENTINEL; + asm volatile (".insn i 0x03, 3, a2, a4, 0" // ld + : "+r"(cause_ld), "+r"(rd_ld) : "r"(addr) : "t0", "memory"); + uint32_t c_ld = cause_ld, v_ld = rd_ld; + register uint32_t cause_lwu asm("t1") = 0; + register uint32_t rd_lwu asm("a2") = LDST64_SENTINEL; + asm volatile (".insn i 0x03, 6, a2, a4, 0" // lwu + : "+r"(cause_lwu), "+r"(rd_lwu) : "r"(addr) : "t0", "memory"); + uint32_t c_lwu = cause_lwu, v_lwu = rd_lwu; + register uint32_t cause_sd asm("t1") = 0; + register uint32_t rs_sd asm("a2") = LDST64_SENTINEL; + asm volatile (".insn s 0x23, 3, a2, 0(a4)" // sd + : "+r"(cause_sd) : "r"(addr), "r"(rs_sd) : "t0", "memory"); + uint32_t c_sd = cause_sd; + __syncthreads(); + asm volatile ("csrw mtvec, %0" :: "r"(saved_mtvec)); + per_hart[hart_id()] = c_ld | (c_lwu << 8) | (c_sd << 16) + | ((v_ld != LDST64_SENTINEL) << 24) | ((v_lwu != LDST64_SENTINEL) << 25); +} +#endif // Scattered atomics: each hart adds into a line of a region larger than the // dcache, most of them missing. @@ -549,6 +996,34 @@ void kernel_amo_line_burst(kernel_arg_t* __UNIFORM__ arg) { } } +#if AMO64_TESTS +// Pair atomics streaming into local memory: each hart adds to four words of its +// own, back to back, so the local memory serves one pair after another and its +// bank hold after each (three cycles, a word atomic two) bounds the kernel. No +// word is shared, so every returned old value is known; a hart counts the ones +// that are not, and the final words that are not, into per_hart. +#define AMO64_STREAM_WORDS 4 +void kernel_amo64_lmem_stream(kernel_arg_t* __UNIFORM__ arg) { + auto w = (volatile uint64_t*)__local_mem() + threadIdx.x * AMO64_STREAM_WORDS; + auto per_hart = (uint32_t*)arg->per_hart_addr; + for (uint32_t k = 0; k < AMO64_STREAM_WORDS; ++k) { + w[k] = AMO64_ADD_BASE + k; + } + uint32_t bad = 0; + for (uint32_t i = 0; i < arg->iters; ++i) { + const uint64_t want = AMO64_ADD_BASE + (uint64_t)i * AMO64_ADD_STEP; + const uint64_t o0 = amo64_add(&w[0], AMO64_ADD_STEP); + const uint64_t o1 = amo64_add(&w[1], AMO64_ADD_STEP); + const uint64_t o2 = amo64_add(&w[2], AMO64_ADD_STEP); + const uint64_t o3 = amo64_add(&w[3], AMO64_ADD_STEP); + bad += (o0 != want) + (o1 != want + 1) + (o2 != want + 2) + (o3 != want + 3); + } + for (uint32_t k = 0; k < AMO64_STREAM_WORDS; ++k) { + bad += w[k] != AMO64_ADD_BASE + k + (uint64_t)arg->iters * AMO64_ADD_STEP; + } + per_hart[hart_id()] = bad; +} +#endif // Misaligned atomics trap precisely: store/AMO address misaligned (6), load // address misaligned (4) for LR, mepc the atomic, mtval 0 (read-only zero). @@ -684,6 +1159,75 @@ void kernel_amo_misaligned(kernel_arg_t* __UNIFORM__ arg) { ((volatile uint32_t*)base)[AMO_MIS_BYTES / 4 - 1] = flags; } +#if VX_CFG_XLEN == 32 && VX_CFG_EXT_AMO64_ENABLED +// Misaligned register-pair atomics (word aligned, not doubleword aligned) trap +// as a word atomic does, and their high-half uop retires exactly where the +// atomic did: neither half of rd changes for a faulted lane, and both get the +// old words for a lane of an LSU packet or SIMD slice ahead of the misaligned +// one. The handler and AMO_MIS_TRY are amo_misaligned's. +#define AMO64_MIS_TRY(addr, cause, ok, precise, lo, hi) \ + do { \ + register uintptr_t _a asm("a4") = (addr); \ + register uint32_t _lo asm("a2") = AMO_MIS_RD; \ + register uint32_t _hi asm("a3") = AMO_MIS_RD + 1; \ + register uint32_t _r0 asm("a6") = 1; \ + register uint32_t _r1 asm("a7") = 0; \ + AMO_MIS_TRY(".insn r 0x2f, 3, 0x00, a2, a4, a6", cause, ok, precise, \ + "+r"(_lo), "+r"(_hi) : "r"(_a), "r"(_r0), "r"(_r1) \ + : "t0", "memory"); /* amoadd.d */ \ + lo = _lo; \ + hi = _hi; \ + } while (0) + +void kernel_amo64_misaligned(kernel_arg_t* __UNIFORM__ arg) { + auto per_hart = (uint32_t*)arg->per_hart_addr; + const uint32_t hid = hart_id(); + const uint32_t lane = hid % VX_CFG_NUM_THREADS; + const uintptr_t base = (uintptr_t)arg->shared_addr + hid * AMO_MIS_BYTES; + uintptr_t saved_mtvec; + asm volatile ("csrr %0, mtvec" : "=r"(saved_mtvec)); + __syncthreads(); + asm volatile ("csrw mtvec, %0" :: "r"(&amo_misaligned_handler)); + + uint32_t ca = 0, cb = 0, cc = 0, cd = 0; + uint32_t lo = 0, hi = 0; + bool ok = false, precise = true; + uint32_t flags = 0; + // every lane misaligned (words 1 and 2 stay) + AMO64_MIS_TRY(base + 4, ca, ok, precise, lo, hi); + flags |= (ok && lo == AMO_MIS_RD && hi == AMO_MIS_RD + 1) ? 1u : 0u; + { + // the misaligned lane opens the warp's second LSU packet: the first + // packet's lanes perform (words 6 and 7), the rest do not + AMO64_MIS_TRY(base + ((lane == VX_CFG_NUM_LSU_LANES) ? 20 : 24), cb, ok, precise, lo, hi); + const bool done = (lane < VX_CFG_NUM_LSU_LANES); + flags |= (ok && lo == (done ? amo_mis_word(hid, 6) : AMO_MIS_RD) + && hi == (done ? amo_mis_word(hid, 7) : AMO_MIS_RD + 1)) ? 2u : 0u; + } + { + // the misaligned lane opens the warp's second SIMD slice: the first + // slice's lanes perform (words 10 and 11), the rest do not + AMO64_MIS_TRY(base + ((lane == VX_CFG_SIMD_WIDTH) ? 36 : 40), cc, ok, precise, lo, hi); + const bool done = (lane < VX_CFG_SIMD_WIDTH); + flags |= (ok && lo == (done ? amo_mis_word(hid, 10) : AMO_MIS_RD) + && hi == (done ? amo_mis_word(hid, 11) : AMO_MIS_RD + 1)) ? 4u : 0u; + } + { + // even warps misaligned, odd warps aligned (words 12 and 13), side by side + const bool odd = ((hid / VX_CFG_NUM_THREADS) & 1u) != 0; + AMO64_MIS_TRY(base + (odd ? 48 : 44), cd, ok, precise, lo, hi); + flags |= (ok && lo == (odd ? amo_mis_word(hid, 12) : AMO_MIS_RD) + && hi == (odd ? amo_mis_word(hid, 13) : AMO_MIS_RD + 1)) ? 8u : 0u; + } + flags |= precise ? 16u : 0u; + __syncthreads(); + asm volatile ("csrw mtvec, %0" :: "r"(saved_mtvec)); + // causes in 4-bit fields: all lanes, packet, slice, even/odd warps + per_hart[hid] = (ca & 15) | ((cb & 15) << 4) | ((cc & 15) << 8) | ((cd & 15) << 12); + ((volatile uint32_t*)base)[AMO_MIS_BYTES / 4 - 1] = flags; +} +#endif + static const PFN_Kernel sc_tests[] = { kernel_amoadd, // 0 kernel_amoor, // 1 @@ -706,10 +1250,38 @@ static const PFN_Kernel sc_tests[] = { kernel_cas_lmem, // 17 compare-and-swap on local memory (LMEM banks) kernel_cas_counter, // 18 compare-and-swap increment, nonzero comparands kernel_cas_counter_lmem, // 19 the same on local memory (LMEM banks) +#endif +#if AMO64_TESTS + kernel_amo64_arith, // 64-bit add/min/max (global) + kernel_amo64_arith_lmem, // the same on local memory + kernel_amo64_bits, // 64-bit or/and/xor/swap (global) + kernel_amo64_bits_lmem, // the same on local memory +#if VX_CFG_EXT_ZACAS_ENABLED + kernel_amo64_cas_counter, // 64-bit compare-and-swap counter (global) + kernel_amo64_cas_counter_lmem, // the same on local memory + kernel_amo64_cas_ladder, // 64-bit compare-and-swap ladder +#endif +#if VX_CFG_XLEN == 32 + kernel_amo64_war, // write-after-write on rd+1 (global) + kernel_amo64_war_lmem, // the same on local memory + kernel_amo64_store_amo, // a thread's store orders before its atomic + kernel_amo64_store_amo_lmem, // the same on local memory + kernel_amo64_x0, // the zero pair as a discarded destination +#endif +#endif +#if VX_CFG_XLEN == 32 + kernel_amo64_illegal, // refused .D encodings trap + kernel_ldst64_illegal, // LD/LWU/SD trap on a 32-bit hart, touching nothing #endif kernel_amoadd_scatter, // atomics spread over more lines than the dcache holds kernel_amo_line_burst, // atomics to distinct words of cold lines, in bursts kernel_amo_misaligned, // misaligned atomics trap precisely +#if AMO64_TESTS + kernel_amo64_lmem_stream, // pair atomics back to back into local memory +#endif +#if VX_CFG_XLEN == 32 && VX_CFG_EXT_AMO64_ENABLED + kernel_amo64_misaligned, // misaligned register-pair atomics trap precisely +#endif }; __kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) { diff --git a/tests/regression/amo/main.cpp b/tests/regression/amo/main.cpp index 1d71cdea82..6e1dc55313 100644 --- a/tests/regression/amo/main.cpp +++ b/tests/regression/amo/main.cpp @@ -89,8 +89,10 @@ int main(int argc, char *argv[]) { // AMOs stay on one bank). self_consistency uses one private 64B line // per hart (shared + hart_id*64) to test issuer self-consistency // WITHOUT false sharing. Size to the larger of the two needs. - // plus room for the scattered and bursting atomics' lines - const size_t shared_bytes = (size_t)num_harts * 64 + (size_t)AMO_SCATTER_LINES * 64; + // plus room for one word per thread a dcache bank stride apart (store_amo) + // and the scattered and bursting atomics' lines + const size_t shared_bytes = (size_t)num_harts * 64 + + std::max((size_t)num_threads * 2048, (size_t)AMO_SCATTER_LINES * 64); const size_t per_hart_bytes = num_harts * sizeof(uint32_t); RT_CHECK(vx_buffer_create(device, shared_bytes, VX_MEM_READ_WRITE, &shared_buffer)); diff --git a/tests/regression/amo/testcases.h b/tests/regression/amo/testcases.h index 40be3e9818..f64b73e4e4 100644 --- a/tests/regression/amo/testcases.h +++ b/tests/regression/amo/testcases.h @@ -557,6 +557,430 @@ class Test_CAS_COUNTER_LMEM : public ITestCase { } }; +#if AMO64_TESTS +static inline uint64_t word64(const uint32_t* p, uint32_t i) { + return ((uint64_t)p[2 * i + 1] << 32) | p[2 * i]; +} + +static inline void set_word64(uint32_t* p, uint32_t i, uint64_t v) { + p[2 * i] = (uint32_t)v; + p[2 * i + 1] = (uint32_t)(v >> 32); +} + +static int check64(const char* what, uint64_t got, uint64_t expected) { + if (got == expected) { + return 0; + } + std::cout << " " << what << "=0x" << std::hex << got << " expected=0x" + << expected << std::dec << std::endl; + return 1; +} + +static int check_no_bad_olds(uint32_t n, const uint32_t* per_hart) { + for (uint32_t h = 0; h < n; ++h) { + if (per_hart[h] != 0) { + std::cout << " hart " << h << " saw " << per_hart[h] + << " old value(s) the word could not hold" << std::endl; + return 1; + } + } + return 0; +} + +struct Amo64Extremes { + uint64_t max_s, min_s, max_u, min_u, bits_or, bits_xor; + Amo64Extremes(uint32_t n) { + int64_t mx = INT64_MIN, mn = INT64_MAX; + uint64_t mxu = 0, mnu = UINT64_MAX, bo = 0, bx = 0; + for (uint32_t h = 0; h < n; ++h) { + uint64_t v = amo64_minmax_value(h); + mx = std::max(mx, (int64_t)v); + mn = std::min(mn, (int64_t)v); + mxu = std::max(mxu, v); + mnu = std::min(mnu, v); + bo |= amo64_bit_value(h); + bx ^= amo64_bit_value(h); + } + max_s = (uint64_t)mx; min_s = (uint64_t)mn; max_u = mxu; min_u = mnu; + bits_or = bo; bits_xor = bx; + } +}; + +// Leader records published by the local-memory variants: one 64-byte line per +// group, tagged in its last word with the group size. +static std::vector amo64_lmem_leaders(uint32_t n, const uint32_t* shared) { + std::vector leaders; + for (uint32_t h = 0; h < n; ++h) { + uint64_t tag = word64(shared, h * 8 + 7); + if ((tag >> 32) == (AMO64_LMEM_MARK >> 32)) { + leaders.push_back(h); + } + } + return leaders; +} + +static bool amo64_minmax_in_set(uint64_t v, uint32_t n) { + uint32_t k = (uint32_t)v - 1; + return (k < n) && (amo64_minmax_value(k) == v); +} + +static bool amo64_swap_in_set(uint64_t v, uint32_t n) { + uint32_t k = (uint32_t)(v >> 32) - 1; + return (k < n) && (amo64_swap_value(k) == v); +} + +// 64-bit add / signed and unsigned min and max, every hart on one set of words. +class Test_AMO64_ARITH : public ITestCase { +public: + Test_AMO64_ARITH(TestSuite* s) : ITestCase(s, "amo64_arith") {} + void setup(uint32_t, uint32_t* shared, uint32_t*) override { + set_word64(shared, 0, AMO64_ADD_BASE); + set_word64(shared, 1, (uint64_t)INT64_MIN); + set_word64(shared, 2, (uint64_t)INT64_MAX); + set_word64(shared, 3, 0); + set_word64(shared, 4, UINT64_MAX); + } + int verify(uint32_t n, uint32_t iters, + const uint32_t* shared, const uint32_t* per_hart) override { + Amo64Extremes x(n); + int err = 0; + err += check64("add", word64(shared, 0), AMO64_ADD_BASE + (uint64_t)n * iters * AMO64_ADD_STEP); + err += check64("max", word64(shared, 1), x.max_s); + err += check64("min", word64(shared, 2), x.min_s); + err += check64("maxu", word64(shared, 3), x.max_u); + err += check64("minu", word64(shared, 4), x.min_u); + err += check_no_bad_olds(n, per_hart); + return err; + } +}; + +class Test_AMO64_ARITH_LMEM : public ITestCase { +public: + Test_AMO64_ARITH_LMEM(TestSuite* s) : ITestCase(s, "amo64_arith_lmem") {} + void setup(uint32_t, uint32_t*, uint32_t*) override {} + int verify(uint32_t n, uint32_t iters, + const uint32_t* shared, const uint32_t* per_hart) override { + auto leaders = amo64_lmem_leaders(n, shared); + if (leaders.empty()) { + std::cout << " no group published -- the kernel did not run" << std::endl; + return 1; + } + Amo64Extremes x(n); + int64_t mx = INT64_MIN, mn = INT64_MAX; + uint64_t mxu = 0, mnu = UINT64_MAX; + uint32_t harts = 0; + int err = 0; + for (auto h : leaders) { + uint32_t gs = (uint32_t)word64(shared, h * 8 + 7); + harts += gs; + err += check64("group add", word64(shared, h * 8 + 0), + AMO64_ADD_BASE + (uint64_t)gs * iters * AMO64_ADD_STEP); + for (uint32_t i = 1; i <= 4; ++i) { + if (!amo64_minmax_in_set(word64(shared, h * 8 + i), n)) { + std::cout << " group of hart " << h << ": word " << i << " = 0x" << std::hex + << word64(shared, h * 8 + i) << std::dec << " is no hart's operand" << std::endl; + ++err; + } + } + mx = std::max(mx, (int64_t)word64(shared, h * 8 + 1)); + mn = std::min(mn, (int64_t)word64(shared, h * 8 + 2)); + mxu = std::max(mxu, word64(shared, h * 8 + 3)); + mnu = std::min(mnu, word64(shared, h * 8 + 4)); + } + if (harts != n) { + std::cout << " groups cover " << harts << " harts, expected " << n << std::endl; + ++err; + } + err += check64("max", (uint64_t)mx, x.max_s); + err += check64("min", (uint64_t)mn, x.min_s); + err += check64("maxu", mxu, x.max_u); + err += check64("minu", mnu, x.min_u); + err += check_no_bad_olds(n, per_hart); + return err; + } +}; + +// 64-bit or / and / xor / exchange. +class Test_AMO64_BITS : public ITestCase { +public: + Test_AMO64_BITS(TestSuite* s) : ITestCase(s, "amo64_bits") {} + void setup(uint32_t, uint32_t* shared, uint32_t*) override { + set_word64(shared, 0, 0); + set_word64(shared, 1, UINT64_MAX); + set_word64(shared, 2, AMO64_XOR_INIT); + set_word64(shared, 3, AMO64_SWAP_INIT); + } + int verify(uint32_t n, uint32_t, + const uint32_t* shared, const uint32_t* per_hart) override { + Amo64Extremes x(n); + int err = 0; + err += check64("or", word64(shared, 0), x.bits_or); + err += check64("and", word64(shared, 1), ~x.bits_or); + err += check64("xor", word64(shared, 2), AMO64_XOR_INIT ^ x.bits_xor); + if (!amo64_swap_in_set(word64(shared, 3), n)) { + std::cout << " swap word = 0x" << std::hex << word64(shared, 3) << std::dec + << " is no hart's operand" << std::endl; + ++err; + } + err += check_no_bad_olds(n, per_hart); + return err; + } +}; + +class Test_AMO64_BITS_LMEM : public ITestCase { +public: + Test_AMO64_BITS_LMEM(TestSuite* s) : ITestCase(s, "amo64_bits_lmem") {} + void setup(uint32_t, uint32_t*, uint32_t*) override {} + int verify(uint32_t n, uint32_t, + const uint32_t* shared, const uint32_t* per_hart) override { + auto leaders = amo64_lmem_leaders(n, shared); + if (leaders.empty()) { + std::cout << " no group published -- the kernel did not run" << std::endl; + return 1; + } + Amo64Extremes x(n); + uint64_t bo = 0, ba = UINT64_MAX, bx = 0; + uint32_t harts = 0; + int err = 0; + for (auto h : leaders) { + harts += (uint32_t)word64(shared, h * 8 + 7); + bo |= word64(shared, h * 8 + 0); + ba &= word64(shared, h * 8 + 1); + bx ^= word64(shared, h * 8 + 2) ^ AMO64_XOR_INIT; + if (!amo64_swap_in_set(word64(shared, h * 8 + 3), n)) { + std::cout << " group of hart " << h << ": swap word = 0x" << std::hex + << word64(shared, h * 8 + 3) << std::dec << " is no hart's operand" << std::endl; + ++err; + } + } + if (harts != n) { + std::cout << " groups cover " << harts << " harts, expected " << n << std::endl; + ++err; + } + err += check64("or", bo, x.bits_or); + err += check64("and", ba, ~x.bits_or); + err += check64("xor", bx, x.bits_xor); + err += check_no_bad_olds(n, per_hart); + return err; + } +}; + +#if VX_CFG_EXT_ZACAS_ENABLED +class Test_AMO64_CAS_COUNTER : public ITestCase { +public: + Test_AMO64_CAS_COUNTER(TestSuite* s) : ITestCase(s, "amo64_cas_counter") {} + void setup(uint32_t, uint32_t* shared, uint32_t*) override { + set_word64(shared, 0, AMO64_CAS_BASE); + } + int verify(uint32_t n, uint32_t iters, + const uint32_t* shared, const uint32_t*) override { + return check64("counter", word64(shared, 0), AMO64_CAS_BASE + (uint64_t)n * iters); + } +}; + +class Test_AMO64_CAS_COUNTER_LMEM : public ITestCase { +public: + Test_AMO64_CAS_COUNTER_LMEM(TestSuite* s) : ITestCase(s, "amo64_cas_counter_lmem") {} + void setup(uint32_t, uint32_t*, uint32_t*) override {} + int verify(uint32_t n, uint32_t iters, + const uint32_t*, const uint32_t* per_hart) override { + uint64_t total = 0; + uint32_t groups = 0; + for (uint32_t h = 0; h < n; ++h) { + if (per_hart[h] == 0xffffffffu) { + std::cout << " group of hart " << h << " ended with a corrupt high word" << std::endl; + return 1; + } + if (per_hart[h] != 0) { total += per_hart[h]; ++groups; } + } + if (groups == 0) { + std::cout << " no group published a count -- the kernel did not run" << std::endl; + return 1; + } + if (total != (uint64_t)n * iters) { + std::cout << " published total=" << total << " expected=" << (uint64_t)n * iters << std::endl; + return 1; + } + return 0; + } +}; + +class Test_AMO64_CAS_LADDER : public ITestCase { +public: + Test_AMO64_CAS_LADDER(TestSuite* s) : ITestCase(s, "amo64_cas_ladder") {} + void setup(uint32_t, uint32_t*, uint32_t*) override {} + int verify(uint32_t n, uint32_t iters, + const uint32_t* shared, const uint32_t* per_hart) override { + const uint32_t slots = (iters < CAS_LADDER_SLOTS) ? iters : CAS_LADDER_SLOTS; + uint64_t wins = 0; + for (uint32_t h = 0; h < n; ++h) { + wins += per_hart[h]; + } + if (wins != slots) { + std::cout << " wins=" << wins << " expected=" << slots + << " (each slot must have exactly one winner)" << std::endl; + return 1; + } + for (uint32_t i = 0; i < slots; ++i) { + uint64_t v = word64(shared, i); + uint32_t k = (uint32_t)v - 1; + if (k >= n || v != amo64_claim_value(k)) { + std::cout << " slot " << i << " holds 0x" << std::hex << v << std::dec + << ", expected a hart's claim" << std::endl; + return 1; + } + } + return 0; + } +}; +#endif + +#if VX_CFG_XLEN == 32 +// A slow load into rd+1, then amoadd.d. Every returned old value must be +// decodable (per-hart bad count 0) and the counter must total base + n*step. +class Test_AMO64_WAR : public ITestCase { +public: + Test_AMO64_WAR(TestSuite* s) : ITestCase(s, "amo64_war") {} + void setup(uint32_t, uint32_t* shared, uint32_t*) override { + set_word64(shared, 0, AMO64_ADD_BASE); + } + int verify(uint32_t n, uint32_t iters, + const uint32_t* shared, const uint32_t* per_hart) override { + int err = check_no_bad_olds(n, per_hart); + err += check64("counter", word64(shared, 0), + AMO64_ADD_BASE + (uint64_t)n * iters * AMO64_ADD_STEP); + return err; + } +}; + +class Test_AMO64_WAR_LMEM : public ITestCase { +public: + Test_AMO64_WAR_LMEM(TestSuite* s) : ITestCase(s, "amo64_war_lmem") {} + void setup(uint32_t n, uint32_t*, uint32_t* per_hart) override { + for (uint32_t h = 0; h < n; ++h) per_hart[h] = ~0u; + } + int verify(uint32_t n, uint32_t iters, + const uint32_t* shared, const uint32_t* per_hart) override { + int err = check_no_bad_olds(n, per_hart); + auto leaders = amo64_lmem_leaders(n, shared); + if (leaders.empty()) { + std::cout << " no group published -- the kernel did not run" << std::endl; + return 1; + } + for (uint32_t h : leaders) { + uint32_t gs = (uint32_t)(word64(shared, h * 8 + 7) & 0xffffu); + err += check64("lmem counter", word64(shared, h * 8), + AMO64_ADD_BASE + (uint64_t)gs * iters * AMO64_ADD_STEP); + } + return err; + } +}; + +// Each thread stores 0 then atomically adds 1 to its own word; the store +// must order before the atomic, so the per-hart bad count must be 0. +class Test_AMO64_STORE_AMO : public ITestCase { +public: + Test_AMO64_STORE_AMO(TestSuite* s) : ITestCase(s, "amo64_store_amo") {} + void setup(uint32_t n, uint32_t*, uint32_t* per_hart) override { + for (uint32_t h = 0; h < n; ++h) per_hart[h] = ~0u; + } + int verify(uint32_t n, uint32_t, + const uint32_t*, const uint32_t* per_hart) override { + return check_no_bad_olds(n, per_hart); + } +}; + +class Test_AMO64_STORE_AMO_LMEM : public ITestCase { +public: + Test_AMO64_STORE_AMO_LMEM(TestSuite* s) : ITestCase(s, "amo64_store_amo_lmem") {} + void setup(uint32_t n, uint32_t*, uint32_t* per_hart) override { + for (uint32_t h = 0; h < n; ++h) per_hart[h] = ~0u; + } + int verify(uint32_t n, uint32_t, + const uint32_t*, const uint32_t* per_hart) override { + return check_no_bad_olds(n, per_hart); + } +}; + +// The zero pair as a discarded destination: each hart's word holds its last +// stored value plus the step, and every read-back matched. +class Test_AMO64_X0 : public ITestCase { +public: + Test_AMO64_X0(TestSuite* s) : ITestCase(s, "amo64_x0") {} + void setup(uint32_t, uint32_t*, uint32_t*) override {} + int verify(uint32_t n, uint32_t iters, + const uint32_t* shared, const uint32_t* per_hart) override { + int err = check_no_bad_olds(n, per_hart); + for (uint32_t h = 0; h < n && iters != 0; ++h) { + uint64_t got = (uint64_t)shared[h * 16] | ((uint64_t)shared[h * 16 + 1] << 32); + uint64_t expected = amo64_x0_value(h, iters - 1) + AMO64_ADD_STEP; + if (got != expected) { + std::cout << " hart " << h << " word=0x" << std::hex << got + << " expected=0x" << expected << std::dec << std::endl; + ++err; + } + } + return err; + } +}; +#endif +#endif + +#if VX_CFG_XLEN == 32 +// Refused .D encodings trap as illegal instructions and leave memory alone. +class Test_AMO64_ILLEGAL : public ITestCase { +public: + Test_AMO64_ILLEGAL(TestSuite* s) : ITestCase(s, "amo64_illegal") {} + void setup(uint32_t, uint32_t* shared, uint32_t*) override { + shared[0] = 0x11111111u; + shared[1] = 0x22222222u; + } + int verify(uint32_t n, uint32_t, + const uint32_t* shared, const uint32_t* per_hart) override { + const uint32_t c = VX_TRAP_CAUSE_ILLEGAL_INSTR; + const uint32_t expected = c | (c << 8) | (c << 16); + for (uint32_t h = 0; h < n; ++h) { + if (per_hart[h] != expected) { + std::cout << " hart " << h << " trap causes=0x" << std::hex << per_hart[h] + << " expected=0x" << expected << std::dec << std::endl; + return 1; + } + } + if (shared[0] != 0x11111111u || shared[1] != 0x22222222u) { + std::cout << " a refused atomic changed memory" << std::endl; + return 1; + } + return 0; + } +}; + +// LD, LWU and SD trap on a 32-bit hart; rd and memory stay as they were. +class Test_LDST64_ILLEGAL : public ITestCase { +public: + Test_LDST64_ILLEGAL(TestSuite* s) : ITestCase(s, "ldst64_illegal") {} + void setup(uint32_t, uint32_t* shared, uint32_t*) override { + shared[0] = 0x11111111u; + shared[1] = 0x22222222u; + } + int verify(uint32_t n, uint32_t, + const uint32_t* shared, const uint32_t* per_hart) override { + const uint32_t c = VX_TRAP_CAUSE_ILLEGAL_INSTR; + const uint32_t expected = c | (c << 8) | (c << 16); + for (uint32_t h = 0; h < n; ++h) { + if (per_hart[h] != expected) { + std::cout << " hart " << h << " trap causes|rd changed=0x" << std::hex << per_hart[h] + << " expected=0x" << expected << std::dec << std::endl; + return 1; + } + } + if (shared[0] != 0x11111111u || shared[1] != 0x22222222u) { + std::cout << " a refused store changed memory" << std::endl; + return 1; + } + return 0; + } +}; +#endif class Test_AMOADD_SCATTER : public ITestCase { public: @@ -584,6 +1008,73 @@ class Test_AMOADD_SCATTER : public ITestCase { } }; +#if AMO64_TESTS +// Pair atomics back to back into each hart's own local-memory words: the +// device counts every unexpected old value and final word. +class Test_AMO64_LMEM_STREAM : public ITestCase { +public: + Test_AMO64_LMEM_STREAM(TestSuite* s) : ITestCase(s, "amo64_lmem_stream") {} + void setup(uint32_t n, uint32_t*, uint32_t* per_hart) override { + for (uint32_t h = 0; h < n; ++h) per_hart[h] = ~0u; + } + int verify(uint32_t n, uint32_t, const uint32_t*, const uint32_t* per_hart) override { + return check_no_bad_olds(n, per_hart); + } +}; +#endif + +#if VX_CFG_XLEN == 32 && VX_CFG_EXT_AMO64_ENABLED +// Misaligned register-pair atomics trap and change nothing; the lanes ahead of +// the misaligned packet or slice, and the aligned warps, perform. +class Test_AMO64_MISALIGNED : public ITestCase { +public: + Test_AMO64_MISALIGNED(TestSuite* s, uint32_t num_threads_per_warp) + : ITestCase(s, "amo64_misaligned"), num_threads_(num_threads_per_warp) {} + void setup(uint32_t n, uint32_t* shared, uint32_t*) override { + for (uint32_t h = 0; h < n; ++h) + for (uint32_t k = 0; k < AMO_MIS_BYTES / 4; ++k) + shared[h * (AMO_MIS_BYTES / 4) + k] = amo_mis_word(h, k); + } + int verify(uint32_t n, uint32_t, + const uint32_t* shared, const uint32_t* per_hart) override { + const uint32_t st = AMO_MIS_CAUSE_AMO; + // a warp of one LSU packet, or of one SIMD slice, has no misaligned lane + // in the packet and slice tries + const bool packets = num_threads_ > VX_CFG_NUM_LSU_LANES; + const bool slices = num_threads_ > VX_CFG_SIMD_WIDTH; + const uint32_t last = AMO_MIS_BYTES / 4 - 1; + int errors = 0; + for (uint32_t h = 0; h < n; ++h) { + const uint32_t lane = h % num_threads_; + const bool odd = ((h / num_threads_) & 1u) != 0; + const uint32_t expected = st | ((packets ? st : 0u) << 4) | ((slices ? st : 0u) << 8) + | ((odd ? 0u : st) << 12); + if (per_hart[h] != expected && errors++ < 4) { + std::cout << " hart " << h << " causes=0x" << std::hex << per_hart[h] + << " expected=0x" << expected << std::dec << std::endl; + } + for (uint32_t k = 0; k < AMO_MIS_BYTES / 4; ++k) { + // the low words of the lanes ahead of the misaligned packet (6) and + // slice (10), and of the aligned warps (12), take the add of 1 + const bool add = (k == 6 && lane < VX_CFG_NUM_LSU_LANES) + || (k == 10 && lane < VX_CFG_SIMD_WIDTH) + || (k == 12 && odd); + const uint32_t want = (k == last) ? 0x1fu : amo_mis_word(h, k) + (add ? 1u : 0u); + if (shared[h * (AMO_MIS_BYTES / 4) + k] != want && errors++ < 4) { + std::cout << " hart " << h << " word " << k << "=0x" << std::hex + << shared[h * (AMO_MIS_BYTES / 4) + k] << " expected=0x" << want + << std::dec << std::endl; + } + } + } + return errors; + } + +private: + uint32_t num_threads_; +}; +#endif + // Each word of the burst lines counts the atomics its harts made. class Test_AMO_LINE_BURST : public ITestCase { public: @@ -692,10 +1183,38 @@ inline TestSuite::TestSuite(vx_device_h device) : device_(device) { add_test(new Test_CAS_LMEM(this)); add_test(new Test_CAS_COUNTER(this)); add_test(new Test_CAS_COUNTER_LMEM(this)); +#endif +#if AMO64_TESTS + add_test(new Test_AMO64_ARITH(this)); + add_test(new Test_AMO64_ARITH_LMEM(this)); + add_test(new Test_AMO64_BITS(this)); + add_test(new Test_AMO64_BITS_LMEM(this)); +#if VX_CFG_EXT_ZACAS_ENABLED + add_test(new Test_AMO64_CAS_COUNTER(this)); + add_test(new Test_AMO64_CAS_COUNTER_LMEM(this)); + add_test(new Test_AMO64_CAS_LADDER(this)); +#endif +#if VX_CFG_XLEN == 32 + add_test(new Test_AMO64_WAR(this)); + add_test(new Test_AMO64_WAR_LMEM(this)); + add_test(new Test_AMO64_STORE_AMO(this)); + add_test(new Test_AMO64_STORE_AMO_LMEM(this)); + add_test(new Test_AMO64_X0(this)); +#endif +#endif +#if VX_CFG_XLEN == 32 + add_test(new Test_AMO64_ILLEGAL(this)); + add_test(new Test_LDST64_ILLEGAL(this)); #endif add_test(new Test_AMOADD_SCATTER(this)); add_test(new Test_AMO_LINE_BURST(this)); add_test(new Test_AMO_MISALIGNED(this, (uint32_t)num_threads)); +#if AMO64_TESTS + add_test(new Test_AMO64_LMEM_STREAM(this)); +#endif +#if VX_CFG_XLEN == 32 && VX_CFG_EXT_AMO64_ENABLED + add_test(new Test_AMO64_MISALIGNED(this, (uint32_t)num_threads)); +#endif } inline TestSuite::~TestSuite() {