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

Zahira Ammarguellat via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 29 15:20:48 PST 2022


zahiraam added a comment.

In D137107#3931259 <https://reviews.llvm.org/D137107#3931259>, @efriedma wrote:

> There are multiple notions of "constant" here:
>
> - Whether a value is invariant at runtime.
> - Whether a value is legal in a "constexpr" expression.
> - Whether we can emit a constant without emitting code that runs at startup to initialize it.
>
> The third is generally a superset of the second; most targets have relocations that allow us emit the address of a global variable into another global variable.  dllimport variables are an exception: even though the address is constant, we can't emit a relocation.
>
> To make this work, what we need to do is:
>
> - Extend the notion of a "constant" in the AST/Sema to allow dllimport variables.  (There should be code specifically checking for dllimport variables; we just need to drop that check.)
> - Teach IR generation (clang/lib/CodeGen) how to emit constants that don't have a relocation.  This probably means a check that if we're generating the initializer for a global variable, and the initializer refers to the address of a dllimport variable, we instead emit a global constructor.  (Maybe with a higher priority that normal global constructor, so the user can't accidentally observe an uninitialized variable?  I forget if MSVC actually has constructor priorities...).  Or I guess as an initial patch, we could just emit an error if we detect this case.

@efriedma Thanks for taking the time to look at it and for the explanation. I made an attempt to make a few changes.  Please let me know if this is moving in the right direction. Thanks.


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

https://reviews.llvm.org/D137107



More information about the cfe-commits mailing list