[flang] [libcxx] [libc] [compiler-rt] [llvm] [clang-tools-extra] [clang] [mlir] Make SmallVectorImpl destructor protected (PR #71439)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 6 12:03:08 PST 2023


https://github.com/andykaylor created https://github.com/llvm/llvm-project/pull/71439

Because the SmallVectorImpl destructor is not virtual, the destructor of derived classes will not be called if pointers to the SmallVectorImpl class are deleted directly. Making the SmallVectorImpl destructor protected will prevent this.

>From 0a599a8259d4162d2478b5b5ae3be098bfc70f30 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <andrew.kaylor at intel.com>
Date: Mon, 6 Nov 2023 11:43:42 -0800
Subject: [PATCH 1/2] Update SerializeToHsaco to avoid
 unique_ptr<SmallVectorImpl>

The use of unique_ptr<SmallVectorImpl> can lead to leaked resources.
This change replaces a use of that template in SerializeToHsaco with a
similar unique_ptr<SmallVector<char, 16>>. This may not be the best
replacement. It is being done to enable builds with a protected
destructor for SmallVectorImpl.
---
 mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp b/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp
index 5c288e977ad0f23..97b3b66eda00133 100644
--- a/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp
+++ b/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp
@@ -95,7 +95,7 @@ class SerializeToHsacoPass
   std::unique_ptr<std::vector<char>>
   serializeISA(const std::string &isa) override;
 
-  std::unique_ptr<SmallVectorImpl<char>> assembleIsa(const std::string &isa);
+  std::unique_ptr<SmallVector<char, 16>> assembleIsa(const std::string &isa);
   std::unique_ptr<std::vector<char>>
   createHsaco(const SmallVectorImpl<char> &isaBinary);
 
@@ -318,7 +318,7 @@ SerializeToHsacoPass::translateToLLVMIR(llvm::LLVMContext &llvmContext) {
   return ret;
 }
 
-std::unique_ptr<SmallVectorImpl<char>>
+std::unique_ptr<SmallVector<char, 16>>
 SerializeToHsacoPass::assembleIsa(const std::string &isa) {
   auto loc = getOperation().getLoc();
 
@@ -381,7 +381,7 @@ SerializeToHsacoPass::assembleIsa(const std::string &isa) {
   parser->setTargetParser(*tap);
   parser->Run(false);
 
-  return std::make_unique<SmallVector<char, 0>>(std::move(result));
+  return std::make_unique<SmallVector<char, 16>>(std::move(result));
 }
 
 std::unique_ptr<std::vector<char>>

>From af52cde83236b000f948805374dd29765bad765d Mon Sep 17 00:00:00 2001
From: Andy Kaylor <andrew.kaylor at intel.com>
Date: Mon, 6 Nov 2023 11:46:17 -0800
Subject: [PATCH 2/2] Make SmallVectorImpl destructor protected

Because the SmallVectorImpl destructor is not virtual, the destructor of
derived classes will not be called if pointers to the SmallVectorImpl class are deleted directly. Making the SmallVectorImpl destructor protected will prevent this.
---
 llvm/include/llvm/ADT/SmallVector.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 53a107b1574c6a3..2e6d2dc6ce90a2c 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -601,9 +601,6 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
     RHS.resetToSmall();
   }
 
-public:
-  SmallVectorImpl(const SmallVectorImpl &) = delete;
-
   ~SmallVectorImpl() {
     // Subclass has already destructed this vector's elements.
     // If this wasn't grown from the inline copy, deallocate the old space.
@@ -611,6 +608,9 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
       free(this->begin());
   }
 
+public:
+  SmallVectorImpl(const SmallVectorImpl &) = delete;
+
   void clear() {
     this->destroy_range(this->begin(), this->end());
     this->Size = 0;



More information about the cfe-commits mailing list