Skip to content

[syntax] Support for metadata over iteration and dispatch statements. #4684

@modulovalue

Description

@modulovalue

There's no way to attach metadata to statements that have multiple valid compilation outputs. Consider:

forStatement
    :    AWAIT? FOR '(' forLoopParts ')' statement
    ;
whileStatement
    :    WHILE '(' expression ')' statement
    ;

doStatement
    :    DO statement WHILE '(' expression ')' ';'
    ;
switchStatement
    :    SWITCH '(' expression ')'
         LBRACE switchStatementCase* switchStatementDefault? RBRACE
    ;

This makes it rather difficult to run VM experiments that depend on different code generation patterns. For example, repetition statements such as the forStatement, doStatement or whileStatement could be annotated to be made eligible to (or forced to) unroll which is common in bit arrays, e.g. see FastBitSet.js

That is, the following is not possible today:

void fn() {
  @pragma("...unroll", 8)
  for (...) {}
}

Same for switchStatement, there are many valid generation patterns all with different trade-offs (e.g., computed gotos, branch tables, binary search, linear search). Some of that has been explored in dart-lang/sdk#49585. However, this optimization fails silently if the pattern diverges from what the compiler expects and performance becomes unpredictable. There's no way to easily lock in the optimization due to lack of support for metadata there.

Same here, the following is not possible today:

void fn() {
  @pragma("...computed-gotos")
  switch (...) {}
}

Proposal:

Support metadata on forStatement, whileStatement, doStatement, switchStatement.

forStatement
    :    metadata AWAIT? FOR '(' forLoopParts ')' statement
    ;

whileStatement
    :    metadata WHILE '(' expression ')' statement
    ;

doStatement
    :    metadata DO statement WHILE '(' expression ')' ';'
    ;

switchStatement
    :    metadata SWITCH '(' expression ')'
         LBRACE switchStatementCase* switchStatementDefault? RBRACE
    ;

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions