JuliaLinearAlgebra / libblastrampoline

Using PLT trampolines to provide a BLAS and LAPACK demuxing library.
MIT License
66 stars 17 forks source link

Improve error messages by including symbol index in trampolines #133

Closed staticfloat closed 2 months ago

staticfloat commented 2 months ago

We smuggle symbol indices in a scratch register through our trampolines, allowing our default error printing function to rescue out the appropriate symbol index, and turn that into a symbol name. While this is fancy, reduces code size, and makes me feel clever, it's also not very reproducible for other projects that wish to replicate this technique without requiring that the scratch register be preserved long enough for their handling code to get at it. In such a case, the fallback implementation would be to generate function stubs for every symbol individually. A Julia implementation of this follows:

First, a module is defined with error stubs for every symbol:

module LBTDebuggingFuncs
import LinearAlgebra.BLAS: lbt_get_config
for func_name in lbt_get_config().exported_symbols
    @eval $(Symbol(func_name))() = println("Error: no BLAS/LAPACK library loaded for ", $(func_name), "()!")
end
end # module LBTDebuggingFuncs

Next, these functions are inserted as the base layer of forwards for LBT:

symbol_list = lbt_get_config().exported_symbols
for (symbol_idx, symbol) in enumerate(symbol_list)
    func = getproperty(LBTDebuggingFuncs, Symbol(symbol))
    debug_fptr = @cfunction($func, Cvoid, ())
    lbt_set_forward_by_index(symbol_idx-1, debug_fptr, :ilp64)
    lbt_set_forward_by_index(symbol_idx-1, debug_fptr, :lp64)
end

After that, subsequent lbt_forward() calls (without clear set, of course) will fill in forwards to the actual backing BLAS calls, but those left behind will have customized error messages, denoting which BLAS call was unsupported.

imciner2 commented 2 months ago

This is great! I had been wanting to do something like this for a while because of all the various issues we seemed to be having earlier with function mapping, and it was so obtuse to figure out what functions weren't actually getting mapped.

giordano commented 2 months ago

This is independent from https://github.com/JuliaLang/julia/pull/55302, right? That should work without the new feature here in LBT?

staticfloat commented 2 months ago

Correct