diff options
author | Yonghong Song <yhs@fb.com> | 2017-08-23 04:25:57 +0000 |
---|---|---|
committer | Yonghong Song <yhs@fb.com> | 2017-08-23 04:25:57 +0000 |
commit | dc1dbf6ef320175acbdc1206da4b0a176b304449 (patch) | |
tree | 8bdc3de57d2c7319c86e5f484184e2dfb8b00532 /llvm/lib | |
parent | d6c0868da50280334e0dbad9ab6dd87875d2afbd (diff) | |
download | bcm5719-llvm-dc1dbf6ef320175acbdc1206da4b0a176b304449.tar.gz bcm5719-llvm-dc1dbf6ef320175acbdc1206da4b0a176b304449.zip |
bpf: add variants of -mcpu=# and support for additional jmp insns
-mcpu=# will support:
. generic: the default insn set
. v1: insn set version 1, the same as generic
. v2: insn set version 2, version 1 + additional jmp insns
. probe: the compiler will probe the underlying kernel to
decide proper version of insn set.
We did not not use -mcpu=native since llc/llvm will interpret -mcpu=native
as the underlying hardware architecture regardless of -march value.
Currently, only x86_64 supports -mcpu=probe. Other architecture will
silently revert to "generic".
Also added -mcpu=help to print available cpu parameters.
llvm will print out the information only if there are at least one
cpu and at least one feature. Add an unused dummy feature to
enable the printout.
Examples for usage:
$ llc -march=bpf -mcpu=v1 -filetype=asm t.ll
$ llc -march=bpf -mcpu=v2 -filetype=asm t.ll
$ llc -march=bpf -mcpu=generic -filetype=asm t.ll
$ llc -march=bpf -mcpu=probe -filetype=asm t.ll
$ llc -march=bpf -mcpu=v3 -filetype=asm t.ll
'v3' is not a recognized processor for this target (ignoring processor)
...
$ llc -march=bpf -mcpu=help -filetype=asm t.ll
Available CPUs for this target:
generic - Select the generic processor.
probe - Select the probe processor.
v1 - Select the v1 processor.
v2 - Select the v2 processor.
Available features for this target:
dummy - unused feature.
Use +feature to enable a feature, or -feature to disable it.
For example, llc -mcpu=mycpu -mattr=+feature1,-feature2
...
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Yonghong Song <yhs@fb.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
llvm-svn: 311522
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Support/Host.cpp | 39 | ||||
-rw-r--r-- | llvm/lib/Target/BPF/BPF.td | 6 | ||||
-rw-r--r-- | llvm/lib/Target/BPF/BPFISelLowering.cpp | 21 | ||||
-rw-r--r-- | llvm/lib/Target/BPF/BPFISelLowering.h | 5 | ||||
-rw-r--r-- | llvm/lib/Target/BPF/BPFInstrInfo.td | 12 | ||||
-rw-r--r-- | llvm/lib/Target/BPF/BPFSubtarget.cpp | 26 | ||||
-rw-r--r-- | llvm/lib/Target/BPF/BPFSubtarget.h | 15 |
7 files changed, 120 insertions, 4 deletions
diff --git a/llvm/lib/Support/Host.cpp b/llvm/lib/Support/Host.cpp index ac2af7ba567..aa1c4aeaf21 100644 --- a/llvm/lib/Support/Host.cpp +++ b/llvm/lib/Support/Host.cpp @@ -267,6 +267,43 @@ StringRef sys::detail::getHostCPUNameForS390x( return "generic"; } +StringRef sys::detail::getHostCPUNameForBPF() { +#if !defined(__linux__) || !defined(__x86_64__) + return "generic"; +#else + uint8_t insns[40] __attribute__ ((aligned (8))) = + /* BPF_MOV64_IMM(BPF_REG_0, 0) */ + { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + /* BPF_MOV64_IMM(BPF_REG_2, 1) */ + 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, + /* BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */ + 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, + /* BPF_MOV64_IMM(BPF_REG_0, 1) */ + 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, + /* BPF_EXIT_INSN() */ + 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; + + struct bpf_prog_load_attr { + uint32_t prog_type; + uint32_t insn_cnt; + uint64_t insns; + uint64_t license; + uint32_t log_level; + uint32_t log_size; + uint64_t log_buf; + uint32_t kern_version; + uint32_t prog_flags; + } attr = {}; + attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */ + attr.insn_cnt = 5; + attr.insns = (uint64_t)insns; + attr.license = (uint64_t)"DUMMY"; + + int fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr, sizeof(attr)); + return (fd > 0) ? "v2" : "v1"; +#endif +} + #if defined(__i386__) || defined(_M_IX86) || \ defined(__x86_64__) || defined(_M_X64) @@ -1420,7 +1457,7 @@ bool sys::getHostCPUFeatures(StringMap<bool> &Features) { Features["prefetchwt1"] = HasLeaf7 && (ECX & 1); Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save; - Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save; + Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save; // Enable protection keys Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1); diff --git a/llvm/lib/Target/BPF/BPF.td b/llvm/lib/Target/BPF/BPF.td index 11abe520c50..2d0c22a3a51 100644 --- a/llvm/lib/Target/BPF/BPF.td +++ b/llvm/lib/Target/BPF/BPF.td @@ -19,6 +19,12 @@ class Proc<string Name, list<SubtargetFeature> Features> : Processor<Name, NoItineraries, Features>; def : Proc<"generic", []>; +def : Proc<"v1", []>; +def : Proc<"v2", []>; +def : Proc<"probe", []>; + +def DummyFeature : SubtargetFeature<"dummy", "isDummyMode", + "true", "unused feature">; def BPFInstPrinter : AsmWriter { string AsmWriterClassName = "InstPrinter"; diff --git a/llvm/lib/Target/BPF/BPFISelLowering.cpp b/llvm/lib/Target/BPF/BPFISelLowering.cpp index 360de1467b3..94b3c7a16aa 100644 --- a/llvm/lib/Target/BPF/BPFISelLowering.cpp +++ b/llvm/lib/Target/BPF/BPFISelLowering.cpp @@ -130,6 +130,9 @@ BPFTargetLowering::BPFTargetLowering(const TargetMachine &TM, MaxStoresPerMemset = MaxStoresPerMemsetOptSize = 128; MaxStoresPerMemcpy = MaxStoresPerMemcpyOptSize = 128; MaxStoresPerMemmove = MaxStoresPerMemmoveOptSize = 128; + + // CPU/Feature control + HasJmpExt = STI.getHasJmpExt(); } bool BPFTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { @@ -456,7 +459,8 @@ SDValue BPFTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { SDValue Dest = Op.getOperand(4); SDLoc DL(Op); - NegateCC(LHS, RHS, CC); + if (!getHasJmpExt()) + NegateCC(LHS, RHS, CC); return DAG.getNode(BPFISD::BR_CC, DL, Op.getValueType(), Chain, LHS, RHS, DAG.getConstant(CC, DL, MVT::i64), Dest); @@ -470,7 +474,8 @@ SDValue BPFTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); SDLoc DL(Op); - NegateCC(LHS, RHS, CC); + if (!getHasJmpExt()) + NegateCC(LHS, RHS, CC); SDValue TargetCC = DAG.getConstant(CC, DL, MVT::i64); @@ -570,6 +575,18 @@ BPFTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, case ISD::SETNE: NewCC = isSelectOp ? BPF::JNE_rr : BPF::JNE_ri; break; + case ISD::SETLT: + NewCC = isSelectOp ? BPF::JSLT_rr : BPF::JSLT_ri; + break; + case ISD::SETULT: + NewCC = isSelectOp ? BPF::JULT_rr : BPF::JULT_ri; + break; + case ISD::SETLE: + NewCC = isSelectOp ? BPF::JSLE_rr : BPF::JSLE_ri; + break; + case ISD::SETULE: + NewCC = isSelectOp ? BPF::JULE_rr : BPF::JULE_ri; + break; default: report_fatal_error("unimplemented select CondCode " + Twine(CC)); } diff --git a/llvm/lib/Target/BPF/BPFISelLowering.h b/llvm/lib/Target/BPF/BPFISelLowering.h index 0b8a8ca20c3..35065f26495 100644 --- a/llvm/lib/Target/BPF/BPFISelLowering.h +++ b/llvm/lib/Target/BPF/BPFISelLowering.h @@ -50,7 +50,12 @@ public: EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *BB) const override; + bool getHasJmpExt() const { return HasJmpExt; } + private: + // Control Instruction Selection Features + bool HasJmpExt; + SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const; SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const; SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const; diff --git a/llvm/lib/Target/BPF/BPFInstrInfo.td b/llvm/lib/Target/BPF/BPFInstrInfo.td index f68357809ad..3b239e7463d 100644 --- a/llvm/lib/Target/BPF/BPFInstrInfo.td +++ b/llvm/lib/Target/BPF/BPFInstrInfo.td @@ -79,6 +79,14 @@ def BPF_CC_GTU : PatLeaf<(i64 imm), [{return (N->getZExtValue() == ISD::SETUGT);}]>; def BPF_CC_GEU : PatLeaf<(i64 imm), [{return (N->getZExtValue() == ISD::SETUGE);}]>; +def BPF_CC_LE : PatLeaf<(i64 imm), + [{return (N->getZExtValue() == ISD::SETLE);}]>; +def BPF_CC_LT : PatLeaf<(i64 imm), + [{return (N->getZExtValue() == ISD::SETLT);}]>; +def BPF_CC_LTU : PatLeaf<(i64 imm), + [{return (N->getZExtValue() == ISD::SETULT);}]>; +def BPF_CC_LEU : PatLeaf<(i64 imm), + [{return (N->getZExtValue() == ISD::SETULE);}]>; // jump instructions class JMP_RR<bits<4> Opc, string OpcodeStr, PatLeaf Cond> @@ -136,6 +144,10 @@ defm JUGE : J<0x3, ">=", BPF_CC_GEU>; defm JNE : J<0x5, "!=", BPF_CC_NE>; defm JSGT : J<0x6, "s>", BPF_CC_GT>; defm JSGE : J<0x7, "s>=", BPF_CC_GE>; +defm JULT : J<0xa, "<", BPF_CC_LTU>; +defm JULE : J<0xb, "<=", BPF_CC_LEU>; +defm JSLT : J<0xc, "s<", BPF_CC_LT>; +defm JSLE : J<0xd, "s<=", BPF_CC_LE>; } // ALU instructions diff --git a/llvm/lib/Target/BPF/BPFSubtarget.cpp b/llvm/lib/Target/BPF/BPFSubtarget.cpp index c3a8b1caa63..42ca87f9ef6 100644 --- a/llvm/lib/Target/BPF/BPFSubtarget.cpp +++ b/llvm/lib/Target/BPF/BPFSubtarget.cpp @@ -13,6 +13,7 @@ #include "BPFSubtarget.h" #include "BPF.h" +#include "llvm/Support/Host.h" #include "llvm/Support/TargetRegistry.h" using namespace llvm; @@ -25,7 +26,30 @@ using namespace llvm; void BPFSubtarget::anchor() {} +BPFSubtarget &BPFSubtarget::initializeSubtargetDependencies(StringRef CPU, + StringRef FS) { + initializeEnvironment(); + initSubtargetFeatures(CPU, FS); + return *this; +} + +void BPFSubtarget::initializeEnvironment() { + HasJmpExt = false; +} + +void BPFSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) { + if (CPU == "probe") + CPU = sys::detail::getHostCPUNameForBPF(); + if (CPU == "generic" || CPU == "v1") + return; + if (CPU == "v2") { + HasJmpExt = true; + return; + } +} + BPFSubtarget::BPFSubtarget(const Triple &TT, const std::string &CPU, const std::string &FS, const TargetMachine &TM) - : BPFGenSubtargetInfo(TT, CPU, FS), InstrInfo(), FrameLowering(*this), + : BPFGenSubtargetInfo(TT, CPU, FS), InstrInfo(), + FrameLowering(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM, *this) {} diff --git a/llvm/lib/Target/BPF/BPFSubtarget.h b/llvm/lib/Target/BPF/BPFSubtarget.h index 27cc9a262fc..d88d70d09ad 100644 --- a/llvm/lib/Target/BPF/BPFSubtarget.h +++ b/llvm/lib/Target/BPF/BPFSubtarget.h @@ -35,15 +35,30 @@ class BPFSubtarget : public BPFGenSubtargetInfo { BPFTargetLowering TLInfo; SelectionDAGTargetInfo TSInfo; +private: + void initializeEnvironment(); + void initSubtargetFeatures(StringRef CPU, StringRef FS); + bool probeJmpExt(); + +protected: + // unused + bool isDummyMode; + + // whether the cpu supports jmp ext + bool HasJmpExt; + 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 FS); + bool getHasJmpExt() const { return HasJmpExt; } const BPFInstrInfo *getInstrInfo() const override { return &InstrInfo; } const BPFFrameLowering *getFrameLowering() const override { |