Skip to content

check annotated operand types in generated builders - #847

Open
JonhasA wants to merge 6 commits into
rustfrom
jc/rs-ssa-val-check
Open

JonhasA wants to merge 6 commits into
rustfrom
jc/rs-ssa-val-check

Conversation

@JonhasA

@JonhasA JonhasA commented Nov 10, 2026 •

Copy link
Copy Markdown

I was doing something like the following:

#[derive(Clone, Debug, PartialEq, Dialect, HasParser, PrettyPrint)]
#[kirin(builders, type = TypeSys::<CodeType>)]
#[chumsky(format = "$TransversalH(code_block={code_block}):{block_type} -> {result:type}")]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TransversalH {
    pub block_type: CodeType,
    #[kirin(type=TypeSys::Scalar(block_type))]
    pub code_block: SSAValue,

    #[kirin(type=TypeSys::Scalar(block_type))]
    pub result: ResultValue,
}

and I noticed that for the argument operands, in this case,

#[kirin(type=TypeSys::Scalar(block_type))]
pub code_block: SSAValue, 

did not have any type verification being done, compared to pub result: ResultValue, which did.

We had that #[kirin(type = ...)] was accepted on SSAValue operand fields but ignored. This is because the expr was stored and never emitted. This patch enforces that generated builders now enforce it, ie., we panic if an annotated operand's type is known and differs from the annotation. Unannotated operands and operands whose type is not yet set (such as graph-port placeholders) are skipped.

Note that this only applies to generated stage builders. Parsed IR and direct statement construction still require a separate verification pass.

@Roger-luo

Copy link
Copy Markdown
Collaborator

How does this interact with type inference? e.g., when you mark certain type as unknown and waiting for inference?

@JonhasA

JonhasA commented Nov 10, 2026

Copy link
Copy Markdown
Author

How does this interact with type inference? e.g., when you mark certain type as unknown and waiting for inference?

So going over it now, it does not handle it well. When testing that against this patch, if a value's type is unknown and waiting for the type to be inferred, the verification step panics. Currently, if the type is just None, the verification step just skips it.

When the value's type is unknown and its waiting to be inferred, its stored as Result<T::placeholder>. The simplest solution to this without making any additional changes is to also skip when the operand’s type is the placeholder.

…ers, added Typeresolution for stateents with annotated single operants.
@zhenrongliew

zhenrongliew commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

I think for this PR we should keep only the first commit (the distinction between annotated and unannotated operands via ssa_type: Option<Expr>), and move the type check out of the builder. That way other phases (rewriter) could run it at their pass boundaries.

I feel like type checking at build-phase while many values are still placeholder can’t decide much, and it would duplicate what a full pass has to do anyway.

Nevermind, moving it into a shared verifier could be future work.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants