2018-11-30 09:01:52 +08:00
|
|
|
//===------- X86InsertPrefetch.cpp - Insert cache prefetch hints ----------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2018-11-30 09:01:52 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass applies cache prefetch instructions based on a profile. The pass
|
|
|
|
// assumes DiscriminateMemOps ran immediately before, to ensure debug info
|
|
|
|
// matches the one used at profile generation time. The profile is encoded in
|
|
|
|
// afdo format (text or binary). It contains prefetch hints recommendations.
|
|
|
|
// Each recommendation is made in terms of debug info locations, a type (i.e.
|
|
|
|
// nta, t{0|1|2}) and a delta. The debug info identifies an instruction with a
|
|
|
|
// memory operand (see X86DiscriminateMemOps). The prefetch will be made for
|
|
|
|
// a location at that memory operand + the delta specified in the
|
|
|
|
// recommendation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "X86.h"
|
|
|
|
#include "X86InstrBuilder.h"
|
|
|
|
#include "X86InstrInfo.h"
|
|
|
|
#include "X86MachineFunctionInfo.h"
|
|
|
|
#include "X86Subtarget.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
|
|
|
#include "llvm/ProfileData/SampleProf.h"
|
|
|
|
#include "llvm/ProfileData/SampleProfReader.h"
|
|
|
|
#include "llvm/Transforms/IPO/SampleProfile.h"
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace sampleprof;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
PrefetchHintsFile("prefetch-hints-file",
|
[llvm] Opt-in flag for X86DiscriminateMemOps
Summary:
Currently, if an instruction with a memory operand has no debug information,
X86DiscriminateMemOps will generate one based on the first line of the
enclosing function, or the last seen debug info.
This may cause confusion in certain debugging scenarios. The long term
approach would be to use the line number '0' in such cases, however, that
brings in challenges: the base discriminator value range is limited
(4096 values).
For the short term, adding an opt-in flag for this feature.
See bug 40319 (https://bugs.llvm.org/show_bug.cgi?id=40319)
Reviewers: dblaikie, jmorse, gbedwell
Reviewed By: dblaikie
Subscribers: aprantl, eraman, hiraditya
Differential Revision: https://reviews.llvm.org/D57257
llvm-svn: 352246
2019-01-26 05:49:54 +08:00
|
|
|
cl::desc("Path to the prefetch hints profile. See also "
|
|
|
|
"-x86-discriminate-memops"),
|
2018-11-30 09:01:52 +08:00
|
|
|
cl::Hidden);
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class X86InsertPrefetch : public MachineFunctionPass {
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
bool doInitialization(Module &) override;
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
struct PrefetchInfo {
|
|
|
|
unsigned InstructionID;
|
|
|
|
int64_t Delta;
|
|
|
|
};
|
|
|
|
typedef SmallVectorImpl<PrefetchInfo> Prefetches;
|
|
|
|
bool findPrefetchInfo(const FunctionSamples *Samples, const MachineInstr &MI,
|
|
|
|
Prefetches &prefetches) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
X86InsertPrefetch(const std::string &PrefetchHintsFilename);
|
|
|
|
StringRef getPassName() const override {
|
|
|
|
return "X86 Insert Cache Prefetches";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string Filename;
|
|
|
|
std::unique_ptr<SampleProfileReader> Reader;
|
|
|
|
};
|
|
|
|
|
|
|
|
using PrefetchHints = SampleRecord::CallTargetMap;
|
|
|
|
|
|
|
|
// Return any prefetching hints for the specified MachineInstruction. The hints
|
|
|
|
// are returned as pairs (name, delta).
|
|
|
|
ErrorOr<PrefetchHints> getPrefetchHints(const FunctionSamples *TopSamples,
|
|
|
|
const MachineInstr &MI) {
|
|
|
|
if (const auto &Loc = MI.getDebugLoc())
|
|
|
|
if (const auto *Samples = TopSamples->findFunctionSamples(Loc))
|
|
|
|
return Samples->findCallTargetMapAt(FunctionSamples::getOffset(Loc),
|
|
|
|
Loc->getBaseDiscriminator());
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The prefetch instruction can't take memory operands involving vector
|
|
|
|
// registers.
|
|
|
|
bool IsMemOpCompatibleWithPrefetch(const MachineInstr &MI, int Op) {
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register BaseReg = MI.getOperand(Op + X86::AddrBaseReg).getReg();
|
|
|
|
Register IndexReg = MI.getOperand(Op + X86::AddrIndexReg).getReg();
|
2018-11-30 09:01:52 +08:00
|
|
|
return (BaseReg == 0 ||
|
|
|
|
X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) ||
|
|
|
|
X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg)) &&
|
|
|
|
(IndexReg == 0 ||
|
|
|
|
X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg) ||
|
|
|
|
X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
char X86InsertPrefetch::ID = 0;
|
|
|
|
|
|
|
|
X86InsertPrefetch::X86InsertPrefetch(const std::string &PrefetchHintsFilename)
|
|
|
|
: MachineFunctionPass(ID), Filename(PrefetchHintsFilename) {}
|
|
|
|
|
|
|
|
/// Return true if the provided MachineInstruction has cache prefetch hints. In
|
|
|
|
/// that case, the prefetch hints are stored, in order, in the Prefetches
|
|
|
|
/// vector.
|
|
|
|
bool X86InsertPrefetch::findPrefetchInfo(const FunctionSamples *TopSamples,
|
|
|
|
const MachineInstr &MI,
|
|
|
|
Prefetches &Prefetches) const {
|
|
|
|
assert(Prefetches.empty() &&
|
|
|
|
"Expected caller passed empty PrefetchInfo vector.");
|
2019-09-20 22:31:42 +08:00
|
|
|
static constexpr std::pair<StringLiteral, unsigned> HintTypes[] = {
|
2018-11-30 09:01:52 +08:00
|
|
|
{"_nta_", X86::PREFETCHNTA},
|
|
|
|
{"_t0_", X86::PREFETCHT0},
|
|
|
|
{"_t1_", X86::PREFETCHT1},
|
|
|
|
{"_t2_", X86::PREFETCHT2},
|
|
|
|
};
|
|
|
|
static const char *SerializedPrefetchPrefix = "__prefetch";
|
|
|
|
|
|
|
|
const ErrorOr<PrefetchHints> T = getPrefetchHints(TopSamples, MI);
|
|
|
|
if (!T)
|
|
|
|
return false;
|
|
|
|
int16_t max_index = -1;
|
|
|
|
// Convert serialized prefetch hints into PrefetchInfo objects, and populate
|
|
|
|
// the Prefetches vector.
|
|
|
|
for (const auto &S_V : *T) {
|
|
|
|
StringRef Name = S_V.getKey();
|
|
|
|
if (Name.consume_front(SerializedPrefetchPrefix)) {
|
|
|
|
int64_t D = static_cast<int64_t>(S_V.second);
|
|
|
|
unsigned IID = 0;
|
|
|
|
for (const auto &HintType : HintTypes) {
|
|
|
|
if (Name.startswith(HintType.first)) {
|
|
|
|
Name = Name.drop_front(HintType.first.size());
|
|
|
|
IID = HintType.second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IID == 0)
|
|
|
|
return false;
|
|
|
|
uint8_t index = 0;
|
|
|
|
Name.consumeInteger(10, index);
|
|
|
|
|
|
|
|
if (index >= Prefetches.size())
|
|
|
|
Prefetches.resize(index + 1);
|
|
|
|
Prefetches[index] = {IID, D};
|
|
|
|
max_index = std::max(max_index, static_cast<int16_t>(index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(max_index + 1 >= 0 &&
|
|
|
|
"Possible overflow: max_index + 1 should be positive.");
|
|
|
|
assert(static_cast<size_t>(max_index + 1) == Prefetches.size() &&
|
|
|
|
"The number of prefetch hints received should match the number of "
|
|
|
|
"PrefetchInfo objects returned");
|
|
|
|
return !Prefetches.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool X86InsertPrefetch::doInitialization(Module &M) {
|
|
|
|
if (Filename.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
LLVMContext &Ctx = M.getContext();
|
|
|
|
ErrorOr<std::unique_ptr<SampleProfileReader>> ReaderOrErr =
|
|
|
|
SampleProfileReader::create(Filename, Ctx);
|
|
|
|
if (std::error_code EC = ReaderOrErr.getError()) {
|
|
|
|
std::string Msg = "Could not open profile: " + EC.message();
|
|
|
|
Ctx.diagnose(DiagnosticInfoSampleProfile(Filename, Msg,
|
|
|
|
DiagnosticSeverity::DS_Warning));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Reader = std::move(ReaderOrErr.get());
|
|
|
|
Reader->read();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void X86InsertPrefetch::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2020-07-01 10:10:01 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
2018-11-30 09:01:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool X86InsertPrefetch::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
if (!Reader)
|
|
|
|
return false;
|
|
|
|
const FunctionSamples *Samples = Reader->getSamplesFor(MF.getFunction());
|
|
|
|
if (!Samples)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
|
|
|
|
SmallVector<PrefetchInfo, 4> Prefetches;
|
|
|
|
for (auto &MBB : MF) {
|
|
|
|
for (auto MI = MBB.instr_begin(); MI != MBB.instr_end();) {
|
|
|
|
auto Current = MI;
|
|
|
|
++MI;
|
|
|
|
|
|
|
|
int Offset = X86II::getMemoryOperandNo(Current->getDesc().TSFlags);
|
|
|
|
if (Offset < 0)
|
|
|
|
continue;
|
|
|
|
unsigned Bias = X86II::getOperandBias(Current->getDesc());
|
|
|
|
int MemOpOffset = Offset + Bias;
|
|
|
|
// FIXME(mtrofin): ORE message when the recommendation cannot be taken.
|
|
|
|
if (!IsMemOpCompatibleWithPrefetch(*Current, MemOpOffset))
|
|
|
|
continue;
|
|
|
|
Prefetches.clear();
|
|
|
|
if (!findPrefetchInfo(Samples, *Current, Prefetches))
|
|
|
|
continue;
|
|
|
|
assert(!Prefetches.empty() &&
|
|
|
|
"The Prefetches vector should contain at least a value if "
|
|
|
|
"findPrefetchInfo returned true.");
|
|
|
|
for (auto &PrefInfo : Prefetches) {
|
|
|
|
unsigned PFetchInstrID = PrefInfo.InstructionID;
|
|
|
|
int64_t Delta = PrefInfo.Delta;
|
|
|
|
const MCInstrDesc &Desc = TII->get(PFetchInstrID);
|
|
|
|
MachineInstr *PFetch =
|
|
|
|
MF.CreateMachineInstr(Desc, Current->getDebugLoc(), true);
|
|
|
|
MachineInstrBuilder MIB(MF, PFetch);
|
|
|
|
|
2021-01-23 15:25:05 +08:00
|
|
|
static_assert(X86::AddrBaseReg == 0 && X86::AddrScaleAmt == 1 &&
|
|
|
|
X86::AddrIndexReg == 2 && X86::AddrDisp == 3 &&
|
|
|
|
X86::AddrSegmentReg == 4,
|
|
|
|
"Unexpected change in X86 operand offset order.");
|
2018-11-30 09:01:52 +08:00
|
|
|
|
|
|
|
// This assumes X86::AddBaseReg = 0, {...}ScaleAmt = 1, etc.
|
|
|
|
// FIXME(mtrofin): consider adding a:
|
|
|
|
// MachineInstrBuilder::set(unsigned offset, op).
|
|
|
|
MIB.addReg(Current->getOperand(MemOpOffset + X86::AddrBaseReg).getReg())
|
|
|
|
.addImm(
|
|
|
|
Current->getOperand(MemOpOffset + X86::AddrScaleAmt).getImm())
|
|
|
|
.addReg(
|
|
|
|
Current->getOperand(MemOpOffset + X86::AddrIndexReg).getReg())
|
|
|
|
.addImm(Current->getOperand(MemOpOffset + X86::AddrDisp).getImm() +
|
|
|
|
Delta)
|
|
|
|
.addReg(Current->getOperand(MemOpOffset + X86::AddrSegmentReg)
|
|
|
|
.getReg());
|
|
|
|
|
|
|
|
if (!Current->memoperands_empty()) {
|
|
|
|
MachineMemOperand *CurrentOp = *(Current->memoperands_begin());
|
|
|
|
MIB.addMemOperand(MF.getMachineMemOperand(
|
|
|
|
CurrentOp, CurrentOp->getOffset() + Delta, CurrentOp->getSize()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert before Current. This is because Current may clobber some of
|
|
|
|
// the registers used to describe the input memory operand.
|
|
|
|
MBB.insert(Current, PFetch);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createX86InsertPrefetchPass() {
|
|
|
|
return new X86InsertPrefetch(PrefetchHintsFile);
|
|
|
|
}
|