diff --git a/CMakeLists.txt b/CMakeLists.txt index 54aa3a78043b..a7e829de5b3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -950,8 +950,8 @@ if(IREE_ENABLE_CLANG_TIDY) endif() if(IREE_BUILD_TRACY) - if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux") - message(WARNING "Building Tracy (IREE_BUILD_TRACY) on non-Linux is unsupported and may fail below.") + if(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin|Linux") + message(WARNING "Building Tracy (IREE_BUILD_TRACY) on non-Darwin/Linux is unsupported and may fail below.") endif() add_subdirectory(build_tools/third_party/tracy ${CMAKE_CURRENT_BINARY_DIR}/tracy) if(NOT TARGET IREETracyCaptureServer) diff --git a/build_tools/third_party/tracy/CMakeLists.txt b/build_tools/third_party/tracy/CMakeLists.txt index 3ca9023c9735..7efc9592994f 100644 --- a/build_tools/third_party/tracy/CMakeLists.txt +++ b/build_tools/third_party/tracy/CMakeLists.txt @@ -19,9 +19,13 @@ find_package(Threads REQUIRED) message(STATUS "Checking for Tracy dependencies...") find_program(PKGCONFIG pkg-config) if(NOT PKGCONFIG) - message(STATUS "Could not find pkg-config (on Ubuntu/Debian, 'apt install pkg-config')") + message(STATUS "Could not find pkg-config to get dependencies; you need to specify them manually or use pkg-config") + message(STATUS " Ubuntu/Debian: `apt install pkg-config`") + message(STATUS " MacOS: `brew install pkg-config`") else() include(FindPkgConfig) + + # Deps slightly differ by platform but some are common. pkg_check_modules(TRACY_DEPS tbb libzstd @@ -29,31 +33,41 @@ else() pkg_check_modules(TRACY_CAPSTONE_DEPS capstone ) - pkg_check_modules(TRACY_GTK_DEPS - freetype2 - glfw3 - gtk+-3.0 - dbus-1 - ) -endif() - - -if(NOT TRACY_DEPS_FOUND) - message(STATUS "Could not find Tracy dependencies (Tracy server will not be built).") - message(STATUS "To build Tracy, install packages libzstd, and tbb (on Ubuntu/Debian, 'apt install libcapstone-dev libtbb-dev libzstd-dev')") - return() -endif() - -if(NOT TRACY_CAPSTONE_DEPS_FOUND) - message(STATUS "Could not find capstone, a Tracy dependency (Tracy server will not be built).") - message(STATUS "To build Tracy, install capstone (on Ubuntu/Debian, 'apt install libcapstone-dev')") - return() -endif() - -if(NOT TRACY_GTK_DEPS_FOUND) - message(STATUS - "Could not find deps required to build graphical programs: " - "Tracy graphical profiler will not be built (on Ubuntu/Debian, 'apt install libglfw3-dev libfreetype-dev libgtk-3-dev libdbus-1-dev')") + if(APPLE) + pkg_check_modules(TRACY_GUI_DEPS + freetype2 + glfw3 + ) + else() + pkg_check_modules(TRACY_GUI_DEPS + freetype2 + glfw3 + gtk+-3.0 + dbus-1 + ) + endif() + + if(NOT TRACY_DEPS_FOUND) + message(STATUS "Could not find Tracy dependencies (Tracy server will not be built).") + message(STATUS "To build Tracy, install packages libzstd, and tbb:") + message(STATUS " Ubuntu/Debian: `apt install libcapstone-dev libtbb-dev libzstd-dev`") + message(STATUS " MacOS: `brew install capstone tbb zstd`") + return() + endif() + + if(NOT TRACY_CAPSTONE_DEPS_FOUND) + message(STATUS "Could not find capstone, a Tracy dependency (Tracy server will not be built).") + message(STATUS "To build Tracy, install capstone or build from source:") + message(STATUS " Ubuntu/Debian: `apt install libcapstone-dev`") + message(STATUS " MacOS: `brew install capstone`") + return() + endif() + + if(NOT TRACY_GUI_DEPS_FOUND) + message(STATUS "Could not find deps required to build graphical programs (Tracy graphical profiler will not be built).") + message(STATUS " Ubuntu/Debian: `apt install libglfw3-dev libfreetype-dev libgtk-3-dev libdbus-1-dev`") + message(STATUS " MacOS: `brew install glfw freetype`") + endif() endif() #------------------------------------------------------------------------------- @@ -94,20 +108,20 @@ endfunction() function(setup_graphics_deps name) target_compile_definitions(${name} - PRIVATE - DISPLAY_SERVER_X11 + PRIVATE + DISPLAY_SERVER_X11 ) target_include_directories(${name} PUBLIC - ${TRACY_GTK_DEPS_INCLUDE_DIRS} + ${TRACY_GUI_DEPS_INCLUDE_DIRS} ) target_link_libraries(${name} PRIVATE - ${TRACY_GTK_DEPS_LIBRARIES} + ${TRACY_GUI_DEPS_LIBRARIES} ) target_link_directories(${name} PRIVATE - ${TRACY_GTK_DEPS_LIBRARY_DIRS} + ${TRACY_GUI_DEPS_LIBRARY_DIRS} ) endfunction() @@ -175,7 +189,7 @@ target_link_libraries(IREETracyCSVExport # Graphical frontends #------------------------------------------------------------------------------- -if(TRACY_GTK_DEPS_FOUND) +if(TRACY_GUI_DEPS_FOUND) #----------------------------------------------------------------------------- # IMGUI library #----------------------------------------------------------------------------- @@ -188,17 +202,18 @@ if(TRACY_GTK_DEPS_FOUND) ${IMGUI_SOURCES} ) setup_cxx_options(IREETracyIMGUI) + setup_graphics_deps(IREETracyIMGUI) #----------------------------------------------------------------------------- # NFD library #----------------------------------------------------------------------------- - set(NFD_SOURCES - ${TRACY_SOURCE_DIR}/nfd/nfd_portal.cpp - ) - add_library(IREETracyNFD - ${NFD_SOURCES} - ) + if(APPLE) + set(NFD_SOURCES ${TRACY_SOURCE_DIR}/nfd/nfd_cocoa.m) + else() + set(NFD_SOURCES ${TRACY_SOURCE_DIR}/nfd/nfd_portal.cpp) + endif() + add_library(IREETracyNFD ${NFD_SOURCES}) setup_cxx_options(IREETracyNFD) setup_graphics_deps(IREETracyNFD) @@ -210,9 +225,7 @@ if(TRACY_GTK_DEPS_FOUND) # Remove the source files for Wayland backend; right now we just use GLFW. list(REMOVE_ITEM PROFILER_SRCS "${TRACY_SOURCE_DIR}/profiler/src/BackendWayland.cpp") - add_executable(IREETracyProfiler - ${PROFILER_SRCS} - ) + add_executable(IREETracyProfiler ${PROFILER_SRCS}) set_target_properties(IREETracyProfiler PROPERTIES OUTPUT_NAME "iree-tracy-profiler" @@ -227,4 +240,12 @@ if(TRACY_GTK_DEPS_FOUND) IREETracyServer ${CMAKE_THREAD_LIBS_INIT} ) + if(APPLE) + target_link_libraries(IREETracyProfiler + PRIVATE + "-framework Foundation" + "-framework AppKit" + "-framework UniformTypeIdentifiers" + ) + endif() endif() diff --git a/docs/developers/developing_iree/profiling_with_tracy.md b/docs/developers/developing_iree/profiling_with_tracy.md index 8d42b1737824..22c5e4dd8bd7 100644 --- a/docs/developers/developing_iree/profiling_with_tracy.md +++ b/docs/developers/developing_iree/profiling_with_tracy.md @@ -99,7 +99,7 @@ brew install capstone Install other dependencies: ```shell -brew install glfw freetype +brew install pkg-config glfw freetype tbb zstd ``` ## Build the Tracy tools diff --git a/runtime/src/iree/base/tracing/tracy.cc b/runtime/src/iree/base/tracing/tracy.cc index d500a5ca9fd7..722ade897b70 100644 --- a/runtime/src/iree/base/tracing/tracy.cc +++ b/runtime/src/iree/base/tracing/tracy.cc @@ -30,6 +30,24 @@ void IREEDbgHelpUnlock(void) { ReleaseMutex(iree_dbghelp_mutex); } #if IREE_TRACING_FEATURES != 0 +void iree_tracing_tracy_initialize() { + // No-op. +} + +void iree_tracing_tracy_deinitialize() { +#if defined(IREE_PLATFORM_APPLE) + // Synchronously shut down the profiler service. + // This is required on some platforms to support TRACY_NO_EXIT=1 such as + // MacOS and iOS. It should be harmless on other platforms as it returns + // quickly if TRACY_NO_EXIT=1 is not set. + // See: https://github.com/wolfpld/tracy/issues/8 + tracy::GetProfiler().RequestShutdown(); + while (!tracy::GetProfiler().HasShutdownFinished()) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } +#endif // IREE_PLATFORM_* +} + iree_zone_id_t iree_tracing_zone_begin_impl( const iree_tracing_location_t* src_loc, const char* name, size_t name_length) { diff --git a/runtime/src/iree/base/tracing/tracy.h b/runtime/src/iree/base/tracing/tracy.h index ffe410c83fd8..fcc15854a5b7 100644 --- a/runtime/src/iree/base/tracing/tracy.h +++ b/runtime/src/iree/base/tracing/tracy.h @@ -104,6 +104,9 @@ typedef struct ___tracy_source_location_data iree_tracing_location_t; (TracyCZoneCtx) { zone_id, 1 } #endif // __cplusplus +void iree_tracing_tracy_initialize(); +void iree_tracing_tracy_deinitialize(); + IREE_MUST_USE_RESULT iree_zone_id_t iree_tracing_zone_begin_impl(const iree_tracing_location_t* src_loc, const char* name, size_t name_length); @@ -185,8 +188,8 @@ void* iree_tracing_obscure_ptr(void* ptr); #define IREE_TRACE(expr) expr -#define IREE_TRACE_APP_ENTER() -#define IREE_TRACE_APP_EXIT(exit_code) +#define IREE_TRACE_APP_ENTER() iree_tracing_tracy_initialize() +#define IREE_TRACE_APP_EXIT(exit_code) iree_tracing_tracy_deinitialize() #define IREE_TRACE_SET_APP_INFO(value, value_length) \ ___tracy_emit_message_appinfo(value, value_length) #define IREE_TRACE_SET_THREAD_NAME(name) ___tracy_set_thread_name(name)