#include "../Common/AssemblerUtils.h"
#include "LlvmState.h"
#include "MCInstrDescView.h"
#include "ParallelSnippetGenerator.h"
#include "RegisterAliasing.h"
#include "SerialSnippetGenerator.h"
#include "TestBase.h"
#include "X86InstrInfo.h"
#include <unordered_set>
namespace llvm {
namespace exegesis {
void InitializeX86ExegesisTarget();
namespace {
using testing::AnyOf;
using testing::ElementsAre;
using testing::Gt;
using testing::HasSubstr;
using testing::Not;
using testing::SizeIs;
using testing::UnorderedElementsAre;
MATCHER(IsInvalid, "") { return !arg.isValid(); }
MATCHER(IsReg, "") { return arg.isReg(); }
template <typename SnippetGeneratorT>
class X86SnippetGeneratorTest : public X86TestBase {
protected:
X86SnippetGeneratorTest() : Generator(State, SnippetGenerator::Options()),
InstrInfo(State.getInstrInfo()) {}
std::vector<CodeTemplate> checkAndGetCodeTemplates(unsigned Opcode) {
randomGenerator().seed(0); const Instruction &Instr = State.getIC().getInstr(Opcode);
auto CodeTemplateOrError = Generator.generateCodeTemplates(
&Instr, State.getRATC().emptyRegisters());
EXPECT_FALSE(CodeTemplateOrError.takeError()); return std::move(CodeTemplateOrError.get());
}
SnippetGeneratorT Generator;
const MCInstrInfo &InstrInfo;
};
using X86SerialSnippetGeneratorTest = X86SnippetGeneratorTest<SerialSnippetGenerator>;
using X86ParallelSnippetGeneratorTest =
X86SnippetGeneratorTest<ParallelSnippetGenerator>;
TEST_F(X86SerialSnippetGeneratorTest, ImplicitSelfDependencyThroughImplicitReg) {
const unsigned Opcode = X86::ADC16i16;
EXPECT_THAT(InstrInfo.get(Opcode).getImplicitDefs()[0], X86::AX);
EXPECT_THAT(InstrInfo.get(Opcode).getImplicitDefs()[1], X86::EFLAGS);
EXPECT_THAT(InstrInfo.get(Opcode).getImplicitUses()[0], X86::AX);
EXPECT_THAT(InstrInfo.get(Opcode).getImplicitUses()[1], X86::EFLAGS);
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
ASSERT_THAT(CodeTemplates, SizeIs(1));
const auto &CT = CodeTemplates[0];
EXPECT_THAT(CT.Execution, ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS);
ASSERT_THAT(CT.Instructions, SizeIs(1));
const InstructionTemplate &IT = CT.Instructions[0];
EXPECT_THAT(IT.getOpcode(), Opcode);
ASSERT_THAT(IT.getVariableValues(), SizeIs(1)); EXPECT_THAT(IT.getVariableValues()[0], IsInvalid()) << "Immediate is not set";
}
TEST_F(X86SerialSnippetGeneratorTest, ImplicitSelfDependencyThroughTiedRegs) {
const unsigned Opcode = X86::ADD16ri;
EXPECT_THAT(InstrInfo.get(Opcode).getImplicitDefs()[0], X86::EFLAGS);
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
ASSERT_THAT(CodeTemplates, SizeIs(1));
const auto &CT = CodeTemplates[0];
EXPECT_THAT(CT.Execution, ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS);
ASSERT_THAT(CT.Instructions, SizeIs(1));
const InstructionTemplate &IT = CT.Instructions[0];
EXPECT_THAT(IT.getOpcode(), Opcode);
ASSERT_THAT(IT.getVariableValues(), SizeIs(2));
EXPECT_THAT(IT.getVariableValues()[0], IsInvalid()) << "Operand 1 is not set";
EXPECT_THAT(IT.getVariableValues()[1], IsInvalid()) << "Operand 2 is not set";
}
TEST_F(X86SerialSnippetGeneratorTest, ImplicitSelfDependencyThroughExplicitRegs) {
const unsigned Opcode = X86::VXORPSrr;
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
ASSERT_THAT(CodeTemplates, SizeIs(1));
const auto &CT = CodeTemplates[0];
EXPECT_THAT(CT.Execution, ExecutionMode::SERIAL_VIA_EXPLICIT_REGS);
ASSERT_THAT(CT.Instructions, SizeIs(1));
const InstructionTemplate &IT = CT.Instructions[0];
EXPECT_THAT(IT.getOpcode(), Opcode);
ASSERT_THAT(IT.getVariableValues(), SizeIs(3));
EXPECT_THAT(IT.getVariableValues(),
AnyOf(ElementsAre(IsReg(), IsInvalid(), IsReg()),
ElementsAre(IsReg(), IsReg(), IsInvalid())))
<< "Op0 is either set to Op1 or to Op2";
}
TEST_F(X86SerialSnippetGeneratorTest,
ImplicitSelfDependencyThroughExplicitRegsForbidAll) {
const unsigned Opcode = X86::VXORPSrr;
randomGenerator().seed(0); const Instruction &Instr = State.getIC().getInstr(Opcode);
auto AllRegisters = State.getRATC().emptyRegisters();
AllRegisters.flip();
auto Error =
Generator.generateCodeTemplates(&Instr, AllRegisters).takeError();
EXPECT_TRUE((bool)Error);
consumeError(std::move(Error));
}
TEST_F(X86SerialSnippetGeneratorTest, DependencyThroughOtherOpcode) {
const unsigned Opcode = X86::CMP64rr;
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
ASSERT_THAT(CodeTemplates, SizeIs(Gt(1U))) << "Many templates are available";
for (const auto &CT : CodeTemplates) {
EXPECT_THAT(CT.Execution, ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR);
ASSERT_THAT(CT.Instructions, SizeIs(2));
const InstructionTemplate &IT = CT.Instructions[0];
EXPECT_THAT(IT.getOpcode(), Opcode);
ASSERT_THAT(IT.getVariableValues(), SizeIs(2));
EXPECT_THAT(IT.getVariableValues(),
AnyOf(ElementsAre(IsReg(), IsInvalid()),
ElementsAre(IsInvalid(), IsReg())));
EXPECT_THAT(CT.Instructions[1].getOpcode(), Not(Opcode));
}
}
TEST_F(X86SerialSnippetGeneratorTest, LAHF) {
const unsigned Opcode = X86::LAHF;
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
ASSERT_THAT(CodeTemplates, SizeIs(Gt(1U))) << "Many templates are available";
for (const auto &CT : CodeTemplates) {
EXPECT_THAT(CT.Execution, ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR);
ASSERT_THAT(CT.Instructions, SizeIs(2));
const InstructionTemplate &IT = CT.Instructions[0];
EXPECT_THAT(IT.getOpcode(), Opcode);
ASSERT_THAT(IT.getVariableValues(), SizeIs(0));
}
}
TEST_F(X86SerialSnippetGeneratorTest, VCVTUSI642SDZrrb_Int) {
const unsigned Opcode = X86::VCVTUSI642SDZrrb_Int;
const Instruction &Instr = State.getIC().getInstr(Opcode);
std::vector<BenchmarkCode> Configs;
auto Error = Generator.generateConfigurations(
&Instr, Configs, State.getRATC().emptyRegisters());
ASSERT_FALSE(Error);
ASSERT_THAT(Configs, SizeIs(1));
const BenchmarkCode &BC = Configs[0];
ASSERT_THAT(BC.Key.Instructions, SizeIs(1));
ASSERT_TRUE(BC.Key.Instructions[0].getOperand(3).isImm());
}
TEST_F(X86ParallelSnippetGeneratorTest, SerialInstruction) {
const unsigned Opcode = X86::CDQ;
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
ASSERT_THAT(CodeTemplates, SizeIs(1));
const auto &CT = CodeTemplates[0];
EXPECT_THAT(CT.Info, HasSubstr("serial"));
EXPECT_THAT(CT.Execution, ExecutionMode::UNKNOWN);
ASSERT_THAT(CT.Instructions, SizeIs(1));
const InstructionTemplate &IT = CT.Instructions[0];
EXPECT_THAT(IT.getOpcode(), Opcode);
ASSERT_THAT(IT.getVariableValues(), SizeIs(0));
}
TEST_F(X86ParallelSnippetGeneratorTest, StaticRenaming) {
const unsigned Opcode = X86::CMOV32rr;
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
ASSERT_THAT(CodeTemplates, SizeIs(1));
const auto &CT = CodeTemplates[0];
EXPECT_THAT(CT.Info, HasSubstr("static renaming"));
EXPECT_THAT(CT.Execution, ExecutionMode::UNKNOWN);
constexpr const unsigned kInstructionCount = 15;
ASSERT_THAT(CT.Instructions, SizeIs(kInstructionCount));
std::unordered_set<unsigned> AllDefRegisters;
for (const auto &IT : CT.Instructions) {
ASSERT_THAT(IT.getVariableValues(), SizeIs(3));
AllDefRegisters.insert(IT.getVariableValues()[0].getReg());
}
EXPECT_THAT(AllDefRegisters, SizeIs(kInstructionCount))
<< "Each instruction writes to a different register";
}
TEST_F(X86ParallelSnippetGeneratorTest, NoTiedVariables) {
const unsigned Opcode = X86::CMOV_GR32;
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
ASSERT_THAT(CodeTemplates, SizeIs(1));
const auto &CT = CodeTemplates[0];
EXPECT_THAT(CT.Info, HasSubstr("no tied variables"));
EXPECT_THAT(CT.Execution, ExecutionMode::UNKNOWN);
ASSERT_THAT(CT.Instructions, SizeIs(1));
const InstructionTemplate &IT = CT.Instructions[0];
EXPECT_THAT(IT.getOpcode(), Opcode);
ASSERT_THAT(IT.getVariableValues(), SizeIs(4));
EXPECT_THAT(IT.getVariableValues()[0].getReg(),
Not(IT.getVariableValues()[1].getReg()))
<< "Def is different from first Use";
EXPECT_THAT(IT.getVariableValues()[0].getReg(),
Not(IT.getVariableValues()[2].getReg()))
<< "Def is different from second Use";
EXPECT_THAT(IT.getVariableValues()[3], IsInvalid());
}
TEST_F(X86ParallelSnippetGeneratorTest, MemoryUse) {
const unsigned Opcode = X86::MOV32rm;
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
ASSERT_THAT(CodeTemplates, SizeIs(1));
const auto &CT = CodeTemplates[0];
EXPECT_THAT(CT.Info, HasSubstr("no tied variables"));
EXPECT_THAT(CT.Execution, ExecutionMode::UNKNOWN);
ASSERT_THAT(CT.Instructions,
SizeIs(ParallelSnippetGenerator::kMinNumDifferentAddresses));
const InstructionTemplate &IT = CT.Instructions[0];
EXPECT_THAT(IT.getOpcode(), Opcode);
ASSERT_THAT(IT.getVariableValues(), SizeIs(6));
EXPECT_EQ(IT.getVariableValues()[2].getImm(), 1);
EXPECT_EQ(IT.getVariableValues()[3].getReg(), 0u);
EXPECT_EQ(IT.getVariableValues()[4].getImm(), 0);
EXPECT_EQ(IT.getVariableValues()[5].getReg(), 0u);
}
TEST_F(X86ParallelSnippetGeneratorTest, MOV16ms) {
const unsigned Opcode = X86::MOV16ms;
const Instruction &Instr = State.getIC().getInstr(Opcode);
std::vector<BenchmarkCode> Benchmarks;
auto Err = Generator.generateConfigurations(&Instr, Benchmarks,
State.getRATC().emptyRegisters());
EXPECT_TRUE((bool)Err);
EXPECT_THAT(toString(std::move(Err)),
testing::HasSubstr("no available registers"));
}
TEST_F(X86ParallelSnippetGeneratorTest,
AvoidSerializingThroughImplicitRegisters) {
const unsigned Opcode = X86::MULX32rr;
randomGenerator().seed(0); const Instruction &Instr = State.getIC().getInstr(Opcode);
auto AllRegisters = State.getRATC().emptyRegisters();
AllRegisters.flip();
AllRegisters.reset(X86::RDX);
AllRegisters.reset(X86::EDX);
AllRegisters.reset(X86::DX);
AllRegisters.reset(X86::DH);
AllRegisters.reset(X86::DL);
auto Err = Generator.generateCodeTemplates(&Instr, AllRegisters);
EXPECT_FALSE((bool)Err);
EXPECT_THAT(toString(Err.takeError()),
testing::HasSubstr("no available registers"));
}
class X86FakeSnippetGenerator : public SnippetGenerator {
public:
X86FakeSnippetGenerator(const LLVMState &State, const Options &Opts)
: SnippetGenerator(State, Opts) {}
const Instruction &getInstr(unsigned Opcode) {
return State.getIC().getInstr(Opcode);
}
InstructionTemplate getInstructionTemplate(unsigned Opcode) {
return {&getInstr(Opcode)};
}
private:
Expected<std::vector<CodeTemplate>>
generateCodeTemplates(InstructionTemplate, const BitVector &) const override {
return make_error<StringError>("not implemented", inconvertibleErrorCode());
}
};
using X86FakeSnippetGeneratorTest = X86SnippetGeneratorTest<X86FakeSnippetGenerator>;
testing::Matcher<const RegisterValue &> IsRegisterValue(unsigned Reg,
APInt Value) {
return testing::AllOf(testing::Field(&RegisterValue::Register, Reg),
testing::Field(&RegisterValue::Value, Value));
}
TEST_F(X86FakeSnippetGeneratorTest, MemoryUse_Movsb) {
const unsigned Opcode = X86::MOVSB;
const Instruction &Instr = State.getIC().getInstr(Opcode);
std::vector<BenchmarkCode> Benchmarks;
auto Error = Generator.generateConfigurations(
&Instr, Benchmarks, State.getRATC().emptyRegisters());
EXPECT_TRUE((bool)Error);
consumeError(std::move(Error));
}
TEST_F(X86FakeSnippetGeneratorTest, ComputeRegisterInitialValuesAdd16ri) {
InstructionTemplate IT = Generator.getInstructionTemplate(X86::ADD16ri);
IT.getValueFor(IT.getInstr().Variables[0]) = MCOperand::createReg(X86::AX);
std::vector<InstructionTemplate> Snippet;
Snippet.push_back(std::move(IT));
const auto RIV = Generator.computeRegisterInitialValues(Snippet);
EXPECT_THAT(RIV, ElementsAre(IsRegisterValue(X86::AX, APInt())));
}
TEST_F(X86FakeSnippetGeneratorTest, ComputeRegisterInitialValuesAdd64rr) {
std::vector<InstructionTemplate> Snippet;
{
InstructionTemplate Mov = Generator.getInstructionTemplate(X86::MOV64ri);
Mov.getValueFor(Mov.getInstr().Variables[0]) =
MCOperand::createReg(X86::RAX);
Mov.getValueFor(Mov.getInstr().Variables[1]) = MCOperand::createImm(42);
Snippet.push_back(std::move(Mov));
}
{
InstructionTemplate Add = Generator.getInstructionTemplate(X86::ADD64rr);
Add.getValueFor(Add.getInstr().Variables[0]) =
MCOperand::createReg(X86::RAX);
Add.getValueFor(Add.getInstr().Variables[1]) =
MCOperand::createReg(X86::RBX);
Snippet.push_back(std::move(Add));
}
const auto RIV = Generator.computeRegisterInitialValues(Snippet);
EXPECT_THAT(RIV, ElementsAre(IsRegisterValue(X86::RBX, APInt())));
}
} } }