Skip to content

Conversation

@guosran
Copy link
Collaborator

@guosran guosran commented Dec 29, 2025

Description

This PR addresses issue #221. It improves the spatial-temporal mapping quality by implementing degree-based operation scheduling and an adaptive link congestion penalty. These changes allow the mapper to find more efficient placements for critical nodes and reduce the overall compiled_ii for several complex kernels.

Key Algorithmic Improvements:

  1. Degree-Based Priority Scheduling:
    Operations within each ALAP (As-Late-As-Possible) level are now sorted by their connectivity (total degree: fan-in + fan-out). High-degree operations are prioritized for placement, ensuring that nodes with the most routing constraints secure optimal physical resources early in the mapping process.

  2. Adaptive Link Congestion Penalty:
    A quadratic penalty term based on link occupancy has been integrated into the calculateAward cost function. This guides the mapper to automatically avoid congested areas of the CGRA fabric, preventing routing bottlenecks that previously led to higher II.

  3. Deterministic Tie-Breaking:
    Stable tie-breaking logic using time-steps and degrees has been introduced. This ensures that the mapping results are consistent across different machines and parallel test executions, eliminating "flaky" test failures.

Results:

Test Case Original II Optimized II
simple_loop_reduction.mlir 4 3
test_code_generate.mlir 5 4
perfect_nested.mlir 10 8
nested_loop/test.mlir 13 11
fusion/test.mlir 13 12

Copilot AI review requested due to automatic review settings December 29, 2025 14:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the spatial-temporal mapping heuristic for CGRA (Coarse-Grained Reconfigurable Architecture) compilation, achieving significant Initiation Interval (II) reductions across multiple benchmarks. The optimization introduces degree-based operation scheduling, adaptive link congestion penalties, and deterministic tie-breaking to improve mapping quality and test stability.

Key changes:

  • Implements degree-based priority scheduling to map high-connectivity operations first
  • Adds adaptive quadratic penalty for link congestion to avoid routing bottlenecks
  • Introduces stable tie-breaking logic for consistent mapping results across executions

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.

Show a summary per file
File Description
lib/NeuraDialect/Mapping/mapping_util.cpp Core algorithm changes: degree-based sorting in flatten_level_buckets() and adaptive congestion penalty in calculateAward()
MAPPING_OPTIMIZATION_SUMMARY.md New documentation summarizing performance improvements and algorithm changes
test/neura/fusion/test.mlir Updated II expectation from 13→12 (note: description claims 11)
test/controflow_fuse/simple_loop_reduction/simple_loop_reduction.mlir Updated II expectation from 4→3
test/controflow_fuse/perfect_nested/perfect_nested.mlir Updated II expectation from 10→8 (note: description claims different baseline)
test/code_gen/test_code_generate.mlir Updated II expectation from 5→4 with detailed mapping output changes
test/c2llvm2mlir/nested_loop/test.mlir Updated II expectation from 13→11
Multiple test files Updated mapping expectations reflecting new operation placement strategies

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@guosran guosran force-pushed the optimize-mapping-heuristic branch 2 times, most recently from 9ae899b to b1b1297 Compare December 31, 2025 13:09
@guosran guosran marked this pull request as draft January 4, 2026 07:25
@guosran guosran force-pushed the optimize-mapping-heuristic branch from 8fa2071 to 8ea76f4 Compare January 4, 2026 15:05
@guosran
Copy link
Collaborator Author

guosran commented Jan 4, 2026

The previous commits used non-deterministic representations for some IIs, for the mapping was not stable, and some of the IIs stated in the description are due to coincidence. I'm sorry for the confusion.

The calculateAward() function used std::sort() to order operations by scheduling awards. When multiple operations had equal awards, std::sort() provided no ordering guarantee, causing non-deterministic mapping results across different runs.

Solution: Changed std::sort() to std::stable_sort().

However, the mapping became even messier :(

I will try to figure out the problem tmr :(

This commit introduces significant performance improvements to the spatial-temporal
mapping algorithm by implementing degree-based operation scheduling and link
congestion awareness.

Key improvements:
- Implemented degree-based priority scheduling in flatten_level_buckets()
  - Operations are now sorted by connectivity (fan-in + fan-out) within each ALAP level
  - High-degree operations get mapped first, securing optimal placements
  - Added stable tie-breaking to ensure deterministic results

- Added balanced link congestion penalty in calculateAward()
  - Quadratic penalty based on incoming/outgoing link occupancy
  - Guides mapper away from congested areas without over-constraining

- Improved sorting stability with time-step tie-breaking
  - Minimizes non-deterministic mapping variations in tests

Performance results:
- fusion/test.mlir: II reduced from 13 to 11 (-15.4%)
- nested_loop/test.mlir: II reduced from 13 to 11 (-15.4%)
- code_gen/test_code_generate.mlir: II reduced from 5 to 4 (-20%)
- All other tests maintain or improve their II

Test updates:
- Updated test expectations for improved II values
- nested_loop/test.mlir: updated CHECK-LLVM2NEURA-MAP to expect II=11
- fusion/test.mlir: updated CHECK-MAPPING to expect II=11

Files modified:
- lib/NeuraDialect/Mapping/mapping_util.cpp
- test/c2llvm2mlir/nested_loop/test.mlir
- test/neura/fusion/test.mlir
- MAPPING_OPTIMIZATION_SUMMARY.md (new documentation)
Further improved the link congestion penalty by:
1. Increased penalty coefficient from 10 to 50
2. Added fan-in-based scaling: penalty *= (1 + num_producers)
   - Operations with more data dependencies are more sensitive to congestion
   - This prevents high-fanin ops from being placed in bottleneck areas

Performance improvements:
- fusion/test.mlir: II reduced from 13 to 11 (-15.4%)
- nested_loop/test.mlir: II reduced from 13 to 11 (-15.4%)
- code_gen/test_code_generate.mlir: II reduced from 5 to 4 (-20%)

Test status:
- 71/83 tests passing (85.54%)
- Remaining failures are due to detailed mapping layout changes (PE positions,
  register allocations) which are expected when scheduling order changes
- Core II improvements are verified and consistent
… penalty

Implements core mapping optimizations to reduce Initiation Interval (II):
1. Degree-based priority scheduling: Maps high-connectivity nodes first.
2. Adaptive Congestion Penalty:
   - High fan-in ops (>=3 producers): Strong penalty (coeff 60) to avoid congestion.
   - Low fan-in ops: Weak penalty (coeff 15) to allow dense packing.

Performance improvements:
- fusion/test.mlir (fuse-pattern): II 13 -> 12 (-7.7%)
- fusion/test.mlir (iter-merge): II 12 -> 12 (No regression)
- nested_loop/test.mlir: II 13 -> 11 (-15.4%)
- code_gen/test_code_generate.mlir: II 5 -> 4 (-20%)

Tests updated:
- Updated expectations for fusion, nested_loop, code_gen, and branch_for.
- Remaining test failures are due to benign mapping layout changes.
Addresses PR #222 / Issue #221 feedback from tancheng.

Problem: Within same ALAP level, high-degree non-critical ops
were mapped before low-degree critical ops, causing routing
congestion and suboptimal II.

Solution: Modified flatten_level_buckets() to use criticality
as PRIMARY sorting criterion within each ALAP level:
  Priority 1: Criticality (critical ops first)
  Priority 2: Degree (higher degree first within category)
  Priority 3: Original index (stability)

Conservative implementation: Only reorders within levels,
preserves ALAP level boundaries and overall scheduling.

Results:
- bicg: II 11 → 10 (-1 cycle)
- test_code_generate: II 5 → 4 (-1 cycle)
- Other tests: stable (no regressions)

Modified files:
- include/NeuraDialect/Mapping/mapping_util.h
- lib/NeuraDialect/Mapping/mapping_util.cpp
- lib/NeuraDialect/Transforms/MapToAcceleratorPass.cpp
- test/e2e/bicg/bicg_kernel.mlir (updated FileCheck)
- Update 5 failing tests to match new mapping results from critical path optimization
- test_code_generate.mlir: II improved from 5→4, full MAPPING (72 lines) + YAML + ASM
- bicg_kernel.mlir: II improved from 13→10, full MAPPING (217 lines) + YAML/ASM (40 lines)
- histogram_kernel.mlir: Full MAPPING (46 lines) with regex for module attributes + YAML/ASM (40 lines)
- relu_kernel.mlir: Full MAPPING (59 lines) + complete YAML (527 lines) + ASM (104 lines)
- branch_for.mlir: Simple MAPPING check + YAML/ASM (40 lines)
- All tests now pass: 80/83 (96.39%), 3 expectedly failed
- No II performance regressions confirmed
@guosran guosran force-pushed the optimize-mapping-heuristic branch from 37e7fe6 to d12394d Compare January 5, 2026 07:43
@guosran guosran marked this pull request as ready for review January 5, 2026 07:43
@guosran guosran requested a review from Copilot January 5, 2026 07:44
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@guosran
Copy link
Collaborator Author

guosran commented Jan 5, 2026

progress up to now in terms of compiled_ii:

Test File main Current Change
simple_loop_reduction.mlir 4 3 -1
test_code_generate.mlir 5 4 -1
perfect_nested.mlir 10 8 -2
nested_loop/test.mlir 13 11 -2
fusion/test.mlir 13 12 -1
e2e/bicg/bicg_kernel.mlir 13 10 -3
mapping_quality/tiny_loop.mlir 4 5 +1

- Implement Critical Path First heuristic in MapToAcceleratorPass
- Sort by Critical > Materialized > Degree > Topological dfg_id
- Amplify proximity and time bonuses for critical ops
- Update all failing tests with new deterministic mapping output
- Improve test stability by fixing non-deterministic tie-breakers
@guosran guosran force-pushed the optimize-mapping-heuristic branch from ca37249 to 72257d9 Compare January 5, 2026 10:20
@coredac coredac deleted a comment from Copilot AI Jan 5, 2026
@coredac coredac deleted a comment from Copilot AI Jan 5, 2026
@guosran guosran force-pushed the optimize-mapping-heuristic branch from 72257d9 to 82f0cdb Compare January 6, 2026 07:19
@guosran guosran merged commit 6657186 into main Jan 6, 2026
1 check passed
@guosran guosran deleted the optimize-mapping-heuristic branch January 6, 2026 07:43
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