Skip to content

Commit

Permalink
[CMake] Support generation of linker order files using dtrace
Browse files Browse the repository at this point in the history
Summary:
This patch extends the lit-based perf-training tooling supplied for PGO data generation to also generate linker order files using dtrace.

This patch should work on any system that has dtrace. If CMake can find the dtrace tool it will generate a target 'generate-order-file' which will run the per-training tests wrapped by dtrace to capture function entries. There are several algorithms implemented for sorting the order files which can be experimented with for best performance. The dtrace wrapper also supports bot oneshot and pid probes.

The perf-helper.py changes to support order file construction are ported from internal changes by ddunbar; he gets all the credit for the hard work here, I just copy and pasted.

Note: I've tested these patches on FreeBSD and OS X 10.10.

Reviewers: ddunbar, bogner, silvas

Subscribers: llvm-commits, emaste

Differential Revision: http://reviews.llvm.org/D16134

llvm-svn: 257934
  • Loading branch information
Chris Bieneman committed Jan 15, 2016
1 parent 44b3f96 commit d8b5bde
Show file tree
Hide file tree
Showing 4 changed files with 408 additions and 22 deletions.
43 changes: 33 additions & 10 deletions clang/utils/perf-training/CMakeLists.txt
@@ -1,24 +1,24 @@
if(LLVM_BUILD_INSTRUMENTED)
if (CMAKE_CFG_INTDIR STREQUAL ".")
set(LLVM_BUILD_MODE ".")
else ()
set(LLVM_BUILD_MODE "%(build_mode)s")
endif ()
if (CMAKE_CFG_INTDIR STREQUAL ".")
set(LLVM_BUILD_MODE ".")
else ()
set(LLVM_BUILD_MODE "%(build_mode)s")
endif ()

string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} CLANG_TOOLS_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} CLANG_TOOLS_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})

if(LLVM_BUILD_INSTRUMENTED)
configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
${CMAKE_CURRENT_BINARY_DIR}/pgo-data/lit.site.cfg
)

add_lit_testsuite(generate-profraw "Generating clang PGO data"
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_BINARY_DIR}/pgo-data/
DEPENDS clang clear-profraw
)

add_custom_target(clear-profraw
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} profraw
COMMENT "Clearing old profraw data")

if(NOT LLVM_PROFDATA)
Expand All @@ -34,3 +34,26 @@ if(LLVM_BUILD_INSTRUMENTED)
COMMENT "Merging profdata"
DEPENDS generate-profraw)
endif()

find_program(DTRACE dtrace)
if(DTRACE)
configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/order-files.lit.site.cfg.in
${CMAKE_CURRENT_BINARY_DIR}/order-files/lit.site.cfg
)

add_lit_testsuite(generate-dtrace-logs "Generating clang dtrace data"
${CMAKE_CURRENT_BINARY_DIR}/order-files/
DEPENDS clang clear-dtrace-logs
)

add_custom_target(clear-dtrace-logs
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} dtrace
COMMENT "Clearing old dtrace data")


add_custom_target(generate-order-file
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py gen-order-file --binary $<TARGET_FILE:clang> --output ${CMAKE_CURRENT_BINARY_DIR}/clang.order ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating order file"
DEPENDS generate-dtrace-logs)
endif()
39 changes: 39 additions & 0 deletions clang/utils/perf-training/order-files.lit.cfg
@@ -0,0 +1,39 @@
# -*- Python -*-

from lit import Test
import lit.formats
import lit.util
import os

def getSysrootFlagsOnDarwin(config, lit_config):
# On Darwin, support relocatable SDKs by providing Clang with a
# default system root path.
if 'darwin' in config.target_triple:
try:
out = lit.util.capture(['xcrun', '--show-sdk-path']).strip()
res = 0
except OSError:
res = -1
if res == 0 and out:
sdk_path = out
lit_config.note('using SDKROOT: %r' % sdk_path)
return '-isysroot %s' % sdk_path
return ''

sysroot_flags = getSysrootFlagsOnDarwin(config, lit_config)

config.clang = os.path.realpath(lit.util.which('clang', config.clang_tools_dir)).replace('\\', '/')

config.name = 'Clang Perf Training'
config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.S', '.modulemap']

dtrace_wrapper = '%s %s/perf-helper.py dtrace' % (config.python_exe, config.test_source_root)

use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL")
config.test_format = lit.formats.ShTest(use_lit_shell == "0")
config.substitutions.append( ('%clang_cpp', ' %s %s --driver-mode=cpp %s ' % (dtrace_wrapper, config.clang, sysroot_flags)))
config.substitutions.append( ('%clang_cc1', ' %s %s -cc1 %s ' % (dtrace_wrapper, config.clang, sysroot_flags)))
config.substitutions.append( ('%clang', ' %s %s %s ' % (dtrace_wrapper, config.clang, sysroot_flags) ) )
config.substitutions.append( ('%test_root', config.test_exec_root ) )


21 changes: 21 additions & 0 deletions clang/utils/perf-training/order-files.lit.site.cfg.in
@@ -0,0 +1,21 @@
import sys

## Autogenerated by LLVM/Clang configuration.
# Do not edit!
config.clang_tools_dir = "@CLANG_TOOLS_DIR@"
config.test_exec_root = "@CMAKE_CURRENT_BINARY_DIR@"
config.test_source_root = "@CMAKE_CURRENT_SOURCE_DIR@"
config.target_triple = "@TARGET_TRIPLE@"
config.python_exe = "@PYTHON_EXECUTABLE@"

# Support substitution of the tools and libs dirs with user parameters. This is
# used when we can't determine the tool dir at configuration time.
try:
config.clang_tools_dir = config.clang_tools_dir % lit_config.params
except KeyError:
e = sys.exc_info()[1]
key, = e.args
lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key))

# Let the main config do the real work.
lit_config.load_config(config, "@CLANG_SOURCE_DIR@/utils/perf-training/order-files.lit.cfg")

0 comments on commit d8b5bde

Please sign in to comment.