Skip to content

Commit 8b5f090

Browse files
committed
[RISCV][GISel] Stop miscompiling Zalasr and properly support it.
Our GISel specific atomic patterns didn't use relaxed_load which caused Zalasr to miscompile and use normal load/store instructions without a fence. Fix this and add the necessary patterns to make Zalasr select correctly.
1 parent 129d5ce commit 8b5f090

File tree

4 files changed

+265
-25
lines changed

4 files changed

+265
-25
lines changed

llvm/lib/Target/RISCV/RISCVGISel.td

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,15 @@ def : StPat<truncstorei8, SB, GPR, i16>;
110110

111111
let Predicates = [HasAtomicLdSt] in {
112112
// Prefer unsigned due to no c.lb in Zcb.
113-
def : LdPat<atomic_load_aext_8, LBU, i16>;
114-
def : LdPat<atomic_load_nonext_16, LH, i16>;
113+
def : LdPat<relaxed_load<atomic_load_aext_8>, LBU, i16>;
114+
def : LdPat<relaxed_load<atomic_load_nonext_16>, LH, i16>;
115115

116-
def : StPat<atomic_store_8, SB, GPR, i16>;
117-
def : StPat<atomic_store_16, SH, GPR, i16>;
116+
def : StPat<relaxed_store<atomic_store_8>, SB, GPR, i16>;
117+
def : StPat<relaxed_store<atomic_store_16>, SH, GPR, i16>;
118118
}
119119

120120
let Predicates = [HasAtomicLdSt, IsRV64] in {
121-
def : LdPat<atomic_load_nonext_32, LW, i32>;
122-
def : StPat<atomic_store_32, SW, GPR, i32>;
121+
def : StPat<relaxed_store<atomic_store_32>, SW, GPR, i32>;
123122
}
124123

125124
//===----------------------------------------------------------------------===//
@@ -202,3 +201,28 @@ let Predicates = [HasStdExtZbkb, NoStdExtZbb, IsRV64] in {
202201
def : Pat<(i64 (zext (i16 GPR:$rs))), (PACKW GPR:$rs, (XLenVT X0))>;
203202
def : Pat<(i32 (zext (i16 GPR:$rs))), (PACKW GPR:$rs, (XLenVT X0))>;
204203
}
204+
205+
//===----------------------------------------------------------------------===//
206+
// Zalasr patterns not used by SelectionDAG
207+
//===----------------------------------------------------------------------===//
208+
209+
let Predicates = [HasStdExtZalasr] in {
210+
// the sequentially consistent loads use
211+
// .aq instead of .aqrl to match the psABI/A.7
212+
def : PatLAQ<acquiring_load<atomic_load_aext_8>, LB_AQ, i16>;
213+
def : PatLAQ<seq_cst_load<atomic_load_aext_8>, LB_AQ, i16>;
214+
215+
def : PatLAQ<acquiring_load<atomic_load_nonext_16>, LH_AQ, i16>;
216+
def : PatLAQ<seq_cst_load<atomic_load_nonext_16>, LH_AQ, i16>;
217+
218+
def : PatSRL<releasing_store<atomic_store_8>, SB_RL, i16>;
219+
def : PatSRL<seq_cst_store<atomic_store_8>, SB_RL, i16>;
220+
221+
def : PatSRL<releasing_store<atomic_store_16>, SH_RL, i16>;
222+
def : PatSRL<seq_cst_store<atomic_store_16>, SH_RL, i16>;
223+
}
224+
225+
let Predicates = [HasStdExtZalasr, IsRV64] in {
226+
def : PatSRL<releasing_store<atomic_store_32>, SW_RL, i32>;
227+
def : PatSRL<seq_cst_store<atomic_store_32>, SW_RL, i32>;
228+
}

llvm/lib/Target/RISCV/RISCVInstrInfoA.td

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,14 @@ let Predicates = [HasAtomicLdSt] in {
174174
def : StPat<relaxed_store<atomic_store_8>, SB, GPR, XLenVT>;
175175
def : StPat<relaxed_store<atomic_store_16>, SH, GPR, XLenVT>;
176176
def : StPat<relaxed_store<atomic_store_32>, SW, GPR, XLenVT>;
177-
}
178177

179-
let Predicates = [HasAtomicLdSt, IsRV32] in {
180-
def : LdPat<relaxed_load<atomic_load_nonext_32>, LW>;
178+
// Used by GISel for RV32 and RV64.
179+
def : LdPat<relaxed_load<atomic_load_nonext_32>, LW, i32>;
181180
}
182181

