650cfe
diff -up firefox-99.0/build/moz.configure/lto-pgo.configure.pgo firefox-99.0/build/moz.configure/lto-pgo.configure
650cfe
--- firefox-99.0/build/moz.configure/lto-pgo.configure.pgo	2022-03-31 01:24:38.000000000 +0200
650cfe
+++ firefox-99.0/build/moz.configure/lto-pgo.configure	2022-04-04 10:15:45.387694143 +0200
650cfe
@@ -247,8 +247,8 @@ def lto(
650cfe
             cflags.append("-flto")
650cfe
             ldflags.append("-flto")
650cfe
         else:
650cfe
-            cflags.append("-flto=thin")
650cfe
-            ldflags.append("-flto=thin")
650cfe
+            cflags.append("-flto")
650cfe
+            ldflags.append("-flto")
650cfe
 
650cfe
         if target.os == "Android" and value == "cross":
650cfe
             # Work around https://github.com/rust-lang/rust/issues/90088
650cfe
@@ -264,7 +264,7 @@ def lto(
650cfe
         if value == "full":
650cfe
             cflags.append("-flto")
650cfe
         else:
650cfe
-            cflags.append("-flto=thin")
650cfe
+            cflags.append("-flto")
650cfe
         # With clang-cl, -flto can only be used with -c or -fuse-ld=lld.
650cfe
         # AC_TRY_LINKs during configure don't have -c, so pass -fuse-ld=lld.
650cfe
         cflags.append("-fuse-ld=lld")
650cfe
diff -up firefox-99.0/build/pgo/profileserver.py.pgo firefox-99.0/build/pgo/profileserver.py
650cfe
--- firefox-99.0/build/pgo/profileserver.py.pgo	2022-03-31 01:24:38.000000000 +0200
650cfe
+++ firefox-99.0/build/pgo/profileserver.py	2022-04-04 10:15:45.387694143 +0200
650cfe
@@ -11,7 +11,7 @@ import glob
650cfe
 import subprocess
650cfe
 
650cfe
 import mozcrash
650cfe
-from mozbuild.base import MozbuildObject, BinaryNotFoundException
650cfe
+from mozbuild.base import MozbuildObject, BinaryNotFoundException, BuildEnvironmentNotFoundException
650cfe
 from mozfile import TemporaryDirectory
650cfe
 from mozhttpd import MozHttpd
650cfe
 from mozprofile import FirefoxProfile, Preferences
650cfe
@@ -87,9 +87,22 @@ if __name__ == "__main__":
650cfe
     locations = ServerLocations()
650cfe
     locations.add_host(host="127.0.0.1", port=PORT, options="primary,privileged")
650cfe
 
650cfe
-    old_profraw_files = glob.glob("*.profraw")
650cfe
-    for f in old_profraw_files:
650cfe
-        os.remove(f)
650cfe
+    using_gcc = False
650cfe
+    try:
650cfe
+        if build.config_environment.substs.get('CC_TYPE') == 'gcc':
650cfe
+            using_gcc = True
650cfe
+    except BuildEnvironmentNotFoundException:
650cfe
+        pass
650cfe
+
650cfe
+    if using_gcc:
650cfe
+        for dirpath, _, filenames in os.walk('.'):
650cfe
+            for f in filenames:
650cfe
+                if f.endswith('.gcda'):
650cfe
+                    os.remove(os.path.join(dirpath, f))
650cfe
+    else:
650cfe
+        old_profraw_files = glob.glob('*.profraw')
650cfe
+        for f in old_profraw_files:
650cfe
+            os.remove(f)
650cfe
 
650cfe
     with TemporaryDirectory() as profilePath:
650cfe
         # TODO: refactor this into mozprofile
650cfe
@@ -212,6 +225,10 @@ if __name__ == "__main__":
650cfe
             print("Firefox exited successfully, but produced a crashreport")
650cfe
             sys.exit(1)
650cfe
 
650cfe
+        print('Copying profile data....')
650cfe
+        os.system('pwd');
650cfe
+        os.system('tar cf profdata.tar.gz `find . -name "*.gcda"`; cd ..; tar xf instrumented/profdata.tar.gz;');
650cfe
+
650cfe
         llvm_profdata = env.get("LLVM_PROFDATA")
650cfe
         if llvm_profdata:
650cfe
             profraw_files = glob.glob("*.profraw")
650cfe
diff -up firefox-99.0/build/unix/mozconfig.unix.pgo firefox-99.0/build/unix/mozconfig.unix
650cfe
--- firefox-99.0/build/unix/mozconfig.unix.pgo	2022-03-31 01:24:38.000000000 +0200
650cfe
+++ firefox-99.0/build/unix/mozconfig.unix	2022-04-04 10:15:45.387694143 +0200
650cfe
@@ -4,6 +4,15 @@ if [ -n "$FORCE_GCC" ]; then
650cfe
     CC="$MOZ_FETCHES_DIR/gcc/bin/gcc"
650cfe
     CXX="$MOZ_FETCHES_DIR/gcc/bin/g++"
650cfe
 
650cfe
+    if [ -n "$MOZ_PGO" ]; then
650cfe
+        if [ -z "$USE_ARTIFACT" ]; then
650cfe
+            ac_add_options --enable-lto
650cfe
+        fi
650cfe
+        export AR="$topsrcdir/gcc/bin/gcc-ar"
650cfe
+        export NM="$topsrcdir/gcc/bin/gcc-nm"
650cfe
+        export RANLIB="$topsrcdir/gcc/bin/gcc-ranlib"
650cfe
+    fi
650cfe
+
650cfe
     # We want to make sure we use binutils and other binaries in the tooltool
650cfe
     # package.
650cfe
     mk_add_options "export PATH=$MOZ_FETCHES_DIR/gcc/bin:$PATH"
650cfe
diff -up firefox-99.0/extensions/spellcheck/src/moz.build.pgo firefox-99.0/extensions/spellcheck/src/moz.build
650cfe
--- firefox-99.0/extensions/spellcheck/src/moz.build.pgo	2022-03-31 01:24:50.000000000 +0200
650cfe
+++ firefox-99.0/extensions/spellcheck/src/moz.build	2022-04-04 10:15:45.387694143 +0200
650cfe
@@ -28,3 +28,5 @@ EXPORTS.mozilla += [
650cfe
     "mozInlineSpellChecker.h",
650cfe
     "mozSpellChecker.h",
650cfe
 ]
650cfe
+
650cfe
+CXXFLAGS += ['-fno-devirtualize']
650cfe
diff -up firefox-99.0/toolkit/components/terminator/nsTerminator.cpp.pgo firefox-99.0/toolkit/components/terminator/nsTerminator.cpp
650cfe
--- firefox-99.0/toolkit/components/terminator/nsTerminator.cpp.pgo	2022-04-04 10:15:45.387694143 +0200
650cfe
+++ firefox-99.0/toolkit/components/terminator/nsTerminator.cpp	2022-04-04 10:19:07.022239556 +0200
650cfe
@@ -466,6 +466,11 @@ void nsTerminator::StartWatchdog() {
650cfe
   }
650cfe
 #endif
650cfe
 
650cfe
+  // Disable watchdog for PGO train builds - writting profile information at
650cfe
+  // exit may take time and it is better to make build hang rather than
650cfe
+  // silently produce poorly performing binary.
650cfe
+  crashAfterMS = INT32_MAX;
650cfe
+
650cfe
   UniquePtr<Options> options(new Options());
650cfe
   // crashAfterTicks is guaranteed to be > 0 as
650cfe
   // crashAfterMS >= ADDITIONAL_WAIT_BEFORE_CRASH_MS >> HEARTBEAT_INTERVAL_MS