{"record":{"id":"145ecaf266d3aa8f","repo":"stride3d/stride","slug":"cannot-find-constant-instruction-for-id","errorCode":null,"errorMessage":"Cannot find constant instruction for id ","messagePattern":"Cannot find constant instruction for id ","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Context.Constants.cs","lineNumber":42,"sourceCode":"            long v => Buffer.AddData(new OpConstant<long>(GetOrRegister(ScalarType.Int64), Bound++, v)),\n            Half v => Buffer.AddData(new OpConstant<Half>(GetOrRegister(ScalarType.Half), Bound++, v)),\n            float v => Buffer.AddData(new OpConstant<float>(GetOrRegister(ScalarType.Float), Bound++, v)),\n            double v => Buffer.AddData(new OpConstant<double>(GetOrRegister(ScalarType.Double), Bound++, v)),\n            _ => throw new NotImplementedException()\n        };\n        if (InstructionInfo.GetInfo(data).GetResultIndex(out var index))\n            return data.Memory.Span[index + 1];\n        throw new Exception(\"Constant has no result id\");\n    }\n\n    public object GetConstantValue(int constantId)\n    {\n        if (Buffer.TryGetInstructionById(constantId, out var constant))\n        {\n            return ResolveConstantValue(constant);\n        }\n\n        throw new Exception(\"Cannot find constant instruction for id \" + constantId);\n    }\n\n    public bool TryGetConstantValue(int constantId, [MaybeNullWhen(false)] out object value, out int typeId)\n    {\n        if (Buffer.TryGetInstructionById(constantId, out var constant))\n        {\n            return TryGetConstantValue(constant, out value, out typeId);\n        }\n\n        typeId = 0;\n        value = null;\n        return false;\n    }\n\n    public object ResolveConstantValue(OpDataIndex i)\n    {\n        if (!TryGetConstantValue(i, out var value, out _))\n            throw new InvalidOperationException($\"Can't process constant {i.Data.IdResult}\");","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Context.Constants.cs#L24-L60","documentation":"GetConstantValue maps a SPIR-V result id to its defining OpConstant instruction via Buffer.TryGetConstantValue and evaluates it. When no instruction with that id exists in the buffer, the id is stale, was never registered, or belongs to an instruction kind that is not a constant (e.g. an OpSpecConstant or a type id passed by mistake), and the method throws 'Cannot find constant instruction for id N'.","triggerScenarios":"Passing a result id to GetConstantValue that (a) was never emitted by AddConstant, (b) refers to a non-constant instruction (type, variable, function), or (c) was looked up in the wrong Buffer/context instance.","commonSituations":"Processing SPIR-V produced by an external compiler where specialization constants (OpSpecConstant) or composite constants are referenced during import/composition (callers include ProcessImportInfo, GetGenericsArguments, compositionIndex); analyzing a second module with ids from a first.","solutions":["Use TryGetConstantValue(int, out object, out int) instead to handle the missing-id case gracefully.","Verify the id actually denotes an OpConstant (not OpSpecConstant or a type) before calling.","Ensure you are querying the same Context/Buffer that contains the defining instruction.","Pre-resolve specialization constants to concrete values before constant folding."],"exampleFix":"// before\nvar v = context.GetConstantValue(specConstantId); // throws\n// after\nif (context.TryGetConstantValue(id, out var v, out var typeId)) { /* use v */ }","handlingStrategy":"fallback","validationCode":"if (buffer.TryGetInstructionById(id, out var instr) && instr.opcode == Specification.Op.OpConstant) { /* safe to call GetConstantValue */ }","typeGuard":"bool IsConstantId(Context ctx, int id) => ctx.Buffer.TryGetInstructionById(id, out var i) && InstructionInfo.GetInfo(i).Op == Specification.Op.OpConstant;","tryCatchPattern":"try { value = context.GetConstantValue(id); } catch (Exception ex) when (ex.Message.StartsWith(\"Cannot find constant instruction for id\")) { value = null; /* treat as non-constant or defer */ }","preventionTips":["Prefer TryGetConstantValue over GetConstantValue at call sites where absence is possible","Never pass type or variable ids to constant lookups","Resolve specialization constants before folding","Use a single Context for lookups and emission"],"tags":["spirv","constant","lookup","not-found"],"backgroundTag":"record-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"}