Skip to content

[MergeFunc] Keep comdat on new function, not thunk. #130583

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

Merged
merged 1 commit into from
Mar 10, 2025

Conversation

fhahn
Copy link
Contributor

@fhahn fhahn commented Mar 10, 2025

MergeFunc uses the original function F as Thunk and creates a new function NewF for the original function F. Preserve F's comdat info on NewF instead of the thunk.

This fixes a crash when trying to lower comdat on the thunk during codegen.

MergeFunc uses the original function F as Thunk and creates a new
function NewF for the original function F. Preserve F's comdat info
on NewF instead of the thunk.

This fixes a crash when trying to lower comdat on the thunk during
codegen.
@llvmbot
Copy link
Member

llvmbot commented Mar 10, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Florian Hahn (fhahn)

Changes

MergeFunc uses the original function F as Thunk and creates a new function NewF for the original function F. Preserve F's comdat info on NewF instead of the thunk.

This fixes a crash when trying to lower comdat on the thunk during codegen.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/MergeFunctions.cpp (+2)
  • (modified) llvm/test/Transforms/MergeFunc/comdat.ll (+6-4)
diff --git a/llvm/lib/Transforms/IPO/MergeFunctions.cpp b/llvm/lib/Transforms/IPO/MergeFunctions.cpp
index d2443a83535e4..c463cda68d4b5 100644
--- a/llvm/lib/Transforms/IPO/MergeFunctions.cpp
+++ b/llvm/lib/Transforms/IPO/MergeFunctions.cpp
@@ -923,6 +923,8 @@ void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
                                       F->getAddressSpace(), "", F->getParent());
     NewF->copyAttributesFrom(F);
     NewF->takeName(F);
+    NewF->setComdat(F->getComdat());
+    F->setComdat(nullptr);
     NewF->IsNewDbgInfoFormat = F->IsNewDbgInfoFormat;
     // Ensure CFI type metadata is propagated to the new function.
     copyMetadataIfPresent(F, NewF, "type");
diff --git a/llvm/test/Transforms/MergeFunc/comdat.ll b/llvm/test/Transforms/MergeFunc/comdat.ll
index 007edaf7b36d2..ba887f86f7412 100644
--- a/llvm/test/Transforms/MergeFunc/comdat.ll
+++ b/llvm/test/Transforms/MergeFunc/comdat.ll
@@ -1,9 +1,11 @@
 ; RUN: opt -S -passes=mergefunc %s | FileCheck %s
 
+target triple = "x86_64-unknown-windows-msvc19.42.34436"
+
 @symbols = linkonce_odr global <{ ptr, ptr }> <{ ptr @f, ptr @g }>
 
-$f = comdat any
 $g = comdat any
+$f = comdat any
 
 define linkonce_odr hidden i32 @f(i32 %x, i32 %y) comdat {
   %sum = add i32 %x, %y
@@ -19,14 +21,14 @@ define linkonce_odr hidden i32 @g(i32 %x, i32 %y) comdat {
   ret i32 %sum3
 }
 
-; CHECK: $f = comdat any
 ; CHECK: $g = comdat any
+; CHECK: $f = comdat any
 
 ;.
 ; CHECK: @symbols = linkonce_odr global <{ ptr, ptr }> <{ ptr @f, ptr @g }>
 ;.
 ; CHECK-LABEL: define private i32 @0(
-; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) comdat($f) {
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
 ; CHECK-NEXT:    [[SUM:%.*]] = add i32 [[X]], [[Y]]
 ; CHECK-NEXT:    [[SUM2:%.*]] = add i32 [[X]], [[SUM]]
 ; CHECK-NEXT:    [[SUM3:%.*]] = add i32 [[X]], [[SUM]]
@@ -40,7 +42,7 @@ define linkonce_odr hidden i32 @g(i32 %x, i32 %y) comdat {
 ;
 ;
 ; CHECK-LABEL: define linkonce_odr hidden i32 @f(
-; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) comdat {
 ; CHECK-NEXT:    [[TMP3:%.*]] = tail call i32 @[[GLOB0]](i32 [[TMP0]], i32 [[TMP1]])
 ; CHECK-NEXT:    ret i32 [[TMP3]]
 ;

@fhahn fhahn merged commit c7f7ac7 into llvm:main Mar 10, 2025
13 checks passed
@fhahn fhahn deleted the mergefunc-comdat-thunk branch March 23, 2025 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants