Translate-c vector types on macOS

Hello,

On macOS, I compile ObjC with all kind of frameworks and libs with the Zig Build System .
But I cannot translate-c headers containing definitions from <simd/vector_types.h>. Looking into it, it seems translate-c is detected differently and the following macro guard results into false:
SIMD_COMPILER_HAS_REQUIRED_FEATURES

More context:
I’ve been mainly defining things on Zig’s side and duplicating the structs in headers.

Parallel declaration has worked so far (obviously, sometimes I would get it wrong and get some pretty obvious UB).

Then, I wanted to ensure a constant was declared once for both sides and got into translate-c. It worked fine and everything was great so far.

But then I thought: OK, If declare my exported structs in ObjC then I never have the problem of matcging declarations again. But:

  • I can’t figure out how to export vector_float2 for example if it is even possible
  • I cannot control defaults (everything gets 0, false etc)
  • I don’t know how much completion I get, for some reason I had none?

Thank you for your attention!

You should be able to create a separate header file that does the following:

Set up all the #defines that you need
Include the header you are currently translating.

That should allow you to control the defaults.

If you are using the build system and b.addTranslateC, it should work the same way. Have a separate header file that does all the setup you need and includes what you need.

The thing is zig cc comes with them out of the box, I don’t have to define macros as it has the right values already (simd is guarded by macros).
I would expect translate-c to be able to do the same? Otherwise I guess I’d have to dig into those macro definitons…

Now supposing I need to do that manually, ending up accepting the two other bullet points makes the effort questionable to me. I’m fine keeping it in sync manually if that’s the price to keep the advantages I already have.

zig cc is similar to invoking clang under the hood, if I remember correctly.
zig translate-c will use arocc which explains some of the differences. I would assume that arocc is not detecting features the same way as Clang.
This is probably a gap in arocc, and you may want to look at the issue tracker and see if someone has already reported it. Note that zig 0.16.0 is using an older arocc, so if it has been fixed, you will have to either use the build system and updated translate-c dependency or use zig master.

With out understanding the workflow you are hoping for, it’s hard to give better suggestions.

thanks that explains it… Yes I will look into it and fill an issue if adequate.

About the workflow, I have made a painting application that uses objc to send stuff to metal and open native windows. It’s all Zig backed (and built).

I have some exported structs that I mirror on ObjC side (like TabletPoint and so on)

At some point I have needed a shared constant between the GPU and CPU so I have used translate-c to define it in C and have it available in Zig. Then I just wanted to try and see if I could have all my exported structs only written in .h files.

Then, if I do it ignoring vectors, I just hit the wall of “no defaults to C fields” and it seems to me ZLS doesn’t pick up completion.

Some example:

pub const TabletPoint = extern struct {
    cursor: @Vector(2, f32),
    pressure: f32,
    tilt_x: f16,
    tilt_y: f16,
};

is maintained as:

typedef struct TabletPoint {
    vector_float2 position;
    float pressure;
    __fp16 tilt_x;
    __fp16 tilt_y;
} TabletPoint;
1 Like

Arocc readme has instructions to use specific version independently of what is shipped inside Zig.

The caveat is that there are sometimes version mismatch when arocc outputs Zig code that don’t compile for your version, or your version can’t compile arocc.

But if you are able to work with arocc I recommend opening clear bug reports. The maintainer Vexu is really reactive and good at fixing bugs that have a small repro.

Good to know! Yes reproduction is trivial, that would make a good starting point.