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