Skip to content

Commit f1963fd

Browse files
Reland "[llvm-exegesis] Add in snippet address annotation (#74218)"
This reverts commit 30d7001. This relands commit 3ab41f9. When I was updating the patch to use llvm::to_integer, I only ran the lit tests and didn't run the unit tests, one of which started to fail. This patch fixes the broken unit test.
1 parent 04ce9a3 commit f1963fd

File tree

8 files changed

+76
-5
lines changed

8 files changed

+76
-5
lines changed

llvm/test/tools/llvm-exegesis/X86/latency/memory-annotations-unsupported.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# RUN: not llvm-exegesis -mtriple=x86_64-unknown-unknown -snippets-file=%s -mode=latency 2>&1 | FileCheck %s
44

5-
# CHECK: llvm-exegesis error: Memory annotations are only supported in subprocess execution mode
5+
# CHECK: llvm-exegesis error: Memory and snippet address annotations are only supported in subprocess execution mode
66

77
# LLVM-EXEGESIS-MEM-DEF test1 4096 ff
88

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# REQUIRES: exegesis-can-measure-latency, x86_64-linux
2+
3+
# RUN: not llvm-exegesis -mtriple=x86_64-unknown-unknown -snippets-file=%s -mode=latency 2>&1 | FileCheck %s
4+
5+
# CHECK: llvm-exegesis error: Memory and snippet address annotations are only supported in subprocess execution mode
6+
7+
# LLVM-EXEGESIS-SNIPPET-ADDRESS 10000
8+
9+
movq $0, %rax
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# REQUIRES: exegesis-can-measure-latency, x86_64-linux
2+
3+
# RUN: llvm-exegesis -mtriple=x86_64-unknown-unknown -mode=latency -snippets-file=%s -execution-mode=subprocess | FileCheck %s
4+
5+
# Check that the code is loaded in at the expected address.
6+
7+
# LLVM-EXEGESIS-SNIPPET-ADDRESS 20000
8+
# LLVM-EXEGESIS-DEFREG RAX 0
9+
# LLVM-EXEGESIS-DEFREG R14 127
10+
# LLVM-EXEGESIS-DEFREG R15 0
11+
# LLVM-EXEGESIS-DEFREG RDI 0
12+
13+
# Load the instruction pointer and round down to the nearest page as there
14+
# will be some setup code loaded in before this part begins to execute.
15+
lea 0(%rip), %rax
16+
shrq $12, %rax
17+
shlq $12, %rax
18+
19+
cmpq $0x20000, %rax
20+
cmovneq %r14, %r15
21+
22+
movq $60, %rax
23+
movq %r15, %rdi
24+
syscall
25+
26+
# CHECK-NOT: error: 'Child benchmarking process exited with non-zero exit code: Child process returned with unknown exit code'

llvm/tools/llvm-exegesis/lib/BenchmarkResult.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ struct BenchmarkKey {
7070
// An opaque configuration, that can be used to separate several benchmarks of
7171
// the same instruction under different configurations.
7272
std::string Config;
73+
// The address that the snippet should be loaded in at if the execution mode
74+
// being used supports it.
75+
intptr_t SnippetAddress = 0;
7376
};
7477

7578
struct BenchmarkMeasure {

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,17 @@ class SubProcessFunctionExecutorImpl
416416
#endif // GLIBC_INITS_RSEQ
417417

418418
size_t FunctionDataCopySize = this->Function.FunctionBytes.size();
419+
void *MapAddress = NULL;
420+
int MapFlags = MAP_PRIVATE | MAP_ANONYMOUS;
421+
422+
if (Key.SnippetAddress != 0) {
423+
MapAddress = reinterpret_cast<void *>(Key.SnippetAddress);
424+
MapFlags |= MAP_FIXED_NOREPLACE;
425+
}
426+
419427
char *FunctionDataCopy =
420-
(char *)mmap(NULL, FunctionDataCopySize, PROT_READ | PROT_WRITE,
421-
MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
428+
(char *)mmap(MapAddress, FunctionDataCopySize, PROT_READ | PROT_WRITE,
429+
MapFlags, 0, 0);
422430
if ((intptr_t)FunctionDataCopy == -1)
423431
exit(ChildProcessExitCodeE::FunctionDataMappingFailed);
424432

llvm/tools/llvm-exegesis/lib/SnippetFile.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ class BenchmarkCodeStreamer : public MCStreamer, public AsmCommentConsumer {
131131
Result->Key.MemoryMappings.push_back(std::move(MemMap));
132132
return;
133133
}
134+
if (CommentText.consume_front("SNIPPET-ADDRESS")) {
135+
// LLVM-EXEGESIS-SNIPPET-ADDRESS <address>
136+
if (!to_integer<intptr_t>(CommentText.trim(), Result->Key.SnippetAddress,
137+
16)) {
138+
errs() << "invalid comment 'LLVM-EXEGESIS-SNIPPET-ADDRESS "
139+
<< CommentText
140+
<< "', expected <ADDRESS> to contain a valid integer in "
141+
"hexadecimal format";
142+
++InvalidComments;
143+
return;
144+
}
145+
return;
146+
}
134147
}
135148

136149
unsigned numInvalidComments() const { return InvalidComments; }

llvm/tools/llvm-exegesis/llvm-exegesis.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,10 @@ void benchmarkMain() {
533533
for (const auto &Configuration : Configurations) {
534534
if (ExecutionMode != BenchmarkRunner::ExecutionModeE::SubProcess &&
535535
(Configuration.Key.MemoryMappings.size() != 0 ||
536-
Configuration.Key.MemoryValues.size() != 0))
537-
ExitWithError("Memory annotations are only supported in subprocess "
536+
Configuration.Key.MemoryValues.size() != 0 ||
537+
Configuration.Key.SnippetAddress != 0))
538+
ExitWithError("Memory and snippet address annotations are only "
539+
"supported in subprocess "
538540
"execution mode");
539541
}
540542
}

llvm/unittests/tools/llvm-exegesis/X86/SnippetFileTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,16 @@ TEST_F(X86SnippetFileTest, MemoryMappingNoDefinition) {
209209
consumeError(std::move(Error));
210210
}
211211

212+
TEST_F(X86SnippetFileTest, SnippetAddress) {
213+
auto Snippets = TestCommon(R"(
214+
# LLVM-EXEGESIS-SNIPPET-ADDRESS 10000
215+
)");
216+
ASSERT_TRUE(static_cast<bool>(Snippets));
217+
EXPECT_THAT(*Snippets, SizeIs(1));
218+
const auto &Snippet = (*Snippets)[0];
219+
EXPECT_EQ(Snippet.Key.SnippetAddress, 0x10000);
220+
}
221+
212222
} // namespace
213223
} // namespace exegesis
214224
} // namespace llvm

0 commit comments

Comments
 (0)