Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,10 +991,21 @@ bool GCNTTIImpl::isSourceOfDivergence(const Value *V) const {
return true;

if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
if (Intrinsic->getIntrinsicID() == Intrinsic::read_register)
Intrinsic::ID IID = Intrinsic->getIntrinsicID();
switch (IID) {
case Intrinsic::read_register:
return isReadRegisterSourceOfDivergence(Intrinsic);

return AMDGPU::isIntrinsicSourceOfDivergence(Intrinsic->getIntrinsicID());
case Intrinsic::amdgcn_addrspacecast_nonnull: {
unsigned SrcAS =
Intrinsic->getOperand(0)->getType()->getPointerAddressSpace();
unsigned DstAS = Intrinsic->getType()->getPointerAddressSpace();
return SrcAS == AMDGPUAS::PRIVATE_ADDRESS &&
DstAS == AMDGPUAS::FLAT_ADDRESS &&
ST->hasGloballyAddressableScratch();
}
default:
return AMDGPU::isIntrinsicSourceOfDivergence(IID);
}
}

// Assume all function calls are a source of divergence.
Expand All @@ -1008,6 +1019,15 @@ bool GCNTTIImpl::isSourceOfDivergence(const Value *V) const {
if (isa<InvokeInst>(V))
return true;

// If the target supports globally addressable scratch, the mapping from
// scratch memory to the flat aperture changes therefore an address space cast
// is no longer uniform.
if (auto *CastI = dyn_cast<AddrSpaceCastInst>(V)) {
return CastI->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS &&
CastI->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS &&
ST->hasGloballyAddressableScratch();
}

return false;
}

Expand Down
25 changes: 25 additions & 0 deletions llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10074,7 +10074,30 @@ unsigned SIInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,

InstructionUniformity
SIInstrInfo::getGenericInstructionUniformity(const MachineInstr &MI) const {
const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
unsigned opcode = MI.getOpcode();

auto HandleAddrSpaceCast = [this, &MRI](const MachineInstr &MI) {
Register Dst = MI.getOperand(0).getReg();
Register Src = isa<GIntrinsic>(MI) ? MI.getOperand(2).getReg()
: MI.getOperand(1).getReg();
LLT DstTy = MRI.getType(Dst);
LLT SrcTy = MRI.getType(Src);
unsigned DstAS = DstTy.getAddressSpace();
unsigned SrcAS = SrcTy.getAddressSpace();
return SrcAS == AMDGPUAS::PRIVATE_ADDRESS &&
DstAS == AMDGPUAS::FLAT_ADDRESS &&
ST.hasGloballyAddressableScratch()
? InstructionUniformity::NeverUniform
: InstructionUniformity::Default;
};

// If the target supports globally addressable scratch, the mapping from
// scratch memory to the flat aperture changes therefore an address space cast
// is no longer uniform.
if (opcode == TargetOpcode::G_ADDRSPACE_CAST)
return HandleAddrSpaceCast(MI);

if (auto *GI = dyn_cast<GIntrinsic>(&MI)) {
auto IID = GI->getIntrinsicID();
if (AMDGPU::isIntrinsicSourceOfDivergence(IID))
Expand All @@ -10083,6 +10106,8 @@ SIInstrInfo::getGenericInstructionUniformity(const MachineInstr &MI) const {
return InstructionUniformity::AlwaysUniform;

switch (IID) {
case Intrinsic::amdgcn_addrspacecast_nonnull:
return HandleAddrSpaceCast(MI);
case Intrinsic::amdgcn_if:
case Intrinsic::amdgcn_else:
// FIXME: Uniform if second result
Expand Down
35 changes: 35 additions & 0 deletions llvm/test/Analysis/UniformityAnalysis/AMDGPU/MIR/addrspacecast.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# NOTE: This file is Generic MIR translation of llvm/test/Analysis/UniformityAnalysis/AMDGPU/addrspacecast.ll test file
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -run-pass=print-machine-uniformity -filetype=null %s 2>&1 | FileCheck %s --check-prefix=UNI
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1250 -run-pass=print-machine-uniformity -filetype=null %s 2>&1 | FileCheck %s --check-prefix=DIV

# UNI: ALL VALUES UNIFORM
# DIV: DIVERGENT: %3: %3:_(p0) = G_ADDRSPACE_CAST %2:_(p5)
# DIV: DIVERGENT: %4: %4:_(p0) = G_INTRINSIC intrinsic(@llvm.amdgcn.addrspacecast.nonnull), %2:_(p5)

--- |
define void @foo() {
%alloca = alloca i32, align 4, addrspace(5)
%cast = addrspacecast ptr addrspace(5) %alloca to ptr
store i32 1, ptr %cast, align 4
%cast.1 = call ptr @llvm.amdgcn.addrspacecast.nonnull.p0.p5(ptr addrspace(5) %alloca)
store i32 2, ptr %cast.1, align 4
ret void
}
...
---
name: foo
stack:
- { id: 0, name: alloca, type: default, offset: 0, size: 4, alignment: 4,
stack-id: default, callee-saved-register: '', callee-saved-restored: true,
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
body: |
bb.1 (%ir-block.0):
%10:_(s32) = G_CONSTANT i32 1
%12:_(s32) = G_CONSTANT i32 2
%8:_(p5) = G_FRAME_INDEX %stack.0.alloca
%9:_(p0) = G_ADDRSPACE_CAST %8(p5)
G_STORE %10(s32), %9(p0) :: (store (s32) into %ir.cast)
%11:_(p0) = G_INTRINSIC intrinsic(@llvm.amdgcn.addrspacecast.nonnull), %8(p5)
G_STORE %12(s32), %11(p0) :: (store (s32) into %ir.cast.1)
SI_RETURN
...
14 changes: 14 additions & 0 deletions llvm/test/Analysis/UniformityAnalysis/AMDGPU/addrspacecast.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; RUN: opt -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -passes='print<uniformity>' -disable-output %s 2>&1 | FileCheck %s --check-prefix=UNI
; RUN: opt -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1250 -passes='print<uniformity>' -disable-output %s 2>&1 | FileCheck %s --check-prefix=DIV

; UNI: ALL VALUES UNIFORM
; DIV: DIVERGENT: %cast = addrspacecast ptr addrspace(5) %alloca to ptr
; DIV: DIVERGENT: %cast.1 = call ptr @llvm.amdgcn.addrspacecast.nonnull.p0.p5(ptr addrspace(5) %alloca)
define void @foo() {
%alloca = alloca i32, align 4, addrspace(5)
%cast = addrspacecast ptr addrspace(5) %alloca to ptr
store i32 1, ptr %cast
%cast.1 = call ptr @llvm.amdgcn.addrspacecast.nonnull.p0.p5(ptr addrspace(5) %alloca)
store i32 2, ptr %cast.1
ret void
}