Skip to content

LLVM codegen is pulling in libc/libm (malloc, free, floorf) functions and shouldn't be #4717

Closed

Activity

  1. ScottTodd commented on Feb 4, 2021

    @ScottTodd
    MemberAuthor

    In this zip file: iree-llvmaot-linking.zip,

    • 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)

    opening with https://cutter.re/ shows that both have some imports:
    image

    Also see Discord discussions around here

  2. added
    compiler/dialects
    on Feb 4, 2021
  3. ScottTodd commented on Feb 4, 2021

    @ScottTodd
    MemberAuthor

    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:

    ndk-stack -sym iree-build-android\iree\tools -i crash_logs_from_logcat.txt
    

    Sample symbolized crash:

    ********** Crash dump: **********
    Build fingerprint: 'samsung/beyond1qlteue/beyond1q:10/QP1A.190711.020/G973U1UES4ETK1:user/release-keys'
    #00 0x0000000000029088 /data/local/tmp/iree-run-module
    std::__ndk1::basic_streambuf<char, std::__ndk1::char_traits<char> >::pubsync()
    D:/dev/sdks/Android/ndk/android-ndk-r20b/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1\streambuf:168:28
    #01 0x0000000000028fe8 /data/local/tmp/iree-run-module
    std::__ndk1::basic_ostream<char, std::__ndk1::char_traits<char> >::flush()
    D:/dev/sdks/Android/ndk/android-ndk-r20b/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1\ostream:948:36
    #02 0x000000000020ca44 /data/local/tmp/iree-run-module
    std::__ndk1::ios_base::Init::~Init()
    /buildbot/src/googleplex-android/ndk-release-r20/external/libcxx/src\iostream.cpp:114:15
    #03 0x00000000000ea21c /apex/com.android.runtime/lib64/bionic/libc.so (__cxa_finalize+344) (BuildId: d00c50b4798c95f2447d684ed3ea7dcb)
    #04 0x00000000000e56a4 /apex/com.android.runtime/lib64/bionic/libc.so (exit+24) (BuildId: d00c50b4798c95f2447d684ed3ea7dcb)
    #05 0x000000000007e89c /apex/com.android.runtime/lib64/bionic/libc.so (__libc_init+112) (BuildId: d00c50b4798c95f2447d684ed3ea7dcb)
    
  4. 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
  5. benvanik commented on Feb 4, 2021

    @benvanik
    Collaborator

    Somewhere in the linalg-on-tensors -> LLVM path we are getting mallocs:

     %225 = llvm.call @malloc(%224) : (i64) -> !llvm.ptr<i8>

    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.

  6. stellaraccident commented on Feb 4, 2021

    @stellaraccident
    Collaborator

    Also, we shouldn't have mallocs, but a malloc without a free seems worse?

  7. benvanik commented on Feb 4, 2021

    @benvanik
    Collaborator

    yep :P

  8. benvanik commented on Mar 24, 2021

    @benvanik
    Collaborator

    (thanks for continuing to push on this)

  9. 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
  10. benvanik commented on Apr 22, 2021

    @benvanik
    Collaborator
  11. benvanik commented on Jun 16, 2021

    @benvanik
    Collaborator

    It looks like our options are:

    1. statically link some libm variant
    2. lower math to our own vector math/operations

    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 :)

  12. MaheshRavishankar commented on Jun 16, 2021

    @MaheshRavishankar
    Collaborator

    @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).

  13. 19 remaining items

  14. benvanik commented on Nov 3, 2021

    @benvanik
    Collaborator

    image
    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!

  15. ScottTodd commented on Dec 6, 2021

    @ScottTodd
    MemberAuthor

    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 to tensor<3xf32>)

  16. benvanik commented on Dec 6, 2021

    @benvanik
    Collaborator

    yeah - right now it'll run floor of 3xf32 as:

    for (int i = 0; i < 3; ++i) result[i] = floorf(input[i]);

    which is:

          ^bb3(%38: i64):  // 2 preds: ^bb2, ^bb4
            %39 = llvm.icmp "slt" %38, %33 : i64
            llvm.cond_br %39, ^bb4, ^bb5
          ^bb4:  // pred: ^bb3
            %40 = llvm.add %36, %38  : i64
            %41 = llvm.getelementptr %34[%40] : (!llvm.ptr<f32>, i64) -> !llvm.ptr<f32>
            %42 = llvm.load %41 : !llvm.ptr<f32>
            %43 = "llvm.intr.floor"(%42) : (f32) -> f32
            %44 = llvm.getelementptr %37[%40] : (!llvm.ptr<f32>, i64) -> !llvm.ptr<f32>
            llvm.store %43, %44 : !llvm.ptr<f32>
            %45 = llvm.add %38, %2  : i64
            llvm.br ^bb3(%45 : i64)

    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.

  17. benvanik commented on Dec 6, 2021

    @benvanik
    Collaborator

    (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 :)

  18. ScottTodd commented on Jun 28, 2022

    @ScottTodd
    MemberAuthor

    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.

  19. moved this to Done in IREEon Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

compiler/dialects

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions