From 265203d50ea609fcf5481a987b52745e8b96783e Mon Sep 17 00:00:00 2001 From: Artem Pianykh Date: Thu, 12 Sep 2024 15:09:44 -0700 Subject: [PATCH 1/6] [NFC][Utils] Clone basic blocks after we're done with metadata in CloneFunctionInto 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 stack-info: PR: https://github.com/llvm/llvm-project/pull/118621, branch: users/artempyanykh/fast-coro-upstream/2 --- llvm/lib/Transforms/Utils/CloneFunction.cpp | 56 ++++++++++----------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp index b55776d736e66..d038117090e4c 100644 --- a/llvm/lib/Transforms/Utils/CloneFunction.cpp +++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp @@ -210,34 +210,6 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, DISubprogram *SPClonedWithinModule = CollectDebugInfoForCloning(*OldFunc, Changes, DIFinder); - // Loop over all of the basic blocks in the function, cloning them as - // appropriate. Note that we save BE this way in order to handle cloning of - // recursive functions into themselves. - for (const BasicBlock &BB : *OldFunc) { - - // Create a new basic block and copy instructions into it! - BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo); - - // Add basic block mapping. - VMap[&BB] = CBB; - - // It is only legal to clone a function if a block address within that - // function is never referenced outside of the function. Given that, we - // want to map block addresses from the old function to block addresses in - // the clone. (This is different from the generic ValueMapper - // implementation, which generates an invalid blockaddress when - // cloning a function.) - if (BB.hasAddressTaken()) { - Constant *OldBBAddr = BlockAddress::get(const_cast(OldFunc), - const_cast(&BB)); - VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB); - } - - // Note return instructions for the caller. - if (ReturnInst *RI = dyn_cast(CBB->getTerminator())) - Returns.push_back(RI); - } - if (Changes < CloneFunctionChangeType::DifferentModule && DIFinder.subprogram_count() > 0) { // Turn on module-level changes, since we need to clone (some of) the @@ -289,6 +261,34 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, TypeMapper, Materializer)); } + // Loop over all of the basic blocks in the function, cloning them as + // appropriate. Note that we save BE this way in order to handle cloning of + // recursive functions into themselves. + for (const BasicBlock &BB : *OldFunc) { + + // Create a new basic block and copy instructions into it! + BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo); + + // Add basic block mapping. + VMap[&BB] = CBB; + + // It is only legal to clone a function if a block address within that + // function is never referenced outside of the function. Given that, we + // want to map block addresses from the old function to block addresses in + // the clone. (This is different from the generic ValueMapper + // implementation, which generates an invalid blockaddress when + // cloning a function.) + if (BB.hasAddressTaken()) { + Constant *OldBBAddr = BlockAddress::get(const_cast(OldFunc), + const_cast(&BB)); + VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB); + } + + // Note return instructions for the caller. + if (ReturnInst *RI = dyn_cast(CBB->getTerminator())) + Returns.push_back(RI); + } + // Loop over all of the instructions in the new function, fixing up operand // references as we go. This uses VMap to do all the hard work. for (Function::iterator From 69aad056b1d71cada26e32a02179d3671d45a941 Mon Sep 17 00:00:00 2001 From: Artem Pianykh Date: Thu, 12 Sep 2024 15:23:43 -0700 Subject: [PATCH 2/6] [NFC][Utils] Extract BuildDebugInfoMDMap from CloneFunctionInto 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 stack-info: PR: https://github.com/llvm/llvm-project/pull/118622, branch: users/artempyanykh/fast-coro-upstream/3 --- llvm/include/llvm/IR/ValueMap.h | 4 +- llvm/include/llvm/Transforms/Utils/Cloning.h | 7 ++ llvm/lib/Transforms/Utils/CloneFunction.cpp | 88 +++++++++++--------- 3 files changed, 59 insertions(+), 40 deletions(-) diff --git a/llvm/include/llvm/IR/ValueMap.h b/llvm/include/llvm/IR/ValueMap.h index d12d639aaa888..fd4c08492e012 100644 --- a/llvm/include/llvm/IR/ValueMap.h +++ b/llvm/include/llvm/IR/ValueMap.h @@ -79,6 +79,9 @@ struct ValueMapConfig { static mutex_type *getMutex(const ExtraDataT &/*Data*/) { return nullptr; } }; +/// This type stores Metadata. Used in ValueMap. +using MDMapT = DenseMap; + /// See the file comment. template> class ValueMap { @@ -86,7 +89,6 @@ class ValueMap { using ValueMapCVH = ValueMapCallbackVH; using MapT = DenseMap>; - using MDMapT = DenseMap; using ExtraData = typename Config::ExtraData; MapT Map; diff --git a/llvm/include/llvm/Transforms/Utils/Cloning.h b/llvm/include/llvm/Transforms/Utils/Cloning.h index 3c8f2cbfaa9b8..434089138bc52 100644 --- a/llvm/include/llvm/Transforms/Utils/Cloning.h +++ b/llvm/include/llvm/Transforms/Utils/Cloning.h @@ -220,6 +220,13 @@ DISubprogram *CollectDebugInfoForCloning(const Function &F, CloneFunctionChangeType Changes, DebugInfoFinder &DIFinder); +/// Build a map of debug info to use during Metadata cloning. +/// Returns true if cloning would need module level changes and false if there +/// would only be local changes. +bool BuildDebugInfoMDMap(MDMapT &MD, CloneFunctionChangeType Changes, + DebugInfoFinder &DIFinder, + DISubprogram *SPClonedWithinModule); + /// This class captures the data input to the InlineFunction call, and records /// the auxiliary results produced by it. class InlineFunctionInfo { diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp index d038117090e4c..736c4a13045c1 100644 --- a/llvm/lib/Transforms/Utils/CloneFunction.cpp +++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp @@ -152,6 +152,53 @@ DISubprogram *llvm::CollectDebugInfoForCloning(const Function &F, return SPClonedWithinModule; } +bool llvm::BuildDebugInfoMDMap(MDMapT &MD, CloneFunctionChangeType Changes, + DebugInfoFinder &DIFinder, + DISubprogram *SPClonedWithinModule) { + bool ModuleLevelChanges = Changes > CloneFunctionChangeType::LocalChangesOnly; + if (Changes < CloneFunctionChangeType::DifferentModule && + DIFinder.subprogram_count() > 0) { + // Turn on module-level changes, since we need to clone (some of) the + // debug info metadata. + // + // FIXME: Metadata effectively owned by a function should be made + // local, and only that local metadata should be cloned. + ModuleLevelChanges = true; + + auto mapToSelfIfNew = [&MD](MDNode *N) { + // Avoid clobbering an existing mapping. + (void)MD.try_emplace(N, N); + }; + + // Avoid cloning types, compile units, and (other) subprograms. + SmallPtrSet MappedToSelfSPs; + for (DISubprogram *ISP : DIFinder.subprograms()) { + if (ISP != SPClonedWithinModule) { + mapToSelfIfNew(ISP); + MappedToSelfSPs.insert(ISP); + } + } + + // If a subprogram isn't going to be cloned skip its lexical blocks as well. + for (DIScope *S : DIFinder.scopes()) { + auto *LScope = dyn_cast(S); + if (LScope && MappedToSelfSPs.count(LScope->getSubprogram())) + mapToSelfIfNew(S); + } + + for (DICompileUnit *CU : DIFinder.compile_units()) + mapToSelfIfNew(CU); + + for (DIType *Type : DIFinder.types()) + mapToSelfIfNew(Type); + } else { + assert(!SPClonedWithinModule && + "Subprogram should be in DIFinder->subprogram_count()..."); + } + + return ModuleLevelChanges; +} + // Clone OldFunc into NewFunc, transforming the old arguments into references to // VMap values. void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, @@ -210,45 +257,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, DISubprogram *SPClonedWithinModule = CollectDebugInfoForCloning(*OldFunc, Changes, DIFinder); - if (Changes < CloneFunctionChangeType::DifferentModule && - DIFinder.subprogram_count() > 0) { - // Turn on module-level changes, since we need to clone (some of) the - // debug info metadata. - // - // FIXME: Metadata effectively owned by a function should be made - // local, and only that local metadata should be cloned. - ModuleLevelChanges = true; - - auto mapToSelfIfNew = [&VMap](MDNode *N) { - // Avoid clobbering an existing mapping. - (void)VMap.MD().try_emplace(N, N); - }; - - // Avoid cloning types, compile units, and (other) subprograms. - SmallPtrSet MappedToSelfSPs; - for (DISubprogram *ISP : DIFinder.subprograms()) { - if (ISP != SPClonedWithinModule) { - mapToSelfIfNew(ISP); - MappedToSelfSPs.insert(ISP); - } - } - - // If a subprogram isn't going to be cloned skip its lexical blocks as well. - for (DIScope *S : DIFinder.scopes()) { - auto *LScope = dyn_cast(S); - if (LScope && MappedToSelfSPs.count(LScope->getSubprogram())) - mapToSelfIfNew(S); - } - - for (DICompileUnit *CU : DIFinder.compile_units()) - mapToSelfIfNew(CU); - - for (DIType *Type : DIFinder.types()) - mapToSelfIfNew(Type); - } else { - assert(!SPClonedWithinModule && - "Subprogram should be in DIFinder->subprogram_count()..."); - } + ModuleLevelChanges = + BuildDebugInfoMDMap(VMap.MD(), Changes, DIFinder, SPClonedWithinModule); const auto RemapFlag = ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges; // Duplicate the metadata that is attached to the cloned function. From 8540f2583c09034e67cf02729b99757d82558335 Mon Sep 17 00:00:00 2001 From: Artem Pianykh Date: Thu, 12 Sep 2024 15:35:38 -0700 Subject: [PATCH 3/6] [NFC][Utils] Extract CloneFunctionMetadataInto from CloneFunctionInto 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 stack-info: PR: https://github.com/llvm/llvm-project/pull/118623, branch: users/artempyanykh/fast-coro-upstream/4 --- llvm/include/llvm/Transforms/Utils/Cloning.h | 12 +++++++++ llvm/lib/Transforms/Utils/CloneFunction.cpp | 28 +++++++++++++------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/llvm/include/llvm/Transforms/Utils/Cloning.h b/llvm/include/llvm/Transforms/Utils/Cloning.h index 434089138bc52..698d773525e80 100644 --- a/llvm/include/llvm/Transforms/Utils/Cloning.h +++ b/llvm/include/llvm/Transforms/Utils/Cloning.h @@ -182,6 +182,18 @@ void CloneFunctionAttributesInto(Function *NewFunc, const Function *OldFunc, ValueMapTypeRemapper *TypeMapper = nullptr, ValueMaterializer *Materializer = nullptr); +/// Clone OldFunc's metadata into NewFunc. +/// +/// The caller is expected to populate \p VMap beforehand and set an appropriate +/// \p RemapFlag. +/// +/// NOTE: This function doesn't clone !llvm.dbg.cu when cloning into a different +/// module. Use CloneFunctionInto for that behavior. +void CloneFunctionMetadataInto(Function *NewFunc, const Function *OldFunc, + ValueToValueMapTy &VMap, RemapFlags RemapFlag, + ValueMapTypeRemapper *TypeMapper = nullptr, + ValueMaterializer *Materializer = nullptr); + void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc, const Instruction *StartingInst, ValueToValueMapTy &VMap, bool ModuleLevelChanges, diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp index 736c4a13045c1..4306e5d2abdd1 100644 --- a/llvm/lib/Transforms/Utils/CloneFunction.cpp +++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp @@ -199,6 +199,22 @@ bool llvm::BuildDebugInfoMDMap(MDMapT &MD, CloneFunctionChangeType Changes, return ModuleLevelChanges; } +void llvm::CloneFunctionMetadataInto(Function *NewFunc, const Function *OldFunc, + ValueToValueMapTy &VMap, + RemapFlags RemapFlag, + ValueMapTypeRemapper *TypeMapper, + ValueMaterializer *Materializer) { + // Duplicate the metadata that is attached to the cloned function. + // Subprograms/CUs/types that were already mapped to themselves won't be + // duplicated. + SmallVector, 1> MDs; + OldFunc->getAllMetadata(MDs); + for (auto MD : MDs) { + NewFunc->addMetadata(MD.first, *MapMetadata(MD.second, VMap, RemapFlag, + TypeMapper, Materializer)); + } +} + // Clone OldFunc into NewFunc, transforming the old arguments into references to // VMap values. void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, @@ -261,15 +277,9 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, BuildDebugInfoMDMap(VMap.MD(), Changes, DIFinder, SPClonedWithinModule); const auto RemapFlag = ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges; - // Duplicate the metadata that is attached to the cloned function. - // Subprograms/CUs/types that were already mapped to themselves won't be - // duplicated. - SmallVector, 1> MDs; - OldFunc->getAllMetadata(MDs); - for (auto MD : MDs) { - NewFunc->addMetadata(MD.first, *MapMetadata(MD.second, VMap, RemapFlag, - TypeMapper, Materializer)); - } + + CloneFunctionMetadataInto(NewFunc, OldFunc, VMap, RemapFlag, TypeMapper, + Materializer); // Loop over all of the basic blocks in the function, cloning them as // appropriate. Note that we save BE this way in order to handle cloning of From 39cccf5abfd4c685dbde8cd1efcab0066ce186fc Mon Sep 17 00:00:00 2001 From: Artem Pianykh Date: Thu, 12 Sep 2024 15:50:25 -0700 Subject: [PATCH 4/6] [NFC][Utils] Extract CloneFunctionBodyInto from CloneFunctionInto Summary: This and previously extracted `CloneFunction*Into` functions will be used in later diffs. Test Plan: ninja check-llvm-unit check-llvm stack-info: PR: https://github.com/llvm/llvm-project/pull/118624, branch: users/artempyanykh/fast-coro-upstream/5 --- llvm/include/llvm/Transforms/Utils/Cloning.h | 34 ++++--- llvm/lib/Transforms/Utils/CloneFunction.cpp | 96 +++++++++++--------- 2 files changed, 76 insertions(+), 54 deletions(-) diff --git a/llvm/include/llvm/Transforms/Utils/Cloning.h b/llvm/include/llvm/Transforms/Utils/Cloning.h index 698d773525e80..47b75853ce948 100644 --- a/llvm/include/llvm/Transforms/Utils/Cloning.h +++ b/llvm/include/llvm/Transforms/Utils/Cloning.h @@ -194,6 +194,15 @@ void CloneFunctionMetadataInto(Function *NewFunc, const Function *OldFunc, ValueMapTypeRemapper *TypeMapper = nullptr, ValueMaterializer *Materializer = nullptr); +/// Clone OldFunc's body into NewFunc. +void CloneFunctionBodyInto(Function *NewFunc, const Function *OldFunc, + ValueToValueMapTy &VMap, RemapFlags RemapFlag, + SmallVectorImpl &Returns, + const char *NameSuffix = "", + ClonedCodeInfo *CodeInfo = nullptr, + ValueMapTypeRemapper *TypeMapper = nullptr, + ValueMaterializer *Materializer = nullptr); + void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc, const Instruction *StartingInst, ValueToValueMapTy &VMap, bool ModuleLevelChanges, @@ -214,7 +223,7 @@ void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc, /// void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, bool ModuleLevelChanges, - SmallVectorImpl &Returns, + SmallVectorImpl &Returns, const char *NameSuffix = "", ClonedCodeInfo *CodeInfo = nullptr); @@ -360,32 +369,31 @@ void updateProfileCallee( /// Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified /// basic blocks and extract their scope. These are candidates for duplication /// when cloning. -void identifyNoAliasScopesToClone( - ArrayRef BBs, SmallVectorImpl &NoAliasDeclScopes); +void identifyNoAliasScopesToClone(ArrayRef BBs, + SmallVectorImpl &NoAliasDeclScopes); /// Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified /// instruction range and extract their scope. These are candidates for /// duplication when cloning. -void identifyNoAliasScopesToClone( - BasicBlock::iterator Start, BasicBlock::iterator End, - SmallVectorImpl &NoAliasDeclScopes); +void identifyNoAliasScopesToClone(BasicBlock::iterator Start, + BasicBlock::iterator End, + SmallVectorImpl &NoAliasDeclScopes); /// Duplicate the specified list of noalias decl scopes. /// The 'Ext' string is added as an extension to the name. /// Afterwards, the ClonedScopes contains the mapping of the original scope /// MDNode onto the cloned scope. /// Be aware that the cloned scopes are still part of the original scope domain. -void cloneNoAliasScopes( - ArrayRef NoAliasDeclScopes, - DenseMap &ClonedScopes, - StringRef Ext, LLVMContext &Context); +void cloneNoAliasScopes(ArrayRef NoAliasDeclScopes, + DenseMap &ClonedScopes, + StringRef Ext, LLVMContext &Context); /// Adapt the metadata for the specified instruction according to the /// provided mapping. This is normally used after cloning an instruction, when /// some noalias scopes needed to be cloned. -void adaptNoAliasScopes( - llvm::Instruction *I, const DenseMap &ClonedScopes, - LLVMContext &Context); +void adaptNoAliasScopes(llvm::Instruction *I, + const DenseMap &ClonedScopes, + LLVMContext &Context); /// Clone the specified noalias decl scopes. Then adapt all instructions in the /// NewBlocks basicblocks to the cloned versions. diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp index 4306e5d2abdd1..1f4cdec333a2c 100644 --- a/llvm/lib/Transforms/Utils/CloneFunction.cpp +++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp @@ -215,6 +215,59 @@ void llvm::CloneFunctionMetadataInto(Function *NewFunc, const Function *OldFunc, } } +void llvm::CloneFunctionBodyInto(Function *NewFunc, const Function *OldFunc, + ValueToValueMapTy &VMap, RemapFlags RemapFlag, + SmallVectorImpl &Returns, + const char *NameSuffix, + ClonedCodeInfo *CodeInfo, + ValueMapTypeRemapper *TypeMapper, + ValueMaterializer *Materializer) { + if (OldFunc->isDeclaration()) + return; + + // Loop over all of the basic blocks in the function, cloning them as + // appropriate. Note that we save BE this way in order to handle cloning of + // recursive functions into themselves. + for (const BasicBlock &BB : *OldFunc) { + + // Create a new basic block and copy instructions into it! + BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo); + + // Add basic block mapping. + VMap[&BB] = CBB; + + // It is only legal to clone a function if a block address within that + // function is never referenced outside of the function. Given that, we + // want to map block addresses from the old function to block addresses in + // the clone. (This is different from the generic ValueMapper + // implementation, which generates an invalid blockaddress when + // cloning a function.) + if (BB.hasAddressTaken()) { + Constant *OldBBAddr = BlockAddress::get(const_cast(OldFunc), + const_cast(&BB)); + VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB); + } + + // Note return instructions for the caller. + if (ReturnInst *RI = dyn_cast(CBB->getTerminator())) + Returns.push_back(RI); + } + + // Loop over all of the instructions in the new function, fixing up operand + // references as we go. This uses VMap to do all the hard work. + for (Function::iterator + BB = cast(VMap[&OldFunc->front()])->getIterator(), + BE = NewFunc->end(); + BB != BE; ++BB) + // Loop over all instructions, fixing each one as we find it, and any + // attached debug-info records. + for (Instruction &II : *BB) { + RemapInstruction(&II, VMap, RemapFlag, TypeMapper, Materializer); + RemapDbgRecordRange(II.getModule(), II.getDbgRecordRange(), VMap, + RemapFlag, TypeMapper, Materializer); + } +} + // Clone OldFunc into NewFunc, transforming the old arguments into references to // VMap values. void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, @@ -281,47 +334,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, CloneFunctionMetadataInto(NewFunc, OldFunc, VMap, RemapFlag, TypeMapper, Materializer); - // Loop over all of the basic blocks in the function, cloning them as - // appropriate. Note that we save BE this way in order to handle cloning of - // recursive functions into themselves. - for (const BasicBlock &BB : *OldFunc) { - - // Create a new basic block and copy instructions into it! - BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo); - - // Add basic block mapping. - VMap[&BB] = CBB; - - // It is only legal to clone a function if a block address within that - // function is never referenced outside of the function. Given that, we - // want to map block addresses from the old function to block addresses in - // the clone. (This is different from the generic ValueMapper - // implementation, which generates an invalid blockaddress when - // cloning a function.) - if (BB.hasAddressTaken()) { - Constant *OldBBAddr = BlockAddress::get(const_cast(OldFunc), - const_cast(&BB)); - VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB); - } - - // Note return instructions for the caller. - if (ReturnInst *RI = dyn_cast(CBB->getTerminator())) - Returns.push_back(RI); - } - - // Loop over all of the instructions in the new function, fixing up operand - // references as we go. This uses VMap to do all the hard work. - for (Function::iterator - BB = cast(VMap[&OldFunc->front()])->getIterator(), - BE = NewFunc->end(); - BB != BE; ++BB) - // Loop over all instructions, fixing each one as we find it, and any - // attached debug-info records. - for (Instruction &II : *BB) { - RemapInstruction(&II, VMap, RemapFlag, TypeMapper, Materializer); - RemapDbgRecordRange(II.getModule(), II.getDbgRecordRange(), VMap, - RemapFlag, TypeMapper, Materializer); - } + CloneFunctionBodyInto(NewFunc, OldFunc, VMap, RemapFlag, Returns, NameSuffix, + CodeInfo, TypeMapper, Materializer); // Only update !llvm.dbg.cu for DifferentModule (not CloneModule). In the // same module, the compile unit will already be listed (or not). When From 1b7941e037c85729a5661944a13bfe0d40aa26cf Mon Sep 17 00:00:00 2001 From: Artem Pianykh Date: Sat, 14 Sep 2024 16:02:51 -0700 Subject: [PATCH 5/6] [NFC][Utils] Eliminate DISubprogram set from BuildDebugInfoMDMap 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 stack-info: PR: https://github.com/llvm/llvm-project/pull/118625, branch: users/artempyanykh/fast-coro-upstream/6 --- llvm/lib/Transforms/Utils/CloneFunction.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp index 1f4cdec333a2c..5bc90bc7ae23d 100644 --- a/llvm/lib/Transforms/Utils/CloneFunction.cpp +++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp @@ -171,18 +171,15 @@ bool llvm::BuildDebugInfoMDMap(MDMapT &MD, CloneFunctionChangeType Changes, }; // Avoid cloning types, compile units, and (other) subprograms. - SmallPtrSet MappedToSelfSPs; for (DISubprogram *ISP : DIFinder.subprograms()) { - if (ISP != SPClonedWithinModule) { + if (ISP != SPClonedWithinModule) mapToSelfIfNew(ISP); - MappedToSelfSPs.insert(ISP); - } } // If a subprogram isn't going to be cloned skip its lexical blocks as well. for (DIScope *S : DIFinder.scopes()) { auto *LScope = dyn_cast(S); - if (LScope && MappedToSelfSPs.count(LScope->getSubprogram())) + if (LScope && LScope->getSubprogram() != SPClonedWithinModule) mapToSelfIfNew(S); } From f0e2d2b11fc5b133e657621f917efb3b2a6db4ce Mon Sep 17 00:00:00 2001 From: Artem Pianykh Date: Sun, 15 Sep 2024 02:59:24 -0700 Subject: [PATCH 6/6] [NFC] Remove adhoc definition of MDMapT in IRMover Summary: The typedef was there probably because the type alias in ValueMap was private. Test Plan: ninja check-llvm-unit check-llvm stack-info: PR: https://github.com/llvm/llvm-project/pull/118626, branch: users/artempyanykh/fast-coro-upstream/7 --- llvm/include/llvm/Linker/IRMover.h | 4 +--- llvm/lib/Linker/IRMover.cpp | 3 --- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/llvm/include/llvm/Linker/IRMover.h b/llvm/include/llvm/Linker/IRMover.h index 1e3c5394ffa2a..89e9cbe0be18e 100644 --- a/llvm/include/llvm/Linker/IRMover.h +++ b/llvm/include/llvm/Linker/IRMover.h @@ -12,6 +12,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/FunctionExtras.h" +#include "llvm/IR/ValueMap.h" #include namespace llvm { @@ -41,9 +42,6 @@ class IRMover { static bool isEqual(const StructType *LHS, const StructType *RHS); }; - /// Type of the Metadata map in \a ValueToValueMapTy. - typedef DenseMap MDMapT; - public: class IdentifiedStructTypeSet { // The set of opaque types is the composite module. diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp index a0c3f2c5b0baf..76af83df00cba 100644 --- a/llvm/lib/Linker/IRMover.cpp +++ b/llvm/lib/Linker/IRMover.cpp @@ -373,9 +373,6 @@ class LocalValueMaterializer final : public ValueMaterializer { Value *materialize(Value *V) override; }; -/// Type of the Metadata map in \a ValueToValueMapTy. -typedef DenseMap MDMapT; - /// This is responsible for keeping track of the state used for moving data /// from SrcM to DstM. class IRLinker {