Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/lib/Target/BPF/BPF.td
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def MisalignedMemAccess : SubtargetFeature<"allows-misaligned-mem-access",
"AllowsMisalignedMemAccess", "true",
"Allows misaligned memory access">;

def AllowBuiltinCall : SubtargetFeature<"allow-builtin-calls",
"AllowBuiltinCalls", "true",
"Allow calls to builtin functions">;

def : Proc<"generic", []>;
def : Proc<"v1", []>;
def : Proc<"v2", []>;
Expand Down
23 changes: 20 additions & 3 deletions llvm/lib/Target/BPF/BPFISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ BPFTargetLowering::BPFTargetLowering(const TargetMachine &TM,
HasMovsx = STI.hasMovsx();

AllowsMisalignedMemAccess = STI.getAllowsMisalignedMemAccess();
AllowBuiltinCalls = STI.getAllowBuiltinCalls();
}

bool BPFTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, unsigned, Align,
Expand Down Expand Up @@ -567,9 +568,10 @@ SDValue BPFTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
} else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
if (StringRef(E->getSymbol()) != BPF_TRAP) {
Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT, 0);
fail(CLI.DL, DAG,
Twine("A call to built-in function '" + StringRef(E->getSymbol()) +
"' is not supported."));
if (!AllowBuiltinCalls)
fail(CLI.DL, DAG,
Twine("A call to built-in function '" + StringRef(E->getSymbol()) +
"' is not supported."));
}
}

Expand Down Expand Up @@ -1196,3 +1198,18 @@ bool BPFTargetLowering::isLegalAddressingMode(const DataLayout &DL,

return true;
}

bool BPFTargetLowering::shouldSignExtendTypeInLibCall(Type *Ty,
bool IsSigned) const {
return IsSigned || Ty->isIntegerTy(32);
}

bool BPFTargetLowering::CanLowerReturn(
CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context,
const Type *RetTy) const {
// At minimal return Outs.size() <= 1, or check valid types in CC.
SmallVector<CCValAssign, 16> RVLocs;
CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
return CCInfo.CheckReturn(Outs, getHasAlu32() ? RetCC_BPF32 : RetCC_BPF64);
}
10 changes: 10 additions & 0 deletions llvm/lib/Target/BPF/BPFISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class BPFTargetLowering : public TargetLowering {
// Allows Misalignment
bool AllowsMisalignedMemAccess;

bool AllowBuiltinCalls;

SDValue LowerSDIVSREM(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
Expand Down Expand Up @@ -163,6 +165,14 @@ class BPFTargetLowering : public TargetLowering {
MachineBasicBlock *
EmitInstrWithCustomInserterLDimm64(MachineInstr &MI,
MachineBasicBlock *BB) const;

// Returns true if arguments should be sign-extended in lib calls.
bool shouldSignExtendTypeInLibCall(Type *Ty, bool IsSigned) const override;

bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
bool IsVarArg,
const SmallVectorImpl<ISD::OutputArg> &Outs,
LLVMContext &Context, const Type *RetTy) const override;
};
}

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/BPF/BPFSubtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void BPFSubtarget::initializeEnvironment() {
HasLoadAcqStoreRel = false;
HasGotox = false;
AllowsMisalignedMemAccess = false;
AllowBuiltinCalls = false;
}

void BPFSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/BPF/BPFSubtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class BPFSubtarget : public BPFGenSubtargetInfo {
bool HasLdsx, HasMovsx, HasBswap, HasSdivSmod, HasGotol, HasStoreImm,
HasLoadAcqStoreRel, HasGotox;

bool AllowBuiltinCalls;

std::unique_ptr<CallLowering> CallLoweringInfo;
std::unique_ptr<InstructionSelector> InstSelector;
std::unique_ptr<LegalizerInfo> Legalizer;
Expand Down Expand Up @@ -101,6 +103,7 @@ class BPFSubtarget : public BPFGenSubtargetInfo {
bool hasStoreImm() const { return HasStoreImm; }
bool hasLoadAcqStoreRel() const { return HasLoadAcqStoreRel; }
bool hasGotox() const { return HasGotox; }
bool getAllowBuiltinCalls() const { return AllowBuiltinCalls; }

bool isLittleEndian() const { return IsLittleEndian; }

Expand Down
2 changes: 0 additions & 2 deletions llvm/test/CodeGen/BPF/atomic-oversize.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
; RUN: llc -mtriple=bpf < %s | FileCheck %s
; XFAIL: *
; Doesn't currently build, with error 'only small returns supported'.

define void @test(ptr %a) nounwind {
; CHECK-LABEL: test:
Expand Down
39 changes: 39 additions & 0 deletions llvm/test/CodeGen/BPF/builtin_calls.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
; RUN: llc -march=bpfel -mattr=+allow-builtin-calls < %s | FileCheck %s
;
; C code for this test case:
;
; long func(long a, long b) {
; long x;
; return __builtin_mul_overflow(a, b, &x);
; }


declare { i64, i1 } @llvm.smul.with.overflow.i64(i64, i64)

define noundef range(i64 0, 2) i64 @func(i64 noundef %a, i64 noundef %b) local_unnamed_addr {
entry:
%0 = tail call { i64, i1 } @llvm.smul.with.overflow.i64(i64 %a, i64 %b)
%1 = extractvalue { i64, i1 } %0, 1
%conv = zext i1 %1 to i64
ret i64 %conv
}

; CHECK-LABEL: func
; CHECK: r4 = r2
; CHECK: r2 = r1
; CHECK: r3 = r2
; CHECK: r3 s>>= 63
; CHECK: r5 = r4
; CHECK: r5 s>>= 63
; CHECK: r1 = r10
; CHECK: r1 += -16
; CHECK: call __multi3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain in the commit/description message how do you handle __multi3 function? This is clang built-in so you will implement this function in the bpf prog?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we build the core library in Rust, which implements the primitive types and their math operations, we also build as a dependency Rust's compiler-builtin crate. This crate has the implementation of __multi3 (see https://github.com/rust-lang/compiler-builtins/blob/eba1a3f3d2824739f07fe434624b0d2fee917d89/compiler-builtins/src/int/mul.rs#L108-L110).

Once we build the target program and all the dependencies (core and compiler-builtins), we merge all LLVM modules and emit a final object.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this message clear for the commit?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the commit message. Let me know if it is clear. I'm happy to re-write it.

; CHECK: r1 = *(u64 *)(r10 - 16)
; CHECK: r1 s>>= 63
; CHECK: w0 = 1
; CHECK: r2 = *(u64 *)(r10 - 8)
; CHECK: if r2 != r1 goto LBB0_2
; CHECK: # %bb.1: # %entry
; CHECK: w0 = 0
; CHECK: LBB0_2: # %entry
; CHECK: exit
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/BPF/struct_ret1.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; RUN: not llc -mtriple=bpf < %s 2> %t1
; RUN: FileCheck %s < %t1
; CHECK: error: <unknown>:0:0: in function bar { i64, i32 } (i32, i32, i32, i32, i32): aggregate returns are not supported
; CHECK: error: <unknown>:0:0: in function bar { i64, i32 } (i32, i32, i32, i32, i32): stack arguments are not supported

%struct.S = type { i32, i32, i32 }

Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/BPF/struct_ret2.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; RUN: not llc -mtriple=bpf < %s 2> %t1
; RUN: FileCheck %s < %t1
; CHECK: only small returns
; CHECK: too many arguments

; Function Attrs: nounwind uwtable
define { i64, i32 } @foo(i32 %a, i32 %b, i32 %c) #0 {
Expand Down