Skip to content
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

Experiment to improve code readability in CodegenXtensa #7471

Draft
wants to merge 2 commits into
base: xtensa-codegen
Choose a base branch
from

Conversation

steven-johnson
Copy link
Contributor

This is just a draft because (1) it's not complete, and (2) I'm not 100% sure this is the best improvement to make.

The problem I wanted to solve here was that CodeGenXtensa currently has a pervasive pattern of repeated-if-else code to choose the right type, and repeated (redundant) code inside each else clause.

I was hoping that something enabling a more switch-like chunk of code would be smaller, easier to read and reason about, and have less redundant chunks (e.g. calls to print_assignment() for every type clause which are identical except for the instruction name).

This takes the approach that we can use a map to make halide_type_t -> a bespoke enum; if the type is in the map, it means it's equivalent to is_native_xtensa_vector<>() returning true. This allows us to use switch statements in many places with only a slight overhead, but (maybe?) simpler-to-read code; when combined with judicious use of lambdas, we (hopefully?) get less redundant code.

That said... this still isn't quite as elegant / easy to read as I'd like; whether or not this is enough of an improvement to be worth finishing is a matter I'd like some other folks thoughts on.

Alternately, if someone has an even-better idea for handling all this, I'd love to hear anout it :-)

This is just a draft because (1) it's not complete, and (2) I'm not 100% sure this is the best improvement to make.

The problem I wanted to solve here was that CodeGenXtensa currently has a pervasive pattern of repeated-if-else code to choose the right type, and repeated (redundant) code inside each else clause.

I was hoping that something enabling a more `switch`-like chunk of code would be smaller, easier to read and reason about, and have less redundant chunks (e.g. calls to `print_assignment()` for every type clause which are identical except for the instruction name).

This takes the approach that we can use a map to make `halide_type_t` -> a bespoke enum; if the type is in the map, it means it's equivalent to `is_native_xtensa_vector<>()` returning true. This allows us to use `switch` statements in many places with only a slight overhead, but (maybe?) simpler-to-read code; when combined with judicious use of lambdas, we (hopefully?) get less redundant code.

That said... this still isn't quite as elegant / easy to read as I'd like; whether or not this is enough of an improvement to be worth finishing is a matter I'd like some other folks thoughts on.

Alternately, if someone has an even-better idea for handling all this, I'd love to hear anout it :-)
steven-johnson added a commit that referenced this pull request Apr 1, 2023
steven-johnson added a commit that referenced this pull request Apr 1, 2023
We currently re-create this map every time `print_xtensa_call()` is called, which is silly. Let's create it once per instance and keep it in a member variables. Also, an unordered_map is likely to be more efficient for our purposes here. (Harvested from #7471)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant