From 75737c36d52640dd9701531beedf347876ab3b94 Mon Sep 17 00:00:00 2001
From: Kugan <kvivekananda@nvidia.com>
Date: Sat, 3 May 2025 17:52:22 -0700
Subject: [PATCH 4/4] [AUTOFDO][AARCH64] Add support for profilebootstrap

Add support for autoprofiledbootstrap in aarch64.
This is similar to what is done for i386. Added
gcc/config/aarch64/gcc-auto-profile for aarch64 profile
creation.

How to run:
configure --with-build-config=bootstrap-lto
make autoprofiledbootstrap

ChangeLog:

	* Makefile.def: AUTO_PROFILE based on autofdo_target.
	* Makefile.in: Likewise.
	* configure: Regenerate.
	* configure.ac: Set autofdo_target.

gcc/ChangeLog:

	* config/aarch64/gcc-auto-profile: New file.

Signed-off-by: Kugan Vivekanandarajah <kvivekananda@nvidia.com>
---
 Makefile.def                        |  2 +-
 Makefile.in                         | 68 ++++++++++++++---------------
 configure                           |  9 ++++
 configure.ac                        |  8 ++++
 gcc/config/aarch64/gcc-auto-profile | 49 +++++++++++++++++++++
 5 files changed, 101 insertions(+), 35 deletions(-)
 create mode 100755 gcc/config/aarch64/gcc-auto-profile

