[NVPTX] Replace PTX's ManagedStringPool with StringSaver
authorLuke Drummond <luke.drummond@codeplay.com>
Tue, 20 Dec 2022 00:57:54 +0000 (00:57 +0000)
committerLuke Drummond <luke.drummond@codeplay.com>
Wed, 4 Jan 2023 11:28:39 +0000 (11:28 +0000)
In use ManagedStringPool caused a lot of heap allocations. At least one
for every register name lookup in NVPTXTargetRegisterInfo and one for
every symbol lookup in the target machine and isel lowering. There
already exists an llvm/Support string interning-class that has better
memory performance. Use LLVM's and delete ManagedStringPool which was
unique to PTX

llc Binary Size (.text only; bss and data were unchanged):
  MinsizeRel:
    Before: 31219884
    After: 31219796
  Release:
    Before: 42961872
    After: 42960656

Total heap allocations by the NVPTX string saving code running
check-llvm-codegen-nvptx

Total bytes allocated:
  Before: 2431825
  After: 2288151

(All numbers on x86-64-linux-gnu / gcc-12 / lld14)

I didn't see obvious time differences when running the tests.

Reviewers: tra, avasonic
Differential Revision: https://reviews.llvm.org/D140704

llvm/lib/Target/NVPTX/ManagedStringPool.h [deleted file]
llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
llvm/lib/Target/NVPTX/NVPTXRegisterInfo.cpp
llvm/lib/Target/NVPTX/NVPTXRegisterInfo.h
llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp
llvm/lib/Target/NVPTX/NVPTXTargetMachine.h

diff --git a/llvm/lib/Target/NVPTX/ManagedStringPool.h b/llvm/lib/Target/NVPTX/ManagedStringPool.h
deleted file mode 100644 (file)
index fb838d1..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-//===-- ManagedStringPool.h - Managed String Pool ---------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// The strings allocated from a managed string pool are owned by the string
-// pool and will be deleted together with the managed string pool.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIB_TARGET_NVPTX_MANAGEDSTRINGPOOL_H
-#define LLVM_LIB_TARGET_NVPTX_MANAGEDSTRINGPOOL_H
-
-#include "llvm/ADT/SmallVector.h"
-#include <string>
-
-namespace llvm {
-
-/// ManagedStringPool - The strings allocated from a managed string pool are
-/// owned by the string pool and will be deleted together with the managed
-/// string pool.
-class ManagedStringPool {
-  SmallVector<std::string *, 8> Pool;
-
-public:
-  ManagedStringPool() = default;
-
-  ~ManagedStringPool() {
-    SmallVectorImpl<std::string *>::iterator Current = Pool.begin();
-    while (Current != Pool.end()) {
-      delete *Current;
-      ++Current;
-    }
-  }
-
-  std::string *getManagedString(const char *S) {
-    std::string *Str = new std::string(S);
-    Pool.push_back(Str);
-    return Str;
-  }
-};
-
-} // end namespace llvm
-
-#endif // LLVM_LIB_TARGET_NVPTX_MANAGEDSTRINGPOOL_H
index 7d8d37a..eab1954 100644 (file)
@@ -206,9 +206,8 @@ void NVPTXAsmPrinter::lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp) {
   NVPTXTargetMachine &nvTM = static_cast<NVPTXTargetMachine&>(TM);
   const NVPTXMachineFunctionInfo *MFI = MF->getInfo<NVPTXMachineFunctionInfo>();
   const char *Sym = MFI->getImageHandleSymbol(Index);
-  std::string *SymNamePtr =
-    nvTM.getManagedStrPool()->getManagedString(Sym);
-  MCOp = GetSymbolRef(OutContext.getOrCreateSymbol(StringRef(*SymNamePtr)));
+  StringRef SymName = nvTM.getStrPool().save(Sym);
+  MCOp = GetSymbolRef(OutContext.getOrCreateSymbol(SymName));
 }
 
 void NVPTXAsmPrinter::lowerToMCInst(const MachineInstr *MI, MCInst &OutMI) {
index 8b57181..b7e81ea 100644 (file)
@@ -1822,10 +1822,11 @@ SDValue NVPTXTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
                       ->getAPIntValue()))
             : std::nullopt,
         *CB, UniqueCallSite);
-    const char *ProtoStr =
-      nvTM->getManagedStrPool()->getManagedString(Proto.c_str())->c_str();
+    const char *ProtoStr = nvTM->getStrPool().save(Proto).data();
     SDValue ProtoOps[] = {
-      Chain, DAG.getTargetExternalSymbol(ProtoStr, MVT::i32), InFlag,
+        Chain,
+        DAG.getTargetExternalSymbol(ProtoStr, MVT::i32),
+        InFlag,
     };
     Chain = DAG.getNode(NVPTXISD::CallPrototype, dl, ProtoVTs, ProtoOps);
     InFlag = Chain.getValue(1);
