llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[fuzzer-found] [LAA] Compile time is cubic in the number of pointers that need a no-wrap predicate (regression in LLVM 23)

Open ubmaxxer opened this issue 1 week ago • 0 comments

Since #203787, the dependence check in LoopAccessAnalysis adds the no-wrap predicates of both pointers to PredicatedScalarEvolution again for every pair of accesses, and each add walks the predicates already held with SCEV queries. For a loop with N accesses whose pointers need such a predicate, that is about N³/2 implication checks. A 5-line function with one loop pragma takes 12.8 s to compile with LLVM 23.1.6 and 0.7 s with LLVM 22.1.8.

Reproducer

void f(int *a, int x, long n) {
#pragma clang loop vectorize(enable) vectorize_width(64) interleave_count(8) vectorize_predicate(enable)
  for (long i = 0; i < n; i++)
    a[i] = x;
}

clang -O3 -c repro.c, x86-64 Linux, official release binaries:

LLVM 22.1.8 LLVM 23.1.6
repro.c 0.7 s 12.8 s

Plain x86-64 has no masked store, so the tail-folded vector loop has 64 × 8 = 512 conditional scalar stores. LoopLoadElimination then requests LoopAccessAnalysis for that loop, and -ftime-report shows 97% of the time in LoopLoadEliminationPass. -O1 and -O2 take the same time. --target=aarch64-linux-gnu is also slow (12.6 s); -march=x86-64-v3 is not (0.03 s).

Without a pragma

The same shape written by hand: N stores to one array, each behind a bounds test, in a loop that steps by N.

import sys
N = int(sys.argv[1])
print("void f(int *a, int x, long n) {")
print("  for (long i = 0; i < n; i += %d) {" % N)
for k in range(N):
    print("    if (i + %d < n) a[i + %d] = x + %d;" % (k, k, k))
print("  }\n}")
$ python3 gen.py 512 > gen-512.c
$ time clang -O3 -c gen-512.c
N LLVM 22.1.8 LLVM 23.1.6
128 0.06 s 0.24 s
256 0.16 s 1.8 s
512 0.47 s 12.5 s
1024 1.8 s 101 s

Here the time is in LoopVectorizePass (98%), through LoopVectorizationLegality::canVectorizeMemory. This form does not depend on the target: N = 512 takes 12.6 to 13.6 s for x86-64 (also with -march=x86-64-v4), AArch64, Armv7, RISC-V, PowerPC, SystemZ, WebAssembly and LoongArch.

Where the time is spent

Stack for the generated loop, sampled with a debugger (main at 84a7a7d12ccc):

ScalarEvolution::isKnownPredicate
SCEVUnionPredicate::implies
PredicatedScalarEvolution::addPredicate
PredicatedScalarEvolution::addPredicates
MemoryDepChecker::getDependenceDistanceStrideAndSize
MemoryDepChecker::isDependent
MemoryDepChecker::areDepsSafe
LoopAccessInfo::analyzeLoop
  1. getDependenceDistanceStrideAndSize runs for every pair of accesses. It calls getPtrStride for both pointers and then PSE.addPredicates with what they collected.
  2. For these pointers isNoWrap collects an IncrementNUSW wrap predicate each time: the AddRec has no no-wrap flags, the access is in a block that needs predication, and the stride is not 1 or -1.
  3. PredicatedScalarEvolution::addPredicate first calls SCEVUnionPredicate::implies, which asks the predicates in the union one by one. For a wrap predicate on a different AddRec, SCEVWrapPredicate::implies calls isKnownPositive and isKnownPredicate.

With a counter added to the code (same commit), the 512-store loop calls PredicatedScalarEvolution::addPredicate 261,633 times, and all but 512 of those calls return early because the union already implies the predicate.

Before #203787, isNoWrap returned early through PSE.hasNoOverflow(Ptr, SCEVWrapPredicate::IncrementNUSW) once a pointer's predicate had been added, so each predicate was added once. The limit of 16 from #158652 is in SCEVUnionPredicate::add; the check in addPredicate runs before it and is not limited.

Notes

  • Confirmed by building opt at the commit and at its parent: at 871990a01ea7 (#203787) opt -O3 on the 512-store loop takes 17.8 s, at its parent 5b0cb22661a9 it takes 0.65 s.
  • Still present on main at 373d1cb22079: opt -O3 takes 19.2 s for the 512-store loop (build with assertions).
  • The generated code is not affected, only compile time.

One thing I tried: making SCEVUnionPredicate::implies return true right away when the predicate is already in the union. On main, the 512-store loop then takes 0.6 s instead of 19.2 s, and the opt -O3 output is unchanged. I haven’t prepared a PR, but I’m good to open one if you’d find it useful.

This problem was found with a fuzzer; the reproducers above were written by hand from its test case. The report was written with LLM assistance.

ubmaxxer avatar Nov 25 '26 08:11 ubmaxxer