Skip to content

feat(compiler): Implement true concurrent parallel execution for compiled code#109

Merged
KrisSimon merged 2 commits intomainfrom
feature/concurrent-parallel-foreach
Jan 2, 2026
Merged

feat(compiler): Implement true concurrent parallel execution for compiled code#109
KrisSimon merged 2 commits intomainfrom
feature/concurrent-parallel-foreach

Conversation

@KrisSimon
Copy link
Copy Markdown
Member

Overview

Implements true concurrent parallel execution for parallel for each loops in compiled ARO binaries using function pointers and DispatchQueue, matching the interpreter's non-deterministic concurrent behavior.

Changes

Runtime Bridge (RuntimeBridge.swift)

  • ✅ Add RuntimeError enum for compilation error handling
  • ✅ Add aro_parallel_for_each_execute C-callable function
    • Uses DispatchQueue + DispatchGroup for reliable parallelism
    • Implements concurrency limiting with DispatchSemaphore
    • Thread-safe error tracking with NSLock
    • Defaults to System.coreCount threads, configurable via with <concurrency: N>

LLVM Code Generator (LLVMCodeGenerator.swift)

  • ✅ Add global runtime pointer (@global_runtime) for parallel executor access
  • ✅ Extract parallel loop bodies as separate LLVM functions
    • Function signature: ptr function(ptr context, ptr item, i64 index)
    • Proper variable binding and filter support
    • Isolated %__result variable per loop body
  • ✅ Replace serial parallel loop generation with function pointer approach
  • ✅ Add generateLoopBodyFunction() to emit loop body functions
  • ✅ Add generatePendingLoopBodies() to emit after feature sets
  • ✅ Update external declarations with parallel executor

Example

  • ✅ Add Examples/ParallelForEach/ demonstrating serial vs parallel iteration
  • ✅ Shows non-deterministic execution order in parallel loops

Technical Highlights

  • Function Pointers: Loop bodies compiled as separate LLVM functions, passed to runtime executor
  • Concurrency Control: Default to System.coreCount, override via with <concurrency: N> syntax
  • Context Isolation: Child contexts per iteration for immutability guarantees
  • DispatchQueue: Replaced Swift concurrency (Task/TaskGroup) for compiled binary compatibility
  • No Breaking Changes: Serial loops unchanged, parallel loops now truly parallel

Testing

Verified with Examples/ParallelForEach:

Interpreter (aro run):

=== Parallel Iteration ===
1, 3, 4, 5, 2, 8, 6, 10, 7, 9  ← Non-deterministic ✓

Compiled Binary (aro build):

=== Parallel Iteration ===
1, 2, 3, 4, 6, 7, 8, 9, 5, 10  ← Non-deterministic ✓

Both show true concurrent execution with varying iteration order across runs.

Performance

  • Serial loops: Unchanged performance, deterministic ordering preserved
  • Parallel loops: True concurrent execution leveraging multiple CPU cores
  • Concurrency limiting: Prevents thread explosion on large collections
  • Context overhead: Minimal - child contexts created per iteration as in interpreter

Example Usage

