M
Modular•6mo ago
Frank Saez

FFI

Hi, I'm working on FFI and I've got two small questions : 1 - what is the equivalent of size_t in Mojo ? 2 - Dynamic linking is easy but I haven't found a way to proceed with a static library (.a file). Is there a clean and official way to do that ? Thanks.
14 Replies
Mohamed Mabrouk
Mohamed Mabrouk•6mo ago
I think size_t equivalent is Int (or maybe UInt?) which is the size of the pointer in your machine.
Frank Saez
Frank SaezOP•6mo ago
thanks. It should be UInt since size_t is supposed to be unsigned
Mohamed Mabrouk
Mohamed Mabrouk•6mo ago
UInt was only recently introduced, so maybe it's not as complete as Int.
Frank Saez
Frank SaezOP•6mo ago
yeah, I've start to feel the pain with this. I've found nothing to convert to/from Int and even a basic & doesn't seems to exist 😋 Guess, it will be Int for now
gabrieldemarmiesse
gabrieldemarmiesse•6mo ago
I'm working on improving UInt but feel free to send PRs! It's pretty easy to get started with the stdlib 🙂
gabrieldemarmiesse
gabrieldemarmiesse•6mo ago
It's basically mapping those operations: https://mlir.llvm.org/docs/Dialects/IndexOps/ to dunder methods, while being careful to use the unsigned operations whenever available
'index' Dialect - MLIR
Multi-Level IR Compiler Framework
Jack Clayton
Jack Clayton•6mo ago
Hi @FSaez static linking isn't officialy supported yet, there is a hacky way to do it here until we add it though: https://github.com/ihnorton/mojo-ffi/tree/ffi-demo/mojo-exe-link-c-static
GitHub
mojo-ffi/mojo-exe-link-c-static at ffi-demo · ihnorton/mojo-ffi
Contribute to ihnorton/mojo-ffi development by creating an account on GitHub.
Jack Clayton
Jack Clayton•6mo ago
For size_t for our interop we've used Int for that in the stdlib, but UInt is more correct I haven't personally tried it yet with interop though
Frank Saez
Frank SaezOP•6mo ago
thanks. I've already found this and it's why I've asked for a "clean and official" way 😋
ModularBot
ModularBot•6mo ago
Congrats @FSaez, you just advanced to level 1!
Frank Saez
Frank SaezOP•6mo ago
thanks. I'm "plumbing" Mojo with the libjpeg and I'll dive into MLIR after that another question but I guess I know the answer. Any plan for LTO/PGO/BOLT ? BOLT could be the easier to enable as it usually rely on a LDFLAGS.
Jack Clayton
Jack Clayton•6mo ago
LTO is already done at the MLIR level, for the other two this was a comment from an engineer:
Profile guided optimization and BOLT are amazing technologies in my opinion, supporting them would be really cool. For PGO (sometimes also referred to as FDO -- feedback-directed optimization) would need to add options to the mojo driver to take profiles as inputs when compiling or running Mojo code.
BOLT refers to the optimization of fully compiled binaries -- in theory it might be possible to use already, with executables produced by mojo build.
Profile guided optimization and BOLT are amazing technologies in my opinion, supporting them would be really cool. For PGO (sometimes also referred to as FDO -- feedback-directed optimization) would need to add options to the mojo driver to take profiles as inputs when compiling or running Mojo code.
BOLT refers to the optimization of fully compiled binaries -- in theory it might be possible to use already, with executables produced by mojo build.
Frank Saez
Frank SaezOP•6mo ago
thanks. BOLT doesn't work, at least on my setup, that's why I've asked. I've got this error :
BOLT-ERROR: function reordering only works when relocations are enabled
BOLT-ERROR: function reordering only works when relocations are enabled
It's usually solved with something like "-Wl,--emit-relocs" in the LDFLAGS. but it's no big deal, really.
Jack Clayton
Jack Clayton•6mo ago
Cool thanks for letting me know, there's interest on this internally, will let you know if someone gets it working
Want results from more Discord servers?
Add your server