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

Assertion failure in SCEV - "Your comparator is not a valid strict-weak ordering"

Open alexfh opened this issue 1 year ago • 8 comments

While debugging another issue, we found this assertion failure arising from this stable_sort invocation: https://github.com/llvm/llvm-project/blame/main/llvm/lib/Analysis/ScalarEvolution.cpp#L795, which is using this comparator: https://github.com/llvm/llvm-project/blame/main/llvm/lib/Analysis/ScalarEvolution.cpp#L781.

This only happens when clang is compiled with the corresponding libc++ hardening enabled (-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG).

A reduced test case:

$ cat reduced.ll
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 ptr @_f1(ptr %this, ptr %__first) {
entry:
  %call = call i64 @_f2(ptr %this, ptr %__first)
  %call3 = call ptr @_f3(ptr %this, i64 %call)
  ret ptr null
}

define void @_f4(ptr %__from_s, ptr %__to) {
entry:
  %0 = load ptr, ptr %__from_s, align 8
  %sub.ptr.lhs.cast = ptrtoint ptr %0 to i64
  %sub.ptr.rhs.cast = ptrtoint ptr %__to to i64
  %sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast
  %add.ptr = getelementptr i8, ptr %__from_s, i64 %sub.ptr.sub
  br label %for.cond

for.cond:                                         ; preds = %for.cond, %entry
  %__i.0 = phi ptr [ %add.ptr, %entry ], [ %incdec.ptr, %for.cond ]
  store volatile ptr %__i.0, ptr null, align 8
  %incdec.ptr = getelementptr i8, ptr %__i.0, i64 1
  br label %for.cond
}

define ptr @_f3(ptr %this, i64 %__n) {
entry:
  %0 = load ptr, ptr %this, align 8
  %call3 = load volatile i64, ptr null, align 8
  %add.ptr = getelementptr i8, ptr %0, i64 %call3
  %add.ptr16 = getelementptr i8, ptr %add.ptr, i64 %__n
  call void @_f4(ptr %add.ptr, ptr %add.ptr16)
  ret ptr null
}

define i64 @_f2(ptr %__first, ptr %__last) {
entry:
  %call = call i64 @_f5(ptr %__first, ptr %__last)
  ret i64 %call
}

define i64 @_f5(ptr %__first, ptr %__last) {
entry:
  %sub.ptr.lhs.cast = ptrtoint ptr %__last to i64
  %sub.ptr.rhs.cast = ptrtoint ptr %__first to i64
  %sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast
  ret i64 %sub.ptr.sub
}
$ ./clang-bad -O1 -c reduced.ll -o -
libcxx/include/__debug_utils/strict_weak_ordering_check.h:59: assertion __comp(*(__first + __a), *(__first + __b)) failed: Your comparator is not a valid strict-weak ordering
...

alexfh avatar Mar 10 '25 23:03 alexfh

@nikic @xortator @preames

alexfh avatar Mar 11 '25 00:03 alexfh

Sounds similar to https://github.com/llvm/llvm-project/pull/100721.

nikic avatar Mar 11 '25 08:03 nikic

Sounds similar to #100721.

Indeed, then also @jreiffers

alexfh avatar Mar 11 '25 15:03 alexfh

Yeah, looks like it's the same issue. Did you check the hit rate of the cache?

jreiffers avatar Mar 12 '25 08:03 jreiffers

We are now seeing miscompiles likely related to this issue.

Can you please give this a higher priority?

eaeltsin avatar Mar 26 '25 13:03 eaeltsin

$ cat /tmp/reduced.ll
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-grtev4-linux-gnu"

%"class.llvm::StringRef" = type { ptr, i64 }

define ptr @f(ptr %this, ptr %__position.coerce, ptr %__first.coerce, i64 %__n, ptr %0) {
entry:
  %sub.ptr.lhs.cast.i1 = ptrtoint ptr %this to i64
  %sub.ptr.rhs.cast.i = ptrtoint ptr %__first.coerce to i64
  %sub.ptr.sub.i = sub i64 %sub.ptr.lhs.cast.i1, %sub.ptr.rhs.cast.i
  %add.ptr = getelementptr i8, ptr %0, i64 %sub.ptr.sub.i
  %add.ptr257 = getelementptr %"class.llvm::StringRef", ptr %add.ptr, i64 %__n
  %sub.ptr.rhs.cast.i24 = ptrtoint ptr %__position.coerce to i64
  %sub.ptr.sub.i25 = sub i64 0, %sub.ptr.rhs.cast.i24
  %add.ptr.i = getelementptr i8, ptr %add.ptr, i64 %sub.ptr.sub.i25
  ret ptr null
}
$ opt /tmp/reduced.ll -p slp-vectorizer -disable-output

more reduced

aeubanks avatar Mar 26 '25 16:03 aeubanks

printing out the results of IsLessComplex() on all inputs here, I get

0 (16 * %__n)
1 (-1 * (ptrtoint ptr %__first.coerce to i64))
2 (ptrtoint ptr %this to i64)
3 (-1 * (ptrtoint ptr %this to i64))
4 (ptrtoint ptr %__position.coerce to i64)
5 (ptrtoint ptr %__first.coerce to i64)
  0 1 true
  1 0 false
  0 2 true
  2 0 false
  0 3 true
  3 0 false
  0 4 true
  4 0 false
  0 5 true
  5 0 false
  1 2 true
  2 1 false
  1 3 false
  3 1 false
  1 4 true
  4 1 false
  1 5 true
  5 1 false
  2 3 false
  3 2 true
  2 4 true
  4 2 false
  2 5 false
  5 2 false
  3 4 true
  4 3 false
  3 5 true
  5 3 false
  4 5 true
  5 4 false

2 -> 4 is true and 4 -> 5 is true, but 2 -> 5 is not true, breaking transitivity

aeubanks avatar Mar 26 '25 19:03 aeubanks

@rnk pointed out https://github.com/llvm/llvm-project/blob/25f4f0a56de741a8120cfeab297211e9505ec1d7/llvm/lib/Analysis/ScalarEvolution.cpp#L581 which just bails on comparing values if the depth > 2. the comparison needs to be correct, and bumping up the depth value seems to fix a miscompile we're seeing

aeubanks avatar Mar 26 '25 20:03 aeubanks