可可西

Xcode之PGO机制

PGO (Profile-guided optimization)是指利用程序运行过程中采集到的 profile 数据,来重新编译程序以达到优化效果的 post-link 优化技术。它是一种通用技术,不局限于某种语言

具体过程是:首先要对程序进行剖分埋点(Profile),收集程序实际运行的数据生成 profiling 文件,根据此文件来进行性能优化:通过缩小代码大小,减少错误分支预测,重新组织代码布局减少指令缓存问题等

大体流程如下:

 

本文使用的Xcode16.2讲解PGO的详细过程

 

编译

不开启PGO进行编译(原始Binary)

-+= 00001 root /sbin/launchd
 \-+= 47868 admin /Applications/Xcode.app/Contents/MacOS/Xcode
   \-+= 52941 admin /Applications/Xcode.app/Contents/SharedFrameworks/XCBuild.framework/Versions/A/PlugIns/XCBBuildService.bundle/Contents/MacOS/XCBBuildService
     \-+= 66741 admin /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -ivfsstatcache 
/Users/admin/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx15.2-24C94-1dd28740b163f220e3e9b9fd2a542f53.sdkstatcache -fmessage-length=0 -fdiagnostics-show-note-include-stack
-fmacro-backtrace-limit=0 -fno-color-diagnostics -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fbuild-session-file=/Users/admin/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation
-fmodules-validate-once-per-build-session -Wnon-modular-include-in-framework-module -Werror=non-modular-include-in-framework-module -Wno-trigraphs -Wno-missing-field-initializers -Wno-missing-prototypes
-Werror=return-type -Wdocumentation -Wunreachable-code -Wquoted-include-in-framework-header -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-non-virtual-dtor -Wno-overloaded-virtual
-Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized
-Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion
-Wnon-literal-null-conversion -Wobjc-literal-conversion -Wshorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -Wno-implicit-fallthrough -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof
-Wno-sign-conversion -Winfinite-recursion -Wmove -Wcomma -Wblock-capture-autoreleasing -Wstrict-prototypes -Wrange-loop-analysis -Wno-semicolon-before-method-body -Wunguarded-availability -index-store-path
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Index.noindex/DataStore
@/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/82b82416624d2658e5098eb0a28c15c5-common-args.resp
-MMD -MT dependencies -MF /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.d
--serialize-diagnostics /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.dia
-c /Users/admin/Github/LearnMetalCPP/learn-metal/01-primitive/01-primitive.cpp
-o /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o
-index-unit-output-path /LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o \--- 66744 admin /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -cc1 -triple x86_64-apple-macosx12.0.0 -Wundef-prefix=TARGET_OS_ -Wdeprecated-objc-isa-usage
-Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name 01-primitive.cpp
-mrelocation-model pic -pic-level 2 -mframe-pointer=all -fno-strict-return -ffp-contract=on -fno-rounding-math -funwind-tables=2 -target-sdk-version=15.2 -fvisibility-inlines-hidden-static-local-var
-fno-modulemap-allow-subdirectory-search -target-cpu penryn -tune-cpu generic -dwarf-ext-refs -fmodule-format=obj -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb -target-linker-version 1115.7.3
-fcoverage-compilation-dir=/Users/admin/Github/LearnMetalCPP -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16 -index-store-path
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Index.noindex/DataStore -index-unit-output-path /LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o
-dependency-file /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.d
-skip-unused-modulemap-deps -MT dependencies -ivfsstatcache /Users/admin/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx15.2-24C94-1dd28740b163f220e3e9b9fd2a542f53.sdkstatcache
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -iquote
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-generated-files.hmap
-iquote /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-project-headers.hmap
-D _LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG -D DEBUG=1
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-own-target-headers.hmap
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-all-target-headers.hmap
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Products/Debug/include -I metal-cpp -I metal-cpp-extensions
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/DerivedSources-normal/x86_64
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/DerivedSources/x86_64
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/DerivedSources
-F/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Products/Debug -stdlib=libc++
-internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk/usr/include/c++/v1
-internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk/usr/local/include
-internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16/include
-internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk/usr/include
-internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -O0 -Wnon-modular-include-in-framework-module
-Werror=non-modular-include-in-framework-module -Wno-trigraphs -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wdocumentation -Wunreachable-code
-Wquoted-include-in-framework-header -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces
-Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized -Wconditional-uninitialized -Wno-unknown-pragmas
-Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion -Wnon-literal-null-conversion -Wobjc-literal-conversion
-Wshorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -Wno-implicit-fallthrough -Wdeprecated-declarations -Winvalid-offsetof -Wno-sign-conversion -Winfinite-recursion -Wmove -Wcomma -Wblock-capture-autoreleasing
-Wstrict-prototypes -Wrange-loop-analysis -Wno-semicolon-before-method-body -Wunguarded-availability -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class
-Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-elaborated-enum-base -Wno-reserved-identifier -Wno-gnu-folding-constant
-std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/Users/admin/Github/LearnMetalCPP -ferror-limit 19 -fmacro-backtrace-limit=0 -fvisibility-inlines-hidden -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link
-fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-cxx-modules -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fcxx-exceptions -fexceptions
-fpascal-strings -fmax-type-align=16 -fdiagnostics-show-note-include-stack -fasm-blocks -serialize-diagnostic-file
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.dia
-clang-vendor-feature=+disableNonDependentMemberExprInCurrentInstantiation -fno-odr-hash-protocols -clang-vendor-feature=+enableAggressiveVLAFolding -clang-vendor-feature=+revert09abecef7bbf
-clang-vendor-feature=+thisNoAlignAttr -clang-vendor-feature=+thisNoNullAttr -clang-vendor-feature=+disableAtImportPrivateFrameworkInImplementationError -D__GCC_HAVE_DWARF2_CFI_ASM=1
-o /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o
-x c++ /Users/admin/Github/LearnMetalCPP/learn-metal/01-primitive/01-primitive.cpp

 

