You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LLVM codegen is pulling in libc/libm (malloc, free, floorf) functions and shouldn't be #4717
abs_ex_dispatch_0-staticcpp.so uses the current code, with the flag
abs_ex_dispatch_0-nostaticcpp.so omits the flag
Both were generated with
iree-translate.exe -iree-mlir-to-vm-bytecode-module -iree-hal-target-backends=dylib-llvm-aot -iree-llvm-keep-linker-artifacts --iree-llvm-target-triple=aarch64-none-linux-android30 iree/tools/test/iree-run-module.mlir
(then check the output to see where the linker artifacts went)
changed the title [-]LLVM linking for Android requires `-static-libstdc++` when it probably shouldn't[/-][+]LLVM codegen is pulling in libc (malloc, free, etc) functions and shouldn't be[/+]on Feb 4, 2021
That's the root issue - malloc requires libc and shared linking, and the previous workaround was just to link in all of libc++. #4719 limits this to just libc (no more c++ global initializers and stuff!), but we still need to sever this dep: we can't have non-hermetic binaries.
changed the title [-]LLVM codegen is pulling in libc (malloc, free, etc) functions and shouldn't be[/-][+]LLVM codegen is pulling in libc/libm (malloc, free, floorf) functions and shouldn't be[/+]on Apr 22, 2021
I think I could wrangle 1 into working by taking newlib, openlibm, or musl and cross-compiling it to bitcode for some set of architectures and checking in the bitcode files.
That's a terrible mid/long-term option, though, especially if we care about performance as nothing in libm is fast. All those functions operate on scalars, have enough logic that they are never autovectorized, and often read/write the current system floating point rounding mode (which introduces a bunch of issues on its own) and set errno. We do not want to be shipping things that call into libm, even when statically linked.
Option 2 is the real way forward - we should be taking vector operations and lowering them to our own vector variants - we do this for some things already but our coverage is partial. I'd hoped that someone upstream in MLIR would have done this already (for their own sakes I'm really hoping the kernel codegen folks aren't also calling libm).
I'm not sure what the work difference is here between the two. It will take 1+ weeks to get option 1 working and will have a high maintenance burden because we check in per-arch binaries - and adding a new arch requires people to build/manage their own versions. If this is a short-term solution that we eventually cut that's probably ok (but an unfortunate time waste): it would at least unblock our cross-platform work. But if it's 1+ weeks to get proper lowerings for the 10-20 operations we support that seems like the better path given that we know we need to do that anyway.
@MaheshRavishankar / @asaadaldien what is your super rough estimate on 2? Do you know if this exists upstream somewhere that we could use, or if anyone is working on it? It feels like nice separable work (std/math -> vector) that maybe we could get help on?
Timing-wise we need to fix this issue ASAP - we can't do device multitargeting without it. I can take on option 1 if required (which it seems to be, and no one else is willing to touch this 😭), but want your eyes/planning on option 2 - the performance burden is more difficult to manage when we transparently call out to nasty scalar functions as we do today by calling out to a non-inlinable function for every vector element. If you're going to be doing CPU performance tuning the first thing you're going to want to do is not call out to libm as doing so will invalidate any measurements you make while trying to optimize work distribution as the work is off by 1000x. So even if I do 1 here and statically link in libm you're going to need to do this work anyway and will want to track that. Here I just want to make sure I won't be going down the 1 rabbit hole only for 2 to be done properly in a few weeks :)
@benvanik responded on discord, and echoing that here. I am sorry that mot much progress has been made on this. This seems like we are taking libdevice approach and putting it on steroids. For libdevice in CUDA context, its a more constrained eco system (just the architectures NVIDIA supports). For different architectures, the burden here is probably much higher than having a priority list of functions that we want to burndown. To start with can we just list out the math functions we need. I think if we just dont link libm, see what fails and just add lowerings for them in the math dialect. In its own right its a valuable contribution to MLIR. It is not a blocker for anyone upstream, so it might actually just fall to us to deal with this).
There are still hacks on the system linker path that we could avoid if LLVM did not produce calls to libc/libm, but the embedded linker path is now the default and it uses our librt solution for such calls.
Good job all - this was a tricky one!
If we don't have an issue for avoiding those scalar intrinsics we should - that'll be solely compiler-side work and maybe something purely upstream, so it'd be good to have a clear issue that we can share with people. At least now we aren't blocked on the embedded elf cases and can start on more multi-targeting/cross-compilation goodness!
With the recent change to link in musl as bitcode it may be possible for LLVM to inline the call to floor and then autovectorize it but I've never seen it do that (I think because the llvm.intr.floor gets turned into a call to floorf after the autovectorizer runs, but haven't verified that).
Note that even for an odd 3 elements it's almost always worth using proper vector instructions as the registers are likely coming from/going to other vector operations and using 3/4 SIMD lanes (in 128-bit vectors) would make that whole loop above 1 instruction + a masking operation. vroundss (single scalar) and vroundps (4 packed scalars) both execute in the same number of cycles for example.
(this is really about orders of magnitude - 20 instructions -> 4 instructions, 160 bytes of instruction cache, RAM, and binary size consumption -> 16 bytes, 2+ branch predictor/etc reservations + extra hazard tracking -> 0, etc etc - all combine to yield outsized improvements to doing something better than a simple scalar for loop :)
I'm going to close this for now, but we should still follow up on
If we don't have an issue for avoiding those scalar intrinsics we should - that'll be solely compiler-side work and maybe something purely upstream, so it'd be good to have a clear issue that we can share with people.
Activity
ScottTodd commentedon Feb 4, 2021
In this zip file: iree-llvmaot-linking.zip,
abs_ex_dispatch_0-staticcpp.souses the current code, with the flagabs_ex_dispatch_0-nostaticcpp.soomits the flagBoth were generated with
iree-translate.exe -iree-mlir-to-vm-bytecode-module -iree-hal-target-backends=dylib-llvm-aot -iree-llvm-keep-linker-artifacts --iree-llvm-target-triple=aarch64-none-linux-android30 iree/tools/test/iree-run-module.mlir(then check the output to see where the linker artifacts went)
opening with https://cutter.re/ shows that both have some imports:

Also see Discord discussions around here
ScottTodd commentedon Feb 4, 2021
The steps at https://google.github.io/iree/get-started/getting-started-android-cmake#dylib-llvm-aot-backend show how compile and run modules for Android using LLVM AOT. When I've tried that without the
-static-libstdc++flag, I get segfaults that can be symbolized with ndk-stack:Sample symbolized crash:
[-]LLVM linking for Android requires `-static-libstdc++` when it probably shouldn't[/-][+]LLVM codegen is pulling in libc (malloc, free, etc) functions and shouldn't be[/+]benvanik commentedon Feb 4, 2021
Somewhere in the linalg-on-tensors -> LLVM path we are getting mallocs:
That's the root issue - malloc requires libc and shared linking, and the previous workaround was just to link in all of libc++. #4719 limits this to just libc (no more c++ global initializers and stuff!), but we still need to sever this dep: we can't have non-hermetic binaries.
stellaraccident commentedon Feb 4, 2021
Also, we shouldn't have mallocs, but a malloc without a free seems worse?
benvanik commentedon Feb 4, 2021
yep :P
ScottTodd commentedon Mar 24, 2021
Checking the status here.
I'm testing https://github.com/google/iree/blob/main/iree/test/e2e/models/bert_encoder_unrolled_fake_weights.mlir.
Linux
When I flip these flags around: https://github.com/google/iree/blob/603b208f623e65eb48d8594c4ffc4218f36f41f5/iree/compiler/Dialect/HAL/Target/LLVM/internal/UnixLinkerTool.cpp#L96-L105
and compile for
-iree-llvm-target-triple=x86_64-linux-gnu(on Windows)I see
floorfandfmafundefinedWasm
I actually am able to remove
--allow-undefinedfrom WASM linking (-iree-hal-target-backends=wasm-llvm-aot) though, thanks to #5142: https://github.com/google/iree/blob/603b208f623e65eb48d8594c4ffc4218f36f41f5/iree/compiler/Dialect/HAL/Target/LLVM/internal/WasmLinkerTool.cpp#L93-L95Android
Not tested yet. These flags would be changed: https://github.com/google/iree/blob/603b208f623e65eb48d8594c4ffc4218f36f41f5/iree/compiler/Dialect/HAL/Target/LLVM/internal/AndroidLinkerTool.cpp#L152-L155
benvanik commentedon Mar 24, 2021
(thanks for continuing to push on this)
[-]LLVM codegen is pulling in libc (malloc, free, etc) functions and shouldn't be[/-][+]LLVM codegen is pulling in libc/libm (malloc, free, floorf) functions and shouldn't be[/+]benvanik commentedon Apr 22, 2021
For the embedded ELF linker this is even more critical as we can't import any functions. The linker configuration is here:
https://github.com/google/iree/blob/96fd277e4424c8a195e4ab3b94a42a1824ae4322/iree/compiler/Dialect/HAL/Target/LLVM/internal/EmbeddedLinkerTool.cpp
benvanik commentedon Jun 16, 2021
It looks like our options are:
I think I could wrangle 1 into working by taking newlib, openlibm, or musl and cross-compiling it to bitcode for some set of architectures and checking in the bitcode files.
That's a terrible mid/long-term option, though, especially if we care about performance as nothing in libm is fast. All those functions operate on scalars, have enough logic that they are never autovectorized, and often read/write the current system floating point rounding mode (which introduces a bunch of issues on its own) and set errno. We do not want to be shipping things that call into libm, even when statically linked.
Option 2 is the real way forward - we should be taking vector operations and lowering them to our own vector variants - we do this for some things already but our coverage is partial. I'd hoped that someone upstream in MLIR would have done this already (for their own sakes I'm really hoping the kernel codegen folks aren't also calling libm).
I'm not sure what the work difference is here between the two. It will take 1+ weeks to get option 1 working and will have a high maintenance burden because we check in per-arch binaries - and adding a new arch requires people to build/manage their own versions. If this is a short-term solution that we eventually cut that's probably ok (but an unfortunate time waste): it would at least unblock our cross-platform work. But if it's 1+ weeks to get proper lowerings for the 10-20 operations we support that seems like the better path given that we know we need to do that anyway.
@MaheshRavishankar / @asaadaldien what is your super rough estimate on 2? Do you know if this exists upstream somewhere that we could use, or if anyone is working on it? It feels like nice separable work (std/math -> vector) that maybe we could get help on?
Timing-wise we need to fix this issue ASAP - we can't do device multitargeting without it. I can take on option 1 if required (which it seems to be, and no one else is willing to touch this 😭), but want your eyes/planning on option 2 - the performance burden is more difficult to manage when we transparently call out to nasty scalar functions as we do today by calling out to a non-inlinable function for every vector element. If you're going to be doing CPU performance tuning the first thing you're going to want to do is not call out to libm as doing so will invalidate any measurements you make while trying to optimize work distribution as the work is off by 1000x. So even if I do 1 here and statically link in libm you're going to need to do this work anyway and will want to track that. Here I just want to make sure I won't be going down the 1 rabbit hole only for 2 to be done properly in a few weeks :)
I'll get started on a prototype for 1, but I'm hoping that you looking at what we're doing today/will be doing with the static linking will put some energy behind 2 :)
MaheshRavishankar commentedon Jun 16, 2021
@benvanik responded on discord, and echoing that here. I am sorry that mot much progress has been made on this. This seems like we are taking libdevice approach and putting it on steroids. For libdevice in CUDA context, its a more constrained eco system (just the architectures NVIDIA supports). For different architectures, the burden here is probably much higher than having a priority list of functions that we want to burndown. To start with can we just list out the math functions we need. I think if we just dont link
libm, see what fails and just add lowerings for them in the math dialect. In its own right its a valuable contribution to MLIR. It is not a blocker for anyone upstream, so it might actually just fall to us to deal with this).19 remaining items
Add ceilf and powf functions to librt in LLVM target. (#7404)
Add ceilf and powf functions to librt in LLVM target. (iree-org#7404)
Flip -iree-llvm-link-embedded to true by default. (#7372)
ScottTodd commentedon Nov 3, 2021
Shall we call this fixed now?
There are still hacks on the system linker path that we could avoid if LLVM did not produce calls to libc/libm, but the embedded linker path is now the default and it uses our
librtsolution for such calls.https://github.com/google/iree/blob/981f48768a5a27baac818b8afa506abaf4ba75ad/iree/compiler/Dialect/HAL/Target/LLVM/internal/UnixLinkerTool.cpp#L78-L87
https://github.com/google/iree/blob/981f48768a5a27baac818b8afa506abaf4ba75ad/iree/compiler/Dialect/HAL/Target/LLVM/internal/EmbeddedLinkerTool.cpp#L107-L109
benvanik commentedon Nov 3, 2021
Good job all - this was a tricky one!
If we don't have an issue for avoiding those scalar intrinsics we should - that'll be solely compiler-side work and maybe something purely upstream, so it'd be good to have a clear issue that we can share with people. At least now we aren't blocked on the embedded elf cases and can start on more multi-targeting/cross-compilation goodness!
ScottTodd commentedon Dec 6, 2021
What would we like LLVM to emit instead of the scalar intrinsics? Vector operations? Here's some IR from the test I'm adding in #7810: https://gist.github.com/ScottTodd/b7ca6277d8ef4ba4f6d2a6e726abb529 (with
tensor<f32>changed totensor<3xf32>)benvanik commentedon Dec 6, 2021
yeah - right now it'll run floor of 3xf32 as:
which is:
With the recent change to link in musl as bitcode it may be possible for LLVM to inline the call to floor and then autovectorize it but I've never seen it do that (I think because the llvm.intr.floor gets turned into a call to floorf after the autovectorizer runs, but haven't verified that).
Note that even for an odd 3 elements it's almost always worth using proper vector instructions as the registers are likely coming from/going to other vector operations and using 3/4 SIMD lanes (in 128-bit vectors) would make that whole loop above 1 instruction + a masking operation. vroundss (single scalar) and vroundps (4 packed scalars) both execute in the same number of cycles for example.
benvanik commentedon Dec 6, 2021
(this is really about orders of magnitude - 20 instructions -> 4 instructions, 160 bytes of instruction cache, RAM, and binary size consumption -> 16 bytes, 2+ branch predictor/etc reservations + extra hazard tracking -> 0, etc etc - all combine to yield outsized improvements to doing something better than a simple scalar for loop :)
Remove linker hacks and add test coverage for musl linking. (#7810)
ScottTodd commentedon Jun 28, 2022
I'm going to close this for now, but we should still follow up on