Skip to content

Commit ea91758

Browse files
Miloš StojanovićMiloš Stojanović
authored andcommitted
[llvm-exegesis][mips] Add support for memory instructions
Implementing functions used to enable testing of memory instructions. Differential Revision: https://reviews.llvm.org/D72858
1 parent 791f132 commit ea91758

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

llvm/tools/llvm-exegesis/lib/Mips/Target.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,44 @@
77
//===----------------------------------------------------------------------===//
88
#include "../Target.h"
99
#include "../Latency.h"
10+
#include "MCTargetDesc/MipsBaseInfo.h"
1011
#include "Mips.h"
1112
#include "MipsRegisterInfo.h"
1213

1314
namespace llvm {
1415
namespace exegesis {
1516

17+
// Returns an error if we cannot handle the memory references in this
18+
// instruction.
19+
static Error isInvalidMemoryInstr(const Instruction &Instr) {
20+
switch (Instr.Description.TSFlags & MipsII::FormMask) {
21+
default:
22+
return Error::success();
23+
llvm_unreachable("Unknown FormMask value");
24+
// These have no memory access.
25+
case MipsII::Pseudo:
26+
case MipsII::FrmR:
27+
case MipsII::FrmJ:
28+
case MipsII::FrmFR:
29+
return Error::success();
30+
// These access memory and are handled.
31+
case MipsII::FrmI:
32+
return Error::success();
33+
// These access memory and are not handled yet.
34+
case MipsII::FrmFI:
35+
case MipsII::FrmOther:
36+
return make_error<Failure>("unsupported opcode: non uniform memory access");
37+
}
38+
}
39+
40+
// Helper to fill a memory operand with a value.
41+
static void setMemOp(InstructionTemplate &IT, int OpIdx,
42+
const MCOperand &OpVal) {
43+
const auto Op = IT.getInstr().Operands[OpIdx];
44+
assert(Op.isExplicit() && "invalid memory pattern");
45+
IT.getValueFor(Op) = OpVal;
46+
}
47+
1648
#include "MipsGenExegesis.inc"
1749

1850
namespace {
@@ -21,6 +53,11 @@ class ExegesisMipsTarget : public ExegesisTarget {
2153
ExegesisMipsTarget() : ExegesisTarget(MipsCpuPfmCounters) {}
2254

2355
private:
56+
unsigned getScratchMemoryRegister(const llvm::Triple &TT) const override;
57+
unsigned getMaxMemoryAccessSize() const override { return 64; }
58+
void fillMemoryOperands(InstructionTemplate &IT, unsigned Reg,
59+
unsigned Offset) const override;
60+
2461
std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, unsigned Reg,
2562
const APInt &Value) const override;
2663
bool matchesArch(Triple::ArchType Arch) const override {
@@ -92,6 +129,20 @@ static std::vector<MCInst> loadImmediate(unsigned Reg, bool IsGPR32,
92129
llvm_unreachable("Not implemented for values wider than 32 bits");
93130
}
94131

132+
unsigned ExegesisMipsTarget::getScratchMemoryRegister(const Triple &TT) const {
133+
return TT.isArch64Bit() ? Mips::A0_64 : Mips::A0;
134+
}
135+
136+
void ExegesisMipsTarget::fillMemoryOperands(InstructionTemplate &IT,
137+
unsigned Reg,
138+
unsigned Offset) const {
139+
assert(!isInvalidMemoryInstr(IT.getInstr()) &&
140+
"fillMemoryOperands requires a valid memory instruction");
141+
setMemOp(IT, 0, MCOperand::createReg(0)); // IndexReg
142+
setMemOp(IT, 1, MCOperand::createReg(Reg)); // BaseReg
143+
setMemOp(IT, 2, MCOperand::createImm(Offset)); // Disp
144+
}
145+
95146
std::vector<MCInst> ExegesisMipsTarget::setRegTo(const MCSubtargetInfo &STI,
96147
unsigned Reg,
97148
const APInt &Value) const {

llvm/unittests/tools/llvm-exegesis/Mips/SnippetGeneratorTest.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "MipsInstrInfo.h"
1414
#include "RegisterAliasing.h"
1515
#include "TestBase.h"
16+
#include "Uops.h"
1617

1718
#include <unordered_set>
1819

@@ -22,6 +23,7 @@ namespace {
2223

2324
using testing::AnyOf;
2425
using testing::ElementsAre;
26+
using testing::HasSubstr;
2527
using testing::SizeIs;
2628

2729
MATCHER(IsInvalid, "") { return !arg.isValid(); }
@@ -49,6 +51,8 @@ class SnippetGeneratorTest : public MipsSnippetGeneratorTest {
4951
using LatencySnippetGeneratorTest =
5052
SnippetGeneratorTest<LatencySnippetGenerator>;
5153

54+
using UopsSnippetGeneratorTest = SnippetGeneratorTest<UopsSnippetGenerator>;
55+
5256
TEST_F(LatencySnippetGeneratorTest, ImplicitSelfDependencyThroughExplicitRegs) {
5357
// - ADD
5458
// - Op0 Explicit Def RegClass(GPR32)
@@ -57,6 +61,7 @@ TEST_F(LatencySnippetGeneratorTest, ImplicitSelfDependencyThroughExplicitRegs) {
5761
// - Var0 [Op0]
5862
// - Var1 [Op1]
5963
// - Var2 [Op2]
64+
// - hasAliasingRegisters
6065
const unsigned Opcode = Mips::ADD;
6166
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
6267
ASSERT_THAT(CodeTemplates, SizeIs(1));
@@ -81,6 +86,7 @@ TEST_F(LatencySnippetGeneratorTest,
8186
// - Var0 [Op0]
8287
// - Var1 [Op1]
8388
// - Var2 [Op2]
89+
// - hasAliasingRegisters
8490
randomGenerator().seed(0); // Initialize seed.
8591
const Instruction &Instr = State.getIC().getInstr(Mips::XOR);
8692
auto AllRegisters = State.getRATC().emptyRegisters();
@@ -90,6 +96,31 @@ TEST_F(LatencySnippetGeneratorTest,
9096
consumeError(std::move(Error));
9197
}
9298

99+
TEST_F(UopsSnippetGeneratorTest, MemoryUse) {
100+
// LB reads from memory.
101+
// - LB
102+
// - Op0 Explicit Def RegClass(GPR32)
103+
// - Op1 Explicit Use Memory RegClass(MSA128F16)
104+
// - Op2 Explicit Use Memory
105+
// - Var0 [Op0]
106+
// - Var1 [Op1]
107+
// - Var2 [Op2]
108+
// - hasMemoryOperands
109+
const unsigned Opcode = Mips::LB;
110+
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
111+
ASSERT_THAT(CodeTemplates, SizeIs(1));
112+
const auto &CT = CodeTemplates[0];
113+
EXPECT_THAT(CT.Info, HasSubstr("instruction is parallel, repeating a random one."));
114+
EXPECT_THAT(CT.Execution, ExecutionMode::UNKNOWN);
115+
ASSERT_THAT(CT.Instructions,
116+
SizeIs(UopsSnippetGenerator::kMinNumDifferentAddresses));
117+
const InstructionTemplate &IT = CT.Instructions[0];
118+
EXPECT_THAT(IT.getOpcode(), Opcode);
119+
ASSERT_THAT(IT.getVariableValues(), SizeIs(3));
120+
EXPECT_EQ(IT.getVariableValues()[0].getReg(), 0u);
121+
EXPECT_EQ(IT.getVariableValues()[2].getImm(), 0);
122+
}
123+
93124
} // namespace
94125
} // namespace exegesis
95126
} // namespace llvm

0 commit comments

Comments
 (0)