Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Assertion Failure Using -fcs-profile-generate with distributed thin-lto #129736

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

NuriAmari
Copy link
Contributor

When using -fcs-generate-profile with distributed thin-lto in the same fashion we do for local thin-lto, we hit the following assertion:

assert(this->CSAction != CSIRInstr || !this->CSProfileGenFile.empty());

Using local thin-lto with LLD for MachO, we set the missing path automatically to a default value: https://reviews.llvm.org/D151589. In this fix we add the same behavior.

These tests all deal with distributed thin-lto, and while their names
indicate this, it is easier to find them in a common directory.
Copy link

github-actions bot commented Mar 4, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link

github-actions bot commented Mar 4, 2025

⚠️ undef deprecator found issues in your code. ⚠️

You can test this locally with the following command:
git diff -U0 --pickaxe-regex -S '([^a-zA-Z0-9#_-]undef[^a-zA-Z0-9_-]|UndefValue::get)' 6c87ec4f4d083a85ebcfbbda166ad4ba41d5da8d 4ef49b07453f7806ecd594bfaf570c4a6c5bc596 clang/test/CodeGen/distributed-thin-lto/cs-irpgo.c clang/lib/CodeGen/BackendUtil.cpp clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-backend-skip.ll clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi-devirt.ll clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi.ll clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-newpm.ll clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-objc-contract-pass.ll clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-supports-hot-cold-new.ll clang/test/CodeGen/distributed-thin-lto/thinlto-distributed.ll

The following files introduce new uses of undef:

  • clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi-devirt.ll
  • clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi.ll

Undef is now deprecated and should only be used in the rare cases where no replacement is possible. For example, a load of uninitialized memory yields undef. You should use poison values for placeholders instead.

In tests, avoid using undef and having tests that trigger undefined behavior. If you need an operand with some unimportant value, you can add a new argument to the function and use that instead.

For example, this is considered a bad practice:

define void @fn() {
  ...
  br i1 undef, ...
}

Please use the following instead:

define void @fn(i1 %cond) {
  ...
  br i1 %cond, ...
}

Please refer to the Undefined Behavior Manual for more information.

When using -fcs-generate-profile with distributed thin-lto without
setting a profile output path, we hit an assertion constructing
PGOOptions:

https://github.com/llvm/llvm-project/blob/6041c745f32e8fd60ed24e29e7d919d8d1c87ca6/llvm/lib/Support/PGOOptions.cpp#L36

In non distributed thin-lto settings (local thin-lto for example), this
profile output path is automatically set to a default value if
-fcs-generate-profile is passed.

This patch follows suit, using the default output path if one isn't set.
@NuriAmari NuriAmari force-pushed the nuriamari/dthin-lto-cs-irpgo branch from cf1c88c to 4ef49b0 Compare March 4, 2025 17:11
@NuriAmari
Copy link
Contributor Author

The following files introduce new uses of undef

I haven't added new tests, I just moved existing ones. If the change looks otherwise reasonable, I'll try to figure out how to fix these tests.

@NuriAmari
Copy link
Contributor Author

It's not entirely clear to me from the tests that exist if we are doing something wrong by passing flags in the manner shown here, IIUC Google already uses CSIRPGO with distributed thin-lto, but maybe more flags are used there.

@NuriAmari NuriAmari marked this pull request as ready for review March 4, 2025 18:58
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen labels Mar 4, 2025
@NuriAmari NuriAmari changed the title Avoid Assertion Failure Using fcs-profile-generate with distributed thin-lto Avoid Assertion Failure Using -fcs-profile-generate with distributed thin-lto Mar 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 4, 2025

@llvm/pr-subscribers-clang

Author: Nuri Amari (NuriAmari)

Changes

When using -fcs-generate-profile with distributed thin-lto in the same fashion we do for local thin-lto, we hit the following assertion:

assert(this->CSAction != CSIRInstr || !this->CSProfileGenFile.empty());

Using local thin-lto with LLD for MachO, we set the missing path automatically to a default value: https://reviews.llvm.org/D151589. In this fix we add the same behavior.


Full diff: https://github.com/llvm/llvm-project/pull/129736.diff

