From c3de9c1c7b93683ab9d390cb2d830204bfa657bc Mon Sep 17 00:00:00 2001 From: Johannes Doerfert Date: Tue, 10 Jan 2023 14:01:00 -0800 Subject: [PATCH] [OpenMP] Ensure AAHeapToShared is only looking at one function When we collect and process allocations we did not verify the call against the anchor scope / associated function. This should be done to avoid processing calls multiple times and generally looking at calls not in the AAs scope. --- llvm/lib/Transforms/IPO/OpenMPOpt.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp index 1b226c7..eebe8d3 100644 --- a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp +++ b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp @@ -2995,6 +2995,8 @@ struct AAHeapToSharedFunction : public AAHeapToShared { bool &) -> std::optional { return nullptr; }; for (User *U : RFI.Declaration->users()) if (CallBase *CB = dyn_cast(U)) { + if (CB->getCaller() != getAnchorScope()) + continue; MallocCalls.insert(CB); A.registerSimplificationCallback(IRPosition::callsite_returned(*CB), SCB); @@ -3091,6 +3093,8 @@ struct AAHeapToSharedFunction : public AAHeapToShared { } ChangeStatus updateImpl(Attributor &A) override { + if (MallocCalls.empty()) + return indicatePessimisticFixpoint(); auto &OMPInfoCache = static_cast(A.getInfoCache()); auto &RFI = OMPInfoCache.RFIs[OMPRTL___kmpc_alloc_shared]; if (!RFI.Declaration) @@ -3102,12 +3106,18 @@ struct AAHeapToSharedFunction : public AAHeapToShared { // Only consider malloc calls executed by a single thread with a constant. for (User *U : RFI.Declaration->users()) { - const auto &ED = A.getAAFor( - *this, IRPosition::function(*F), DepClassTy::REQUIRED); - if (CallBase *CB = dyn_cast(U)) - if (!isa(CB->getArgOperand(0)) || - !ED.isExecutedByInitialThreadOnly(*CB)) + if (CallBase *CB = dyn_cast(U)) { + if (CB->getCaller() != F) + continue; + if (!isa(CB->getArgOperand(0))) { + MallocCalls.remove(CB); + continue; + } + const auto &ED = A.getAAFor( + *this, IRPosition::function(*F), DepClassTy::REQUIRED); + if (!ED.isExecutedByInitialThreadOnly(*CB)) MallocCalls.remove(CB); + } } findPotentialRemovedFreeCalls(A); @@ -4829,13 +4839,6 @@ void OpenMPOpt::registerAAs(bool IsModulePass) { GetterRFI.foreachUse(SCC, CreateAA); } } - auto &GlobalizationRFI = OMPInfoCache.RFIs[OMPRTL___kmpc_alloc_shared]; - auto CreateAA = [&](Use &U, Function &F) { - A.getOrCreateAAFor(IRPosition::function(F)); - return false; - }; - if (!DisableOpenMPOptDeglobalization) - GlobalizationRFI.foreachUse(SCC, CreateAA); // Create an ExecutionDomain AA for every function and a HeapToStack AA for // every function if there is a device kernel. @@ -4846,6 +4849,8 @@ void OpenMPOpt::registerAAs(bool IsModulePass) { if (F->isDeclaration()) continue; + if (!DisableOpenMPOptDeglobalization) + A.getOrCreateAAFor(IRPosition::function(F)); A.getOrCreateAAFor(IRPosition::function(*F)); if (!DisableOpenMPOptDeglobalization) A.getOrCreateAAFor(IRPosition::function(*F)); -- 2.7.4