stride3d/stride · error · Exception

Cannot find type instruction for id

Error message

Cannot find type instruction for id 

What it means

TryGetConstantValue looks up the type instruction for a constant's Result Type id via Buffer.TryGetInstructionById. If no instruction with that id exists in the buffer, it throws a plain Exception 'Cannot find type instruction for id <typeId>'. This indicates a broken or partially-loaded SPIR-V module where a type definition is missing.

Solutions

  1. Verify the SPIR-V binary is complete and valid (run spirv-val on the file); re-export or recompile the shader if corrupted
  2. Ensure all type-definition instructions are registered in the Buffer before constants are evaluated (check parsing order in the SPIR-V loader)
  3. Dump typeId at the throw site and confirm the module actually contains an instruction with that result id — if not, the binary or loader is at fault

Example fix

// before
throw new Exception("Cannot find type instruction for id " + typeId);
// after
if (!Buffer.TryGetInstructionById(typeId, out var typeInst))
    throw new InvalidDataException($"SPIR-V module is missing type instruction for id {typeId}; file may be truncated or malformed");
Defensive patterns

Strategy: validation

Validate before calling

// verify the type id referenced by a constant exists before evaluation
bool TypeInstructionExists(SpirvBuffer buffer, Instruction constant)
    => buffer.TryGetInstructionById(constant.Data.Memory.Span[1], out _);

Try / catch

try
{
    context.TryGetConstantValue(id, out var value, out var typeId);
}
catch (Exception ex) when (ex.Message.StartsWith("Cannot find type instruction for id "))
{
    // module is truncated/malformed: report and fall back to a re-imported valid module
    ReportCorruptModule(ex.Message);
}

Prevention

When it happens

Trigger: Parsing a truncated or malformed SPIR-V binary where OpTypeInt/OpTypeFloat defining the referenced type id was never added to the buffer; a parser ordering bug where constants are evaluated before their type instructions are registered; an invalid type id in the constant instruction.

Common situations: Corrupted shader files (truncated .spv); custom module loaders that skip or reorder type-definition sections; manually assembled SPIR-V with dangling type references; version mismatches between the producer and this parser.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/bb3d9f23820af737. Report an issue: GitHub.

Appendix: source

Thrown at sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Context.Constants.cs:243

                return true;
            }
            else if (typeInst.Op == Specification.Op.OpTypeFloat)
            {
                var type = new OpTypeFloat(typeInst);
                value = type switch
                {
                    { Width: 16 } => operand.ToLiteral<Half>(),
                    { Width: 32 } => operand.ToLiteral<float>(),
                    { Width: 64 } => operand.ToLiteral<double>(),
                    _ => throw new NotImplementedException($"Unsupported float width {type.Width}"),
                };
                return true;
            }
            else
                throw new NotImplementedException($"Unsupported context dependent number with type {typeInst.Op}");
        }

        throw new Exception("Cannot find type instruction for id " + typeId);
    }

    public SpirvValue CreateDefaultConstantComposite(SymbolType type)
    {
        // TODO: cache results (either here or even more generally for any composite constant even if non-zero)
        return new(Buffer.AddData(new OpConstantNull(GetOrRegister(type), Bound++)));
    }

    public SpirvValue CreateConstantCompositeVectorRepeat(Literal literal, int size)
    {
        var value = CompileConstantLiteral(literal);
        if (size == 1)
            return value;

        var type = new VectorType((ScalarType)ReverseTypes[value.TypeId], size);
        return CreateConstantCompositeRepeat(type, value, size);
    }

View on GitHub (pinned to 96fad776d2)