<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 14, 2017, at 12:03 PM, Jordan Rose <<a href="mailto:jordan_rose@apple.com" class="">jordan_rose@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class="Apple-interchange-newline"><br class=""><blockquote type="cite" class=""><div class="">On Jun 14, 2017, at 11:24, Erik Eckstein via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Hi,<div class=""><br class=""></div><div class="">I’m about implementing statically initialized arrays. It’s about allocating storage for arrays in the data section rather than on the heap.</div><div class=""><br class=""></div><div class="">Info: the array storage is a heap object. So in the following I’m using the general term “object” but the optimization will (probably) only handle array buffers.</div><div class=""><br class=""></div><div class="">This optimization can be done for array literals containing only other literals as elements.</div><div class="">Example:</div><div class=""><br class=""></div><div class="">func createArray() -> [Int] {</div><div class=""> return [1, 2, 3]</div><div class="">}</div><div class=""><br class=""></div><div class="">The compiler can allocate the whole array buffer as a statically initialized global llvm-variable with a reference count of 2 to make it immortal.</div><div class="">It avoids heap allocations for array literals in cases stack-promotion can’t kick in. It also saves code size.</div><div class=""><br class=""></div><div class="">What’s needed for this optimization?</div><div class=""><br class=""></div><div class="">1) An optimization pass (GlobalOpt) which detects such array literal initialization patterns and “outlines” those into a statically initialized global variable</div><div class="">2) A representation of statically initialized global variables in SIL</div><div class="">3) IRGen to create statically initialized objects as global llvm-variables</div><div class=""><br class=""></div><div class="">ad 2) Changes in SIL:</div><div class=""><br class=""></div><div class="">Currently a static initialized sil_global is represented by having a reference to a globalinit function which has to match a very specific pattern (e.g. must contain a single store to the global).</div><div class="">This is somehow quirky and would get even more complicated for statically initialized objects.</div><div class=""><br class=""></div><div class="">I’d like to change that so that the sil_global itself contains the initialization value.</div><div class="">This part is not yet related to statically initialized objects. It just improves the representation of statically initialized global in general.</div><div class=""><br class=""></div><div class=""><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(51, 187, 200); font-variant-ligatures: no-common-ligatures;">@@ -1210,7 +1210,9 @@</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>Global Variables</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;"> ::</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;"> decl ::= sil-global-variable</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ static-initializer ::= '{' sil-instruction-def* '}'</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;"> sil-global-variable ::= 'sil_global' sil-linkage identifier ':' sil-type</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ (static-initializer)?</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;"> SIL representation of a global variable.</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(51, 187, 200);">@@ -1221,6 +1223,19 @@</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>SIL instructions. Prior to performing any access on the global, the</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;"> Once a global's storage has been initialized, ``global_addr`` is used to</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;"> project the value.</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+A global can also have a static initializer if it's initial value can be</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+composed of literals. The static initializer is represented as a list of</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+literal and aggregate instructions where the last instruction is the top-level</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+value of the static initializer::</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ sil_global hidden @_T04test3varSiv : $Int {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ %0 = integer_literal $Builtin.Int64, 27</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ %1 = struct $Int (%0 : $Builtin.Int64)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ }</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+In case a global has a static initializer, no ``alloc_global`` is needed before</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+it can be accessed.</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+</span></div></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div></div></div></div></blockquote></div></div></blockquote><div><br class=""></div><div>I just talked to MichaelG in person. He pointed out that the static initializer should not look like a function. Also the implicit convention that the last value is the actual top-level value is not obvious.</div><div>I think it makes sense to add some syntactic sugar to make this more clear (adding ‘=‘ and ‘init_value’):</div><div><br class=""></div><div><span style="color: rgb(52, 188, 38); font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">+ sil_global hidden @_T04test3varSiv : $Int = {</span><br class=""><span style="color: rgb(52, 188, 38); font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">+ %0 = integer_literal $Builtin.Int64, 27</span><br class=""><span style="color: rgb(52, 188, 38); font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">+ init_value = struct $Int (%0 : $Builtin.Int64)</span></div><div><div class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ }</span></div></div></div></div></div></div></div><div><br class=""></div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="">Now to represent a statically initialized<span class="Apple-converted-space"> </span><i class="">object</i>, we need a new instruction. Note that this “instruction" can<span class="Apple-converted-space"> </span><i class="">only</i><span class="Apple-converted-space"> </span>appear in the initializer of a sil_global.</div></div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+object</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+``````</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+::</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ sil-instruction ::= 'object' sil-type '(' (sil-operand (',' sil-operand)*)? ')'</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ object $T (%a : $A, %b : $B, ...)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ // $T must be a non-generic or bound generic reference type</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ // The first operands must match the stored properties of T</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ // Optionally there may be more elements, which are tail-allocated to T</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+Constructs a statically initialized object. This instruction can only appear</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+as final instruction in a global variable static initializer list.</span></div></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><br class=""></div><div class="">Finally we need an instruction to use such a statically initialized global object.</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+global_object</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+`````````````</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);">+::</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ sil-instruction ::= 'global_object' sil-global-name ':' sil-type</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ %1 = global_object @v : $T</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ // @v must be a global variable with a static initialized object</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+ // $T must be a reference type</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+Creates a reference to the address of a global variable which has a static</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+initializer which is an object, i.e. the last instruction of the global's</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 188, 38); background-color: rgb(255, 255, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">+static initializer list is an ``object`` instruction.</span></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">ad 3) IRGen support</div><div class=""><br class=""></div><div class="">Generating statically initialized globals is already done today for structs and tuples.</div><div class="">What’s needed is the handling of objects.</div><div class="">In addition to creating the global itself, we also need a runtime call to initialize the object header. In other words: the object is statically initialized, except the header.</div><div class=""><br class=""></div><div class=""><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);">HeapObject<span class=""> *</span>swift::swift_initImmortalObject(HeapMetadata <span class="" style="color: rgb(186, 45, 162);">const</span> *metadata, HeapObject *object)</div></div></div><div class=""><br class=""></div><div class="">There are 2 reasons for that: first, the object header format is not part of the ABI. And second, in case of a bound generic type (e.g. array buffers) the metadata is not statically available.</div><div class=""><br class=""></div><div class="">One way to call this runtime function is dynamically at the global_object instruction whenever the metadata pointer is still null (via swift_once).</div><div class="">Another possibility would be to call it in a global constructor.</div><div class=""><br class=""></div><div class="">If you have any feedback, please let me know</div></div></div></blockquote><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Please do not use a global constructor. :-) Globals are already set up to handle one-time initialization; the fact that that initialization is now cheaper is still a good thing.</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">To be clear, this sort of operation is only safe when the layout of the instance is statically known. The layout of an array buffer is<span class="Apple-converted-space"> </span><i class="">especially</i> brittle, since we use trailing storage, so this kind of operation really will be hardcoded in that case. I think that's fine.</div></div></blockquote><div><br class=""></div><div>I assume you are referring to the fact that the tail allocated array buffer layout was implemented in the stdlib originally. But this is not the case anymore. It’s already hard-coded in the compiler (we have a dedicated instruction for this).</div><br class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Jordan</div></div></blockquote></div><br class=""></body></html>