-
Notifications
You must be signed in to change notification settings - Fork 131
fix: integer-address pointer reconstruction (inttoptr + GEP)
#4251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ianna
wants to merge
4
commits into
main
Choose a base branch
from
ianna/integer-address_pointer_reconstruction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−26
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| import awkward as ak | ||
|
|
||
| numba = pytest.importorskip("numba") | ||
|
|
||
| ak.numba.register_and_check() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "array", | ||
| [ | ||
| ak.Array([True, False, True]), | ||
| ak.Array([1, 2, 3]), | ||
| ak.Array([1.5, 2.5, 3.5]), | ||
| ak.Array([[1, 2], [], [3]]), | ||
| ak.Array(["hello", "world"]), | ||
| ak.Array([b"hello", b"\x00\xff"]), | ||
| ], | ||
| ) | ||
| def test_numba_getitem_after_pointer_conversion(array): | ||
| @numba.njit | ||
| def getitem(array, index): | ||
| return array[index] | ||
|
|
||
| for index, expected in enumerate(array.to_list()): | ||
| assert ak.to_list(getitem(array, index)) == expected | ||
|
|
||
|
|
||
| def test_numba_ragged_string_membership(): | ||
| array = ak.Array( | ||
| [ | ||
| ["TRA", "TRB"], | ||
| [], | ||
| None, | ||
| ["TRG", "TRA"], | ||
| ] | ||
| ) | ||
|
|
||
| @numba.njit | ||
| def isin(array, haystack, builder): | ||
| for row in array: | ||
| builder.begin_list() | ||
|
|
||
| if row is not None: | ||
| for value in row: | ||
| builder.append(value in haystack) | ||
|
|
||
| builder.end_list() | ||
|
|
||
| return builder | ||
|
|
||
| result = isin( | ||
| array, | ||
| ("TRA", "TRB"), | ||
| ak.ArrayBuilder(), | ||
| ).snapshot() | ||
|
|
||
| assert result.to_list() == [ | ||
| [True, True], | ||
| [], | ||
| [], | ||
| [False, True], | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
inttoptrnecessary? In numba, we have seen LLVM loop-unrolling passes that ignores the pointer provenance frominttoptr. I highly recommend avoiding it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw
awkward/src/awkward/_connect/numba/arrayview.py
Line 279 in eae50c8
Can you model
arrayptrsasi8**(so like avoid**) instead ofintp*?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @sklam ! One thing I'd like to clarify is the scope of this PR change.
This PR updates Awkward to explicitly reconstruct typed pointers with
inttoptrbefore performing pointer arithmetic withgetelementptr, making it compatible with Numba's recent changes topointer_add.However, I don't think we should describe this as fully solving pointer provenance. Awkward's lookup table still stores buffer addresses as
intpvalues, so the original pointer provenance has already been discarded before the lowering reconstructs a pointer. This PR improves correctness relative to the new Numba implementation, but it doesn't eliminate the underlying design issue.I think preserving pointer provenance end-to-end would require a larger redesign of the lookup representation (for example, separating integer layout metadata from a table of actual buffer pointers). I've opened a follow-up issue to discuss that independently: #4252
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm recommending extra caution here because:
a) The pointer provenance issue is complicated by the uncertain state in LLVM. It is a LLVM design bug that is being addressed. See https://www.npopov.com/2026/01/31/This-year-in-LLVM-2025.html#ptrtoaddr .
b) The effect from it is very difficult to predict, replicate or track down. Undefined behavior gives LLVM the license to do very unusual things (see LLVM blog). For example, Numba saw the issue to only appear in a very particular loop nest pattern and on a specific CPU ISA due to LLVM's target cost model.
There are more details from my debugging in the original Numba issue here: numba/numba#10695 (comment).
All these made me scared about having
inttoptrandptrtoint.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @sklam ! You are right it is a dangerous territory! I ought to look into a safer fix.