9 Files Affected:

  • (modified) clang/lib/CodeGen/BackendUtil.cpp (+4-1)
  • (added) clang/test/CodeGen/distributed-thin-lto/cs-irpgo.c (+8)
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-backend-skip.ll (+1-1)
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi-devirt.ll ()
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi.ll ()
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-newpm.ll ()
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-objc-contract-pass.ll ()
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-supports-hot-cold-new.ll ()
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed.ll ()
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 1750719e17670..3a41a412f2a8e 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1334,7 +1334,10 @@ runThinLTOBackend(CompilerInstance &CI, ModuleSummaryIndex *CombinedIndex,
   // Context sensitive profile.
   if (CGOpts.hasProfileCSIRInstr()) {
     Conf.RunCSIRInstr = true;
-    Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
+    Conf.CSIRProfile = CGOpts.InstrProfileOutput.empty()
+                           ? getDefaultProfileGenName()
+                           : std::move(CGOpts.InstrProfileOutput);
+
   } else if (CGOpts.hasProfileCSIRUse()) {
     Conf.RunCSIRInstr = false;
     Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
diff --git a/clang/test/CodeGen/distributed-thin-lto/cs-irpgo.c b/clang/test/CodeGen/distributed-thin-lto/cs-irpgo.c
new file mode 100644
index 0000000000000..28fc8d3519968
--- /dev/null
+++ b/clang/test/CodeGen/distributed-thin-lto/cs-irpgo.c
@@ -0,0 +1,8 @@
+// RUN: mkdir -p %t
+// RUN: %clang -target x86_64-apple-darwin -flto=thin %s -fcs-profile-generate -c -o %t/main.bc
+// RUN: llvm-lto2 run --thinlto-distributed-indexes %t/main.bc -r=%t/main.bc,_main,px -o %t/index
+// RUN: %clang -target x86_64-apple-darwin -fcs-profile-generate -fthinlto-index=%t/main.bc.thinlto.bc %t/main.bc -c -o main.o
+
+int main() {
+    return 0;
+}
diff --git a/clang/test/CodeGen/thinlto-distributed-backend-skip.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-backend-skip.ll
similarity index 87%
rename from clang/test/CodeGen/thinlto-distributed-backend-skip.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-backend-skip.ll
index d7b8225ee2693..62aa8aa8e7dc4 100644
--- a/clang/test/CodeGen/thinlto-distributed-backend-skip.ll
+++ b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-backend-skip.ll
@@ -6,7 +6,7 @@
 ; RUN: opt -thinlto-bc -o %t.o %s
 
 ; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \
-; RUN:   -fthinlto-index=%S/Inputs/thinlto-distributed-backend-skip.bc \
+; RUN:   -fthinlto-index=%S/../Inputs/thinlto-distributed-backend-skip.bc \
 ; RUN:   -emit-llvm -o - -x ir %t.o | FileCheck %s
 
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
diff --git a/clang/test/CodeGen/thinlto-distributed-cfi-devirt.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi-devirt.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed-cfi-devirt.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi-devirt.ll
diff --git a/clang/test/CodeGen/thinlto-distributed-cfi.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed-cfi.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi.ll
diff --git a/clang/test/CodeGen/thinlto-distributed-newpm.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-newpm.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed-newpm.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-newpm.ll
diff --git a/clang/test/CodeGen/thinlto-distributed-objc-contract-pass.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-objc-contract-pass.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed-objc-contract-pass.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-objc-contract-pass.ll
diff --git a/clang/test/CodeGen/thinlto-distributed-supports-hot-cold-new.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-supports-hot-cold-new.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed-supports-hot-cold-new.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-supports-hot-cold-new.ll
diff --git a/clang/test/CodeGen/thinlto-distributed.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed.ll

@llvmbot
Copy link
Member

llvmbot commented Mar 4, 2025

@llvm/pr-subscribers-clang-codegen

Author: Nuri Amari (NuriAmari)

Changes

When using -fcs-generate-profile with distributed thin-lto in the same fashion we do for local thin-lto, we hit the following assertion:

assert(this->CSAction != CSIRInstr || !this->CSProfileGenFile.empty());

Using local thin-lto with LLD for MachO, we set the missing path automatically to a default value: https://reviews.llvm.org/D151589. In this fix we add the same behavior.


Full diff: https://github.com/llvm/llvm-project/pull/129736.diff

9 Files Affected:

  • (modified) clang/lib/CodeGen/BackendUtil.cpp (+4-1)
  • (added) clang/test/CodeGen/distributed-thin-lto/cs-irpgo.c (+8)
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-backend-skip.ll (+1-1)
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi-devirt.ll ()
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi.ll ()
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-newpm.ll ()
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-objc-contract-pass.ll ()
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-supports-hot-cold-new.ll ()
  • (renamed) clang/test/CodeGen/distributed-thin-lto/thinlto-distributed.ll ()
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 1750719e17670..3a41a412f2a8e 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1334,7 +1334,10 @@ runThinLTOBackend(CompilerInstance &CI, ModuleSummaryIndex *CombinedIndex,
   // Context sensitive profile.
   if (CGOpts.hasProfileCSIRInstr()) {
     Conf.RunCSIRInstr = true;
-    Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
+    Conf.CSIRProfile = CGOpts.InstrProfileOutput.empty()
+                           ? getDefaultProfileGenName()
+                           : std::move(CGOpts.InstrProfileOutput);
+
   } else if (CGOpts.hasProfileCSIRUse()) {
     Conf.RunCSIRInstr = false;
     Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
diff --git a/clang/test/CodeGen/distributed-thin-lto/cs-irpgo.c b/clang/test/CodeGen/distributed-thin-lto/cs-irpgo.c
new file mode 100644
index 0000000000000..28fc8d3519968
--- /dev/null
+++ b/clang/test/CodeGen/distributed-thin-lto/cs-irpgo.c
@@ -0,0 +1,8 @@
+// RUN: mkdir -p %t
+// RUN: %clang -target x86_64-apple-darwin -flto=thin %s -fcs-profile-generate -c -o %t/main.bc
+// RUN: llvm-lto2 run --thinlto-distributed-indexes %t/main.bc -r=%t/main.bc,_main,px -o %t/index
+// RUN: %clang -target x86_64-apple-darwin -fcs-profile-generate -fthinlto-index=%t/main.bc.thinlto.bc %t/main.bc -c -o main.o
+
+int main() {
+    return 0;
+}
diff --git a/clang/test/CodeGen/thinlto-distributed-backend-skip.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-backend-skip.ll
similarity index 87%
rename from clang/test/CodeGen/thinlto-distributed-backend-skip.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-backend-skip.ll
index d7b8225ee2693..62aa8aa8e7dc4 100644
--- a/clang/test/CodeGen/thinlto-distributed-backend-skip.ll
+++ b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-backend-skip.ll
@@ -6,7 +6,7 @@
 ; RUN: opt -thinlto-bc -o %t.o %s
 
 ; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \
-; RUN:   -fthinlto-index=%S/Inputs/thinlto-distributed-backend-skip.bc \
+; RUN:   -fthinlto-index=%S/../Inputs/thinlto-distributed-backend-skip.bc \
 ; RUN:   -emit-llvm -o - -x ir %t.o | FileCheck %s
 
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
diff --git a/clang/test/CodeGen/thinlto-distributed-cfi-devirt.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi-devirt.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed-cfi-devirt.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi-devirt.ll
diff --git a/clang/test/CodeGen/thinlto-distributed-cfi.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed-cfi.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-cfi.ll
diff --git a/clang/test/CodeGen/thinlto-distributed-newpm.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-newpm.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed-newpm.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-newpm.ll
diff --git a/clang/test/CodeGen/thinlto-distributed-objc-contract-pass.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-objc-contract-pass.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed-objc-contract-pass.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-objc-contract-pass.ll
diff --git a/clang/test/CodeGen/thinlto-distributed-supports-hot-cold-new.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-supports-hot-cold-new.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed-supports-hot-cold-new.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed-supports-hot-cold-new.ll
diff --git a/clang/test/CodeGen/thinlto-distributed.ll b/clang/test/CodeGen/distributed-thin-lto/thinlto-distributed.ll
similarity index 100%
rename from clang/test/CodeGen/thinlto-distributed.ll
rename to clang/test/CodeGen/distributed-thin-lto/thinlto-distributed.ll

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the thinlto-distributed from the filenames since the directory is named distributed-thin-lto?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Comment on lines +1337 to +1340
Conf.CSIRProfile = CGOpts.InstrProfileOutput.empty()
? getDefaultProfileGenName()
: std::move(CGOpts.InstrProfileOutput);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we use getProfileGenName() now.

Suggested change
Conf.CSIRProfile = CGOpts.InstrProfileOutput.empty()
? getDefaultProfileGenName()
: std::move(CGOpts.InstrProfileOutput);
Conf.CSIRProfile = getProfileGenName(CGOpts)

Copy link
Contributor

@teresajohnson teresajohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm with the change suggested by @ellishg to use getProfileGenName. I believe we always specify a profile filename using the = form of the option, which is why we have not encountered this issue.

@@ -6,7 +6,7 @@
; RUN: opt -thinlto-bc -o %t.o %s

; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \
; RUN: -fthinlto-index=%S/Inputs/thinlto-distributed-backend-skip.bc \
; RUN: -fthinlto-index=%S/../Inputs/thinlto-distributed-backend-skip.bc \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants