llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

If we don't have any (live) dynamic allocations, we can save ourselves the work 
of checking all the pointers in the returned value.

Also move the `getAllocator()` call in `Free()` further down so we don' 
unnecessarily create a dynamic allocator.

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


3 Files Affected:

- (modified) clang/lib/AST/ByteCode/EvaluationResult.cpp (+3) 
- (modified) clang/lib/AST/ByteCode/Interp.cpp (+2-3) 
- (modified) clang/lib/AST/ByteCode/InterpState.h (+3-1) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/EvaluationResult.cpp 
b/clang/lib/AST/ByteCode/EvaluationResult.cpp
index 09b1eb822b13e..905cd8cf93c64 100644
--- a/clang/lib/AST/ByteCode/EvaluationResult.cpp
+++ b/clang/lib/AST/ByteCode/EvaluationResult.cpp
@@ -246,6 +246,9 @@ static void collectBlocks(PtrView Ptr,
 bool EvaluationResult::checkDynamicAllocations(InterpState &S,
                                                const Pointer &Ptr,
                                                SourceInfo Info) {
+  if (!S.hasDynamicAllocations())
+    return true;
+
   if (!Ptr.isBlockPointer())
     return true;
   // Collect all blocks that this pointer (transitively) points to and
diff --git a/clang/lib/AST/ByteCode/Interp.cpp 
b/clang/lib/AST/ByteCode/Interp.cpp
index 49ebcc789b7dd..da192d254919a 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1474,8 +1474,6 @@ bool Free(InterpState &S, CodePtr OpPC, bool 
DeleteIsArrayForm,
   if (!CheckDynamicMemoryAllocation(S, OpPC))
     return false;
 
-  DynamicAllocator &Allocator = S.getAllocator();
-
   const Expr *Source = nullptr;
   const Block *BlockToDelete = nullptr;
   {
@@ -1500,7 +1498,7 @@ bool Free(InterpState &S, CodePtr OpPC, bool 
DeleteIsArrayForm,
     // Check that new[]/delete[] or new/delete were used, not a mixture.
     const Descriptor *BlockDesc = BlockToDelete->getDescriptor();
     if (std::optional<DynamicAllocator::Form> AllocForm =
-            Allocator.getAllocationForm(Source)) {
+            S.getAllocator().getAllocationForm(Source)) {
       DynamicAllocator::Form DeleteForm =
           DeleteIsArrayForm ? DynamicAllocator::Form::Array
                             : DynamicAllocator::Form::NonArray;
@@ -1560,6 +1558,7 @@ bool Free(InterpState &S, CodePtr OpPC, bool 
DeleteIsArrayForm,
   if (!RunDestructors(S, OpPC, BlockToDelete))
     return false;
 
+  DynamicAllocator &Allocator = S.getAllocator();
   if (!Allocator.deallocate(Source, BlockToDelete)) {
     // Nothing has been deallocated, this must be a double-delete.
     const SourceInfo &Loc = S.Current->getSource(OpPC);
diff --git a/clang/lib/AST/ByteCode/InterpState.h 
b/clang/lib/AST/ByteCode/InterpState.h
index 920197d8021c0..b03679ace8eb5 100644
--- a/clang/lib/AST/ByteCode/InterpState.h
+++ b/clang/lib/AST/ByteCode/InterpState.h
@@ -82,9 +82,11 @@ class InterpState final : public State {
     if (!Alloc) {
       Alloc = std::make_unique<DynamicAllocator>();
     }
-
     return *Alloc;
   }
+  bool hasDynamicAllocations() const {
+    return Alloc && Alloc->hasAllocations();
+  }
 
   /// Diagnose any dynamic allocations that haven't been freed yet.
   /// Will return \c false if there were any allocations to diagnose,

``````````

</details>


https://github.com/llvm/llvm-project/pull/224887
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to