[llvm] r272599 - Add support for collating profiles for use with code coverage

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 13 16:33:48 PDT 2016


Author: vedantk
Date: Mon Jun 13 18:33:48 2016
New Revision: 272599

URL: http://llvm.org/viewvc/llvm-project?rev=272599&view=rev
Log:
Add support for collating profiles for use with code coverage

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

Added:
    llvm/trunk/utils/prepare-code-coverage-artifact.py
Modified:
    llvm/trunk/CMakeLists.txt
    llvm/trunk/cmake/modules/HandleLLVMOptions.cmake

Modified: llvm/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/CMakeLists.txt?rev=272599&r1=272598&r2=272599&view=diff
==============================================================================
--- llvm/trunk/CMakeLists.txt (original)
+++ llvm/trunk/CMakeLists.txt Mon Jun 13 18:33:48 2016
@@ -430,6 +430,21 @@ else()
   set(LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION FALSE CACHE INTERNAL "For Visual Studio 2013, manually copy natvis files to Documents\\Visual Studio 2013\\Visualizers" FORCE)
 endif()
 
+if (LLVM_BUILD_INSTRUMENTED OR LLVM_BUILD_INSTRUMENTED_COVERAGE)
+  if(NOT LLVM_PROFILE_MERGE_POOL_SIZE)
+    # A pool size of 1-2 is probably sufficient on a SSD. 3-4 should be fine
+    # for spining disks. Anything higher may only help on slower mediums.
+    set(LLVM_PROFILE_MERGE_POOL_SIZE "4")
+  endif()
+  if(NOT LLVM_PROFILE_FILE_PATTERN)
+    if(NOT LLVM_PROFILE_DATA_DIR)
+      set(LLVM_PROFILE_FILE_PATTERN "%${LLVM_PROFILE_MERGE_POOL_SIZE}m.profraw")
+    else()
+      file(TO_NATIVE_PATH "${LLVM_PROFILE_DATA_DIR}/%${LLVM_PROFILE_MERGE_POOL_SIZE}m.profraw" LLVM_PROFILE_FILE_PATTERN)
+    endif()
+  endif()
+endif()
+
 # All options referred to from HandleLLVMOptions have to be specified
 # BEFORE this include, otherwise options will not be correctly set on
 # first cmake run

Modified: llvm/trunk/cmake/modules/HandleLLVMOptions.cmake
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/cmake/modules/HandleLLVMOptions.cmake?rev=272599&r1=272598&r2=272599&view=diff
==============================================================================
--- llvm/trunk/cmake/modules/HandleLLVMOptions.cmake (original)
+++ llvm/trunk/cmake/modules/HandleLLVMOptions.cmake Mon Jun 13 18:33:48 2016
@@ -598,7 +598,15 @@ endif()
 
 option(LLVM_BUILD_INSTRUMENTED "Build LLVM and tools with PGO instrumentation (experimental)" Off)
 mark_as_advanced(LLVM_BUILD_INSTRUMENTED)
-append_if(LLVM_BUILD_INSTRUMENTED "-fprofile-instr-generate"
+append_if(LLVM_BUILD_INSTRUMENTED "-fprofile-instr-generate='${LLVM_PROFILE_FILE_PATTERN}'"
+  CMAKE_CXX_FLAGS
+  CMAKE_C_FLAGS
+  CMAKE_EXE_LINKER_FLAGS
+  CMAKE_SHARED_LINKER_FLAGS)
+
+option(LLVM_BUILD_INSTRUMENTED_COVERAGE "Build LLVM and tools with Code Coverage instrumentation (experimental)" Off)
+mark_as_advanced(LLVM_BUILD_INSTRUMENTED_COVERAGE)
+append_if(LLVM_BUILD_INSTRUMENTED_COVERAGE "-fprofile-instr-generate='${LLVM_PROFILE_FILE_PATTERN}' -fcoverage-mapping"
   CMAKE_CXX_FLAGS
   CMAKE_C_FLAGS
   CMAKE_EXE_LINKER_FLAGS

Added: llvm/trunk/utils/prepare-code-coverage-artifact.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/prepare-code-coverage-artifact.py?rev=272599&view=auto
==============================================================================
--- llvm/trunk/utils/prepare-code-coverage-artifact.py (added)
+++ llvm/trunk/utils/prepare-code-coverage-artifact.py Mon Jun 13 18:33:48 2016
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+
+'''Prepare a code coverage artifact.
+
+- Collate raw profiles into one indexed profile.
+- Delete the raw profiles.
+- Copy the coverage mappings in the binaries directory.
+'''
+
+import argparse
+import glob
+import os
+import subprocess
+import sys
+
+def merge_raw_profiles(host_llvm_profdata, profile_data_dir):
+    print ':: Merging raw profiles...',
+    sys.stdout.flush()
+    raw_profiles = glob.glob(os.path.join(profile_data_dir, '*.profraw'))
+    manifest_path = os.path.join(profile_data_dir, 'profiles.manifest')
+    profdata_path = os.path.join(profile_data_dir, 'Coverage.profdata')
+    with open(manifest_path, 'w') as manifest:
+        manifest.write('\n'.join(raw_profiles))
+    subprocess.check_call([host_llvm_profdata, 'merge', '-sparse', '-f',
+                           manifest_path, '-o', profdata_path])
+    for raw_profile in raw_profiles:
+        os.remove(raw_profile)
+    print 'Done!'
+
+def extract_covmappings(host_llvm_cov, profile_data_dir, llvm_bin_dir):
+    print ':: Extracting covmappings...',
+    sys.stdout.flush()
+    for prog in os.listdir(llvm_bin_dir):
+        if prog == 'llvm-lit':
+            continue
+        covmapping_path = os.path.join(profile_data_dir,
+                                       os.path.basename(prog) + '.covmapping')
+        subprocess.check_call([host_llvm_cov, 'convert-for-testing',
+                               os.path.join(llvm_bin_dir, prog), '-o',
+                               covmapping_path])
+    print 'Done!'
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument('host_llvm_profdata', help='Path to llvm-profdata')
+    parser.add_argument('host_llvm_cov', help='Path to llvm-cov')
+    parser.add_argument('profile_data_dir',
+                       help='Path to the directory containing the raw profiles')
+    parser.add_argument('llvm_bin_dir',
+                       help='Path to the directory containing llvm binaries')
+    args = parser.parse_args()
+
+    merge_raw_profiles(args.host_llvm_profdata, args.profile_data_dir)
+    extract_covmappings(args.host_llvm_cov, args.profile_data_dir,
+                        args.llvm_bin_dir)




More information about the llvm-commits mailing list