aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/CloneFunction.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-07-16[DebugInfo] Strip more debug-intrinsic code from local utils (#149037)Jeremy Morse1-25/+5
SROA and a few other facilities use generic-lambdas and some overloaded functions to deal with both intrinsics and debug-records at the same time. As part of stripping out intrinsic support, delete a swathe of this code from things in the Utils directory. This is a large diff, but is mostly about removing functions that were duplicated during the migration to debug records. I've taken a few opportunities to replace comments about "intrinsics" with "records", and replace generic lambdas with plain lambdas (I believe this makes it more readable). All of this is chipping away at intrinsic-specific code until we get to removing parts of findDbgUsers, which is the final boss -- we can't remove that until almost everything else is gone.
2025-06-12Follow-up to 97ac6483aae, squelch an unused lambda capture warningJeremy Morse1-1/+1
NewBB here was being captured for some code that was deleted in 97ac6483aae, and that leads to some warnings on some compilers.
2025-06-12[DebugInfo][RemoveDIs] Delete debug-info-format flag (#143746)Jeremy Morse1-7/+0
This flag was used to let us incrementally introduce debug records into LLVM, however everything is now using records. It serves no purpose now, so delete it.
2025-05-14[CloneFunction][DebugInfo] Ensure DILocalVariables of inlined functions are ↵Vladislav Dzhidzhoev1-2/+13
not cloned (NFC) (#138590) This change was separated from https://github.com/llvm/llvm-project/pull/119001. When cloning functions, use IdentityMDPredicate to ensure that if DISubprogram is not cloned, then its DILocalVariables are not cloned either. This is currently expected to be an NFC, as DILocalVariables only reference their subprograms (via DILocalScopes) and types, and inlined DISubprograms and DITypes are not cloned. Thus, DILocalVariables are mapped to self in ValueMapper (in mapTopLevelUniquedNode). However, it will be needed for the original PR https://github.com/llvm/llvm-project/pull/119001, where a DILocalVariable may refer to a local type of a DISubprogram that is being cloned. In that case, ValueMapper will clone DILocalVariable even if it belongs to an inlined DISubprogram that is not cloned, which should be avoided. I'm making this change into a separate PR to make the original PR a bit smaller, and because this has more to do with variables than with types.
2025-05-10[Utils] Remove redundant calls to std::unique_ptr<T>::get (NFC) (#139352)Kazu Hirata1-1/+1
2025-05-06[NFC][KeyInstr] Add Atom Group (re)mapping (#133479)Orlando Cazalet-Hyams1-1/+28
Add: mapAtomInstance - map the atom group number to a new group. RemapSourceAtom - apply the mapped atom group number to this instruction. Modify: CloneBasicBlock - Call mapAtomInstance on cloned instruction's DebugLocs if MapAtoms is true (default). Setting to false could lead to a degraded debugging experience. See code comment. Optimisations like loop unroll that duplicate instructions need to remap source atom groups so that each duplicated source construct instance is considered distinct when determining is_stmt locations. This commit adds the remapping functionality and a unittest. RFC: https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668
2025-04-09[DebugInfo][Inline] Propagate source locs when simplifying cond branches ↵Stephen Tozer1-2/+6
(#134827) During inlining, we may opportunistically simplify conditional branches (incl. switches) to unconditional branches if, after inlining, their destination is fixed. While we do this, we should propagate any DILocation attached to the original branch to the simplified branch, which this patch enables. Found using https://github.com/llvm/llvm-project/pull/107279.
2025-04-03CloneFunction: Do not delete blocks with address taken (#134209)Matt Arsenault1-1/+1
If a block with a single predecessor also had its address taken, it was getting deleted in this post-inline cleanup step. This would result in the blockaddress in the resulting function getting deleted and replaced with inttoptr 1. This fixes one bug required to permit inlining of functions with blockaddress uses. At the moment this is not testable (at least without an annoyingly complex unit test), and is a pre-bug fix for future patches. Functions with blockaddress uses are rejected in isInlineViable, so we don't get this far with the current InlineFunction uses (some of the existing cases seem to reproduce this part of the rejection logic, like PartialInliner). This will be tested in a pending llvm-reduce change. Prerequisite for #38908
2025-03-23[Transforms] Use *Set::insert_range (NFC) (#132652)Kazu Hirata1-3/+1
We can use *Set::insert_range to collapse: for (auto Elem : Range) Set.insert(E); down to: Set.insert_range(Range); In some cases, we can further fold that into the set declaration.
2025-03-16[NFC][Cloning] Move DebugInfoFinder decl closer to its place of usage (#129154)Artem Pianykh1-3/+2
Summary: This makes it clear that DIFinder is only really necessary for llvm.dbg.cu update. Test Plan: ninja check-llvm-unit
2025-03-15[NFC][Cloning] Clean up comments in CloneFunctionInto (#129153)Artem Pianykh1-11/+0
Summary: Some comments no longer make sense nor refer to an existing code path. Test Plan: ninja check-llvm-unit
2025-03-15[NFC][Cloning] Remove now unused CollectDebugInfoForCloning (#129152)Artem Pianykh1-21/+0
Summary: This function is no longer used, let's remove it from the header and impl. Test Plan: ninja check-llvm-unit
2025-03-15[NFC][Cloning] Remove now unused FindDebugInfoToIdentityMap (#129151)Artem Pianykh1-34/+0
Summary: This function is no longer needed. Test Plan: ninja check-llvm-unit
2025-03-13[NFC][Cloning] Replace DIFinder usage in CloneFunctionInto with a ↵Artem Pianykh1-7/+25
MetadataPredicate (#129148) Summary: The new code should be functionally identical to the old one (but faster). The reasoning is as follows. In the old code when cloning within the module: 1. DIFinder traverses and collects *all* debug info reachable from a function, its instructions, and its owning compile unit. 2. Then "compile units, types, other subprograms, and lexical blocks of other subprograms" are saved in a set. 3. Then when we MapMetadata, we traverse the function's debug info _again_ and those nodes that are in the set from p.2 are identity mapped. This looks equivalent to just doing step 3 with identity mapping based on a predicate that says to identity map "compile units, types, other subprograms, and lexical blocks of other subprograms" (same as in step 2). This is what the new code does. Test Plan: ninja check-all There's a bunch of tests around cloning and all of them pass.
2025-03-12[NFC][Cloning] Replace IdentityMD set with a predicate in ValueMapper (#129147)Artem Pianykh1-4/+6
Summary: We used the set only to check if it contains certain metadata nodes. Replacing the set with a predicate makes the intention clearer and the API more general. Test Plan: ninja check-all
2025-03-12[NFC][Cloning] Make DifferentModule case more obvious in ↵Artem Pianykh1-2/+8
CollectDebugInfoForCloning (#129146) Summary: This should be behaviorally equivalent. DIFinder is only used when cloning into a DifferentModule as part of llvm.dbg.cu update in CloneFunctionInto. Test Plan: ninja check-llvm-unit check-llvm
2025-03-09[NFC][Cloning] Add a helper to collect debug info from instructions (#129145)Artem Pianykh1-6/+13
Summary: Just moving around. This helper will be used for further refactoring. Test Plan: ninja check-llvm-unit check-llvm
2025-03-09[NFC][Cloning] Simplify the flow in FindDebugInfoToIdentityMap (#129144)Artem Pianykh1-17/+17
Summary: The new flow should make it more clear what is happening in cases of Different of Cloned modules. Test Plan: ninja check-llvm-unit check-llvm
2025-03-09[NFC][Cloning] Make ClonedModule case more obvious in ↵Artem Pianykh1-1/+5
CollectDebugInfoForCloning (#129143) Summary: The code's behavior is unchanged, but it's more obvious right now. Test Plan: ninja check-llvm-unit check-llvm
2025-01-24[NFC][DebugInfo] Use iterator moveBefore at many call-sites (#123583)Jeremy Morse1-1/+1
As part of the "RemoveDIs" project, BasicBlock::iterator now carries a debug-info bit that's needed when getFirstNonPHI and similar feed into instruction insertion positions. Call-sites where that's necessary were updated a year ago; but to ensure some type safety however, we'd like to have all calls to moveBefore use iterators. This patch adds a (guaranteed dereferenceable) iterator-taking moveBefore, and changes a bunch of call-sites where it's obviously safe to change to use it by just calling getIterator() on an instruction pointer. A follow-up patch will contain less-obviously-safe changes. We'll eventually deprecate and remove the instruction-pointer insertBefore, but not before adding concise documentation of what considerations are needed (very few).
2025-01-24[Utils] Identity map module-level debug info on first use in CloneFunction* ↵Artem Pianykh1-33/+29
(#118627) Summary: To avoid cloning module-level debug info (owned by the module rather than the function), CloneFunction implementation used to eagerly identity map such debug info into ValueMap's MD map. In larger modules with meaningful volume of debug info this gets very expensive. By passing such debug info metadata via an IdentityMD set for the ValueMapper to map on first use, we get several benefits: 1. Mapping metadata is not cheap, particularly because of tracking. When cloning a Function we identity map lots of global module-level metadata to avoid cloning it, while only a fraction of it is actually used by the function. Mapping on first use is a lot faster for modules with meaningful amount of debug info. 2. Eagerly identity mapping metadata makes it harder to cache module-level data (e.g. a set of metadata nodes in a \a DICompileUnit). With this patch we can cache certain module-level metadata calculations to speed things up further. Anecdata from compiling a sample cpp file with full debug info shows that this moderately speeds up CoroSplitPass which is one of the heavier users of cloning: | | Baseline | IdentityMD set | |-----------------|----------|----------------| | CoroSplitPass | 306ms | 221ms | | CoroCloner | 101ms | 72ms | | Speed up | 1x | 1.4x | Test Plan: ninja check-llvm-unit ninja check-llvm
2024-12-17[NFC][Utils] Eliminate DISubprogram set from BuildDebugInfoMDMap (#118625)Artem Pianykh1-5/+2
Summary: Previously, we'd add all SPs distinct from the cloned one into a set. Then when cloning a local scope we'd check if it's from one of those 'distinct' SPs by checking if it's in the set. We don't need to do that. We can just check against the cloned SP directly and drop the set. Test Plan: ninja check-llvm-unit check-llvm
2024-12-16[NFC][Utils] Extract CloneFunctionBodyInto from CloneFunctionInto (#118624)Artem Pianykh1-41/+56
Summary: This and previously extracted `CloneFunction*Into` functions will be used in later diffs. Test Plan: ninja check-llvm-unit check-llvm
2024-12-16[NFC][Utils] Extract CloneFunctionMetadataInto from CloneFunctionInto (#118623)Artem Pianykh1-9/+16
Summary: The new API expects the caller to populate the VMap. We need it this way for a subsequent change around coroutine cloning. Test Plan: ninja check-llvm-unit check-llvm
2024-12-10[NFC][Utils] Extract BuildDebugInfoMDMap from CloneFunctionInto (#118622)Artem Pianykh1-39/+50
Summary: Extract the logic to build up a metadata map to use in metadata cloning into a separate function. Test Plan: ninja check-llvm-unit check-llvm
2024-12-09[NFC][Utils] Clone basic blocks after we're done with metadata in ↵Artem Pianykh1-28/+28
CloneFunctionInto (#118621) Summary: Moving the cloning of BBs after the metadata makes the flow of the function a bit more straightforward and makes it easier to extract more into helper functions. Test Plan: ninja check-llvm-unit check-llvm
2024-12-06[NFC][Utils] Remove DebugInfoFinder parameter from CloneBasicBlock (#118620)Artem Pianykh1-10/+2
Summary: There was a single usage of CloneBasicBlock with non-default DebugInfoFinder inside CloneFunctionInto which has been refactored in more focused. Test Plan: ninja check-llvm-unit check-llvm
2024-11-20[Utils] Extract CollectDebugInfoForCloning from CloneFunctionInto (#114537)Artem Pianykh1-20/+39
Summary: Consolidate the logic in a single function. We do an extra pass over Instructions but this is necessary to untangle things and extract metadata cloning in a future diff. Test Plan: ``` $ ninja check-llvm-unit check-llvm [211/213] Running the LLVM regression tests Testing Time: 106.06s Total Discovered Tests: 62601 Skipped : 17 (0.03%) Unsupported : 2518 (4.02%) Passed : 59911 (95.70%) Expectedly Failed: 155 (0.25%) [212/213] Running lit suite Testing Time: 12.47s Total Discovered Tests: 8474 Skipped: 17 (0.20%) Passed : 8457 (99.80%) ``` Extracted from #109032 (commit 3) (there are more refactors and cleanups in subsequent commits)
2024-11-04[Utils] Remove unused includes (NFC) (#114748)Kazu Hirata1-1/+0
Identified with misc-include-cleaner.
2024-10-30[NFC][Utils] Extract CloneFunctionAttributesInto from CloneFunctionInto ↵Artem Pianykh1-22/+32
(#112976) This patch is a part of step-by-step refactoring of CloneFunctionInto. The goal is to extract reusable pieces out of it that will be later used to optimize function cloning e.g. in coroutine processing. Extracted from #109032 (commit 2)
2024-10-17[SimplifyLibCall][Attribute] Fix bug where we may keep `range` attr with ↵goldsteinn1-2/+2
incompatible type (#112649) In a variety of places we change the bitwidth of a parameter but don't update the attributes. The issue in this case is from the `range` attribute when inlining `__memset_chk`. `optimizeMemSetChk` will replace an `i32` with an `i8`, and if the `i32` had a `range` attr assosiated it will cause an error. Fixes #112633
2024-10-11[NFC] Rename `Intrinsic::getDeclaration` to `getOrInsertDeclaration` (#111752)Rahul Joshi1-2/+2
Rename the function to reflect its correct behavior and to be consistent with `Module::getOrInsertFunction`. This is also in preparation of adding a new `Intrinsic::getDeclaration` that will have behavior similar to `Module::getFunction` (i.e, just lookup, no creation).
2024-10-03[MemProf] Strip callsite metadata when inlining an unprofiled callsite (#110998)Teresa Johnson1-0/+2
We weren't flagging inlined callee functions with callsite but not memprof metadata correctly, leading to the callsite metadata not being stripped when that function was inlined into a callsite that didn't itself have callsite metadata. In practice, this meant that we went into the LTO link with many more calls than necessary having callsite metadata / summary records, which in turn made the graph larger than necessary. Fixing this oversight resulted in huge reductions in the thin link of a large target: 99% fewer duplicated context ids (recall we have to duplicate when callsites containing the same stack ids are in different functions) 71% fewer graph edges 17% fewer graph nodes 13% fewer functions cloned 44% smaller peak memory 47% smaller time
2024-08-29[ExtendLifetimes] Implement llvm.fake.use to extend variable lifetimes (#86149)Stephen Tozer1-0/+6
This patch is part of a set of patches that add an `-fextend-lifetimes` flag to clang, which extends the lifetimes of local variables and parameters for improved debuggability. In addition to that flag, the patch series adds a pragma to selectively disable `-fextend-lifetimes`, and an `-fextend-this-ptr` flag which functions as `-fextend-lifetimes` for this pointers only. All changes and tests in these patches were written by Wolfgang Pieb (@wolfy1961), while Stephen Tozer (@SLTozer) has handled review and merging. The extend lifetimes flag is intended to eventually be set on by `-Og`, as discussed in the RFC here: https://discourse.llvm.org/t/rfc-redefine-og-o1-and-add-a-new-level-of-og/72850 This patch implements a new intrinsic instruction in LLVM, `llvm.fake.use` in IR and `FAKE_USE` in MIR, that takes a single operand and has no effect other than "using" its operand, to ensure that its operand remains live until after the fake use. This patch does not emit fake uses anywhere; the next patch in this sequence causes them to be emitted from the clang frontend, such that for each variable (or this) a fake.use operand is inserted at the end of that variable's scope, using that variable's value. This patch covers everything post-frontend, which is largely just the basic plumbing for a new intrinsic/instruction, along with a few steps to preserve the fake uses through optimizations (such as moving them ahead of a tail call or translating them through SROA). Co-authored-by: Stephen Tozer <stephen.tozer@sony.com>
2024-06-28[IR] Add getDataLayout() helpers to Function and GlobalValue (#96919)Nikita Popov1-1/+1
Similar to https://github.com/llvm/llvm-project/pull/96902, this adds `getDataLayout()` helpers to Function and GlobalValue, replacing the current `getParent()->getDataLayout()` pattern.
2024-06-27[IR] Add getDataLayout() helpers to BasicBlock and Instruction (#96902)Nikita Popov1-1/+1
This is a helper to avoid writing `getModule()->getDataLayout()`. I regularly try to use this method only to remember it doesn't exist... `getModule()->getDataLayout()` is also a common (the most common?) reason why code has to include the Module.h header.
2024-05-20[CloneFunction] Remove check that is no longer necessary (#92577)Danila Malyutin1-7/+0
We do not need to concern ourselves with CGSCC since all remaining CG related updates went away in fa6ea7a419f37befbed04368bcb8af4c718facbb as pointed out by @nikic in https://github.com/llvm/llvm-project/pull/87963#issuecomment-2113937182.
2024-05-08[RemoveDIs] Change remapDbgVariableRecord to remapDbgRecord (#91456)Harald van Dijk1-9/+8
We need to remap any DbgRecord, not just DbgVariableRecords. This is the followup to #91447. Co-authored-by: PietroGhg <pietro.ghiglio@codeplay.com>
2024-05-07[NFC][LLVM] Refactor rounding mode detection of constrained fp intrinsic IDs ↵Paul Walker1-13/+1
(#90854) I've refactored the code to genericise the implementation to better allow for target specific constrained fp intrinsics.
2024-05-02[Inline][Cloning] Drop incompatible attributes from `NewFunc`Antonio Frighetto1-0/+12
Performing `instSimplify` while cloning is unsafe due to incomplete remapping (as reported in #87534). Ideally, `instSimplify` ought to reason on the updated newly-cloned function, after returns have been rewritten and callee entry basic block / call-site have been fixed up. This is in contrast to `CloneAndPruneIntoFromInst` behaviour, which is inherently expected to clone basic blocks, with pruning on top of – if any –, and not actually fixing up returns / CFG, which should be up to the Inliner. We may solve this by letting `instSimplify` work on the newly-cloned function, while maintaining old function attributes, so as to avoid inconsistencies between the yet-to-be-solved return type, and new function ret type attributes.
2024-05-02Reapply "[Inline][Cloning] Defer simplification after phi-nodes resolution"Antonio Frighetto1-57/+35
Original commit: a61f9fe31750cee65c726fb51f1b14e31e177258 Multiple 2-stage buildbots were reporting failures. These issues have been addressed separately. Fixes: https://github.com/llvm/llvm-project/issues/87534.
2024-04-26[Transforms] Debug values are not remapped when cloning. (#87747)Carlos Alberto Enciso1-0/+3
When cloning instructions from one basic block to another, the debug values are not remapped, in the same was as the normal instructions.
2024-04-24Revert "[Inline][Cloning] Defer simplification after phi-nodes resolution" ↵Vitaly Buka1-35/+57
#87963 Reopens #87534. Breaks multiple bots: https://lab.llvm.org/buildbot/#/builders/168/builds/20028 https://lab.llvm.org/buildbot/#/builders/74/builds/27773 And reproducer in a61f9fe31750cee65c726fb51f1b14e31e177258. This reverts commit a61f9fe31750cee65c726fb51f1b14e31e177258.
2024-04-24[Inline][Cloning] Defer simplification after phi-nodes resolutionAntonio Frighetto1-57/+35
A logic issue arose when inlining via `CloneAndPruneFunctionInto`, which, besides cloning, performs instruction simplification as well. By the time a new cloned instruction is being simplified, phi-nodes are not remapped yet as the whole CFG needs to be processed first. As `VMap` state at this stage is incomplete, `threadCmpOverPHI` and variants could lead to unsound optimizations. This issue has been addressed by performing basic constant folding while cloning, and postponing instruction simplification once phi-nodes are revisited. Fixes: https://github.com/llvm/llvm-project/issues/87534.
2024-03-19[RemoveDIs][NFC] Rename DPValue -> DbgVariableRecord (#85216)Stephen Tozer1-11/+13
This is the major rename patch that prior patches have built towards. The DPValue class is being renamed to DbgVariableRecord, which reflects the updated terminology for the "final" implementation of the RemoveDI feature. This is a pure string substitution + clang-format patch. The only manual component of this patch was determining where to perform these string substitutions: `DPValue` and `DPV` are almost exclusively used for DbgRecords, *except* for: - llvm/lib/target, where 'DP' is used to mean double-precision, and so appears as part of .td files and in variable names. NB: There is a single existing use of `DPValue` here that refers to debug info, which I've manually updated. - llvm/tools/gold, where 'LDPV' is used as a prefix for symbol visibility enums. Outside of these places, I've applied several basic string substitutions, with the intent that they only affect DbgRecord-related identifiers; I've checked them as I went through to verify this, with reasonable confidence that there are no unintended changes that slipped through the cracks. The substitutions applied are all case-sensitive, and are applied in the order shown: ``` DPValue -> DbgVariableRecord DPVal -> DbgVarRec DPV -> DVR ``` Following the previous rename patches, it should be the case that there are no instances of any of these strings that are meant to refer to the general case of DbgRecords, or anything other than the DPValue class. The idea behind this patch is therefore that pure string substitution is correct in all cases as long as these assumptions hold.
2024-03-12[RemoveDIs][NFC] Rename common interface functions for DPValues->DbgRecords ↵Stephen Tozer1-3/+3
(#84793) As part of the effort to rename the DbgRecord classes, this patch renames the widely-used functions that operate on DbgRecords but refer to DbgValues or DPValues in their names to refer to DbgRecords instead; all such functions are defined in one of `BasicBlock.h`, `Instruction.h`, and `DebugProgramInstruction.h`. This patch explicitly does not change the names of any comments or variables, except for where they use the exact name of one of the renamed functions. The reason for this is reviewability; this patch can be trivially examined to determine that the only changes are direct string substitutions and any results from clang-format responding to the changed line lengths. Future patches will cover renaming variables and comments, and then renaming the classes themselves.
2024-01-16Revert "[CloneFunction][DebugInfo] Avoid cloning DILocalVariables of inlined ↵Davide Italiano1-14/+1
functions (#75385)" This reverts commit fc6faa1113e9069f41b5500db051210af0eea843.
2024-01-11[CloneFunction][DebugInfo] Avoid cloning DILocalVariables of inlined ↵Vladislav Dzhidzhoev1-1/+14
functions (#75385) - [DebugMetadata][DwarfDebug] Support function-local types in lexical block scopes (4/7) - [CloneFunction][DebugInfo] Avoid cloning DILocalVariables of inlined functions This is a follow-up for https://reviews.llvm.org/D144006, fixing a crash reported in Chromium (https://reviews.llvm.org/D144006#4651955). The first commit is added for convenience, as it has already been accepted. If DISubpogram was not cloned (e.g. we are cloning a function that has other functions inlined into it, and subprograms of the inlined functions are not supposed to be cloned), it doesn't make sense to clone its DILocalVariables as well. Otherwise get duplicated DILocalVariables not tracked in their subprogram's retainedNodes, that crash LTO with Chromium. This is meant to be committed along with https://reviews.llvm.org/D144006.
2023-11-26Suppressed unused-var warning from c672ba7ddeJeremy Morse1-1/+0
2023-11-26[DebugInfo][RemoveDIs] Instrument inliner for non-instr debug-info (#72884)Jeremy Morse1-5/+55
With intrinsics representing debug-info, we just clone all the intrinsics when inlining a function and don't think about it any further. With non-instruction debug-info however we need to be a bit more careful and manually move the debug-info from one place to another. For the most part, this means keeping a "cursor" during block cloning of where we last copied debug-info from, and performing debug-info copying whenever we successfully clone another instruction. There are several utilities in LLVM for doing this, all of which now need to manually call cloneDebugInfo. The testing story for this is not well covered as we could rely on normal instruction-cloning mechanisms to do all the hard stuff. Thus, I've added a few tests to explicitly test dbg.value behaviours, ahead of them becoming not-instructions.