Skip to content

spirv-opt: An off-by-1 error in constant folding #3631

@Vasniktel

Description

@Vasniktel

Consider the following SPIR-V

               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint Fragment %4 "main"
               OpExecutionMode %4 OriginUpperLeft
               OpSource ESSL 310
          %2 = OpTypeVoid
          %3 = OpTypeFunction %2
          %6 = OpTypeFloat 32
          %7 = OpTypeVector %6 3
          %8 = OpConstant %6 0
          %9 = OpConstantComposite %7 %8 %8 %8
         %10 = OpTypeVector %6 4
          %4 = OpFunction %2 None %3
          %5 = OpLabel
         %11 = OpCompositeConstruct %10 %9 %8
         %12 = OpCompositeExtract %6 %11 3
               OpReturn
               OpFunctionEnd

An attempt to replace the use of %11 in %12 fails because of the off-by-1 error in

uint32_t vector_size = element_type->element_count();
if (vector_size < element_index) {
// The element we want comes after this vector.
element_index -= vector_size;
} else {
// We want an element of this vector.
operands.push_back({SPV_OPERAND_TYPE_ID, {element_id}});
operands.push_back(
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {element_index}});
break;
}

Can be reproduced on b78f4b1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions