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

Improve error messages by including symbol index in trampolines #133

Merged
merged 1 commit into from
Jul 30, 2024

Conversation

staticfloat
Copy link
Member

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.

@staticfloat staticfloat force-pushed the sf/better_error_messages branch 7 times, most recently from ec1b7e4 to 430e295 Compare July 30, 2024 04:21
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:
```julia
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:
```julia
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.
@staticfloat staticfloat force-pushed the sf/better_error_messages branch from 430e295 to e25bc22 Compare July 30, 2024 04:25
@staticfloat staticfloat merged commit d5543a9 into main Jul 30, 2024
4 of 5 checks passed
@staticfloat staticfloat deleted the sf/better_error_messages branch July 30, 2024 05:09
@imciner2
Copy link

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
Copy link
Collaborator

This is independent from JuliaLang/julia#55302, right? That should work without the new feature here in LBT?

@staticfloat
Copy link
Member Author

Correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants