我正在 Ubuntu 20.04 上使用 CMake 3.16.3 和 G++ 9.3 编译应用程序。这是当前的源目录:
. (this is called MyApp)
├── src
│ ├── /* source files */
│ └── CMakeLists.txt
├── tests
│ ├── /* source files */
│ └── CMakeLists.txt
├── build-release
│ └── pgo /* folder for .gcda files */
├── build_release.sh
└── CMakeLists.txt*
我在 CMakeLists.txt (根也标有星号)中设置标志如下:
set(MYAPP_PGO "-fprofile-dir=${MYAPP_PGO} -fprofile-generate=${MYAPP_PGO}")
// apply flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O3 -march=native -flto ${MYAPP_PGO}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MYAPP_PGO}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${MYAPP_PGO}")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${MYAPP_PGO}")
add_subdirectory(src)
add_subdirectory(tests)
src/CMakeLists.txt
和中没有设置标志tests/CMakeLists.txt
。
MYAPP_PGO
在 bash 中设置,是build-release/pgo
. 我有一个build_release.sh
构建程序的脚本 ( ):
# excluded other lines and flags for brevity
cd build-release
cmake -DMYAPP_PGO="$(pwd)/pgo/" ..
make -j1
profile 运行后,我看到里面有多个文件, and里面build-release/pgo
每个cpp
文件一个,比如src
test
#home#johndoe#MyApp#build-release#src#CMakeFiles#MYAPPLICATION_myapp.dir#sample_source.cpp.gcda
但是,当再次编译时
// this is a multithreaded program
set(MYAPP_PGO "-fprofile-dir=${MYAPP_PGO} -fprofile-use=${MYAPP_PGO} -fprofile-correction")
// apply flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O3 -march=native -flto ${MYAPP_PGO}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MYAPP_PGO}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${MYAPP_PGO}")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${MYAPP_PGO}")
add_subdirectory(src)
add_subdirectory(tests)
我收到一条警告,指出缺少配置文件:
/home/johndoe/MyApp/src/sample_source.cpp: In function ‘_GLOBAL__sub_I_sample_source.cpp’:
/home/johndoe/MyApp/src/sample_source.cpp:215:1: warning: ‘/home/johndoe/MyApp/src/sample_source.cpp/build-release/pgo//#home#johndoe#MyApp#build-release#src#CMakeFiles#MYAPPLICATION_myapp.dir#sample_source.cpp.gcda’ profile count data file not found [-Wmissing-profile]
215:1
指此源文件中的最后一个字符。
我已经测试了有和没有 PGO 的程序的速度,并没有看到任何改进。尽管 PGO 不能保证带来任何加速,但我倾向于认为 PGO 在这里没有按预期工作。难道我做错了什么?