Skip to content

Conversation

fhahn
Copy link
Contributor

@fhahn fhahn commented Sep 9, 2025

If C2 >u C1 and C1 >u 1, fold to A /u (C2 /u C1).

Depends on #157555.

Alive2 Proof: https://alive2.llvm.org/ce/z/BWvQYN

@llvmbot llvmbot added llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms labels Sep 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 9, 2025

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-transforms

Author: Florian Hahn (fhahn)

Changes

If C2 >u C1 and C1 >u 1, fold to A /u (C2 /u C1).

Depends on #157555. (included in PR)

Alive2 Proof: https://alive2.llvm.org/ce/z/BWvQYN


Full diff: https://github.com/llvm/llvm-project/pull/157656.diff

3 Files Affected:

  • (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+16-6)
  • (modified) llvm/test/Analysis/ScalarEvolution/mul-udiv-folds.ll (+2-2)
  • (modified) llvm/test/Transforms/LoopStrengthReduce/duplicated-phis.ll (+1-2)
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 34e497d9ea3cb..9f5b33f50cc0b 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -3217,15 +3217,25 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
       }
 
       // Try to fold (C1 * D /u C2) -> C1/C2 * D, if C1 and C2 are powers-of-2,
-      // D is a multiple of C2, and C1 is a multiple of C1.
+      // D is a multiple of C2, and C1 is a multiple of C2. If C2 is a multiple
+      // of C1, fold to (D /u (C2 /u C1)).
       const SCEV *D;
+      APInt C1V = LHSC->getAPInt();
+      // If C1 is negative, try (-1 * abs(C1)) instead.
+      if (C1V.isNegative() && !C1V.isMinSignedValue())
+        C1V = C1V.abs();
       const SCEVConstant *C2;
-      const APInt &LHSV = LHSC->getAPInt();
-      if (LHSV.isPowerOf2() &&
+      if (C1V.isPowerOf2() &&
           match(Ops[1], m_scev_UDiv(m_SCEV(D), m_SCEVConstant(C2))) &&
-          C2->getAPInt().isPowerOf2() && LHSV.uge(C2->getAPInt()) &&
-          LHSV.logBase2() <= getMinTrailingZeros(D)) {
-        return getMulExpr(getUDivExpr(LHSC, C2), D);
+          C2->getAPInt().isPowerOf2() &&
+          C1V.logBase2() <= getMinTrailingZeros(D)) {
+        const SCEV *NewMul = nullptr;
+        if (C1V.uge(C2->getAPInt()))
+          NewMul = getMulExpr(getUDivExpr(getConstant(C1V), C2), D);
+        else if (C1V.ugt(1))
+          NewMul = getUDivExpr(D, getUDivExpr(C2, getConstant(C1V)));
+        if (NewMul)
+          return C1V == LHSC->getAPInt() ? NewMul : getNegativeSCEV(NewMul);
       }
     }
   }
diff --git a/llvm/test/Analysis/ScalarEvolution/mul-udiv-folds.ll b/llvm/test/Analysis/ScalarEvolution/mul-udiv-folds.ll
index dfaf0c95bc2f8..1d34706baadeb 100644
--- a/llvm/test/Analysis/ScalarEvolution/mul-udiv-folds.ll
+++ b/llvm/test/Analysis/ScalarEvolution/mul-udiv-folds.ll
@@ -21,9 +21,9 @@ define void @udiv4_and_udiv2(i1 %c, ptr %A) {
 ; CHECK-NEXT:    %gep.8 = getelementptr i8, ptr %A, i64 %iv
 ; CHECK-NEXT:    --> {(((zext i32 %start to i64) /u 4) + %A),+,1}<%loop> U: full-set S: full-set Exits: (((zext i32 %start to i64) /u 2) + %A) LoopDispositions: { %loop: Computable }
 ; CHECK-NEXT:    %gep.16 = getelementptr i16, ptr %A, i64 %iv
-; CHECK-NEXT:    --> {((2 * ((zext i32 %start to i64) /u 4))<nuw><nsw> + %A),+,2}<%loop> U: full-set S: full-set Exits: ((zext i32 %start to i64) + %A) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    --> {(((zext i32 %start to i64) /u 2) + %A),+,2}<%loop> U: full-set S: full-set Exits: ((zext i32 %start to i64) + %A) LoopDispositions: { %loop: Computable }
 ; CHECK-NEXT:    %gep.32 = getelementptr i32, ptr %A, i64 %iv
-; CHECK-NEXT:    --> {((zext i32 %start to i64) + %A),+,4}<%loop> U: full-set S: full-set Exits: ((3 * (zext i32 %start to i64))<nuw><nsw> + (-4 * ((zext i32 %start to i64) /u 4))<nsw> + %A) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    --> {((zext i32 %start to i64) + %A),+,4}<%loop> U: full-set S: full-set Exits: ((2 * (zext i32 %start to i64))<nuw><nsw> + %A) LoopDispositions: { %loop: Computable }
 ; CHECK-NEXT:    %gep.40 = getelementptr <{ i32, i8 }>, ptr %A, i64 %iv
 ; CHECK-NEXT:    --> {((5 * ((zext i32 %start to i64) /u 4))<nuw><nsw> + %A),+,5}<%loop> U: full-set S: full-set Exits: ((5 * ((zext i32 %start to i64) /u 2))<nuw><nsw> + %A) LoopDispositions: { %loop: Computable }
 ; CHECK-NEXT:    %gep.48 = getelementptr <{ i32, i16 }>, ptr %A, i64 %iv
diff --git a/llvm/test/Transforms/LoopStrengthReduce/duplicated-phis.ll b/llvm/test/Transforms/LoopStrengthReduce/duplicated-phis.ll
index cee8c8abdb450..c59f7d9c2a41a 100644
--- a/llvm/test/Transforms/LoopStrengthReduce/duplicated-phis.ll
+++ b/llvm/test/Transforms/LoopStrengthReduce/duplicated-phis.ll
@@ -18,8 +18,7 @@ define i64 @test_duplicated_phis(i64 noundef %N) {
 ; CHECK:       [[FOR_BODY_PREHEADER_NEW]]:
 ; CHECK-NEXT:    [[UNROLL_ITER:%.*]] = and i64 [[MUL]], -4
 ; CHECK-NEXT:    [[TMP4:%.*]] = add i64 [[UNROLL_ITER]], -4
-; CHECK-NEXT:    [[TMP5:%.*]] = lshr i64 [[TMP4]], 2
-; CHECK-NEXT:    [[TMP3:%.*]] = shl nuw nsw i64 [[TMP5]], 1
+; CHECK-NEXT:    [[TMP3:%.*]] = lshr i64 [[TMP4]], 1
 ; CHECK-NEXT:    [[LSR_IV_NEXT:%.*]] = sub i64 -3, [[TMP3]]
 ; CHECK-NEXT:    br label %[[FOR_BODY:.*]]
 ; CHECK:       [[FOR_BODY]]:

@fhahn fhahn force-pushed the scev-div-larger-than-mul branch from 9881292 to 86bb7d6 Compare September 10, 2025 14:36
Copy link
Contributor Author

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebased after landing #157555

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could assert this, as C1 == 0, and C1 == 1 must be folded away earlier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. This required excluding C1 = -1 above, otherwise we would end up with -1 * 1, triggering the assert

@fhahn fhahn force-pushed the scev-div-larger-than-mul branch from 86bb7d6 to 5764262 Compare September 10, 2025 17:59
@fhahn fhahn enabled auto-merge (squash) September 11, 2025 07:31
@fhahn fhahn merged commit 70012fd into llvm:main Sep 11, 2025
9 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 11, 2025

LLVM Buildbot has detected a new failure on builder lldb-x86_64-debian running on lldb-x86_64-debian while building llvm at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/30895

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-api :: functionalities/thread/finish-from-empty-func/TestEmptyFuncThreadStepOut.py (815 of 3188)
PASS: lldb-api :: commands/disassemble/basic/TestDisassembleBreakpoint.py (816 of 3188)
PASS: lldb-api :: python_api/findvalue_duplist/TestSBFrameFindValue.py (817 of 3188)
PASS: lldb-api :: functionalities/thread/backtrace_limit/TestBacktraceLimit.py (818 of 3188)
PASS: lldb-api :: linux/mix-dwo-and-regular-objects/TestMixedDwarfBinary.py (819 of 3188)
PASS: lldb-api :: arm/emulation/TestEmulations.py (820 of 3188)
PASS: lldb-api :: python_api/watchpoint/TestWatchpointIter.py (821 of 3188)
PASS: lldb-api :: commands/disassemble/basic/TestFrameDisassemble.py (822 of 3188)
PASS: lldb-shell :: SymbolFile/NativePDB/find-functions.cpp (823 of 3188)
PASS: lldb-api :: linux/add-symbols/TestTargetSymbolsAddCommand.py (824 of 3188)
FAIL: lldb-api :: functionalities/postmortem/netbsd-core/TestNetBSDCore.py (825 of 3188)
******************** TEST 'lldb-api :: functionalities/postmortem/netbsd-core/TestNetBSDCore.py' FAILED ********************
Script:
--
/usr/bin/python3 /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./lib --env LLVM_INCLUDE_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/include --env LLVM_TOOLS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./bin --arch x86_64 --build-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex --lldb-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/lldb --compiler /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/clang --dsymutil /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./bin --lldb-obj-root /home/worker/2.0.1/lldb-x86_64-debian/build/tools/lldb --lldb-libs-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./lib --cmake-build-type Release -t /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/functionalities/postmortem/netbsd-core -p TestNetBSDCore.py
--
Exit Code: -6

Command Output (stdout):
--
lldb version 22.0.0git (https://github.com/llvm/llvm-project.git revision 70012fda6312ba87bc0bf9009402e0869a816d1f)
  clang revision 70012fda6312ba87bc0bf9009402e0869a816d1f
  llvm revision 70012fda6312ba87bc0bf9009402e0869a816d1f
Skipping the following test categories: ['libc++', 'msvcstl', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
Change dir to: /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/functionalities/postmortem/netbsd-core
runCmd: settings clear --all

output: 

runCmd: settings set symbols.enable-external-lookup false

output: 

runCmd: settings set target.inherit-tcc true

output: 

runCmd: settings set target.disable-aslr false

output: 

runCmd: settings set target.detach-on-error false

output: 


@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 11, 2025

LLVM Buildbot has detected a new failure on builder lldb-aarch64-ubuntu running on linaro-lldb-aarch64-ubuntu while building llvm at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/59/builds/24048

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-api :: functionalities/postmortem/elf-core/expr/TestExpr.py (571 of 2318)
PASS: lldb-api :: functionalities/postmortem/elf-core/gcore/TestGCore.py (572 of 2318)
PASS: lldb-api :: functionalities/postmortem/elf-core/thread_crash/TestLinuxCoreThreads.py (573 of 2318)
PASS: lldb-api :: functionalities/plugins/python_os_plugin/stepping_plugin_threads/TestOSPluginStepping.py (574 of 2318)
PASS: lldb-api :: functionalities/postmortem/minidump-new/TestMiniDumpNew.py (575 of 2318)
PASS: lldb-api :: functionalities/postmortem/minidump/TestMiniDump.py (576 of 2318)
PASS: lldb-api :: functionalities/postmortem/minidump-new/TestMiniDumpUUID.py (577 of 2318)
UNSUPPORTED: lldb-api :: functionalities/pre_run_dylibs/TestPreRunDylibs.py (578 of 2318)
PASS: lldb-api :: functionalities/postmortem/wow64_minidump/TestWow64MiniDump.py (579 of 2318)
UNRESOLVED: lldb-api :: functionalities/postmortem/netbsd-core/TestNetBSDCore.py (580 of 2318)
******************** TEST 'lldb-api :: functionalities/postmortem/netbsd-core/TestNetBSDCore.py' FAILED ********************
Script:
--
/usr/bin/python3.10 /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib --env LLVM_INCLUDE_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/include --env LLVM_TOOLS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --arch aarch64 --build-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex --lldb-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/lldb --compiler /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/clang --dsymutil /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --lldb-obj-root /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb --lldb-libs-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib --cmake-build-type Release /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/functionalities/postmortem/netbsd-core -p TestNetBSDCore.py
--
Exit Code: -6

Command Output (stdout):
--
lldb version 22.0.0git (https://github.com/llvm/llvm-project.git revision 70012fda6312ba87bc0bf9009402e0869a816d1f)
  clang revision 70012fda6312ba87bc0bf9009402e0869a816d1f
  llvm revision 70012fda6312ba87bc0bf9009402e0869a816d1f
Skipping the following test categories: ['libc++', 'msvcstl', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_aarch64_single_threaded (TestNetBSDCore.NetBSD1LWPCoreTestCase)
double free or corruption (fasttop)
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
 #0 0x0000e3b42768d7ac llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) Signals.cpp:0:0
 #1 0x0000e3b42768b2c8 llvm::sys::RunSignalHandlers() Signals.cpp:0:0
 #2 0x0000e3b42768e5c8 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #3 0x0000e3b42e3048f8 (linux-vdso.so.1+0x8f8)
 #4 0x0000e3b42e08f1f0 __pthread_kill_implementation ./nptl/./nptl/pthread_kill.c:44:76
 #5 0x0000e3b42e04a67c gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #6 0x0000e3b42e037130 abort ./stdlib/./stdlib/abort.c:81:7
 #7 0x0000e3b42e083300 __libc_message ./libio/../sysdeps/posix/libc_fatal.c:154:6
 #8 0x0000e3b42e09956c ./malloc/./malloc/malloc.c:5668:3
 #9 0x0000e3b42e09b244 _int_free ./malloc/./malloc/malloc.c:4700:5
#10 0x0000e3b42e09dc74 __libc_free ./malloc/./malloc/malloc.c:3394:3
#11 0x0000e3b427484154 ProcessElfCore::~ProcessElfCore() ProcessElfCore.cpp:0:0
#12 0x0000e3b4270d0b28 lldb_private::Target::DeleteCurrentProcess() Target.cpp:0:0
#13 0x0000e3b4270d2ab4 lldb_private::Target::Destroy() Target.cpp:0:0
#14 0x0000e3b426c7538c lldb::SBDebugger::DeleteTarget(lldb::SBTarget&) (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/local/lib/python3.10/dist-packages/lldb/_lldb.cpython-310-aarch64-linux-gnu.so+0x578538c)
#15 0x0000e3b426db0da0 _wrap_SBDebugger_DeleteTarget(_object*, _object*) LLDBWrapPython.cpp:0:0
#16 0x0000b7bbf7943c64 (/usr/bin/python3.10+0x103c64)
#17 0x0000b7bbf793a240 _PyObject_MakeTpCall (/usr/bin/python3.10+0xfa240)
#18 0x0000b7bbf7931518 _PyEval_EvalFrameDefault (/usr/bin/python3.10+0xf1518)

@fhahn fhahn deleted the scev-div-larger-than-mul branch September 11, 2025 08:35
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Sep 11, 2025
@durin42
Copy link
Contributor

durin42 commented Sep 11, 2025

I'm seeing some segfaults in Rust that root-cause to this change, I'll try and figure out more later.

@DKLoehr
Copy link
Contributor

DKLoehr commented Sep 11, 2025

We've started seeing crashes in the rust compiler when using head LLVM, and it bisected to this commit. Unfortunately I'm not sure how to put together a useful reproducer. It's possible there's a bug here or that it simply revealed one elsewhere.

@fhahn
Copy link
Contributor Author

fhahn commented Sep 11, 2025

Crashes in LLVM directly or a mis-compile that is causing the crash?

@ilovepi
Copy link
Contributor

ilovepi commented Sep 11, 2025

We're seeing one of the msan tests fail after this patch: MemorySanitizer-AARCH64 :: preinit_array.cpp, and I've bisected the failure to this patch. I'm guessing the other failures are somewhat related.

Bot: https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-linux-arm64/b8704035006372869713/overview

Blame: https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-linux-arm64/b8704035006372869713/blamelist

Logs: https://logs.chromium.org/logs/fuchsia/buildbucket/cr-buildbucket/8704035006372869713/+/u/clang/test/stdout

This is a normal aarch64 linux build of the toolchain and runtimes using the Fuchsia.cmake cache file. Its a pretty vanilla build otherwise. The CMake can be found here: https://logs.chromium.org/logs/fuchsia/buildbucket/cr-buildbucket/8704035006372869713/+/u/clang/configure/l_execution_details

Command Output (stderr):
--
/b/s/w/ir/x/w/llvm_build/./bin/clang  --driver-mode=g++ -fsanitize=memory -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls   -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta   -gline-tables-only -O0 /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/test/msan/preinit_array.cpp -o /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.tmp &&  /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.tmp # RUN: at line 1
+ /b/s/w/ir/x/w/llvm_build/./bin/clang --driver-mode=g++ -fsanitize=memory -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta -gline-tables-only -O0 /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/test/msan/preinit_array.cpp -o /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.tmp
+ /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.tmp
/b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.script: line 1: 1053327 Segmentation fault      /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.tmp

@aeubanks
Copy link
Contributor

Crashes in LLVM directly or a mis-compile that is causing the crash?

Looks like rustc crashing in non-LLVM code, so likely a miscompile rather than LLVM crash

@ilovepi
Copy link
Contributor

ilovepi commented Sep 12, 2025

We're seeing one of the msan tests fail after this patch: MemorySanitizer-AARCH64 :: preinit_array.cpp, and I've bisected the failure to this patch. I'm guessing the other failures are somewhat related.

Bot: https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-linux-arm64/b8704035006372869713/overview

Blame: https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-linux-arm64/b8704035006372869713/blamelist

Logs: https://logs.chromium.org/logs/fuchsia/buildbucket/cr-buildbucket/8704035006372869713/+/u/clang/test/stdout

This is a normal aarch64 linux build of the toolchain and runtimes using the Fuchsia.cmake cache file. Its a pretty vanilla build otherwise. The CMake can be found here: https://logs.chromium.org/logs/fuchsia/buildbucket/cr-buildbucket/8704035006372869713/+/u/clang/configure/l_execution_details

Command Output (stderr):
--
/b/s/w/ir/x/w/llvm_build/./bin/clang  --driver-mode=g++ -fsanitize=memory -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls   -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta   -gline-tables-only -O0 /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/test/msan/preinit_array.cpp -o /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.tmp &&  /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.tmp # RUN: at line 1
+ /b/s/w/ir/x/w/llvm_build/./bin/clang --driver-mode=g++ -fsanitize=memory -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta -gline-tables-only -O0 /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/test/msan/preinit_array.cpp -o /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.tmp
+ /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.tmp
/b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.script: line 1: 1053327 Segmentation fault      /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-aarch64-unknown-linux-gnu-bins/compiler-rt/test/msan/AARCH64/Output/preinit_array.cpp.tmp

Yeah I think the bisect logic failed and it's a different patch. Sorry for the noise. The Rust problem may be unrelated. Thanks @aeubanks for pointing this out.

@mikaelholmen
Copy link
Collaborator

Was the talk about miscompiles above here false alarm or real problems?

We're seeing a runtime failure downstream with this patch that I think is a miscompile but we haven't been able to pinpoint what's happening, so wondering what the status really is about these other previous reports.

@fhahn
Copy link
Contributor Author

fhahn commented Sep 12, 2025

@mikaelholmen I think it sounds like there may be a potential mis-compile when bootstrapping the Rust compiler IIUC. We can revert the PR, but without reproducer it is impossible to figure out exactly where the issue is.

@mikaelholmen
Copy link
Collaborator

Yes I understand.

I think something breaks in LSR in llc for our out-of-tree target with the patch, but I'm not sure what and even with the reduced C reproducer I have it's hard to understand. And the code diff I see after LSR in llc does not appear if I run LSR alone in opt so there apparently must be some state present from earlier passes for the difference to appear.

We'll continue digging next week but unfortunately the whole team is off Monday-Tuesday so progress from us (if any) will probably not happen until second half of next week.

Hopefully someone else can beat us to it and come up with a reproducer earlier.

@durin42
Copy link
Contributor

durin42 commented Sep 12, 2025

Sorry, life got in the way and I'm just back to this. Yes, it looks like it's probably a miscompile leading to a segfault in rustc: it only happens in a stage2 compiler, and the tracebacks don't seem to have any LLVM frames in them.

I'm not really sure what to do in terms of reducing the failure tho. rustc is pretty big.

rnk added a commit that referenced this pull request Sep 12, 2025
rnk added a commit that referenced this pull request Sep 12, 2025
…158328)

Reverts #157656

There are multiple reports that this is causing miscompiles in the MSan
test suite after bootstrapping and that this is causing miscompiles in
rustc. Let's revert for now, and work to capture a reproducer next week.
@zmodem
Copy link
Collaborator

zmodem commented Sep 15, 2025

We're crashing in _RINvMs_NtCsrCDUsq4Rvv_9rustc_abi6layoutINtB5_16LayoutCalculatorNtNtNtCs1Y6DsoOfemJ_12rustc_middle2ty7context6TyCtxtE17univariant_biasedNtNtB5_2ty8FieldIdxNtB2b_10VariantIdxINtB2b_11TyAndLayoutNtB13_2TyEECs5vQwWmLTrEi_14rustc_ty_utils

That comes from rustc_ty_utils-c1870303faf57b40.rustc_ty_utils.4038986b86944b28-cgu.15.rcgu.bc.gz and the function has a large diff after this change.

It's still not entirely clear what's going wrong though; we'll keep looking.

llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Sep 16, 2025
…C2 > C1." (#158328)

Reverts llvm/llvm-project#157656

There are multiple reports that this is causing miscompiles in the MSan
test suite after bootstrapping and that this is causing miscompiles in
rustc. Let's revert for now, and work to capture a reproducer next week.
@zmodem
Copy link
Collaborator

zmodem commented Sep 16, 2025

A reduced test case:

$ cat /tmp/reduce.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 fastcc i64 @foo(i64 %0) {
  %2 = shl i64 %0, 1
  %3 = lshr exact i64 %2, 4
  %4 = and i64 %3, 1152921504606846974
  br label %5

5:                                                ; preds = %5, %1
  %6 = phi i64 [ 0, %1 ], [ %8, %5 ]
  %7 = phi i64 [ 0, %1 ], [ %9, %5 ]
  %8 = add i64 %6, 2
  %9 = add i64 %7, 2
  %10 = icmp eq i64 %9, %4
  br i1 %10, label %.loopexit14, label %5

.loopexit14:                                      ; preds = %5
  ret i64 %8
}

After this change, loop-reduce will remove the and:

$ build/bin/opt.bad -S -loop-reduce -o - /tmp/reduce.ll
; ModuleID = '/tmp/reduce.ll'
source_filename = "/tmp/reduce.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 fastcc i64 @foo(i64 %0) {
  %2 = shl i64 %0, 1
  %3 = lshr exact i64 %2, 4
  br label %4

4:                                                ; preds = %4, %1
  %5 = phi i64 [ 0, %1 ], [ %6, %4 ]
  %6 = add i64 %5, 2
  %7 = icmp eq i64 %3, %6
  br i1 %7, label %.loopexit14, label %4

.loopexit14:                                      ; preds = %4
  ret i64 %6
}

which is not valid (consider for example %0 = 8).

fhahn added a commit that referenced this pull request Sep 17, 2025
Add additional test coverage for
#157656 covering cases where
the dividend is not a multiple of the divisor, causing the revert of
70012fd.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Sep 17, 2025
Add additional test coverage for
llvm/llvm-project#157656 covering cases where
the dividend is not a multiple of the divisor, causing the revert of
70012fd.
fhahn added a commit that referenced this pull request Sep 17, 2025
…158328)

This reverts commit fd58f23.

The recommit contains an extra check to make sure that D is a multiple of
C2, if C2 > C1. This fixes the issue causing the revert fd58f23. Tests
have been added in 6a726e9.

Original message:
If C2 >u C1 and C1 >u 1, fold to A /u (C2 /u C1).

Depends on #157555.

Alive2 Proof: https://alive2.llvm.org/ce/z/BWvQYN

PR: #157656
@fhahn
Copy link
Contributor Author

fhahn commented Sep 17, 2025

@zmodem thank you very much for the reproducer! The issue was a missing check that D is a multiple of C2, if C2 > 1. Recommitted with an extra check: f78150d

llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Sep 17, 2025
… C2 > C1." (#158328)

This reverts commit fd58f23.

The recommit contains an extra check to make sure that D is a multiple of
C2, if C2 > C1. This fixes the issue causing the revert fd58f23. Tests
have been added in 6a726e9.

Original message:
If C2 >u C1 and C1 >u 1, fold to A /u (C2 /u C1).

Depends on llvm/llvm-project#157555.

Alive2 Proof: https://alive2.llvm.org/ce/z/BWvQYN

PR: llvm/llvm-project#157656
@mikaelholmen
Copy link
Collaborator

I've verified that the testcase previously failing for us with the original patch still works after the reapplied patch including the fix.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants