1 //===-- NVPTXAtomicLower.cpp - Lower atomics of local memory ----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Lower atomics of local memory to simple load/stores 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "NVPTXAtomicLower.h" 14 #include "llvm/CodeGen/StackProtector.h" 15 #include "llvm/IR/Function.h" 16 #include "llvm/IR/InstIterator.h" 17 #include "llvm/IR/Instructions.h" 18 #include "llvm/Transforms/Utils/LowerAtomic.h" 19 20 #include "MCTargetDesc/NVPTXBaseInfo.h" 21 using namespace llvm; 22 23 namespace { 24 // Hoisting the alloca instructions in the non-entry blocks to the entry 25 // block. 26 class NVPTXAtomicLower : public FunctionPass { 27 public: 28 static char ID; // Pass ID 29 NVPTXAtomicLower() : FunctionPass(ID) {} 30 31 void getAnalysisUsage(AnalysisUsage &AU) const override { 32 AU.setPreservesCFG(); 33 } 34 35 StringRef getPassName() const override { 36 return "NVPTX lower atomics of local memory"; 37 } 38 39 bool runOnFunction(Function &F) override; 40 }; 41 } // namespace 42 43 bool NVPTXAtomicLower::runOnFunction(Function &F) { 44 SmallVector<AtomicRMWInst *> LocalMemoryAtomics; 45 for (Instruction &I : instructions(F)) 46 if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) 47 if (RMWI->getPointerAddressSpace() == ADDRESS_SPACE_LOCAL) 48 LocalMemoryAtomics.push_back(RMWI); 49 50 bool Changed = false; 51 for (AtomicRMWInst *RMWI : LocalMemoryAtomics) 52 Changed |= lowerAtomicRMWInst(RMWI); 53 return Changed; 54 } 55 56 char NVPTXAtomicLower::ID = 0; 57 58 namespace llvm { 59 void initializeNVPTXAtomicLowerPass(PassRegistry &); 60 } 61 62 INITIALIZE_PASS(NVPTXAtomicLower, "nvptx-atomic-lower", 63 "Lower atomics of local memory to simple load/stores", false, 64 false) 65 66 FunctionPass *llvm::createNVPTXAtomicLowerPass() { 67 return new NVPTXAtomicLower(); 68 } 69