Skip to content

Revisit/replace priority_queue usage in sroa-hlsl #5701

Closed as not planned
Closed as not planned

Description

@tex3d

TL;DR
Can we get rid of problematic std::priority_queue usage, such as in SROA?

Description
std::priority_queue used in SROAGlobalAndAllocas in file lib/Transforms/Scalar/ScalarReplAggregatesHLSL.cpp does not have the same behavior across C++ implementations for items that compare equal.

Observed inconsistent ordering between platforms in this case has been "fixed" by comparing more properties in the comparison function. However, this fix is ugly, and may not be sufficient for all cases.

We should revisit this code and reconsider whether sorting by size is needed in the first place, and whether this is the right approach if so. Could there be a cleaner, portable, stable approach?

One suggestion was to "use an llvm::SmallVector<std::pair<uint64_t,Value*> > for the work list where the uint64_t is the size. After all the elements are added to the work list a std::stable_sort should give a consistent and implementation-independent ordering." However, items are added to the worklist at various points as it's processed, which could mean a lot of extra sorting/comparison work if not careful.

For context see PR: #5259

Activity

  1. python3kgae commented on Sep 13, 2023

    @python3kgae
    Contributor

    The reason to sort is to lowered more memcpy into replace src with dst instead of copy of all elements.

    If SROA on small struct first, when lower memcpy on the small struct, src/dst of the memcpy might be part of another big struct which need more check to make sure it is safe to replace. Now we only check the src has one use or dst has one define case.

  2. llvm-beanz commented on Sep 13, 2023

    @llvm-beanz
    Collaborator

    We can sort without using a priority queue. We could use a SmallVector of std::pair and sort with std::stable_sort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions