Skip to content

Commit 22ad355

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=v4 -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=v4 -c bar.c > /dev/null bar.c:1:5: error: sequentially consistent (seq_cst) atomic load/store is not supported 1 | int foo(int *ptr) { return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); } | ^ ...
1 parent 4b3b71f commit 22ad355

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

llvm/lib/Target/BPF/BPFISelLowering.cpp

Lines changed: 25 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::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,9 @@ 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+
case ISD::ATOMIC_STORE:
329+
return LowerATOMIC_LOAD_STORE(Op, DAG);
319330
}
320331
}
321332

@@ -703,6 +714,20 @@ SDValue BPFTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
703714
return DAG.getNode(BPFISD::SELECT_CC, DL, VTs, Ops);
704715
}
705716

717+
SDValue BPFTargetLowering::LowerATOMIC_LOAD_STORE(SDValue Op,
718+
SelectionDAG &DAG) const {
719+
SDNode *N = Op.getNode();
720+
SDLoc DL(N);
721+
722+
if (cast<AtomicSDNode>(N)->getMergedOrdering() ==
723+
AtomicOrdering::SequentiallyConsistent)
724+
fail(
725+
DL, DAG,
726+
"sequentially consistent (seq_cst) atomic load/store is not supported");
727+
728+
return Op;
729+
}
730+
706731
const char *BPFTargetLowering::getTargetNodeName(unsigned Opcode) const {
707732
switch ((BPFISD::NodeType)Opcode) {
708733
case BPFISD::FIRST_NUMBER:

llvm/lib/Target/BPF/BPFISelLowering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ 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_STORE(SDValue Op, SelectionDAG &DAG) const;
8181
SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
8282
SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
8383

0 commit comments

Comments
 (0)