Skip to content

Commit 06ac94b

Browse files
committed
[BPF] Improve error message for seq_cst atomic load and store
Sequentially consistent (seq_cst) atomic load and store are not supported yet for BPF. Right now, calling __atomic_{load,store}{,_n}() with __ATOMIC_SEQ_CST will cause an error: $ cat bar.c int foo(int *ptr) { return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); } $ clang --target=bpf -mcpu=v5 -c bar.c > /dev/null fatal error: error in backend: Cannot select: t8: i32,ch = AtomicLoad<(load seq_cst (s32) from %ir.0)> t7:1, t7 ... Which isn't very useful. Just like commit 379d908 ("BPF: provide better error message for unsupported atomic operations"), make it generate an error message saying that the requested operation isn't supported, before triggering that "fatal error": $ clang --target=bpf -mcpu=v5 -c bar.c > /dev/null bar.c:1:5: error: sequentially consistent (seq_cst) atomic load is not supported 1 | int foo(int *ptr) { return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); } | ^ ...
1 parent 845e062 commit 06ac94b

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

llvm/lib/Target/BPF/BPFISelLowering.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ BPFTargetLowering::BPFTargetLowering(const TargetMachine &TM,
9393
setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Custom);
9494
}
9595

96+
for (auto VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
97+
setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
98+
setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
99+
}
100+
96101
for (auto VT : { MVT::i32, MVT::i64 }) {
97102
if (VT == MVT::i32 && !STI.getHasAlu32())
98103
continue;
@@ -291,6 +296,9 @@ void BPFTargetLowering::ReplaceNodeResults(
291296
else
292297
Msg = "unsupported atomic operation, please use 64 bit version";
293298
break;
299+
case ISD::ATOMIC_LOAD:
300+
case ISD::ATOMIC_STORE:
301+
return;
294302
}
295303

296304
SDLoc DL(N);
@@ -316,6 +324,10 @@ SDValue BPFTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
316324
return LowerSDIVSREM(Op, DAG);
317325
case ISD::DYNAMIC_STACKALLOC:
318326
return LowerDYNAMIC_STACKALLOC(Op, DAG);
327+
case ISD::ATOMIC_LOAD:
328+
return LowerATOMIC_LOAD(Op, DAG);
329+
case ISD::ATOMIC_STORE:
330+
return LowerATOMIC_STORE(Op, DAG);
319331
}
320332
}
321333

@@ -703,6 +715,39 @@ SDValue BPFTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
703715
return DAG.getNode(BPFISD::SELECT_CC, DL, VTs, Ops);
704716
}
705717

718+
SDValue BPFTargetLowering::LowerATOMIC_LOAD(SDValue Op,
719+
SelectionDAG &DAG) const {
720+
const char *Msg =
721+
"sequentially consistent (seq_cst) atomic load is not supported";
722+
SDNode *N = Op.getNode();
723+
SDLoc DL(N);
724+
725+
if (cast<AtomicSDNode>(N)->getMergedOrdering() ==
726+
AtomicOrdering::SequentiallyConsistent)
727+
fail(DL, DAG, Msg);
728+
729+
return Op;
730+
}
731+
732+
SDValue BPFTargetLowering::LowerATOMIC_STORE(SDValue Op,
733+
SelectionDAG &DAG) const {
734+
const char *Msg =
735+
"sequentially consistent (seq_cst) atomic store is not supported";
736+
EVT VT = Op.getOperand(1).getValueType();
737+
SDNode *N = Op.getNode();
738+
SDLoc DL(N);
739+
740+
// Promote operand #1 (value to store) if it is an i8 or i16.
741+
if (!isTypeLegal(VT))
742+
return SDValue();
743+
744+
if (cast<AtomicSDNode>(N)->getMergedOrdering() ==
745+
AtomicOrdering::SequentiallyConsistent)
746+
fail(DL, DAG, Msg);
747+
748+
return Op;
749+
}
750+
706751
const char *BPFTargetLowering::getTargetNodeName(unsigned Opcode) const {
707752
switch ((BPFISD::NodeType)Opcode) {
708753
case BPFISD::FIRST_NUMBER:

llvm/lib/Target/BPF/BPFISelLowering.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class BPFTargetLowering : public TargetLowering {
7777
SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
7878
SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
7979
SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
80-
80+
SDValue LowerATOMIC_LOAD(SDValue Op, SelectionDAG &DAG) const;
81+
SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) const;
8182
SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
8283
SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
8384

0 commit comments

Comments
 (0)