diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -663,6 +663,11 @@ if (MSVC_IDE OR XCODE) set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar") endif() + +if(LLVM_TEST_COVERAGE) + set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --emit-coverage") +endif() + set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit") # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools. @@ -784,24 +789,27 @@ option(LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION "Configure project to use Visual Studio native visualizers" TRUE) endif() -if (LLVM_BUILD_INSTRUMENTED OR LLVM_BUILD_INSTRUMENTED_COVERAGE OR - LLVM_ENABLE_IR_PGO) - 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) - file(TO_NATIVE_PATH "${LLVM_BINARY_DIR}/profiles" LLVM_PROFILE_DATA_DIR) +if(NOT LLVM_TEST_COVERAGE) + if(LLVM_BUILD_INSTRUMENTED OR LLVM_BUILD_INSTRUMENTED_COVERAGE OR LLVM_ENABLE_IR_PGO) + if(NOT LLVM_PROFILE_MERGE_POOL_SIZE) + # A pool size of 1-2 is probably sufficient on an SSD. 3-4 should be fine + # for spinning disks. Anything higher may only help on slower mediums. + set(LLVM_PROFILE_MERGE_POOL_SIZE "4") endif() - file(TO_NATIVE_PATH "${LLVM_PROFILE_DATA_DIR}/%${LLVM_PROFILE_MERGE_POOL_SIZE}m.profraw" LLVM_PROFILE_FILE_PATTERN) - endif() - if(NOT LLVM_CSPROFILE_FILE_PATTERN) - if(NOT LLVM_CSPROFILE_DATA_DIR) - file(TO_NATIVE_PATH "${LLVM_BINARY_DIR}/csprofiles" LLVM_CSPROFILE_DATA_DIR) + + if(NOT LLVM_PROFILE_FILE_PATTERN) + if(NOT LLVM_PROFILE_DATA_DIR) + file(TO_NATIVE_PATH "${LLVM_BINARY_DIR}/profiles" LLVM_PROFILE_DATA_DIR) + endif() + file(TO_NATIVE_PATH "${LLVM_PROFILE_DATA_DIR}/%${LLVM_PROFILE_MERGE_POOL_SIZE}m.profraw" LLVM_PROFILE_FILE_PATTERN) + endif() + + if(NOT LLVM_CSPROFILE_FILE_PATTERN) + if(NOT LLVM_CSPROFILE_DATA_DIR) + file(TO_NATIVE_PATH "${LLVM_BINARY_DIR}/csprofiles" LLVM_CSPROFILE_DATA_DIR) + endif() + file(TO_NATIVE_PATH "${LLVM_CSPROFILE_DATA_DIR}/%${LLVM_PROFILE_MERGE_POOL_SIZE}m.profraw" LLVM_CSPROFILE_FILE_PATTERN) endif() - file(TO_NATIVE_PATH "${LLVM_CSPROFILE_DATA_DIR}/%${LLVM_PROFILE_MERGE_POOL_SIZE}m.profraw" LLVM_CSPROFILE_FILE_PATTERN) endif() endif() diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/llvm/cmake/modules/HandleLLVMOptions.cmake @@ -1150,6 +1150,7 @@ endif() option(LLVM_BUILD_INSTRUMENTED_COVERAGE "Build LLVM and tools with Code Coverage instrumentation" Off) +option(LLVM_TEST_COVERAGE "Enable patch based test case coverage." 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 diff --git a/llvm/docs/CMake.rst b/llvm/docs/CMake.rst --- a/llvm/docs/CMake.rst +++ b/llvm/docs/CMake.rst @@ -375,6 +375,12 @@ will limit code coverage summaries to just the listed directories. If unset, coverage reports will include all sources identified by the tooling. +**LLVM_TEST_COVERAGE**:BOOL + Enable individual test case coverage. When set to ON, code coverage data + for each test case will be generated and written to a separate directory + in the build directory. This allows you to analyze the code coverage of + each test case individually. Defaults to OFF + **LLVM_BUILD_LLVM_DYLIB**:BOOL If enabled, the target for building the libLLVM shared library is added. This library contains all of LLVM's components in a single shared library. diff --git a/llvm/docs/CommandGuide/lit.rst b/llvm/docs/CommandGuide/lit.rst --- a/llvm/docs/CommandGuide/lit.rst +++ b/llvm/docs/CommandGuide/lit.rst @@ -186,6 +186,12 @@ Run the tests in a random order, not failing/slowest first. Deprecated, use :option:`--order` instead. +.. option:: --emit-coverage + + Emit the necessary test coverage data, divided per test case (involves + setting a unique value to LLVM_PROFILE_FILE for each RUN). The coverage + data files will be located in the /test/ directory. + .. option:: --max-failures N Stop execution after the given number ``N`` of failures. diff --git a/llvm/utils/lit/lit/LitConfig.py b/llvm/utils/lit/lit/LitConfig.py --- a/llvm/utils/lit/lit/LitConfig.py +++ b/llvm/utils/lit/lit/LitConfig.py @@ -37,6 +37,7 @@ maxIndividualTestTime=0, parallelism_groups={}, echo_all_commands=False, + emit_coverage=False, ): # The name of the test runner. self.progname = progname @@ -87,6 +88,7 @@ self.maxIndividualTestTime = maxIndividualTestTime self.parallelism_groups = parallelism_groups self.echo_all_commands = echo_all_commands + self.emit_coverage = emit_coverage @property def maxIndividualTestTime(self): diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py --- a/llvm/utils/lit/lit/TestRunner.py +++ b/llvm/utils/lit/lit/TestRunner.py @@ -1125,7 +1125,25 @@ out, err, exitCode = lit.util.executeCommand( command, cwd=cwd, - env=test.config.environment, + timeout=litConfig.maxIndividualTestTime, + ) + + # Check if the --emit-coverage option is enabled + if litConfig.emit_coverage: + # Extract the test case name from the test object + test_case_name = test.path_in_suite[-1] + test_case_name = test_case_name.rsplit(".", 1)[0] # Remove the file extension + + # Set the LLVM_PROFILE_FILE environment variable + llvm_profile_file = test_case_name + ".profraw" + env = {"LLVM_PROFILE_FILE": llvm_profile_file} + else: + env = test.config.environment + + out, err, exitCode = lit.util.executeCommand( + command, + cwd=cwd, + env=env, timeout=litConfig.maxIndividualTestTime, ) return (out, err, exitCode, None) diff --git a/llvm/utils/lit/lit/cl_arguments.py b/llvm/utils/lit/lit/cl_arguments.py --- a/llvm/utils/lit/lit/cl_arguments.py +++ b/llvm/utils/lit/lit/cl_arguments.py @@ -184,6 +184,12 @@ help="Do not fail the run if all tests are filtered out", action="store_true", ) + execution_group.add_argument( + "--emit-coverage", + dest="emit_coverage", + action="store_true", + help="Enable individual test case coverage", + ) execution_group.add_argument( "--ignore-fail", dest="ignoreFail", diff --git a/llvm/utils/lit/lit/main.py b/llvm/utils/lit/lit/main.py --- a/llvm/utils/lit/lit/main.py +++ b/llvm/utils/lit/lit/main.py @@ -41,6 +41,7 @@ params=params, config_prefix=opts.configPrefix, echo_all_commands=opts.echoAllCommands, + emit_coverage=opts.emit_coverage, ) discovered_tests = lit.discovery.find_tests_for_inputs(