Skip to content

Conversation

@kubamracek
Copy link
Contributor

Add @_used and @_section attributes for global variables and top-level functions

This adds:

  • @_used attribute that flags as a global variable or a top-level function as "do not dead-strip" via llvm.used, roughly the equivalent of __attribute__((used)) in C/C++.
  • @_section("...") attribute that places a global variable or a top-level function into a section with that name, roughly the equivalent of __attribute__((section("..."))) in C/C++.

@kubamracek kubamracek requested a review from eeckstein May 13, 2023 05:06
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to think about inlining behavior. What should happen if a function with a section attribute is inlined into a function without (or a different) section attribute? Should inlining be prevented in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. My sense is that we should mirror what __attribute__((section)) does in Clang: It does not prevent inlining or interact with it in any way. What the attribute does is that it makes sure that the standalone/top-level function is emitted into that section, but other functions are still free to inline the code from it (as long as we also emit the standalone function that is in the right section). If the user wants to prevent inlining, they can use another attribute for that, __attribute__((noinline)) / @inline(never).

WDYT?

@kubamracek kubamracek marked this pull request as ready for review May 19, 2023 23:43
@kubamracek kubamracek requested a review from jckarter May 19, 2023 23:44
@kubamracek kubamracek force-pushed the used-and-section branch 7 times, most recently from 7d2e1e5 to 888f675 Compare May 22, 2023 14:52
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

As @jckarter pointed out to me, it's needed to use different section names for different platforms / object file formats, but luckily this can already be done by conditionalizing the attribute:

#if os(...)
@_section("...")
#elif os(...)
@_section("...")
#endif
func foo()

@kubamracek
Copy link
Contributor Author

Notes to self: I need to also update (1) UnderscoredAttributes.md, (2) the attribute list in SwiftSyntax.

@hborla
Copy link
Member

hborla commented May 24, 2023

I don't see a new experimental feature for these attributes. Can you please gate them behind a new EXPERIMENTAL_FEATURE identifier (declared in Features.def)?

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

lgtm!

@kubamracek kubamracek requested a review from tshortli as a code owner May 24, 2023 23:29
@kubamracek
Copy link
Contributor Author

I don't see a new experimental feature for these attributes. Can you please gate them behind a new EXPERIMENTAL_FEATURE identifier (declared in Features.def)?

Added.

@kubamracek
Copy link
Contributor Author

@swift-ci please test

2 similar comments
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

@swift-ci please test

…l functions

This adds:
- @_used attribute that flags as a global variable or a top-level function as
  "do not dead-strip" via llvm.used, roughly the equivalent of
  __attribute__((used)) in C/C++.
- @_section("...") attribute that places a global variable or a top-level
  function into a section with that name, roughly the equivalent of
  __attribute__((section("..."))) in C/C++.
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@dreampiggy
Copy link

Finally😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants