Skip to content

Commit c7f7ac7

Browse files
authored
[MergeFunc] Keep comdat on new function, not thunk. (#130583)
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.
1 parent 22f7897 commit c7f7ac7

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

llvm/lib/Transforms/IPO/MergeFunctions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,8 @@ void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
923923
F->getAddressSpace(), "", F->getParent());
924924
NewF->copyAttributesFrom(F);
925925
NewF->takeName(F);
926+
NewF->setComdat(F->getComdat());
927+
F->setComdat(nullptr);
926928
NewF->IsNewDbgInfoFormat = F->IsNewDbgInfoFormat;
927929
// Ensure CFI type metadata is propagated to the new function.
928930
copyMetadataIfPresent(F, NewF, "type");

llvm/test/Transforms/MergeFunc/comdat.ll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
; RUN: opt -S -passes=mergefunc %s | FileCheck %s
22

3+
target triple = "x86_64-unknown-windows-msvc19.42.34436"
4+
35
@symbols = linkonce_odr global <{ ptr, ptr }> <{ ptr @f, ptr @g }>
46

5-
$f = comdat any
67
$g = comdat any
8+
$f = comdat any
79

810
define linkonce_odr hidden i32 @f(i32 %x, i32 %y) comdat {
911
%sum = add i32 %x, %y
@@ -19,14 +21,14 @@ define linkonce_odr hidden i32 @g(i32 %x, i32 %y) comdat {
1921
ret i32 %sum3
2022
}
2123

22-
; CHECK: $f = comdat any
2324
; CHECK: $g = comdat any
25+
; CHECK: $f = comdat any
2426

2527
;.
2628
; CHECK: @symbols = linkonce_odr global <{ ptr, ptr }> <{ ptr @f, ptr @g }>
2729
;.
2830
; CHECK-LABEL: define private i32 @0(
29-
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) comdat($f) {
31+
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
3032
; CHECK-NEXT: [[SUM:%.*]] = add i32 [[X]], [[Y]]
3133
; CHECK-NEXT: [[SUM2:%.*]] = add i32 [[X]], [[SUM]]
3234
; CHECK-NEXT: [[SUM3:%.*]] = add i32 [[X]], [[SUM]]
@@ -40,7 +42,7 @@ define linkonce_odr hidden i32 @g(i32 %x, i32 %y) comdat {
4042
;
4143
;
4244
; CHECK-LABEL: define linkonce_odr hidden i32 @f(
43-
; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
45+
; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) comdat {
4446
; CHECK-NEXT: [[TMP3:%.*]] = tail call i32 @[[GLOB0]](i32 [[TMP0]], i32 [[TMP1]])
4547
; CHECK-NEXT: ret i32 [[TMP3]]
4648
;

0 commit comments

Comments
 (0)