Skip to content

Commit

Permalink
Improve error messages by including symbol index in trampolines
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
staticfloat committed Jul 30, 2024
1 parent ff05ebb commit d5543a9
Show file tree
Hide file tree
Showing 15 changed files with 5,206 additions and 5,143 deletions.
2 changes: 1 addition & 1 deletion ext/gensymbol/generate_func_list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ echo "#ifndef EXPORTED_FUNCS" >> "${OUTPUT_FILE}"
echo "#define EXPORTED_FUNCS(XX) \\" >> "${OUTPUT_FILE}"
NUM_EXPORTED=0
for s in ${EXPORTED_FUNCS}; do
echo " XX(${s}) \\" >> "${OUTPUT_FILE}"
echo " XX(${s}, ${NUM_EXPORTED}) \\" >> "${OUTPUT_FILE}"
NUM_EXPORTED=$((${NUM_EXPORTED} + 1))
[ $((${NUM_EXPORTED} % 100)) == 0 ] && echo -n "."
done
Expand Down
2 changes: 1 addition & 1 deletion src/Make.inc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ else
SONAME_FLAG := -Wl,-soname=$(LIB_MAJOR_VERSION)
endif

LBT_CFLAGS := -g -O2 -std=c99 $(FPIC) -DLIBRARY_EXPORTS -D_GNU_SOURCE $(CFLAGS)
LBT_CFLAGS := -g -O2 -std=gnu99 $(FPIC) -DLIBRARY_EXPORTS -D_GNU_SOURCE -DARCH_$(ARCH) $(CFLAGS)
LBT_LDFLAGS := $(LDFLAGS)

ifeq ($(OS),Linux)
Expand Down
Loading

0 comments on commit d5543a9

Please sign in to comment.