54dc0a
From 19c4942a1415d0f99c910949ba55c95ddcc5a976 Mon Sep 17 00:00:00 2001
642cd7
From: Song Liu <songliubraving@fb.com>
642cd7
Date: Wed, 13 Oct 2021 01:03:55 -0700
54dc0a
Subject: [PATCH 104/107] kpatch-build: add support for clang pgo
642cd7
642cd7
For clang with Profile-Guided Optimization (PGO), profile data is needed
642cd7
to compile the livepatch properly. Add option -p|--profile-data, which
642cd7
specifies the profile data file. This option is only valid with
642cd7
CONFIG_CC_IS_CLANG and CONFIG_PGO_CLANG.
642cd7
642cd7
Signed-off-by: Song Liu <songliubraving@fb.com>
642cd7
---
642cd7
 kpatch-build/kpatch-build | 21 ++++++++++++++++++++-
642cd7
 1 file changed, 20 insertions(+), 1 deletion(-)
642cd7
642cd7
diff --git a/kpatch-build/kpatch-build b/kpatch-build/kpatch-build
54dc0a
index f712578..fc9f92b 100755
642cd7
--- a/kpatch-build/kpatch-build
642cd7
+++ b/kpatch-build/kpatch-build
642cd7
@@ -677,9 +677,10 @@ usage() {
642cd7
 	echo "		--skip-cleanup          Skip post-build cleanup" >&2
642cd7
 	echo "		--skip-compiler-check   Skip compiler version matching check" >&2
642cd7
 	echo "		                        (not recommended)" >&2
642cd7
+	echo "		-p, --profile-data      specify profile data for PGO (clang only)" >&2
642cd7
 }
642cd7
 
642cd7
-options="$(getopt -o ha:r:s:c:v:j:t:n:o:dR -l "help,archversion:,sourcerpm:,sourcedir:,config:,vmlinux:,jobs:,target:,name:,output:,oot-module:,oot-module-src:,debug,skip-gcc-check,skip-compiler-check,skip-cleanup,non-replace" -- "$@")" || die "getopt failed"
642cd7
+options="$(getopt -o ha:r:s:c:v:j:t:n:o:dRp: -l "help,archversion:,sourcerpm:,sourcedir:,config:,vmlinux:,jobs:,target:,name:,output:,oot-module:,oot-module-src:,debug,skip-gcc-check,skip-compiler-check,skip-cleanup,non-replace,profile-data" -- "$@")" || die "getopt failed"
642cd7
 
642cd7
 eval set -- "$options"
642cd7
 
642cd7
@@ -761,6 +762,10 @@ while [[ $# -gt 0 ]]; do
642cd7
 		echo "WARNING: Skipping compiler version matching check (not recommended)"
642cd7
 		SKIPCOMPILERCHECK=1
642cd7
 		;;
642cd7
+	-p|--profile-data)
642cd7
+		PROFILE_DATA="$(readlink -f "$2")"
642cd7
+		shift
642cd7
+		;;
642cd7
 	*)
642cd7
 		[[ "$1" = "--" ]] && shift && continue
642cd7
 		[[ ! -f "$1" ]] && die "patch file '$1' not found"
54dc0a
@@ -1073,6 +1078,16 @@ fi
642cd7
 
642cd7
 if [[ -n "$CONFIG_CC_IS_CLANG" ]]; then
642cd7
 	echo "WARNING: Clang support is experimental"
642cd7
+	if [[ -z "$PROFILE_DATA" ]] && [[ -n "$CONFIG_PGO_CLANG" ]]; then
642cd7
+		die "Please specify profile-data for CONFIG_PGO_CLANG"
642cd7
+	fi
642cd7
+	if [[ -n "$PROFILE_DATA" ]] && [[ -z "$CONFIG_PGO_CLANG" ]]; then
642cd7
+		echo "WARNING profile-data specified w/o CONFIG_PGO_CLANG, ignore it"
642cd7
+	fi
642cd7
+else
642cd7
+	if [[ -n "$PROFILE_DATA" ]]; then
642cd7
+		die "Only supports profile-data with Clang"
642cd7
+	fi
642cd7
 fi
642cd7
 
642cd7
 if [[ "$SKIPCOMPILERCHECK" -eq 0 ]]; then
54dc0a
@@ -1124,6 +1139,10 @@ declare -a MAKEVARS
642cd7
 if [[ -n "$CONFIG_CC_IS_CLANG" ]]; then
642cd7
 	MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${CLANG}")
642cd7
 	MAKEVARS+=("HOSTCC=clang")
642cd7
+	if [[ -n "$CONFIG_PGO_CLANG" ]]; then
642cd7
+		MAKEVARS+=("CFLAGS_PGO_CLANG=-fprofile-use=$PROFILE_DATA")
642cd7
+		MAKEVARS+=("LLVM=1")
642cd7
+	fi
642cd7
 else
642cd7
 	MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${GCC}")
642cd7
 fi
642cd7
-- 
642cd7
2.37.3
642cd7