Skip to content

defer: enable loop lowering#1

Closed
cpunion wants to merge 94 commits intofeature/tls-gcfrom
feature/defer-loop
Closed

defer: enable loop lowering#1
cpunion wants to merge 94 commits intofeature/tls-gcfrom
feature/defer-loop

Conversation

@cpunion
Copy link
Owner

@cpunion cpunion commented Oct 15, 2025

Summary

  • allow SSA to lower defer inside loops and drain built chains via the runtime TLS handle added in tls: add gc-aware pthread slots goplus/llgo#1347
  • add runtime shims so loop defer nodes are freed through GC-aware helpers across GC and nogc builds
  • add regression and stress coverage for loop defers plus parity fixtures

Dependencies

Testing

  • PATH=/Users/lijie/go/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Library/TeX/texbin:/Users/lijie/.local/bin:/var/folders/wb/8zzrvxd56294wxlxxbpws9440000gn/T/.tmplpgJ6h:/Users/lijie/Library/pnpm:/Users/lijie/bin:/opt/homebrew/opt/bison/bin:/Users/lijie/.bun/bin:/Users/lijie/.wasmer/bin:/opt/homebrew/opt/ruby/bin:/Users/lijie/.codeium/windsurf/bin:/Users/lijie/.nvm/versions/node/v23.11.0/bin:/Users/lijie/.cargo/bin:/Users/lijie/go/bin:/Applications/iTerm.app/Contents/Resources/utilities:/Users/lijie/.orbstack/bin:/Users/lijie/.rvm/bin:/Users/lijie/.orbstack/bin ./llgo.sh test ./test -run TestDeferLoopStress
  • PATH=/Users/lijie/go/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Library/TeX/texbin:/Users/lijie/.local/bin:/var/folders/wb/8zzrvxd56294wxlxxbpws9440000gn/T/.tmplpgJ6h:/Users/lijie/Library/pnpm:/Users/lijie/bin:/opt/homebrew/opt/bison/bin:/Users/lijie/.bun/bin:/Users/lijie/.wasmer/bin:/opt/homebrew/opt/ruby/bin:/Users/lijie/.codeium/windsurf/bin:/Users/lijie/.nvm/versions/node/v23.11.0/bin:/Users/lijie/.cargo/bin:/Users/lijie/go/bin:/Applications/iTerm.app/Contents/Resources/utilities:/Users/lijie/.orbstack/bin:/Users/lijie/.rvm/bin:/Users/lijie/.orbstack/bin ./llgo.sh test ./test

