Skip to content

How would you feel about macrofying more of the implementation? #106

@HadrienG2

Description

@HadrienG2

While investigating #105 and #104, I noticed that there is a lot of duplicated patterns in the code, like this sort of function declaration header...

#[must_use]
#[inline(always)]
#[cfg_attr(docs_rs, doc(cfg(target_feature = <some feature>)))]

...and most implementations follow a very simple pattern like "pass through the elements to the intrinsic, possibly with some .0 access, and get the result back" or "get the immediate const and use it as the rightmost argument to the intrinsic".

Replacing as many of these patterns as possible with code generation macros would have some advantages:

  • It would reduce the risk for copy-paste errors like Missing 256-bit vpermps ? #104
  • It would simplify global changes (e.g. at some point you started using #[must_use], but you did not do it everywhere in a seemingly inconsistent way which suggests you forgot / did not find the motivation to propagate it).
  • It would make the code more focused on its core business of wrapping intrinsics, with less line noise that the eye just scans through without reading even though it may actually contain important info.

It would also have some maintainability drawbacks: even declarative macros have quite arcane syntax that makes them hard to write, debug and extend. So more suffering when you or someone else needs to edit the macro. Also IDE-style tooling like rust-analyzer tends to just give up inside of macros, so things like autoformat and autocompletion may not work as nicely as usual when editing code.

On the neutral side, there are some things we can't do with (declarative) macros and still need to copy-paste by hand if we're not ready to go all the way to procedural macros (which I would not advise). This most importantly includes links to the intrinsics in the documentation.

To help gauge the tradeoff, I tried my hand at a quick prototype that does the codegen work for the small adx instruction set using declarative macros. The macro would obviously get more complex as more instruction sets are added, but I think this already gives a fairly good feeling of what the end result would look like : HadrienG2/safe_arch@main...HadrienG2:safe_arch:macrofy .

How would you feel about this change?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions