Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 1 | //===-- SnippetGeneratorTest.cpp --------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "../Common/AssemblerUtils.h" |
| 11 | #include "Latency.h" |
| 12 | #include "LlvmState.h" |
| 13 | #include "MCInstrDescView.h" |
| 14 | #include "RegisterAliasing.h" |
| 15 | #include "Uops.h" |
| 16 | #include "X86InstrInfo.h" |
| 17 | |
| 18 | #include <unordered_set> |
| 19 | |
| 20 | namespace exegesis { |
| 21 | namespace { |
| 22 | |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 23 | using testing::HasSubstr; |
| 24 | using testing::Not; |
| 25 | using testing::SizeIs; |
| 26 | |
| 27 | MATCHER(IsInvalid, "") { return !arg.isValid(); } |
| 28 | MATCHER(IsReg, "") { return arg.isReg(); } |
| 29 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 30 | class X86SnippetGeneratorTest : public ::testing::Test { |
| 31 | protected: |
| 32 | X86SnippetGeneratorTest() |
Guillaume Chatelet | b391f24 | 2018-06-13 14:07:36 +0000 | [diff] [blame] | 33 | : State("x86_64-unknown-linux", "haswell"), |
| 34 | MCInstrInfo(State.getInstrInfo()), MCRegisterInfo(State.getRegInfo()) {} |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 35 | |
| 36 | static void SetUpTestCase() { |
| 37 | LLVMInitializeX86TargetInfo(); |
| 38 | LLVMInitializeX86TargetMC(); |
| 39 | LLVMInitializeX86Target(); |
| 40 | LLVMInitializeX86AsmPrinter(); |
| 41 | } |
| 42 | |
| 43 | const LLVMState State; |
| 44 | const llvm::MCInstrInfo &MCInstrInfo; |
| 45 | const llvm::MCRegisterInfo &MCRegisterInfo; |
| 46 | }; |
| 47 | |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 48 | template <typename BenchmarkRunner> |
| 49 | class SnippetGeneratorTest : public X86SnippetGeneratorTest { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 50 | protected: |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 51 | SnippetGeneratorTest() : Runner(State) {} |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 52 | |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 53 | SnippetPrototype checkAndGetConfigurations(unsigned Opcode) { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 54 | randomGenerator().seed(0); // Initialize seed. |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 55 | auto ProtoOrError = Runner.generatePrototype(Opcode); |
| 56 | EXPECT_FALSE(ProtoOrError.takeError()); // Valid configuration. |
| 57 | return std::move(ProtoOrError.get()); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 60 | BenchmarkRunner Runner; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 63 | using LatencySnippetGeneratorTest = |
| 64 | SnippetGeneratorTest<LatencyBenchmarkRunner>; |
| 65 | |
| 66 | using UopsSnippetGeneratorTest = SnippetGeneratorTest<UopsBenchmarkRunner>; |
| 67 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 68 | TEST_F(LatencySnippetGeneratorTest, ImplicitSelfDependency) { |
| 69 | // ADC16i16 self alias because of implicit use and def. |
| 70 | |
| 71 | // explicit use 0 : imm |
| 72 | // implicit def : AX |
| 73 | // implicit def : EFLAGS |
| 74 | // implicit use : AX |
| 75 | // implicit use : EFLAGS |
| 76 | const unsigned Opcode = llvm::X86::ADC16i16; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 77 | EXPECT_THAT(MCInstrInfo.get(Opcode).getImplicitDefs()[0], llvm::X86::AX); |
| 78 | EXPECT_THAT(MCInstrInfo.get(Opcode).getImplicitDefs()[1], llvm::X86::EFLAGS); |
| 79 | EXPECT_THAT(MCInstrInfo.get(Opcode).getImplicitUses()[0], llvm::X86::AX); |
| 80 | EXPECT_THAT(MCInstrInfo.get(Opcode).getImplicitUses()[1], llvm::X86::EFLAGS); |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 81 | const SnippetPrototype Proto = checkAndGetConfigurations(Opcode); |
| 82 | EXPECT_THAT(Proto.Explanation, HasSubstr("implicit")); |
| 83 | ASSERT_THAT(Proto.Snippet, SizeIs(1)); |
| 84 | const InstructionInstance &II = Proto.Snippet[0]; |
| 85 | EXPECT_THAT(II.getOpcode(), Opcode); |
| 86 | ASSERT_THAT(II.VariableValues, SizeIs(1)); // Imm. |
| 87 | EXPECT_THAT(II.VariableValues[0], IsInvalid()) << "Immediate is not set"; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST_F(LatencySnippetGeneratorTest, ExplicitSelfDependency) { |
| 91 | // ADD16ri self alias because Op0 and Op1 are tied together. |
| 92 | |
| 93 | // explicit def 0 : reg RegClass=GR16 |
| 94 | // explicit use 1 : reg RegClass=GR16 | TIED_TO:0 |
| 95 | // explicit use 2 : imm |
| 96 | // implicit def : EFLAGS |
| 97 | const unsigned Opcode = llvm::X86::ADD16ri; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 98 | EXPECT_THAT(MCInstrInfo.get(Opcode).getImplicitDefs()[0], llvm::X86::EFLAGS); |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 99 | const SnippetPrototype Proto = checkAndGetConfigurations(Opcode); |
| 100 | EXPECT_THAT(Proto.Explanation, HasSubstr("explicit")); |
| 101 | ASSERT_THAT(Proto.Snippet, SizeIs(1)); |
| 102 | const InstructionInstance &II = Proto.Snippet[0]; |
| 103 | EXPECT_THAT(II.getOpcode(), Opcode); |
| 104 | ASSERT_THAT(II.VariableValues, SizeIs(2)); |
| 105 | EXPECT_THAT(II.VariableValues[0], IsReg()) << "Operand 0 and 1"; |
| 106 | EXPECT_THAT(II.VariableValues[1], IsInvalid()) << "Operand 2 is not set"; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_F(LatencySnippetGeneratorTest, DependencyThroughOtherOpcode) { |
| 110 | // CMP64rr |
| 111 | // explicit use 0 : reg RegClass=GR64 |
| 112 | // explicit use 1 : reg RegClass=GR64 |
| 113 | // implicit def : EFLAGS |
| 114 | |
| 115 | const unsigned Opcode = llvm::X86::CMP64rr; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 116 | const SnippetPrototype Proto = checkAndGetConfigurations(Opcode); |
| 117 | EXPECT_THAT(Proto.Explanation, HasSubstr("cycle through")); |
| 118 | ASSERT_THAT(Proto.Snippet, SizeIs(2)); |
| 119 | const InstructionInstance &II = Proto.Snippet[0]; |
| 120 | EXPECT_THAT(II.getOpcode(), Opcode); |
| 121 | ASSERT_THAT(II.VariableValues, SizeIs(2)); |
| 122 | EXPECT_THAT(II.VariableValues[0], IsReg()); |
| 123 | EXPECT_THAT(II.VariableValues[1], IsInvalid()); |
| 124 | EXPECT_THAT(Proto.Snippet[1].getOpcode(), Not(Opcode)); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 125 | // TODO: check that the two instructions alias each other. |
| 126 | } |
| 127 | |
Guillaume Chatelet | 60e3d58 | 2018-06-13 13:53:56 +0000 | [diff] [blame] | 128 | TEST_F(LatencySnippetGeneratorTest, LAHF) { |
| 129 | const unsigned Opcode = llvm::X86::LAHF; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 130 | const SnippetPrototype Proto = checkAndGetConfigurations(Opcode); |
| 131 | EXPECT_THAT(Proto.Explanation, HasSubstr("cycle through")); |
| 132 | ASSERT_THAT(Proto.Snippet, SizeIs(2)); |
| 133 | const InstructionInstance &II = Proto.Snippet[0]; |
| 134 | EXPECT_THAT(II.getOpcode(), Opcode); |
| 135 | ASSERT_THAT(II.VariableValues, SizeIs(0)); |
Guillaume Chatelet | 60e3d58 | 2018-06-13 13:53:56 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 138 | TEST_F(UopsSnippetGeneratorTest, ParallelInstruction) { |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 139 | // BNDCL32rr is parallel no matter what. |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 140 | |
| 141 | // explicit use 0 : reg RegClass=BNDR |
| 142 | // explicit use 1 : reg RegClass=GR32 |
| 143 | |
| 144 | const unsigned Opcode = llvm::X86::BNDCL32rr; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 145 | const SnippetPrototype Proto = checkAndGetConfigurations(Opcode); |
| 146 | EXPECT_THAT(Proto.Explanation, HasSubstr("parallel")); |
| 147 | ASSERT_THAT(Proto.Snippet, SizeIs(1)); |
| 148 | const InstructionInstance &II = Proto.Snippet[0]; |
| 149 | EXPECT_THAT(II.getOpcode(), Opcode); |
| 150 | ASSERT_THAT(II.VariableValues, SizeIs(2)); |
| 151 | EXPECT_THAT(II.VariableValues[0], IsInvalid()); |
| 152 | EXPECT_THAT(II.VariableValues[1], IsInvalid()); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | TEST_F(UopsSnippetGeneratorTest, SerialInstruction) { |
| 156 | // CDQ is serial no matter what. |
| 157 | |
| 158 | // implicit def : EAX |
| 159 | // implicit def : EDX |
| 160 | // implicit use : EAX |
| 161 | const unsigned Opcode = llvm::X86::CDQ; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 162 | const SnippetPrototype Proto = checkAndGetConfigurations(Opcode); |
| 163 | EXPECT_THAT(Proto.Explanation, HasSubstr("serial")); |
| 164 | ASSERT_THAT(Proto.Snippet, SizeIs(1)); |
| 165 | const InstructionInstance &II = Proto.Snippet[0]; |
| 166 | EXPECT_THAT(II.getOpcode(), Opcode); |
| 167 | ASSERT_THAT(II.VariableValues, SizeIs(0)); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | TEST_F(UopsSnippetGeneratorTest, StaticRenaming) { |
| 171 | // CMOVA32rr has tied variables, we enumarate the possible values to execute |
| 172 | // as many in parallel as possible. |
| 173 | |
| 174 | // explicit def 0 : reg RegClass=GR32 |
| 175 | // explicit use 1 : reg RegClass=GR32 | TIED_TO:0 |
| 176 | // explicit use 2 : reg RegClass=GR32 |
| 177 | // implicit use : EFLAGS |
| 178 | const unsigned Opcode = llvm::X86::CMOVA32rr; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 179 | const SnippetPrototype Proto = checkAndGetConfigurations(Opcode); |
| 180 | EXPECT_THAT(Proto.Explanation, HasSubstr("static renaming")); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 181 | constexpr const unsigned kInstructionCount = 15; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 182 | ASSERT_THAT(Proto.Snippet, SizeIs(kInstructionCount)); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 183 | std::unordered_set<unsigned> AllDefRegisters; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 184 | for (const auto &II : Proto.Snippet) { |
| 185 | ASSERT_THAT(II.VariableValues, SizeIs(2)); |
| 186 | AllDefRegisters.insert(II.VariableValues[0].getReg()); |
| 187 | } |
| 188 | EXPECT_THAT(AllDefRegisters, SizeIs(kInstructionCount)) |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 189 | << "Each instruction writes to a different register"; |
| 190 | } |
| 191 | |
| 192 | TEST_F(UopsSnippetGeneratorTest, NoTiedVariables) { |
| 193 | // CMOV_GR32 has no tied variables, we make sure def and use are different |
| 194 | // from each other. |
| 195 | |
| 196 | // explicit def 0 : reg RegClass=GR32 |
| 197 | // explicit use 1 : reg RegClass=GR32 |
| 198 | // explicit use 2 : reg RegClass=GR32 |
| 199 | // explicit use 3 : imm |
| 200 | // implicit use : EFLAGS |
| 201 | const unsigned Opcode = llvm::X86::CMOV_GR32; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 202 | const SnippetPrototype Proto = checkAndGetConfigurations(Opcode); |
| 203 | EXPECT_THAT(Proto.Explanation, HasSubstr("no tied variables")); |
| 204 | ASSERT_THAT(Proto.Snippet, SizeIs(1)); |
| 205 | const InstructionInstance &II = Proto.Snippet[0]; |
| 206 | EXPECT_THAT(II.getOpcode(), Opcode); |
| 207 | ASSERT_THAT(II.VariableValues, SizeIs(4)); |
| 208 | EXPECT_THAT(II.VariableValues[0].getReg(), Not(II.VariableValues[1].getReg())) |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 209 | << "Def is different from first Use"; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 210 | EXPECT_THAT(II.VariableValues[0].getReg(), Not(II.VariableValues[2].getReg())) |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 211 | << "Def is different from second Use"; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame^] | 212 | EXPECT_THAT(II.VariableValues[3], IsInvalid()); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | } // namespace |
| 216 | } // namespace exegesis |