Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reland "[Transforms] LoopIdiomRecognize recognize strlen and wcslen (#108985)" #131412

Merged
merged 18 commits into from
Mar 21, 2025

Conversation

mustartt
Copy link
Member

@mustartt mustartt commented Mar 15, 2025

Relands #108985

Fixed the logic error in #108985 that does not bailout if the library function is not emitable.

if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen) &&
    !DisableLIRP::Wcslen)
  return false;

=>

if (DisableLIRP::Wcslen)
  return false;
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen))
  return false;

Fixed this issue and added a test for checking emitable.

Copy link

github-actions bot commented Mar 15, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@mustartt mustartt force-pushed the strlen-loop-idiom branch from 3aa7cce to 0e951d8 Compare March 15, 2025 03:12
@mustartt mustartt requested a review from Meinersbur March 18, 2025 00:50
Copy link
Member

@Meinersbur Meinersbur left a comment

Choose a reason for hiding this comment

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

LGTM

@mustartt mustartt merged commit ac9049d into llvm:main Mar 21, 2025
11 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Mar 21, 2025

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

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

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-shell :: ObjectFile/ELF/section-addresses.yaml (1394 of 2928)
PASS: lldb-shell :: ObjectFile/ELF/riscv-arch.yaml (1395 of 2928)
PASS: lldb-shell :: ObjectFile/ELF/section-permissions.yaml (1396 of 2928)
PASS: lldb-shell :: ObjectFile/ELF/section-types-edgecases.yaml (1397 of 2928)
PASS: lldb-shell :: ObjectFile/ELF/minidebuginfo-set-and-hit-breakpoint.test (1398 of 2928)
PASS: lldb-shell :: ObjectFile/ELF/short-build-id.yaml (1399 of 2928)
PASS: lldb-shell :: ObjectFile/ELF/section-types.yaml (1400 of 2928)
PASS: lldb-shell :: ObjectFile/MachO/lc_build_version.yaml (1401 of 2928)
PASS: lldb-shell :: ObjectFile/MachO/lc_build_version_notools.yaml (1402 of 2928)
UNRESOLVED: lldb-api :: tools/lldb-server/TestLldbGdbServer.py (1403 of 2928)
******************** TEST 'lldb-api :: tools/lldb-server/TestLldbGdbServer.py' FAILED ********************
Script:
--
/usr/bin/python3.10 /home/tcwg-buildbot/worker/lldb-arm-ubuntu/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/./lib --env LLVM_INCLUDE_DIR=/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/include --env LLVM_TOOLS_DIR=/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/./bin --arch armv8l --build-dir /home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/lldb-test-build.noindex --lldb-module-cache-dir /home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/./bin/lldb --compiler /home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/./bin/clang --dsymutil /home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/./bin --lldb-obj-root /home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/tools/lldb --lldb-libs-dir /home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/./lib /home/tcwg-buildbot/worker/lldb-arm-ubuntu/llvm-project/lldb/test/API/tools/lldb-server -p TestLldbGdbServer.py
--
Exit Code: 1

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

--
Command Output (stderr):
--
UNSUPPORTED: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_Hc_then_Csignal_signals_correct_thread_launch_debugserver (TestLldbGdbServer.LldbGdbServerTestCase) (test case does not fall in any category of interest for this run) 
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_Hc_then_Csignal_signals_correct_thread_launch_llgs (TestLldbGdbServer.LldbGdbServerTestCase)
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_Hg_fails_on_another_pid_llgs (TestLldbGdbServer.LldbGdbServerTestCase)
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_Hg_fails_on_minus_one_pid_llgs (TestLldbGdbServer.LldbGdbServerTestCase)
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_Hg_fails_on_zero_pid_llgs (TestLldbGdbServer.LldbGdbServerTestCase)
UNSUPPORTED: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_Hg_switches_to_3_threads_launch_debugserver (TestLldbGdbServer.LldbGdbServerTestCase) (test case does not fall in any category of interest for this run) 
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_Hg_switches_to_3_threads_launch_llgs (TestLldbGdbServer.LldbGdbServerTestCase)
UNSUPPORTED: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_P_and_p_thread_suffix_work_debugserver (TestLldbGdbServer.LldbGdbServerTestCase) (test case does not fall in any category of interest for this run) 
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_P_and_p_thread_suffix_work_llgs (TestLldbGdbServer.LldbGdbServerTestCase)
UNSUPPORTED: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_P_writes_all_gpr_registers_debugserver (TestLldbGdbServer.LldbGdbServerTestCase) (test case does not fall in any category of interest for this run) 
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_P_writes_all_gpr_registers_llgs (TestLldbGdbServer.LldbGdbServerTestCase)
UNSUPPORTED: LLDB (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/clang-arm) :: test_attach_commandline_continue_app_exits_debugserver (TestLldbGdbServer.LldbGdbServerTestCase) (test case does not fall in any category of interest for this run) 
Program aborted due to an unhandled Error:
Operation not permitted
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/lldb-server gdbserver --attach=1339397 --reverse-connect [::1]:47909
#0 0x00d54ad8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/lldb-server+0x7a0ad8)
#1 0x00d524d8 llvm::sys::RunSignalHandlers() (/home/tcwg-buildbot/worker/lldb-arm-ubuntu/build/bin/lldb-server+0x79e4d8)
#2 0x00d5535c SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0xf751d6f0 __default_rt_sa_restorer ./signal/../sysdeps/unix/sysv/linux/arm/sigrestorer.S:80:0
#4 0xf750db06 ./csu/../sysdeps/unix/sysv/linux/arm/libc-do-syscall.S:47:0

@mustartt mustartt deleted the strlen-loop-idiom branch March 21, 2025 14:39
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

(Just some drive-by notes.)

const ConstantInt *CmpZero = dyn_cast<ConstantInt>(Val);
if (!CmpZero || !CmpZero->isZero())
return false;
return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like match(Val, m_Zero()). Though I'm not super clear on why this patch has to add the pointer handling at all.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for taking. You are right, it's no longer needed. We used to use this to condition the loop preheader on a nullptr comparison for the base of the string. I will clean this up

Comment on lines +1660 to +1662
dbgs() << "loop exit phi scev: ";
Ev->print(dbgs());
dbgs() << "\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

Can use << *Ev, no need for print().

@@ -0,0 +1,66 @@
; RUN: opt -passes='loop(loop-idiom),verify' < %s -S | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

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

verify is redundant, it is scheduled automatically.

@mustartt
Copy link
Member Author

Another small bug got passed testing without expensive checks and corresponding cleanups: #132421

mstorsjo added a commit that referenced this pull request Mar 22, 2025
…wcslen (#108985)" (#131412)"

This reverts commit ac9049d.

This change broke Wine (causing it to hang on startup), see
#108985 for discussion.
mustartt added a commit to mustartt/llvm-project that referenced this pull request Mar 22, 2025
…lvm#108985)" (llvm#131412)

Relands llvm#108985

This PR continues the effort made in
https://discourse.llvm.org/t/rfc-strlen-loop-idiom-recognition-folding/55848
and https://reviews.llvm.org/D83392 and https://reviews.llvm.org/D88460
to extend `LoopIdiomRecognize` to find and replace loops of the form
```c
base = str;
while (*str)
  ++str;
```
and transforming the `strlen` loop idiom into the appropriate `strlen`
and `wcslen` library call which will give a small performance boost if
replaced.
```c
str = base + strlen(base)
len = str - base
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants