<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/92410>92410</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Better ways to write GlobalISel combine "match" and "apply" functions
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jayfoad
      </td>
    </tr>
</table>

<pre>
    GlobalISel combines are written as separate "match" and "apply" functions but since #92239 we try to inline those into a single "match+apply" function. The two parts can share information in a couple of ways:

1. You can declare a matchinfo (aka matchdata) type, an instance of which is populated by the match function and can be used by the apply function. This adds a bit of boilerplate to the tablegen declarations and is slightly less ergonomic than allowing the apply function to refer to the match function's locals directly.

2. (Really a special case of 1) the match function can set the matchdata to be a lambda which _is_ the real apply function. In tablegen the match function is declared to be `Helper.applyBuildFn` which is a helper that just invokes the lambda. This has the advantage that the apply function _can_ refer to (captured) locals from the match function directly.

The problem with (2) is that when we try to compile the combined match+apply function, I think the compiler will not be able to see through`Helper.applyBuildFn` in order to inline the real apply function.

I would like to find a way to write combines that is as ergonomic _and_ as efficient as possible.

One idea I had was to invent some tablegen syntax that lets you declare a single combined match+apply function that you write explicitly in C++, instead of writing the two parts separately.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVV1v6zYM_TXKC3EDR_lqHvKw9iJbnwZse9lTQEt0zJaWDEmur__9ICW5SdEMG1A0kE2eQ55jUhgjnxzRXq2f1fr7DIfU-rB_w6nxaGe1t9P-V_E1yuufJGB8V7OjCBgIxsApkQOMEKnHgIlAad1hMq3SGtDZfMa-lymfm8GZxN5FqIcEkZ3J8cud1ssdjAQpTJA8sBN2BKn1kYBd8oA5-CT36M9fUOfwV0uQRg89hhTBoIPY5jrZNT50mIOAHSAYP_RC4BsYcYpq-Yuqvqvq8n8xh7_9UNItGckACIU144DST_h-e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