
As discussed in [1], introduce BPF instructions with load-acquire and store-release semantics under -mcpu=v4. Define 2 new flags: BPF_LOAD_ACQ 0x100 BPF_STORE_REL 0x110 A "load-acquire" is a BPF_STX | BPF_ATOMIC instruction with the 'imm' field set to BPF_LOAD_ACQ (0x100). Similarly, a "store-release" is a BPF_STX | BPF_ATOMIC instruction with the 'imm' field set to BPF_STORE_REL (0x110). Unlike existing atomic read-modify-write operations that only support BPF_W (32-bit) and BPF_DW (64-bit) size modifiers, load-acquires and store-releases also support BPF_B (8-bit) and BPF_H (16-bit). An 8- or 16-bit load-acquire zero-extends the value before writing it to a 32-bit register, just like ARM64 instruction LDAPRH and friends. As an example (assuming little-endian): long foo(long *ptr) { return __atomic_load_n(ptr, __ATOMIC_ACQUIRE); } foo() can be compiled to: db 10 00 00 00 01 00 00 r0 = load_acquire((u64 *)(r1 + 0x0)) 95 00 00 00 00 00 00 00 exit opcode (0xdb): BPF_ATOMIC | BPF_DW | BPF_STX imm (0x00000100): BPF_LOAD_ACQ Similarly: void bar(short *ptr, short val) { __atomic_store_n(ptr, val, __ATOMIC_RELEASE); } bar() can be compiled to: cb 21 00 00 10 01 00 00 store_release((u16 *)(r1 + 0x0), w2) 95 00 00 00 00 00 00 00 exit opcode (0xcb): BPF_ATOMIC | BPF_H | BPF_STX imm (0x00000110): BPF_STORE_REL Inline assembly is also supported. Add a pre-defined macro, __BPF_FEATURE_LOAD_ACQ_STORE_REL, to let developers detect this new feature. It can also be disabled using a new llc option, -disable-load-acq-store-rel. Using __ATOMIC_RELAXED for __atomic_store{,_n}() will generate a "plain" store (BPF_MEM | BPF_STX) instruction: void foo(short *ptr, short val) { __atomic_store_n(ptr, val, __ATOMIC_RELAXED); } 6b 21 00 00 00 00 00 00 *(u16 *)(r1 + 0x0) = w2 95 00 00 00 00 00 00 00 exit Similarly, using __ATOMIC_RELAXED for __atomic_load{,_n}() will generate a zero-extending, "plain" load (BPF_MEM | BPF_LDX) instruction: int foo(char *ptr) { return __atomic_load_n(ptr, __ATOMIC_RELAXED); } 71 11 00 00 00 00 00 00 w1 = *(u8 *)(r1 + 0x0) bc 10 08 00 00 00 00 00 w0 = (s8)w1 95 00 00 00 00 00 00 00 exit Currently __ATOMIC_CONSUME is an alias for __ATOMIC_ACQUIRE. Using __ATOMIC_SEQ_CST ("sequentially consistent") is not supported yet and will cause an 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); } | ^ ... Finally, rename those isST*() and isLD*() helper functions in BPFMISimplifyPatchable.cpp based on what the instructions actually do, rather than their instruction class. [1] https://lore.kernel.org/all/20240729183246.4110549-1-yepeilin@google.com/
122 lines
4.0 KiB
C++
122 lines
4.0 KiB
C++
//===-- BPFSubtarget.h - Define Subtarget for the BPF -----------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file declares the BPF specific subclass of TargetSubtargetInfo.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_LIB_TARGET_BPF_BPFSUBTARGET_H
|
|
#define LLVM_LIB_TARGET_BPF_BPFSUBTARGET_H
|
|
|
|
#include "BPFFrameLowering.h"
|
|
#include "BPFISelLowering.h"
|
|
#include "BPFInstrInfo.h"
|
|
#include "BPFRegisterInfo.h"
|
|
#include "BPFSelectionDAGInfo.h"
|
|
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
|
|
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
|
|
#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
|
|
#include "llvm/CodeGen/RegisterBankInfo.h"
|
|
#include "llvm/CodeGen/SelectionDAGTargetInfo.h"
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
|
#include "llvm/IR/DataLayout.h"
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#define GET_SUBTARGETINFO_HEADER
|
|
#include "BPFGenSubtargetInfo.inc"
|
|
|
|
namespace llvm {
|
|
class StringRef;
|
|
|
|
class BPFSubtarget : public BPFGenSubtargetInfo {
|
|
virtual void anchor();
|
|
BPFInstrInfo InstrInfo;
|
|
BPFFrameLowering FrameLowering;
|
|
BPFTargetLowering TLInfo;
|
|
BPFSelectionDAGInfo TSInfo;
|
|
|
|
private:
|
|
void initializeEnvironment();
|
|
void initSubtargetFeatures(StringRef CPU, StringRef FS);
|
|
|
|
protected:
|
|
// unused
|
|
bool isDummyMode;
|
|
|
|
bool IsLittleEndian;
|
|
|
|
// whether the cpu supports jmp ext
|
|
bool HasJmpExt;
|
|
|
|
// whether the cpu supports jmp32 ext.
|
|
// NOTE: jmp32 is not enabled when alu32 enabled.
|
|
bool HasJmp32;
|
|
|
|
// whether the cpu supports alu32 instructions.
|
|
bool HasAlu32;
|
|
|
|
// whether we should enable MCAsmInfo DwarfUsesRelocationsAcrossSections
|
|
bool UseDwarfRIS;
|
|
|
|
// whether cpu v4 insns are enabled.
|
|
bool HasLdsx, HasMovsx, HasBswap, HasSdivSmod, HasGotol, HasStoreImm,
|
|
HasLoadAcqStoreRel;
|
|
|
|
std::unique_ptr<CallLowering> CallLoweringInfo;
|
|
std::unique_ptr<InstructionSelector> InstSelector;
|
|
std::unique_ptr<LegalizerInfo> Legalizer;
|
|
std::unique_ptr<RegisterBankInfo> RegBankInfo;
|
|
|
|
public:
|
|
// This constructor initializes the data members to match that
|
|
// of the specified triple.
|
|
BPFSubtarget(const Triple &TT, const std::string &CPU, const std::string &FS,
|
|
const TargetMachine &TM);
|
|
|
|
BPFSubtarget &initializeSubtargetDependencies(StringRef CPU, StringRef FS);
|
|
|
|
// ParseSubtargetFeatures - Parses features string setting specified
|
|
// subtarget options. Definition of function is auto generated by tblgen.
|
|
void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
|
|
bool getHasJmpExt() const { return HasJmpExt; }
|
|
bool getHasJmp32() const { return HasJmp32; }
|
|
bool getHasAlu32() const { return HasAlu32; }
|
|
bool getUseDwarfRIS() const { return UseDwarfRIS; }
|
|
bool hasLdsx() const { return HasLdsx; }
|
|
bool hasMovsx() const { return HasMovsx; }
|
|
bool hasBswap() const { return HasBswap; }
|
|
bool hasSdivSmod() const { return HasSdivSmod; }
|
|
bool hasGotol() const { return HasGotol; }
|
|
bool hasStoreImm() const { return HasStoreImm; }
|
|
bool hasLoadAcqStoreRel() const { return HasLoadAcqStoreRel; }
|
|
|
|
bool isLittleEndian() const { return IsLittleEndian; }
|
|
|
|
const BPFInstrInfo *getInstrInfo() const override { return &InstrInfo; }
|
|
const BPFFrameLowering *getFrameLowering() const override {
|
|
return &FrameLowering;
|
|
}
|
|
const BPFTargetLowering *getTargetLowering() const override {
|
|
return &TLInfo;
|
|
}
|
|
const BPFSelectionDAGInfo *getSelectionDAGInfo() const override {
|
|
return &TSInfo;
|
|
}
|
|
const BPFRegisterInfo *getRegisterInfo() const override {
|
|
return &InstrInfo.getRegisterInfo();
|
|
}
|
|
|
|
const CallLowering *getCallLowering() const override;
|
|
InstructionSelector *getInstructionSelector() const override;
|
|
const LegalizerInfo *getLegalizerInfo() const override;
|
|
const RegisterBankInfo *getRegBankInfo() const override;
|
|
};
|
|
} // End llvm namespace
|
|
|
|
#endif
|