MeteorsLiu and others added 29 commits September 28, 2025 09:20
Bumps [github.com/goplus/gogen](https://github.com/goplus/gogen) from 1.19.2 to 1.19.3.
- [Release notes](https://github.com/goplus/gogen/releases)
- [Commits](goplus/gogen@v1.19.2...v1.19.3)

---
updated-dependencies:
- dependency-name: github.com/goplus/gogen
  dependency-version: 1.19.3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
…ub.com/goplus/gogen-1.19.3

chore(deps): bump github.com/goplus/gogen from 1.19.2 to 1.19.3
…ub.com/goplus/gogen-1.19.3

chore(deps): bump github.com/goplus/gogen from 1.19.2 to 1.19.3
- Update _cmptest/ description to clarify its purpose
- Remove scattered 'Verified output' sections
- Consolidate all validation information into unified Validation section

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xgopilot <noreply@goplus.org>
- Link Development Environment section to README to avoid redundancy
- Remove 'Verified Environment' subsection
- Remove 'Build pydump' section as not needed for current doc

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xgopilot <noreply@goplus.org>
- Combines Testing and Validation sections for better organization
- Emphasizes these steps are essential when fixing bugs or implementing features
- Maintains all validation information with clearer structure

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xgopilot <noreply@goplus.org>
- Add clear 'Validation Workflow' section with numbered steps
- Create prominent 'LLGO_ROOT Environment Variable' section
- Add requirement: All bug fixes/features MUST include tests
- Reorganize Important Notes for better clarity

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xgopilot <noreply@goplus.org>
- Move demo examples to Project Structure with simplified descriptions
- Change 'Test a simple example' to focus on writing and running test cases
- Remove standalone Running Examples section to reduce redundancy

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xgopilot <noreply@goplus.org>
- Remove 'Validated Commands and Expected Outputs' section (lines 71-95)
- This information is already covered in 'Validation Workflow' section
- Reduces redundancy and improves document clarity

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xgopilot <noreply@goplus.org>
- Add requirement to run formatting before submitting code updates
- Emphasize that go fmt must be run before committing changes
- Clarify that formatting ensures consistent code formatting

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xgopilot <noreply@goplus.org>
- Move Build Commands into Common Development Tasks section
- Remove duplicate Validation Workflow section
- Simplify document structure by consolidating build commands

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xgopilot <noreply@goplus.org>
- Add 'Build llgo command specifically' and 'Check llgo version' commands
- Consolidate all build-related commands in one section
- Reduce redundancy by having single section for development tasks

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xgopilot <noreply@goplus.org>
- Register hash/maphash in hasAltPkg map in runtime/build.go
- Add rand() function that bridges to fastrand64() in runtime overlay
- Add memhash() function that bridges to internal memhash() in runtime overlay
- Fixes issue where hash/maphash package failed with undefined symbols

Fixes goplus#1338

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
Co-authored-by: xgopilot <noreply@goplus.org>
Add test case in _demo/go/maphash to verify hash/maphash functionality
with the new runtime.rand and runtime.memhash support.

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
Co-authored-by: xgopilot <noreply@goplus.org>
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
Co-authored-by: xgopilot <noreply@goplus.org>
Move runtime_rand and runtime_memhash linkname declarations from
runtime/internal/lib/runtime/runtime.go to runtime/internal/lib/hash/maphash/maphash.go
to avoid polluting the runtime package namespace.

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
The llgo:skipall directive was preventing fallback to the standard library,
causing undefined symbol errors for functions like WriteString, MakeSeed, etc.
The overlay now only defines the two linkname functions, allowing typepatch
to handle the rest.

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
Implement randUint64() directly in the maphash overlay to ensure compatibility
across Go versions. Go 1.21's randUint64() calls runtime.fastrand64, while
Go 1.22+ calls runtime.rand. The wrapper function bridges to llgo's fastrand64
implementation, avoiding undefined symbol errors on Go 1.21.

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
- Add tests for all major hash/maphash public APIs:
  * Hash basics (WriteString, Sum64, Reset)
  * MakeSeed and SetSeed functionality
  * Write methods (Write, WriteByte, WriteString)
  * Bytes and String convenience functions
- Use panic() for unexpected errors instead of silent failures
- Add proper error checking and validation
- Document Comparable/WriteComparable limitations in overlay

Note: Comparable() and WriteComparable() are not yet supported and will
panic with 'intrinsic' error as they require runtime intrinsic support.

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
- Fix line 67: Change unused variable 'n' to blank identifier '_'
- Fix line 102: Correct WriteByte call to expect only error return value
  (WriteByte returns only error, not (int, error))

These fixes resolve the compilation errors reported by the CI.

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
Add !xgopilot/** exclusion pattern to all workflow trigger configurations,
matching the existing dependabot pattern. This ensures that xgopilot branches
only trigger CI checks on pull_request events, eliminating redundant push
event triggers.

Fixes goplus#1340

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
ci: prevent duplicate workflow runs for xgopilot branches
…-1760330027

Add CLAUDE.md: AI Assistant Guide
…-1760425043

fix: add runtime.rand and runtime.memhash for hash/maphash support
Bumps [actions/setup-node](https://github.com/actions/setup-node) from 5 to 6.
- [Release notes](https://github.com/actions/setup-node/releases)
- [Commits](actions/setup-node@v5...v6)

---
updated-dependencies:
- dependency-name: actions/setup-node
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Update README to reflect that hash/maphash package has been mostly
implemented and verified by adding it to the Go packages support
section with "(partially)" status.

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
@gemini-code-assist
Copy link

Summary of Changes

Hello @cpunion, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances LLGo's defer mechanism by enabling its use within loops. The core change involves a robust integration with the garbage collector and thread-local storage, ensuring that deferred calls within loops are correctly managed and cleaned up, thereby preventing memory-related issues. This brings LLGo's defer behavior closer to that of standard Go, improving language compatibility and reliability for complex control flows.

Highlights

  • Defer in Loops: LLGo now supports defer statements within loops, aligning its behavior with standard Go semantics, where defers execute in LIFO order for each iteration.
  • GC-Aware Defer Chains: The implementation integrates defer chains with a GC-aware Thread-Local Storage (TLS) handle, preventing memory leaks in scenarios with numerous loop defers by ensuring that TLS-resident pointers are visible to the Boehm GC.
  • Runtime Shims for Memory Management: New runtime shims (FreeDeferNode) have been added to correctly free defer nodes, supporting both GC-enabled and nogc builds.
  • Comprehensive Testing: New regression and stress tests have been introduced, including TestDeferLoopStress which validates the correct execution of 1,000,000 loop defers without failure, confirming proper GC root tracking.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

xgopilot and others added 24 commits October 31, 2025 03:43
Removed the sentence about test failures as requested.
The "Why this matters" section now focuses on the benefits
of the process rather than the negative consequences.

🤖 Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com>
Removes the deprecated //export LLGoVersion comment from internal/env/version.go
as it is no longer needed.

Fixes goplus#1385

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com>
…ds cross-package

Co-authored-by: xgopilot <noreply@goplus.org>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: xgopilot <noreply@goplus.org>
Co-authored-by: Claude <noreply@anthropic.com>
ci:avoid auto upgrade before brew install
fix(runtime): segmentation fault when calling interface private methods cross-package
…-1762256384

chore: remove deprecated //export LLGoVersion comment
…utll-process-1730340128

docs(CLAUDE.md): document out.ll update process after IR changes
…ference to recoverable panic

- Add z_rt_default.go with signal handler for SIGSEGV on non-wasm platforms
- Convert segmentation faults from nil pointer access to Go panic
- Enable recover() to catch nil pointer dereference errors
- Use build tag (!wasm) to maintain wasm platform compatibility
- Remove commented-out signal handling code from z_rt.go

This aligns llgo's behavior with standard Go, where accessing nil pointer
fields triggers a recoverable panic instead of immediate program crash.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
fix(runtime): restore SIGSEGV signal handler for non-wasm platforms
Bumps [github.com/goplus/lib](https://github.com/goplus/lib) from 0.3.0 to 0.3.1.
- [Release notes](https://github.com/goplus/lib/releases)
- [Commits](goplus/lib@v0.3.0...v0.3.1)

---
updated-dependencies:
- dependency-name: github.com/goplus/lib
  dependency-version: 0.3.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
…ub.com/goplus/lib-0.3.1

chore(deps): bump github.com/goplus/lib from 0.3.0 to 0.3.1
Bumps [github.com/goplus/gogen](https://github.com/goplus/gogen) from 1.19.3 to 1.19.5.
- [Release notes](https://github.com/goplus/gogen/releases)
- [Commits](goplus/gogen@v1.19.3...v1.19.5)

---
updated-dependencies:
- dependency-name: github.com/goplus/gogen
  dependency-version: 1.19.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
…ub.com/goplus/gogen-1.19.5

chore(deps): bump github.com/goplus/gogen from 1.19.3 to 1.19.5
@cpunion cpunion closed this Nov 13, 2025
@cpunion cpunion deleted the feature/defer-loop branch November 13, 2025 11:54
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.

5 participants