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