-
Notifications
You must be signed in to change notification settings - Fork 269
Replace O(N) recursive sequence_map_inverse with O(1) pack expansion #3596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tenpercent
wants to merge
1
commit into
develop
Choose a base branch
from
mpodkory/recursive-to-pack-expansion
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+62
−20
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -525,31 +525,73 @@ struct is_valid_sequence_map : is_same<typename arithmetic_sequence_gen<0, SeqMa | |||||
| { | ||||||
| }; | ||||||
|
|
||||||
| template <typename SeqMap> | ||||||
| struct sequence_map_inverse | ||||||
| // Invert a permutation sequence: given X2Y = {a, b, c, ...}, compute Y2X where Y2X[X2Y[i]] = i | ||||||
| // Example: Sequence<2,0,1> (meaning pos0->2, pos1->0, pos2->1) inverts to Sequence<1,2,0> | ||||||
| // | ||||||
| // Why this implementation is faster to compile than recursive templates: | ||||||
| // | ||||||
| // The old recursive approach created a new template type for each element: | ||||||
| // sequence_map_inverse<Seq<2,0,1>> -> sequence_map_inverse<Seq<0,1>> -> | ||||||
| // sequence_map_inverse<Seq<1>> | ||||||
| // Each "->" is a new type the compiler must create, track, and manage. For N elements, that's | ||||||
| // N template types, each with overhead (name mangling, debug info, symbol table entries). | ||||||
| // | ||||||
| // This implementation uses a different strategy: | ||||||
| // 1. Store the sequence values in a regular array (ConstexprArray) | ||||||
| // 2. Use a normal for-loop (find_inverse) to search the array - runs at compile-time via constexpr | ||||||
| // 3. Use "..." pack expansion to call find_inverse once per position in a single expression | ||||||
| // | ||||||
| // The key insight: a constexpr for-loop compiles to ONE template, while a recursive template | ||||||
| // compiles to N templates. Both do N iterations of work, but the for-loop avoids creating | ||||||
| // N separate types. This reduced compilation time by ~10% on large builds. | ||||||
| namespace detail { | ||||||
| // TODO: Replace with std::array when HIPRTC supports it | ||||||
| // Simple array wrapper that works in constexpr context. Lets us convert the template parameter | ||||||
| // pack (Is...) into an indexable array, so find_inverse() can loop over it. | ||||||
| template <typename T, index_t N> | ||||||
| struct ConstexprArray | ||||||
| { | ||||||
| template <typename X2Y, typename WorkingY2X, index_t XBegin, index_t XRemain> | ||||||
| struct sequence_map_inverse_impl | ||||||
| { | ||||||
| static constexpr auto new_y2x = | ||||||
| WorkingY2X::Modify(X2Y::At(Number<XBegin>{}), Number<XBegin>{}); | ||||||
| T data[N]; | ||||||
|
|
||||||
| using type = | ||||||
| typename sequence_map_inverse_impl<X2Y, decltype(new_y2x), XBegin + 1, XRemain - 1>:: | ||||||
| type; | ||||||
| }; | ||||||
| constexpr const T& operator[](index_t i) const { return data[i]; } | ||||||
| }; | ||||||
| } // namespace detail | ||||||
|
|
||||||
| template <index_t... Is> | ||||||
| struct sequence_map_inverse<Sequence<Is...>> | ||||||
| { | ||||||
| private: | ||||||
| // Convert template parameters to array: Sequence<2,0,1> becomes values = {2,0,1} | ||||||
| static constexpr detail::ConstexprArray<index_t, sizeof...(Is)> values = {{Is...}}; | ||||||
|
|
||||||
| template <typename X2Y, typename WorkingY2X, index_t XBegin> | ||||||
| struct sequence_map_inverse_impl<X2Y, WorkingY2X, XBegin, 0> | ||||||
| // Given a target value, find which position contains it. | ||||||
| // Example: values={2,0,1}, find_inverse(1) returns 2 because values[2]==1 | ||||||
| // This is a regular for-loop, but runs at compile-time because it's constexpr. | ||||||
| static constexpr index_t find_inverse(index_t target) | ||||||
| { | ||||||
| using type = WorkingY2X; | ||||||
| }; | ||||||
| for(index_t i = 0; i < static_cast<index_t>(sizeof...(Is)); ++i) | ||||||
| { | ||||||
| if(values[i] == target) | ||||||
| return i; | ||||||
| } | ||||||
| return -1; // should not reach for valid permutation | ||||||
|
||||||
| return -1; // should not reach for valid permutation | |
| return static_cast<index_t>(-1); // should not reach for valid permutation |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'which' to 'witch' in the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting suggestion but nope