[RFC] riscv: Can a valid LR/SC sequence have a backward branch on a failure path?

From: Vladimir Kondratiev

Date: Tue Nov 17 2026 - 04:53:36 EST


Hi all,



I have run into a question regarding LR/SC sequence constraints: can a
valid constrained LR/SC sequence contain a backward branch on a failure
path?



Consider this CAS example, where I placed the failure path before the
LR/SC sequence:



cas:

j 1f

2:

li a0, -1

ret

1:

lr.w t0, (a0)

bne t0, a1, 2b

sc.w t0, a2, (a0)

bnez t0, 1b

li a0, 0

ret



The rules in [1] prohibit taken backward branches within the sequence.
In this example, the backward branch (`2b`) is not taken when LR/SC
successfully completes; it is only taken on the failure path.



My context is control flow graph (CFG) analysis when detecting whether a
particular address belongs to a valid constrained LR/SC loop (where
probes must be rejected).



When scanning `bne t0, a1, 2b` in this scenario, should the scanner
treat this as an invalid sequence and stop, or should it ignore this
branch target and continue scanning forward?



[1]
https://docs.riscv.org/reference/isa/v20260120/unpriv/a-st-ext.html#sec:lrscseq



Thanks,

Vladimir