Skip to content

Commit

Permalink
BPF: Implement TTI.IntImmCost() properly
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yonghong-song committed Feb 11, 2021
1 parent 7e3b9ab commit a260ae7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions llvm/lib/Target/BPF/BPFTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "BPFTargetMachine.h"
#include "BPF.h"
#include "BPFTargetTransformInfo.h"
#include "MCTargetDesc/BPFMCAsmInfo.h"
#include "TargetInfo/BPFTargetInfo.h"
#include "llvm/CodeGen/Passes.h"
Expand Down Expand Up @@ -145,6 +146,11 @@ void BPFPassConfig::addIRPasses() {
TargetPassConfig::addIRPasses();
}

TargetTransformInfo
BPFTargetMachine::getTargetTransformInfo(const Function &F) {
return TargetTransformInfo(BPFTTIImpl(this, F));
}

// Install an instruction selector pass using
// the ISelDag to gen BPF code.
bool BPFPassConfig::addInstSelector() {
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/BPF/BPFTargetMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class BPFTargetMachine : public LLVMTargetMachine {

TargetPassConfig *createPassConfig(PassManagerBase &PM) override;

TargetTransformInfo getTargetTransformInfo(const Function &F) override;

TargetLoweringObjectFile *getObjFileLowering() const override {
return TLOF.get();
}
Expand Down
49 changes: 49 additions & 0 deletions llvm/lib/Target/BPF/BPFTargetTransformInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===------ BPFTargetTransformInfo.h - BPF specific TTI ---------*- 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 uses the target's specific information to
// provide more precise answers to certain TTI queries, while letting the
// target independent and default TTI implementations handle the rest.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_BPF_BPFTARGETTRANSFORMINFO_H
#define LLVM_LIB_TARGET_BPF_BPFTARGETTRANSFORMINFO_H

#include "BPFTargetMachine.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/CodeGen/BasicTTIImpl.h"

namespace llvm {
class BPFTTIImpl : public BasicTTIImplBase<BPFTTIImpl> {
typedef BasicTTIImplBase<BPFTTIImpl> BaseT;
typedef TargetTransformInfo TTI;
friend BaseT;

const BPFSubtarget *ST;
const BPFTargetLowering *TLI;

const BPFSubtarget *getST() const { return ST; }
const BPFTargetLowering *getTLI() const { return TLI; }

public:
explicit BPFTTIImpl(const BPFTargetMachine *TM, const Function &F)
: BaseT(TM, F.getParent()->getDataLayout()), ST(TM->getSubtargetImpl(F)),
TLI(ST->getTargetLowering()) {}

int getIntImmCost(const APInt &Imm, Type *Ty, TTI::TargetCostKind CostKind) {
if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
return TTI::TCC_Free;

return TTI::TCC_Basic;
}
};

} // end namespace llvm

#endif // LLVM_LIB_TARGET_BPF_BPFTARGETTRANSFORMINFO_H

0 comments on commit a260ae7

Please sign in to comment.