Skip to content

[clang] Improve the documentation for the init_priority attribute #123098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 20, 2025

Conversation

ldionne
Copy link
Member

@ldionne ldionne commented Jan 15, 2025

The documentation wasn't very clear about whether ordering is controlled within or across TUs, and same for dylibs. Clarify that, and also add mentions for the state of support on Mach-O platforms.

The documentation wasn't very clear about whether ordering is controlled
within or across TUs, and same for dylibs. Clarify that, and also add
mentions for the state of support on Mach-O platforms.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 15, 2025

@llvm/pr-subscribers-clang

Author: Louis Dionne (ldionne)

Changes

The documentation wasn't very clear about whether ordering is controlled within or across TUs, and same for dylibs. Clarify that, and also add mentions for the state of support on Mach-O platforms.


Full diff: https://github.com/llvm/llvm-project/pull/123098.diff

1 Files Affected:

  • (modified) clang/include/clang/Basic/AttrDocs.td (+12-5)
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 5e66e752512d0d..4edad37ba3a8ee 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -121,11 +121,12 @@ def InitPriorityDocs : Documentation {
 In C++, the order in which global variables are initialized across translation
 units is unspecified, unlike the ordering within a single translation unit. The
 ``init_priority`` attribute allows you to specify a relative ordering for the
-initialization of objects declared at namespace scope in C++. The priority is
-given as an integer constant expression between 101 and 65535 (inclusive).
-Priorities outside of that range are reserved for use by the implementation. A
-lower value indicates a higher priority of initialization. Note that only the
-relative ordering of values is important. For example:
+initialization of objects declared at namespace scope in C++, even between translation
+units (but within a single final linked image). The priority is given as an integer
+constant expression between 101 and 65535 (inclusive). Priorities outside of that
+range are reserved for use by the implementation. A lower value indicates a higher
+priority of initialization. Note that only the relative ordering of values is
+important. For example:
 
 .. code-block:: c++
 
@@ -136,12 +137,18 @@ relative ordering of values is important. For example:
 ``Obj2`` will be initialized *before* ``Obj1`` despite the usual order of
 initialization being the opposite.
 
+Note that this attribute does not control the initialization order of objects
+across final linked image boundaries like shared objects and executables.
+
 On Windows, ``init_seg(compiler)`` is represented with a priority of 200 and
 ``init_seg(library)`` is represented with a priority of 400. ``init_seg(user)``
 uses the default 65535 priority.
 
 This attribute is only supported for C++ and Objective-C++ and is ignored in
 other language modes. Currently, this attribute is not implemented on z/OS.
+This attribute also does not control the order of initialization across
+translation units on Mach-O platforms, where it only affects the order within
+a single TU.
   }];
 }
 

lower value indicates a higher priority of initialization. Note that only the
relative ordering of values is important. For example:
initialization of objects declared at namespace scope in C++, even between translation
units (but within a single final linked image). The priority is given as an integer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this reads better:

... declared at namespace scope in C++ with in a single linked image on supported platforms

I am unsure about how COFF support is, does that work across all .dlls? If Windows actually works across boundaries, this needs to be revised.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this reads better. I am not certain about how this is handled with DLLs, maybe someone like @rnk would know?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my understanding from the windows document, I don't think that work across the DLLs (meaning grouping the init from the same priority group from different DLL together). Updates LGTM.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you for the improved docs!

@ldionne ldionne merged commit 64edde6 into llvm:main Jan 20, 2025
9 checks passed
@ldionne ldionne deleted the review/init-priority-documentation branch January 20, 2025 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants