[PATCH] D58829: Do a sign-extension in a compare-and-swap of 32 bit in RV64A

Ferran Pallarès Roca via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 1 07:17:07 PST 2019


fpallares created this revision.
fpallares added a reviewer: asb.
Herald added subscribers: llvm-commits, jdoerfert, jocewei, PkmX, jfb, the_o, brucehoult, MartinMosbeck, edward-jones, zzheng, jrtc27, niosHD, sabuasal, apazos, simoncook, johnrusso, rbar.
Herald added a project: LLVM.

`AtomicCmpSwapWithSuccess` is legalised into an `AtomicCmpSwap` plus a comparison. This requires an extension of the value which, by default, is a zero-extension.

When we later lower `AtomicCmpSwap` into a `PseudoCmpXchg32` and then expanded in `RISCVExpandPseudoInsts.cpp`, the `lr.w` instruction does a sign-extension.

This mismatch of extensions causes the comparison to fail when the compared value is negative.

This change overrides `TargetLowering::getExtendForAtomicOps` for RISC-V so it does a sign-extension instead.


Repository:
  rL LLVM

https://reviews.llvm.org/D58829

Files:
  lib/Target/RISCV/RISCVISelLowering.h
  test/CodeGen/RISCV/atomic-cmpxchg-flag.ll


Index: test/CodeGen/RISCV/atomic-cmpxchg-flag.ll
===================================================================
--- /dev/null
+++ test/CodeGen/RISCV/atomic-cmpxchg-flag.ll
@@ -0,0 +1,14 @@
+; RUN: llc -mtriple=riscv64 -mattr=+a -verify-machineinstrs < %s \
+; RUN:   | FileCheck -check-prefix=RV64IA %s
+
+define i1 @cmpxchg_i32_seq_cst_seq_cst(i32* %ptr, i32 signext %cmp, i32 signext %val) {
+; RV64IA-LABEL: cmpxchg_i32_seq_cst_seq_cst:
+; RV64IA:       lr.w.aqrl [[OLD:[ast][0-9]+]]
+; RV64IA:       bne [[OLD]], [[CMP:a[0-7]+]], {{.*}}
+; RV64IA-NOT:   [[CMP]]
+; RV64IA:       xor {{.*}} [[OLD]], [[CMP]]
+entry:
+  %0 = cmpxchg i32* %ptr, i32 %cmp, i32 %val seq_cst seq_cst
+  %1 = extractvalue { i32, i1 } %0, 1
+  ret i1 %1
+}
Index: lib/Target/RISCV/RISCVISelLowering.h
===================================================================
--- lib/Target/RISCV/RISCVISelLowering.h
+++ lib/Target/RISCV/RISCVISelLowering.h
@@ -106,6 +106,10 @@
   Instruction *emitTrailingFence(IRBuilder<> &Builder, Instruction *Inst,
                                  AtomicOrdering Ord) const override;
 
+  ISD::NodeType getExtendForAtomicOps() const override {
+    return ISD::SIGN_EXTEND;
+  }
+
 private:
   void analyzeInputArgs(MachineFunction &MF, CCState &CCInfo,
                         const SmallVectorImpl<ISD::InputArg> &Ins,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58829.188903.patch
Type: text/x-patch
Size: 1339 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190301/72475d06/attachment.bin>


More information about the llvm-commits mailing list