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

Combine string constants in combine_strings() #6717

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/IROperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ Expr combine_strings(const std::vector<Expr> &args) {
}
}

// Now combine all adjacent string literals, which is
// useful to reduce emitted code size when printing
size_t i = 0;
while (i < strings.size() - 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a for loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, except that in the 'continue' case we'd need to decrement i, since we want to stay on the same element and the for loop would increment it, and for a size_t that could underflow on the first element, and even if the increment 'fixes' it, it still feels icky and dangerous.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, I missed the continue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...we both missed the 'oops I will crash if passed an empty vector' bug :-(

const auto *cur_str = strings[i].as<Internal::StringImm>();
const auto *next_str = strings[i + 1].as<Internal::StringImm>();
if (cur_str && next_str) {
strings[i] = Internal::StringImm::make(cur_str->value + next_str->value);
strings.erase(strings.begin() + i + 1);
continue;
}
i++;
}

return stringify(strings);
}

Expand Down