diff --git a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp --- a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp +++ b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp @@ -22,6 +22,7 @@ #include "llvm/ADT/EnumeratedArray.h" #include "llvm/ADT/PostOrderIterator.h" #include "llvm/ADT/Statistic.h" +#include "llvm/ADT/StringRef.h" #include "llvm/Analysis/CallGraph.h" #include "llvm/Analysis/CallGraphSCCPass.h" #include "llvm/Analysis/OptimizationRemarkEmitter.h" @@ -43,6 +44,8 @@ #include "llvm/Transforms/Utils/CallGraphUpdater.h" #include "llvm/Transforms/Utils/CodeExtractor.h" +#include + using namespace llvm; using namespace omp; @@ -633,13 +636,15 @@ KernelInfoState operator^=(const KernelInfoState &KIS) { // Do not merge two different _init and _deinit call sites. if (KIS.KernelInitCB) { - if(KernelInitCB && KernelInitCB != KIS.KernelInitCB) - llvm_unreachable("Kernel that calls another kernel violates OpenMP-Opt assumptions."); + if (KernelInitCB && KernelInitCB != KIS.KernelInitCB) + llvm_unreachable("Kernel that calls another kernel violates OpenMP-Opt " + "assumptions."); KernelInitCB = KIS.KernelInitCB; } if (KIS.KernelDeinitCB) { - if(KernelDeinitCB && KernelDeinitCB != KIS.KernelDeinitCB) - llvm_unreachable("Kernel that calls another kernel violates OpenMP-Opt assumptions."); + if (KernelDeinitCB && KernelDeinitCB != KIS.KernelDeinitCB) + llvm_unreachable("Kernel that calls another kernel violates OpenMP-Opt " + "assumptions."); KernelDeinitCB = KIS.KernelDeinitCB; } SPMDCompatibilityTracker ^= KIS.SPMDCompatibilityTracker; @@ -3031,6 +3036,18 @@ SPMDCompatibilityTracker.indicatePessimisticFixpoint(); } + /// Sanitize the string \p S such that it is a suitable global symbol name. + static std::string sanitizeForGlobalName(std::string S) { + std::replace_if( + S.begin(), S.end(), + [](const char C) { + return !((C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') || + (C >= '0' && C <= '9') || C == '_'); + }, + '.'); + return S; + } + /// Modify the IR based on the KernelInfoState as the fixpoint iteration is /// finished now. ChangeStatus manifest(Attributor &A) override { @@ -3161,8 +3178,9 @@ auto *SharedMem = new GlobalVariable( M, I.getType(), /* IsConstant */ false, GlobalValue::InternalLinkage, UndefValue::get(I.getType()), - I.getName() + ".guarded.output.alloc", nullptr, - GlobalValue::NotThreadLocal, + sanitizeForGlobalName( + (I.getName() + ".guarded.output.alloc").str()), + nullptr, GlobalValue::NotThreadLocal, static_cast(AddressSpace::Shared)); // Emit a store instruction to update the value. @@ -3173,11 +3191,8 @@ RegionBarrierBB->getTerminator()); // Emit a load instruction and replace uses of the output value. - for (Instruction *UsrI : OutsideUsers) { - assert(UsrI->getParent() == RegionExitBB && - "Expected escaping users in exit region"); + for (Instruction *UsrI : OutsideUsers) UsrI->replaceUsesOfWith(&I, LoadI); - } } auto &OMPInfoCache = static_cast(A.getInfoCache()); @@ -3695,7 +3710,8 @@ bool UsedAssumedInformationInCheckCallInst = false; if (!A.checkForAllCallLikeInstructions( CheckCallInst, *this, UsedAssumedInformationInCheckCallInst)) { - LLVM_DEBUG(dbgs() << TAG << "Failed to visit all call-like instructions!\n";); + LLVM_DEBUG(dbgs() << TAG + << "Failed to visit all call-like instructions!\n";); return indicatePessimisticFixpoint(); }