How to add a new swift-based SIL optimization pass?

i've been trying to learn a bit more about the SIL optimization passes written in swift by adding a new one locally to test things out. however i've yet to be able to successfully run my new pass that does nothing but print() a string without crashing the compiler. what i've tried thus far:

  1. add a new file in the SwiftCompilerSources/Sources/Optimizer/FunctionPasses directory
  2. update the relevant CMakeLists.txt with the new file name
  3. add a new global variable for the pass and stub out the implementation of the FunctionPass type with a print statement (trying to copy the scaffolding of the existing swift-based function passes).
  4. update Passes.def with a new PASS macro entry with the appropriate id & tag
  5. update registerSwiftPasses() to explicitly register the function pass
  6. register the pass with a pipeline in addMandatoryDiagnosticOptPipeline so it will always run when emitting SIL

after doing this, when i try to run my dev compiler, it crashes on this assertion. i haven't yet made sense of what that assertion is enforcing exactly, nor how to change my code to avoid hitting it. any advice would be appreciated!

3 Likes

after cleaning my build directory and rebuilding, the code no longer hits the assertion and successfully issues the expected print statements when emitting SIL. so i guess i'll chalk this up as one of the mysteries of the universe :person_shrugging:

2 Likes

This usually indicates a missing dependency in the build system, so something didn’t get recompiled that should have the first time.

1 Like