Skip to content

Commit 8442410

Browse files
[CAS] Add support for PluginCAS
Allow loading external CAS implementation via PluginCAS. In this patch, it adds: * C APIs that can be implemented by plugin, from which LLVM can load the dylib to use external CAS implementation * A PluginCAS, that implements vending external CAS implementation as llvm ObjectStore and ActionCache class. * A libCASPluginTest dylib, that provides example external CAS implementation that wraps LLVM CAS for testing purpose. * Add a unified way to load external CAS implementation.
1 parent 34cbc76 commit 8442410

21 files changed

+2019
-82
lines changed
Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
/*===-- llvm-c/CAS/PluginAPI_functions.h - CAS Plugin Functions Interface -===*\
2+
|* *|
3+
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
4+
|* Exceptions. *|
5+
|* See https://llvm.org/LICENSE.txt for license information. *|
6+
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
7+
|* *|
8+
|*===----------------------------------------------------------------------===*|
9+
|* *|
10+
|* The functions for the LLVM CAS plugin API. Intended for assisting *|
11+
|* implementations of the API. *|
12+
|* The API is experimental and subject to change. *|
13+
|* *|
14+
\*===----------------------------------------------------------------------===*/
15+
16+
#ifndef LLVM_C_CAS_PLUGINAPI_FUNCTIONS_H
17+
#define LLVM_C_CAS_PLUGINAPI_FUNCTIONS_H
18+
19+
#include "llvm-c/CAS/PluginAPI_types.h"
20+
#include "llvm-c/ExternC.h"
21+
22+
#ifndef LLCAS_PUBLIC
23+
#ifdef _WIN32
24+
#define LLCAS_PUBLIC __declspec(dllexport)
25+
#else
26+
#define LLCAS_PUBLIC
27+
#endif
28+
#endif
29+
30+
LLVM_C_EXTERN_C_BEGIN
31+
32+
/**
33+
* Returns the \c LLCAS_VERSION_MAJOR and \c LLCAS_VERSION_MINOR values that the
34+
* plugin was compiled with.
35+
* Intended for assisting compatibility with different versions.
36+
*/
37+
LLCAS_PUBLIC void llcas_get_plugin_version(unsigned *major, unsigned *minor);
38+
39+
/**
40+
* Releases memory of C string pointers provided by other functions.
41+
*/
42+
LLCAS_PUBLIC void llcas_string_dispose(char *);
43+
44+
/**
45+
* Options object to configure creation of \c llcas_cas_t. After passing to
46+
* \c llcas_cas_create, its memory can be released via
47+
* \c llcas_cas_options_dispose.
48+
*/
49+
LLCAS_PUBLIC llcas_cas_options_t llcas_cas_options_create(void);
50+
51+
LLCAS_PUBLIC void llcas_cas_options_dispose(llcas_cas_options_t);
52+
53+
/**
54+
* Receives the \c LLCAS_VERSION_MAJOR and \c LLCAS_VERSION_MINOR values that
55+
* the client was compiled with.
56+
* Intended for assisting compatibility with different versions.
57+
*/
58+
LLCAS_PUBLIC void llcas_cas_options_set_client_version(llcas_cas_options_t,
59+
unsigned major,
60+
unsigned minor);
61+
62+
/**
63+
* Receives a local file-system path that the plugin should use for any on-disk
64+
* resources/caches.
65+
*/
66+
LLCAS_PUBLIC void llcas_cas_options_set_ondisk_path(llcas_cas_options_t,
67+
const char *path);
68+
69+
/**
70+
* Receives a name/value strings pair, for the plugin to set as a custom option
71+
* it supports. These are usually passed through as invocation options and are
72+
* opaque to the client.
73+
*
74+
* \param error optional pointer to receive an error message if an error
75+
* occurred. If set, the memory it points to needs to be released via
76+
* \c llcas_string_dispose.
77+
* \returns true if there was an error, false otherwise.
78+
*/
79+
LLCAS_PUBLIC bool llcas_cas_options_set_option(llcas_cas_options_t,
80+
const char *name,
81+
const char *value, char **error);
82+
83+
/**
84+
* Creates a new \c llcas_cas_t object. The objects returned from the other
85+
* functions are only valid to use while the \c llcas_cas_t object that they
86+
* came from is still valid.
87+
*
88+
* \param error optional pointer to receive an error message if an error
89+
* occurred. If set, the memory it points to needs to be released via
90+
* \c llcas_string_dispose.
91+
* \returns \c NULL if there was an error.
92+
*/
93+
LLCAS_PUBLIC llcas_cas_t llcas_cas_create(llcas_cas_options_t, char **error);
94+
95+
/**
96+
* Releases memory of \c llcas_cas_t. After calling this it is invalid to keep
97+
* using objects that originated from this \c llcas_cas_t instance.
98+
*/
99+
LLCAS_PUBLIC void llcas_cas_dispose(llcas_cas_t);
100+
101+
/**
102+
* \returns the hash schema name that the plugin is using. The string memory it
103+
* points to needs to be released via \c llcas_string_dispose.
104+
*/
105+
LLCAS_PUBLIC char *llcas_cas_get_hash_schema_name(llcas_cas_t);
106+
107+
/**
108+
* Parses the printed digest and returns the digest hash bytes.
109+
*
110+
* \param printed_digest a C string that was previously provided by
111+
* \c llcas_digest_print.
112+
* \param bytes pointer to a buffer for writing the digest bytes. Can be \c NULL
113+
* if \p bytes_size is 0.
114+
* \param bytes_size the size of the buffer.
115+
* \param error optional pointer to receive an error message if an error
116+
* occurred. If set, the memory it points to needs to be released via
117+
* \c llcas_string_dispose.
118+
* \returns 0 if there was an error. If \p bytes_size is smaller than the
119+
* required size to fit the digest bytes, returns the required buffer size
120+
* without writing to \c bytes. Otherwise writes the digest bytes to \p bytes
121+
* and returns the number of written bytes.
122+
*/
123+
LLCAS_PUBLIC unsigned llcas_digest_parse(llcas_cas_t,
124+
const char *printed_digest,
125+
uint8_t *bytes, size_t bytes_size,
126+
char **error);
127+
128+
/**
129+
* Returns a string for the given digest bytes that can be passed to
130+
* \c llcas_digest_parse.
131+
*
132+
* \param printed_id pointer to receive the printed digest string. The memory it
133+
* points to needs to be released via \c llcas_string_dispose.
134+
* \param error optional pointer to receive an error message if an error
135+
* occurred. If set, the memory it points to needs to be released via
136+
* \c llcas_string_dispose.
137+
* \returns true if there was an error, false otherwise.
138+
*/
139+
LLCAS_PUBLIC bool llcas_digest_print(llcas_cas_t, llcas_digest_t,
140+
char **printed_id, char **error);
141+
142+
/**
143+
* Provides the \c llcas_objectid_t value for the given \c llcas_digest_t.
144+
*
145+
* \param digest the digest bytes that the returned \c llcas_objectid_t
146+
* represents.
147+
* \param p_id pointer to store the returned \c llcas_objectid_t object.
148+
* \param error optional pointer to receive an error message if an error
149+
* occurred. If set, the memory it points to needs to be released via
150+
* \c llcas_string_dispose.
151+
* \returns true if there was an error, false otherwise.
152+
*/
153+
LLCAS_PUBLIC bool llcas_cas_get_objectid(llcas_cas_t, llcas_digest_t digest,
154+
llcas_objectid_t *p_id, char **error);
155+
156+
/**
157+
* \returns the \c llcas_digest_t value for the given \c llcas_objectid_t.
158+
* The memory that the buffer points to is valid for the lifetime of the
159+
* \c llcas_cas_t object.
160+
*/
161+
LLCAS_PUBLIC llcas_digest_t llcas_objectid_get_digest(llcas_cas_t,
162+
llcas_objectid_t);
163+
164+
/**
165+
* Checks whether a \c llcas_objectid_t points to an existing object.
166+
*
167+
* \param globally For CAS implementations that distinguish between local CAS
168+
* and remote/distributed CAS, \p globally set to false indicates that the
169+
* lookup will be restricted to the local CAS, returning "not found" even if the
170+
* object might exist in the remote CAS.
171+
* \param error optional pointer to receive an error message if an error
172+
* occurred. If set, the memory it points to needs to be released via
173+
* \c llcas_string_dispose.
174+
* \returns one of \c llcas_lookup_result_t.
175+
*/
176+
LLCAS_PUBLIC llcas_lookup_result_t llcas_cas_contains_object(llcas_cas_t,
177+
llcas_objectid_t,
178+
bool globally,
179+
char **error);
180+
181+
/**
182+
* Loads the object that \c llcas_objectid_t points to.
183+
*
184+
* \param error optional pointer to receive an error message if an error
185+
* occurred. If set, the memory it points to needs to be released via
186+
* \c llcas_string_dispose.
187+
* \returns one of \c llcas_lookup_result_t.
188+
*/
189+
LLCAS_PUBLIC llcas_lookup_result_t llcas_cas_load_object(
190+
llcas_cas_t, llcas_objectid_t, llcas_loaded_object_t *, char **error);
191+
192+
/**
193+
* Like \c llcas_cas_load_object but loading happens via a callback function.
194+
* Whether the call is asynchronous or not depends on the implementation.
195+
*
196+
* \param ctx_cb pointer to pass to the callback function.
197+
*/
198+
LLCAS_PUBLIC void llcas_cas_load_object_async(llcas_cas_t, llcas_objectid_t,
199+
void *ctx_cb,
200+
llcas_cas_load_object_cb);
201+
202+
/**
203+
* Stores the object with the provided data buffer and \c llcas_objectid_t
204+
* references, and provides its associated \c llcas_objectid_t.
205+
*
206+
* \param refs pointer to array of \c llcas_objectid_t. Can be \c NULL if
207+
* \p refs_count is 0.
208+
* \param refs_count number of \c llcas_objectid_t objects in the array.
209+
* \param p_id pointer to store the returned \c llcas_objectid_t object.
210+
* \param error optional pointer to receive an error message if an error
211+
* occurred. If set, the memory it points to needs to be released via
212+
* \c llcas_string_dispose.
213+
* \returns true if there was an error, false otherwise.
214+
*/
215+
LLCAS_PUBLIC bool llcas_cas_store_object(llcas_cas_t, llcas_data_t,
216+
const llcas_objectid_t *refs,
217+
size_t refs_count,
218+
llcas_objectid_t *p_id, char **error);
219+
220+
/**
221+
* \returns the data buffer of the provided \c llcas_loaded_object_t. The buffer
222+
* pointer must be 8-byte aligned and \c NULL terminated. The memory that the
223+
* buffer points to is valid for the lifetime of the \c llcas_cas_t object.
224+
*/
225+
LLCAS_PUBLIC llcas_data_t llcas_loaded_object_get_data(llcas_cas_t,
226+
llcas_loaded_object_t);
227+
228+
/**
229+
* \returns the references of the provided \c llcas_loaded_object_t.
230+
*/
231+
LLCAS_PUBLIC llcas_object_refs_t
232+
llcas_loaded_object_get_refs(llcas_cas_t, llcas_loaded_object_t);
233+
234+
/**
235+
* \returns the number of references in the provided \c llcas_object_refs_t.
236+
*/
237+
LLCAS_PUBLIC size_t llcas_object_refs_get_count(llcas_cas_t,
238+
llcas_object_refs_t);
239+
240+
/**
241+
* \returns the \c llcas_objectid_t of the reference at \p index. It is invalid
242+
* to pass an index that is out of the range of references.
243+
*/
244+
LLCAS_PUBLIC llcas_objectid_t llcas_object_refs_get_id(llcas_cas_t,
245+
llcas_object_refs_t,
246+
size_t index);
247+
248+
/**
249+
* Retrieves the \c llcas_objectid_t value associated with a \p key.
250+
*
251+
* \param p_value pointer to store the returned \c llcas_objectid_t object.
252+
* \param globally if true it is a hint to the underlying implementation that
253+
* the lookup is profitable to be done on a distributed caching level, not just
254+
* locally. The implementation is free to ignore this flag.
255+
* \param error optional pointer to receive an error message if an error
256+
* occurred. If set, the memory it points to needs to be released via
257+
* \c llcas_string_dispose.
258+
* \returns one of \c llcas_lookup_result_t.
259+
*/
260+
LLCAS_PUBLIC llcas_lookup_result_t llcas_actioncache_get_for_digest(
261+
llcas_cas_t, llcas_digest_t key, llcas_objectid_t *p_value, bool globally,
262+
char **error);
263+
264+
/**
265+
* Like \c llcas_actioncache_get_for_digest but result is provided to a callback
266+
* function. Whether the call is asynchronous or not depends on the
267+
* implementation.
268+
*
269+
* \param ctx_cb pointer to pass to the callback function.
270+
*/
271+
LLCAS_PUBLIC void
272+
llcas_actioncache_get_for_digest_async(llcas_cas_t, llcas_digest_t key,
273+
bool globally, void *ctx_cb,
274+
llcas_actioncache_get_cb);
275+
276+
/**
277+
* Associates a \c llcas_objectid_t \p value with a \p key. It is invalid to set
278+
* a different \p value to the same \p key.
279+
*
280+
* \param globally if true it is a hint to the underlying implementation that
281+
* the association is profitable to be done on a distributed caching level, not
282+
* just locally. The implementation is free to ignore this flag.
283+
* \param error optional pointer to receive an error message if an error
284+
* occurred. If set, the memory it points to needs to be released via
285+
* \c llcas_string_dispose.
286+
* \returns true if there was an error, false otherwise.
287+
*/
288+
LLCAS_PUBLIC bool llcas_actioncache_put_for_digest(llcas_cas_t,
289+
llcas_digest_t key,
290+
llcas_objectid_t value,
291+
bool globally, char **error);
292+
293+
/**
294+
* Like \c llcas_actioncache_put_for_digest but result is provided to a callback
295+
* function. Whether the call is asynchronous or not depends on the
296+
* implementation.
297+
*
298+
* \param ctx_cb pointer to pass to the callback function.
299+
*/
300+
LLCAS_PUBLIC void
301+
llcas_actioncache_put_for_digest_async(llcas_cas_t, llcas_digest_t key,
302+
llcas_objectid_t value, bool globally,
303+
void *ctx_cb, llcas_actioncache_put_cb);
304+
305+
LLVM_C_EXTERN_C_END
306+
307+
#endif /* LLVM_C_CAS_PLUGINAPI_FUNCTIONS_H */

0 commit comments

Comments
 (0)