Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 13, 2024

Conversation

s-barannikov
Copy link
Contributor

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.

@llvmbot
Copy link
Member

llvmbot commented Aug 13, 2024

@llvm/pr-subscribers-llvm-ir

Author: Sergei Barannikov (s-barannikov)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/102993.diff

3 Files Affected:

  • (modified) llvm/include/llvm/IR/DataLayout.h (+3-10)
  • (modified) llvm/lib/IR/DataLayout.cpp (+7-23)
  • (modified) llvm/lib/IR/Module.cpp (+1-1)
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; }

Copy link

github-actions bot commented Aug 13, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@s-barannikov s-barannikov force-pushed the datalayout/remove-reset-clear branch from 43ec4d2 to a7cac38 Compare August 13, 2024 02:23
`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.
@s-barannikov s-barannikov force-pushed the datalayout/remove-reset-clear branch from a7cac38 to a9bb740 Compare August 13, 2024 02:32
@s-barannikov s-barannikov requested a review from nikic August 13, 2024 03:26
}

DataLayout &DataLayout::operator=(const DataLayout &Other) {
clear();
// Copy everything except for LayoutMap, which will be recomputed on demand.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we explicitly clear it then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course.
For some reason I assumed that the operator should work the same way as a copy constructor.
Thanks for catching.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed. I had to move class declaration to make it visible to delete.

@s-barannikov s-barannikov force-pushed the datalayout/remove-reset-clear branch from becfa2f to 17d84f7 Compare August 13, 2024 11:48
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@s-barannikov s-barannikov merged commit b1aa0b0 into llvm:main Aug 13, 2024
8 checks passed
@s-barannikov s-barannikov deleted the datalayout/remove-reset-clear branch August 13, 2024 17:10
@maleadt
Copy link
Contributor

maleadt commented May 16, 2025

This inadvertently caused some issues with the Julia JIT, where we used DataLayout::reset to replace a TargetMachine's data layout with one that marks certain address spaces as non-integral. I realize that isn't kosher, so what would be the "proper" way of creating a TargetMachine with a custom DataLayout? createTargetMachine doesn't have any customization.

FWIW, ignoring the mismatch between the TM's data layout and the one we use on modules causes issues down the line, e.g., when MachineFunction asserts that both match:

assert(Target.isCompatibleDataLayout(getDataLayout()) &&
"Can't create a MachineFunction using a Module with a "
"Target-incompatible DataLayout attached\n");

@s-barannikov
Copy link
Contributor Author

we used DataLayout::reset to replace a TargetMachine's data layout with one that marks certain address spaces as non-integral

TargetMachine's DL member is const and createDataLayout() returns a const copy of it. How did you manage to call reset() on a copy and get it working? If you modified TargetMachine.h, I guess you can also replace reset() with copy assignment?

@maleadt
Copy link
Contributor

maleadt commented May 20, 2025

createDataLayout() returns a const copy of it.

You're right, I misdiagnosed the issue (which did appear after upgrading to LLVM 20, so this change seemed likely).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants