[polly] [Polly] Keep the detection context of the expanded region (PR #229341)
Timur Baidusenov via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 6 01:47:44 PDT 2026
https://github.com/bai-tim created https://github.com/llvm/llvm-project/pull/229341
**Problem**
`ScopDetection::expandRegion` keeps the largest valid region found so far, but when a larger valid region replaced the previous one it erased the detection context with the key of the *new* region instead of the replaced one. So after two or more successful steps the expanded region has no context and the context of an earlier step stays in `DetectionContextMap` with a reference to a freed `Region`. `detect()` then matches each context to a valid region by the address of its `Region` when it prunes unprofitable regions, so depending on whether the next `Region` happens to be allocated at the freed address.
**Solution**
- Erase the context of the replaced region (its key is taken before the region is destroyed).
- The assertion at the end of `detect()` only compared the sizes of `ValidRegions` and `DetectionContextMap`, which the stale entries satisfy. It now checks that every valid region has a detection context.
**Run time, compile time and object size**
Run time is the minimum of 5 interleaved runs, compile time the CPU time (user + sys) of one compilation of the object, and object size the size of the `.o` file. The first rows are objects whose code the patch changes (61 of 4000, in 58 programs); the last three rows are totals. x86-64 (Xeon Gold 6348)
| program | run time | compile time (CPU) | object size (.o) |
|---|---|---|---|
| PolyBench doitgen | 1.220 s → 0.164 s (7.42×) | 2.03 s → 2.89 s (+43%) | 14.5 KiB → 29.3 KiB (+103%) |
| PolyBench syr2k | 7.352 s → 1.447 s (5.08×) | 0.14 s → 0.52 s (+286%) | 9.4 KiB → 15.4 KiB (+64%) |
| PolyBench syrk | 2.814 s → 0.919 s (3.06×) | 0.14 s → 0.48 s (+247%) | 9.2 KiB → 13.5 KiB (+47%) |
| PolyBench fdtd-2d | 2.215 s → 1.340 s (1.65×) | 0.21 s → 0.99 s (+370%) | 13.5 KiB → 20.7 KiB (+54%) |
| PolyBench 2mm | 5 ms → 5 ms † | 0.15 s → 0.67 s (+344%) | 19.7 KiB → 52.8 KiB (+168%) |
| PolyBench bicg | 30 ms → 24 ms † | 0.09 s → 0.23 s (+150%) | 6.8 KiB → 10.7 KiB (+57%) |
| PolyBench nussinov | 5.770 s → 5.805 s (0.99×) | 0.13 s → 1.00 s (+675%) | 7.4 KiB → 8.3 KiB (+13%) |
| Shootout ary3 | 0.382 s → 0.210 s (1.82×) | 0.05 s → 0.09 s (+84%) | 3.0 KiB → 3.1 KiB (+2%) |
| himenobmtxpa | 0.280 s → 0.275 s (1.02×) | 1.12 s → 0.22 s (-80%) | 68.1 KiB → 15.5 KiB (-77%) |
| TSVC (29 programs) | geomean 1.001× (min 0.988×, max 1.022×) | 82.27 s → 48.81 s (-41%) | 6.51 MB → 5.36 MB (-18%) |
| all 61 changed objects | – | 109.68 s → 74.81 s (-32%) | 7.95 MB → 6.83 MB (-14%); .text 5.27 MB → 4.23 MB (-20%) |
| whole test-suite (4000 objects) | – | 1006 s → 954 s (-5.2%) | 69.82 MB → 68.44 MB (-2.0%) |
Assisted-by: Claude (Anthropic)
>From 79deb6b6ddd512adcfeb4905ca8975e8c46dea32 Mon Sep 17 00:00:00 2001
From: Timur Baidusenov <timurbaidusenov at gmail.com>
Date: Mon, 5 Oct 2026 10:46:46 +0000
Subject: [PATCH] [Polly] Keep the detection context of the expanded region
When expandRegion replaces the largest valid region found so far by a larger one, it erased the detection context of the larger region instead of the replaced one, which left the expanded region without a context. The context of the replaced region in DetectionContextMap referring to freed Region. Because detect() matches a context to valid region by the address of its Region, the pruning of unprofitable regions then used the stale context or skipped the region depending on where the next Region was allocated, so profitable regions were dropped and the detected SCoPs varied between compilations of the same input. Erase the context of the replaced region and assert that every valid region has a detection context, which makes llvm-test-suite build reproducibly and makes PolyBench doitgen, syr2k, syrk and fdtd-2d 7.4, 5.0, 3.1 and 1.6 times faster without slowing down any other program.
Assisted-by: Claude (Anthropic)
---
polly/lib/Analysis/ScopDetection.cpp | 9 +-
.../expand-region-detection-context.ll | 95 +++++++++++++++++++
2 files changed, 101 insertions(+), 3 deletions(-)
create mode 100644 polly/test/ScopDetect/expand-region-detection-context.ll
diff --git a/polly/lib/Analysis/ScopDetection.cpp b/polly/lib/Analysis/ScopDetection.cpp
index 12cbee3a95a49..1831637272927 100644
--- a/polly/lib/Analysis/ScopDetection.cpp
+++ b/polly/lib/Analysis/ScopDetection.cpp
@@ -387,8 +387,11 @@ void ScopDetection::detect(Function &F) {
if (ReportLevel)
printLocations(F);
- assert(ValidRegions.size() <= DetectionContextMap.size() &&
- "Cached more results than valid regions");
+ assert(all_of(ValidRegions,
+ [this](const Region *R) {
+ return DetectionContextMap.contains(getBBPairForRegion(R));
+ }) &&
+ "Every valid region must have a detection context");
}
template <class RR, typename... Args>
@@ -1532,7 +1535,7 @@ Region *ScopDetection::expandRegion(Region &R) {
// far).
if (LastValidRegion) {
removeCachedResults(*LastValidRegion);
- DetectionContextMap.erase(P);
+ DetectionContextMap.erase(getBBPairForRegion(LastValidRegion.get()));
}
LastValidRegion = std::move(ExpandedRegion);
diff --git a/polly/test/ScopDetect/expand-region-detection-context.ll b/polly/test/ScopDetect/expand-region-detection-context.ll
new file mode 100644
index 0000000000000..0dbff417e8a3c
--- /dev/null
+++ b/polly/test/ScopDetect/expand-region-detection-context.ll
@@ -0,0 +1,95 @@
+; RUN: opt %loadNPMPolly -plugin-arg=Polly,-polly-process-unprofitable=false '-passes=polly-custom<detect>' -plugin-arg=Polly,-polly-print-detect -disable-output < %s 2>&1 | FileCheck %s
+;
+; The four loops are valid regions on their own, and ScopDetection expands the
+; first one by one loop at a time until the call to @ext stops it. Every valid
+; region has to keep its detection context while that happens; ScopDetection
+; asserts this when it has detected all regions.
+;
+; void ext(void);
+;
+; void f(int n, double *A, double *B, double *C, double *D) {
+; ext();
+; for (int i = 0; i < n; i++)
+; A[i] = 1.0;
+; for (int i = 0; i < n; i++)
+; B[i] = 2.0;
+; for (int i = 0; i < n; i++)
+; C[i] = 3.0;
+; for (int i = 0; i < n; i++)
+; D[i] = A[i] + B[i] + C[i];
+; ext();
+; }
+;
+; CHECK: Valid Region for Scop: for.body => for.cond.cleanup24
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @f(i32 %n, ptr %A, ptr %B, ptr %C, ptr %D) {
+entry:
+ tail call void @ext()
+ %cmp55 = icmp sgt i32 %n, 0
+ br i1 %cmp55, label %for.body.preheader, label %for.cond.cleanup24
+
+for.body.preheader: ; preds = %entry
+ %wide.trip.count = zext nneg i32 %n to i64
+ br label %for.body
+
+for.body5.preheader: ; preds = %for.body
+ %wide.trip.count67 = zext nneg i32 %n to i64
+ br label %for.body5
+
+for.body: ; preds = %for.body.preheader, %for.body
+ %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
+ %arrayidx = getelementptr inbounds nuw [8 x i8], ptr %A, i64 %indvars.iv
+ store double 1.000000e+00, ptr %arrayidx, align 8
+ %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+ %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
+ br i1 %exitcond.not, label %for.body5.preheader, label %for.body
+
+for.body15.preheader: ; preds = %for.body5
+ %wide.trip.count72 = zext nneg i32 %n to i64
+ br label %for.body15
+
+for.body5: ; preds = %for.body5.preheader, %for.body5
+ %indvars.iv64 = phi i64 [ 0, %for.body5.preheader ], [ %indvars.iv.next65, %for.body5 ]
+ %arrayidx7 = getelementptr inbounds nuw [8 x i8], ptr %B, i64 %indvars.iv64
+ store double 2.000000e+00, ptr %arrayidx7, align 8
+ %indvars.iv.next65 = add nuw nsw i64 %indvars.iv64, 1
+ %exitcond68.not = icmp eq i64 %indvars.iv.next65, %wide.trip.count67
+ br i1 %exitcond68.not, label %for.body15.preheader, label %for.body5
+
+for.body25.preheader: ; preds = %for.body15
+ %wide.trip.count77 = zext nneg i32 %n to i64
+ br label %for.body25
+
+for.body15: ; preds = %for.body15.preheader, %for.body15
+ %indvars.iv69 = phi i64 [ 0, %for.body15.preheader ], [ %indvars.iv.next70, %for.body15 ]
+ %arrayidx17 = getelementptr inbounds nuw [8 x i8], ptr %C, i64 %indvars.iv69
+ store double 3.000000e+00, ptr %arrayidx17, align 8
+ %indvars.iv.next70 = add nuw nsw i64 %indvars.iv69, 1
+ %exitcond73.not = icmp eq i64 %indvars.iv.next70, %wide.trip.count72
+ br i1 %exitcond73.not, label %for.body25.preheader, label %for.body15
+
+for.cond.cleanup24: ; preds = %for.body25, %entry
+ tail call void @ext()
+ ret void
+
+for.body25: ; preds = %for.body25.preheader, %for.body25
+ %indvars.iv74 = phi i64 [ 0, %for.body25.preheader ], [ %indvars.iv.next75, %for.body25 ]
+ %arrayidx27 = getelementptr inbounds nuw [8 x i8], ptr %A, i64 %indvars.iv74
+ %0 = load double, ptr %arrayidx27, align 8
+ %arrayidx29 = getelementptr inbounds nuw [8 x i8], ptr %B, i64 %indvars.iv74
+ %1 = load double, ptr %arrayidx29, align 8
+ %add = fadd double %0, %1
+ %arrayidx31 = getelementptr inbounds nuw [8 x i8], ptr %C, i64 %indvars.iv74
+ %2 = load double, ptr %arrayidx31, align 8
+ %add32 = fadd double %add, %2
+ %arrayidx34 = getelementptr inbounds nuw [8 x i8], ptr %D, i64 %indvars.iv74
+ store double %add32, ptr %arrayidx34, align 8
+ %indvars.iv.next75 = add nuw nsw i64 %indvars.iv74, 1
+ %exitcond78.not = icmp eq i64 %indvars.iv.next75, %wide.trip.count77
+ br i1 %exitcond78.not, label %for.cond.cleanup24, label %for.body25
+}
+
+declare void @ext()
More information about the llvm-commits
mailing list