<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Always_inline / __forceinline should override dllimport inline function suppression"
   href="https://bugs.llvm.org/show_bug.cgi?id=48925">48925</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Always_inline / __forceinline should override dllimport inline function suppression
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>rnk@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>eric@efcs.ca, hans@chromium.org, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider:

$ cat t.cpp 
int notImported();
int __forceinline __declspec(dllimport) foo() { return notImported(); }
int bar() { return foo(); }

$ clang -S t.cpp  -o - --target=x86_64-windows-msvc
...
# %bb.0:                                # %entry
        subq    $40, %rsp
        .seh_stackalloc 40
        .seh_endprologue
        callq   *"__imp_?foo@@YAHXZ"(%rip)
        nop
        addq    $40, %rsp
        retq
        .seh_endproc
...


Clang doesn't honor the __forceinline attribute here because the dllimport
function refers to something that is not also imported. This logic lives here:
<a href="https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CodeGenModule.cpp#L3035">https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CodeGenModule.cpp#L3035</a>
  if (F->hasAttr<DLLImportAttr>()) {
    // Check whether it would be safe to inline this dllimport function.
    DLLImportFunctionVisitor Visitor;
    Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F));
    if (!Visitor.SafeToInline)
      return false;

It seems reasonable to power down this safety check when the user has an
explicit attribute. MSVC will inline when optimizations are enabled, but not at
-Od, so this should be OK:
<a href="https://gcc.godbolt.org/z/GYTKhM">https://gcc.godbolt.org/z/GYTKhM</a>

This is related to <a href="https://crbug.com/1090975#c10">https://crbug.com/1090975#c10</a> and the FIXME here
<a href="http://github.com/llvm/llvm-project/commit/411210838d762303027f7ac8ddc12427d774b170">http://github.com/llvm/llvm-project/commit/411210838d762303027f7ac8ddc12427d774b170</a>.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>