Skip to content

Commit

Permalink
Merge pull request swiftlang#14157 from mikeash/conditionally-disable…
Browse files Browse the repository at this point in the history
…-runtimefunctioncounters

[Runtime] Remove RuntimeFunctionCounters in no-assert builds.
  • Loading branch information
mikeash authored Jan 29, 2018
2 parents ce0c40e + 6f37cca commit 5e2b11d
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 289 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ option(SWIFT_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
"Build the standard libraries, overlays, and runtime with normal arguments at +0"
FALSE)

option(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS
"Enable runtime function counters and expose the API."
FALSE)

#
# End of user-configurable options.
#
Expand Down
8 changes: 8 additions & 0 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ function(_add_variant_c_compile_flags)
else()
list(APPEND result "-DNDEBUG")
endif()

if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)
list(APPEND result "-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS")
endif()

if(CFLAGS_ANALYZE_CODE_COVERAGE)
list(APPEND result "-fprofile-instr-generate"
Expand Down Expand Up @@ -316,6 +320,10 @@ function(_add_variant_swift_compile_flags
if (SWIFT_ENABLE_GUARANTEED_NORMAL_ARGUMENTS)
list(APPEND result "-Xfrontend" "-enable-guaranteed-normal-arguments")
endif()

if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)
list(APPEND result "-D" "SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS")
endif()

set("${result_var_name}" "${result}" PARENT_SCOPE)
endfunction()
Expand Down
8 changes: 8 additions & 0 deletions stdlib/public/core/RuntimeFunctionCounters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
// number of invocations, or per-object counters, which represent the
// number of runtime functions calls for a specific object.

// By default, this feature is enabled only when assertions are enabled. To control it
// separately, set the SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS environment variable when
// invoking build-script:
// SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS=TRUE ./utils/build-script ...
#if SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS

/// Collect all references inside the object using Mirrors.
/// - Parameter value: the value to be inspected
/// - Parameter references: the array which should contain the collected
Expand Down Expand Up @@ -541,3 +547,5 @@ func _measureRuntimeFunctionCountersDiffs(
mode: savedMode)
return (globalCountersBefore.diff(globalCountersAfter), objectsCountersDiff)
}

#endif
7 changes: 3 additions & 4 deletions stdlib/public/runtime/RuntimeInvocationsTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Mutex.h"

// This file is compiled always, even if assertions are disabled and no runtime
// functions are being tracked. This is done to avoid recompiling Swift clients
// using these APIs. They should be able to link against the standard library
// independent of the fact whether assertions are enabled or not.
#if defined(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)

#define SWIFT_RT_FUNCTION_INVOCATION_COUNTER_NAME(RT_FUNCTION) \
invocationCounter_##RT_FUNCTION
Expand Down Expand Up @@ -238,3 +235,5 @@ _swift_setGlobalRuntimeFunctionCountersUpdateHandler(
/// TODO: Provide an API to remove any counters releated to a specific object
/// or all objects.
/// This is useful if you want to reset the stats for some/all objects.

#endif
22 changes: 11 additions & 11 deletions stdlib/public/runtime/RuntimeInvocationsTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

#include "swift/Runtime/Config.h"

/// This API is only enabled if this define is set.
#if defined(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)

#if defined(__cplusplus)

namespace swift {
Expand All @@ -39,10 +42,6 @@ struct RuntimeFunctionCountersState;
#define SWIFT_RT_TRACK_INVOCATION_NAME(RT_FUNCTION) \
swift_trackRuntimeInvocation_##RT_FUNCTION

/// Instrument the runtime functions only if we are building with
/// assertions enabled.
#if !defined(NDEBUG)

/// Invoke a helper function for tracking the calls of a runtime function.
#define SWIFT_RT_TRACK_INVOCATION(OBJ, RT_FUNCTION) \
SWIFT_RT_TRACK_INVOCATION_NAME(RT_FUNCTION)(OBJ)
Expand All @@ -52,13 +51,6 @@ struct RuntimeFunctionCountersState;
/// Declarations of external functions for invocations tracking.
#include "RuntimeInvocationsTracking.def"

#else

/// It is just a NOP if assertions are not enabled.
#define SWIFT_RT_TRACK_INVOCATION(OBJ, RT_FUNCTION)

#endif // NDEBUG

/// This type defines a callback to be called on any intercepted runtime
/// function.
using RuntimeFunctionCountersUpdateHandler =
Expand Down Expand Up @@ -118,4 +110,12 @@ SWIFT_RUNTIME_EXPORT RuntimeFunctionCountersUpdateHandler
_swift_setGlobalRuntimeFunctionCountersUpdateHandler(
RuntimeFunctionCountersUpdateHandler handler);

#else

/// Let runtime functions unconditionally use this define by making it a NOP if
/// counters are not enabled.
#define SWIFT_RT_TRACK_INVOCATION(OBJ, RT_FUNCTION)

#endif // SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS

#endif
3 changes: 3 additions & 0 deletions test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ if "@SWIFT_STDLIB_USE_NONATOMIC_RC@" == "TRUE":
if "@SWIFT_HAVE_WORKING_STD_REGEX@" == "FALSE":
config.available_features.add('broken_std_regex')

if "@SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS@" == "TRUE":
config.available_features.add('runtime_function_counters')

if "@CMAKE_GENERATOR@" == "Xcode":
xcode_bin_dir = os.path.join(config.llvm_obj_root, "@LLVM_BUILD_TYPE@",
'bin')
Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/test_runtime_function_counters.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/test_runtime_function_counters
// RUN: %target-run %t/test_runtime_function_counters 2>&1 | %FileCheck %s
// REQUIRES: swift_stdlib_asserts
// REQUIRES: runtime_function_counters
// REQUIRES: executable_test

/// Test functionality related to the runtime function counters.
Expand Down
Loading

0 comments on commit 5e2b11d

Please sign in to comment.