Coder Social home page Coder Social logo

Comments (8)

wallabra avatar wallabra commented on July 18, 2024

In the meantime, I wrote a workaround script in Bash that pretty much parses all the arguments passed to it, and then at some point passes them to a clang (passing -fuse-lld to use mold for most objects, and lld for LLVM IR objects in an intermediary pass).

#!/bin/bash

# Parse arguments and list input files

# - Primary linker invoker: default to /usr/bin/clang++
#   (Requires -fuse-ld support)
: "${LD:=/usr/bin/clang++}"

# - Linker for IR bitcode: default to /usr/bin/clang++
#   (Requires -fuse-ld support)
: "${IR_LD:=/usr/bin/clang++}"

# - lld path
: "${LLD:=/usr/bin/ld.lld}"

# - mold path
: "${MOLD:=/usr/bin/mold}"

# - Where to cache: defaults to /tmp/irld, set empty to not do.
: "${IRLD_CACHE_DIR:=/tmp/irld}"

inputs=()
output=""
options=()

function anymsg {
    color="$1"
    type="$2"
    msg="$3"
    printf "\e[1;%sm[%s] %s ::\e[0;%sm %s\e[m\n" "$color" "$(realpath "$0")" "$type" "$color" "$msg" >/dev/tty
}

function errormsg {
    anymsg "31" "error" "$1"
}

function statusmsg {
    anymsg "34" "info" "$1"
}

function err_exit {
    errormsg "$1"
    exit "$2"
}

function assert_arg {
    if [[ -z "$1" ]]; then
        errormsg "Option '$2' is incomplete; value expected before end of arguments!"
        exit 1
    fi
}

while [[ -n "$1" ]]; do
    case "$1" in
        -o*)
            if [[ "$1" == "-o" ]]; then
                assert_arg "$2" "$1"
                shift
                output="$1"
                shift
            else
                output="${1:#-o}"
                shift
            fi ;;

        -*)
            options+=("$1")
            shift ;;

        "")
            shift ;;

        *)
            inputs+=("$1")
            shift ;;
    esac
done

# Fetch and whitelist libraries among inputs

libraries=()
objects=()

for inp in "${inputs[@]}"; do
    case "$inp" in
        *.a|*.lib|*.so)
            libraries+=("$inp") ;;

        *)
            objects+=("$inp") ;;
    esac
done

# 'Transform' objects that are LLVM IR bitcode

transformed=()
regular=()

function irld_cache {
    INPUT="$1"
    OUTPUT="$1.lib"

    # cache related parameters
    KEY="$( ( echo "${options[*]};${libraries[*]}::"; cat "$INPUT" ) | shasum -a256 | od -An -tx1 | tr -d ' ' | tr -d '\n' )"
    KEYDIR="$IRLD_CACHE_DIR/${KEY:0:2}"
    KEYFN="$KEYDIR/${KEY:2}"

    if [[ -n "$IRLD_CACHE_DIR" ]]; then
        # ensure dir exists
        mkdir -p "$IRLD_CACHE_DIR"

        # check if record exists
        if [[ -f "$KEYFN" ]]; then
            statusmsg "Found precached existing ephemeral intermediary static library for LLVM IR: '$INPUT' ... reusing (at '$KEYFN')"
            cp "$KEYFN" "$OUTPUT"
            # done!
            echo "$OUTPUT"
            return 0
        fi
    fi

    # record does not exist, or no caching is in place.
    statusmsg "Building ephemeral intermediary static library with lld from LLVM IR: '$INPUT'"

    "$IR_LD" "${options[@]}" "${libraries[@]}" "$INPUT" -o"$OUTPUT" -fuse-ld="$LLD" || {
         errormsg "Could not build ephemeral intermediary static library from LLVM IR bitcode object '$INPUT'; error $?"
         exit 2
    }

    # building succeeded

    if [[ -n "$IRLD_CACHE_DIR" ]]; then
        # we have already ensured the dir's existence, DRY.

        # make the record
        mkdir -p "$KEYDIR"
        cp "$OUTPUT" "$KEYFN"
    fi

    echo "$OUTPUT"
    return 0
}

for obj in "${objects[@]}"; do
    case "$(file "$obj")" in
        *LLVM*IR*|*LLVM*bitcode*)
            transformed+=("$(irld_cache "$obj")") || exit 2
            ;;

        *)
            regular+=("$obj")
            ;;
    esac
done


# Call the linker, finally!

statusmsg "Invoking primary linker like:"
statusmsg "$ $LD ${options[*]} ${libraries[*]} ${regular[*]} ${transformed[*]} -fuse-ld="$MOLD" -o "$output""
"$LD" "${options[@]}" "${libraries[@]}" "${regular[@]}" "${transformed[@]}" -fuse-ld="$MOLD" -o "$output" || {
    errormsg "Primary linker returned error code $?"
    exit 2
}

# Remove 'temporary' .lib transformations. (Remember the caching!)

for tra in "${transformed[@]}"; do
    rm "$tra"
done

exit 0

It's pretty hacky and I don't know if it even works all that well. I think I'll pass on using it myself, for now. :P

from mold.

rui314 avatar rui314 commented on July 18, 2024

Recent versions of gcc and clang come with shared object files that can be used as linker plug-ins. Linkers are supposed to dlopen() given shared object file and make some function calls to convert compiler's intermediate code into a compiled form. mold should be able to use the plug-in shared object files. I want to try that at some point in future.

For now, I thought that one can use mold only for the debug build and use lld for the release build. Did you always enable LTO?

from mold.

wallabra avatar wallabra commented on July 18, 2024

Hmmm... Those are interesting ideas and I back them fully. :)

And, apologies for my ignorance, but where does the LTO come in again? I'm slightly lost here. Are you referring to me trying to build LLVM or something?

from mold.

rui314 avatar rui314 commented on July 18, 2024

LTO is short for Link-Time Optimization. If you pass -flto to clang, it emits not a natively-compiled ELF file but an LLVM bitcode. A linker that supports LLVM's LTO can take not only the native ELF file but also LLVM bitcode. If given, a linker passes bitcode files to the LLVM backend to turn it into native code.

So, if you have a LLVM bitcode file as an input to the linker, you are doing LTO. Can you search for -flto in your project?

from mold.

wallabra avatar wallabra commented on July 18, 2024

Ah, I see why you asked about LTO now. :)

Yes, I was using link-time optimization in my attempt to build LLVM. Although I had unset it, as it was consuming an uneconomical amount of memory. Odd that there are still LTO-related object files laying around.. But thanks for the clarification nonetheless!

So what you mean, is that mold is not LTO-ready yet?

from mold.

rui314 avatar rui314 commented on July 18, 2024

mold is not LTO-ready yet. It's a nice-to-have feature but not high priority.

from mold.

wallabra avatar wallabra commented on July 18, 2024

Yeah, I think that's the general sentiment ^^

from mold.

rui314 avatar rui314 commented on July 18, 2024

I'll manage the progress of LTO support at #181.

from mold.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.