<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jun 14, 2017, at 4:04 PM, Michael Gottesman 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 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=""><br class="Apple-interchange-newline">On Jun 14, 2017, at 11:24 AM, 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; 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></div></blockquote><div class=""><br class=""></div><div class="">I was thinking about this a little bit. IMO, we probably actually want a bit in the header. The reason why is that even though setting the ref count to be unbalanced makes an object immortal, retain/release will still modify the reference count meaning that the statically initialized constant can not be in read only memory. On the other hand, if we had a bit in the header, we could use read only memory.</div><div class=""><br class=""></div><div class="">Michael</div></div></div></blockquote><div><br class=""></div><div>This is also surprisingly perf-relevant in concurrent situations. We ran into a scenario in Foundation this year where contention on the refcount of a global object was enough to cause an order of magnitude slowdown when the test was run on 10 threads.</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>David</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=""><blockquote type="cite" class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><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 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 class=""><br class=""></div><div class="">Thanks,</div><div class="">Erik</div></div>_______________________________________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a><br class=""></div></blockquote></div><br class="" 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;"><span 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; float: none; display: inline !important;" class="">_______________________________________________</span><br 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=""><span 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; float: none; display: inline !important;" class="">swift-dev mailing list</span><br 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=""><a href="mailto:swift-dev@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">swift-dev@swift.org</a><br 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=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a></div></blockquote></div><br class=""></body></html>