Skip to content

[lldb] Avoid calling DataLayout constructor accepting Module pointer (NFC) #102839

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

@s-barannikov s-barannikov commented Aug 12, 2024

The constructor initializes *this with a copy of M->getDataLayout(), which can just be spelled as DataLayout DL = M->getDataLayout(). In all places where the constructor is used, Module outlives DataLayout, so store a reference to it instead of cloning.

Prerequisite for #102841.

…(NFC)

The constructor initializes `*this` with a copy of `M->getDataLayout()`,
which can just be spelled as `DataLayout DL = M->getDataLayout()`.
In all places where the constructor is used, Module outlives DataLayout,
so store a reference to it instead of cloning.
@llvmbot
Copy link
Member

llvmbot commented Aug 12, 2024

@llvm/pr-subscribers-lldb

Author: Sergei Barannikov (s-barannikov)

Changes

The constructor initializes *this with a copy of M->getDataLayout(), which can just be spelled as DataLayout DL = M->getDataLayout(). In all places where the constructor is used, Module outlives DataLayout, so store a reference to it instead of cloning.


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

4 Files Affected:

  • (modified) lldb/source/Expression/IRInterpreter.cpp (+3-3)
  • (modified) lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp (+3-4)
  • (modified) lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp (+1-1)
  • (modified) lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.h (+2-2)
diff --git a/lldb/source/Expression/IRInterpreter.cpp b/lldb/source/Expression/IRInterpreter.cpp
index 5b670067b5c433..030b30f070ee35 100644
--- a/lldb/source/Expression/IRInterpreter.cpp
+++ b/lldb/source/Expression/IRInterpreter.cpp
@@ -96,7 +96,7 @@ class InterpreterStackFrame {
   typedef std::map<const Value *, lldb::addr_t> ValueMap;
 
   ValueMap m_values;
-  DataLayout &m_target_data;
+  const DataLayout &m_target_data;
   lldb_private::IRExecutionUnit &m_execution_unit;
   const BasicBlock *m_bb = nullptr;
   const BasicBlock *m_prev_bb = nullptr;
@@ -110,7 +110,7 @@ class InterpreterStackFrame {
   lldb::ByteOrder m_byte_order;
   size_t m_addr_byte_size;
 
-  InterpreterStackFrame(DataLayout &target_data,
+  InterpreterStackFrame(const DataLayout &target_data,
                         lldb_private::IRExecutionUnit &execution_unit,
                         lldb::addr_t stack_frame_bottom,
                         lldb::addr_t stack_frame_top)
@@ -703,7 +703,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
               s.c_str());
   }
 
-  DataLayout data_layout(&module);
+  const DataLayout &data_layout = module.getDataLayout();
 
   InterpreterStackFrame frame(data_layout, execution_unit, stack_frame_bottom,
                               stack_frame_top);
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp b/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp
index bc0f5993aad0d6..defd72bbd93106 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp
@@ -280,10 +280,9 @@ class Instrumenter {
 
   IntegerType *GetIntptrTy() {
     if (!m_intptr_ty) {
-      llvm::DataLayout data_layout(&m_module);
-
-      m_intptr_ty = llvm::Type::getIntNTy(m_module.getContext(),
-                                          data_layout.getPointerSizeInBits());
+      m_intptr_ty = llvm::Type::getIntNTy(
+          m_module.getContext(),
+          m_module.getDataLayout().getPointerSizeInBits());
     }
 
     return m_intptr_ty;
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp
index cc9bd14c6194e4..34461da46dfc7b 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp
@@ -1606,7 +1606,7 @@ bool IRForTarget::runOnModule(Module &llvm_module) {
   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
 
   m_module = &llvm_module;
-  m_target_data = std::make_unique<DataLayout>(m_module);
+  m_target_data = &m_module->getDataLayout();
   m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(),
                                       m_target_data->getPointerSizeInBits());
 
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.h b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.h
index a924187ba04c06..45027fcd6fa492 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.h
+++ b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.h
@@ -331,9 +331,9 @@ class IRForTarget {
   lldb_private::TypeFromParser m_result_type;
   /// The module being processed, or NULL if that has not been determined yet.
   llvm::Module *m_module = nullptr;
-  /// The target data for the module being processed, or NULL if there is no
+  /// The target data for the module being processed, or nullptr if there is no
   /// module.
-  std::unique_ptr<llvm::DataLayout> m_target_data;
+  const llvm::DataLayout *m_target_data = nullptr;
   /// The DeclMap containing the Decls
   lldb_private::ClangExpressionDeclMap *m_decl_map;
   /// The address of the function CFStringCreateWithBytes, cast to the

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

Copy link
Member

@Michael137 Michael137 left a comment

Choose a reason for hiding this comment

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

Lifetimes look correct to me, thanks!

@s-barannikov s-barannikov merged commit e26b42c into llvm:main Aug 12, 2024
7 checks passed
@s-barannikov s-barannikov deleted the lldb/data-layout-module-constructor branch August 12, 2024 18:46
adrian-prantl pushed a commit to adrian-prantl/llvm-project that referenced this pull request Oct 12, 2024
…(NFC) (llvm#102839)

The constructor initializes `*this` with a copy of `M->getDataLayout()`,
which can just be spelled as `DataLayout DL = M->getDataLayout()`. In
all places where the constructor is used, Module outlives DataLayout, so
store a reference to it instead of cloning.

Pull Request: llvm#102839

(cherry picked from commit e26b42c)
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