Skip to content

Fix local consts data ref - #8

Draft
Mittagskogel wants to merge 1 commit into
spcl:mainfrom
Mittagskogel:local-constant-dataref
Draft

Mittagskogel wants to merge 1 commit into
spcl:mainfrom
Mittagskogel:local-constant-dataref

Conversation

@Mittagskogel

Copy link
Copy Markdown

This PR extends the capabilities of the _track_local_consts() AST desugaring transformation.

By introducing a new spec generator find_indexed_dataref_component_spec(), we enable constant replacement of Part_Refs and Data_Refs like a(5) or a % b(4) % c(7,3). These Data_Refs get turned into specs like ('main', ('a', 5)) or (('main', 'a'), (a, ('b', 4), ('c', 7, 3)). Furthermore, the constant tracker can detect and replace array pointer references:

SUBROUTINE foo(var)
REAL, TARGET arr(4, 6)
REAL, POINTER arrptr
REAL, INTENT(IN) :: var

arrptr => arr(:,:)
arrptr(2,3) = var  ! This gets replaced with arr(2,3) = var
END SUBROUTINE foo

Additionally, we fix a bug in which variable pointer assignments are propagated. For example:

SUBROUTINE bar(idx)
REAL, TARGET :: arr(6)
REAL, POINTER :: ptr

REAL :: var
INTEGER, INTENT(IN) :: idx

ptr => arr(idx)
idx = idx + 1
var = ptr  ! arr(idx) cannot be propagated here because idx has changed!
END SUBROUTINE bar

The fix checks whether Data_Ref nodes on the RHS of the pointer assignment contain any variables.

Ported from spcl/dace#2213

Extend local constant tracking to indexed array elements and data-ref
components, allowing substitution of scalar constant sub-expressions.

Also tighten pointer target propagation: only track a pointer as a known
alias when its RHS is a plain target or constant-indexed element;
variable-indexed targets are left symbolic.

Add regression tests in tests/inliner/optimizations_test.py.

Co-Authored-By: Claude Code <noreply@anthropic.com>
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