[compiler-rt] [Profile] Disable continuous mode when reset to default.profraw due to malformed LLVM_PROFILE_FILE. (PR #74879)

Zequan Wu via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 8 10:43:31 PST 2023


https://github.com/ZequanWu updated https://github.com/llvm/llvm-project/pull/74879

>From 1212429ba57c60388f59311a2e52700d2d2dfdd7 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Fri, 8 Dec 2023 13:30:48 -0500
Subject: [PATCH 1/3] [Profile] Disable continuous mode when reset to
 default.profraw due to malformed LLVM_PROFILE_FILE.

---
 compiler-rt/lib/profile/InstrProfiling.h      |  6 ++++++
 .../lib/profile/InstrProfilingBuffer.c        |  4 ++++
 compiler-rt/lib/profile/InstrProfilingFile.c  |  1 +
 .../reset-default-profile.c                   | 21 +++++++++++++++++++
 4 files changed, 32 insertions(+)
 create mode 100644 compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c

diff --git a/compiler-rt/lib/profile/InstrProfiling.h b/compiler-rt/lib/profile/InstrProfiling.h
index c5b0b34f2d8af0..813545a29a8e86 100644
--- a/compiler-rt/lib/profile/InstrProfiling.h
+++ b/compiler-rt/lib/profile/InstrProfiling.h
@@ -54,6 +54,12 @@ int __llvm_profile_is_continuous_mode_enabled(void);
  */
 void __llvm_profile_enable_continuous_mode(void);
 
+/*!
+ * \brief Enable continuous mode.
+ *
+ */
+void __llvm_profile_disable_continuous_mode(void);
+
 /*!
  * \brief Set the page size.
  *
diff --git a/compiler-rt/lib/profile/InstrProfilingBuffer.c b/compiler-rt/lib/profile/InstrProfilingBuffer.c
index cd1f067bd188e4..5657bf5bacf225 100644
--- a/compiler-rt/lib/profile/InstrProfilingBuffer.c
+++ b/compiler-rt/lib/profile/InstrProfilingBuffer.c
@@ -33,6 +33,10 @@ COMPILER_RT_VISIBILITY void __llvm_profile_enable_continuous_mode(void) {
   ContinuouslySyncProfile = 1;
 }
 
+void __llvm_profile_disable_continuous_mode(void) {
+  ContinuouslySyncProfile = 0;
+}
+
 COMPILER_RT_VISIBILITY void __llvm_profile_set_page_size(unsigned PS) {
   PageSize = PS;
 }
diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c
index 1685b30b9492a6..4fea835f3332da 100644
--- a/compiler-rt/lib/profile/InstrProfilingFile.c
+++ b/compiler-rt/lib/profile/InstrProfilingFile.c
@@ -711,6 +711,7 @@ static void resetFilenameToDefault(void) {
   memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
   lprofCurFilename.FilenamePat = DefaultProfileName;
   lprofCurFilename.PNS = PNS_default;
+  __llvm_profile_disable_continuous_mode();
 }
 
 static unsigned getMergePoolSize(const char *FilenamePat, int *I) {
diff --git a/compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c b/compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c
new file mode 100644
index 00000000000000..f09ab40f4b0352
--- /dev/null
+++ b/compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c
@@ -0,0 +1,21 @@
+// REQUIRES: darwin || linux
+
+// Test when LLVM_PROFILE_FILE is set incorrectly, it should fall backs to use default.profraw without runtime error.
+
+// Create & cd into a temporary directory.
+// RUN: rm -rf %t.dir && mkdir -p %t.dir && cd %t.dir
+// RUN: %clang -fprofile-instr-generate -fcoverage-mapping -mllvm -runtime-counter-relocation=true -o %t.exe %s
+// RUN: env LLVM_PROFILE_FILE="incorrect-profile-name%m%c%c.profraw" %run %t.exe
+// RUN: ls -l default.profraw | FileCheck %s
+
+// CHECK:     default.profraw
+// CEHCK-NOT: incorrect-profile-name.profraw
+
+#include <stdio.h>
+int f() { return 0; }
+
+int main(int argc, char **argv) {
+  FILE* File = fopen("default.profraw", "w");
+  f();
+  return 0;
+}

>From 2f9ed125cedfb2e0e9390bd7a74e0106a3a9b2c6 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Fri, 8 Dec 2023 13:42:45 -0500
Subject: [PATCH 2/3] fixup! [Profile] Disable continuous mode when reset to
 default.profraw due to malformed LLVM_PROFILE_FILE.

---
 compiler-rt/lib/profile/InstrProfiling.h                        | 2 +-
 .../test/profile/ContinuousSyncMode/reset-default-profile.c     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/profile/InstrProfiling.h b/compiler-rt/lib/profile/InstrProfiling.h
index 813545a29a8e86..137115996748ce 100644
--- a/compiler-rt/lib/profile/InstrProfiling.h
+++ b/compiler-rt/lib/profile/InstrProfiling.h
@@ -55,7 +55,7 @@ int __llvm_profile_is_continuous_mode_enabled(void);
 void __llvm_profile_enable_continuous_mode(void);
 
 /*!
- * \brief Enable continuous mode.
+ * \brief Disable continuous mode.
  *
  */
 void __llvm_profile_disable_continuous_mode(void);
diff --git a/compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c b/compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c
index f09ab40f4b0352..43365de097dc19 100644
--- a/compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c
+++ b/compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c
@@ -15,7 +15,7 @@
 int f() { return 0; }
 
 int main(int argc, char **argv) {
-  FILE* File = fopen("default.profraw", "w");
+  FILE *File = fopen("default.profraw", "w");
   f();
   return 0;
 }

>From a35a7cec31a725820a5d65b81cd0ea378f5a2280 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Fri, 8 Dec 2023 13:43:18 -0500
Subject: [PATCH 3/3] fixup! fixup! [Profile] Disable continuous mode when
 reset to default.profraw due to malformed LLVM_PROFILE_FILE.

---
 .../test/profile/ContinuousSyncMode/reset-default-profile.c     | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c b/compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c
index 43365de097dc19..e979afba214801 100644
--- a/compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c
+++ b/compiler-rt/test/profile/ContinuousSyncMode/reset-default-profile.c
@@ -9,7 +9,7 @@
 // RUN: ls -l default.profraw | FileCheck %s
 
 // CHECK:     default.profraw
-// CEHCK-NOT: incorrect-profile-name.profraw
+// CHECK-NOT: incorrect-profile-name.profraw
 
 #include <stdio.h>
 int f() { return 0; }



More information about the llvm-commits mailing list