Add @_used and @_section attributes for global variables and top-level functions (#65901)

* 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++.
This commit is contained in:
Kuba (Brecka) Mracek
2023-05-26 14:02:32 -07:00
committed by GitHub
parent ff696c4b5d
commit 2d5f33e2e3
24 changed files with 355 additions and 2 deletions

View File

@@ -291,6 +291,9 @@ private:
/// The function's remaining set of specialize attributes.
std::vector<SILSpecializeAttr*> SpecializeAttrSet;
/// Name of a section if @_section attribute was used, otherwise empty.
StringRef Section;
/// Has value if there's a profile for this function
/// Contains Function Entry Count
ProfileCounter EntryCount;
@@ -346,6 +349,9 @@ private:
/// would indicate.
unsigned HasCReferences : 1;
/// Whether attribute @_used was present
unsigned MarkedAsUsed : 1;
/// Whether cross-module references to this function should always use weak
/// linking.
unsigned IsAlwaysWeakImported : 1;
@@ -1234,6 +1240,14 @@ public:
return V && V->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>();
}
/// Return whether this function has attribute @_used on it
bool markedAsUsed() const { return MarkedAsUsed; }
void setMarkedAsUsed(bool value) { MarkedAsUsed = value; }
/// Return custom section name if @_section was used, otherwise empty
StringRef section() const { return Section; }
void setSection(StringRef value) { Section = value; }
/// Returns true if this function belongs to a declaration that returns
/// an opaque result type with one or more availability conditions that are
/// allowed to produce a different underlying type at runtime.