-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[MemProf] Don't mutate the function type when calling clone #147829
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
teresajohnson
merged 3 commits into
llvm:main
from
teresajohnson:memprof_call_update_fix
Jul 11, 2025
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| ;; Test to ensure the call updated to call a clone does not mutate the callee | ||
snehasish marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ;; function type. In rare cases we may end up with a callee declaration that | ||
| ;; does not match the call type, because it was imported from a different | ||
| ;; module with an incomplete return type (in which case clang gives it a void | ||
| ;; return type). | ||
|
|
||
| ; RUN: rm -rf %t && split-file %s %t && cd %t | ||
snehasish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ; RUN: llvm-as src.ll -o src.o | ||
| ; RUN: llvm-as src.o.thinlto.ll -o src.o.thinlto.bc | ||
|
|
||
snehasish marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ; RUN: opt -passes=memprof-context-disambiguation src.o -S -memprof-import-summary=src.o.thinlto.bc | FileCheck %s | ||
|
|
||
| ;--- src.ll | ||
| ; ModuleID = 'src.o' | ||
| source_filename = "src.c" | ||
| 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" | ||
| target triple = "x86_64-unknown-linux-gnu" | ||
|
|
||
| define i32 @main(ptr %b) { | ||
| entry: | ||
| ;; This call is not changed as the summary specifies clone 0. | ||
| ; CHECK: call ptr @_Z3foov() | ||
| %call = call ptr @_Z3foov(), !callsite !5 | ||
| ;; After changing this call to call a clone, the function type should still | ||
| ;; be ptr, despite the void on the callee declaration. | ||
| ; CHECK: call ptr @_Z3foov.memprof.1() | ||
| %call1 = call ptr @_Z3foov(), !callsite !6 | ||
| %0 = load ptr, ptr %b, align 8 | ||
| ;; Although the summary indicates this should call clone 1, and the VP | ||
| ;; metadata indicates the callee is _Z3foov, it is not updated because | ||
| ;; the ICP facility requires the function types to match. | ||
| ; CHECK: call ptr %0() | ||
| %call2 = call ptr %0(), !prof !7, !callsite !8 | ||
| ret i32 0 | ||
| } | ||
|
|
||
| ;; Both the original callee function declaration and its clone have void return | ||
| ;; type. | ||
| ; CHECK: declare void @_Z3foov() | ||
| ; CHECK: declare void @_Z3foov.memprof.1() | ||
| declare void @_Z3foov() | ||
|
|
||
| !llvm.dbg.cu = !{!0} | ||
| !llvm.module.flags = !{!2, !3, !4} | ||
|
|
||
| !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) | ||
| !1 = !DIFile(filename: "src.cc", directory: "", checksumkind: CSK_MD5, checksum: "8636c46e81402013b9d54e8307d2f149") | ||
| !2 = !{i32 7, !"Dwarf Version", i32 5} | ||
| !3 = !{i32 2, !"Debug Info Version", i32 3} | ||
| !4 = !{i32 1, !"EnableSplitLTOUnit", i32 0} | ||
| !5 = !{i64 8632435727821051414} | ||
| !6 = !{i64 -3421689549917153178} | ||
| !7 = !{!"VP", i32 0, i64 4, i64 9191153033785521275, i64 4} | ||
| !8 = !{i64 1234} | ||
|
|
||
| ;--- src.o.thinlto.ll | ||
| ; ModuleID = 'src.o.thinlto.bc' | ||
| source_filename = "src.o.thinlto.bc" | ||
|
|
||
| ^0 = module: (path: "src.o", hash: (2823430083, 3994560862, 899296057, 1055405378, 2961356784)) | ||
| ^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)))))) | ||
| ^2 = flags: 353 | ||
| ^3 = blockcount: 0 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to assert that the type of the clone is the same as the original function here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started to add such an assert, however, it seems unnecessary to me as we create NewF from the function type of CalledFunction just a few lines above here. However, looking at this made me concerned about the possibility of a different issue. Where we get CalledFunction we try various ways to find the function from the CallBase if getCalledFunction returns null. One of those things is to invoke stripPointerCasts. Now if we had a pointer cast here it would not be valid (pre-opaque pointers) to simply replace the called operand. But in theory, with opaque pointers, we should not actually find any pointer casts to strip. For testing, I added some checks in that code, as well as in the module summary analysis which does the same thing for all ThinLTO bitcode generation and kicks in much more broadly, and never found (with the regression tests or with a large app) any where it found anything to actually strip. But out of an abundance of caution I have added some handling above here to make sure the called function is the called operand (or its aliasee), and skip rewriting the call otherwise (also incrementing a new statistic in case we want to watch for cases this actually doing anything). In theory we could remove the stripPointerCasts call, but that needs to stay in sync with module summary analysis, so that's a change that could be considered later.