HowToUseInstrMappings.rst 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ===============================
  2. How To Use Instruction Mappings
  3. ===============================
  4. .. contents::
  5. :local:
  6. Introduction
  7. ============
  8. This document contains information about adding instruction mapping support
  9. for a target. The motivation behind this feature comes from the need to switch
  10. between different instruction formats during various optimizations. One approach
  11. could be to use switch cases which list all the instructions along with formats
  12. they can transition to. However, it has large maintenance overhead
  13. because of the hardcoded instruction names. Also, whenever a new instruction is
  14. added in the .td files, all the relevant switch cases should be modified
  15. accordingly. Instead, the same functionality could be achieved with TableGen and
  16. some support from the .td files for a fraction of maintenance cost.
  17. ``InstrMapping`` Class Overview
  18. ===============================
  19. TableGen uses relationship models to map instructions with each other. These
  20. models are described using ``InstrMapping`` class as a base. Each model sets
  21. various fields of the ``InstrMapping`` class such that they can uniquely
  22. describe all the instructions using that model. TableGen parses all the relation
  23. models and uses the information to construct relation tables which relate
  24. instructions with each other. These tables are emitted in the
  25. ``XXXInstrInfo.inc`` file along with the functions to query them. Following
  26. is the definition of ``InstrMapping`` class definied in Target.td file:
  27. .. code-block:: text
  28. class InstrMapping {
  29. // Used to reduce search space only to the instructions using this
  30. // relation model.
  31. string FilterClass;
  32. // List of fields/attributes that should be same for all the instructions in
  33. // a row of the relation table. Think of this as a set of properties shared
  34. // by all the instructions related by this relationship.
  35. list<string> RowFields = [];
  36. // List of fields/attributes that are same for all the instructions
  37. // in a column of the relation table.
  38. list<string> ColFields = [];
  39. // Values for the fields/attributes listed in 'ColFields' corresponding to
  40. // the key instruction. This is the instruction that will be transformed
  41. // using this relation model.
  42. list<string> KeyCol = [];
  43. // List of values for the fields/attributes listed in 'ColFields', one for
  44. // each column in the relation table. These are the instructions a key
  45. // instruction will be transformed into.
  46. list<list<string> > ValueCols = [];
  47. }
  48. Sample Example
  49. --------------
  50. Let's say that we want to have a function
  51. ``int getPredOpcode(uint16_t Opcode, enum PredSense inPredSense)`` which
  52. takes a non-predicated instruction and returns its predicated true or false form
  53. depending on some input flag, ``inPredSense``. The first step in the process is
  54. to define a relationship model that relates predicated instructions to their
  55. non-predicated form by assigning appropriate values to the ``InstrMapping``
  56. fields. For this relationship, non-predicated instructions are treated as key
  57. instruction since they are the one used to query the interface function.
  58. .. code-block:: text
  59. def getPredOpcode : InstrMapping {
  60. // Choose a FilterClass that is used as a base class for all the
  61. // instructions modeling this relationship. This is done to reduce the
  62. // search space only to these set of instructions.
  63. let FilterClass = "PredRel";
  64. // Instructions with same values for all the fields in RowFields form a
  65. // row in the resulting relation table.
  66. // For example, if we want to relate 'ADD' (non-predicated) with 'Add_pt'
  67. // (predicated true) and 'Add_pf' (predicated false), then all 3
  68. // instructions need to have same value for BaseOpcode field. It can be any
  69. // unique value (Ex: XYZ) and should not be shared with any other
  70. // instruction not related to 'add'.
  71. let RowFields = ["BaseOpcode"];
  72. // List of attributes that can be used to define key and column instructions
  73. // for a relation. Key instruction is passed as an argument
  74. // to the function used for querying relation tables. Column instructions
  75. // are the instructions they (key) can transform into.
  76. //
  77. // Here, we choose 'PredSense' as ColFields since this is the unique
  78. // attribute of the key (non-predicated) and column (true/false)
  79. // instructions involved in this relationship model.
  80. let ColFields = ["PredSense"];
  81. // The key column contains non-predicated instructions.
  82. let KeyCol = ["none"];
  83. // Two value columns - first column contains instructions with
  84. // PredSense=true while second column has instructions with PredSense=false.
  85. let ValueCols = [["true"], ["false"]];
  86. }
  87. TableGen uses the above relationship model to emit relation table that maps
  88. non-predicated instructions with their predicated forms. It also outputs the
  89. interface function
  90. ``int getPredOpcode(uint16_t Opcode, enum PredSense inPredSense)`` to query
  91. the table. Here, Function ``getPredOpcode`` takes two arguments, opcode of the
  92. current instruction and PredSense of the desired instruction, and returns
  93. predicated form of the instruction, if found in the relation table.
  94. In order for an instruction to be added into the relation table, it needs
  95. to include relevant information in its definition. For example, consider
  96. following to be the current definitions of ADD, ADD_pt (true) and ADD_pf (false)
  97. instructions:
  98. .. code-block:: text
  99. def ADD : ALU32_rr<(outs IntRegs:$dst), (ins IntRegs:$a, IntRegs:$b),
  100. "$dst = add($a, $b)",
  101. [(set (i32 IntRegs:$dst), (add (i32 IntRegs:$a),
  102. (i32 IntRegs:$b)))]>;
  103. def ADD_Pt : ALU32_rr<(outs IntRegs:$dst),
  104. (ins PredRegs:$p, IntRegs:$a, IntRegs:$b),
  105. "if ($p) $dst = add($a, $b)",
  106. []>;
  107. def ADD_Pf : ALU32_rr<(outs IntRegs:$dst),
  108. (ins PredRegs:$p, IntRegs:$a, IntRegs:$b),
  109. "if (!$p) $dst = add($a, $b)",
  110. []>;
  111. In this step, we modify these instructions to include the information
  112. required by the relationship model, <tt>getPredOpcode</tt>, so that they can
  113. be related.
  114. .. code-block:: text
  115. def ADD : PredRel, ALU32_rr<(outs IntRegs:$dst), (ins IntRegs:$a, IntRegs:$b),
  116. "$dst = add($a, $b)",
  117. [(set (i32 IntRegs:$dst), (add (i32 IntRegs:$a),
  118. (i32 IntRegs:$b)))]> {
  119. let BaseOpcode = "ADD";
  120. let PredSense = "none";
  121. }
  122. def ADD_Pt : PredRel, ALU32_rr<(outs IntRegs:$dst),
  123. (ins PredRegs:$p, IntRegs:$a, IntRegs:$b),
  124. "if ($p) $dst = add($a, $b)",
  125. []> {
  126. let BaseOpcode = "ADD";
  127. let PredSense = "true";
  128. }
  129. def ADD_Pf : PredRel, ALU32_rr<(outs IntRegs:$dst),
  130. (ins PredRegs:$p, IntRegs:$a, IntRegs:$b),
  131. "if (!$p) $dst = add($a, $b)",
  132. []> {
  133. let BaseOpcode = "ADD";
  134. let PredSense = "false";
  135. }
  136. Please note that all the above instructions use ``PredRel`` as a base class.
  137. This is extremely important since TableGen uses it as a filter for selecting
  138. instructions for ``getPredOpcode`` model. Any instruction not derived from
  139. ``PredRel`` is excluded from the analysis. ``BaseOpcode`` is another important
  140. field. Since it's selected as a ``RowFields`` of the model, it is required
  141. to have the same value for all 3 instructions in order to be related. Next,
  142. ``PredSense`` is used to determine their column positions by comparing its value
  143. with ``KeyCol`` and ``ValueCols``. If an instruction sets its ``PredSense``
  144. value to something not used in the relation model, it will not be assigned
  145. a column in the relation table.