Skip to content

Commit

Permalink
Teach ADCE about OpImageTexelPointer
Browse files Browse the repository at this point in the history
Currently OpImageTexelPointer operations are treat like a use of the
pointer, but it does
not look for the memory being referenced to make sure stores are not
removed.

This change teaches it so identify the memory being accessed, and
treats it as if that memory is loaded.

Contributes to #1445.
  • Loading branch information
s-perron committed Apr 2, 2018
1 parent fc9f621 commit 1fccc6c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
9 changes: 9 additions & 0 deletions source/opt/aggressive_dead_code_elim_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,15 @@ bool AggressiveDCEPass::AggressiveDCE(ir::Function* func) {
else if (liveInst->opcode() == SpvOpFunctionParameter) {
ProcessLoad(liveInst->result_id());
}
// We treat an OpImageTexelPointer as a load of the pointer, and
// that value is manipulated to get the result.
else if (liveInst->opcode() == SpvOpImageTexelPointer) {
uint32_t varId;
(void)GetPtr(liveInst, &varId);
if (varId != 0) {
ProcessLoad(varId);
}
}
worklist_.pop();
}

Expand Down
4 changes: 2 additions & 2 deletions source/opt/instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ bool Instruction::IsReadOnlyLoad() const {

Instruction* Instruction::GetBaseAddress() const {
assert((IsLoad() || opcode() == SpvOpStore || opcode() == SpvOpAccessChain ||
opcode() == SpvOpInBoundsAccessChain ||
opcode() == SpvOpCopyObject) &&
opcode() == SpvOpInBoundsAccessChain || opcode() == SpvOpCopyObject ||
opcode() == SpvOpImageTexelPointer) &&
"GetBaseAddress should only be called on instructions that take a "
"pointer or image.");
uint32_t base = GetSingleWordInOperand(kLoadBaseIndex);
Expand Down
9 changes: 4 additions & 5 deletions source/opt/mem_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ namespace opt {
namespace {

const uint32_t kCopyObjectOperandInIdx = 0;
const uint32_t kLoadPtrIdInIdx = 0;
const uint32_t kLoopMergeMergeBlockIdInIdx = 0;
const uint32_t kStorePtrIdInIdx = 0;
const uint32_t kStoreValIdInIdx = 1;
const uint32_t kTypePointerStorageClassInIdx = 0;
const uint32_t kTypePointerTypeIdInIdx = 1;
Expand Down Expand Up @@ -120,9 +118,10 @@ ir::Instruction* MemPass::GetPtr(uint32_t ptrId, uint32_t* varId) {

ir::Instruction* MemPass::GetPtr(ir::Instruction* ip, uint32_t* varId) {
const SpvOp op = ip->opcode();
assert(op == SpvOpStore || op == SpvOpLoad);
const uint32_t ptrId = ip->GetSingleWordInOperand(
op == SpvOpStore ? kStorePtrIdInIdx : kLoadPtrIdInIdx);
assert(op == SpvOpStore || op == SpvOpLoad || op == SpvOpImageTexelPointer);

// All of these opcode place the pointer in position 0.
const uint32_t ptrId = ip->GetSingleWordInOperand(0);
return GetPtr(ptrId, varId);
}

Expand Down
41 changes: 41 additions & 0 deletions test/opt/aggressive_dead_code_elim_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5369,6 +5369,47 @@ OpFunctionEnd
SinglePassRunAndCheck<opt::AggressiveDCEPass>(text, text, true, true);
}

TEST_F(AggressiveDCETest, AtomicAdd) {
const std::string text = R"(OpCapability SampledBuffer
OpCapability StorageImageExtendedFormats
OpCapability ImageBuffer
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %2 "min" %gl_GlobalInvocationID
OpExecutionMode %2 LocalSize 64 1 1
OpSource HLSL 600
OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
OpDecorate %4 DescriptorSet 4
OpDecorate %4 Binding 70
%uint = OpTypeInt 32 0
%6 = OpTypeImage %uint Buffer 0 0 0 2 R32ui
%_ptr_UniformConstant_6 = OpTypePointer UniformConstant %6
%_ptr_Private_6 = OpTypePointer Private %6
%void = OpTypeVoid
%10 = OpTypeFunction %void
%uint_0 = OpConstant %uint 0
%uint_1 = OpConstant %uint 1
%v3uint = OpTypeVector %uint 3
%_ptr_Input_v3uint = OpTypePointer Input %v3uint
%_ptr_Image_uint = OpTypePointer Image %uint
%4 = OpVariable %_ptr_UniformConstant_6 UniformConstant
%16 = OpVariable %_ptr_Private_6 Private
%gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
%2 = OpFunction %void None %10
%17 = OpLabel
%18 = OpLoad %6 %4
OpStore %16 %18
%19 = OpImageTexelPointer %_ptr_Image_uint %16 %uint_0 %uint_0
%20 = OpAtomicIAdd %uint %19 %uint_1 %uint_0 %uint_1
OpReturn
OpFunctionEnd
)";

SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
SinglePassRunAndCheck<opt::AggressiveDCEPass>(text, text, true, true);
}

// TODO(greg-lunarg): Add tests to verify handling of these cases:
//
// Check that logical addressing required
Expand Down

0 comments on commit 1fccc6c

Please sign in to comment.