Loading...
Searching...
No Matches
RgpotAbi.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#pragma once
5
6// ABI contract with rgpot-core, the Rust core that librgpot carries. The
7// rgpot.h gpridge compiles against must declare the major gpridge is
8// written for; the library loaded at run time must accept the stamp built
9// from that header and agree on major and layout revision. The stamp covers
10// the C interface of rgpot-core. It does not cover the layout of rgpot's
11// header-only C++ classes (Potential, ForceOut, the potential frontends)
12// that gpridge compiles into itself; those follow the revision pinned in
13// subprojects/rgpot.wrap.
14//
15// Builds without rgpot-core have no rgpot.h and no stamp; the check is
16// then absent at compile time and a no-op at run time.
17
18#if __has_include(<rgpot.h>)
19#define GPR_RGPOT_ABI_CHECKED 1
20#include <rgpot.h>
21
22#include <cstdint>
23#include <mutex>
24#include <sstream>
25#include <stdexcept>
26#include <string>
27
29#define GPR_RGPOT_ABI_MAJOR 1
32#define GPR_RGPOT_ABI_MIN_MINOR 0
33
34static_assert(RGPOT_ABI_VERSION_MAJOR == GPR_RGPOT_ABI_MAJOR,
35 "gpridge is written against rgpot-core C ABI major "
36 "GPR_RGPOT_ABI_MAJOR; the rgpot.h found declares another major");
37static_assert(RGPOT_ABI_VERSION_MINOR >= GPR_RGPOT_ABI_MIN_MINOR,
38 "rgpot-core C ABI is older than gpridge needs "
39 "(see subprojects/rgpot.wrap)");
40
41namespace gpr::rgpot_abi {
42
43struct Stamp {
44 unsigned major = 0;
45 unsigned minor = 0;
46 unsigned layout = 0;
47};
48
49[[nodiscard]] inline std::string describe(const Stamp& stamp)
50{
51 std::ostringstream out;
52 out << stamp.major << '.' << stamp.minor << " layout " << stamp.layout;
53 return out.str();
54}
55
60[[nodiscard]] inline std::string mismatch(const Stamp& header,
61 const Stamp& library,
62 bool library_accepts)
63{
64 if (library_accepts && header.major == library.major &&
65 header.layout == library.layout)
66 return {};
67 return "rgpot ABI mismatch: gpridge was built against rgpot.h " +
68 describe(header) + " and the loaded rgpot library reports " +
69 describe(library) +
70 (library_accepts ? "" : " and rejects the header's stamp");
71}
72
73[[nodiscard]] inline Stamp headerStamp() noexcept
74{
75 return {RGPOT_ABI_VERSION_MAJOR, RGPOT_ABI_VERSION_MINOR,
76 RGPOT_ABI_LAYOUT_REVISION};
77}
78
79[[nodiscard]] inline Stamp libraryStamp()
80{
81 const rgpot_abi_stamp_t stamp = rgpot_abi_stamp();
82 return {stamp.abi_major, stamp.abi_minor, stamp.layout_revision};
83}
84
86[[nodiscard]] inline std::string loadedLibraryMismatch()
87{
88 const Stamp header = headerStamp();
89 rgpot_abi_stamp_t compiled{};
90 compiled.abi_major = static_cast<std::uint16_t>(header.major);
91 compiled.abi_minor = static_cast<std::uint16_t>(header.minor);
92 compiled.layout_revision = static_cast<std::uint16_t>(header.layout);
93 return mismatch(header, libraryStamp(),
94 rgpot_abi_compatible(&compiled) != 0);
95}
96
99inline void requireCompatibleLibrary()
100{
101 static std::once_flag checked;
102 static std::string failure;
103 std::call_once(checked, [] { failure = loadedLibraryMismatch(); });
104 if (!failure.empty()) throw std::runtime_error(failure);
105}
106
107} // namespace gpr::rgpot_abi
108#else
109namespace gpr::rgpot_abi {
111} // namespace gpr::rgpot_abi
112#endif
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
bool compiled() noexcept
Definition CutlassGemm.h:38
std::string loadedLibraryMismatch()
The mismatch message for the loaded library; empty when compatible.
std::string describe(const Stamp &stamp)
Definition RgminAbi.h:41
Stamp libraryStamp()
Definition RgminAbi.h:71
Stamp headerStamp() noexcept
Definition RgminAbi.h:65
std::string mismatch(const Stamp &header, const Stamp &library, bool library_accepts)
Definition RgminAbi.h:52
void requireCompatibleLibrary()
Definition RgpotAbi.h:110