[PATCH] D137107: Allow MS extension: support of constexpr with __declspec(dllimport).

Zahira Ammarguellat via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 31 14:22:58 PDT 2022


zahiraam added a comment.

In D137107#3897326 <https://reviews.llvm.org/D137107#3897326>, @rnk wrote:

> Unless I'm missing something, I think Clang's behavior here is preferable to MSVC's. MSVC produces code which will not link. Clang turns the linker error into a compiler error, which is generally easier for the user to understand. To my knowledge, it is still true that there is no COFF relocation which can statically initialize a global variable with the address of a dllimported symbol.
>
> For the use case you are considering, how do things work out in practice? Is there some new import patching facility that I'm not aware of? Is the variable linked statically instead of dynamically, and then the linker relaxes the __imp references to direct references?

I haven't used any import patch. I just used MSVC 2019 and the test compiles and links. Not sure how the variable is linked.
The symbols generated are:
009 00000000 UNDEF  notype       External     | __imp_?next@@YAHH at Z (__declspec(dllimport) int __cdecl next(int))
012 00000000 UNDEF  notype       External     | __imp_?val@@3HA (__declspec(dllimport) int val)
and this is the asm:
main    PROC
$LN3:

  sub     rsp, 56                                 ; 00000038H

; Line 5

  mov     rax, QWORD PTR __imp_?val@@3HA
  mov     QWORD PTR val_ref$[rsp], rax

; Line 6

  mov     rax, QWORD PTR val_ref$[rsp]
  mov     ecx, DWORD PTR [rax]
  call    QWORD PTR __imp_?next@@YAHH at Z
  mov     DWORD PTR i$[rsp], eax

; Line 8

  mov     eax, DWORD PTR i$[rsp]

; Line 9

  add     rsp, 56                                 ; 00000038H
  ret     0

main    ENDP


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137107/new/

https://reviews.llvm.org/D137107



More information about the cfe-commits mailing list