-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[CaptureTracking] Handle ptrtoaddr #152221
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
Merged
arichardson
merged 5 commits into
main
from
users/arichardson/spr/capturetracking-handle-ptrtoaddr
Aug 8, 2025
Merged
[CaptureTracking] Handle ptrtoaddr #152221
arichardson
merged 5 commits into
main
from
users/arichardson/spr/capturetracking-handle-ptrtoaddr
Aug 8, 2025
Conversation
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
Created using spr 1.3.6-beta.1 [skip ci]
Created using spr 1.3.6-beta.1
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-llvm-analysis Author: Alexander Richardson (arichardson) ChangesUnlike ptrtoint, ptrtoaddr does not capture provenance, only the address. Full diff: https://github.com/llvm/llvm-project/pull/152221.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/CaptureTracking.cpp b/llvm/lib/Analysis/CaptureTracking.cpp
index 076f4176c0219..4857c3581b982 100644
--- a/llvm/lib/Analysis/CaptureTracking.cpp
+++ b/llvm/lib/Analysis/CaptureTracking.cpp
@@ -359,6 +359,11 @@ UseCaptureInfo llvm::DetermineUseCaptureKind(const Use &U, const Value *Base) {
case Instruction::AddrSpaceCast:
// The original value is not captured via this if the new value isn't.
return UseCaptureInfo::passthrough();
+ case Instruction::PtrToAddr:
+ // FIXME: the following does not work as expected, so just assume address
+ // is always captured:
+ // return UseCaptureInfo(CaptureComponents::None, CaptureComponents::Address);
+ return CaptureComponents::Address;
case Instruction::ICmp: {
unsigned Idx = U.getOperandNo();
unsigned OtherIdx = 1 - Idx;
diff --git a/llvm/test/Transforms/FunctionAttrs/nocapture.ll b/llvm/test/Transforms/FunctionAttrs/nocapture.ll
index 9d6acc410de75..2cba1069ecdc7 100644
--- a/llvm/test/Transforms/FunctionAttrs/nocapture.ll
+++ b/llvm/test/Transforms/FunctionAttrs/nocapture.ll
@@ -1082,6 +1082,65 @@ define i64 @captures_not_ret_only(ptr %p) {
ret i64 %int
}
+@gi = global i64 0
+
+;; Unlike ptrtoint, ptrtoaddr only captures the address
+define i64 @captures_ptrtoaddr_stored(ptr %p) {
+; FNATTRS: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(write, argmem: none, inaccessiblemem: none)
+; FNATTRS-LABEL: define noundef i64 @captures_ptrtoaddr_stored
+; FNATTRS-SAME: (ptr captures(address) [[P:%.*]]) #[[ATTR1]] {
+; FNATTRS-NEXT: [[INT:%.*]] = ptrtoaddr ptr [[P]] to i64
+; FNATTRS-NEXT: store i64 [[INT]], ptr @gi, align 8
+; FNATTRS-NEXT: ret i64 0
+;
+; ATTRIBUTOR: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(write)
+; ATTRIBUTOR-LABEL: define i64 @captures_ptrtoaddr_stored
+; ATTRIBUTOR-SAME: (ptr nofree writeonly [[P:%.*]]) #[[ATTR1]] {
+; ATTRIBUTOR-NEXT: [[INT:%.*]] = ptrtoaddr ptr [[P]] to i64
+; ATTRIBUTOR-NEXT: store i64 [[INT]], ptr @gi, align 8
+; ATTRIBUTOR-NEXT: ret i64 0
+;
+ %int = ptrtoaddr ptr %p to i64
+ store i64 %int, ptr @gi, align 8
+ ret i64 0
+}
+
+;; TODO: Shouldn't this be captures(ret: address)?
+define i64 @captures_ptrtoaddr_ret(ptr %p) {
+; FNATTRS: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+; FNATTRS-LABEL: define i64 @captures_ptrtoaddr_ret_only
+; FNATTRS-SAME: (ptr captures(address) [[P:%.*]]) #[[ATTR0]] {
+; FNATTRS-NEXT: [[INT:%.*]] = ptrtoaddr ptr [[P]] to i64
+; FNATTRS-NEXT: ret i64 [[INT]]
+;
+; ATTRIBUTOR: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+; ATTRIBUTOR-LABEL: define i64 @captures_ptrtoaddr_ret_only
+; ATTRIBUTOR-SAME: (ptr nofree readnone [[P:%.*]]) #[[ATTR0]] {
+; ATTRIBUTOR-NEXT: [[INT:%.*]] = ptrtoaddr ptr [[P]] to i64
+; ATTRIBUTOR-NEXT: ret i64 [[INT]]
+;
+ %int = ptrtoaddr ptr %p to i64
+ ret i64 %int
+}
+
+;; TODO: Shouldn't this be captures(none)?
+define i64 @captures_ptrtoaddr_ignored(ptr %p) {
+; FNATTRS: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+; FNATTRS-LABEL: define noundef i64 @captures_ptrtoaddr_ignored
+; FNATTRS-SAME: (ptr captures(address) [[P:%.*]]) #[[ATTR0]] {
+; FNATTRS-NEXT: [[INT:%.*]] = ptrtoaddr ptr [[P]] to i64
+; FNATTRS-NEXT: ret i64 0
+;
+; ATTRIBUTOR: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+; ATTRIBUTOR-LABEL: define i64 @captures_ptrtoaddr_ignored
+; ATTRIBUTOR-SAME: (ptr nofree readnone [[P:%.*]]) #[[ATTR0]] {
+; ATTRIBUTOR-NEXT: [[INT:%.*]] = ptrtoaddr ptr [[P]] to i64
+; ATTRIBUTOR-NEXT: ret i64 0
+;
+ %int = ptrtoaddr ptr %p to i64
+ ret i64 0
+}
+
define void @captures_read_provenance(ptr %p) {
; FNATTRS-LABEL: define void @captures_read_provenance
; FNATTRS-SAME: (ptr captures(address, read_provenance) [[P:%.*]]) {
|
arichardson
commented
Aug 5, 2025
arichardson
commented
Aug 5, 2025
Created using spr 1.3.6-beta.1 [skip ci]
Created using spr 1.3.6-beta.1
nikic
approved these changes
Aug 8, 2025
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.
LGTM
Created using spr 1.3.6-beta.1
llvm-sync bot
pushed a commit
to arm/arm-toolchain
that referenced
this pull request
Aug 8, 2025
Unlike ptrtoint, ptrtoaddr does not capture provenance, only the address. Note: As defined by the LangRef, we always treat `ptrtoaddr` as a location-independent address capture since it is a direct inspection of the pointer address. Reviewed By: nikic Pull Request: llvm/llvm-project#152221
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
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.
Unlike ptrtoint, ptrtoaddr does not capture provenance, only the address.
Note: As defined by the LangRef, we always treat
ptrtoaddr
as alocation-independent address capture since it is a direct inspection of the
pointer address.