@@ -2634,9 +2635,9 @@ SDValue NVPTXTargetLowering::getParamSymbol(SelectionDAG &DAG, int idx,
   else
     ParamStr << "_param_" << idx;
 
-  std::string *SavedStr =
-    nvTM->getManagedStrPool()->getManagedString(ParamSym.c_str());
-  return DAG.getTargetExternalSymbol(SavedStr->c_str(), v);
+  StringRef SavedStr =
+    nvTM->getStrPool().save(ParamSym);
+  return DAG.getTargetExternalSymbol(SavedStr.data(), v);
 }
 
 SDValue NVPTXTargetLowering::LowerFormalArguments(
index 291572b..6e4208d 100644 (file)
@@ -93,7 +93,8 @@ std::string getNVPTXRegClassStr(TargetRegisterClass const *RC) {
 }
 }
 
-NVPTXRegisterInfo::NVPTXRegisterInfo() : NVPTXGenRegisterInfo(0) {}
+NVPTXRegisterInfo::NVPTXRegisterInfo()
+    : NVPTXGenRegisterInfo(0), StrPool(StrAlloc) {}
 
 #define GET_REGINFO_TARGET_DESC
 #include "NVPTXGenRegisterInfo.inc"
index 57218b9..7bce3bd 100644 (file)
@@ -13,8 +13,8 @@
 #ifndef LLVM_LIB_TARGET_NVPTX_NVPTXREGISTERINFO_H
 #define LLVM_LIB_TARGET_NVPTX_NVPTXREGISTERINFO_H
 
-#include "ManagedStringPool.h"
 #include "llvm/CodeGen/TargetRegisterInfo.h"
+#include "llvm/Support/StringSaver.h"
 #include <sstream>
 
 #define GET_REGINFO_HEADER
@@ -24,7 +24,8 @@ namespace llvm {
 class NVPTXRegisterInfo : public NVPTXGenRegisterInfo {
 private:
   // Hold Strings that can be free'd all together with NVPTXRegisterInfo
-  ManagedStringPool ManagedStrPool;
+  BumpPtrAllocator StrAlloc;
+  UniqueStringSaver StrPool;
 
 public:
   NVPTXRegisterInfo();
@@ -45,14 +46,14 @@ public:
   Register getFrameRegister(const MachineFunction &MF) const override;
   Register getFrameLocalRegister(const MachineFunction &MF) const;
 
-  ManagedStringPool *getStrPool() const {
-    return const_cast<ManagedStringPool *>(&ManagedStrPool);
+  UniqueStringSaver &getStrPool() const {
+    return const_cast<UniqueStringSaver &>(StrPool);
   }
 
   const char *getName(unsigned RegNo) const {
     std::stringstream O;
     O << "reg" << RegNo;
-    return getStrPool()->getManagedString(O.str().c_str())->c_str();
+    return getStrPool().save(O.str()).data();
   }
 
 };
index 4ef989c..3c379bb 100644 (file)
@@ -124,7 +124,8 @@ NVPTXTargetMachine::NVPTXTargetMachine(const Target &T, const Triple &TT,
                         getEffectiveCodeModel(CM, CodeModel::Small), OL),
       is64bit(is64bit), UseShortPointers(UseShortPointersOpt),
       TLOF(std::make_unique<NVPTXTargetObjectFile>()),
-      Subtarget(TT, std::string(CPU), std::string(FS), *this) {
+      Subtarget(TT, std::string(CPU), std::string(FS), *this),
+      StrPool(StrAlloc) {
   if (TT.getOS() == Triple::NVCL)
     drvInterface = NVPTX::NVCL;
   else
index 12005e0..843c3a2 100644 (file)
@@ -13,7 +13,6 @@
 #ifndef LLVM_LIB_TARGET_NVPTX_NVPTXTARGETMACHINE_H
 #define LLVM_LIB_TARGET_NVPTX_NVPTXTARGETMACHINE_H
 
-#include "ManagedStringPool.h"
 #include "NVPTXSubtarget.h"
 #include "llvm/Target/TargetMachine.h"
 #include <optional>
@@ -32,7 +31,8 @@ class NVPTXTargetMachine : public LLVMTargetMachine {
   NVPTXSubtarget Subtarget;
 
   // Hold Strings that can be free'd all together with NVPTXTargetMachine
-  ManagedStringPool ManagedStrPool;
+  BumpPtrAllocator StrAlloc;
+  UniqueStringSaver StrPool;
 
 public:
   NVPTXTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
@@ -48,8 +48,8 @@ public:
   bool is64Bit() const { return is64bit; }
   bool useShortPointers() const { return UseShortPointers; }
   NVPTX::DrvInterface getDrvInterface() const { return drvInterface; }
-  ManagedStringPool *getManagedStrPool() const {
-    return const_cast<ManagedStringPool *>(&ManagedStrPool);
+  UniqueStringSaver &getStrPool() const {
+    return const_cast<UniqueStringSaver &>(StrPool);
   }
 
   TargetPassConfig *createPassConfig(PassManagerBase &PM) override;