Skip to content

Commit ceca3b4

Browse files
committed
[MemProf] Don't mutate the function type when calling clone
In rare cases the declaration of a function may not match its callsite after function importing, when the declaration was imported from a module where the function had void return type (presumably due to incomplete types). Instead of using setCalledFunction() to change a call to call its clone, which updates the call's function type as well, just call setCalledOperand directly so the only thing changed is the function target. Note this can't happen for the other places where we call setCalledFunction: FullLTO requires the cloned callee to be defined in the same FullLTO merged module; ThinLTO memprof ICP calls an ICP facility to first perform the promotion and that will be blocked if the function type doesn't match the callsite (the new test explicitly tests this latter case).
1 parent 6f291cb commit ceca3b4

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5173,7 +5173,13 @@ bool MemProfContextDisambiguation::applyImport(Module &M) {
51735173
CBClone = CB;
51745174
else
51755175
CBClone = cast<CallBase>((*VMaps[J - 1])[CB]);
5176-
CBClone->setCalledFunction(NewF);
5176+
// Set the called operand directly instead of calling setCalledFunction,
5177+
// as the latter mutates the function type on the call. In rare cases
5178+
// we may have a slightly different type on a callee function
5179+
// declaration due to it being imported from a different module with
5180+
// incomplete types. We really just want to change the name of the
5181+
// function to the clone, and not make any type changes.
5182+
CBClone->setCalledOperand(NewF.getCallee());
51775183
ORE.emit(OptimizationRemark(DEBUG_TYPE, "MemprofCall", CBClone)
51785184
<< ore::NV("Call", CBClone) << " in clone "
51795185
<< ore::NV("Caller", CBClone->getFunction())
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
;; Test to ensure the call updated to call a clone does not mutate the callee
2+
;; function type. In rare cases we may end up with a callee declaration that
3+
;; does not match the call type, because it was imported from a different
4+
;; module with an incomplete return type (in which case clang gives it a void
5+
;; return type).
6+
7+
; RUN: rm -rf %t && split-file %s %t && cd %t
8+
; RUN: llvm-as src.ll -o src.o
9+
; RUN: llvm-as src.o.thinlto.ll -o src.o.thinlto.bc
10+
11+
; RUN: opt -passes=memprof-context-disambiguation src.o -S -memprof-import-summary=src.o.thinlto.bc | FileCheck %s
12+
13+
;--- src.ll
14+
; ModuleID = 'src.o'
15+
source_filename = "src.c"
16+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
17+
target triple = "x86_64-unknown-linux-gnu"
18+
19+
define i32 @main(ptr %b) {
20+
entry:
21+
;; This call is not changed as the summary specifies clone 0.
22+
; CHECK: call ptr @_Z3foov()
23+
%call = call ptr @_Z3foov(), !callsite !5
24+
;; After changing this call to call a clone, the function type should still
25+
;; be ptr, despite the void on the callee declaration.
26+
; CHECK: call ptr @_Z3foov.memprof.1()
27+
%call1 = call ptr @_Z3foov(), !callsite !6
28+
%0 = load ptr, ptr %b, align 8
29+
;; Although the summary indicates this should call clone 1, and the VP
30+
;; metadata indicates the callee is _Z3foov, it is not updated because
31+
;; the ICP facility requires the function types to match.
32+
; CHECK: call ptr %0()
33+
%call2 = call ptr %0(), !prof !7, !callsite !8
34+
ret i32 0
35+
}
36+
37+
;; Both the original callee function declaration and its clone have void return
38+
;; type.
39+
; CHECK: declare void @_Z3foov()
40+
; CHECK: declare void @_Z3foov.memprof.1()
41+
declare void @_Z3foov()
42+
43+
!llvm.dbg.cu = !{!0}
44+
!llvm.module.flags = !{!2, !3, !4}
45+
46+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 21.0.0git ([email protected]:llvm/llvm-project.git e391301e0e4d9183fe06e69602e87b0bc889aeda)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
47+
!1 = !DIFile(filename: "src.cc", directory: "", checksumkind: CSK_MD5, checksum: "8636c46e81402013b9d54e8307d2f149")
48+
!2 = !{i32 7, !"Dwarf Version", i32 5}
49+
!3 = !{i32 2, !"Debug Info Version", i32 3}
50+
!4 = !{i32 1, !"EnableSplitLTOUnit", i32 0}
51+
!5 = !{i64 8632435727821051414}
52+
!6 = !{i64 -3421689549917153178}
53+
!7 = !{!"VP", i32 0, i64 4, i64 9191153033785521275, i64 4}
54+
!8 = !{i64 1234}
55+
56+
;--- src.o.thinlto.ll
57+
; ModuleID = 'src.o.thinlto.bc'
58+
source_filename = "src.o.thinlto.bc"
59+
60+
^0 = module: (path: "src.o", hash: (2823430083, 3994560862, 899296057, 1055405378, 2961356784))
61+
^1 = gv: (guid: 15822663052811949562, summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 0, live: 1, dsoLocal: 1, canAutoHide: 0, importType: definition), insts: 3, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0, noUnwind: 0, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0), callsites: ((callee: null, clones: (0), stackIds: (8632435727821051414)), (callee: null, clones: (1), stackIds: (15025054523792398438)), (callee: null, clones: (1), stackIds: (1234))))))
62+
^2 = flags: 353
63+
^3 = blockcount: 0

0 commit comments

Comments
 (0)