Skip to content

CBLAS: unify the XERBLA name/INFO logic between the library and the test harness - #1340

Merged
langou merged 9 commits into
Reference-LAPACK:masterfrom
ACSimon33:cblas_xerbla_unify
Jul 30, 2026
Merged

langou merged 9 commits into
Reference-LAPACK:masterfrom
ACSimon33:cblas_xerbla_unify

Conversation

@ACSimon33

@ACSimon33 ACSimon33 commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Motivation

CBLAS carries three near-identical copies of the same two pieces of logic — building the cblas_-prefixed routine name, and remapping the Fortran argument number to the CBLAS argument position for row-major calls: CBLAS/src/cblas_xerbla.c, CBLAS/src/xerbla.c and CBLAS/testing/c_xerbla.c. They have drifted apart, and the strstr()-based matching they use is wrong for some routine names.

This PR extracts the logic into a single header, CBLAS/include/cblas_xerbla_internal.h, and makes all three sites use it. No public API or ABI change.

Bugs fixed

  • gemmtr got the gemm remapping. strstr(rout, "gemm") also matches
    cblas_?gemmtr, so row-major gemmtr errors swapped arguments 4 and 5 and reported
    the wrong parameter number. Matching is now exact (with the test harness's existing
    gemmtr special case folded in).

  • Routine names longer than six characters were truncated. src/xerbla.c copied a
    fixed six characters into a fixed-size buffer, so e.g. cblas_dskewsymm was reported
    as cblas_dskewsy. The name is now trimmed from the real Fortran string length into a
    correctly sized buffer (with a bit of headroom for potential new routines).

  • Extended (64-bit) API builds reported the wrong name. Generate64BitSuffixedSource
    rewrites the XERBLA literals, so the Fortran layer passes DGEMM_64. The suffix is now
    stripped when rebuilding the name and re-applied when reporting, so a 64-bit build names
    cblas_dgemm_64() and the test harness's name comparison matches.

  • INFO was read as the wrong integer type. Both xerbla.c files dereferenced the
    Fortran-supplied void *info as int/CBLAS_INT; it is now read as F77_INT.

  • skewsymm was only handled by substring accident (strstr(rout, "symm")); it is now
    matched explicitly.

  • Unbounded write into a fixed-size buffer in the test harness. F77_xerbla() copied
    the name with for (i = 0; i < srname_len; i++) rout[i+6] = ..., where srname_len is
    the hidden Fortran string length, so nothing bounds the write by the size of rout.
    GCC 16 on macOS flags this:

    CBLAS/testing/c_xerbla.c:130:46: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
    

    cblas_xerbla_make_rout() takes the destination size and bounds every write by it, so
    the warning goes away.

Other changes

  • CBLAS_WEAK_SYMBOL replaces the repeated #ifdef HAS_ATTRIBUTE_WEAK_SUPPORT /
    __attribute__((weak)) blocks in cblas.h, cblas_64.h and cblas_f77.h.
  • cblas_test.h now provides F77_INT and FCHAR, so the harness no longer rolls its own
    F77_Integer/F77_Char variants (which were invalid).
  • (int) casts on CBLAS_INT values in the test printfs are replaced by CBLAS_IFMT,
    which was already the convention elsewhere; same for PRId64 in cblas_example1_64.c.
  • A CBLAS/.clang-format matching the existing CBLAS style, and .gitignore entries for
    shared libraries and debug info.

I tried to keep the .clang-format stye as close to the existing CBLAS formatting. This could enable us to actually format the entire CBLAS codebase without too many changes. Could be discussed in the future.

ACSimon33 and others added 9 commits July 28, 2026 21:35
Replace the repeated

    void
    #ifdef HAS_ATTRIBUTE_WEAK_SUPPORT
    __attribute__((weak))
    #endif

preamble on the cblas_xerbla(), cblas_xerbla_64() and F77_xerbla_base()
declarations with a single CBLAS_WEAK_SYMBOL macro.

Define it in cblas.h ahead of the cblas_64.h include: cblas_64.h declares
cblas_xerbla_64() with the macro, and its own include of cblas.h is a
no-op while cblas.h is still inside its own include guard.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
c_xerbla.c needs the Fortran integer and character types to declare
F77_xerbla(), but the testing sources deliberately do not include
cblas_f77.h: that header maps every F77_* name to the real BLAS symbol
while cblas_test.h maps them to the Fortran test wrappers, and 141 of
those names collide.

Copy the two fallbacks into cblas_test.h instead, alongside the
BLAS_FORTRAN_STRLEN_END and FORTRAN_STRLEN definitions it already
duplicates for the same reason.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
cblas_xerbla_internal.h collects the routine-name construction and the
row-major INFO remapping that the library and the test harness each
open-code today, so the two copies can no longer drift apart.

Relative to those copies the helper also trims the blank padding Fortran
supplies, drops the _64 suffix that BUILD_INDEX64_EXT_API rewrites into
the XERBLA name literals, matches operation names exactly rather than by
substring, and derives its buffer size from a named maximum so that long
names are clamped instead of silently truncated.

It has no user yet; the xerbla sources are switched over next.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Switch cblas_xerbla(), F77_xerbla_base() and the test harness over to
cblas_xerbla_internal.h. Two diagnostic bugs go away with the duplicated
code:

  - the row-major remap keyed off strstr(rout, "gemm"), which also
    matches gemmtr and so wrongly swapped its arguments 4 and 5;

  - the six-character name buffer truncated cblas_sgemmtr and
    cblas_sskewsyr2k in the library, while the harness used an
    eleven-character buffer and did not, so the two disagreed about the
    same routine.

The Fortran entry points now take FCHAR and read the argument number
through F77_INT consistently, honour the hidden string length instead of
assuming six characters, and carry doxygen comments.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Describes the style the xerbla sources are written in: three-space
indent, Allman braces, 80 columns, and the return type on its own line
for definitions. Every option clang-format knows about is listed, with
the ones inherited from the LLVM base style commented out, so that the
uncommented lines are exactly what this style changes.

The file applies to all of CBLAS, but the older sources here do not
follow it, so format only the lines you touch, e.g. with
git clang-format.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 29, 2026 13:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@langou
langou merged commit c08a083 into Reference-LAPACK:master Jul 30, 2026
16 checks passed
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