Commit e47671d
Fix ReplicateGlobalsPerAffinity to maintain correct order of globals and initializers (iree-org#22401)
## Summary
Fix ordering issue in ReplicateGlobalsPerAffinity pass where new globals
and their initializers were incorrectly inserted immediately after the
original global, breaking dependency order when the original global had
an initializer.
## Problem
The ReplicateGlobalsPerAffinity pass creates per-affinity copies of
globals by inserting new global operations and their initializers right
after the original global operation. This breaks the correct ordering
when the original global has an initializer, as the new initializers
(which load from the original global) would be placed before the
original global's initializer runs.
**Before (incorrect ordering):**
```
global
global_a <- inserted here
initializer_a <- loads from global (not yet initialized!)
initializer <- initializes global
```
**After (correct ordering):**
```
global
initializer <- initializes global
global_a <- inserted after initializer
initializer_a <- can safely load from global
```
## Solution
Modified `ValuePerAffinityHelper::getOrCreateGlobalForAffinity()` to
track and insert new globals after the last initializer that references
the original global:
1. **Implemented constructor caching**: Pre-computes the insertion point
(last initializer) for each global upfront using GlobalTable
2. **Efficient lookup**: Uses GlobalTable's forEach method with direct
access to storeOps collection after calling rebuild()
3. **Correct ordering**: Gets parent initializer directly from store
operations, ensuring new globals are inserted after all initializers
that reference the original global
## Testing
Added comprehensive test case `unknown_global_device_with_initializer`
that verifies:
- Original global and its initializer appear first
- Replicated globals and their initializers follow in correct dependency
order
- Each replicated initializer can safely load from the initialized
original global
## Performance
- O(N+M) complexity: GlobalTable scans globals and their uses once, then
forEach provides efficient iteration
- No redundant IR walking: uses pre-collected storeOps from GlobalTable
- Efficient lookup: cached insertion points avoid repeated scans
Fixes iree-org#22399
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: hanhanW <[email protected]>1 parent e9907a8 commit e47671d
File tree
2 files changed
+98
-4
lines changed- compiler/src/iree/compiler/Dialect/Stream/Transforms
- test
2 files changed
+98
-4
lines changedLines changed: 43 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | | - | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
73 | 77 | | |
74 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
75 | 108 | | |
76 | 109 | | |
77 | 110 | | |
| |||
126 | 159 | | |
127 | 160 | | |
128 | 161 | | |
129 | | - | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
130 | 169 | | |
131 | | - | |
| 170 | + | |
132 | 171 | | |
133 | 172 | | |
134 | 173 | | |
| |||
Lines changed: 55 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
46 | 101 | | |
47 | 102 | | |
48 | 103 | | |
| |||
0 commit comments