# How to efficiently compute ordering of two SILInstructions?

**URL:** <https://forums.swift.org/t/how-to-efficiently-compute-ordering-of-two-silinstructions/77052>\
**Category:** Compiler\
**Tags:** sil\
**Created:** [January 7, 2025, 1:14pm UTC](https://forums.swift.org/t/how-to-efficiently-compute-ordering-of-two-silinstructions/77052 "2025-01-07T13:14:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamieQ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jamieq/32/5117_2.png) [@jamieQ](https://forums.swift.org/u/jamieQ)\
**Post date:** [January 7, 2025, 1:14pm UTC](https://forums.swift.org/t/how-to-efficiently-compute-ordering-of-two-silinstructions/77052/1 "2025-01-07T13:14:29Z")

</div>

i've been looking into some of the proposed improvements for DI performance outlined [here](https://forums.swift.org/t/definite-initialization-accidentally-or-essentially-quadratic/76298/3), but have run into an issue i don't quite understand how to handle. specifically, if i have two `SILInstruction`s that are known to have the same parent, is there a way to determine their ordering without walking the underlying list of instructions in the block? i've sort of assumed this should be possible since when i look at SILGen output, there are increasing ids for the instruction values\[1\], but i haven't determined if/how that information can be used for this purpose.

* * *

1. not sure if this is the correct terminology, but the things like `%2`, `%3`, etc

---

<div class="post-metadata">

**Author:** ![jamieQ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jamieq/32/5117_2.png) [@jamieQ](https://forums.swift.org/u/jamieQ)\
**Post date:** [January 11, 2025, 1:56pm UTC](https://forums.swift.org/t/how-to-efficiently-compute-ordering-of-two-silinstructions/77052/2 "2025-01-11T13:56:13Z")

</div>

it seems i may have been mistaken in my assumption that there is a way to do this based on the fact that there are increasing 'identifier' values printed in the `SILGen` output. i was informed that this is a byproduct of [the logic](https://github.com/swiftlang/swift/blob/c879e3f0aaab224f46cf2792ae11f5f9550dd6bb/lib/SIL/IR/SILPrinter.cpp#L4661-L4680) in `SILPrinter` which also does a traversal of all blocks/instructions to generate those values.

so, i will assume this isn't possible to do without some form of local cache derived from walking the instructions at least once.

---

<div class="post-metadata">

**Author:** ![jamieQ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jamieq/32/5117_2.png) [@jamieQ](https://forums.swift.org/u/jamieQ)\
**Post date:** [February 1, 2026, 2:41pm UTC](https://forums.swift.org/t/how-to-efficiently-compute-ordering-of-two-silinstructions/77052/3 "2026-02-01T14:41:26Z")

</div>

to resurrect & continue this soliloquy – it turns out [there is now a utility to effectively do this](https://github.com/swiftlang/swift/blob/edf1242d46e195fa09e1bf55391b054f7d67cfe1/include/swift/SIL/InstructionUtils.h#L266), which was [recently added](https://github.com/swiftlang/swift/pull/86781). it will still have to walk basic blocks once to compute the indices, but IIUC it stores them efficiently within the instruction itself without requiring a new field (correct me if i'm wrong). i note this primarily out of personal interest, but it may also be useful for posterity.
