This has been suggested as an incremental follow-up to https://reviews.llvm.org/D26769.
Adding a unit field to DIGlobalVariable makes the design consistent with the design of DISubprogram
(DISubprogram definitions that are attached to Functions also have a unit field).
It also allows us to more efficiently process global variables in the DWARF backend.
After this patch, global variables occur in three forms in the debug info metadata:
- Variable definitions are reachable via a !dbg attachment on a global that points to a DIGlobalVariableExpression which points to a distinct DIGlobalVariable (and an optional DIExpression) with a unit field pointing to the variable's DICompileUnit.
- Constant definitions are reachable via the DICompileUnit's list of globals which points to a DIGlobalVariableExpression with a DIGlobalVariable without a unit field and a DIExpression describing a constant value.
- Declarations are DIGlobalVariables without a unit field that are only reachable via a DIImportedEntity and have isDefinition set to false.
Probably could omit the reference to linkonce_odr and let the rest of the definition stand.
That way if anything were to duplicate other globals (eg: thinlto might import globals, etc) they could/would use this functionality as well, potentially.