原始Binary的PGO相关编译配置如下:

 

开启PGO Profile进行编译(剖分埋点Binary)

PGO Profile,即instrumented for PGO data collection

开启之后,编译得到的Binary会增大不少(如:从197MB增大到347MB)

-+= 00001 root /sbin/launchd
 \-+= 47868 admin /Applications/Xcode.app/Contents/MacOS/Xcode
   \-+= 52941 admin /Applications/Xcode.app/Contents/SharedFrameworks/XCBuild.framework/Versions/A/PlugIns/XCBBuildService.bundle/Contents/MacOS/XCBBuildService
     \-+= 68275 admin /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -ivfsstatcache 
/Users/admin/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx15.2-24C94-1dd28740b163f220e3e9b9fd2a542f53.sdkstatcache -fmessage-length=0 -fdiagnostics-show-note-include-stack
-fmacro-backtrace-limit=0 -fno-color-diagnostics -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fbuild-session-file=/Users/admin/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation
-fmodules-validate-once-per-build-session -Wnon-modular-include-in-framework-module -Werror=non-modular-include-in-framework-module -Wno-trigraphs -Wno-missing-field-initializers -Wno-missing-prototypes
-Werror=return-type -Wdocumentation -Wunreachable-code -Wquoted-include-in-framework-header -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-non-virtual-dtor -Wno-overloaded-virtual
-Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized
-Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion
-Wnon-literal-null-conversion -Wobjc-literal-conversion -Wshorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -Wno-implicit-fallthrough -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof
-Wno-sign-conversion -Winfinite-recursion -Wmove -Wcomma -Wblock-capture-autoreleasing -Wstrict-prototypes -Wrange-loop-analysis -Wno-semicolon-before-method-body -Wunguarded-availability -index-store-path
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Index.noindex/DataStore
@/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/82b82416624d2658e5098eb0a28c15c5-common-args.resp
-MMD -MT dependencies -MF /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.d
--serialize-diagnostics /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.dia
-c /Users/admin/Github/LearnMetalCPP/learn-metal/01-primitive/01-primitive.cpp
-o /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o
-index-unit-output-path /LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o \--- 68276 admin /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -cc1 -triple x86_64-apple-macosx12.0.0 -Wundef-prefix=TARGET_OS_ -Wdeprecated-objc-isa-usage
-Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name 01-primitive.cpp
-mrelocation-model pic -pic-level 2 -mframe-pointer=all -fno-strict-return -ffp-contract=on -fno-rounding-math -funwind-tables=2 -target-sdk-version=15.2 -fvisibility-inlines-hidden-static-local-var
-fno-modulemap-allow-subdirectory-search -target-cpu penryn -tune-cpu generic -dwarf-ext-refs -fmodule-format=obj -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb -target-linker-version 1115.7.3
-fprofile-instrument=clang -fcoverage-compilation-dir=/Users/admin/Github/LearnMetalCPP -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16 -index-store-path
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Index.noindex/DataStore -index-unit-output-path /LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o
-dependency-file /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.d
-skip-unused-modulemap-deps -MT dependencies -ivfsstatcache /Users/admin/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx15.2-24C94-1dd28740b163f220e3e9b9fd2a542f53.sdkstatcache
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -iquote
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-generated-files.hmap
-iquote /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-project-headers.hmap
-D _LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG -D DEBUG=1
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-own-target-headers.hmap
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-all-target-headers.hmap
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Products/Debug/include -I metal-cpp -I metal-cpp-extensions
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/DerivedSources-normal/x86_64
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/DerivedSources/x86_64
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/DerivedSources
-F/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Products/Debug -stdlib=libc++
-internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk/usr/include/c++/v1
-internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk/usr/local/include
-internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16/include
-internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk/usr/include
-internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -O0 -Wnon-modular-include-in-framework-module
-Werror=non-modular-include-in-framework-module -Wno-trigraphs -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wdocumentation -Wunreachable-code
-Wquoted-include-in-framework-header -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces
-Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized -Wconditional-uninitialized -Wno-unknown-pragmas
-Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion -Wnon-literal-null-conversion -Wobjc-literal-conversion
-Wshorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -Wno-implicit-fallthrough -Wdeprecated-declarations -Winvalid-offsetof -Wno-sign-conversion -Winfinite-recursion -Wmove -Wcomma -Wblock-capture-autoreleasing
-Wstrict-prototypes -Wrange-loop-analysis -Wno-semicolon-before-method-body -Wunguarded-availability -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class
-Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-elaborated-enum-base -Wno-reserved-identifier -Wno-gnu-folding-constant
-std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/Users/admin/Github/LearnMetalCPP -ferror-limit 19 -fmacro-backtrace-limit=0 -fvisibility-inlines-hidden -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link
-fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-cxx-modules -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fcxx-exceptions -fexceptions
-fpascal-strings -fmax-type-align=16 -fdiagnostics-show-note-include-stack -fasm-blocks -serialize-diagnostic-file
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.dia
-clang-vendor-feature=+disableNonDependentMemberExprInCurrentInstantiation -fno-odr-hash-protocols -clang-vendor-feature=+enableAggressiveVLAFolding -clang-vendor-feature=+revert09abecef7bbf -
clang-vendor-feature=+thisNoAlignAttr -clang-vendor-feature=+thisNoNullAttr -clang-vendor-feature=+disableAtImportPrivateFrameworkInImplementationError -D__GCC_HAVE_DWARF2_CFI_ASM=1
-o /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/PGO/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o
-x c++ /Users/admin/Github/LearnMetalCPP/learn-metal/01-primitive/01-primitive.cpp

 

运行PGO剖分埋点Binary

点击菜单Product - Perform Action - Generate Optimization Profile...来运行

 

会弹出Generate Optimization Profile对话框

 

点击Run,会弹出Enable Profile Guided Optimization对话框

 

点击Enable后,Use Optimization Profile就会变成Yes

注:下次编译就会使用Optimization Profile File指定的文件/Users/admin/Github/LearnMetalCPP/OptimizationProfiles/LearnMetalCPP.profdata进行优化编译

 

当结束运行时会生成/Users/admin/Github/LearnMetalCPP/OptimizationProfiles/LearnMetalCPP.profdata 

 

LearnMetalCPP.profdata为一个二进制文件

 

开启PGO优化进行编译(优化Binary)

使用收集的profdata来指导编译,得到优化Binary。会比原始Binary要小一点(如:从197MB减小为178MB)

-+= 00001 root /sbin/launchd
 \-+= 47868 admin /Applications/Xcode.app/Contents/MacOS/Xcode
   \-+= 52941 admin /Applications/Xcode.app/Contents/SharedFrameworks/XCBuild.framework/Versions/A/PlugIns/XCBBuildService.bundle/Contents/MacOS/XCBBuildService
     \-+= 95912 admin /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -ivfsstatcache 
/Users/admin/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx15.2-24C94-1dd28740b163f220e3e9b9fd2a542f53.sdkstatcache -fmessage-length=0 -fdiagnostics-show-note-include-stack
-fmacro-backtrace-limit=0 -fno-color-diagnostics -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fbuild-session-file=/Users/admin/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation
-fmodules-validate-once-per-build-session -Wnon-modular-include-in-framework-module -Werror=non-modular-include-in-framework-module -Wno-trigraphs -Wno-missing-field-initializers -Wno-missing-prototypes
-Werror=return-type -Wdocumentation -Wunreachable-code -Wquoted-include-in-framework-header -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-non-virtual-dtor -Wno-overloaded-virtual
-Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized
-Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion
-Wnon-literal-null-conversion -Wobjc-literal-conversion -Wshorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -Wno-implicit-fallthrough -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof
-Wno-sign-conversion -Winfinite-recursion -Wmove -Wcomma -Wblock-capture-autoreleasing -Wstrict-prototypes -Wrange-loop-analysis -Wno-semicolon-before-method-body -Wunguarded-availability -index-store-path
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Index.noindex/DataStore
@/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/82b82416624d2658e5098eb0a28c15c5-common-args.resp
-MMD -MT dependencies -MF /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.d
--serialize-diagnostics /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.dia
-c /Users/admin/Github/LearnMetalCPP/learn-metal/01-primitive/01-primitive.cpp
-o /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o
-index-unit-output-path /LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o \--- 95913 admin /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -cc1 -triple x86_64-apple-macosx12.0.0 -Wundef-prefix=TARGET_OS_ -Wdeprecated-objc-isa-usage
-Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name 01-primitive.cpp
-mrelocation-model pic -pic-level 2 -mframe-pointer=all -fno-strict-return -ffp-contract=on -fno-rounding-math -funwind-tables=2 -target-sdk-version=15.2 -fvisibility-inlines-hidden-static-local-var
-fno-modulemap-allow-subdirectory-search -target-cpu penryn -tune-cpu generic -dwarf-ext-refs -fmodule-format=obj -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb -target-linker-version 1115.7.3
-fprofile-instrument-use-path=/Users/admin/Github/LearnMetalCPP/OptimizationProfiles/LearnMetalCPP.profdata
-fcoverage-compilation-dir=/Users/admin/Github/LearnMetalCPP -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16 -index-store-path
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Index.noindex/DataStore -index-unit-output-path /LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o
-dependency-file /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.d
-skip-unused-modulemap-deps -MT dependencies -ivfsstatcache /Users/admin/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx15.2-24C94-1dd28740b163f220e3e9b9fd2a542f53.sdkstatcache
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -iquote
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-generated-files.hmap
-iquote /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-project-headers.hmap
-D _LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG -D DEBUG=1
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-own-target-headers.hmap
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/01-primitive-all-target-headers.hmap
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Products/Debug/include -I metal-cpp -I metal-cpp-extensions
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/DerivedSources-normal/x86_64
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/DerivedSources/x86_64
-I /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/DerivedSources
-F/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Products/Debug -stdlib=libc++
-internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk/usr/include/c++/v1
-internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk/usr/local/include
-internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16/include
-internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk/usr/include
-internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -O0 -Wnon-modular-include-in-framework-module
-Werror=non-modular-include-in-framework-module -Wno-trigraphs -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wdocumentation -Wunreachable-code
-Wquoted-include-in-framework-header -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces
-Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized -Wconditional-uninitialized -Wno-unknown-pragmas
-Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion -Wnon-literal-null-conversion -Wobjc-literal-conversion
-Wshorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -Wno-implicit-fallthrough -Wdeprecated-declarations -Winvalid-offsetof -Wno-sign-conversion -Winfinite-recursion -Wmove -Wcomma -Wblock-capture-autoreleasing
-Wstrict-prototypes -Wrange-loop-analysis -Wno-semicolon-before-method-body -Wunguarded-availability -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class
-Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-elaborated-enum-base -Wno-reserved-identifier -Wno-gnu-folding-constant
-std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/Users/admin/Github/LearnMetalCPP -ferror-limit 19 -fmacro-backtrace-limit=0 -fvisibility-inlines-hidden -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link
-fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-cxx-modules -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fcxx-exceptions -fexceptions
-fpascal-strings -fmax-type-align=16 -fdiagnostics-show-note-include-stack -fasm-blocks -serialize-diagnostic-file
/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.dia
-clang-vendor-feature=+disableNonDependentMemberExprInCurrentInstantiation -fno-odr-hash-protocols -clang-vendor-feature=+enableAggressiveVLAFolding -clang-vendor-feature=+revert09abecef7bbf
-clang-vendor-feature=+thisNoAlignAttr -clang-vendor-feature=+thisNoNullAttr -clang-vendor-feature=+disableAtImportPrivateFrameworkInImplementationError -D__GCC_HAVE_DWARF2_CFI_ASM=1
-o /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.o
-x c++ /Users/admin/Github/LearnMetalCPP/learn-metal/01-primitive/01-primitive.cpp

 

