Skip to content

Commit

Permalink
[llvm-exegesis][NFC] Simplify LLVMState.
Browse files Browse the repository at this point in the history
Summary: Pretty much everything we need is in llvm::TargetMachine.

Reviewers: gchatelet

Subscribers: llvm-commits, tschuett

Differential Revision: https://reviews.llvm.org/D48428

llvm-svn: 335237
  • Loading branch information
legrosbuffle committed Jun 21, 2018
1 parent bcadb5a commit 760d1d5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
5 changes: 3 additions & 2 deletions llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
Expand Up @@ -63,8 +63,9 @@ BenchmarkRunner::runOne(const BenchmarkConfiguration &Configuration,
unsigned Opcode, unsigned NumRepetitions) const {
InstructionBenchmark InstrBenchmark;
InstrBenchmark.Mode = getMode();
InstrBenchmark.CpuName = State.getCpuName();
InstrBenchmark.LLVMTriple = State.getTriple();
InstrBenchmark.CpuName = State.getTargetMachine().getTargetCPU();
InstrBenchmark.LLVMTriple =
State.getTargetMachine().getTargetTriple().normalize();
InstrBenchmark.NumRepetitions = NumRepetitions;
InstrBenchmark.Info = Configuration.Info;

Expand Down
34 changes: 20 additions & 14 deletions llvm/tools/llvm-exegesis/lib/LlvmState.cpp
Expand Up @@ -20,16 +20,15 @@

namespace exegesis {

LLVMState::LLVMState(const std::string &Triple, const std::string &CpuName)
: TheTriple(Triple), CpuName(CpuName) {
LLVMState::LLVMState(const std::string &Triple, const std::string &CpuName) {
std::string Error;
TheTarget = llvm::TargetRegistry::lookupTarget(TheTriple, Error);
const llvm::Target *const TheTarget =
llvm::TargetRegistry::lookupTarget(Triple, Error);
assert(TheTarget && "unknown target for host");
SubtargetInfo.reset(
TheTarget->createMCSubtargetInfo(TheTriple, CpuName, Features));
InstrInfo.reset(TheTarget->createMCInstrInfo());
RegInfo.reset(TheTarget->createMCRegInfo(TheTriple));
AsmInfo.reset(TheTarget->createMCAsmInfo(*RegInfo, TheTriple));
const llvm::TargetOptions Options;
TargetMachine.reset(static_cast<llvm::LLVMTargetMachine *>(
TheTarget->createTargetMachine(Triple, CpuName, /*Features*/ "", Options,
llvm::Reloc::Model::Static)));
}

LLVMState::LLVMState()
Expand All @@ -38,21 +37,28 @@ LLVMState::LLVMState()

std::unique_ptr<llvm::LLVMTargetMachine>
LLVMState::createTargetMachine() const {
const llvm::TargetOptions Options;
return std::unique_ptr<llvm::LLVMTargetMachine>(
static_cast<llvm::LLVMTargetMachine *>(TheTarget->createTargetMachine(
TheTriple, CpuName, Features, Options, llvm::Reloc::Model::Static)));
static_cast<llvm::LLVMTargetMachine *>(
TargetMachine->getTarget().createTargetMachine(
TargetMachine->getTargetTriple().normalize(),
TargetMachine->getTargetCPU(),
TargetMachine->getTargetFeatureString(), TargetMachine->Options,
llvm::Reloc::Model::Static)));
}

bool LLVMState::canAssemble(const llvm::MCInst &Inst) const {
llvm::MCObjectFileInfo ObjectFileInfo;
llvm::MCContext Context(AsmInfo.get(), RegInfo.get(), &ObjectFileInfo);
llvm::MCContext Context(TargetMachine->getMCAsmInfo(),
TargetMachine->getMCRegisterInfo(), &ObjectFileInfo);
std::unique_ptr<const llvm::MCCodeEmitter> CodeEmitter(
TheTarget->createMCCodeEmitter(*InstrInfo, *RegInfo, Context));
TargetMachine->getTarget().createMCCodeEmitter(
*TargetMachine->getMCInstrInfo(), *TargetMachine->getMCRegisterInfo(),
Context));
llvm::SmallVector<char, 16> Tmp;
llvm::raw_svector_ostream OS(Tmp);
llvm::SmallVector<llvm::MCFixup, 4> Fixups;
CodeEmitter->encodeInstruction(Inst, OS, Fixups, *SubtargetInfo);
CodeEmitter->encodeInstruction(Inst, OS, Fixups,
*TargetMachine->getMCSubtargetInfo());
return Tmp.size() > 0;
}

Expand Down
34 changes: 16 additions & 18 deletions llvm/tools/llvm-exegesis/lib/LlvmState.h
Expand Up @@ -26,6 +26,8 @@

namespace exegesis {

class ExegesisTarget;

// An object to initialize LLVM and prepare objects needed to run the
// measurements.
class LLVMState {
Expand All @@ -35,31 +37,27 @@ class LLVMState {
LLVMState(const std::string &Triple,
const std::string &CpuName); // For tests.

llvm::StringRef getTriple() const { return TheTriple; }
llvm::StringRef getCpuName() const { return CpuName; }
llvm::StringRef getFeatures() const { return Features; }
const llvm::TargetMachine &getTargetMachine() const { return *TargetMachine; }
std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() const;

const llvm::MCInstrInfo &getInstrInfo() const { return *InstrInfo; }
const ExegesisTarget *getExegesisTarget() const { return TheExegesisTarget; }

const llvm::MCRegisterInfo &getRegInfo() const { return *RegInfo; }
bool canAssemble(const llvm::MCInst &mc_inst) const;

// For convenience:
const llvm::MCInstrInfo &getInstrInfo() const {
return *TargetMachine->getMCInstrInfo();
}
const llvm::MCRegisterInfo &getRegInfo() const {
return *TargetMachine->getMCRegisterInfo();
}
const llvm::MCSubtargetInfo &getSubtargetInfo() const {
return *SubtargetInfo;
return *TargetMachine->getMCSubtargetInfo();
}

std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() const;

bool canAssemble(const llvm::MCInst &mc_inst) const;

private:
std::string TheTriple;
std::string CpuName;
std::string Features;
const llvm::Target *TheTarget = nullptr;
std::unique_ptr<const llvm::MCSubtargetInfo> SubtargetInfo;
std::unique_ptr<const llvm::MCInstrInfo> InstrInfo;
std::unique_ptr<const llvm::MCRegisterInfo> RegInfo;
std::unique_ptr<const llvm::MCAsmInfo> AsmInfo;
const ExegesisTarget *TheExegesisTarget = nullptr;
std::unique_ptr<const llvm::TargetMachine> TargetMachine;
};

} // namespace exegesis
Expand Down

0 comments on commit 760d1d5

Please sign in to comment.