stride3d/stride · error · Exception
Cannot find constant instruction for id
Error message
Cannot find constant instruction for id
What it means
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'.
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.
Example fix
// before
var v = context.GetConstantValue(specConstantId); // throws
// after
if (context.TryGetConstantValue(id, out var v, out var typeId)) { /* use v */ } Defensive patterns
Strategy: fallback
Validate before calling
if (buffer.TryGetInstructionById(id, out var instr) && instr.opcode == Specification.Op.OpConstant) { /* safe to call GetConstantValue */ } Type guard
bool IsConstantId(Context ctx, int id) => ctx.Buffer.TryGetInstructionById(id, out var i) && InstructionInfo.GetInfo(i).Op == Specification.Op.OpConstant;
Try / catch
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 */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.
Related errors
- Cannot find instruction for id
- Could not load shader
- Constant has no result id
- Can't process constant
- No upgrader found for version
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/145ecaf266d3aa8f.
Report an issue: GitHub.
Appendix: source
Thrown at sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Context.Constants.cs:42
long v => Buffer.AddData(new OpConstant<long>(GetOrRegister(ScalarType.Int64), Bound++, v)),
Half v => Buffer.AddData(new OpConstant<Half>(GetOrRegister(ScalarType.Half), Bound++, v)),
float v => Buffer.AddData(new OpConstant<float>(GetOrRegister(ScalarType.Float), Bound++, v)),
double v => Buffer.AddData(new OpConstant<double>(GetOrRegister(ScalarType.Double), Bound++, v)),
_ => throw new NotImplementedException()
};
if (InstructionInfo.GetInfo(data).GetResultIndex(out var index))
return data.Memory.Span[index + 1];
throw new Exception("Constant has no result id");
}
public object GetConstantValue(int constantId)
{
if (Buffer.TryGetInstructionById(constantId, out var constant))
{
return ResolveConstantValue(constant);
}
throw new Exception("Cannot find constant instruction for id " + constantId);
}
public bool TryGetConstantValue(int constantId, [MaybeNullWhen(false)] out object value, out int typeId)
{
if (Buffer.TryGetInstructionById(constantId, out var constant))
{
return TryGetConstantValue(constant, out value, out typeId);
}
typeId = 0;
value = null;
return false;
}
public object ResolveConstantValue(OpDataIndex i)
{
if (!TryGetConstantValue(i, out var value, out _))
throw new InvalidOperationException($"Can't process constant {i.Data.IdResult}");View on GitHub (pinned to 96fad776d2)