Skip to content

Incorrect return value of tuple with compile_shlib #101

@baggepinnen

Description

@baggepinnen

The following code works as expected when calling compile, but returns the wrong result when using compile_shlib

using StaticArrays, LinearAlgebra, StaticCompiler

T = Float32

Base.@ccallable function controller1(xt::Tuple{Float32,Float32}, ut::Tuple{Float32})::Tuple{Float32,Float32}
    T = Float32
    A_ = @SMatrix T[1 1; 0 1]
    B_ = @SMatrix T[0; 1]
    x = SVector(xt)
    u = SVector(ut)
    xp = A_ * x #+ B_ * u
    xp.data
end

x = @SVector randn(T, 2) 
u = @SVector randn(T, 1) 

x′ = controller1(x.data, u.data) # test
@code_warntype controller1(x.data, u.data) # checks out

argtypes_controller1 = Tuple{typeof(x.data), typeof(u.data)}
controller1_compiled, path_controller1 = compile(controller1, argtypes_controller1, "controller1") 
x′ = controller1_compiled(x.data, u.data) # Works fine

path_controller1 = compile_shlib(controller1, argtypes_controller1, "controller1")

function c_step(x, u)
    Libc.Libdl.dlopen(path_controller1) do lib
        fn = Libc.Libdl.dlsym(lib, :julia_controller1)
        @ccall $(fn)(x::Tuple{Float32, Float32}, u::Tuple{Float32})::Tuple{Float32, Float32}
    end
end

x′ = c_step(x.data, u.data)
julia> x′ = controller1_compiled(x.data, u.data) # Works fine
(-0.22692525f0, -1.4554836f0)

julia> x′ = c_step(x.data, u.data)
(0.0f0, 0.0f0)

If I uncomment the rest of xp = A_ * x #+ B_ * u, it still works with compile, but I instead get a segfault with compile_shlib`.


julia> versioninfo()
Julia Version 1.8.5
Commit 17cfb8e65ea (2023-01-08 06:45 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 24 × AMD Ryzen 9 5900X 12-Core Processor
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, znver3)
  Threads: 12 on 24 virtual cores

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions