Skip to content

Commit 89b78a0

Browse files
author
Brendan Sweeney
committed
Adding isel lowering for Zalasr. Not sure if is matches the psABI yet though
1 parent 56081a5 commit 89b78a0

File tree

4 files changed

+94
-14
lines changed

4 files changed

+94
-14
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19764,6 +19764,14 @@ unsigned RISCVTargetLowering::getCustomCtpopCost(EVT VT,
1976419764
return isCtpopFast(VT) ? 0 : 1;
1976519765
}
1976619766

19767+
bool RISCVTargetLowering::shouldInsertFencesForAtomic(const Instruction *I) const {
19768+
if (Subtarget.hasStdExtZalasr()) {
19769+
return false;
19770+
} else {
19771+
return isa<LoadInst>(I) || isa<StoreInst>(I);
19772+
}
19773+
}
19774+
1976719775
bool RISCVTargetLowering::fallBackToDAGISel(const Instruction &Inst) const {
1976819776
// At the moment, the only scalable instruction GISel knows how to lower is
1976919777
// ret with scalable argument.

llvm/lib/Target/RISCV/RISCVISelLowering.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,8 @@ class RISCVTargetLowering : public TargetLowering {
615615

616616
bool preferZeroCompareBranch() const override { return true; }
617617

618-
bool shouldInsertFencesForAtomic(const Instruction *I) const override {
619-
return isa<LoadInst>(I) || isa<StoreInst>(I);
620-
}
618+
bool shouldInsertFencesForAtomic(const Instruction *I) const override;
619+
621620
Instruction *emitLeadingFence(IRBuilderBase &Builder, Instruction *Inst,
622621
AtomicOrdering Ord) const override;
623622
Instruction *emitTrailingFence(IRBuilderBase &Builder, Instruction *Inst,

llvm/lib/Target/RISCV/RISCVInstrInfoA.td

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,63 @@ defm AMOCAS_Q : AMO_rr_aq_rl<0b00101, 0b100, "amocas.q">;
109109
// Pseudo-instructions and codegen patterns
110110
//===----------------------------------------------------------------------===//
111111

112+
// An atomic load operation that does not need either acquire or release
113+
// semantics.
114+
class relaxed_load<PatFrags base>
115+
: PatFrag<(ops node:$ptr), (base node:$ptr)> {
116+
let IsAtomic = 1;
117+
let IsAtomicOrderingMonotonic = 1;
118+
}
119+
120+
// A atomic load operation that actually needs acquire semantics.
121+
class acquiring_load<PatFrags base>
122+
: PatFrag<(ops node:$ptr), (base node:$ptr)> {
123+
let IsAtomic = 1;
124+
let IsAtomicOrderingAcquire = 1;
125+
}
126+
127+
// An atomic load operation that needs sequential consistency.
128+
class seq_cst_load<PatFrags base>
129+
: PatFrag<(ops node:$ptr), (base node:$ptr)> {
130+
let IsAtomic = 1;
131+
let IsAtomicOrderingSequentiallyConsistent = 1;
132+
}
133+
134+
// An atomic store operation that doesn't actually need to be atomic on RISCV.
135+
class relaxed_store<PatFrag base>
136+
: PatFrag<(ops node:$ptr, node:$val), (base node:$val, node:$ptr)> {
137+
let IsAtomic = 1;
138+
let IsAtomicOrderingMonotonic = 1;
139+
}
140+
141+
// A store operation that actually needs release semantics.
142+
class releasing_store<PatFrag base>
143+
: PatFrag<(ops node:$ptr, node:$val), (base node:$val, node:$ptr)> {
144+
let IsAtomic = 1;
145+
let IsAtomicOrderingReleaseOrStronger = 1;
146+
}
147+
148+
// A store operation that actually needs sequential consistency.
149+
class seq_cst_store<PatFrag base>
150+
: PatFrag<(ops node:$ptr, node:$val), (base node:$val, node:$ptr)> {
151+
let IsAtomic = 1;
152+
let IsAtomicOrderingSequentiallyConsistent = 1;
153+
}
154+
112155
// Atomic load/store are available under both +a and +force-atomics.
113-
// Fences will be inserted for atomic load/stores according to the logic in
114-
// RISCVTargetLowering::{emitLeadingFence,emitTrailingFence}.
115156
let Predicates = [HasAtomicLdSt] in {
116-
def : LdPat<atomic_load_8, LB>;
117-
def : LdPat<atomic_load_16, LH>;
118-
def : LdPat<atomic_load_32, LW>;
157+
def : LdPat<relaxed_load<atomic_load_8>, LB>;
158+
def : LdPat<relaxed_load<atomic_load_16>, LH>;
159+
def : LdPat<relaxed_load<atomic_load_32>, LW>;
119160

120-
def : StPat<atomic_store_8, SB, GPR, XLenVT>;
121-
def : StPat<atomic_store_16, SH, GPR, XLenVT>;
122-
def : StPat<atomic_store_32, SW, GPR, XLenVT>;
161+
def : StPat<relaxed_store<atomic_store_8>, SB, GPR, XLenVT>;
162+
def : StPat<relaxed_store<atomic_store_16>, SH, GPR, XLenVT>;
163+
def : StPat<relaxed_store<atomic_store_32>, SW, GPR, XLenVT>;
123164
}
124165

125166
let Predicates = [HasAtomicLdSt, IsRV64] in {
126-
def : LdPat<atomic_load_64, LD, i64>;
127-
def : StPat<atomic_store_64, SD, GPR, i64>;
167+
def : LdPat<relaxed_load<atomic_load_64>, LD, i64>;
168+
def : StPat<relaxed_store<atomic_store_64>, SD, GPR, i64>;
128169
}
129170

130171
/// AMOs

llvm/lib/Target/RISCV/RISCVInstrInfoZalasr.td

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,36 @@ defm SD_RL : SRL_r_aq_rl<0b011, "sd">;
6363
// Pseudo-instructions and codegen patterns
6464
//===----------------------------------------------------------------------===//
6565

66-
// Future work: Work out mapping with leading/trailing fences, &c
66+
class PatLAQ<SDPatternOperator OpNode, RVInst Inst, ValueType vt = XLenVT>
67+
: Pat<(vt (OpNode (vt GPRMemZeroOffset:$rs1))), (Inst GPRMemZeroOffset:$rs1)>;
68+
69+
class PatSRL<SDPatternOperator OpNode, RVInst Inst, ValueType vt = XLenVT>
70+
: Pat<(OpNode (vt GPR:$rs2), (vt GPRMemZeroOffset:$rs1)), (Inst GPR:$rs2, GPRMemZeroOffset:$rs1)>;
71+
72+
let Predicates = [HasStdExtZalasr] in {
73+
def : PatLAQ<acquiring_load<atomic_load_8>, LB_AQ_AQ>;
74+
def : PatLAQ<seq_cst_load<atomic_load_8>, LB_AQ_AQ_RL>;
75+
76+
def : PatLAQ<acquiring_load<atomic_load_16>, LH_AQ_AQ>;
77+
def : PatLAQ<seq_cst_load<atomic_load_16>, LH_AQ_AQ_RL>;
78+
79+
def : PatLAQ<acquiring_load<atomic_load_32>, LW_AQ_AQ>;
80+
def : PatLAQ<seq_cst_load<atomic_load_32>, LW_AQ_AQ_RL>;
81+
82+
def : PatSRL<releasing_store<atomic_store_8>, SB_RL_RL>;
83+
def : PatSRL<seq_cst_store<atomic_store_8>, SB_RL_AQ_RL>;
84+
85+
def : PatSRL<releasing_store<atomic_store_16>, SH_RL_RL>;
86+
def : PatSRL<seq_cst_store<atomic_store_16>, SH_RL_AQ_RL>;
87+
88+
def : PatSRL<releasing_store<atomic_store_32>, SW_RL_RL>;
89+
def : PatSRL<seq_cst_store<atomic_store_32>, SW_RL_AQ_RL>;
90+
} // Predicates HasStdExtZalasr
91+
92+
let Predicates = [HasStdExtZalasr, IsRV64] in {
93+
def : PatLAQ<acquiring_load<atomic_load_64>, LD_AQ_AQ>;
94+
def : PatLAQ<seq_cst_load<atomic_load_64>, LD_AQ_AQ_RL>;
95+
96+
def : PatSRL<releasing_store<atomic_store_64>, SD_RL_RL>;
97+
def : PatSRL<seq_cst_store<atomic_store_64>, SD_RL_AQ_RL>;
98+
} // Predicates HasStdExtZalasr, IsRV64

0 commit comments

Comments
 (0)