Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@asmcall should use llvmcall ABI #380

Open
maleadt opened this issue Jan 2, 2024 · 1 comment
Open

@asmcall should use llvmcall ABI #380

maleadt opened this issue Jan 2, 2024 · 1 comment
Labels

Comments

@maleadt
Copy link
Owner

maleadt commented Jan 2, 2024

MWE:

using LLVM
using LLVM.Interop

function search(v, key)
    low = 0
    high = length(v)

    @inbounds while low ≤ high
        mid = round((high + low)÷2)
        middle = v[mid]
        if middle == key; return(mid); end
        new_low = mid
        new_high = mid

        low, high =
            @asmcall("""cmp \$4, \$5;
                        cmovae \$2, \$0;
                        cmovb \$3, \$1;""",
                "=r,=r,r,r,r,r,0,1",
                Tuple{Int,Int},
                Tuple{Int,Int,Int,Int,Int,Int},
                low, high, new_low, new_high, middle, key)
    end
end
ERROR: Failed to parse LLVM assembly:
<string>:7:23: error: number of output constraints does not match number of return struct elements
  %6 = call [2 x i64] asm "cmp $4, $5;\0Acmovae $2, $0;\0Acmovb $3, $1;", "=r,=r,r,r,r,r,0,1"(i64 %0, i64 %1, i64 %2, i64 %3, i64 %4, i64 %5)

Note the [2 x i64] in there; LLVM expects { i64, i64 }, so we should be able to invoke julia_type_to_llvm with llvmcall=true (this is currently not exported).

@maleadt
Copy link
Owner Author

maleadt commented Jan 2, 2024

The workaround here is to split the @asmcalls:

using LLVM
using LLVM.Interop

function search(v, key)
    low = 0
    high = length(v)

    @inbounds while low  high
        mid = (high + low)÷2
        middle = v[mid]
        if middle == key
            return(mid)
        end
        new_low = mid
        new_high = mid

        low = @asmcall("cmp \$2, \$3; cmovae \$1, \$0;", "=r,r,r,r,0",
                       Int, Tuple{Int,Int,Int,Int}, low, new_low, middle, key)
        high = @asmcall("cmp \$2, \$3; cmovb \$1, \$0;", "=r,r,r,r,0",
                        Int, Tuple{Int,Int,Int,Int}, high, new_high, middle, key)

    end
end

@maleadt maleadt added the bug label May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant