193 lines · plain
1.. _pipeline:2 3Core Pipeline4=============5 6.. toctree::7 :hidden:8 9 IRTranslator10 Legalizer11 RegBankSelect12 InstructionSelect13 14The core pipeline of GlobalISel is:15 16.. image:: pipeline-overview.png17 18The four passes shown in the diagram consist of:19 20:doc:`IRTranslator`21 22 Converts :doc:`LLVM-IR <../LangRef>` into :doc:`gMIR (Generic MIR) <GMIR>`.23 This is largely a direct translation and has little target customization.24 It's somewhat analogous to SelectionDAGBuilder but builds a flavour of MIR25 called gMIR instead of a specialized representation. gMIR uses exactly the26 same data structures as MIR but has more relaxed constraints. For example,27 a virtual register may be constrained to a particular type without also28 constraining it to a specific register class.29 30:doc:`Legalizer`31 32 Replaces unsupported operations with supported ones. In other words, it shapes33 the gMIR to suit what the backend can support. There is a very small set of34 operations which targets are required to support but aside from that targets35 can shape the MIR as they wish.36 37:doc:`Register Bank Selector <RegBankSelect>`38 39 Binds virtual registers to register banks. This pass is intended to minimize40 cross-register-bank copies by clustering portions of the MIR together.41 42:doc:`Instruction Select <InstructionSelect>`43 44 Select target instructions using the gMIR. At this point, the gMIR has been45 constrained enough that it becomes MIR.46 47Although we tend to talk about them as distinct passes, it should be noted that48there's a good deal of flexibility here and it's ok for things to happen49earlier than described below. For example, it's not unusual for the legalizer to50legalize an intrinsic directly to a target instruction. The concrete51requirement is that the following additional constraints are preserved after52each of these passes:53 54IRTranslator55 56 The representation must be gMIR, MIR, or a mixture of the two after this pass.57 The majority will typically be gMIR to begin with but later passes will58 gradually transition the gMIR to MIR.59 60Legalizer61 62 No illegal operations must remain or be introduced after this pass.63 64Register Bank Selector65 66 All virtual registers must have a register bank assigned after this pass.67 68Instruction Select69 70 No gMIR must remain or be introduced after this pass. In other words, we must71 have completed the conversion from gMIR to MIR.72 73In addition to these passes, there are also some optional passes that perform74an optimization. The current optional passes are:75 76Combiner77 78 Replaces patterns of instructions with a better alternative. Typically, this79 means improving run time performance by replacing instructions with faster80 alternatives but Combiners can also focus on code size or other metrics.81 82Additional passes such as these can be inserted to support higher optimization83levels or target-specific needs. A likely pipeline is:84 85.. image:: pipeline-overview-with-combiners.png86 87Of course, combiners can be inserted in other places too. Also passes can be88replaced entirely so long as their task is complete as shown in this (more89customized) example pipeline.90 91.. image:: pipeline-overview-customized.png92 93.. _maintainability-verifier:94 95MachineVerifier96---------------97 98The pass approach lets us use the ``MachineVerifier`` to enforce invariants99that are required beyond certain points of the pipeline. For example, a100function with the ``legalized`` property can have the ``MachineVerifier``101enforce that no illegal instructions occur. Similarly, a102``regBankSelected`` function may not have virtual registers without a register103bank assigned.104 105.. note::106 107 For layering reasons, ``MachineVerifier`` isn't able to be the sole verifier108 in GlobalISel. Currently some of the passes also perform verification while109 we find a way to solve this problem.110 111 The main issue is that GlobalISel is a separate library, so we can't112 directly reference it from CodeGen.113 114Testing115-------116 117The ability to test GlobalISel is significantly improved over SelectionDAG.118SelectionDAG is something of a black box and there's a lot going on inside it.119This makes it difficult to write a test that reliably tests a particular aspect120of its behaviour. For comparison, see the following diagram:121 122.. image:: testing-pass-level.png123 124Each of the grey boxes indicates an opportunity to serialize the current state125and test the behaviour between two points in the pipeline. The current state126can be serialized using ``-stop-before`` or ``-stop-after`` and loaded using127``-start-before``, ``-start-after``, and ``-run-pass``.128 129We can also go further still, as many of GlobalISel's passes are readily unit130testable:131 132.. image:: testing-unit-level.png133 134It's possible to create an imaginary target such as in `LegalizerHelperTest.cpp <https://github.com/llvm/llvm-project/blob/93b29d3882baf7df42e4e9bc26b977b00373ef56/llvm/unittests/CodeGen/GlobalISel/LegalizerHelperTest.cpp#L28-L57>`_135and perform a single step of the algorithm and check the result. The MIR and136FileCheck directives can be embedded using strings so you still have access to137the convenience available in llvm-lit.138 139Debugging140---------141 142One debugging technique that's proven particularly valuable is to use the143BlockExtractor to extract basic blocks into new functions. This can be used144to track down correctness bugs and can also be used to track down performance145regressions. It can also be coupled with function attributes to disable146GlobalISel for one or more of the extracted functions.147 148.. image:: block-extract.png149 150The command to do the extraction is:151 152.. code-block:: shell153 154 ./bin/llvm-extract -o - -S -b ‘foo:bb1;bb4’ <input> > extracted.ll155 156This particular example extracts two basic blocks from a function named ``foo``.157The new LLVM-IR can then be modified to add the ``failedISel`` attribute to the158extracted function containing bb4 to make that function use SelectionDAG.159 160This can prevent some optimizations as GlobalISel is generally able to work on a161single function at a time. This technique can be repeated for different162combinations of basic blocks until you have identified the critical blocks163involved in a bug.164 165Once the critical blocks have been identified, you can further increase the166resolution to the critical instructions by splitting the blocks like from:167 168.. code-block:: none169 170 bb1:171 ... instructions group 1 ...172 ... instructions group 2 ...173 174into:175 176.. code-block:: none177 178 bb1:179 ... instructions group 1 ...180 br %bb2181 182 bb2:183 ... instructions group 2 ...184 185and then repeating the process for the new blocks.186 187It's also possible to use this technique in a mode where the main function188is compiled with GlobalISel and the extracted basic blocks are compiled with189SelectionDAG (or the other way around) to leverage the existing quality of190another code generator to track down bugs. This technique can also be used to191improve the similarity between fast and slow code when tracking down performance192regressions and help you zero in on a particular cause of the regression.193