Skip to content

isle struct support#13319

Draft
Firestar99 wants to merge 3 commits intobytecodealliance:mainfrom
Firestar99:isle_struct
Draft

isle struct support#13319
Firestar99 wants to merge 3 commits intobytecodealliance:mainfrom
Firestar99:isle_struct

Conversation

@Firestar99
Copy link
Copy Markdown

@Firestar99 Firestar99 commented May 7, 2026

I have a code generator that creates rust code similar to this:

pub struct IAdd {
    pub out: WriteableReg,
    pub a: Reg,
    pub b: Reg,
}

pub struct IConst {
    pub out: WriteableReg,
    pub imm: Imm,
}

pub enum MInst {
    IAdd(IAdd),
    IConst(IConst),
}

I would like to modify my code generator to also emit isle that matches this particular style of codegen. But isle only supports enums, so this PR adds support for structs:

(type IAdd extern (struct
     (out WriteableReg) 
     (a Reg) 
     (b Reg)))
(type IConst extern (struct 
    (out WriteableReg) 
    (imm Imm)))
(type MInst extern (enum 
    (IAdd (x IAdd))  
    (IConst (x IConst))))

This is not quite sufficient to properly map it this example, as isle doesn't support tuple-like enum variants. But that can be a follow up PR some other day.

Minor changes

I've changed the temp directory for isle link and run tests to ./target/tmp/<test-name>/ (via CARGO_TARGET_TMPDIR env). Using system tmp and deleting it regardless of test failure makes it hard to debug broken rust code emissions.

Comment thread cranelift/isle/isle/src/trie_again.rs Outdated
Comment on lines +588 to +597
match variant {
None => Constraint::Struct {
fields,
ty: input_ty,
},
Some(variant) => Constraint::Variant {
fields,
ty: input_ty,
variant,
},
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While in the front end (ast and sema) I added a separate struct variant everywhere, when it proceeds to adding rules and match statements, I start reusing enum's variants by making the VariantId optional everywhere. This is the only place where anything actually matches on it, which allows my code changes to be quite minimal in this area.

Copy link
Copy Markdown
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is a nice extension of ISLE and will certainly make it more useful in binding to other environments.

A few thoughts on the data-structure design below. Overall shape (reusing enum's code as much as possible) seems right, though.

Also cc @mmcloughlin / @avanhatt as they have some (not-yet-upstreamed) additions to ISLE and will probably want to be aware of this.

Comment thread cranelift/isle/isle/src/sema.rs Outdated
Comment thread cranelift/isle/isle/src/sema.rs Outdated
Comment thread cranelift/isle/isle/src/trie_again.rs Outdated
Comment thread cranelift/isle/isle/src/trie_again.rs
let tempdir = tempfile::tempdir().unwrap();
let code = build(isle_filename).unwrap();

fn build_and_link_isle(isle_filename: &str) -> (PathBuf, PathBuf) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes here seem unrelated to struct support?

Copy link
Copy Markdown
Author

@Firestar99 Firestar99 May 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to describe this part in the PR description:

I've changed the temp directory for isle link and run tests to ./target/tmp/<test-name>/ (via CARGO_TARGET_TMPDIR env). Using system tmp and deleting it regardless of test failure makes it hard to debug broken rust code emissions.

Also squashed in a minor fixup into that commit, removing the tempdir dep from [dev-dependencies], that is if you want to keep this change.

@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language labels May 7, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2026

Subscribe to Label Action

cc @cfallin, @fitzgen

Details This issue or pull request has been labeled: "cranelift", "isle"

Thus the following users have been cc'd because of the following labels:

  • cfallin: isle
  • fitzgen: isle

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

Firestar99 added 2 commits May 8, 2026 11:23
Using system tmp and deleting it even if the test failed makes it hard to debug broken rust code emissions
@Firestar99
Copy link
Copy Markdown
Author

Next iteration: propagate struct through sema's terms instead of reusing enums with no variant. This was surprisingly easy to do and found it to be cleaner than special-casing any enum variants already there.

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

Labels

cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants