Skip to content

MLX_SWITCH macros to templates #2320

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

Merged
merged 5 commits into from
Jul 1, 2025
Merged

MLX_SWITCH macros to templates #2320

merged 5 commits into from
Jul 1, 2025

Conversation

angeloskath
Copy link
Member

@angeloskath angeloskath commented Jun 29, 2025

This changes all macros used to switch types or values to constexprs into templates.

The main benefit is that we will now have proper compiler errors instead of one huge line from the preprocessor macros. There is very little change to binary size. When we move to C++20 we can make the lambda get the type directly so we won't have to call MLX_GET_TYPE on the argument.

@zcbenz let me know if you can think of a reason not to switch to this pattern instead.

The binary was previously 219MB and now 222MB which I think is worth it for the quality of life improvement.

@angeloskath angeloskath requested a review from awni June 29, 2025 11:21
@angeloskath angeloskath force-pushed the macros-to-templates branch from 015573d to 4aca093 Compare June 29, 2025 11:46
@zcbenz
Copy link
Collaborator

zcbenz commented Jun 29, 2025

Getting rid of the macros would make dev experience much better, thanks for coming up with this!

@angeloskath angeloskath force-pushed the macros-to-templates branch from cedc026 to 48fa6ae Compare June 30, 2025 01:01
Copy link
Member

@awni awni left a comment

Choose a reason for hiding this comment

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

Looks awesome! Thanks for the update, really useful

@angeloskath angeloskath merged commit 3d5e17e into main Jul 1, 2025
5 checks passed
@angeloskath angeloskath deleted the macros-to-templates branch July 1, 2025 08:33
@zcbenz
Copy link
Collaborator

zcbenz commented Jul 2, 2025

When we move to C++20 we can make the lambda get the type directly so we won't have to call MLX_GET_TYPE on the argument.

This sounds an exciting new feature, where can I learn more about it?

@angeloskath
Copy link
Member Author

The docs have the following.

If the lambda definition uses an explicit template parameter list, that template parameter list is used with operator(). For each parameter in params whose type is specified as auto, an additional invented template parameter is appended to the end of that template parameter list:

// generic lambda, operator() is a template with two parameters
auto glambda = []<class T>(T a, auto&& b) { return a < b; };

// generic lambda, operator() is a template with one parameter pack
auto f = []<typename... Ts>(Ts&&... ts)
{
    return foo([std::forward](https://en.cppreference.com/w/cpp/utility/forward.html)<Ts>(ts)...);
};

The interesting bit is that since the template parameter is explicit we don't really need an argument for it. So no need for tag types.

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.

3 participants