-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[SeparateConstOffsetFromGEP] Don't set unsound inbounds flag #130616
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
ritter-x2a
merged 9 commits into
main
from
users/ritter-x2a/03-10-_separateconstoffsetfromgep_don_t_set_unsound_inbounds_flag
Mar 18, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8f9b64b
[SeparateConstOffsetFromGEP] Don't set unsound inbounds flag
ritter-x2a 824d059
Simplify the test case so that it is easier to handle for Alive2
ritter-x2a 00cdea8
No auto for GEPNoWrapFlags
ritter-x2a 61b11c1
Massage memory_clause.ll so that offsets are still generated
ritter-x2a c4c2d67
Make sure that offsets are still folded...
ritter-x2a b2d3330
Add some addressing mode tests that show the result of dropping the i…
ritter-x2a 4559f61
fold-gep-offset.ll: Drop unrelated addrspaces, drop nuw, more express…
ritter-x2a 3ab2f88
Adjust fold-gep-offset.ll to trunk changes
ritter-x2a 92a516e
Drop explicit addrspace(0)
ritter-x2a 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,159 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py | ||
|
||
; RUN: llc -mtriple=amdgcn -mcpu=gfx90a -mattr=-enable-flat-scratch < %s | FileCheck --check-prefixes=GFX90A,GFX90A-MUBUF %s | ||
; RUN: llc -mtriple=amdgcn -mcpu=gfx90a -mattr=+enable-flat-scratch < %s | FileCheck --check-prefixes=GFX90A,GFX90A-FLATSCR %s | ||
; RUN: llc -mtriple=amdgcn -mcpu=gfx1030 -mattr=-enable-flat-scratch < %s | FileCheck --check-prefixes=GFX10,GFX10-MUBUF %s | ||
; RUN: llc -mtriple=amdgcn -mcpu=gfx1030 -mattr=+enable-flat-scratch < %s | FileCheck --check-prefixes=GFX10,GFX10-FLATSCR %s | ||
; RUN: llc -mtriple=amdgcn -mcpu=gfx942 < %s | FileCheck --check-prefixes=GFX942 %s | ||
; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 < %s | FileCheck --check-prefixes=GFX11 %s | ||
; RUN: llc -mtriple=amdgcn -mcpu=gfx1200 < %s | FileCheck --check-prefixes=GFX12 %s | ||
|
||
; This test checks memory addresses with constant offset components that should | ||
; not be folded into memory accesses with immediate offsets. | ||
; SeparateConstOffsetsFromGEP transforms the GEPs in a way that can lead to | ||
; out-of-bounds or negative intermediate results in the address computation, | ||
; which are problematic for flat and scratch instructions: | ||
; gep[inbounds](p, i + 3) -> gep(gep(p, i), 3) | ||
|
||
|
||
; FIXME the offset here should not be folded: if %p points to the beginning of | ||
; scratch or LDS and %i is -1, a folded offset crashes the program. | ||
define i32 @flat_offset_maybe_oob(ptr %p, i32 %i) { | ||
; GFX90A-LABEL: flat_offset_maybe_oob: | ||
; GFX90A: ; %bb.0: | ||
; GFX90A-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; GFX90A-NEXT: v_ashrrev_i32_e32 v3, 31, v2 | ||
; GFX90A-NEXT: v_lshlrev_b64 v[2:3], 2, v[2:3] | ||
; GFX90A-NEXT: v_add_co_u32_e32 v0, vcc, v0, v2 | ||
; GFX90A-NEXT: v_addc_co_u32_e32 v1, vcc, v1, v3, vcc | ||
; GFX90A-NEXT: flat_load_dword v0, v[0:1] offset:12 | ||
; GFX90A-NEXT: s_waitcnt vmcnt(0) lgkmcnt(0) | ||
; GFX90A-NEXT: s_setpc_b64 s[30:31] | ||
; | ||
; GFX10-LABEL: flat_offset_maybe_oob: | ||
; GFX10: ; %bb.0: | ||
; GFX10-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; GFX10-NEXT: v_ashrrev_i32_e32 v3, 31, v2 | ||
; GFX10-NEXT: v_lshlrev_b64 v[2:3], 2, v[2:3] | ||
; GFX10-NEXT: v_add_co_u32 v0, vcc_lo, v0, v2 | ||
; GFX10-NEXT: v_add_co_ci_u32_e32 v1, vcc_lo, v1, v3, vcc_lo | ||
; GFX10-NEXT: flat_load_dword v0, v[0:1] offset:12 | ||
; GFX10-NEXT: s_waitcnt vmcnt(0) lgkmcnt(0) | ||
; GFX10-NEXT: s_setpc_b64 s[30:31] | ||
; | ||
; GFX942-LABEL: flat_offset_maybe_oob: | ||
; GFX942: ; %bb.0: | ||
; GFX942-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; GFX942-NEXT: v_ashrrev_i32_e32 v3, 31, v2 | ||
; GFX942-NEXT: v_lshl_add_u64 v[0:1], v[2:3], 2, v[0:1] | ||
; GFX942-NEXT: flat_load_dword v0, v[0:1] offset:12 | ||
; GFX942-NEXT: s_waitcnt vmcnt(0) lgkmcnt(0) | ||
; GFX942-NEXT: s_setpc_b64 s[30:31] | ||
; | ||
; GFX11-LABEL: flat_offset_maybe_oob: | ||
; GFX11: ; %bb.0: | ||
; GFX11-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; GFX11-NEXT: v_ashrrev_i32_e32 v3, 31, v2 | ||
; GFX11-NEXT: s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1) | ||
; GFX11-NEXT: v_lshlrev_b64 v[2:3], 2, v[2:3] | ||
; GFX11-NEXT: v_add_co_u32 v0, vcc_lo, v0, v2 | ||
; GFX11-NEXT: s_delay_alu instid0(VALU_DEP_2) | ||
; GFX11-NEXT: v_add_co_ci_u32_e32 v1, vcc_lo, v1, v3, vcc_lo | ||
; GFX11-NEXT: flat_load_b32 v0, v[0:1] offset:12 | ||
; GFX11-NEXT: s_waitcnt vmcnt(0) lgkmcnt(0) | ||
; GFX11-NEXT: s_setpc_b64 s[30:31] | ||
; | ||
; GFX12-LABEL: flat_offset_maybe_oob: | ||
; GFX12: ; %bb.0: | ||
; GFX12-NEXT: s_wait_loadcnt_dscnt 0x0 | ||
; GFX12-NEXT: s_wait_expcnt 0x0 | ||
; GFX12-NEXT: s_wait_samplecnt 0x0 | ||
; GFX12-NEXT: s_wait_bvhcnt 0x0 | ||
; GFX12-NEXT: s_wait_kmcnt 0x0 | ||
; GFX12-NEXT: v_ashrrev_i32_e32 v3, 31, v2 | ||
; GFX12-NEXT: s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1) | ||
; GFX12-NEXT: v_lshlrev_b64_e32 v[2:3], 2, v[2:3] | ||
; GFX12-NEXT: v_add_co_u32 v0, vcc_lo, v0, v2 | ||
; GFX12-NEXT: s_wait_alu 0xfffd | ||
; GFX12-NEXT: v_add_co_ci_u32_e32 v1, vcc_lo, v1, v3, vcc_lo | ||
; GFX12-NEXT: flat_load_b32 v0, v[0:1] offset:12 | ||
; GFX12-NEXT: s_wait_loadcnt_dscnt 0x0 | ||
; GFX12-NEXT: s_wait_alu 0xfffd | ||
; GFX12-NEXT: s_setpc_b64 s[30:31] | ||
%idx = add nsw i32 %i, 3 | ||
%arrayidx = getelementptr inbounds i32, ptr %p, i32 %idx | ||
%l = load i32, ptr %arrayidx | ||
ret i32 %l | ||
} | ||
|
||
; For MUBUF and for GFX12, folding the offset is okay. | ||
define i32 @private_offset_maybe_oob(ptr addrspace(5) %p, i32 %i) { | ||
; GFX90A-MUBUF-LABEL: private_offset_maybe_oob: | ||
; GFX90A-MUBUF: ; %bb.0: | ||
; GFX90A-MUBUF-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; GFX90A-MUBUF-NEXT: v_lshl_add_u32 v0, v1, 2, v0 | ||
; GFX90A-MUBUF-NEXT: buffer_load_dword v0, v0, s[0:3], 0 offen offset:12 | ||
; GFX90A-MUBUF-NEXT: s_waitcnt vmcnt(0) | ||
; GFX90A-MUBUF-NEXT: s_setpc_b64 s[30:31] | ||
; | ||
; GFX90A-FLATSCR-LABEL: private_offset_maybe_oob: | ||
; GFX90A-FLATSCR: ; %bb.0: | ||
; GFX90A-FLATSCR-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; GFX90A-FLATSCR-NEXT: v_lshlrev_b32_e32 v1, 2, v1 | ||
; GFX90A-FLATSCR-NEXT: v_add3_u32 v0, v0, v1, 12 | ||
; GFX90A-FLATSCR-NEXT: scratch_load_dword v0, v0, off | ||
; GFX90A-FLATSCR-NEXT: s_waitcnt vmcnt(0) | ||
; GFX90A-FLATSCR-NEXT: s_setpc_b64 s[30:31] | ||
; | ||
; GFX10-MUBUF-LABEL: private_offset_maybe_oob: | ||
; GFX10-MUBUF: ; %bb.0: | ||
; GFX10-MUBUF-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; GFX10-MUBUF-NEXT: v_lshl_add_u32 v0, v1, 2, v0 | ||
; GFX10-MUBUF-NEXT: buffer_load_dword v0, v0, s[0:3], 0 offen offset:12 | ||
; GFX10-MUBUF-NEXT: s_waitcnt vmcnt(0) | ||
; GFX10-MUBUF-NEXT: s_setpc_b64 s[30:31] | ||
; | ||
; GFX10-FLATSCR-LABEL: private_offset_maybe_oob: | ||
; GFX10-FLATSCR: ; %bb.0: | ||
; GFX10-FLATSCR-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; GFX10-FLATSCR-NEXT: v_lshlrev_b32_e32 v1, 2, v1 | ||
; GFX10-FLATSCR-NEXT: v_add3_u32 v0, v0, v1, 12 | ||
; GFX10-FLATSCR-NEXT: scratch_load_dword v0, v0, off | ||
; GFX10-FLATSCR-NEXT: s_waitcnt vmcnt(0) | ||
; GFX10-FLATSCR-NEXT: s_setpc_b64 s[30:31] | ||
; | ||
; GFX942-LABEL: private_offset_maybe_oob: | ||
; GFX942: ; %bb.0: | ||
; GFX942-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; GFX942-NEXT: v_lshlrev_b32_e32 v1, 2, v1 | ||
; GFX942-NEXT: v_add3_u32 v0, v0, v1, 12 | ||
; GFX942-NEXT: scratch_load_dword v0, v0, off | ||
; GFX942-NEXT: s_waitcnt vmcnt(0) | ||
; GFX942-NEXT: s_setpc_b64 s[30:31] | ||
; | ||
; GFX11-LABEL: private_offset_maybe_oob: | ||
; GFX11: ; %bb.0: | ||
; GFX11-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; GFX11-NEXT: v_lshlrev_b32_e32 v1, 2, v1 | ||
; GFX11-NEXT: s_delay_alu instid0(VALU_DEP_1) | ||
; GFX11-NEXT: v_add3_u32 v0, v0, v1, 12 | ||
; GFX11-NEXT: scratch_load_b32 v0, v0, off | ||
; GFX11-NEXT: s_waitcnt vmcnt(0) | ||
; GFX11-NEXT: s_setpc_b64 s[30:31] | ||
; | ||
; GFX12-LABEL: private_offset_maybe_oob: | ||
; GFX12: ; %bb.0: | ||
; GFX12-NEXT: s_wait_loadcnt_dscnt 0x0 | ||
; GFX12-NEXT: s_wait_expcnt 0x0 | ||
; GFX12-NEXT: s_wait_samplecnt 0x0 | ||
; GFX12-NEXT: s_wait_bvhcnt 0x0 | ||
; GFX12-NEXT: s_wait_kmcnt 0x0 | ||
; GFX12-NEXT: v_lshl_add_u32 v0, v1, 2, v0 | ||
; GFX12-NEXT: scratch_load_b32 v0, v0, off offset:12 | ||
; GFX12-NEXT: s_wait_loadcnt 0x0 | ||
; GFX12-NEXT: s_setpc_b64 s[30:31] | ||
%idx = add nsw i32 %i, 3 | ||
%arrayidx = getelementptr inbounds i32, ptr addrspace(5) %p, i32 %idx | ||
%l = load i32, ptr addrspace(5) %arrayidx | ||
ret i32 %l | ||
} |
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
18 changes: 18 additions & 0 deletions
18
llvm/test/Transforms/SeparateConstOffsetFromGEP/AMDGPU/preserve-inbounds.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,18 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py | ||
; RUN: opt < %s -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1030 -passes=separate-const-offset-from-gep -S | FileCheck %s | ||
|
||
; The inbounds flags cannot be preserved here: If the pointers point to the | ||
; beginning of an object and %i is 1, the intermediate GEPs are out of bounds. | ||
define ptr @maybe_oob(ptr %p, i64 %i) { | ||
; CHECK-LABEL: @maybe_oob( | ||
; CHECK-NEXT: entry: | ||
; CHECK-NEXT: [[IDX1:%.*]] = sub i64 0, [[I:%.*]] | ||
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[IDX1]] | ||
; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr i8, ptr [[TMP0]], i64 4 | ||
; CHECK-NEXT: ret ptr [[ARRAYIDX2]] | ||
; | ||
entry: | ||
%idx = sub nsw i64 1, %i | ||
%arrayidx = getelementptr inbounds i32, ptr %p, i64 %idx | ||
ret ptr %arrayidx | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.