{"record":{"id":"320ea089988464cc","repo":"stride3d/stride","slug":"cannot-find-instruction-for-id-constantid","errorCode":null,"errorMessage":"Cannot find instruction for id {constantId}","messagePattern":"Cannot find instruction for id (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Parsers/Core/ConstantExpression.cs","lineNumber":79,"sourceCode":"        int i => new IntConstExpr(i),\n        uint u => new IntConstExpr(u),\n        long l => new IntConstExpr(l),\n        ulong u => new IntConstExpr((long)u),\n        float f => new FloatConstExpr(f),\n        double d => new FloatConstExpr(d),\n        bool b => new BoolConstExpr(b),\n        string s => new StringConstExpr(s),\n        _ => throw new NotSupportedException($\"Unsupported constant type: {value.GetType()}\")\n    };\n\n    /// <summary>\n    /// Parse a SPIR-V constant ID into a ConstantExpression tree.\n    /// Replaces ExtractConstantFromBuffer for array sizes and generic arguments.\n    /// </summary>\n    public static ConstantExpression ParseFromBuffer(int constantId, SpirvBuffer buffer, SpirvContext context)\n    {\n        if (!buffer.TryGetInstructionById(constantId, out var inst))\n            throw new InvalidOperationException($\"Cannot find instruction for id {constantId}\");\n\n        return ParseInstruction(inst, buffer, context);\n    }\n\n    private static ConstantExpression ParseInstruction(OpDataIndex inst, SpirvBuffer buffer, SpirvContext context)\n    {\n        switch (inst.Op)\n        {\n            case Op.OpConstantTrue:\n                return new BoolConstExpr(true);\n\n            case Op.OpConstantFalse:\n                return new BoolConstExpr(false);\n\n            case Op.OpConstant:\n            case Op.OpSpecConstant:\n            {\n                var typeId = inst.Data.Memory.Span[1];","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Core/ConstantExpression.cs#L61-L97","documentation":"ConstantExpression.ParseFromBuffer throws InvalidOperationException when the given constantId has no corresponding instruction in the provided SpirvBuffer. Parsing a SPIR-V constant into a ConstantExpression tree requires the defining instruction.","triggerScenarios":"Calling ParseFromBuffer with an ID absent from the buffer — wrong module/buffer passed, ID referencing an instruction from another module, or truncated/corrupt SPIR-V.","commonSituations":"Cross-module constant references (array sizes, generic args) where the defining instruction lives in a different buffer; parsing debug/stripped SPIR-V binaries missing debug-relevant definitions; ID offset mismatches.","solutions":["Pass the SpirvBuffer that actually contains the constant's defining instruction.","Verify constantId is valid (exists in the module's ID range) and the buffer isn't truncated.","If the constant comes from another module, resolve/merge the buffer first instead of parsing directly."],"exampleFix":"// before\nvar expr = ConstantExpression.ParseFromBuffer(id, wrongBuffer, context);\n// after\nif (!buffer.TryGetInstructionById(id, out _))\n    throw new InvalidOperationException($\"Buffer does not contain id {id}\");\nvar expr = ConstantExpression.ParseFromBuffer(id, buffer, context);","handlingStrategy":"validation","validationCode":"if (!buffer.TryGetInstructionById(constantId, out _))\n    throw new InvalidOperationException($\"Constant {constantId} not present in this buffer\");\nvar expr = ConstantExpression.ParseFromBuffer(constantId, buffer, context);","typeGuard":null,"tryCatchPattern":"try { expr = ConstantExpression.ParseFromBuffer(id, buffer, ctx); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Cannot find instruction\")) { /* resolve from the owning module instead */ }","preventionTips":["Always pass the buffer that defines the constant","Check for cross-module ID references before parsing","Validate SPIR-V modules are complete and not truncated"],"tags":["spirv","constant","not-found"],"backgroundTag":"resource-not-found","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}