Skip to content

Commit 3568d61

Browse files
yonghong-songtstellar
authored andcommitted
BPF: Implement TTI.IntImmCost() properly
This patch implemented TTI.IntImmCost() properly. Each BPF insn has 32bit immediate space, so for any immediate which can be represented as 32bit signed int, the cost is technically free. If an int cannot be presented as a 32bit signed int, a ld_imm64 instruction is needed and a TCC_Basic is returned. This change is motivated when we observed that several bpf selftests failed with latest llvm trunk, e.g., #10/16 strobemeta.o:FAIL #10/17 strobemeta_nounroll1.o:FAIL #10/18 strobemeta_nounroll2.o:FAIL #10/19 strobemeta_subprogs.o:FAIL #96 snprintf_btf:FAIL The reason of the failure is due to that SpeculateAroundPHIsPass did aggressive transformation which alters control flow for which currently verifer cannot handle well. In llvm12, SpeculateAroundPHIsPass is not called. SpeculateAroundPHIsPass relied on TTI.getIntImmCost() and TTI.getIntImmCostInst() for profitability analysis. This patch implemented TTI.getIntImmCost() properly for BPF backend which also prevented transformation which caused the above test failures. Differential Revision: https://reviews.llvm.org/D96448 (cherry picked from commit a260ae7)
1 parent c27ad80 commit 3568d61

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

llvm/lib/Target/BPF/BPFTargetMachine.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "BPFTargetMachine.h"
1414
#include "BPF.h"
15+
#include "BPFTargetTransformInfo.h"
1516
#include "MCTargetDesc/BPFMCAsmInfo.h"
1617
#include "TargetInfo/BPFTargetInfo.h"
1718
#include "llvm/CodeGen/Passes.h"
@@ -145,6 +146,11 @@ void BPFPassConfig::addIRPasses() {
145146
TargetPassConfig::addIRPasses();
146147
}
147148

149+
TargetTransformInfo
150+
BPFTargetMachine::getTargetTransformInfo(const Function &F) {
151+
return TargetTransformInfo(BPFTTIImpl(this, F));
152+
}
153+
148154
// Install an instruction selector pass using
149155
// the ISelDag to gen BPF code.
150156
bool BPFPassConfig::addInstSelector() {

llvm/lib/Target/BPF/BPFTargetMachine.h

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class BPFTargetMachine : public LLVMTargetMachine {
3434

3535
TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
3636

37+
TargetTransformInfo getTargetTransformInfo(const Function &F) override;
38+
3739
TargetLoweringObjectFile *getObjFileLowering() const override {
3840
return TLOF.get();
3941
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===------ BPFTargetTransformInfo.h - BPF specific TTI ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file uses the target's specific information to
10+
// provide more precise answers to certain TTI queries, while letting the
11+
// target independent and default TTI implementations handle the rest.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_LIB_TARGET_BPF_BPFTARGETTRANSFORMINFO_H
16+
#define LLVM_LIB_TARGET_BPF_BPFTARGETTRANSFORMINFO_H
17+
18+
#include "BPFTargetMachine.h"
19+
#include "llvm/Analysis/TargetTransformInfo.h"
20+
#include "llvm/CodeGen/BasicTTIImpl.h"
21+
22+
namespace llvm {
23+
class BPFTTIImpl : public BasicTTIImplBase<BPFTTIImpl> {
24+
typedef BasicTTIImplBase<BPFTTIImpl> BaseT;
25+
typedef TargetTransformInfo TTI;
26+
friend BaseT;
27+
28+
const BPFSubtarget *ST;
29+
const BPFTargetLowering *TLI;
30+
31+
const BPFSubtarget *getST() const { return ST; }
32+
const BPFTargetLowering *getTLI() const { return TLI; }
33+
34+
public:
35+
explicit BPFTTIImpl(const BPFTargetMachine *TM, const Function &F)
36+
: BaseT(TM, F.getParent()->getDataLayout()), ST(TM->getSubtargetImpl(F)),
37+
TLI(ST->getTargetLowering()) {}
38+
39+
int getIntImmCost(const APInt &Imm, Type *Ty, TTI::TargetCostKind CostKind) {
40+
if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
41+
return TTI::TCC_Free;
42+
43+
return TTI::TCC_Basic;
44+
}
45+
};
46+
47+
} // end namespace llvm
48+
49+
#endif // LLVM_LIB_TARGET_BPF_BPFTARGETTRANSFORMINFO_H

0 commit comments

Comments
 (0)