Allow using structs with trivial initializers in globals that require static initialization (e.g. @_section attribute)

Before this change, if a global variable is required to be statically initialized (e.g. due to @_section attribute), we don't allow its type to be a struct, only a scalar type works. This change improves on that by teaching MandatoryPerformanceOptimizations pass to inline struct initializer calls into initializer of globals, as long as they are simple enough so that we can be sure that we don't trigger recursive/infinite inlining.
This commit is contained in:
Kuba Mracek
2023-07-08 19:26:59 -07:00
parent 438c067c8c
commit 145f12f6a3
11 changed files with 283 additions and 32 deletions

View File

@@ -342,6 +342,8 @@ ERROR(performance_callee_unavailable,none,
"called function is not available in this module and can have unpredictable performance", ())
ERROR(bad_attr_on_non_const_global,none,
"global variable must be a compile-time constant to use %0 attribute", (StringRef))
ERROR(global_must_be_compile_time_const,none,
"global variable must be a compile-time constant", ())
NOTE(performance_called_from,none,
"called from here", ())

View File

@@ -369,6 +369,8 @@ struct BridgedGlobalVar {
inline OptionalBridgedInstruction getStaticInitializerValue() const;
bool canBeInitializedStatically() const;
bool mustBeInitializedStatically() const;
};
struct OptionalBridgedGlobalVar {

View File

@@ -173,6 +173,8 @@ public:
/// static initializer.
SILInstruction *getStaticInitializerValue();
bool mustBeInitializedStatically() const;
/// Returns true if the global is a statically initialized heap object.
bool isInitializedObject() {
return dyn_cast_or_null<ObjectInst>(getStaticInitializerValue()) != nullptr;