[clang] [LifetimeSafety] Avoid adding already present items in sets/maps (PR #159582)
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 19 09:22:15 PDT 2025
https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/159582
>From 215d59c655c6117dc5774dfb8163156aad152044 Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
Date: Thu, 18 Sep 2025 12:52:29 +0000
Subject: [PATCH] [LifetimeSafety] Avoid adding already present items in
sets/maps
---
clang/lib/Analysis/LifetimeSafety.cpp | 12 ++++++++----
clang/test/Analysis/LifetimeSafety/benchmark.py | 11 ++++++-----
2 files changed, 14 insertions(+), 9 deletions(-)
diff --git a/clang/lib/Analysis/LifetimeSafety.cpp b/clang/lib/Analysis/LifetimeSafety.cpp
index 0dd5716d93fb6..d016c6f12e82e 100644
--- a/clang/lib/Analysis/LifetimeSafety.cpp
+++ b/clang/lib/Analysis/LifetimeSafety.cpp
@@ -910,10 +910,13 @@ template <typename T>
static llvm::ImmutableSet<T> join(llvm::ImmutableSet<T> A,
llvm::ImmutableSet<T> B,
typename llvm::ImmutableSet<T>::Factory &F) {
+ if (A == B)
+ return A;
if (A.getHeight() < B.getHeight())
std::swap(A, B);
for (const T &E : B)
- A = F.add(A, E);
+ if (!A.contains(E))
+ A = F.add(A, E);
return A;
}
@@ -947,10 +950,11 @@ join(llvm::ImmutableMap<K, V> A, llvm::ImmutableMap<K, V> B,
for (const auto &Entry : B) {
const K &Key = Entry.first;
const V &ValB = Entry.second;
- if (const V *ValA = A.lookup(Key))
- A = F.add(A, Key, JoinValues(*ValA, ValB));
- else
+ const V *ValA = A.lookup(Key);
+ if (!ValA)
A = F.add(A, Key, ValB);
+ else if (*ValA != ValB)
+ A = F.add(A, Key, JoinValues(*ValA, ValB));
}
return A;
}
diff --git a/clang/test/Analysis/LifetimeSafety/benchmark.py b/clang/test/Analysis/LifetimeSafety/benchmark.py
index 4421fe9a81e21..2373f9984eecd 100644
--- a/clang/test/Analysis/LifetimeSafety/benchmark.py
+++ b/clang/test/Analysis/LifetimeSafety/benchmark.py
@@ -252,7 +252,7 @@ def generate_markdown_report(results: dict) -> str:
report.append(" ".join(row))
report.append("\n**Complexity Analysis:**\n")
- report.append("| Analysis Phase | Complexity O(n<sup>k</sup>) |")
+ report.append("| Analysis Phase | Complexity O(n<sup>k</sup>) | ")
report.append("|:------------------|:--------------------------|")
analysis_phases = {
@@ -302,7 +302,7 @@ def run_single_test(
source_file,
]
- result = subprocess.run(clang_command, capture_output=True, text=True)
+ result = subprocess.run(clang_command, capture_output=True, text=True, timeout=60)
if result.returncode != 0:
print(f"Compilation failed for N={n}!", file=sys.stderr)
@@ -334,24 +334,25 @@ def run_single_test(
os.makedirs(args.output_dir, exist_ok=True)
print(f"Benchmark files will be saved in: {os.path.abspath(args.output_dir)}\n")
+ # Maximize 'n' values while keeping execution time under 10s.
test_configurations = [
{
"name": "cycle",
"title": "Pointer Cycle in Loop",
"generator_func": generate_cpp_cycle_test,
- "n_values": [10, 25, 50, 75, 100, 150],
+ "n_values": [25, 50, 75, 100],
},
{
"name": "merge",
"title": "CFG Merges",
"generator_func": generate_cpp_merge_test,
- "n_values": [10, 50, 100, 200, 400, 800],
+ "n_values": [400, 1000, 2000, 5000],
},
{
"name": "nested_loops",
"title": "Deeply Nested Loops",
"generator_func": generate_cpp_nested_loop_test,
- "n_values": [10, 50, 100, 200, 400, 800],
+ "n_values": [50, 100, 150, 200],
},
]
More information about the cfe-commits
mailing list