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

    <tr>
        <th>Summary</th>
        <td>
            Inlining function with mismatching target feature using [[gnu::flatten]] attribute causes fatal error
        </td>
    </tr>

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

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

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

<pre>
    This minimal example leads to a fatal error (https://godbolt.org/z/Y6cM836Ts):
```c
#include <smmintrin.h>

[[gnu::target("sse4.1")]]
__m128d test_impl() {
 return _mm_ceil_pd(_mm_set_sd(0.0));
}

[[gnu::flatten]]
__m128d test() {
    return test_impl();
}
```
```
fatal error: error in backend: Cannot select: 0x497142f0: v2f64 = X86ISD::VRNDSCALE 0x49714520, TargetConstant:i32<2>, example.c:5:12 @[ example.c:10:12 ]
  0x49714520: v2f64 = BUILD_VECTOR ConstantFP:f64<0.000000e+00>, ConstantFP:f64<0.000000e+00>, example.c:5:12 @[ example.c:10:12 ]
 0x49714440: f64 = ConstantFP<0.000000e+00>
    0x49714440: f64 = ConstantFP<0.000000e+00>
  0x49714210: i32 = TargetConstant<2>
In function: test
```

Using `[[gnu::always_inline]]` on `test_impl` instead of using `[[gnu::flatten]]` on `test` gives a helpful error message instead (https://godbolt.org/z/d5ebWj47n):
```
<source>:9:5: error: always_inline function 'test_impl' requires target feature 'sse4.1', but would be inlined into function 'test' that is compiled without support for 'sse4.1'
    9 | test_impl();
      |     ^
1 error generated.
Compiler returned: 1
```

I am not entirely sure if the same helpful error message should be generated for the initial example, or if the target mismatch should overrule the flatten attribute instead, such that the function is simply not inlined. FWIW, gcc seems to do the 2nd option (https://godbolt.org/z/6Taf8qhzT). But either option seems preferable to the not so helpful fatal error.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVt-P2jgQ_mvMy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