Create an swift compiler api to generate SIL directly

As the swift language is evolving, several code-generator projects have come up to reduce the developer overhead. Some of the popular ones the come to mind are:

Code generators tend to generate a lot of source code and thus take time to compile. I believe we can speed up this compilation by directly generating code in Swift Intermediate Language ( SIL ). This way we can avoid TypeChecking, something that the code generator already has knowledge about.

According to my profiling, swiftc spends about 22% of its time in type-checking. This is when all the types are declared and swift doesn't have to do a lot of type-inference.

Questions:

  • Is generating SIL enough or are we losing some information that is not captured by SIL
  • Can swiftc compile SIL directly, or is this something that would need to be added too?
  • How does the community feel about adding this functionality to the swift-compiler?
2 Likes

To my knowledge, none of these code generators do any type checking. Nothing prevents you from supplying some code that doesn't type check to Sourcery, for example, and get invalid code as an output. The fact that you use any code generators doesn't mean that your code can successfully pass type checking.

In addition, there's a lot more semantic analysis going on before the SIL generation stage, not just type checking. Off the top of my head, checking whether your switch cases are exhausitve, whether let bindings to value types are not attempted to be reassigned or mutated, whether your variables and constants are initialized before use etc, all this happens in the Sema stage before any SIL is generated.

1 Like

SIL is an internal representation, not a stable language, and so is inappropriate to write programs in.

1 Like

What about a friendly Swift API for the AST?

1 Like

If we integrate this into the swift-compiler wouldn't that help? Shouldn't it be fine as long as the compiler version for code-generating and compiling is the same?

Yes, this could work too :slight_smile: I am open to the suggestion.

1 Like

Yes, they don't do type checking. However, they do have knowledge of the types. For e.g. Needle has it's own AST models that have information about types. I am asking here is for an API ( maybe libSwiftCodegen ? ) that helps us dump all the type information and then it can generate the SIL from it.

True, I didn't think about this. We could maybe execute the Sema pass once we dump our type information or as @Paul_Cantrell suggested, maybe we could have the API just for AST instead?