Skip to content

Commit 483cb20

Browse files
committed
[AMDGPU] Check noalias.addrspace in mayAccessScratchThroughFlat
PR #149247 made the MD accessible by the backend so we can now leverage it in the memory model. The first use case here is detecting if a flat op can access scratch memory. Benefits both the MemoryLegalizer and InsertWaitCnt.
1 parent 19803d8 commit 483cb20

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

llvm/lib/Target/AMDGPU/SIInstrInfo.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4249,6 +4249,24 @@ bool SIInstrInfo::isAlwaysGDS(uint16_t Opcode) const {
42494249
Opcode == AMDGPU::DS_SUB_GS_REG_RTN || isGWS(Opcode);
42504250
}
42514251

4252+
static bool hasNoAliasAddrSpaceScratch(const MachineMemOperand *MemOp) {
4253+
const MDNode *MD = MemOp->getAAInfo().NoAliasAddrSpace;
4254+
if (!MD)
4255+
return false;
4256+
4257+
// This MD is structured in ranges [A, B)
4258+
// Check if PRIVATE is included in any of them.
4259+
for (unsigned I = 0, E = MD->getNumOperands() / 2; I != E; ++I) {
4260+
auto *Low = mdconst::extract<ConstantInt>(MD->getOperand(2 * I + 0));
4261+
auto *High = mdconst::extract<ConstantInt>(MD->getOperand(2 * I + 1));
4262+
if (Low->getValue().ule(AMDGPUAS::PRIVATE_ADDRESS) &&
4263+
High->getValue().ugt(AMDGPUAS::PRIVATE_ADDRESS))
4264+
return true;
4265+
}
4266+
4267+
return false;
4268+
}
4269+
42524270
bool SIInstrInfo::mayAccessScratchThroughFlat(const MachineInstr &MI) const {
42534271
if (!isFLAT(MI) || isFLATGlobal(MI))
42544272
return false;
@@ -4271,7 +4289,9 @@ bool SIInstrInfo::mayAccessScratchThroughFlat(const MachineInstr &MI) const {
42714289
// See if any memory operand specifies an address space that involves scratch.
42724290
return any_of(MI.memoperands(), [](const MachineMemOperand *Memop) {
42734291
unsigned AS = Memop->getAddrSpace();
4274-
return AS == AMDGPUAS::PRIVATE_ADDRESS || AS == AMDGPUAS::FLAT_ADDRESS;
4292+
if (AS == AMDGPUAS::FLAT_ADDRESS)
4293+
return !hasNoAliasAddrSpaceScratch(Memop);
4294+
return AS == AMDGPUAS::PRIVATE_ADDRESS;
42754295
});
42764296
}
42774297

llvm/test/CodeGen/AMDGPU/gfx1250-scratch-scope-se.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,19 @@ define void @test_flat_store_no_scratch_alloc(ptr %ptr, i32 %val) #0 {
3939
ret void
4040
}
4141

42-
; TODO: handle
4342
define void @test_flat_store_noalias_addrspace(ptr %ptr, i32 %val) {
4443
; GCN-LABEL: test_flat_store_noalias_addrspace:
4544
; GCN: ; %bb.0:
4645
; GCN-NEXT: s_wait_loadcnt_dscnt 0x0
4746
; GCN-NEXT: s_wait_kmcnt 0x0
48-
; GCN-NEXT: flat_store_b32 v[0:1], v2 scope:SCOPE_SE
47+
; GCN-NEXT: flat_store_b32 v[0:1], v2
4948
; GCN-NEXT: s_wait_dscnt 0x0
5049
; GCN-NEXT: s_set_pc_i64 s[30:31]
5150
store i32 %val, ptr %ptr, !noalias.addrspace !{i32 5, i32 6}
5251
ret void
5352
}
5453

55-
; TODO: would be nice to handle too
54+
; TODO: would be nice to handle
5655
define void @test_flat_store_select(ptr addrspace(1) %a, ptr addrspace(3) %b, i1 %cond, i32 %val) {
5756
; GCN-SDAG-LABEL: test_flat_store_select:
5857
; GCN-SDAG: ; %bb.0:

0 commit comments

Comments
 (0)