You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Variable loc info PR2: Add location alias parsing support in IR parser and trace processor (#187)
Summary:
fix#86
This PR implements comprehensive support for parsing location aliases in TTIR/TTGIR files. It adds regex patterns to match alias definitions, implements alias resolution with cycle detection, tracks metadata (definition lines, alias names, alias targets), and propagates this information through the trace processor to the frontend.
## Background
Recent Triton compiler updates introduced a new location pattern with aliases:
- Named aliases: `#loc16 = loc("pid"(#loc2))` - references `#loc2` with name "pid"
- Bare #loc aliases: `#loc13 = loc("x_ptr"(#loc))` - references main `#loc` with name "x_ptr"
- Simple aliases: `#loc20 = loc(#loc16)` - direct reference without a name
These aliases are used extensively for function parameters and variable names in the new TTIR format.
## Changes
### Backend - IR Parser (`tritonparse/ir_parser.py`)
**New regex patterns:**
- `ALIAS_WITH_NAME_PATTERN`: Matches `#loc13 = loc("x_ptr"(#loc))`
- `ALIAS_SIMPLE_PATTERN`: Matches `#loc20 = loc(#loc16)`
- Updated `LOC_PATTERN`: Now uses `\d+` (only matches numbered locs, not bare `#loc`)
**Alias resolution logic:**
- Build alias map: `alias_id` → `target_id`
- Track definition lines for each location
- Extract alias names from named aliases
- Resolve alias chains recursively with cycle detection
- Store metadata: `def_line`, `alias_name`, `alias_of`
**Key implementation details:**
- Empty string `""` represents bare `#loc` (from PR1)
- Recursive `resolve_alias()` function follows chains to base locations
- Cycle detection prevents infinite loops in malformed IR
### Backend - Trace Processor (`tritonparse/trace_processor.py`)
**Metadata propagation:**
- Add `alias_name` to code reference entries
- Add `loc_id` to identify which location is being referenced
- Create separate entries for loc definition lines with `kind="loc_def"`
- Include all metadata (`alias_name`, `alias_of`, `loc_id`) for definition lines
### Testing (`tests/test_tritonparse.py`)
**New test: `test_loc_alias_parsing()`**
- Tests bare `#loc` references (e.g., function parameters)
- Tests named aliases with numbered references
- Tests simple aliases without names
- Verifies definition line tracking
- Validates alias chain resolution
- Moved to `TestTritonparseCPU` class (doesn't require CUDA)
**Test coverage:**
- Bare #loc: `#loc13 = loc("x_ptr"(#loc))`
- Numbered alias: `#loc16 = loc("y"(#loc1))`
- Chain resolution: `#loc17 → #loc3 → file location`
- Metadata: `alias_name`, `alias_of`, `def_line`
## Benefits
1. **Correct parsing**: Handles new Triton compiler location format
2. **Rich metadata**: Preserves alias names and relationships
3. **Robust**: Cycle detection prevents crashes on malformed IR
4. **Testable**: Comprehensive unit tests ensure correctness
5. **Foundation**: Enables frontend visualization (PR3)
## Backward Compatibility
- ✅ Old TTIR format without aliases continues to work
- ✅ New metadata fields are optional
- ✅ No breaking changes to existing API
## Dependencies
- **Requires**: PR1 (main #loc key fix)
- **Enables**: PR3 (frontend UI visualization)
## Example
Input TTIR:
```mlir
#loc = loc("/scratch/test.py":308:0)
#loc13 = loc("x_ptr"(#loc))
#loc1 = loc("/scratch/test.py":315:12)
#loc16 = loc("y"(#loc1))
```
Parsed locations:
```python
{
"": {"file": "/scratch/test.py", "line": 308, "def_line": 1},
"13": {"file": "/scratch/test.py", "line": 308, "alias_name": "x_ptr", "alias_of": "", "def_line": 2},
"1": {"file": "/scratch/test.py", "line": 315, "def_line": 3},
"16": {"file": "/scratch/test.py", "line": 315, "alias_name": "y", "alias_of": "1", "def_line": 4}
}
```
## Files Changed
- `tritonparse/ir_parser.py`: +93 lines (regex patterns, alias resolution)
- `tritonparse/trace_processor.py`: +27 lines (metadata propagation)
- `tests/test_tritonparse.py`: +64 lines (comprehensive tests)
## Testing
```bash
# Run the new test
python -m unittest tests.test_tritonparse.TestTritonparseCPU.test_loc_alias_parsing -v
```
All tests pass ✓
Pull Request resolved: #187
Reviewed By: adamomainz
Differential Revision: D85605707
Pulled By: FindHao
fbshipit-source-id: 567595c6ef735a743014e0432cd88e7325ed0fa5
0 commit comments