-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[LowerConstantIntrinsics] Improve tests related to llvm.objectsize. NFC #132364
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
Open
bjope
wants to merge
2
commits into
llvm:main
Choose a base branch
from
bjope:objsize1
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
262 changes: 262 additions & 0 deletions
262
llvm/test/Transforms/LowerConstantIntrinsics/builtin-object-size-idxsize.ll
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,262 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: opt -passes=lower-constant-intrinsics -S < %s | FileCheck %s | ||
|
||
; Some extra tests using 16-bit pointers and 16-bit index type size. This | ||
; allows us to for example test what happens when the index type used in a | ||
; getelementptr does not match with the index type size (e.g. when not running | ||
; full opt pipeline before the lower-constant-intrinsics pass). | ||
|
||
target datalayout = "e-p:16:16:16" | ||
|
||
|
||
define i32 @possible_out_of_bounds_gep_i8(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @possible_out_of_bounds_gep_i8( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[OFFSET:%.*]] = select i1 [[C0]], i8 2, i8 10 | ||
; CHECK-NEXT: [[PTR_SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i8 [[OFFSET]] | ||
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i32 3, i32 0 | ||
; CHECK-NEXT: ret i32 [[RES]] | ||
; | ||
entry: | ||
%obj = alloca [5 x i8] | ||
%offset = select i1 %c0, i8 2, i8 10 | ||
%ptr.slide = getelementptr i8, ptr %obj, i8 %offset | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} | ||
|
||
define i32 @possible_out_of_bounds_gep_i16(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @possible_out_of_bounds_gep_i16( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[OFFSET:%.*]] = select i1 [[C0]], i16 2, i16 10 | ||
; CHECK-NEXT: [[PTR_SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i16 [[OFFSET]] | ||
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i32 3, i32 0 | ||
; CHECK-NEXT: ret i32 [[RES]] | ||
; | ||
entry: | ||
%obj = alloca [5 x i8] | ||
%offset = select i1 %c0, i16 2, i16 10 | ||
%ptr.slide = getelementptr i8, ptr %obj, i16 %offset | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} | ||
|
||
define i32 @possible_out_of_bounds_gep_i32(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @possible_out_of_bounds_gep_i32( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[OFFSET:%.*]] = select i1 [[C0]], i32 2, i32 10 | ||
; CHECK-NEXT: [[PTR_SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i32 [[OFFSET]] | ||
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i32 3, i32 0 | ||
; CHECK-NEXT: ret i32 [[RES]] | ||
; | ||
entry: | ||
%obj = alloca [5 x i8] | ||
%offset = select i1 %c0, i32 2, i32 10 | ||
%ptr.slide = getelementptr i8, ptr %obj, i32 %offset | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} | ||
|
||
; SROA would produce IR like this if applied to @possible_out_of_bounds_gep_i16. | ||
; FIXME: The %objsize_min result here is invalid. | ||
define i32 @possible_out_of_bounds_gep_i16_sroa(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @possible_out_of_bounds_gep_i16_sroa( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[DOTSROA_GEP:%.*]] = getelementptr i8, ptr [[OBJ]], i16 2 | ||
; CHECK-NEXT: [[DOTSROA_GEP1:%.*]] = getelementptr i8, ptr [[OBJ]], i16 10 | ||
; CHECK-NEXT: [[OFFSET_SROA_SEL:%.*]] = select i1 [[C0]], ptr [[DOTSROA_GEP]], ptr [[DOTSROA_GEP1]] | ||
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i32 3, i32 65531 | ||
; CHECK-NEXT: ret i32 [[RES]] | ||
; | ||
entry: | ||
%obj = alloca [5 x i8], align 1 | ||
%.sroa.gep = getelementptr i8, ptr %obj, i16 2 | ||
%.sroa.gep1 = getelementptr i8, ptr %obj, i16 10 | ||
%offset.sroa.sel = select i1 %c0, ptr %.sroa.gep, ptr %.sroa.gep1 | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %offset.sroa.sel, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %offset.sroa.sel, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} | ||
|
||
; Indices are truncated to the pointer size in a gep. So "i32 -65526" should | ||
; be treated as "i16 10" and we expect same result as for | ||
; @possible_out_of_bounds_gep_i16 above. | ||
; FIXME: The result here is incorrect (max/min is swapped). | ||
define i32 @possible_out_of_bounds_gep_i32_trunc(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @possible_out_of_bounds_gep_i32_trunc( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[OFFSET:%.*]] = select i1 [[C0]], i32 2, i32 -65526 | ||
; CHECK-NEXT: [[PTR_SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i32 [[OFFSET]] | ||
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i32 0, i32 3 | ||
; CHECK-NEXT: ret i32 [[RES]] | ||
; | ||
entry: | ||
%obj = alloca [5 x i8] | ||
%offset = select i1 %c0, i32 2, i32 -65526 ; 0xffff000a | ||
%ptr.slide = getelementptr i8, ptr %obj, i32 %offset | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} | ||
|
||
define i32 @out_of_bounds_gep_i8(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @out_of_bounds_gep_i8( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[PTR_SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i8 -128 | ||
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i32 -1, i32 0 | ||
; CHECK-NEXT: ret i32 [[RES]] | ||
; | ||
entry: | ||
%obj = alloca [5 x i8] | ||
%ptr.slide = getelementptr i8, ptr %obj, i8 -128 | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} | ||
|
||
define i32 @out_of_bounds_gep_i32(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @out_of_bounds_gep_i32( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[PTR_SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i32 10 | ||
; CHECK-NEXT: ret i32 0 | ||
; | ||
entry: | ||
%obj = alloca [5 x i8] | ||
%ptr.slide = getelementptr i8, ptr %obj, i32 10 | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} | ||
|
||
define i32 @out_of_bounds_gep_i32_trunc(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @out_of_bounds_gep_i32_trunc( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[PTR_SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i32 -65526 | ||
; CHECK-NEXT: ret i32 0 | ||
; | ||
entry: | ||
%obj = alloca [5 x i8] | ||
%ptr.slide = getelementptr i8, ptr %obj, i32 -65526 | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} | ||
|
||
; In this test the index will be out-of-bounds, but the current analysis won't | ||
; detect that. The analysis will find out that %offset is in the range [-2, | ||
; 10] which includes valid offsets that aren't out-of-bounds. Therefore we can | ||
; expect to get -1 for %objsize_max even if an advanced analysis should be | ||
; able to derive that we are out-of-bounds (returning 0 also for | ||
; %objsize_max). | ||
define i32 @out_of_bounds_gep_i16_pos_neg(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @out_of_bounds_gep_i16_pos_neg( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[OFFSET:%.*]] = select i1 [[C0]], i32 10, i32 -2 | ||
; CHECK-NEXT: [[PTR_SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i32 [[OFFSET]] | ||
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i32 -1, i32 0 | ||
; CHECK-NEXT: ret i32 [[RES]] | ||
; | ||
entry: | ||
%obj = alloca [5 x i8] | ||
%offset = select i1 %c0, i32 10, i32 -2 | ||
%ptr.slide = getelementptr i8, ptr %obj, i32 %offset | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} | ||
|
||
; With 16-bit index size %offset is either 32767 or -32768. Thus, when | ||
; aggregating the possible offsets it we know that it is in the range [-32768, | ||
; 32767], which includes valid offsets that aren't out-of-bounds. This is | ||
; similar to the out_of_bounds_gep_i16_pos_neg test above, and the current | ||
; implementation is expected to derive the result -1 for %objsize_max. | ||
define i32 @out_of_bounds_gep_i32_trunc_select(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @out_of_bounds_gep_i32_trunc_select( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[OFFSET:%.*]] = select i1 [[C0]], i32 32767, i32 32768 | ||
; CHECK-NEXT: [[PTR_SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i32 [[OFFSET]] | ||
; CHECK-NEXT: ret i32 0 | ||
; | ||
entry: | ||
%obj = alloca [5 x i8] | ||
%offset = select i1 %c0, i32 32767, i32 32768 | ||
%ptr.slide = getelementptr i8, ptr %obj, i32 %offset | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} | ||
|
||
; FIXME: Is 3 really correct for %objsize_min here? | ||
define i32 @possible_out_of_bounds_gep_i8_neg(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @possible_out_of_bounds_gep_i8_neg( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[OFFSET:%.*]] = select i1 [[C0]], i8 2, i8 -10 | ||
; CHECK-NEXT: [[PTR_SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i8 [[OFFSET]] | ||
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i32 -1, i32 3 | ||
; CHECK-NEXT: ret i32 [[RES]] | ||
; | ||
entry: | ||
%obj = alloca [5 x i8] | ||
%offset = select i1 %c0, i8 2, i8 -10 | ||
%ptr.slide = getelementptr i8, ptr %obj, i8 %offset | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} | ||
|
||
; FIXME: Is 3 really correct for %objsize_min here? | ||
define i32 @possible_out_of_bounds_gep_i16_neg(i1 %c0, i1 %c1) { | ||
; CHECK-LABEL: define i32 @possible_out_of_bounds_gep_i16_neg( | ||
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[OBJ:%.*]] = alloca [5 x i8], align 1 | ||
; CHECK-NEXT: [[OFFSET:%.*]] = select i1 [[C0]], i16 2, i16 -10 | ||
; CHECK-NEXT: [[PTR_SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i16 [[OFFSET]] | ||
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i32 -1, i32 3 | ||
; CHECK-NEXT: ret i32 [[RES]] | ||
; | ||
entry: | ||
%obj = alloca [5 x i8] | ||
%offset = select i1 %c0, i16 2, i16 -10 | ||
%ptr.slide = getelementptr i8, ptr %obj, i16 %offset | ||
%objsize_max = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 false, i1 true, i1 false) | ||
%objsize_min = call i32 @llvm.objectsize.i32.p0(ptr %ptr.slide, i1 true, i1 true, i1 false) | ||
%res = select i1 %c1, i32 %objsize_max, i32 %objsize_min | ||
ret i32 %res | ||
} |
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
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.
instead of having two check prefixes, I'd rather have two different functions and one check, one of which would just hard-code the output of sroa
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.
This is the alternative IR with the output of sroa.
(I've now removed the the alternative RUN line that I used as a reference. So I hope this makes more sense now.)