[llvm] [DataLayout] Remove `clear` and `reset` methods (NFC) (PR #102993)

Sergei Barannikov via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 12 19:19:51 PDT 2024


https://github.com/s-barannikov created https://github.com/llvm/llvm-project/pull/102993

`clear` was never necessary as it is always called on a fresh instance of the class or just before freeing an instance's memory. `reset` is effectively the same as the constructor.

>From 43ec4d25ac8538c94adcdbe935a14413028e304c Mon Sep 17 00:00:00 2001
From: Sergei Barannikov <barannikov88 at gmail.com>
Date: Tue, 13 Aug 2024 05:19:05 +0300
Subject: [PATCH] [DataLayout] Remove `clear` and `reset` methods (NFC)

`clear` was never necessary as it is always called on a fresh instance
of the class or just before freeing an instance's memory.
`reset` is effectively the same as the constructor.
---
 llvm/include/llvm/IR/DataLayout.h | 13 +++----------
 llvm/lib/IR/DataLayout.cpp        | 30 +++++++-----------------------
 llvm/lib/IR/Module.cpp            |  2 +-
 3 files changed, 11 insertions(+), 34 deletions(-)

diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h
index 9619560f4baed3..f4b4b730bee2ae 100644
--- a/llvm/include/llvm/IR/DataLayout.h
+++ b/llvm/include/llvm/IR/DataLayout.h
@@ -185,14 +185,10 @@ class DataLayout {
   /// if the string is malformed.
   Error parseSpecifier(StringRef Desc);
 
-  // Free all internal data structures.
-  void clear();
-
 public:
-  /// Constructs a DataLayout from a specification string. See reset().
-  explicit DataLayout(StringRef LayoutDescription) {
-    reset(LayoutDescription);
-  }
+  /// Constructs a DataLayout from a specification string.
+  /// WARNING: Aborts execution if the string is malformed. Use parse() instead.
+  explicit DataLayout(StringRef LayoutString);
 
   DataLayout(const DataLayout &DL) { *this = DL; }
 
@@ -203,9 +199,6 @@ class DataLayout {
   bool operator==(const DataLayout &Other) const;
   bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
 
-  /// Parse a data layout string (with fallback to default values).
-  void reset(StringRef LayoutDescription);
-
   /// Parse a data layout string and return the layout. Return an error
   /// description on failure.
   static Expected<DataLayout> parse(StringRef LayoutDescription);
diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp
index 1efa61e92d2bc7..bf8ebc5b8c04bf 100644
--- a/llvm/lib/IR/DataLayout.cpp
+++ b/llvm/lib/IR/DataLayout.cpp
@@ -191,36 +191,30 @@ static const std::pair<AlignTypeEnum, LayoutAlignElem> DefaultAlignments[] = {
     {VECTOR_ALIGN, {128, Align(16), Align(16)}}, // v16i8, v8i16, v4i32, ...
 };
 
-void DataLayout::reset(StringRef Desc) {
-  clear();
-
-  LayoutMap = nullptr;
+DataLayout::DataLayout(StringRef LayoutString) {
   BigEndian = false;
   AllocaAddrSpace = 0;
-  StackNaturalAlign.reset();
   ProgramAddrSpace = 0;
   DefaultGlobalsAddrSpace = 0;
-  FunctionPtrAlign.reset();
   TheFunctionPtrAlignType = FunctionPtrAlignType::Independent;
   ManglingMode = MM_None;
-  NonIntegralAddressSpaces.clear();
   StructAlignment = LayoutAlignElem::get(Align(1), Align(8), 0);
 
   // Default alignments
   for (const auto &[Kind, Layout] : DefaultAlignments) {
     if (Error Err = setAlignment(Kind, Layout.ABIAlign, Layout.PrefAlign,
                                  Layout.TypeBitWidth))
-      return report_fatal_error(std::move(Err));
+      report_fatal_error(std::move(Err));
   }
   if (Error Err = setPointerAlignmentInBits(0, Align(8), Align(8), 64, 64))
-    return report_fatal_error(std::move(Err));
+    report_fatal_error(std::move(Err));
 
-  if (Error Err = parseSpecifier(Desc))
-    return report_fatal_error(std::move(Err));
+  if (Error Err = parseSpecifier(LayoutString))
+    report_fatal_error(std::move(Err));
 }
 
 DataLayout &DataLayout::operator=(const DataLayout &Other) {
-  clear();
+  // Copy everything except for LayoutMap, which will be recomputed on demand.
   StringRepresentation = Other.StringRepresentation;
   BigEndian = Other.BigEndian;
   AllocaAddrSpace = Other.AllocaAddrSpace;
@@ -716,18 +710,8 @@ class StructLayoutMap {
 
 } // end anonymous namespace
 
-void DataLayout::clear() {
-  LegalIntWidths.clear();
-  IntAlignments.clear();
-  FloatAlignments.clear();
-  VectorAlignments.clear();
-  Pointers.clear();
-  delete static_cast<StructLayoutMap *>(LayoutMap);
-  LayoutMap = nullptr;
-}
-
 DataLayout::~DataLayout() {
-  clear();
+  delete static_cast<StructLayoutMap *>(LayoutMap);
 }
 
 const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp
index 80b5408b61eda0..3da05b79c874eb 100644
--- a/llvm/lib/IR/Module.cpp
+++ b/llvm/lib/IR/Module.cpp
@@ -389,7 +389,7 @@ void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
 }
 
 void Module::setDataLayout(StringRef Desc) {
-  DL.reset(Desc);
+  DL = DataLayout(Desc);
 }
 
 void Module::setDataLayout(const DataLayout &Other) { DL = Other; }



More information about the llvm-commits mailing list