(Application-Start: Parallel Demo) {
    <Create> the <numbers> with [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

    (* Serial execution - deterministic order *)
    for each <n> in <numbers> {
        <Log> the <msg> for the <console> with <n>.
    }

    (* Parallel execution - non-deterministic order *)
    parallel for each <n> in <numbers> {
        <Log> the <msg> for the <console> with <n>.
    }

    (* With concurrency limit *)
    parallel for each <n> in <numbers> with <concurrency: 4> {
        <Log> the <msg> for the <console> with <n>.
    }

    <Return> an <OK: status> for the <demo>.
}

Build & Test

# Build the project
swift build

# Run with interpreter (already had parallelism)
.build/debug/aro run ./Examples/ParallelForEach

# Compile to native binary (now has parallelism!)
.build/debug/aro build ./Examples/ParallelForEach

# Run compiled binary
./Examples/ParallelForEach/ParallelForEach

Files Changed

  • Sources/ARORuntime/Bridge/RuntimeBridge.swift (+147 lines)
  • Sources/AROCompiler/LLVMCodeGenerator.swift (+322 lines, -66 lines)
  • Examples/ParallelForEach/main.aro (new)

Related

Implements the full vision for parallel execution in compiled ARO code, bridging the gap between interpreter and compiler capabilities.

…iled code

Adds function pointer-based parallel execution for `parallel for each` loops in
compiled ARO binaries, matching the interpreter's non-deterministic concurrent
behavior.

## Changes

### Runtime Bridge (RuntimeBridge.swift)
- Add `RuntimeError` enum for compilation error handling
- Add `aro_parallel_for_each_execute` C-callable function
  - Uses DispatchQueue + DispatchGroup for reliable parallelism
  - Implements concurrency limiting with DispatchSemaphore
  - Thread-safe error tracking with NSLock
  - Defaults to System.coreCount threads, configurable via AST

### LLVM Code Generator (LLVMCodeGenerator.swift)
- Add global runtime pointer (`@global_runtime`) for parallel executor access
- Extract parallel loop bodies as separate LLVM functions
  - Signature: `ptr function(ptr context, ptr item, i64 index)`
  - Proper variable binding and filter support
  - Isolated `%__result` variable per loop body
- Replace serial parallel loop generation with function pointer approach
- Add `generateLoopBodyFunction()` to emit loop body functions
- Add `generatePendingLoopBodies()` to emit after feature sets
- Update external declarations with parallel executor

### Example
- Add `Examples/ParallelForEach/` demonstrating serial vs parallel iteration
- Shows non-deterministic execution order in parallel loops

## Technical Highlights

- **Function Pointers**: Loop bodies compiled as separate LLVM functions
- **Concurrency Control**: Default System.coreCount, override via `with <concurrency: N>`
- **Context Isolation**: Child contexts per iteration for immutability
- **DispatchQueue**: Replaced Swift concurrency for compiled binary compatibility
- **Zero Breaking Changes**: Serial loops unchanged, parallel loops truly parallel

## Testing

Verified with ParallelForEach example:
- Interpreter: Non-deterministic parallel execution ✓
- Compiled binary: Non-deterministic parallel execution ✓
- Serial loops: Deterministic ordering preserved ✓
- Concurrency limiting: Respects System.coreCount ✓

Closes: Implements true concurrent parallel execution for compiled ARO code
Replace System.coreCount with ProcessInfo.processInfo.activeProcessorCount
in RuntimeBridge.swift to fix Windows build failure. System.coreCount is
from the swift-system package which is Unix-only and not available on Windows.

ProcessInfo.processInfo.activeProcessorCount:
- Works on all platforms (macOS, Linux, Windows)
- Part of Foundation (already imported)
- Returns the same information (number of available processor cores)

Fixes Windows CI build error:
error: cannot find 'System' in scope
@KrisSimon KrisSimon merged commit 37e00f9 into main Jan 2, 2026
9 checks passed
@KrisSimon KrisSimon deleted the feature/concurrent-parallel-foreach branch January 2, 2026 08:12
KrisSimon added a commit that referenced this pull request Feb 28, 2026
Introduce raw string literals that prevent escape sequence processing,
making regex patterns, file paths, and backslash-heavy content more readable.

Implementation:
- Add r-prefix syntax for raw strings: r"\d+\.\d+"
- Support both double and single quotes: r"..." and r'...'
- No escape processing except \" and \' for quotes
- scanRawString() method in Lexer

Examples:
  r"\d+\.\d+\.\d+"                    # Regex without double escaping
  r"C:\Users\Admin\config.json"       # Windows path
  r"\\server\share\file"              # UNC path
  r"\documentclass{article}"          # LaTeX

Tests:
- 5 new unit tests in LexerTests.swift
- RawStrings example demonstrating various use cases

Fixes GitLab #109
KrisSimon added a commit that referenced this pull request Feb 28, 2026
Add raw string literals with r-prefix (ARO-0060)

Closes #109

See merge request arolang/aro!127
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.

1 participant