NVPTXAtomicLower.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include "NVPTXAtomicLower.h"
  13. #include "llvm/CodeGen/StackProtector.h"
  14. #include "llvm/IR/Constants.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/IRBuilder.h"
  17. #include "llvm/IR/InstIterator.h"
  18. #include "llvm/IR/Instructions.h"
  19. #include "llvm/Transforms/Scalar/LowerAtomic.h"
  20. #include "MCTargetDesc/NVPTXBaseInfo.h"
  21. using namespace llvm;
  22. namespace {
  23. // Hoisting the alloca instructions in the non-entry blocks to the entry
  24. // block.
  25. class NVPTXAtomicLower : public FunctionPass {
  26. public:
  27. static char ID; // Pass ID
  28. NVPTXAtomicLower() : FunctionPass(ID) {}
  29. void getAnalysisUsage(AnalysisUsage &AU) const override {
  30. AU.setPreservesCFG();
  31. }
  32. StringRef getPassName() const override {
  33. return "NVPTX lower atomics of local memory";
  34. }
  35. bool runOnFunction(Function &F) override;
  36. };
  37. } // namespace
  38. bool NVPTXAtomicLower::runOnFunction(Function &F) {
  39. SmallVector<AtomicRMWInst *> LocalMemoryAtomics;
  40. for (Instruction &I : instructions(F))
  41. if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
  42. if (RMWI->getPointerAddressSpace() == ADDRESS_SPACE_LOCAL)
  43. LocalMemoryAtomics.push_back(RMWI);
  44. bool Changed = false;
  45. for (AtomicRMWInst *RMWI : LocalMemoryAtomics)
  46. Changed |= lowerAtomicRMWInst(RMWI);
  47. return Changed;
  48. }
  49. char NVPTXAtomicLower::ID = 0;
  50. namespace llvm {
  51. void initializeNVPTXAtomicLowerPass(PassRegistry &);
  52. }
  53. INITIALIZE_PASS(NVPTXAtomicLower, "nvptx-atomic-lower",
  54. "Lower atomics of local memory to simple load/stores", false,
  55. false)
  56. FunctionPass *llvm::createNVPTXAtomicLowerPass() {
  57. return new NVPTXAtomicLower();
  58. }