Skip to content

Commit 3194928

Browse files
[llvm-exegesis] Refactor MMAP platform-specific preprocessor directives (llvm#75422)
This patch refactors the MMAP platform-specific preprocessor directives in llvm-exegesis to a single file instead of having duplicate code split across multiple files. These originally got introduced to get buildbots green again due to platform specific failures.
1 parent 46fe854 commit 3194928

File tree

4 files changed

+35
-37
lines changed

4 files changed

+35
-37
lines changed

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "BenchmarkRunner.h"
1414
#include "Error.h"
1515
#include "MCInstrDescView.h"
16+
#include "MmapUtils.h"
1617
#include "PerfHelper.h"
1718
#include "SubprocessMemory.h"
1819
#include "Target.h"
@@ -45,13 +46,6 @@
4546
#define GLIBC_INITS_RSEQ
4647
#endif
4748
#endif
48-
49-
// Before kernel 4.17, Linux did not support MAP_FIXED_NOREPLACE, so if it is
50-
// not available, simplfy define it as MAP_FIXED which performs the same
51-
// function but does not guarantee existing mappings won't get clobbered.
52-
#ifndef MAP_FIXED_NOREPLACE
53-
#define MAP_FIXED_NOREPLACE MAP_FIXED
54-
#endif
5549
#endif // __linux__
5650

5751
namespace llvm {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===-- MmapUtils.h ---------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file contains compatibility-related preprocessor directives related
10+
// to mmap.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifdef __linux__
15+
#include <sys/mman.h>
16+
#include <sys/syscall.h>
17+
18+
// Before kernel 4.17, Linux did not support MAP_FIXED_NOREPLACE, so if it is
19+
// not available, simplfy define it as MAP_FIXED which performs the same
20+
// function but does not guarantee existing mappings won't get clobbered.
21+
#ifndef MAP_FIXED_NOREPLACE
22+
#define MAP_FIXED_NOREPLACE MAP_FIXED
23+
#endif
24+
25+
// Some 32-bit architectures don't have mmap and define mmap2 instead. The only
26+
// difference between the two syscalls is that mmap2's offset parameter is in
27+
// terms 4096 byte offsets rather than individual bytes, so for our purposes
28+
// they are effectively the same as all ofsets here are set to 0.
29+
#if defined(SYS_mmap2) && !defined(SYS_mmap)
30+
#define SYS_mmap SYS_mmap2
31+
#endif
32+
#endif // __linux__

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../Target.h"
99

1010
#include "../Error.h"
11+
#include "../MmapUtils.h"
1112
#include "../ParallelSnippetGenerator.h"
1213
#include "../SerialSnippetGenerator.h"
1314
#include "../SnippetGenerator.h"
@@ -1080,21 +1081,6 @@ ExegesisX86Target::generateExitSyscall(unsigned ExitCode) const {
10801081
return ExitCallCode;
10811082
}
10821083

1083-
// Before kernel 4.17, Linux did not support MAP_FIXED_NOREPLACE, so if it is
1084-
// not available, simplfy define it as MAP_FIXED which performs the same
1085-
// function but does not guarantee existing mappings won't get clobbered.
1086-
#ifndef MAP_FIXED_NOREPLACE
1087-
#define MAP_FIXED_NOREPLACE MAP_FIXED
1088-
#endif
1089-
1090-
// Some 32-bit architectures don't have mmap and define mmap2 instead. The only
1091-
// difference between the two syscalls is that mmap2's offset parameter is in
1092-
// terms 4096 byte offsets rather than individual bytes, so for our purposes
1093-
// they are effectively the same as all ofsets here are set to 0.
1094-
#if defined(SYS_mmap2) && !defined(SYS_mmap)
1095-
#define SYS_mmap SYS_mmap2
1096-
#endif
1097-
10981084
std::vector<MCInst>
10991085
ExegesisX86Target::generateMmap(intptr_t Address, size_t Length,
11001086
intptr_t FileDescriptorAddress) const {

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <memory>
1313

1414
#include "MCTargetDesc/X86MCTargetDesc.h"
15+
#include "MmapUtils.h"
1516
#include "SubprocessMemory.h"
1617
#include "llvm/MC/TargetRegistry.h"
1718
#include "llvm/Support/TargetSelect.h"
@@ -625,21 +626,6 @@ TEST_F(X86Core2TargetTest, GenerateExitSyscallTest) {
625626
OpcodeIs(X86::SYSCALL)));
626627
}
627628

628-
// Before kernel 4.17, Linux did not support MAP_FIXED_NOREPLACE, so if it is
629-
// not available, simplfy define it as MAP_FIXED which performs the same
630-
// function but does not guarantee existing mappings won't get clobbered.
631-
#ifndef MAP_FIXED_NOREPLACE
632-
#define MAP_FIXED_NOREPLACE MAP_FIXED
633-
#endif
634-
635-
// Some 32-bit architectures don't have mmap and define mmap2 instead. The only
636-
// difference between the two syscalls is that mmap2's offset parameter is in
637-
// terms 4096 byte offsets rather than individual bytes, so for our purposes
638-
// they are effectively the same as all ofsets here are set to 0.
639-
#if defined(SYS_mmap2) && !defined(SYS_mmap)
640-
#define SYS_mmap SYS_mmap2
641-
#endif
642-
643629
TEST_F(X86Core2TargetTest, GenerateMmapTest) {
644630
EXPECT_THAT(State.getExegesisTarget().generateMmap(0x1000, 4096, 0x2000),
645631
ElementsAre(IsMovImmediate(X86::MOV64ri, X86::RDI, 0x1000),

0 commit comments

Comments
 (0)