Skip to content

[DataLayout] Move operator= to cpp file (NFC) #102849

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

Conversation

s-barannikov
Copy link
Contributor

DataLayout isn't exactly cheap to copy (448 bytes on a 64-bit host). Move operator= to cpp file to improve compilation time. Also move operator== closer to operator= and add a couple of FIXMEs.

@llvmbot
Copy link
Member

llvmbot commented Aug 12, 2024

@llvm/pr-subscribers-llvm-ir

Author: Sergei Barannikov (s-barannikov)

Changes

DataLayout isn't exactly cheap to copy (448 bytes on a 64-bit host). Move operator= to cpp file to improve compilation time. Also move operator== closer to operator= and add a couple of FIXMEs.


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

2 Files Affected:

  • (modified) llvm/include/llvm/IR/DataLayout.h (+3-21)
  • (modified) llvm/lib/IR/DataLayout.cpp (+41-19)
diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h
index afcb8bcb77f10a..8e6a8f62b3a048 100644
--- a/llvm/include/llvm/IR/DataLayout.h
+++ b/llvm/include/llvm/IR/DataLayout.h
@@ -120,10 +120,10 @@ class DataLayout {
   bool BigEndian;
 
   unsigned AllocaAddrSpace;
-  MaybeAlign StackNaturalAlign;
   unsigned ProgramAddrSpace;
   unsigned DefaultGlobalsAddrSpace;
 
+  MaybeAlign StackNaturalAlign;
   MaybeAlign FunctionPtrAlign;
   FunctionPtrAlignType TheFunctionPtrAlignType;
 
@@ -139,6 +139,7 @@ class DataLayout {
   };
   ManglingModeT ManglingMode;
 
+  // FIXME: `unsigned char` truncates the value parsed by `parseSpecifier`.
   SmallVector<unsigned char, 8> LegalIntWidths;
 
   /// Primitive type alignment data. This is sorted by type and bit
@@ -201,26 +202,7 @@ class DataLayout {
 
   ~DataLayout(); // Not virtual, do not subclass this class
 
-  DataLayout &operator=(const DataLayout &DL) {
-    clear();
-    StringRepresentation = DL.StringRepresentation;
-    BigEndian = DL.isBigEndian();
-    AllocaAddrSpace = DL.AllocaAddrSpace;
-    StackNaturalAlign = DL.StackNaturalAlign;
-    FunctionPtrAlign = DL.FunctionPtrAlign;
-    TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType;
-    ProgramAddrSpace = DL.ProgramAddrSpace;
-    DefaultGlobalsAddrSpace = DL.DefaultGlobalsAddrSpace;
-    ManglingMode = DL.ManglingMode;
-    LegalIntWidths = DL.LegalIntWidths;
-    IntAlignments = DL.IntAlignments;
-    FloatAlignments = DL.FloatAlignments;
-    VectorAlignments = DL.VectorAlignments;
-    StructAlignment = DL.StructAlignment;
-    Pointers = DL.Pointers;
-    NonIntegralAddressSpaces = DL.NonIntegralAddressSpaces;
-    return *this;
-  }
+  DataLayout &operator=(const DataLayout &Other);
 
   bool operator==(const DataLayout &Other) const;
   bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp
index 5104cb86320be7..81479fb365e64f 100644
--- a/llvm/lib/IR/DataLayout.cpp
+++ b/llvm/lib/IR/DataLayout.cpp
@@ -220,6 +220,47 @@ void DataLayout::reset(StringRef Desc) {
     return report_fatal_error(std::move(Err));
 }
 
+DataLayout &DataLayout::operator=(const DataLayout &Other) {
+  clear();
+  StringRepresentation = Other.StringRepresentation;
+  BigEndian = Other.BigEndian;
+  AllocaAddrSpace = Other.AllocaAddrSpace;
+  ProgramAddrSpace = Other.ProgramAddrSpace;
+  DefaultGlobalsAddrSpace = Other.DefaultGlobalsAddrSpace;
+  StackNaturalAlign = Other.StackNaturalAlign;
+  FunctionPtrAlign = Other.FunctionPtrAlign;
+  TheFunctionPtrAlignType = Other.TheFunctionPtrAlignType;
+  ManglingMode = Other.ManglingMode;
+  LegalIntWidths = Other.LegalIntWidths;
+  IntAlignments = Other.IntAlignments;
+  FloatAlignments = Other.FloatAlignments;
+  VectorAlignments = Other.VectorAlignments;
+  StructAlignment = Other.StructAlignment;
+  Pointers = Other.Pointers;
+  NonIntegralAddressSpaces = Other.NonIntegralAddressSpaces;
+  return *this;
+}
+
+bool DataLayout::operator==(const DataLayout &Other) const {
+  // FIXME: NonIntegralAddressSpaces isn't compared.
+  bool Ret = BigEndian == Other.BigEndian &&
+             AllocaAddrSpace == Other.AllocaAddrSpace &&
+             ProgramAddrSpace == Other.ProgramAddrSpace &&
+             DefaultGlobalsAddrSpace == Other.DefaultGlobalsAddrSpace &&
+             StackNaturalAlign == Other.StackNaturalAlign &&
+             FunctionPtrAlign == Other.FunctionPtrAlign &&
+             TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType &&
+             ManglingMode == Other.ManglingMode &&
+             LegalIntWidths == Other.LegalIntWidths &&
+             IntAlignments == Other.IntAlignments &&
+             FloatAlignments == Other.FloatAlignments &&
+             VectorAlignments == Other.VectorAlignments &&
+             StructAlignment == Other.StructAlignment &&
+             Pointers == Other.Pointers;
+  // Note: getStringRepresentation() might differs, it is not canonicalized
+  return Ret;
+}
+
 Expected<DataLayout> DataLayout::parse(StringRef LayoutDescription) {
   DataLayout Layout("");
   if (Error Err = Layout.parseSpecifier(LayoutDescription))
@@ -556,25 +597,6 @@ DataLayout::DataLayout(const Module *M) {
 
 void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
 
-bool DataLayout::operator==(const DataLayout &Other) const {
-  bool Ret = BigEndian == Other.BigEndian &&
-             AllocaAddrSpace == Other.AllocaAddrSpace &&
-             StackNaturalAlign == Other.StackNaturalAlign &&
-             ProgramAddrSpace == Other.ProgramAddrSpace &&
-             DefaultGlobalsAddrSpace == Other.DefaultGlobalsAddrSpace &&
-             FunctionPtrAlign == Other.FunctionPtrAlign &&
-             TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType &&
-             ManglingMode == Other.ManglingMode &&
-             LegalIntWidths == Other.LegalIntWidths &&
-             IntAlignments == Other.IntAlignments &&
-             FloatAlignments == Other.FloatAlignments &&
-             VectorAlignments == Other.VectorAlignments &&
-             StructAlignment == Other.StructAlignment &&
-             Pointers == Other.Pointers;
-  // Note: getStringRepresentation() might differs, it is not canonicalized
-  return Ret;
-}
-
 static SmallVectorImpl<LayoutAlignElem>::const_iterator
 findAlignmentLowerBound(const SmallVectorImpl<LayoutAlignElem> &Alignments,
                         uint32_t BitWidth) {

@s-barannikov s-barannikov force-pushed the datalayout/move-copy-assign-to-cpp branch from 2eaa263 to d1fddc0 Compare August 12, 2024 06:17
`DataLayout` isn't exactly cheap to copy (448 bytes on a 64-bit host).
Move `operator=` to cpp file to improve compilation time.
Also move `operator==` closer to `operator=` and add a couple of FIXMEs.
@s-barannikov s-barannikov force-pushed the datalayout/move-copy-assign-to-cpp branch from d1fddc0 to 43848bc Compare August 12, 2024 06:19
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 875b652 into llvm:main Aug 12, 2024
5 of 8 checks passed
@s-barannikov s-barannikov deleted the datalayout/move-copy-assign-to-cpp branch August 12, 2024 07:17
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.

3 participants