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

#12405: Refactor to use strong types #46

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

patrickroberts
Copy link
Contributor

Ticket

tenstorrent/tt-metal#12405

Problem description

Aliases of integral types are being used interchangeably with the integral types, allowing for erroneous implicit conversions.

What's changed

Refactor to use enum class and make any intentional conversions explicit.

@patrickroberts patrickroberts marked this pull request as draft September 9, 2024 18:42
Comment on lines +15 to +34
namespace tt::umd {

enum class chip_id : int {
none = -1,
};

enum class ethernet_channel : int {};

struct eth_coord {
int x;
int y;
int rack;
int shelf;

// in C++20 this should be defined as:
// constexpr bool operator==(const eth_coord &other) const noexcept = default;
constexpr bool operator==(const eth_coord &other) const noexcept {
return (x == other.x and y == other.y and rack == other.rack and shelf == other.shelf);
}
};
Copy link
Member

Choose a reason for hiding this comment

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

Can establishing of a namespace for these structs come as another PR, where all code consistently use it from within that namespace and not global?

This pattern is widespread in the codebase today and I highly discourage it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's why the aliases are kept in place. I have not changed any existing usage of the aliases that were already there.

Copy link
Member

@ayerofieiev-tt ayerofieiev-tt left a comment

Choose a reason for hiding this comment

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

overall lgtm 👍


using chip_id_t = tt::umd::chip_id;
using ethernet_channel_t = tt::umd::ethernet_channel;
using eth_coord_t = tt::umd::eth_coord;
Copy link
Contributor

Choose a reason for hiding this comment

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

if this is only merged to metal-main then you can potentially get rid of these *_t?


namespace tt::umd {

enum class chip_id : int {
Copy link
Contributor

Choose a reason for hiding this comment

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

Having this as an enum can be a bit confusing, since you're using it such that any int value can be assigned to it, and it doesn't necessarily have a corresponding enum constant. However, I do see the value in using an explicit name for a type to have a clearer requirement and expectations over what is expected from an interface.

the eth_coord clearly benefits in readability from being defined as a struct, but whats the reasoning behind changing using chip_id_t to enum class?
You would then have define a constexpr int c_invalid_chip_id = -1;

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah actually, I saw the attached tt-metal issue, this ensures explicit conversion. And enum class is a different thing than enum :) LGTM

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.

4 participants