This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Use AMOSWAP for 32 and 64-bit atomic stores
Needs ReviewPublic

Authored by paulkirth on Apr 28 2023, 2:56 PM.

Details

Summary

These are expected to be more performant and comply with the
recommendations in Hans Boehm's proposal for changes to table A.6 of the
unprivileged spec and the RISC-V psABI. Further details can be found in
the psABI proposal:
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/378.

The proposal suggests that as an optimization, the memory_order_seq_cst
store mapping may be replaced by the following for 32- and 64-bit
operands:

amoswap.rl{w|d};

Depends on D149486

Diff Detail

Event Timeline

paulkirth created this revision.Apr 28 2023, 2:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2023, 2:56 PM
paulkirth requested review of this revision.Apr 28 2023, 2:56 PM

I thought this was being abandoned because it has worse performance. Certainly there are codegen regressions here that clearly indicate it being a bad idea.

I thought this was being abandoned because it has worse performance. Certainly there are codegen regressions here that clearly indicate it being a bad idea.

The proposed change to the ISA, psABI, and the discussion on tech-unprivileged seem to indicate that this may still be profitable. Since this is only a potential optimization, I think it's fine to wait on this "optimization", or forgo it altogether.

And yes, for forced atomics, it indeed looks like there is a regression in codegen.

llvm/lib/Target/RISCV/RISCVISelLowering.cpp
15805

@jrtc27 Maybe we should forgo the optimization when forced atomics are used? As you've pointed out there seem to be some unintended interactions in that configuration.

paulkirth added inline comments.Apr 28 2023, 3:37 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
15805

oh, this is the wrong place anyway, so ignore my suggestion

paulkirth updated this revision to Diff 518098.Apr 28 2023, 5:13 PM

Disable AMOSWAP optimization with forced atomics

jrtc27 added inline comments.Apr 28 2023, 6:08 PM
llvm/test/CodeGen/RISCV/atomic-load-store.ll
1427

Why are we writing a killed value to a0?

paulkirth added inline comments.May 1 2023, 10:40 AM
llvm/test/CodeGen/RISCV/atomic-load-store.ll
1427

After Atomic Expand the store is rewritten from:

store atomic i32 %b, ptr %a seq_cst, align 4

to

%1 = atomicrmw xchg ptr %a, i32 %b release, align 4

The langref says the semantics for xchg here should be :*ptr = val, so I think that's all correct. https://llvm.org/docs/LangRef.html#id229

From there, the rest of the codegen is consistent w/ the contents of https://github.com/llvm/llvm-project/blob/f762798599171efca03964f4371cc8104d2392f9/llvm/test/CodeGen/RISCV/atomic-rmw.ll#L9844

If that is wrong, then I guess we need to adjust the tablegen for AMOSWAP?

paulkirth updated this revision to Diff 518571.May 1 2023, 2:54 PM
paulkirth edited the summary of this revision. (Show Details)

Rebase

asb added a comment.May 3 2023, 4:09 AM

Do you know if GCC landed this change as well?

llvm/lib/Target/RISCV/RISCVISelLowering.cpp
10097

Nit: Capitalise 'returns' and add full stop at end of sentence.

pirama added a subscriber: pirama.May 25 2023, 9:42 AM
paulkirth updated this revision to Diff 528877.Jun 6 2023, 8:20 AM

Rebase and adpat code to use EnableSeqCstFence.

I should be able to test this on some existing hardware against the existing version and the trailing fence

craig.topper added inline comments.Jun 6 2023, 10:06 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
10098

I think LLVM coding guidelines are to use static for functions rather than anonymous namespace. https://llvm.org/docs/CodingStandards.html#anonymous-namespaces

evandro removed a subscriber: evandro.Jun 6 2023, 10:34 AM
paulkirth updated this revision to Diff 528936.Jun 6 2023, 10:45 AM
paulkirth marked an inline comment as done.

Make helper static instead of using anonymous namespace.

llvm/lib/Target/RISCV/RISCVISelLowering.cpp
10098

huh, I could have sworn we preferred the anonymous namespace, but obviously not.

paulkirth updated this revision to Diff 529042.Jun 6 2023, 2:07 PM

Rebase and adopt feature/march flag