链接

-+= 00001 root /sbin/launchd
 |-+= 49316 admin /Applications/Xcode.app/Contents/MacOS/Xcode
 | |-+= 49605 admin /Applications/Xcode.app/Contents/SharedFrameworks/XCBuild.framework/Versions/A/PlugIns/XCBBuildService.bundle/Contents/MacOS/XCBBuildService
 | | \-+= 54968 admin /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -Xlinker -reproducible -target x86_64-apple-macos12.0 
 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -O0 
 -L/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/EagerLinkingTBDs/Debug 
 -L/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Products/Debug 
 -F/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/EagerLinkingTBDs/Debug 
 -F/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Products/Debug 
 -filelist /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.LinkFileList 
 -Xlinker -rpath -Xlinker @executable_path/../Frameworks -Xlinker -object_path_lto 
 -Xlinker /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive_lto.o 
 -Xlinker -export_dynamic -Xlinker -no_deduplicate -stdlib=libc++ -framework MetalKit -framework Metal -framework Foundation -Xlinker -no_adhoc_codesign -Xlinker -dependency_info 
 -Xlinker /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive_dependency_info.dat 
 -o /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Products/Debug/01-primitive.app/Contents/MacOS/01-primitive
 | |   \--- 54970 admin /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib 
 -no_deduplicate -dynamic -arch x86_64 -platform_version macos 12.0.0 15.2 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -O0 -mllvm -enable-linkonceodr-outlining 
 -o /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Products/Debug/01-primitive.app/Contents/MacOS/01-primitive 
 -L/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/EagerLinkingTBDs/Debug 
 -L/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Products/Debug -reproducible -filelist 
 /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive.LinkFileList 
 -rpath @executable_path/../Frameworks -object_path_lto 
 /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive_lto.o 
 -export_dynamic -no_deduplicate -framework MetalKit -framework Metal -framework Foundation -no_adhoc_codesign -dependency_info 
 /Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/LearnMetalCPP.build/Debug/01-primitive.build/Objects-normal/x86_64/01-primitive_dependency_info.dat 
 -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16/lib/darwin/libclang_rt.osx.a 
 -F/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Intermediates.noindex/EagerLinkingTBDs/Debug 
 -F/Users/admin/Library/Developer/Xcode/DerivedData/LearnMetalCPP-ewyfkefflcxiqyctfbxhqwfdnifl/Build/Products/Debug

注:没有特有的PGO链接选项 

 

参考

Xcode Profile Guided Optimization

基于 Xcode 自带工具 PGO 优化启动时间

iOS启动优化之PGO优化

iOS 基于PGO优化启动时间

posted on 2025-06-30 20:46  可可西  阅读(49)  评论(0)    收藏  举报

导航