Module monomorphization

Source
Expand description

Performs monomorphization of const generic functions within a ProgramScope.

This pass identifies all inline functions that take const generic parameters (e.g., foo::[N: u32]()) and replaces each unique instantiation (e.g., foo::[3](), foo::[7]()) with a concrete version of the function where the const parameter is replaced with its actual value. These concrete instances are generated, added to the reconstructed function list, and inserted into the final program scope.

If a function has been monomorphized and is no longer referenced by any unresolved calls, it will be removed from the reconstructed functions and pruned from the call graph.

ยงExample

transition main(x: u32, y: u32) -> u32 {
    return foo::[3u32]() + foo::[7u32]();
}

inline foo::[N: u32]() -> u32 {
    return N;
}

In the example above:

  • foo::[3u32]() and foo::[7u32]() are two distinct instantiations of foo::[N].
  • This pass will generate two monomorphized versions of foo, one for each unique const argument M and P.
  • These are inserted into the output ProgramScope as separate functions with unique names.
  • If foo::[N] is no longer referenced in any calls, the original generic function is removed.

Modulesยง

ast ๐Ÿ”’
program ๐Ÿ”’
visitor ๐Ÿ”’

Structsยง

Monomorphization
MonomorphizationOutput