[llvm] [Xtensa] Implement volatile load/store. (PR #110292)

Andrei Safronov via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 27 09:17:05 PDT 2024


https://github.com/andreisfr created https://github.com/llvm/llvm-project/pull/110292

Add a memory wait "MEMW" instruction before volatile load/store operations, as implemented in GCC.

>From c629fed9a3cced92bf06404db949b887683f7f12 Mon Sep 17 00:00:00 2001
From: Andrei Safronov <safronov at espressif.com>
Date: Fri, 27 Sep 2024 19:02:30 +0300
Subject: [PATCH] [Xtensa] Implement volatile load/store.

Add a memory wait "MEMW" instruction before volatile
load/store operations, as implemented in GCC.
---
 llvm/lib/Target/Xtensa/XtensaISelLowering.cpp | 14 ++++++
 llvm/lib/Target/Xtensa/XtensaInstrInfo.td     |  4 +-
 llvm/test/CodeGen/Xtensa/blockaddress.ll      | 25 +++++-----
 llvm/test/CodeGen/Xtensa/volatile.ll          | 49 +++++++++++++++++++
 4 files changed, 77 insertions(+), 15 deletions(-)
 create mode 100644 llvm/test/CodeGen/Xtensa/volatile.ll

diff --git a/llvm/lib/Target/Xtensa/XtensaISelLowering.cpp b/llvm/lib/Target/Xtensa/XtensaISelLowering.cpp
index 670930e99334f2..b1d3b5f51d191a 100644
--- a/llvm/lib/Target/Xtensa/XtensaISelLowering.cpp
+++ b/llvm/lib/Target/Xtensa/XtensaISelLowering.cpp
@@ -1104,10 +1104,24 @@ XtensaTargetLowering::emitSelectCC(MachineInstr &MI,
 MachineBasicBlock *XtensaTargetLowering::EmitInstrWithCustomInserter(
     MachineInstr &MI, MachineBasicBlock *MBB) const {
   DebugLoc DL = MI.getDebugLoc();
+  const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
 
   switch (MI.getOpcode()) {
   case Xtensa::SELECT:
     return emitSelectCC(MI, MBB);
+  case Xtensa::S8I:
+  case Xtensa::S16I:
+  case Xtensa::S32I:
+  case Xtensa::L8UI:
+  case Xtensa::L16SI:
+  case Xtensa::L16UI:
+  case Xtensa::L32I: {
+    const MachineMemOperand &MMO = **MI.memoperands_begin();
+    if (MMO.isVolatile()) {
+      BuildMI(*MBB, MI, DL, TII.get(Xtensa::MEMW));
+    }
+    return MBB;
+  }
   default:
     llvm_unreachable("Unexpected instr type to insert");
   }
diff --git a/llvm/lib/Target/Xtensa/XtensaInstrInfo.td b/llvm/lib/Target/Xtensa/XtensaInstrInfo.td
index 0d01864b54bc38..9773480624e92e 100644
--- a/llvm/lib/Target/Xtensa/XtensaInstrInfo.td
+++ b/llvm/lib/Target/Xtensa/XtensaInstrInfo.td
@@ -195,7 +195,7 @@ def SSAI : RRR_Inst<0x00, 0x00, 0x04, (outs), (ins uimm5:$imm),
 //===----------------------------------------------------------------------===//
 
 // Load instructions
-let mayLoad = 1 in {
+let mayLoad = 1, usesCustomInserter = 1 in {
 
   class Load_RRI8<bits<4> oper, string instrAsm, SDPatternOperator opNode,
         ComplexPattern addrOp, Operand memOp>
@@ -216,7 +216,7 @@ def L16UI : Load_RRI8<0x01, "l16ui", zextloadi16, addr_ish2, mem16>;
 def L32I  : Load_RRI8<0x02, "l32i", load, addr_ish4, mem32>;
 
 // Store instructions
-let mayStore = 1 in {
+let mayStore = 1, usesCustomInserter = 1 in {
   class Store_II8<bits<4> oper, string instrAsm, SDPatternOperator opNode,
         ComplexPattern addrOp, Operand memOp>
 	  : RRI8_Inst<0x02, (outs), (ins AR:$t, memOp:$addr),
diff --git a/llvm/test/CodeGen/Xtensa/blockaddress.ll b/llvm/test/CodeGen/Xtensa/blockaddress.ll
index bbeb1790a1b785..e3c23cf8cedf02 100644
--- a/llvm/test/CodeGen/Xtensa/blockaddress.ll
+++ b/llvm/test/CodeGen/Xtensa/blockaddress.ll
@@ -1,22 +1,21 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
 ; RUN: llc --mtriple=xtensa < %s | FileCheck %s
 
 @addr = global ptr null
 
 define void @test_blockaddress() {
-
-  store volatile ptr blockaddress(@test_blockaddress, %block), ptr @addr
-; CHECK:      .literal_position
-; CHECK-NEXT: .literal .LCPI0_0, addr
-; CHECK-NEXT: .literal .LCPI0_1, .Ltmp0
 ; CHECK-LABEL: test_blockaddress:
-; CHECK:      # %bb.0:
-; CHECK-NEXT: l32r a8, .LCPI0_0
-; CHECK-NEXT: l32r a9, .LCPI0_1
-; CHECK-NEXT: s32i a9, a8, 0
-; CHECK-NEXT: l32i a8, a8, 0
-; CHECK-NEXT: jx a8
-; CHECK-NEXT: .Ltmp0:
-; CHECK-NEXT: .LBB0_1:
+; CHECK:         l32r a8, .LCPI0_0
+; CHECK-NEXT:    l32r a9, .LCPI0_1
+; CHECK-NEXT:    memw
+; CHECK-NEXT:    s32i a9, a8, 0
+; CHECK-NEXT:    memw
+; CHECK-NEXT:    l32i a8, a8, 0
+; CHECK-NEXT:    jx a8
+; CHECK-NEXT:  .Ltmp0: # Block address taken
+; CHECK-NEXT:  .LBB0_1: # %block
+; CHECK-NEXT:    ret
+  store volatile ptr blockaddress(@test_blockaddress, %block), ptr @addr
 
   %val = load volatile ptr, ptr @addr
   indirectbr ptr %val, [label %block]
diff --git a/llvm/test/CodeGen/Xtensa/volatile.ll b/llvm/test/CodeGen/Xtensa/volatile.ll
new file mode 100644
index 00000000000000..5d4370167fe763
--- /dev/null
+++ b/llvm/test/CodeGen/Xtensa/volatile.ll
@@ -0,0 +1,49 @@
+; RUN: llc -mtriple=xtensa -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s
+
+ at x_i8 = common dso_local global i8 0, align 8
+ at y_i8 = common dso_local global i8 0, align 8
+ at x_i16 = common dso_local global i16 0, align 8
+ at y_i16 = common dso_local global i16 0, align 8
+ at x_i32 = common dso_local global i32 0, align 8
+ at y_i32 = common dso_local global i32 0, align 8
+
+define void @test() {
+; CHECK: .literal_position
+; CHECK-NEXT:  .literal .LCPI0_0, x_i8
+; CHECK-NEXT:  .literal .LCPI0_1, y_i8
+; CHECK-NEXT:  .literal .LCPI0_2, x_i16
+; CHECK-NEXT:  .literal .LCPI0_3, y_i16
+; CHECK-NEXT:  .literal .LCPI0_4, x_i32
+; CHECK-NEXT:  .literal .LCPI0_5, y_i32
+; CHECK-LABEL: test:
+; CHECK:  # %bb.0:
+; CHECK-NEXT:  l32r a8, .LCPI0_0
+; CHECK-NEXT:  memw
+; CHECK-NEXT:  l8ui a8, a8, 0
+; CHECK-NEXT:  l32r a9, .LCPI0_1
+; CHECK-NEXT:  memw
+; CHECK-NEXT:  s8i a8, a9, 0
+; CHECK-NEXT:  l32r a8, .LCPI0_2
+; CHECK-NEXT:  memw
+; CHECK-NEXT:  l16ui a8, a8, 0
+; CHECK-NEXT:  l32r a9, .LCPI0_3
+; CHECK-NEXT:  memw
+; CHECK-NEXT:  s16i a8, a9, 0
+; CHECK-NEXT:  l32r a8, .LCPI0_4
+; CHECK-NEXT:  memw
+; CHECK-NEXT:  l32i a8, a8, 0
+; CHECK-NEXT:  l32r a9, .LCPI0_5
+; CHECK-NEXT:  memw
+; CHECK-NEXT:  s32i a8, a9, 0
+; CHECK-NEXT:  ret
+
+entry:
+  %0 = load volatile i8, ptr @x_i8, align 4
+  store volatile i8 %0, ptr @y_i8, align 4
+  %1 = load volatile i16, ptr @x_i16, align 4
+  store volatile i16 %1, ptr @y_i16, align 4
+  %2 = load volatile i32, ptr @x_i32, align 4
+  store volatile i32 %2, ptr @y_i32, align 4
+  ret void
+}



More information about the llvm-commits mailing list