From 03b49c843a7005856cf1f7bc94ce9545cd0d834b Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Tue, 28 Jul 2015 18:47:00 +0000 Subject: [PATCH] AMDGPU: Don't try to use LDS/vector for private if pointer value stored If the pointer is the store's value operand, this would produce a broken module. Make sure the use is actually for the pointer operand. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243462 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp | 18 +++++-- .../promote-alloca-stored-pointer-value.ll | 52 +++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 test/CodeGen/AMDGPU/promote-alloca-stored-pointer-value.ll diff --git a/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp b/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp index cd968031a61..57b7a73bf56 100644 --- a/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp +++ b/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp @@ -134,13 +134,17 @@ static Value* GEPToVectorIndex(GetElementPtrInst *GEP) { // // TODO: Check isTriviallyVectorizable for calls and handle other // instructions. -static bool canVectorizeInst(Instruction *Inst) { +static bool canVectorizeInst(Instruction *Inst, User *User) { switch (Inst->getOpcode()) { case Instruction::Load: - case Instruction::Store: case Instruction::BitCast: case Instruction::AddrSpaceCast: return true; + case Instruction::Store: { + // Must be the stored pointer operand, not a stored value. + StoreInst *SI = cast(Inst); + return SI->getPointerOperand() == User; + } default: return false; } @@ -166,7 +170,7 @@ static bool tryPromoteAllocaToVector(AllocaInst *Alloca) { for (User *AllocaUser : Alloca->users()) { GetElementPtrInst *GEP = dyn_cast(AllocaUser); if (!GEP) { - if (!canVectorizeInst(cast(AllocaUser))) + if (!canVectorizeInst(cast(AllocaUser), Alloca)) return false; WorkList.push_back(AllocaUser); @@ -184,7 +188,7 @@ static bool tryPromoteAllocaToVector(AllocaInst *Alloca) { GEPVectorIdx[GEP] = Index; for (User *GEPUser : AllocaUser->users()) { - if (!canVectorizeInst(cast(GEPUser))) + if (!canVectorizeInst(cast(GEPUser), AllocaUser)) return false; WorkList.push_back(GEPUser); @@ -255,6 +259,12 @@ static bool collectUsesWithPtrTypes(Value *Val, std::vector &WorkList) { if (UseInst && UseInst->getOpcode() == Instruction::PtrToInt) return false; + if (StoreInst *SI = dyn_cast_or_null(UseInst)) { + // Reject if the stored value is not the pointer operand. + if (SI->getPointerOperand() != Val) + return false; + } + if (!User->getType()->isPointerTy()) continue; diff --git a/test/CodeGen/AMDGPU/promote-alloca-stored-pointer-value.ll b/test/CodeGen/AMDGPU/promote-alloca-stored-pointer-value.ll new file mode 100644 index 00000000000..2ee98cc3d2d --- /dev/null +++ b/test/CodeGen/AMDGPU/promote-alloca-stored-pointer-value.ll @@ -0,0 +1,52 @@ +; RUN: llc -march=amdgcn < %s | FileCheck -check-prefix=GCN %s + +; Pointer value is stored in a candidate for LDS usage. + +; GCN-LABEL: {{^}}stored_lds_pointer_value: +; GCN: buffer_store_dword v +define void @stored_lds_pointer_value(float* addrspace(1)* %ptr) #0 { + %tmp = alloca float + store float 0.0, float *%tmp + store float* %tmp, float* addrspace(1)* %ptr + ret void +} + +; GCN-LABEL: {{^}}stored_lds_pointer_value_gep: +; GCN-DAG: s_mov_b32 s{{[0-9]+}}, SCRATCH_RSRC_DWORD0 +; GCN-DAG: s_mov_b32 s{{[0-9]+}}, SCRATCH_RSRC_DWORD1 +; GCN: buffer_store_dword v +; GCN: buffer_store_dword v +define void @stored_lds_pointer_value_gep(float* addrspace(1)* %ptr, i32 %idx) #0 { +bb: + %tmp = alloca float, i32 16 + store float 0.0, float* %tmp + %tmp2 = getelementptr inbounds float, float* %tmp, i32 %idx + store float* %tmp2, float* addrspace(1)* %ptr + ret void +} + +; Pointer value is stored in a candidate for vector usage +; GCN-LABEL: {{^}}stored_vector_pointer_value: +; GCN-DAG: s_mov_b32 s{{[0-9]+}}, SCRATCH_RSRC_DWORD0 +; GCN-DAG: s_mov_b32 s{{[0-9]+}}, SCRATCH_RSRC_DWORD1 +; GCN: buffer_store_dword +; GCN: buffer_store_dword +; GCN: buffer_store_dword +; GCN: buffer_store_dword +define void @stored_vector_pointer_value(i32* addrspace(1)* %out, i32 %index) { +entry: + %tmp0 = alloca [4 x i32] + %x = getelementptr [4 x i32], [4 x i32]* %tmp0, i32 0, i32 0 + %y = getelementptr [4 x i32], [4 x i32]* %tmp0, i32 0, i32 1 + %z = getelementptr [4 x i32], [4 x i32]* %tmp0, i32 0, i32 2 + %w = getelementptr [4 x i32], [4 x i32]* %tmp0, i32 0, i32 3 + store i32 0, i32* %x + store i32 1, i32* %y + store i32 2, i32* %z + store i32 3, i32* %w + %tmp1 = getelementptr [4 x i32], [4 x i32]* %tmp0, i32 0, i32 %index + store i32* %tmp1, i32* addrspace(1)* %out + ret void +} + +attributes #0 = { nounwind } -- 2.34.1