diff --git a/Makefile.def b/Makefile.def
index 3f980bce8c0..9c3f3494f15 100644
--- a/Makefile.def
+++ b/Makefile.def
@@ -758,7 +758,7 @@ bootstrap_stage = {
 	bootstrap_target=profiledbootstrap ; };
 bootstrap_stage = {
 	id=autoprofile ; prev=1 ;
-        autoprofile="$$s/gcc/config/i386/$(AUTO_PROFILE)" ; };
+        autoprofile="$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE)" ; };
 bootstrap_stage = {
 	id=autofeedback ; prev=autoprofile ;
 	bootstrap_target=autoprofiledbootstrap ;
diff --git a/Makefile.in b/Makefile.in
index b1ed67d3d4f..1e856472689 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -4271,7 +4271,7 @@ all-stageautoprofile-bfd: configure-stageautoprofile-bfd
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/bfd && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -5411,7 +5411,7 @@ all-stageautoprofile-opcodes: configure-stageautoprofile-opcodes
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/opcodes && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -6551,7 +6551,7 @@ all-stageautoprofile-binutils: configure-stageautoprofile-binutils
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/binutils && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -10037,7 +10037,7 @@ all-stageautoprofile-fixincludes: configure-stageautoprofile-fixincludes
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/fixincludes && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -11617,7 +11617,7 @@ all-stageautoprofile-gas: configure-stageautoprofile-gas
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/gas && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -12766,7 +12766,7 @@ all-stageautoprofile-gcc: configure-stageautoprofile-gcc
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/gcc && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -13915,7 +13915,7 @@ all-stageautoprofile-gmp: configure-stageautoprofile-gmp
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/gmp && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -15052,7 +15052,7 @@ all-stageautoprofile-mpfr: configure-stageautoprofile-mpfr
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/mpfr && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -16189,7 +16189,7 @@ all-stageautoprofile-mpc: configure-stageautoprofile-mpc
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/mpc && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -17326,7 +17326,7 @@ all-stageautoprofile-isl: configure-stageautoprofile-isl
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/isl && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -18454,7 +18454,7 @@ all-stageautoprofile-gold: configure-stageautoprofile-gold
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/gold && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -20539,7 +20539,7 @@ all-stageautoprofile-gettext: configure-stageautoprofile-gettext
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/gettext && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -22449,7 +22449,7 @@ all-stageautoprofile-ld: configure-stageautoprofile-ld
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/ld && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -23589,7 +23589,7 @@ all-stageautoprofile-libbacktrace: configure-stageautoprofile-libbacktrace
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/libbacktrace && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -24729,7 +24729,7 @@ all-stageautoprofile-libcpp: configure-stageautoprofile-libcpp
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/libcpp && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -25869,7 +25869,7 @@ all-stageautoprofile-libcody: configure-stageautoprofile-libcody
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/libcody && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -26858,7 +26858,7 @@ all-stageautoprofile-libdecnumber: configure-stageautoprofile-libdecnumber
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/libdecnumber && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -28460,7 +28460,7 @@ all-stageautoprofile-libiberty: configure-stageautoprofile-libiberty
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/libiberty && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -29609,7 +29609,7 @@ all-stageautoprofile-libiberty-linker-plugin: configure-stageautoprofile-libiber
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/libiberty-linker-plugin && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -30758,7 +30758,7 @@ all-stageautoprofile-libiconv: configure-stageautoprofile-libiconv
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/libiconv && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -34130,7 +34130,7 @@ all-stageautoprofile-zlib: configure-stageautoprofile-zlib
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/zlib && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -40321,7 +40321,7 @@ all-stageautoprofile-lto-plugin: configure-stageautoprofile-lto-plugin
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/lto-plugin && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -42397,7 +42397,7 @@ all-stageautoprofile-libctf: configure-stageautoprofile-libctf
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/libctf && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -43537,7 +43537,7 @@ all-stageautoprofile-libsframe: configure-stageautoprofile-libsframe
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/libsframe && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -44677,7 +44677,7 @@ all-stageautoprofile-libgrust: configure-stageautoprofile-libgrust
 	$(HOST_EXPORTS) \
 	$(POSTSTAGE1_HOST_EXPORTS)  \
 	cd $(HOST_SUBDIR)/libgrust && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(STAGEautoprofile_CFLAGS)" \
 		GENERATOR_CFLAGS="$(STAGEautoprofile_GENERATOR_CFLAGS)" \
@@ -45939,7 +45939,7 @@ all-stageautoprofile-target-libstdc++-v3: configure-stageautoprofile-target-libs
 	$(RAW_CXX_TARGET_EXPORTS) \
 	  \
 	cd $(TARGET_SUBDIR)/libstdc++-v3 && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(CFLAGS_FOR_TARGET)" \
 		CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
@@ -47209,7 +47209,7 @@ all-stageautoprofile-target-libsanitizer: configure-stageautoprofile-target-libs
 	$(RAW_CXX_TARGET_EXPORTS) \
 	  \
 	cd $(TARGET_SUBDIR)/libsanitizer && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(CFLAGS_FOR_TARGET)" \
 		CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
@@ -48479,7 +48479,7 @@ all-stageautoprofile-target-libvtv: configure-stageautoprofile-target-libvtv
 	$(RAW_CXX_TARGET_EXPORTS) \
 	  \
 	cd $(TARGET_SUBDIR)/libvtv && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(CFLAGS_FOR_TARGET)" \
 		CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
@@ -50719,7 +50719,7 @@ all-stageautoprofile-target-libgcc: configure-stageautoprofile-target-libgcc
 	$(NORMAL_TARGET_EXPORTS) \
 	  \
 	cd $(TARGET_SUBDIR)/libgcc && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(CFLAGS_FOR_TARGET)" \
 		CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
@@ -51951,7 +51951,7 @@ all-stageautoprofile-target-libbacktrace: configure-stageautoprofile-target-libb
 	$(NORMAL_TARGET_EXPORTS) \
 	  \
 	cd $(TARGET_SUBDIR)/libbacktrace && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(CFLAGS_FOR_TARGET)" \
 		CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
@@ -55128,7 +55128,7 @@ all-stageautoprofile-target-libphobos: configure-stageautoprofile-target-libphob
 	$(NORMAL_TARGET_EXPORTS) \
 	  \
 	cd $(TARGET_SUBDIR)/libphobos && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(CFLAGS_FOR_TARGET)" \
 		CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
@@ -58258,7 +58258,7 @@ all-stageautoprofile-target-zlib: configure-stageautoprofile-target-zlib
 	$(NORMAL_TARGET_EXPORTS) \
 	  \
 	cd $(TARGET_SUBDIR)/zlib && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(CFLAGS_FOR_TARGET)" \
 		CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
@@ -60983,7 +60983,7 @@ all-stageautoprofile-target-libgomp: configure-stageautoprofile-target-libgomp
 	$(NORMAL_TARGET_EXPORTS) \
 	  \
 	cd $(TARGET_SUBDIR)/libgomp && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(CFLAGS_FOR_TARGET)" \
 		CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
@@ -62738,7 +62738,7 @@ all-stageautoprofile-target-libatomic: configure-stageautoprofile-target-libatom
 	$(NORMAL_TARGET_EXPORTS) \
 	  \
 	cd $(TARGET_SUBDIR)/libatomic && \
-	$$s/gcc/config/i386/$(AUTO_PROFILE) \
+	$$s/gcc/config/@autofdo_target@/$(AUTO_PROFILE) \
 	$(MAKE) $(BASE_FLAGS_TO_PASS) \
 		CFLAGS="$(CFLAGS_FOR_TARGET)" \
 		CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
diff --git a/configure b/configure
index ebc44416b6c..215101eaf58 100755
--- a/configure
+++ b/configure
@@ -730,6 +730,7 @@ CPPFLAGS
 LDFLAGS
 CFLAGS
 CC
+autofdo_target
 target_subdir
 host_subdir
 build_subdir
@@ -3395,6 +3396,14 @@ case "${target}" in
     ;;
 esac
 
+autofdo_target="i386"
+case "${target}" in
+  aarch64-*-*)
+    autofdo_target="aarch64"
+    ;;
+esac
+
+
 # Disable libssp for some systems.
 case "${target}" in
   avr-*-*)
diff --git a/configure.ac b/configure.ac
index 730db3c1402..701284e38f2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -621,6 +621,14 @@ case "${target}" in
     ;;
 esac
 
+autofdo_target="i386"
+case "${target}" in
+  aarch64-*-*)
+    autofdo_target="aarch64"
+    ;;
+esac
+AC_SUBST(autofdo_target)
+
 # Disable libssp for some systems.
 case "${target}" in
   avr-*-*)
diff --git a/gcc/config/aarch64/gcc-auto-profile b/gcc/config/aarch64/gcc-auto-profile
new file mode 100755
index 00000000000..5f792f5fade
--- /dev/null
+++ b/gcc/config/aarch64/gcc-auto-profile
@@ -0,0 +1,49 @@
+#!/bin/sh
+# Profile workload for gcc profile feedback (autofdo) using Linux perf.
+# Copyright (C) 2025 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 3, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# Run perf record with branch stack sampling and check for
+# specific error message to see if it is supported.
+use_brbe=true
+output=$(perf record -j any,u ls 2>&1)
+if [[ "$output" == *"Error::P: PMU Hardware or event type doesn't support branch stack sampling."* ]]; then
+  use_brbe=false
+fi
+
+FLAGS=u
+if [ "$1" = "--kernel" ] ; then
+  FLAGS=k
+  shift
+fi
+if [ "$1" = "--all" ] ; then
+  FLAGS=u,k
+  shift
+fi
+
+if [ "$use_brbe" = true ] ; then
+  if grep -q hypervisor /proc/cpuinfo ; then
+    echo >&2 "Warning: branch profiling may not be functional in VMs"
+  fi
+  set -x
+  perf record -j any,$FLAGS "$@"
+  set +x
+else
+  set -x
+  echo >&2 "Warning: branch profiling may not be functional without BRBE"
+  perf record "$@"
+  set +x
+fi
-- 
2.34.1

