Sven Tennie pushed to branch wip/supersven/riscv-ncg-fix-atomicread at Glasgow Haskell Compiler / GHC
Commits:
-
dadadff3
by Sven Tennie at 2026-11-15T10:33:03+00:00
2 changed files:
Changes:
| 1 | +section: compiler
|
|
| 2 | +synopsis: Fix the RISC-V NCG sign-extending the result of atomic reads of subword values
|
|
| 3 | +issues: #28130
|
|
| 4 | +mrs: !16997
|
|
| 5 | + |
|
| 6 | +description: {
|
|
| 7 | + Atomic reads of 8-, 16-, and 32-bit values used a signed load (``LDR``),
|
|
| 8 | + which left the upper bits sign-extended. This violated the zero-extended
|
|
| 9 | + subword invariant maintained by the RISC-V NCG, so for example an atomic
|
|
| 10 | + read of a byte holding ``0xff`` produced ``-1`` instead of ``255``.
|
|
| 11 | + |
|
| 12 | + The reads now use ``LDRU``, which maps to ``lbu``, ``lhu``, ``lwu``, or
|
|
| 13 | + ``ld`` depending on the width.
|
|
| 14 | +} |
| ... | ... | @@ -1838,16 +1838,18 @@ genCCall (PrimTarget mop) dest_regs arg_regs = do |
| 1838 | 1838 | -- __atomic_load_n(&a, __ATOMIC_ACQUIRE);
|
| 1839 | 1839 | -- __atomic_load_n(&a, __ATOMIC_SEQ_CST);
|
| 1840 | 1840 | let instrs = case ord of
|
| 1841 | - MemOrderRelaxed -> unitOL $ ann moDescr (LDR (intFormat w) (OpReg w dst) (OpAddr $ AddrReg p))
|
|
| 1841 | + -- N.B. use an unsigned load to maintain the subword invariant;
|
|
| 1842 | + -- a signed load would leave the upper bits sign-extended.
|
|
| 1843 | + MemOrderRelaxed -> unitOL $ ann moDescr (LDRU (intFormat w) (OpReg w dst) (OpAddr $ AddrReg p))
|
|
| 1842 | 1844 | MemOrderAcquire ->
|
| 1843 | 1845 | toOL
|
| 1844 | - [ ann moDescr (LDR (intFormat w) (OpReg w dst) (OpAddr $ AddrReg p)),
|
|
| 1846 | + [ ann moDescr (LDRU (intFormat w) (OpReg w dst) (OpAddr $ AddrReg p)),
|
|
| 1845 | 1847 | FENCE FenceRead FenceReadWrite
|
| 1846 | 1848 | ]
|
| 1847 | 1849 | MemOrderSeqCst ->
|
| 1848 | 1850 | toOL
|
| 1849 | 1851 | [ ann moDescr (FENCE FenceReadWrite FenceReadWrite),
|
| 1850 | - LDR (intFormat w) (OpReg w dst) (OpAddr $ AddrReg p),
|
|
| 1852 | + LDRU (intFormat w) (OpReg w dst) (OpAddr $ AddrReg p),
|
|
| 1851 | 1853 | FENCE FenceRead FenceReadWrite
|
| 1852 | 1854 | ]
|
| 1853 | 1855 | MemOrderRelease -> panic $ "Unexpected MemOrderRelease on an AtomicRead: " ++ show mo
|