From 2608c6bbc9e1f830c5f6fea1e953055d7f5049ed Mon Sep 17 00:00:00 2001 From: "Igor S. Gerasimov" Date: Sat, 11 Jan 2025 16:15:33 +0100 Subject: [PATCH 1/5] Use whitespaces only --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0fa6ceedb..5b53a90b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,9 +8,9 @@ project(Tracy LANGUAGES CXX VERSION ${TRACY_VERSION_STRING}) file(GENERATE OUTPUT .gitignore CONTENT "*") if(${BUILD_SHARED_LIBS}) - set(DEFAULT_STATIC OFF) + set(DEFAULT_STATIC OFF) else() - set(DEFAULT_STATIC ON) + set(DEFAULT_STATIC ON) endif() option(TRACY_STATIC "Whether to build Tracy as a static library" ${DEFAULT_STATIC}) @@ -26,9 +26,9 @@ find_package(Threads REQUIRED) set(TRACY_PUBLIC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/public) if(TRACY_STATIC) - set(TRACY_VISIBILITY "STATIC") + set(TRACY_VISIBILITY "STATIC") else() - set(TRACY_VISIBILITY "SHARED") + set(TRACY_VISIBILITY "SHARED") endif() add_library(TracyClient ${TRACY_VISIBILITY} "${TRACY_PUBLIC_DIR}/TracyClient.cpp") From 7c48baf1323d4df970eab5b775da29c77df01b54 Mon Sep 17 00:00:00 2001 From: "Igor S. Gerasimov" Date: Sat, 11 Jan 2025 16:33:01 +0100 Subject: [PATCH 2/5] Add Link-Time optimization option --- CMakeLists.txt | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b53a90b5..b6d077820 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,17 +15,30 @@ endif() option(TRACY_STATIC "Whether to build Tracy as a static library" ${DEFAULT_STATIC}) option(TRACY_Fortran "Build Fortran bindings" OFF) +option(TRACY_LTO "Enable Link-Time optimization" OFF) if(TRACY_Fortran) enable_language(Fortran) set(CMAKE_Fortran_VERSION 2003) endif() +if(TRACY_LTO) + include(CheckIPOSupported) + check_ipo_supported(RESULT LTO_SUPPORTED) + if(NOT LTO_SUPPORTED) + message(WARNING "LTO is not supported!") + endif() +else() + set(LTO_SUPPORTED OFF) +endif() + find_package(Threads REQUIRED) set(TRACY_PUBLIC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/public) -if(TRACY_STATIC) +if(LTO_SUPPORTED) + set(TRACY_VISIBILITY "OBJECT") +elseif(TRACY_STATIC) set(TRACY_VISIBILITY "STATIC") else() set(TRACY_VISIBILITY "SHARED") @@ -33,6 +46,7 @@ endif() add_library(TracyClient ${TRACY_VISIBILITY} "${TRACY_PUBLIC_DIR}/TracyClient.cpp") target_compile_features(TracyClient PUBLIC cxx_std_11) +set_target_properties(TracyClient PROPERTIES INTERPROCEDURAL_OPTIMIZATION ${LTO_SUPPORTED}) target_include_directories(TracyClient SYSTEM PUBLIC $ $) @@ -53,7 +67,8 @@ if(TRACY_Fortran) PUBLIC TracyClient ) - set_target_properties(TracyClientF90 PROPERTIES Fortran_MODULE_DIRECTORY ${PROJECT_BINARY_DIR}) + set_target_properties(TracyClientF90 PROPERTIES Fortran_MODULE_DIRECTORY ${PROJECT_BINARY_DIR} + INTERPROCEDURAL_OPTIMIZATION ${LTO_SUPPORTED}) endif() # Public dependency on some libraries required when using Mingw From 9d03627a17496c6258d12bb6318a685a3e383f62 Mon Sep 17 00:00:00 2001 From: "Igor S. Gerasimov" Date: Sat, 11 Jan 2025 19:05:48 +0100 Subject: [PATCH 3/5] Add info about LTO usage for Fortran; C++ projects can be linked as before --- manual/tracy.tex | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/manual/tracy.tex b/manual/tracy.tex index 185a21e09..58f1551e3 100644 --- a/manual/tracy.tex +++ b/manual/tracy.tex @@ -441,7 +441,7 @@ \subsubsection{CMake integration} \begin{lstlisting} # set options before add_subdirectory -# available options: TRACY_ENABLE, TRACY_ON_DEMAND, TRACY_NO_BROADCAST, TRACY_NO_CODE_TRANSFER, ... +# available options: TRACY_ENABLE, TRACY_LTO, TRACY_ON_DEMAND, TRACY_NO_BROADCAST, TRACY_NO_CODE_TRANSFER, ... option(TRACY_ENABLE "" ON) option(TRACY_ON_DEMAND "" ON) add_subdirectory(3rdparty/tracy) # target: TracyClient or alias Tracy::TracyClient @@ -2406,6 +2406,20 @@ \subsubsection{First steps} target_link_libraries( PUBLIC Tracy::TracyClientF90) \end{lstlisting} +For using Link-Time optimizations, link both \texttt{Tracy::TracyClient} and \texttt{Tracy::TracyClientF90} to any target where you use Tracy for profiling: + +\begin{lstlisting} +target_link_libraries( PUBLIC Tracy::TracyClient Tracy::TracyClientF90) +\end{lstlisting} + +\begin{bclogo}[ +noborder=true, +couleur=black!5, +logo=\bcbombe +]{Important} +The same compiler (vendor + version) must be used for LTO for \textbf{ALL} languages in project. +\end{bclogo} + \begin{bclogo}[ noborder=true, couleur=black!5, @@ -2434,6 +2448,12 @@ \subsubsection{First steps} \begin{lstlisting} target_link_libraries( PUBLIC TracyClientF90) \end{lstlisting} + +For using Link-Time optimizations (LTO), you also need to link with \texttt{TracyClient}: + +\begin{lstlisting} +target_link_libraries( PUBLIC TracyClient TracyClientF90) +\end{lstlisting} \end{bclogo} \paragraph{\texttt{tracy} module} From b87a935c58a89ec9f7b73b5a3c88b7f9b7f23fbe Mon Sep 17 00:00:00 2001 From: "Igor S. Gerasimov" Date: Sat, 11 Jan 2025 19:35:39 +0100 Subject: [PATCH 4/5] Allow global setting of CMAKE_INTERPROCEDURAL_OPTIMIZATION --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b6d077820..1e5171613 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ if(TRACY_Fortran) set(CMAKE_Fortran_VERSION 2003) endif() -if(TRACY_LTO) +if(TRACY_LTO OR CMAKE_INTERPROCEDURAL_OPTIMIZATION) include(CheckIPOSupported) check_ipo_supported(RESULT LTO_SUPPORTED) if(NOT LTO_SUPPORTED) From be3aa5ac219b792ed71a2599e5e5985599817943 Mon Sep 17 00:00:00 2001 From: "Igor S. Gerasimov" Date: Sat, 11 Jan 2025 19:56:02 +0100 Subject: [PATCH 5/5] Add info about TRACY_LTO --- manual/tracy.tex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/manual/tracy.tex b/manual/tracy.tex index 58f1551e3..47675e61d 100644 --- a/manual/tracy.tex +++ b/manual/tracy.tex @@ -479,6 +479,9 @@ \subsubsection{CMake integration} \end{lstlisting} \end{bclogo} +While using \texttt{set(CMAKE\_INTERPROCEDURAL\_OPTIMIZATION ON)} is a convenient way to enable Link-Time Optimization (LTO) for an entire project, there are situations in which this may not work due to excessive compilation times, linking issues, compiler bugs, or other reasons. +For such cases, Tracy provides an option to enable Link-Time Optimization for itself using the \texttt{TRACY\_LTO} variable during the CMake configuration stage. + \subsubsection{Meson integration} If you are using the Meson build system, you can add Tracy using the Wrap dependency system. To do this, place the \texttt{tracy.wrap} file in the \texttt{subprojects} directory of your project, with the following content. The \texttt{head} \texttt{revision} field tracks Tracy's \texttt{master} branch. If you want to lock to a specific version of Tracy instead, you can just set the \texttt{revision} field to an appropriate git tag.