diff --git a/source/opt/aggressive_dead_code_elim_pass.cpp b/source/opt/aggressive_dead_code_elim_pass.cpp index 4b304fa69..e43ba1b1d 100644 --- a/source/opt/aggressive_dead_code_elim_pass.cpp +++ b/source/opt/aggressive_dead_code_elim_pass.cpp @@ -350,7 +350,7 @@ bool AggressiveDCEPass::AggressiveDCE(ir::Function* func) { default: { // Function calls, atomics, function params, function returns, etc. // TODO(greg-lunarg): function calls live only if write to non-local - if (!context()->IsCombinatorInstruction(&*ii)) { + if (!ii->IsOpcodeSafeToDelete()) { AddToWorklist(&*ii); } // Remember function calls diff --git a/source/opt/instruction.cpp b/source/opt/instruction.cpp index 44a69f363..96c782791 100644 --- a/source/opt/instruction.cpp +++ b/source/opt/instruction.cpp @@ -701,5 +701,27 @@ bool Instruction::IsScalarizable() const { return false; } +bool Instruction::IsOpcodeSafeToDelete() const { + if (context()->IsCombinatorInstruction(this)) { + return true; + } + + switch (opcode()) { + case SpvOpDPdx: + case SpvOpDPdy: + case SpvOpFwidth: + case SpvOpDPdxFine: + case SpvOpDPdyFine: + case SpvOpFwidthFine: + case SpvOpDPdxCoarse: + case SpvOpDPdyCoarse: + case SpvOpFwidthCoarse: + case SpvOpImageQueryLod: + return true; + default: + return false; + } +} + } // namespace ir } // namespace spvtools diff --git a/source/opt/instruction.h b/source/opt/instruction.h index 91a6ed7bd..0a425526d 100644 --- a/source/opt/instruction.h +++ b/source/opt/instruction.h @@ -406,8 +406,13 @@ class Instruction : public utils::IntrusiveNodeBase { // is always added to |options|. std::string PrettyPrint(uint32_t options = 0u) const; + // Returns true if the result can be a vector and the result of each component + // depends on the corresponding component of any vector inputs. bool IsScalarizable() const; + // Return true if the only effect of this instructions is the result. + bool IsOpcodeSafeToDelete() const; + private: // Returns the total count of result type id and result id. uint32_t TypeResultIdCount() const { diff --git a/source/opt/ir_context.cpp b/source/opt/ir_context.cpp index 5af4614e6..338a14182 100644 --- a/source/opt/ir_context.cpp +++ b/source/opt/ir_context.cpp @@ -281,161 +281,164 @@ void IRContext::KillNamesAndDecorates(Instruction* inst) { void IRContext::AddCombinatorsForCapability(uint32_t capability) { if (capability == SpvCapabilityShader) { - combinator_ops_[0].insert({ - SpvOpNop, - SpvOpUndef, - SpvOpConstant, - SpvOpConstantTrue, - SpvOpConstantFalse, - SpvOpConstantComposite, - SpvOpConstantSampler, - SpvOpConstantNull, - SpvOpTypeVoid, - SpvOpTypeBool, - SpvOpTypeInt, - SpvOpTypeFloat, - SpvOpTypeVector, - SpvOpTypeMatrix, - SpvOpTypeImage, - SpvOpTypeSampler, - SpvOpTypeSampledImage, - SpvOpTypeArray, - SpvOpTypeRuntimeArray, - SpvOpTypeStruct, - SpvOpTypeOpaque, - SpvOpTypePointer, - SpvOpTypeFunction, - SpvOpTypeEvent, - SpvOpTypeDeviceEvent, - SpvOpTypeReserveId, - SpvOpTypeQueue, - SpvOpTypePipe, - SpvOpTypeForwardPointer, - SpvOpVariable, - SpvOpImageTexelPointer, - SpvOpLoad, - SpvOpAccessChain, - SpvOpInBoundsAccessChain, - SpvOpArrayLength, - SpvOpVectorExtractDynamic, - SpvOpVectorInsertDynamic, - SpvOpVectorShuffle, - SpvOpCompositeConstruct, - SpvOpCompositeExtract, - SpvOpCompositeInsert, - SpvOpCopyObject, - SpvOpTranspose, - SpvOpSampledImage, - SpvOpImageSampleImplicitLod, - SpvOpImageSampleExplicitLod, - SpvOpImageSampleDrefImplicitLod, - SpvOpImageSampleDrefExplicitLod, - SpvOpImageSampleProjImplicitLod, - SpvOpImageSampleProjExplicitLod, - SpvOpImageSampleProjDrefImplicitLod, - SpvOpImageSampleProjDrefExplicitLod, - SpvOpImageFetch, - SpvOpImageGather, - SpvOpImageDrefGather, - SpvOpImageRead, - SpvOpImage, - SpvOpConvertFToU, - SpvOpConvertFToS, - SpvOpConvertSToF, - SpvOpConvertUToF, - SpvOpUConvert, - SpvOpSConvert, - SpvOpFConvert, - SpvOpQuantizeToF16, - SpvOpBitcast, - SpvOpSNegate, - SpvOpFNegate, - SpvOpIAdd, - SpvOpFAdd, - SpvOpISub, - SpvOpFSub, - SpvOpIMul, - SpvOpFMul, - SpvOpUDiv, - SpvOpSDiv, - SpvOpFDiv, - SpvOpUMod, - SpvOpSRem, - SpvOpSMod, - SpvOpFRem, - SpvOpFMod, - SpvOpVectorTimesScalar, - SpvOpMatrixTimesScalar, - SpvOpVectorTimesMatrix, - SpvOpMatrixTimesVector, - SpvOpMatrixTimesMatrix, - SpvOpOuterProduct, - SpvOpDot, - SpvOpIAddCarry, - SpvOpISubBorrow, - SpvOpUMulExtended, - SpvOpSMulExtended, - SpvOpAny, - SpvOpAll, - SpvOpIsNan, - SpvOpIsInf, - SpvOpLogicalEqual, - SpvOpLogicalNotEqual, - SpvOpLogicalOr, - SpvOpLogicalAnd, - SpvOpLogicalNot, - SpvOpSelect, - SpvOpIEqual, - SpvOpINotEqual, - SpvOpUGreaterThan, - SpvOpSGreaterThan, - SpvOpUGreaterThanEqual, - SpvOpSGreaterThanEqual, - SpvOpULessThan, - SpvOpSLessThan, - SpvOpULessThanEqual, - SpvOpSLessThanEqual, - SpvOpFOrdEqual, - SpvOpFUnordEqual, - SpvOpFOrdNotEqual, - SpvOpFUnordNotEqual, - SpvOpFOrdLessThan, - SpvOpFUnordLessThan, - SpvOpFOrdGreaterThan, - SpvOpFUnordGreaterThan, - SpvOpFOrdLessThanEqual, - SpvOpFUnordLessThanEqual, - SpvOpFOrdGreaterThanEqual, - SpvOpFUnordGreaterThanEqual, - SpvOpShiftRightLogical, - SpvOpShiftRightArithmetic, - SpvOpShiftLeftLogical, - SpvOpBitwiseOr, - SpvOpBitwiseXor, - SpvOpBitwiseAnd, - SpvOpNot, - SpvOpBitFieldInsert, - SpvOpBitFieldSExtract, - SpvOpBitFieldUExtract, - SpvOpBitReverse, - SpvOpBitCount, - SpvOpPhi, - SpvOpImageSparseSampleImplicitLod, - SpvOpImageSparseSampleExplicitLod, - SpvOpImageSparseSampleDrefImplicitLod, - SpvOpImageSparseSampleDrefExplicitLod, - SpvOpImageSparseSampleProjImplicitLod, - SpvOpImageSparseSampleProjExplicitLod, - SpvOpImageSparseSampleProjDrefImplicitLod, - SpvOpImageSparseSampleProjDrefExplicitLod, - SpvOpImageSparseFetch, - SpvOpImageSparseGather, - SpvOpImageSparseDrefGather, - SpvOpImageSparseTexelsResident, - SpvOpImageSparseRead, - SpvOpSizeOf - // TODO(dneto): Add instructions enabled by ImageQuery - }); + combinator_ops_[0].insert({SpvOpNop, + SpvOpUndef, + SpvOpConstant, + SpvOpConstantTrue, + SpvOpConstantFalse, + SpvOpConstantComposite, + SpvOpConstantSampler, + SpvOpConstantNull, + SpvOpTypeVoid, + SpvOpTypeBool, + SpvOpTypeInt, + SpvOpTypeFloat, + SpvOpTypeVector, + SpvOpTypeMatrix, + SpvOpTypeImage, + SpvOpTypeSampler, + SpvOpTypeSampledImage, + SpvOpTypeArray, + SpvOpTypeRuntimeArray, + SpvOpTypeStruct, + SpvOpTypeOpaque, + SpvOpTypePointer, + SpvOpTypeFunction, + SpvOpTypeEvent, + SpvOpTypeDeviceEvent, + SpvOpTypeReserveId, + SpvOpTypeQueue, + SpvOpTypePipe, + SpvOpTypeForwardPointer, + SpvOpVariable, + SpvOpImageTexelPointer, + SpvOpLoad, + SpvOpAccessChain, + SpvOpInBoundsAccessChain, + SpvOpArrayLength, + SpvOpVectorExtractDynamic, + SpvOpVectorInsertDynamic, + SpvOpVectorShuffle, + SpvOpCompositeConstruct, + SpvOpCompositeExtract, + SpvOpCompositeInsert, + SpvOpCopyObject, + SpvOpTranspose, + SpvOpSampledImage, + SpvOpImageSampleImplicitLod, + SpvOpImageSampleExplicitLod, + SpvOpImageSampleDrefImplicitLod, + SpvOpImageSampleDrefExplicitLod, + SpvOpImageSampleProjImplicitLod, + SpvOpImageSampleProjExplicitLod, + SpvOpImageSampleProjDrefImplicitLod, + SpvOpImageSampleProjDrefExplicitLod, + SpvOpImageFetch, + SpvOpImageGather, + SpvOpImageDrefGather, + SpvOpImageRead, + SpvOpImage, + SpvOpImageQueryFormat, + SpvOpImageQueryOrder, + SpvOpImageQuerySizeLod, + SpvOpImageQuerySize, + SpvOpImageQueryLevels, + SpvOpImageQuerySamples, + SpvOpConvertFToU, + SpvOpConvertFToS, + SpvOpConvertSToF, + SpvOpConvertUToF, + SpvOpUConvert, + SpvOpSConvert, + SpvOpFConvert, + SpvOpQuantizeToF16, + SpvOpBitcast, + SpvOpSNegate, + SpvOpFNegate, + SpvOpIAdd, + SpvOpFAdd, + SpvOpISub, + SpvOpFSub, + SpvOpIMul, + SpvOpFMul, + SpvOpUDiv, + SpvOpSDiv, + SpvOpFDiv, + SpvOpUMod, + SpvOpSRem, + SpvOpSMod, + SpvOpFRem, + SpvOpFMod, + SpvOpVectorTimesScalar, + SpvOpMatrixTimesScalar, + SpvOpVectorTimesMatrix, + SpvOpMatrixTimesVector, + SpvOpMatrixTimesMatrix, + SpvOpOuterProduct, + SpvOpDot, + SpvOpIAddCarry, + SpvOpISubBorrow, + SpvOpUMulExtended, + SpvOpSMulExtended, + SpvOpAny, + SpvOpAll, + SpvOpIsNan, + SpvOpIsInf, + SpvOpLogicalEqual, + SpvOpLogicalNotEqual, + SpvOpLogicalOr, + SpvOpLogicalAnd, + SpvOpLogicalNot, + SpvOpSelect, + SpvOpIEqual, + SpvOpINotEqual, + SpvOpUGreaterThan, + SpvOpSGreaterThan, + SpvOpUGreaterThanEqual, + SpvOpSGreaterThanEqual, + SpvOpULessThan, + SpvOpSLessThan, + SpvOpULessThanEqual, + SpvOpSLessThanEqual, + SpvOpFOrdEqual, + SpvOpFUnordEqual, + SpvOpFOrdNotEqual, + SpvOpFUnordNotEqual, + SpvOpFOrdLessThan, + SpvOpFUnordLessThan, + SpvOpFOrdGreaterThan, + SpvOpFUnordGreaterThan, + SpvOpFOrdLessThanEqual, + SpvOpFUnordLessThanEqual, + SpvOpFOrdGreaterThanEqual, + SpvOpFUnordGreaterThanEqual, + SpvOpShiftRightLogical, + SpvOpShiftRightArithmetic, + SpvOpShiftLeftLogical, + SpvOpBitwiseOr, + SpvOpBitwiseXor, + SpvOpBitwiseAnd, + SpvOpNot, + SpvOpBitFieldInsert, + SpvOpBitFieldSExtract, + SpvOpBitFieldUExtract, + SpvOpBitReverse, + SpvOpBitCount, + SpvOpPhi, + SpvOpImageSparseSampleImplicitLod, + SpvOpImageSparseSampleExplicitLod, + SpvOpImageSparseSampleDrefImplicitLod, + SpvOpImageSparseSampleDrefExplicitLod, + SpvOpImageSparseSampleProjImplicitLod, + SpvOpImageSparseSampleProjExplicitLod, + SpvOpImageSparseSampleProjDrefImplicitLod, + SpvOpImageSparseSampleProjDrefExplicitLod, + SpvOpImageSparseFetch, + SpvOpImageSparseGather, + SpvOpImageSparseDrefGather, + SpvOpImageSparseTexelsResident, + SpvOpImageSparseRead, + SpvOpSizeOf}); } }