Adding a new machine function pass to LLVM – Beyond Instruction Selection

Now that we’ve learned about instruction selection using the SelectionDAG and GlobalISel LLVM-based frameworks in the previous chapters, we can explore other interesting concepts beyond instruction selection. This chapter encapsulates some more advanced topics outside of the backend that can be interesting for a highly optimizing compiler. For instance, some passes run beyond instruction selection and can perform different optimizations on various instructions, which can mean that developers have the luxury to introduce their own passes to perform meaningful target-specific tasks at this point in the compiler.

Ultimately, within this chapter, we will dive into the following concepts:

  • Adding a new machine function pass to LLVM
  • Integrating a new target into the clang frontend
  • How to target a different CPU architecture

Adding a new machine function pass to LLVM

In this section, we will explore how to implement a new machine function pass within LLVM that runs after instruction selection. Specifically, a MachineFunctionPass class will be created, which is a subset of the original FunctionPass class within LLVM that can be run with opt. This class adapts the original infrastructure to allow for the implementation of passes that operate on the MachineFunction representation in the backend through llc.

It is important to note that the implementation of passes within the backend utilizes the interfaces of the legacy pass manager, rather than the new pass manager. This is because LLVM currently does not have a complete working implementation of the new pass manager within the backend. Due to this, this chapter will follow the method of adding a new pass within the legacy pass manager pipeline.

In terms of the actual implementation, such as function passes, machine function passes optimize a single (machine) function at a time, but instead of overriding the runOnFunction() method, machine function passes override the runOnMachineFunction() method. The machine function pass that will be implemented in this section is a pass that checks for when a division by zero occurs, specifically, to insert code that traps in the backend. This type of pass is important for the M88k target due to hardware limitations on MC88100 as this CPU does not reliably detect division by zero situations.

Continuing from the previous chapter’s implementation of the backend, let’s examine how a backend machine function pass is implemented!

Leave a Reply

Your email address will not be published. Required fields are marked *