Skip to content

Commit e0d9661

Browse files
authored
Merge pull request #18318 from eeckstein/add-bm-option
IRGen, benchmarks: add an option -align-module-to-page-size for benchmarking
2 parents 95652f3 + 1a161c2 commit e0d9661

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ message("-- SWIFT_LIBRARY_PATH = ${SWIFT_LIBRARY_PATH}")
272272
message("-- CLANG_EXEC = ${CLANG_EXEC}")
273273
message("-- SWIFT_OPTIMIZATION_LEVELS = ${SWIFT_OPTIMIZATION_LEVELS}")
274274
message("-- ONLY_PLATFORMS = ${ONLY_PLATFORMS}")
275+
message("-- PAGE_ALIGNMENT_OPTION = ${PAGE_ALIGNMENT_OPTION}")
275276

276277
message("-- found platforms: ${platforms}")
277278
message("-- found sdks:")

benchmark/cmake/modules/AddSwiftBenchmarkSuite.cmake

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ macro(configure_build)
4040
message(FATAL_ERROR "Unsupported platform?!")
4141
endif()
4242

43+
set(PAGE_ALIGNMENT_OPTION "-Xllvm" "-align-module-to-page-size")
44+
execute_process(
45+
COMMAND "touch" "empty.swift"
46+
RESULT_VARIABLE result
47+
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
48+
execute_process(
49+
COMMAND "${SWIFT_EXEC}" ${PAGE_ALIGNMENT_OPTION} "empty.swift"
50+
RESULT_VARIABLE result
51+
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
52+
if(NOT "${result}" MATCHES "0")
53+
set(PAGE_ALIGNMENT_OPTION "")
54+
endif()
55+
4356
# We always infer the SWIFT_LIBRARY_PATH from SWIFT_EXEC unless
4457
# SWIFT_LIBRARY_PATH is specified explicitly.
4558
if(NOT SWIFT_LIBRARY_PATH)
@@ -293,7 +306,7 @@ function (swift_benchmark_compile_archopts)
293306
set(common_options
294307
"-c"
295308
"-target" "${target}"
296-
"-${BENCH_COMPILE_ARCHOPTS_OPT}")
309+
"-${BENCH_COMPILE_ARCHOPTS_OPT}" ${PAGE_ALIGNMENT_OPTION})
297310

298311
if (is_darwin)
299312
list(APPEND common_options

lib/IRGen/IRGen.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474

7575
#include <thread>
7676

77+
#if HAVE_UNISTD_H
78+
#include <unistd.h>
79+
#endif
80+
7781
using namespace swift;
7882
using namespace irgen;
7983
using namespace llvm;
@@ -82,6 +86,14 @@ static cl::opt<bool> DisableObjCARCContract(
8286
"disable-objc-arc-contract", cl::Hidden,
8387
cl::desc("Disable running objc arc contract for testing purposes"));
8488

89+
// This option is for performance benchmarking: to ensure a consistent
90+
// performance data, modules are aligned to the page size.
91+
// Warning: this blows up the text segment size. So use this option only for
92+
// performance benchmarking.
93+
static cl::opt<bool> AlignModuleToPageSize(
94+
"align-module-to-page-size", cl::Hidden,
95+
cl::desc("Align the text section of all LLVM modules to the page size"));
96+
8597
namespace {
8698
// We need this to access IRGenOptions from extension functions
8799
class PassManagerBuilderWrapper : public PassManagerBuilder {
@@ -274,6 +286,23 @@ void swift::performLLVMOptimizations(IRGenOptions &Opts, llvm::Module *Module,
274286

275287
// Do it.
276288
ModulePasses.run(*Module);
289+
290+
if (AlignModuleToPageSize) {
291+
// For performance benchmarking: Align the module to the page size by
292+
// aligning the first function of the module.
293+
unsigned pageSize =
294+
#if HAVE_UNISTD_H
295+
sysconf(_SC_PAGESIZE));
296+
#else
297+
4096; // Use a default value
298+
#endif
299+
for (auto I = Module->begin(), E = Module->end(); I != E; ++I) {
300+
if (!I->isDeclaration()) {
301+
I->setAlignment(pageSize);
302+
break;
303+
}
304+
}
305+
}
277306
}
278307

279308
namespace {

0 commit comments

Comments
 (0)