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