183182
let Predicates = [HasAtomicLdSt, IsRV64] in {
184-
def : LdPat<relaxed_load<atomic_load_asext_32>, LW>;
185-
def : LdPat<relaxed_load<atomic_load_zext_32>, LWU>;
183+
def : LdPat<relaxed_load<atomic_load_asext_32>, LW, i64>;
184+
def : LdPat<relaxed_load<atomic_load_zext_32>, LWU, i64>;
186185
def : LdPat<relaxed_load<atomic_load_nonext_64>, LD, i64>;
187186
def : StPat<relaxed_store<atomic_store_64>, SD, GPR, i64>;
188187
}

llvm/lib/Target/RISCV/RISCVInstrInfoZalasr.td

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ defm SD : SRL_r_aq_rl<0b011, "sd">;
6363
//===----------------------------------------------------------------------===//
6464

6565
class PatLAQ<SDPatternOperator OpNode, RVInst Inst, ValueType vt = XLenVT>
66-
: Pat<(vt (OpNode (vt GPRMemZeroOffset:$rs1))), (Inst GPRMemZeroOffset:$rs1)>;
66+
: Pat<(vt (OpNode (XLenVT GPRMemZeroOffset:$rs1))), (Inst GPRMemZeroOffset:$rs1)>;
6767

6868
// n.b. this switches order of arguments
6969
// to deal with the fact that SRL has addr, data
7070
// while atomic_store has data, addr
7171
class PatSRL<SDPatternOperator OpNode, RVInst Inst, ValueType vt = XLenVT>
72-
: Pat<(OpNode (vt GPR:$rs2), (vt GPRMemZeroOffset:$rs1)),
72+
: Pat<(OpNode (vt GPR:$rs2), (XLenVT GPRMemZeroOffset:$rs1)),
7373
(Inst GPRMemZeroOffset:$rs1, GPR:$rs2)>;
7474

7575

@@ -92,21 +92,19 @@ let Predicates = [HasStdExtZalasr] in {
9292

9393
def : PatSRL<releasing_store<atomic_store_32>, SW_RL>;
9494
def : PatSRL<seq_cst_store<atomic_store_32>, SW_RL>;
95-
} // Predicates = [HasStdExtZalasr]
96-
97-
let Predicates = [HasStdExtZalasr, IsRV32] in {
98-
def : PatLAQ<acquiring_load<atomic_load_nonext_32>, LW_AQ>;
99-
def : PatLAQ<seq_cst_load<atomic_load_nonext_32>, LW_AQ>;
10095

101-
} // Predicates = [HasStdExtZalasr, IsRV64]
96+
// Used by GISel for RV32 and RV64.
97+
def : PatLAQ<acquiring_load<atomic_load_nonext_32>, LW_AQ, i32>;
98+
def : PatLAQ<seq_cst_load<atomic_load_nonext_32>, LW_AQ, i32>;
99+
} // Predicates = [HasStdExtZalasr]
102100

103101
let Predicates = [HasStdExtZalasr, IsRV64] in {
104-
def : PatLAQ<acquiring_load<atomic_load_asext_32>, LW_AQ>;
105-
def : PatLAQ<seq_cst_load<atomic_load_asext_32>, LW_AQ>;
102+
def : PatLAQ<acquiring_load<atomic_load_asext_32>, LW_AQ, i64>;
103+
def : PatLAQ<seq_cst_load<atomic_load_asext_32>, LW_AQ, i64>;
106104

107-
def : PatLAQ<acquiring_load<atomic_load_nonext_64>, LD_AQ>;
108-
def : PatLAQ<seq_cst_load<atomic_load_nonext_64>, LD_AQ>;
105+
def : PatLAQ<acquiring_load<atomic_load_nonext_64>, LD_AQ, i64>;
106+
def : PatLAQ<seq_cst_load<atomic_load_nonext_64>, LD_AQ, i64>;
109107

110-
def : PatSRL<releasing_store<atomic_store_64>, SD_RL>;
111-
def : PatSRL<seq_cst_store<atomic_store_64>, SD_RL>;
108+
def : PatSRL<releasing_store<atomic_store_64>, SD_RL, i64>;
109+
def : PatSRL<seq_cst_store<atomic_store_64>, SD_RL, i64>;
112110
} // Predicates = [HasStdExtZalasr, IsRV64]

0 commit comments

Comments
 (0)