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

Eli Friedman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 16 10:10:07 PST 2022


efriedma added a comment.

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.


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

https://reviews.llvm.org/D137107



More information about the cfe-commits mailing list