[arcilator] Add JIT runtime environment library and stdio hooks #7445
Conversation
| << info.message() << "\n"; | ||
| }); | ||
|
|
||
| arc_jit_runtime_env_deinit(); |
There was a problem hiding this comment.
nit: you can use llvm::make_scope_exit to automatically call deinit.
| // --- Exports to the IR --- | ||
|
|
||
| #ifdef _WIN32 | ||
| #define ARCEXPORT(rtype) extern "C" __declspec(dllexport) rtype __cdecl |
There was a problem hiding this comment.
Can you make this consistent with MLIR_RUNNERUTILS_EXPORT macro?
There was a problem hiding this comment.
MLIR_RUNNERUTILS_EXPORT does not include extern "C" yet it looks like it is prefixed with extern "C" every time it is used. That seems odd to me.
The __cdecl is probably not required. It don't have access to my Windows PC right now, I'll check later today. Basically, I've just used every magic incantation here that I could come up with to increase the likelihood of it linking. 😅
| %ascii_em = llvm.mlir.constant(33 : i32) : i32 | ||
| %ascii_lf = llvm.mlir.constant(10 : i32) : i32 | ||
| %ascii_l = llvm.mlir.constant(108 : i32) : i32 | ||
| %in_clk = arc.root_input "clk", %arg0 {offset = 0 : i32} : (!arc.storage<1>) -> !arc.state<i1> |
There was a problem hiding this comment.
This seems to be unused. Was there some issue with the storage lowering when removing this?
There was a problem hiding this comment.
No, tbh I didn't even try without at least one port as I was unsure whether this is a supported use-case.
Looks like it is working just fine. 👍
| ARC_ENV_DECL_GET_PRINT_STREAM(id) { | ||
| (void)id; | ||
| return stderr; | ||
| } |
There was a problem hiding this comment.
I'm wondering if it would make sense to let the id influence whether stderr or stdout is returned?
There was a problem hiding this comment.
Yeah, I've added this in anticipation of #7111. The problem is, AFAIK we don't have any lowering path that would support this, let alone an agreed on concept for file descriptors / output streams in the IR. I had a short exchange with @seldridge on this in the discord channel some weeks ago. For now we could at best add a command line flag to control this globally.
There was a problem hiding this comment.
Ah I see. I was just thinking about adding an if-statement here to allow the LLVM call operation to choose between stderr and stdout when writing MLIR integration tests and examples by hand. But this is not important at all. We can also just leave it as is until we have a proper story for file descriptors.
| #ifdef _WIN32 | ||
| #define ARCEXPORT(rtype) extern "C" __declspec(dllexport) rtype __cdecl |
There was a problem hiding this comment.
I've no clue about those windows specific things, but thanks for making this work with Windows as well! 🚀
| @@ -0,0 +1,23 @@ | |||
| // Arcilator JIT runtime environment API facing the arcilator.cpp | |||
There was a problem hiding this comment.
We should probably also add the standard header comments here.
There was a problem hiding this comment.
Yes, nice catch. I'm a little surprised the CI did not complain about it. The arcilator-runtime.h and arcilator-header-cpp.py should probably also at least get a license identifier, shouldn't they?
There was a problem hiding this comment.
Yeah, seems like it slipped through there.
| @@ -0,0 +1,7 @@ | |||
| #include "../arcilator-runtime.h" | |||
There was a problem hiding this comment.
We should probably add the standard header comments here.
fabianschuiki
left a comment
There was a problem hiding this comment.
LGTM! Thanks for adding this 🥳
Can't wait for us to finally get the VCD writing out of the runtime header and built into the model a bit more thoroughly. That will make the JIT pretty much equivalent to a separately-compiled model 😄
|
That does make me wonder how to proceed from here. When writing this PR's code I didn't want to mess up existing build flows. So, the "arcilator-runtime" remains a header library and we simply provide a compiled binary of it to the JIT runner. That comes at the cost of mixing declarations and definitions in the header. If our goal is to move stuff out of the header, maybe it would be better if we had an What do you think? |
|
Hmmm yeah I think it would make sense to actually compile a runtime library, link against it from the JIT, and also install it as a standalone library alongside the rest of CIRCT. If you use arcilator to build an object file, you can link against that and optionally the runtime in whatever build flow you're using. And if you use arcilator in JIT mode, you also have the library available. |
|
I've brought the export macro closer to |
|
Thank you @fzi-hielscher! |
This PR adds a dynamically linked runtime environment library to arcilator, which makes functions declared in the
arcilator-runtime.havailable for JIT compiled runs.The runtime is intended to provide support functions that can be called directly from the generated IR, similar to the
CRunnerUtilsin LLVM. I went for a dynamic instead of a static library because I think there is some (limited) use in having the option to swap the runtime environment without having to recompile arcilator itself.Notably, it provides a well defined interface for certain methods of the C standard library (currently
fprintf,fputs,fputc). While it is technically possible to invoke them directly from the IR, going through the standard headers and a C preprocessor should avoid trouble caused by different libc implementations (glibc, musl, msvcrt, ucrt, ...).PS: Just noticed that I switched from camel case to snake case.
_arcLibcFprintfdoesn't look right to me...