Skip to content

refactor(opt,sentinel): split opt.go, add OptimizeLines([]string), and define sentinel error types with errors.Is/As#7

Merged
0xA672 merged 5 commits into
mainfrom
trae/solo-agent-xXFA8l
Jun 22, 2026
Merged

refactor(opt,sentinel): split opt.go, add OptimizeLines([]string), and define sentinel error types with errors.Is/As#7
0xA672 merged 5 commits into
mainfrom
trae/solo-agent-xXFA8l

Conversation

@0xA672

@0xA672 0xA672 commented Jun 22, 2026

Copy link
Copy Markdown
Owner

Summary

This PR refactors the optimizer package to improve maintainability, performance, and error handling.

Changes

1. OptimizeLines([]string) API — eliminate string round-trips in core.go

  • Added OptimizeLines(lines []string, level int) []string and PeepholeOnlyLines(lines []string) []string in vas/opt/opt.go
  • core.go switched from strings.Split → strings.Join to direct []string-based pipeline, reducing string allocation overhead during optimization

2. Split 2100-line opt.go into 6 focused files

File Responsibility
opt.go Public API (Optimize, OptimizeLines, PeepholeOnly, PeepholeOnlyLines)
fold.go Constant folding (FoldConstants, foldLine, tokenizeFold)
block.go Basic-block analysis + dead code elimination (splitBlocks, deadCodeElim, dstReg, readRegs, regIndex, extractLabel, stripComment)
dataflow.go Data-flow analyses (copy propagation, constant propagation, dead store elimination, strength reduction, store-load forwarding)
peephole.go Peephole passes (xorZero, testCmp, nopMerge, leaFuse, noopElim, pushPopMov, cancelPairElim, etc.)
aggressive.go -O2 passes (cse, licm, redundant load elim, pushPopElim, scanPushModPop, tailCallOpt)

3. Sentinel error types (vas/errors.go) — errors.Is/As compatible

var (
    ErrInvalidRegister = errors.New("invalid virtual register (valid: v0-v12)")
    ErrOperandCount    = errors.New("operand count mismatch")
    ErrLintError       = errors.New("lint errors found")
    ErrPreprocessing   = errors.New("preprocessing error")
    ErrUndefinedLabel  = errors.New("undefined label")
)
  • 12 operand-count errors in core.gofmt.Errorf("%w: ...", ErrOperandCount, ...)
  • 2 register out-of-range errors in reg.gofmt.Errorf("%w: %s", ErrInvalidRegister, name)
  • 2 undefined-label errors in prep.gofmt.Errorf("%w: %s", ErrUndefinedLabel, ...)
  • 1 recursion-limit error in prep.gofmt.Errorf("%w: recursion limit exceeded", ErrPreprocessing)
  • 1 lint error in core.gofmt.Errorf("%w (...)", ErrLintError)

Callers can now use errors.Is(err, vas.ErrOperandCount) etc. for precise error classification.

Verification

  • go build ./...
  • go test ./... ✅ (all packages pass)

0xA672 and others added 3 commits June 21, 2026 00:52
Co-authored-by: traeagent <traeagent@users.noreply.github.com>
Co-authored-by: traeagent <traeagent@users.noreply.github.com>
…Lines([]string), and define sentinel error types with errors.Is/As

- opt/: split 2100-line opt.go into opt.go (public API), fold.go (constant folding), block.go (basic blocks), dataflow.go (copy propagation), peephole.go (peephole passes), aggressive.go (-O2 passes)
- opt/: add OptimizeLines([]string) and PeepholeOnlyLines([]string) to eliminate string.Split/Join overhead in core.go
- vas/errors.go: new file defining ErrInvalidRegister, ErrOperandCount, ErrLintError, ErrPreprocessing, ErrUndefinedLabel sentinels
- core.go, prep.go, reg.go: replace fmt.Errorf strings with sentinel-wrapped errors using %w for errors.Is/As compatibility
- Build and all tests pass
@0xA672 0xA672 merged commit 26a47af into main Jun 22, 2026
4 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in Vas Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant