{"data":{"post":{"title":"When the Swift Compiler Deleted Code in Stdlib - A Note of Fixing the Eliminate Redundant Load Pass in Swift 6","subtitle":"","isPublished":true,"createdTime":"2025-03-09T00:00:00.000Z","lastModifiedTime":null,"license":null,"tags":["Swift","Compiler"],"category":"Programming","file":{"childMdx":{"excerpt":"Latest updated: Apple has accepted the fix for this issue. The final\nsolution was adjusted following…","code":{"body":"function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\nconst layoutProps = {};\nreturn class MDXContent extends React.Component {\n  constructor(props) {\n    super(props);\n    this.layout = null;\n  }\n\n  render() {\n    const _this$props = this.props,\n          {\n      components\n    } = _this$props,\n          props = _objectWithoutProperties(_this$props, [\"components\"]);\n\n    return React.createElement(MDXTag, {\n      name: \"wrapper\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"blockquote\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"p\",\n      components: components,\n      parentName: \"blockquote\"\n    }, `Latest updated: Apple has accepted the fix for this issue. The final\nsolution was adjusted following a review by the relevant code owner.`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Right before the Chinese New Year vacation of the Year of the Snake, a\ncolleague showed me a mysterious crash caused by a use-after-free error.\nRecently, I found time to investigate this issue and discovered that the\ncrash resulted from a miscompilation by the Swift compiler. The minimal\nreproducible code appears below and must be compiled with the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `-Osize`), `\noptimization level. We can detect the use-after-free by enabling the\naddress sanitizer during compilation.`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `let storage = ValueStorage()\nstorage.append(1)\n// Crash at runtime!\nstorage.append(2)\n\npublic class ValueStorage {\n    \n    private class Data {\n    \n        var values: [Int] = []\n    \n    }\n    \n    private var data = Data()\n    \n    private func withAutoreleasingUnsafeMutableData<R>(_ body: (_ dataPtr: AutoreleasingUnsafeMutablePointer<Data>) throws -> R) rethrows -> R {\n        try withUnsafeMutablePointer(to: &data) { pointer in\n            try body(AutoreleasingUnsafeMutablePointer(pointer))\n        }\n    }\n    \n    public func append(_ value: Int) {\n        withAutoreleasingUnsafeMutableData { dataPtr in\n            // Immediately crashed line\n            dataPtr.pointee.values.append(value)\n        }\n    }\n    \n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/ad00b90728b64c3d88e939013fbd74f7/deb5c/enable-asan-in-xcode.webp\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"picture\",\n      components: components,\n      parentName: \"a\"\n    }, `\n  `, React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/ad00b90728b64c3d88e939013fbd74f7/0cc25/enable-asan-in-xcode.png\",\n        \"srcSet\": [\"/static/ad00b90728b64c3d88e939013fbd74f7/5116e/enable-asan-in-xcode.png 178w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/92f55/enable-asan-in-xcode.png 356w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/0cc25/enable-asan-in-xcode.png 712w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/7ae06/enable-asan-in-xcode.png 1068w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/eee47/enable-asan-in-xcode.png 1424w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/38407/enable-asan-in-xcode.png 2136w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/5b6d0/enable-asan-in-xcode.png 3024w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/ad00b90728b64c3d88e939013fbd74f7/690c8/enable-asan-in-xcode.webp\",\n        \"srcSet\": [\"/static/ad00b90728b64c3d88e939013fbd74f7/25c8a/enable-asan-in-xcode.webp 178w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/60698/enable-asan-in-xcode.webp 356w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/690c8/enable-asan-in-xcode.webp 712w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/d7e52/enable-asan-in-xcode.webp 1068w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/456ef/enable-asan-in-xcode.webp 1424w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/2a654/enable-asan-in-xcode.webp 2136w\", \"/static/ad00b90728b64c3d88e939013fbd74f7/deb5c/enable-asan-in-xcode.webp 3024w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), `\n  `, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/ad00b90728b64c3d88e939013fbd74f7/deb5c/enable-asan-in-xcode.webp\",\n        \"alt\": \"Enable Address Sanitizer in Xcode\",\n        \"title\": \"Enable Address Sanitizer in Xcode\",\n        \"width\": 712,\n        \"height\": 502,\n        \"loading\": \"lazy\"\n      }\n    }))), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            Enable Address Sanitizer in Xcode\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/fe5df802257e7505d12c12220aa11432/deb5c/uaf-issue.webp\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"picture\",\n      components: components,\n      parentName: \"a\"\n    }, `\n  `, React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/fe5df802257e7505d12c12220aa11432/0cc25/uaf-issue.png\",\n        \"srcSet\": [\"/static/fe5df802257e7505d12c12220aa11432/5116e/uaf-issue.png 178w\", \"/static/fe5df802257e7505d12c12220aa11432/92f55/uaf-issue.png 356w\", \"/static/fe5df802257e7505d12c12220aa11432/0cc25/uaf-issue.png 712w\", \"/static/fe5df802257e7505d12c12220aa11432/7ae06/uaf-issue.png 1068w\", \"/static/fe5df802257e7505d12c12220aa11432/eee47/uaf-issue.png 1424w\", \"/static/fe5df802257e7505d12c12220aa11432/38407/uaf-issue.png 2136w\", \"/static/fe5df802257e7505d12c12220aa11432/5b6d0/uaf-issue.png 3024w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/fe5df802257e7505d12c12220aa11432/690c8/uaf-issue.webp\",\n        \"srcSet\": [\"/static/fe5df802257e7505d12c12220aa11432/25c8a/uaf-issue.webp 178w\", \"/static/fe5df802257e7505d12c12220aa11432/60698/uaf-issue.webp 356w\", \"/static/fe5df802257e7505d12c12220aa11432/690c8/uaf-issue.webp 712w\", \"/static/fe5df802257e7505d12c12220aa11432/d7e52/uaf-issue.webp 1068w\", \"/static/fe5df802257e7505d12c12220aa11432/456ef/uaf-issue.webp 1424w\", \"/static/fe5df802257e7505d12c12220aa11432/2a654/uaf-issue.webp 2136w\", \"/static/fe5df802257e7505d12c12220aa11432/deb5c/uaf-issue.webp 3024w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), `\n  `, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/fe5df802257e7505d12c12220aa11432/deb5c/uaf-issue.webp\",\n        \"alt\": \"UAF Issue\",\n        \"title\": \"UAF Issue Occurred\",\n        \"width\": 712,\n        \"height\": 502,\n        \"loading\": \"lazy\"\n      }\n    }))), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            UAF Issue Occurred\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Interestingly, replacing `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `AutoreleasingUnsafeMutablePointer`), ` with\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `UnsafeMutablePointer`), ` eliminates the issue.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/deb5c/no-uaf-issue.webp\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"picture\",\n      components: components,\n      parentName: \"a\"\n    }, `\n  `, React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/0cc25/no-uaf-issue.png\",\n        \"srcSet\": [\"/static/5fecc6bb5e14e38c6eeab4a07b818f93/5116e/no-uaf-issue.png 178w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/92f55/no-uaf-issue.png 356w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/0cc25/no-uaf-issue.png 712w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/7ae06/no-uaf-issue.png 1068w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/eee47/no-uaf-issue.png 1424w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/38407/no-uaf-issue.png 2136w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/5b6d0/no-uaf-issue.png 3024w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/690c8/no-uaf-issue.webp\",\n        \"srcSet\": [\"/static/5fecc6bb5e14e38c6eeab4a07b818f93/25c8a/no-uaf-issue.webp 178w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/60698/no-uaf-issue.webp 356w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/690c8/no-uaf-issue.webp 712w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/d7e52/no-uaf-issue.webp 1068w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/456ef/no-uaf-issue.webp 1424w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/2a654/no-uaf-issue.webp 2136w\", \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/deb5c/no-uaf-issue.webp 3024w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), `\n  `, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/5fecc6bb5e14e38c6eeab4a07b818f93/deb5c/no-uaf-issue.webp\",\n        \"alt\": \"No UAF Issue\",\n        \"title\": \"No UAF Issue Occurred\",\n        \"width\": 712,\n        \"height\": 502,\n        \"loading\": \"lazy\"\n      }\n    }))), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            No UAF Issue Occurred\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"h2\",\n      components: components\n    }, `Investigating the Primary Scene of the Crash`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `After disassembling the problematic program, we can find that the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Array`), `\nappending function was inlined in the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `ValueStorage.append`), ` function. The\nkey issue is that the program failed to re-acquire the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Array`), `'s buffer\nobject after reallocation. This causes the address computed with register\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `r12`), ` to point to the old buffer if reallocation occurred. The\ndisassembled code can be simplified as:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-asm\"\n      }\n    }, `; load \\`self.values: [Int]\\` to r12\nmov r15, qword [r13 + 0x10]\n; r14 is now \\`self.values: [Int]\\`\nmov r14, r15\n; loads the Array's buffer object to r12\nmov r12, qword [r14 + 0x10]\n; reallocate, may free the old buffer object\ncall Swift.Array._reserveCapacityAssumingUniqueBuffer\n; set the new count to the old buffer object\n; use-after-free occurred\nmov qword [r12 + 0x10] rax\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Here is the complete disassembled code of `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `ValueStorage.append`), `:`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/c570069615c6a8233ddeadee8385f35d/04e84/generated-target-code-with-uaf-issue.webp\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"picture\",\n      components: components,\n      parentName: \"a\"\n    }, `\n  `, React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/c570069615c6a8233ddeadee8385f35d/0cc25/generated-target-code-with-uaf-issue.png\",\n        \"srcSet\": [\"/static/c570069615c6a8233ddeadee8385f35d/5116e/generated-target-code-with-uaf-issue.png 178w\", \"/static/c570069615c6a8233ddeadee8385f35d/92f55/generated-target-code-with-uaf-issue.png 356w\", \"/static/c570069615c6a8233ddeadee8385f35d/0cc25/generated-target-code-with-uaf-issue.png 712w\", \"/static/c570069615c6a8233ddeadee8385f35d/7ae06/generated-target-code-with-uaf-issue.png 1068w\", \"/static/c570069615c6a8233ddeadee8385f35d/eee47/generated-target-code-with-uaf-issue.png 1424w\", \"/static/c570069615c6a8233ddeadee8385f35d/38407/generated-target-code-with-uaf-issue.png 2136w\", \"/static/c570069615c6a8233ddeadee8385f35d/2d4b3/generated-target-code-with-uaf-issue.png 2508w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/c570069615c6a8233ddeadee8385f35d/690c8/generated-target-code-with-uaf-issue.webp\",\n        \"srcSet\": [\"/static/c570069615c6a8233ddeadee8385f35d/25c8a/generated-target-code-with-uaf-issue.webp 178w\", \"/static/c570069615c6a8233ddeadee8385f35d/60698/generated-target-code-with-uaf-issue.webp 356w\", \"/static/c570069615c6a8233ddeadee8385f35d/690c8/generated-target-code-with-uaf-issue.webp 712w\", \"/static/c570069615c6a8233ddeadee8385f35d/d7e52/generated-target-code-with-uaf-issue.webp 1068w\", \"/static/c570069615c6a8233ddeadee8385f35d/456ef/generated-target-code-with-uaf-issue.webp 1424w\", \"/static/c570069615c6a8233ddeadee8385f35d/2a654/generated-target-code-with-uaf-issue.webp 2136w\", \"/static/c570069615c6a8233ddeadee8385f35d/04e84/generated-target-code-with-uaf-issue.webp 2508w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), `\n  `, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/c570069615c6a8233ddeadee8385f35d/04e84/generated-target-code-with-uaf-issue.webp\",\n        \"alt\": \"ValueStorage.append's Generated Target Code\",\n        \"title\": \"ValueStorage.append's Generated Target Code\",\n        \"width\": 712,\n        \"height\": 543,\n        \"loading\": \"lazy\"\n      }\n    }))), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            ValueStorage.append's Generated Target Code\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Examining the Swift standard library reveals that the implementation\nactually updates elements count and inserts new elements by accessing the\nproperty `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `_buffer`), ` on `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `self`), `, not an existing old buffer variable. The\nSwift compiler incorrectly eliminated the re-acquisition of the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `_buffer`), `\nobject in the target code.`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `// Implementation in the Standard Library\n@inlinable\n@_semantics(\"array.mutate_unknown\")\n@_effects(notEscaping self.**)\ninternal mutating func _appendElementAssumeUniqueAndCapacity(\n  _ oldCount: Int,\n  newElement: __owned Element\n) {\n  // Using \\`_buffer\\` of \\`self\\`\n  _buffer.mutableCount = oldCount &+ 1\n  // Using \\`_buffer\\` of \\`self\\`\n  (_buffer.mutableFirstElementAddress + oldCount).initialize(to: newElement)\n}\n\n// An imaginary implementation that might produce the generated target code\n@inlinable\n@_semantics(\"array.mutate_unknown\")\n@_effects(notEscaping self.**)\ninternal mutating func _appendElementAssumeUniqueAndCapacity(\n    _ oldCount: Int,\n    newElement: __owned Element\n    // Explicitly reusing the old buffer\n    oldBufer: _Buffer\n) {\n    // Using \\`oldBuffer\\`\n    oldBufer.mutableCount = oldCount &+ 1\n    (oldBufer.mutableFirstElementAddress + oldCount).initialize(to: newElement)\n}\n`)), React.createElement(MDXTag, {\n      name: \"h2\",\n      components: components\n    }, `Why the Swift Compiler Deleted the Code`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `By examining the intermediate compilation products, we can find the\ninitial miscompilation in the \"optimized SIL\" output, which can be\nobtained by adding the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `-emit-sil`), ` argument when invoking `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `swiftc`), `.\nComparing the optimized SIL of programs using `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `AutoreleasingUnsafeMutablePointer`), `\n(left side) and `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `UnsafeMutablePointer`), ` (right side) revealed that with\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `AutoreleasingUnsafeMutablePointer`), `, a critical `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `load`), ` of the array\nstorage was missing.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/69f84a2517263e76b6cf469c762d60a7/81220/comparing-crashing-non-crashing-sil.webp\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"picture\",\n      components: components,\n      parentName: \"a\"\n    }, `\n  `, React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/69f84a2517263e76b6cf469c762d60a7/0cc25/comparing-crashing-non-crashing-sil.png\",\n        \"srcSet\": [\"/static/69f84a2517263e76b6cf469c762d60a7/5116e/comparing-crashing-non-crashing-sil.png 178w\", \"/static/69f84a2517263e76b6cf469c762d60a7/92f55/comparing-crashing-non-crashing-sil.png 356w\", \"/static/69f84a2517263e76b6cf469c762d60a7/0cc25/comparing-crashing-non-crashing-sil.png 712w\", \"/static/69f84a2517263e76b6cf469c762d60a7/7ae06/comparing-crashing-non-crashing-sil.png 1068w\", \"/static/69f84a2517263e76b6cf469c762d60a7/eee47/comparing-crashing-non-crashing-sil.png 1424w\", \"/static/69f84a2517263e76b6cf469c762d60a7/38407/comparing-crashing-non-crashing-sil.png 2136w\", \"/static/69f84a2517263e76b6cf469c762d60a7/de5ec/comparing-crashing-non-crashing-sil.png 5120w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/69f84a2517263e76b6cf469c762d60a7/690c8/comparing-crashing-non-crashing-sil.webp\",\n        \"srcSet\": [\"/static/69f84a2517263e76b6cf469c762d60a7/25c8a/comparing-crashing-non-crashing-sil.webp 178w\", \"/static/69f84a2517263e76b6cf469c762d60a7/60698/comparing-crashing-non-crashing-sil.webp 356w\", \"/static/69f84a2517263e76b6cf469c762d60a7/690c8/comparing-crashing-non-crashing-sil.webp 712w\", \"/static/69f84a2517263e76b6cf469c762d60a7/d7e52/comparing-crashing-non-crashing-sil.webp 1068w\", \"/static/69f84a2517263e76b6cf469c762d60a7/456ef/comparing-crashing-non-crashing-sil.webp 1424w\", \"/static/69f84a2517263e76b6cf469c762d60a7/2a654/comparing-crashing-non-crashing-sil.webp 2136w\", \"/static/69f84a2517263e76b6cf469c762d60a7/81220/comparing-crashing-non-crashing-sil.webp 5120w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), `\n  `, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/69f84a2517263e76b6cf469c762d60a7/81220/comparing-crashing-non-crashing-sil.webp\",\n        \"alt\": \"Comparing Crashing and Non-Crashing SIL\",\n        \"title\": \"Comparing Crashing and Non-Crashing SIL\",\n        \"width\": 712,\n        \"height\": 401,\n        \"loading\": \"lazy\"\n      }\n    }))), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            Comparing Crashing and Non-Crashing SIL\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `To identify which compiler pass removed this `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `load`), `, we can use the\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `-Xllvm`), ` argument to enable debug prints in the compiler. Specifically,\nwe can use `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `--sil-print-function`), ` to make the compiler print the SIL of the\nspecified function whenever a pass modified its contents:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-shell\"\n      }\n    }, `swiftc YOUR_SWIFT_SOURCE.swift -Osize \\\\\n    -Xllvm '--sil-print-function=$MangledSwiftFunctionName'\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The key logs from this analysis can be summarized as:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-sil\"\n      }\n    }, `  *** SIL function after  #10338, stage MidLevel,Function, pass 37: CSE (cse)\n// closure #1 in ValueStorage.append(_:)\n...\nsil private @$s8Crashing12ValueStorageC6appendyySiFySAyAA4Data33_A856358C389441A2F6EA224BB743344FLLCGXEfU_ : $@convention(thin) @substituted <τ_0_0> (AutoreleasingUnsafeMutablePointer<Data>, Int) -> (@out τ_0_0, @error any Error) for <()> {\n...\nbb3(%17 : $Optional<AnyObject>):\n    ...\n    %26 = load %25 : $*Builtin.BridgeObject\n    ...\n  %35 = function_ref @$sSa36_reserveCapacityAssumingUniqueBuffer8oldCountySi_tFSi_Tg5 : $@convention(method) (Int, @inout Array<Int>) -> () // user: %36\n  %36 = apply %35(%34, %20) : $@convention(method) (Int, @inout Array<Int>) -> ()\n  ...\n  // The load instruction still exists\n  %42 = load %25 : $*Builtin.BridgeObject\n  %43 = unchecked_ref_cast %42 : $Builtin.BridgeObject to $__ContiguousArrayStorageBase\n  %44 = ref_element_addr %43 : $__ContiguousArrayStorageBase, #__ContiguousArrayStorageBase.countAndCapacity\n  %45 = struct_element_addr %44 : $*_ArrayBody, #_ArrayBody._storage\n  %46 = struct_element_addr %45 : $*_SwiftArrayBodyStorage, #_SwiftArrayBodyStorage.count\n  store %41 to %46 : $*Int\n\n  *** SIL function after  #10339, stage MidLevel,Function, pass 38: RedundantLoadElimination (redundant-load-elimination)\n// closure #1 in ValueStorage.append(_:)\n...\nsil private @$s8Crashing12ValueStorageC6appendyySiFySAyAA4Data33_A856358C389441A2F6EA224BB743344FLLCGXEfU_ : $@convention(thin) @substituted <τ_0_0> (AutoreleasingUnsafeMutablePointer<Data>, Int) -> (@out τ_0_0, @error any Error) for <()> {\n...\nbb3(%17 : $Optional<AnyObject>):\n    ...\n    %26 = load %25 : $*Builtin.BridgeObject\n    ...\n  %35 = function_ref @$sSa36_reserveCapacityAssumingUniqueBuffer8oldCountySi_tFSi_Tg5 : $@convention(method) (Int, @inout Array<Int>) -> ()\n  %36 = apply %35(%34, %20) : $@convention(method) (Int, @inout Array<Int>) -> ()\n  ...\n  // The load instruction was eliminated\n  %42 = unchecked_ref_cast %26 : $Builtin.BridgeObject to $__ContiguousArrayStorageBase\n  %43 = ref_element_addr %42 : $__ContiguousArrayStorageBase, #__ContiguousArrayStorageBase.countAndCapacity\n  %44 = struct_element_addr %43 : $*_ArrayBody, #_ArrayBody._storage\n  %45 = struct_element_addr %44 : $*_SwiftArrayBodyStorage, #_SwiftArrayBodyStorage.count\n  store %41 to %45 : $*Int\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `From these logs, we can clearly see that the \"redundant load elimination\"\n(RLE) pass deleted the following `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `load`), ` instruction:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-sil\"\n      }\n    }, `%42 = load %25 : $*Builtin.BridgeObject\n`)), React.createElement(MDXTag, {\n      name: \"h2\",\n      components: components\n    }, `Reasoning the Fix Solution`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `To develop a fix, we first needed to understand RLE. This pass optimizes\ncode by eliminating redundant \"get and set\" operations for both virtual\nand real registers. Consider this example with virtual registers:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-sil\"\n      }\n    }, `%1 = load %x\n%2 = store %1\n%3 = load %2\nreturn %3\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `An optimized equivalent would simply return `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `%1`), ` immediately, as `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `%2`), ` is\njust an intermediate result. This is a case RLE should handle correctly.\nLet's call this case 1.`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-sil\"\n      }\n    }, `%1 = load %x\nreturn %1\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, consider this more complex case:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-sil\"\n      }\n    }, `%1 = load %x\ncall print(%1)\ncall Foo(%x)\n%3 = load %x // Can we eliminate this line?\nreturn %3\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Here, elimination depends on whether `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Foo`), ` modifies the contents of `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `%x`), `.\nIf it does, we cannot directly return `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `%1`), ` because `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `%3 = load %x`), ` loads\nthe updated contents. Let's call this case 2.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Now let’s examine how the Swift compiler implements redundant load\nelimination. The main entry point resides in `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `RedundantLoadElimination.swift`), `:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `let redundantLoadElimination = FunctionPass(name: \"redundant-load-elimination\") {\n    (function: Function, context: FunctionPassContext) in\n  eliminateRedundantLoads(in: function, ignoreArrays: false, context)\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Follow the code from this entry point, we can find that the algorithm\ndiffers from classic RLE approaches:`), React.createElement(MDXTag, {\n      name: \"ol\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ol\"\n    }, `It iterates backward through instructions in each reverse-order basic\nblock to find all `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `load`), ` instructions`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ol\"\n    }, `For each `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `load`), `, it examines prior instructions to find:`, React.createElement(MDXTag, {\n      name: \"ul\",\n      components: components,\n      parentName: \"li\"\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Available `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `store`), ` instructions for optimization (like the first case)`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Available `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `load`), ` instructions for optimization (like the second case)\nwhen no functions with side effects to the address were called between\nloads`))), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ol\"\n    }, `A complexity budget limits instruction scanning for each load`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Comparing the detailed behaviors of RLE with `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `AutoreleasingUnsafeMutablePointer`), `\nand `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `UnsafeMutablePointer`), `, we can get the following log:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-text\"\n      }\n    }, `// AutoreleasingUnsafeMutablePointer\neliminating redundant loads in function: $s8Crashing12ValueStorageC6appendyySiFySAyAA4Data33_A856358C389441A2F6EA224BB743344FLLCGXEfU_\n...\nscanning prior instructions for the load:   %42 = load %25 : $*Builtin.BridgeObject         // users: %52, %43\n...\nvisiting instruction:   %36 = apply %35(%34, %20) : $@convention(method) (Int, @inout Array<Int>) -> ()\ntransparent\n// %35 = function_ref @$sSa36_reserveCapacityAssumingUniqueBuffer8oldCountySi_tFSi_Tg5 : $@convention(method) (Int, @inout Array<Int>) -> () // user: %36\n`)), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-text\"\n      }\n    }, `// UnsafeMutablePointer\neliminating redundant loads in function: $s11NonCrashing12ValueStorageC6appendyySiF\n...\nscanning prior instructions for the load:   %35 = load %18 : $*Builtin.BridgeObject         // users: %45, %36\n...\nvisiting instruction:   %29 = apply %28(%27, %12) : $@convention(method) (Int, @inout Array<Int>) -> ()\noverwritten\n// %28 = function_ref @$sSa36_reserveCapacityAssumingUniqueBuffer8oldCountySi_tFSi_Tg5 : $@convention(method) (Int, @inout Array<Int>) -> () // user: %36\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `This reveals that:`), React.createElement(MDXTag, {\n      name: \"ul\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `With `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `AutoreleasingUnsafeMutablePointer`), `, RLE considered the array\nreallocation function \"transparent\" to the address of the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `load`), `\ninstruction's operand, enabling elimination`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `With `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `UnsafeMutablePointer`), `, RLE correctly recognized that the function\noverwrites the address, preventing elimination`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `For case 2 scenarios, the Swift 6 algorithm checks all prior instructions\nof the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `load`), ` to determine side effects of function calls between the\nsource of the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `load`), `'s operand and the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `load`), ` instruction itself.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/ee70b9cfda3703ee4301c0cf91291a56/98878/side-effects-analysis.webp\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"picture\",\n      components: components,\n      parentName: \"a\"\n    }, `\n  `, React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/ee70b9cfda3703ee4301c0cf91291a56/0cc25/side-effects-analysis.png\",\n        \"srcSet\": [\"/static/ee70b9cfda3703ee4301c0cf91291a56/5116e/side-effects-analysis.png 178w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/92f55/side-effects-analysis.png 356w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/0cc25/side-effects-analysis.png 712w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/7ae06/side-effects-analysis.png 1068w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/eee47/side-effects-analysis.png 1424w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/38407/side-effects-analysis.png 2136w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/fd91f/side-effects-analysis.png 3136w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/ee70b9cfda3703ee4301c0cf91291a56/690c8/side-effects-analysis.webp\",\n        \"srcSet\": [\"/static/ee70b9cfda3703ee4301c0cf91291a56/25c8a/side-effects-analysis.webp 178w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/60698/side-effects-analysis.webp 356w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/690c8/side-effects-analysis.webp 712w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/d7e52/side-effects-analysis.webp 1068w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/456ef/side-effects-analysis.webp 1424w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/2a654/side-effects-analysis.webp 2136w\", \"/static/ee70b9cfda3703ee4301c0cf91291a56/98878/side-effects-analysis.webp 3136w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), `\n  `, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/ee70b9cfda3703ee4301c0cf91291a56/98878/side-effects-analysis.webp\",\n        \"alt\": \"Side Effects Analysis\",\n        \"title\": \"Side Effects Analysis\",\n        \"width\": 712,\n        \"height\": 329,\n        \"loading\": \"lazy\"\n      }\n    }))), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            Side Effects Analysis\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The key insight is that the Swift compiler only evaluates function side\neffects when the ultimate source of the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `load`), `'s operand has an unknown\nescaping result. By setting breakpoints in the following function located\nin `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `AliasAnalysis.swift`), `, I found the critical difference between the two\npointer types:`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/4e8fe/get-apply-effect-function.webp\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"picture\",\n      components: components,\n      parentName: \"a\"\n    }, `\n  `, React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/0cc25/get-apply-effect-function.png\",\n        \"srcSet\": [\"/static/fe416bc81e8c76587339dcc9fb2d8ea6/5116e/get-apply-effect-function.png 178w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/92f55/get-apply-effect-function.png 356w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/0cc25/get-apply-effect-function.png 712w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/7ae06/get-apply-effect-function.png 1068w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/eee47/get-apply-effect-function.png 1424w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/38407/get-apply-effect-function.png 2136w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/b686c/get-apply-effect-function.png 2222w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/690c8/get-apply-effect-function.webp\",\n        \"srcSet\": [\"/static/fe416bc81e8c76587339dcc9fb2d8ea6/25c8a/get-apply-effect-function.webp 178w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/60698/get-apply-effect-function.webp 356w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/690c8/get-apply-effect-function.webp 712w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/d7e52/get-apply-effect-function.webp 1068w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/456ef/get-apply-effect-function.webp 1424w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/2a654/get-apply-effect-function.webp 2136w\", \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/4e8fe/get-apply-effect-function.webp 2222w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), `\n  `, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/fe416bc81e8c76587339dcc9fb2d8ea6/4e8fe/get-apply-effect-function.webp\",\n        \"alt\": \"The getApplyEffect Function\",\n        \"title\": \"The getApplyEffect Function\",\n        \"width\": 712,\n        \"height\": 305,\n        \"loading\": \"lazy\"\n      }\n    }))), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            The getApplyEffect Function\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"ul\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `With `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `AutoreleasingUnsafeMutablePointer`), `, the compiler checks if the\ndefinition source of the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `load`), ` instruction's operand is escaping. When\ndetermined not to be escaping, the compiler incorrectly assumes the\nfunction has no side effects (line 376).`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `With `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `UnsafeMutablePointer`), `, the compiler retrieves the global\nside-effects of the array reallocation function (likely from the\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `@_effects`), ` attribute. Only functions marked `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `readOnly`), ` or `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `readNone`), `\nare considered side effect-free.) (line 381).`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Further investigation led to the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `visit`), ` function at line 371, which\nperforms escape analysis on the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `load`), ` instruction's operand. The\nfollowing diagram illustrates this process:`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/807e9716c55071e6b412f2f65df84dab/a216e/escape-analaysis-1.png\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"a\",\n      props: {\n        \"title\": \"Escape Analysis 1\",\n        \"alt\": \"Escape Analysis 1\",\n        \"src\": \"/static/807e9716c55071e6b412f2f65df84dab/a216e/escape-analaysis-1.png\",\n        \"srcSet\": [\"/static/807e9716c55071e6b412f2f65df84dab/a216e/escape-analaysis-1.png 1x\", \"/static/54ed03a518d4a59e41b6cb63d4a06782/92d5f/escape-analaysis-1%402x.png 2x\", \"/static/2462b38e40b620fe5d2bfcdee027dfb0/0c673/escape-analaysis-1%403x.png 3x\"],\n        \"loading\": \"lazy\"\n      }\n    })), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            Escape Analysis 1\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `This is the escape analysis process:`), React.createElement(MDXTag, {\n      name: \"ol\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ol\"\n    }, `Walks up the use-def chain to analyze escaping behavior`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ol\"\n    }, `Constructs a path representing how to derive the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `load`), ` instruction's\noperand from its definition point in each step`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The complexity arises with `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `AutoreleasingUnsafeMutablePointer`), `'s `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `pointee`), `\ngetter implementation, which presents a non-trivial case for this escaping\nanalysis:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `@frozen\npublic struct AutoreleasingUnsafeMutablePointer<Pointee /* TODO : class */>\n  :  _Pointer {\n  \n  @inlinable\n  public var pointee: Pointee {\n    @_transparent get {\n      // The memory addressed by this pointer contains a non-owning reference,\n      // therefore we *must not* point an \\`UnsafePointer<AnyObject>\\` to\n      // it---otherwise we would allow the compiler to assume it has a +1\n      // refcount, enabling some optimizations that wouldn't be valid.\n      //\n      // Instead, we need to load the pointee as a +0 unmanaged reference. For\n      // an extra twist, \\`Pointee\\` is allowed (but not required) to be an\n      // optional type, so we actually need to load it as an optional, and\n      // explicitly handle the nil case.\n      let unmanaged =\n        UnsafePointer<Optional<Unmanaged<AnyObject>>>(_rawValue).pointee\n      return _unsafeReferenceCast(\n        unmanaged?.takeUnretainedValue(),\n        to: Pointee.self)\n    }\n    ...\n  }\n  \n  ...\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The implementation details reveal that `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `AutoreleasingUnsafeMutablePointer`), `\nmust cast references from `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional<Unmanaged<AnyObject>>`), ` to the\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Pointee`), ` type to maintain +0 reference counting. This casting is\nperformed through the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `_unsafeReferenceCast`), ` function:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `@_transparent\n@unsafe\npublic func _unsafeReferenceCast<T, U>(_ x: T, to: U.Type) -> U {\n  return Builtin.castReference(x)\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The compiler translates this to the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Builtin.castReference`), ` function,\nultimately represented as an `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `unchecked_ref_cast`), ` instruction in SIL:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-sil\"\n      }\n    }, `%y = unchecked_ref_cast %x : $SourceSILType to $DesintationSILType\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The problem arises because `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `AutoreleasingUnsafeMutablePointer`), ` introduces\ncases where `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `$SourceSILType`), ` or `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `$DesintationSILType`), ` may be `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional`), `\ntypes:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-sil\"\n      }\n    }, `%y = unchecked_ref_cast %x : $Optional<AnyObject> to $Data\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `This instruction can cast between `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional`), ` and non-`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional`), ` types,\neffectively wrapping or unwrapping values. Since escape analysis walks the\nuse-def chain with a path that must strictly reflect how to derive the\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `load`), ` operand from its definition point, these implicit `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional`), `\nconversions create path mismatches, as illustrated:`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/f45ea8c289ffcfca0279317186798cfa/a216e/escape-analaysis-2.png\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"a\",\n      props: {\n        \"title\": \"Escape Analysis 2\",\n        \"alt\": \"Escape Analysis 2\",\n        \"src\": \"/static/f45ea8c289ffcfca0279317186798cfa/a216e/escape-analaysis-2.png\",\n        \"srcSet\": [\"/static/f45ea8c289ffcfca0279317186798cfa/a216e/escape-analaysis-2.png 1x\", \"/static/fd7aa821b52b651595f05efa15865fd9/92d5f/escape-analaysis-2%402x.png 2x\", \"/static/d6a3db510965604678cf86e72cfabf04/0c673/escape-analaysis-2%403x.png 3x\"],\n        \"loading\": \"lazy\"\n      }\n    })), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            Escape Analysis 2\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `This hypothesis is confirmed by examining the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `walkUpDefault`), ` function in\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `WalkUtils.swift`), `, which handles various instruction types during the\nwalk-up but lacks proper handling for `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional`), ` conversions in\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `unchecked_ref_cast`), `:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `public mutating func walkUpDefault(value def: Value, path: Path) -> WalkResult {\n    switch def {\n    ...\n    case let urc as UncheckedRefCastInst:\n      if urc.type.isClassExistential || urc.fromInstance.type.isClassExistential {\n        // Sometimes \\`unchecked_ref_cast\\` is misused to cast between AnyObject and a class (instead of\n        // init_existential_ref and open_existential_ref).\n        // We need to ignore this because otherwise the path wouldn't contain the right \\`existential\\` field kind.\n        return rootDef(value: urc, path: path)\n      }\n      return walkUp(value: urc.fromInstance, path: path)\n    ...\n    }\n}\n`)), React.createElement(MDXTag, {\n      name: \"h2\",\n      components: components\n    }, `Fix Solution`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The solution is to account for `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional`), ` and non-`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional`), ` casting in\nthe use-def chain walk-up:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `public mutating func walkUpDefault(value def: Value, path: Path) -> WalkResult {\n    switch def {\n    ...\n    case let urc as UncheckedRefCastInst:\n      if urc.type.isClassExistential || urc.fromInstance.type.isClassExistential {\n        // Sometimes \\`unchecked_ref_cast\\` is misused to cast between AnyObject and a class (instead of\n        // init_existential_ref and open_existential_ref).\n        // We need to ignore this because otherwise the path wouldn't contain the right \\`existential\\` field kind.\n        return rootDef(value: urc, path: path)\n      }\n      switch (urc.type.isOptional, urc.fromInstance.type.isOptional) {\n        case (true, false):\n          if let path = path.popIfMatches(.enumCase, index: 1) {\n            return walkUp(value: urc.fromInstance, path: path)\n          } else {\n            return unmatchedPath(value: urc.fromInstance, path: path)\n          }\n        case (false, true):\n          return walkUp(value: urc.fromInstance, path: path.push(.enumCase, index: 1))\n        default:\n          return walkUp(value: urc.fromInstance, path: path)\n      }\n    ...\n    }\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The diagram below illustrates how this fix accommodates `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional`), ` and\nnon-`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional`), ` type conversions during escape analysis. When the escape\nanalysis process encounters an `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `unchecked_ref_cast`), ` between `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional`), ` and\nnon-`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Optional`), ` types, the fix ensures proper path transformations by\nadjusting the path to account for enum case differences.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/f253b0b736cb4a7a4ee741efae3b34bb/a216e/escape-analaysis-3.png\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"a\",\n      props: {\n        \"title\": \"Escape Analysis 3\",\n        \"alt\": \"Escape Analysis 3.png\",\n        \"src\": \"/static/f253b0b736cb4a7a4ee741efae3b34bb/a216e/escape-analaysis-3.png\",\n        \"srcSet\": [\"/static/f253b0b736cb4a7a4ee741efae3b34bb/a216e/escape-analaysis-3.png 1x\", \"/static/da779ffdeafaa6a0513f4cf5e4f6dcaa/92d5f/escape-analaysis-3%402x.png 2x\", \"/static/ea0d911803efb5a35e1ae378c6fd9c37/0c673/escape-analaysis-3%403x.png 3x\"],\n        \"loading\": \"lazy\"\n      }\n    })), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            Escape Analysis 3\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `A similar change is needed in the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `walkDownDefault`), ` function for def-use\nchain analysis:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `public mutating func walkDownDefault(value operand: Operand, path: Path) -> WalkResult {\n    let instruction = operand.instruction\n    switch instruction {\n        ...\n        case let urc as UncheckedRefCastInst:\n      if urc.type.isClassExistential || urc.fromInstance.type.isClassExistential {\n        // Sometimes \\`unchecked_ref_cast\\` is misused to cast between AnyObject and a class (instead of\n        // init_existential_ref and open_existential_ref).\n        // We need to ignore this because otherwise the path wouldn't contain the right \\`existential\\` field kind.\n        return leafUse(value: operand, path: path)\n      }\n      switch (urc.type.isOptional, urc.fromInstance.type.isOptional) {\n        case (true, false):\n          return walkDownUses(ofValue: operand, path: path.push(.enumCase, index: 1))\n        case (false, true):\n          if let path = path.popIfMatches(.enumCase, index: 1) {\n            return walkDownUses(ofValue: operand, path: path)\n          } else {\n            return unmatchedPath(value: operand, path: path)\n          }\n        default:\n          return walkDownUses(ofValue: operand, path: path)\n      }\n      ...\n    }\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `After implementing this fix, compiling the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `AutoreleasingUnsafeMutablePointer`), `\ncode produces logs showing that RLE correctly recognizes potential side\neffects:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-text\"\n      }\n    }, `eliminating redundant loads in function: $s8Crashing12ValueStorageC6appendyySiF\nscanning prior instructions for the load:   %45 = load %28 : $*Builtin.BridgeObject         // users: %55, %46\n...\nvisiting instruction:   %39 = apply %38(%37, %23) : $@convention(method) (Int, @inout Array<Int>) -> ()\noverwritten\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The optimized SIL now retains the critical `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `load`), ` instruction after the\narray reallocation:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-sil\"\n      }\n    }, `// ValueStorage.append(_:)\n// Isolation: unspecified\nsil [noinline] @$s8Crashing12ValueStorageC6appendyySiF : $@convention(method) (Int, @guaranteed ValueStorage) -> () {\n[%1: noescape, read c*.v**, write c*.v**, copy c*.v**, destroy c*.v**]\n[global: read,write,copy,destroy,allocate,deinit_barrier]\n\n...\n\nbb3(%20 : $Optional<AnyObject>):\n    ...\n  // function_ref specialized Array._reserveCapacityAssumingUniqueBuffer(oldCount:)\n  %38 = function_ref @$sSa36_reserveCapacityAssumingUniqueBuffer8oldCountySi_tFSi_Tg5 : $@convention(method) (Int, @inout Array<Int>) -> ()\n  %39 = apply %38(%37, %23) : $@convention(method) (Int, @inout Array<Int>) -> ()\n  %45 = load %28 : $*Builtin.BridgeObject\n  ...\n} // end sil function '$s8Crashing12ValueStorageC6appendyySiF'\n`)), React.createElement(MDXTag, {\n      name: \"h2\",\n      components: components\n    }, `Tips for Debugging the Swift Compiler`), React.createElement(MDXTag, {\n      name: \"h3\",\n      components: components\n    }, `Getting the Intermediate Products of the Swift Compiler`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `To examine the Swift compiler's intermediate representations at each\ncompilation stage:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-shell\"\n      }\n    }, `swiftc YourSwiftSource.swift -Osize -emit-silgen > YourSwiftSource.silgen.sil # Generating raw SIL\nswiftc YourSwiftSource.swift -Osize -emit-sil > YourSwiftSource.sil.sil # Generating optimized SIL\nswiftc YourSwiftSource.swift -Osize -emit-irgen > YourSwiftSource.irgen.ll # Generating raw LLVM IR\nswiftc YourSwiftSource.swift -Osize -emit-ir > YourSwiftSource.ir.ll # Generating optimized LLVM IR\n`)), React.createElement(MDXTag, {\n      name: \"h3\",\n      components: components\n    }, `Leveraging LLVM Pass-Through Arguments`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `LLVM provides numerous pass-through arguments that can be used with Swift:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-shell\"\n      }\n    }, `# Print SIL changes for the specified function\nswiftc -Xllvm '--sil-print-function=$MangledSwiftFunctionName'\n# Print the name of each SIL pass before it runs\nswiftc -Xllvm '--sil-print-pass-name=pass-name'\n# Print functions that are inlined into other functions\nswiftc -Xllvm '--sil-print-inlining-callee=true'\n`)), React.createElement(MDXTag, {\n      name: \"h3\",\n      components: components\n    }, `Building the Swift Compiler`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `What to notice is that, in this post, we are debugging the detailed\nbehaviors of the compiler, but the Swift programming language is bundled\nwith the standard library. Since the issue is coupled with the inlining\nof the function `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Array.append`), `, we shall build a debug version of the\ncompiler and a release version of the standard library to ensure the\ninlining cost of `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `Array.append`), ` as low as possible. This could be done\nwith the following command:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-shell\"\n      }\n    }, `utils/build-script --no-swift-stdlib-assertions \\\\\n    --skip-ios --skip-tvos --skip-watchos --skip-build-benchmarks\n`)), React.createElement(MDXTag, {\n      name: \"h3\",\n      components: components\n    }, `Syntax Highlights for SIL and LLVM IR`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `You could search \"WeZZard\" in the extension market of VS Code (or Cursor) to get the syntax highlights for SIL in relevant IDE.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `You could find the \"LLVM\" extension by Ingvar Stepanyan in the extension market of VS Code (or Cursor) to get the syntax highlights for LLVM IR in relevant IDE.`));\n  }\n\n}\nMDXContent.isMDXComponent = true;","scope":""},"headings":[{"value":"Investigating the Primary Scene of the Crash","depth":2},{"value":"Why the Swift Compiler Deleted the Code","depth":2},{"value":"Reasoning the Fix Solution","depth":2},{"value":"Fix Solution","depth":2},{"value":"Tips for Debugging the Swift Compiler","depth":2},{"value":"Getting the Intermediate Products of the Swift Compiler","depth":3},{"value":"Leveraging LLVM Pass-Through Arguments","depth":3},{"value":"Building the Swift Compiler","depth":3},{"value":"Syntax Highlights for SIL and LLVM IR","depth":3}]}}},"earlierPostExcerpt":{"slug":"/post/2023/08/swift-macro-revisited-traps-and-pitfalls-1034","title":"Swift Macro: Revisited - Traps and Pitfalls","subtitle":"","createdTime":"2023-08-10T00:00:00.000Z","tags":["Swift","Macro"],"category":"Programming","file":{"childMdx":{"excerpt":"In the previous post, we learned the strengths and the essence that\nuniquely define the Swift Macro. The examples in that post work so far so\ngood. However, can we be confident and bold, implementing any Swift macros\nwe want now? No. The features that bring Swift Macro advantages also introduce…"}}},"laterPostExcerpt":{"slug":"/post/2025/08/the-cupertino-ghost-in-the-machine-9ee3","title":"The Cupertino Ghost in the Machine: An Analysis of Xcode's New AI Assistant","subtitle":"","createdTime":"2025-08-31T00:00:00.000Z","tags":["AI","Developer Tools","Xcode"],"category":"Programming","file":{"childMdx":{"excerpt":"My journey into the internals of Xcode 26’s new AI assistant began not with a bug, but with a feature so persistent it felt like a personality. The animations and design of Xcode have never been better, a testament to Apple's polish. Yet, interacting with the new intelligence features felt…"}}}},"pageContext":{"postId":"a1a051bd-7086-59a6-83e9-167b8d7512f2","earlierPostId":"5dad251c-7c9d-519a-89a1-db4f32ea4fcb","laterPostId":"62c9b374-86f3-5a74-b008-dd3b3ed47e67"}}