Skip to content

IRGen, benchmarks: add an option -align-module-to-page-size for benchmarking #18318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ message("-- SWIFT_LIBRARY_PATH = ${SWIFT_LIBRARY_PATH}")
message("-- CLANG_EXEC = ${CLANG_EXEC}")
message("-- SWIFT_OPTIMIZATION_LEVELS = ${SWIFT_OPTIMIZATION_LEVELS}")
message("-- ONLY_PLATFORMS = ${ONLY_PLATFORMS}")
message("-- PAGE_ALIGNMENT_OPTION = ${PAGE_ALIGNMENT_OPTION}")

message("-- found platforms: ${platforms}")
message("-- found sdks:")
Expand Down
15 changes: 14 additions & 1 deletion benchmark/cmake/modules/AddSwiftBenchmarkSuite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ macro(configure_build)
message(FATAL_ERROR "Unsupported platform?!")
endif()

set(PAGE_ALIGNMENT_OPTION "-Xllvm" "-align-module-to-page-size")
execute_process(
COMMAND "touch" "empty.swift"
RESULT_VARIABLE result
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND "${SWIFT_EXEC}" ${PAGE_ALIGNMENT_OPTION} "empty.swift"
RESULT_VARIABLE result
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT "${result}" MATCHES "0")
set(PAGE_ALIGNMENT_OPTION "")
endif()

# We always infer the SWIFT_LIBRARY_PATH from SWIFT_EXEC unless
# SWIFT_LIBRARY_PATH is specified explicitly.
if(NOT SWIFT_LIBRARY_PATH)
Expand Down Expand Up @@ -293,7 +306,7 @@ function (swift_benchmark_compile_archopts)
set(common_options
"-c"
"-target" "${target}"
"-${BENCH_COMPILE_ARCHOPTS_OPT}")
"-${BENCH_COMPILE_ARCHOPTS_OPT}" ${PAGE_ALIGNMENT_OPTION})

if (is_darwin)
list(APPEND common_options
Expand Down
29 changes: 29 additions & 0 deletions lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@

#include <thread>

#if HAVE_UNISTD_H
#include <unistd.h>
#endif

using namespace swift;
using namespace irgen;
using namespace llvm;
Expand All @@ -82,6 +86,14 @@ static cl::opt<bool> DisableObjCARCContract(
"disable-objc-arc-contract", cl::Hidden,
cl::desc("Disable running objc arc contract for testing purposes"));

// This option is for performance benchmarking: to ensure a consistent
// performance data, modules are aligned to the page size.
// Warning: this blows up the text segment size. So use this option only for
// performance benchmarking.
static cl::opt<bool> AlignModuleToPageSize(
"align-module-to-page-size", cl::Hidden,
cl::desc("Align the text section of all LLVM modules to the page size"));

namespace {
// We need this to access IRGenOptions from extension functions
class PassManagerBuilderWrapper : public PassManagerBuilder {
Expand Down Expand Up @@ -274,6 +286,23 @@ void swift::performLLVMOptimizations(IRGenOptions &Opts, llvm::Module *Module,

// Do it.
ModulePasses.run(*Module);

if (AlignModuleToPageSize) {
// For performance benchmarking: Align the module to the page size by
// aligning the first function of the module.
unsigned pageSize =
#if HAVE_UNISTD_H
sysconf(_SC_PAGESIZE));
#else
4096; // Use a default value
#endif
for (auto I = Module->begin(), E = Module->end(); I != E; ++I) {
if (!I->isDeclaration()) {
I->setAlignment(pageSize);
break;
}
}
}
}

namespace {
Expand Down