bug report on mem2reg

Hi, all,

I think I found a bug on mem2reg in a particular case.
When I ran opt -mem2reg -basicaa -scalar-evolution -loops -da -analyze opts.ll, it collapsed. While without mem2reg option, it runs well. so I guess it has something to do with mem2reg.
The relative files have been affixed, please check them for me.
Thanks in advance.

opts.c (21.8 KB)

opts.h (1.67 KB)

opts.ll (202 KB)

Tried testing it with things like Undefined Behaivor Sanitizer (or other sanitizers - memory/address) or valgrind?

Often “program works sometimes and doesn’t work other times” is because the program has undefined behavior in it & the times its working are just luck.

Sorry, I’m not sure I’ll be able to help you - main.c uses other code (optsfind/check/etc) that I don’t have/don’t know what that code is, and even with it it’s probably a bit big/complicated for me to debug/reduce/etc. If you can reconstruct the smallest complete (reducing the total lines of all source/header files needed to reproduce the problem) example that might be more practical to discuss here & see whether it’s a mem2reg bug, etc.

This is still a rather long test case - I expect it should be able to be reduced further while still demonstrating the problem.

I also don’t understand what the correct/incorrect behavior it is you’re looking for - at least casually, the program prints the same result at -O0, which doesn’t run mem2reg, and at -O3 which does run mem2reg.

This is still a rather long test case - I expect it should be able to be
reduced further while still demonstrating the problem.

I also don't understand what the correct/incorrect behavior it is you're
looking for - at least casually, the program prints the same result at -O0,
which doesn't run mem2reg, and at -O3 which does run mem2reg.

Agreed the case is complex. After clang -O3 -S -emit-llvm opts.c

opt -da -analyze opts.ll => assertion failure at
https://github.com/llvm/llvm-project/blob/master/llvm/lib/Analysis/DependenceAnalysis.cpp#L1155

   // CommonLevels == 0
   assert(0 < Level && Level <= CommonLevels && "level out of range");

So there is definitely a bug in dependency analysis, but I haven't spent time investigating it.