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

    <tr>
        <th>Summary</th>
        <td>
            Support `constexpr` logic in `__attribute__(( naked ))` functions
        </td>
    </tr>

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

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

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

<pre>
    Summary: Clang does not compile `__attribute__(( naked ))` functions containing `constexpr` control flow, despite the fact that this is sensible. Suggest adding support for that.

---

Consider the following example:
```cpp
//(Compile with "-masm=intel")

template<bool use_cmov>
__attribute__(( naked ))
uint32_t bitscan_test( uint32_t val )
{
        if constexpr (use_cmov)
        {
                asm(
                        "bsf eax, edi\n"
                        "cmovz   eax, %0\n"
                        "ret\n"
                        : : "rm"(32) : "edi", "cc"
                );
        }
        else
        {
                asm(
                        "test   edi, edi\n"
                        "je     .ZERO\n"

                        "bsf    eax, edi\n"
                        "ret\n"

                        ".ZERO:\n"
                        "mov eax, 32\n"
                        "ret\n"

                        : : : "eax", "edi", "cc"
                );
        }
}
```
This code selects an implementation to test based on the template argument. The function is declared `naked` because we want to use `ret` directly (and we want to do that to test the branching behavior in the second implementation).

GCC compiles this just fine, but Clang produces:
```text
test.cpp:<line>:2: error: non-ASM statement in naked function is not supported
        if constexpr (use_cmov)
 ^
```
This error seems to have been added to catch cases where the user added normal C / C++ code in the function. Such code could, but probably doesn't, make sense. However, `constexpr` control flow isn't really code at all.

In this context, I read Clang's message of "not supported" as "not valid" instead of "not implemented". However, it is definitely valid, and it *could* be implemented. This issue suggests that it should be.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVstu47gS_Rp6U7DBUJYfCy8cJ7m3F4MBJr2ajUGRZYvdFGmQJTuZrx-UHomTnqTRbSiKRNbzFOuUdM7uGBA3orwV5d1Et1THtNF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