Skip to content

Commit 1f29e42

Browse files
[Caching] Add new CacheReplay APIs to libSwiftScan
Add new APIs libSwiftScan that can be used for cache query and cache replay. This enables swift-driver or build system to query the cache and replay the compilation results without invocation swift-frontend for better scheduling.
1 parent 3dda449 commit 1f29e42

File tree

3 files changed

+907
-37
lines changed

3 files changed

+907
-37
lines changed

include/swift-c/DependencyScan/DependencyScan.h

Lines changed: 172 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/// SWIFTSCAN_VERSION_MINOR should increase when there are API additions.
2626
/// SWIFTSCAN_VERSION_MAJOR is intended for "major" source/ABI breaking changes.
2727
#define SWIFTSCAN_VERSION_MAJOR 0
28-
#define SWIFTSCAN_VERSION_MINOR 5
28+
#define SWIFTSCAN_VERSION_MINOR 6
2929

3030
SWIFTSCAN_BEGIN_DECLS
3131

@@ -441,6 +441,53 @@ typedef struct swiftscan_cas_options_s *swiftscan_cas_options_t;
441441
/// ActionCache.
442442
typedef struct swiftscan_cas_s *swiftscan_cas_t;
443443

444+
/// Opaque container for a cached compilation.
445+
typedef struct swiftscan_cached_compilation_s *swiftscan_cached_compilation_t;
446+
447+
/// Opaque type for a cache replay instance.
448+
typedef struct swiftscan_cache_replay_instance_s
449+
*swiftscan_cache_replay_instance_t;
450+
451+
/// Opaque type for a cancellation token for async cache operations.
452+
typedef struct swiftscan_cache_cancellation_token_s
453+
*swiftscan_cache_cancellation_token_t;
454+
455+
/// Enum types for output types for cache key computation.
456+
/// List should contain all the output file types, including supplemententary
457+
/// outputs, except diagnostics outputs, which are covered by cached diagnostic
458+
/// entry.
459+
typedef enum {
460+
SWIFTSCAN_OUTPUT_TYPE_OBJECT = 0,
461+
SWIFTSCAN_OUTPUT_TYPE_SWIFTMODULE = 1,
462+
SWIFTSCAN_OUTPUT_TYPE_SWIFTINTERFACE = 2,
463+
SWIFTSCAN_OUTPUT_TYPE_SWIFTPRIVATEINTERFACE = 3,
464+
SWIFTSCAN_OUTPUT_TYPE_CLANG_MODULE = 4,
465+
SWIFTSCAN_OUTPUT_TYPE_CLANG_PCH = 5,
466+
SWIFTSCAN_OUTPUT_TYPE_CLANG_HEADER = 6,
467+
SWIFTSCAN_OUTPUT_TYPE_SWIFT_SOURCE_INFO = 7,
468+
SWIFTSCAN_OUTPUT_TYPE_SWIFT_MODULE_DOC = 8,
469+
SWIFTSCAN_OUTPUT_TYPE_DEPENDENCIES = 9,
470+
SWIFTSCAN_OUTPUT_TYPE_SWIFT_DEPS = 10,
471+
SWIFTSCAN_OUTPUT_TYPE_MODULE_TRACE = 11,
472+
SWIFTSCAN_OUTPUT_TYPE_TBD = 12,
473+
SWIFTSCAN_OUTPUT_TYPE_SWIFT_MODULE_SUMMARY = 13,
474+
SWIFTSCAN_OUTPUT_TYPE_SWIFT_ABI_DESCRIPTOR = 14,
475+
SWIFTSCAN_OUTPUT_TYPE_SWIFT_API_DESCRIPTOR = 15,
476+
SWIFTSCAN_OUTPUT_TYPE_CONST_VALUE = 16,
477+
SWIFTSCAN_OUTPUT_TYPE_MODULE_SEMANTIC_INFO = 17,
478+
SWIFTSCAN_OUTPUT_TYPE_YAML_OPT_RECORD = 18,
479+
SWIFTSCAN_OUTPUT_TYPE_BITSTREAM_OPT_RECORD = 19,
480+
SWIFTSCAN_OUTPUT_TYPE_CACHED_DIAGNOSTICS = 20,
481+
SWIFTSCAN_OUTPUT_TYPE_LAST = SWIFTSCAN_OUTPUT_TYPE_CACHED_DIAGNOSTICS
482+
} swiftscan_output_kind_t;
483+
484+
/// Enum types for cache result lookup or replay.
485+
typedef enum {
486+
SWIFTSCAN_CACHE_RESULT_SUCCESS = 0,
487+
SWIFTSCAN_CACHE_RESULT_NOT_FOUND = 1,
488+
SWIFTSCAN_CACHE_RESULT_ERROR = 2,
489+
} swiftscan_cache_result_t;
490+
444491
/// Create a \c CASOptions for creating CAS inside scanner specified.
445492
SWIFTSCAN_PUBLIC swiftscan_cas_options_t swiftscan_cas_options_create(void);
446493

@@ -492,6 +539,130 @@ SWIFTSCAN_PUBLIC swiftscan_string_ref_t
492539
swiftscan_cache_compute_key(swiftscan_cas_t cas, int argc, const char **argv,
493540
const char *input, swiftscan_string_ref_t *error);
494541

542+
/// Query the result of the compilation using the output cache key. \c globally
543+
/// suggests if the lookup should check remote cache if such operation exists.
544+
/// Returns the CachedCompilation of the result if found, or nullptr if output
545+
/// is not found or an error occurs. When an error occurs, the error message is
546+
/// returned via \c error parameter and its caller needs to free the message
547+
/// using `swiftscan_string_dispose`. The returned CachedCompilation needs to be
548+
/// freed via `swiftscan_cached_compilation_dispose`.
549+
SWIFTSCAN_PUBLIC swiftscan_cached_compilation_t
550+
swiftscan_cache_query(swiftscan_cas_t cas, const char *key, bool globally,
551+
swiftscan_string_ref_t *error);
552+
553+
/// Async version of `swiftscan_cache_query` where result is returned via
554+
/// callback. Both cache_result enum and CachedCompilation will be provided to
555+
/// callback. \c ctx is an opaque value that passed to the callback and \c
556+
/// swiftscan_cache_cancellation_token_t will return an token that can be used
557+
/// to cancel the async operation. The token needs to be freed by caller using
558+
/// `swiftscan_cache_cancellation_token_dispose`.
559+
SWIFTSCAN_PUBLIC void swiftscan_cache_query_async(
560+
swiftscan_cas_t cas, const char *key, bool globally, void *ctx,
561+
void (*callback)(void *ctx, swiftscan_cache_result_t,
562+
swiftscan_cached_compilation_t,
563+
swiftscan_string_ref_t error),
564+
swiftscan_cache_cancellation_token_t *);
565+
566+
/// Dispose a CachedCompilation.
567+
SWIFTSCAN_PUBLIC void
568+
swiftscan_cached_compilation_dispose(swiftscan_cached_compilation_t);
569+
570+
/// Download and materialized the CASObject referenced by the CASID in the local
571+
/// CAS if needed from a remote CAS.
572+
/// If the return value is SWIFTSCAN_CACHE_RESULT_ERROR, the error message is
573+
/// returned via \c error parameter and its caller needs to free the message
574+
/// using `swiftscan_string_dispose`.
575+
SWIFTSCAN_PUBLIC swiftscan_cache_result_t
576+
swiftscan_cache_load_cached_compilation(swiftscan_cas_t cas,
577+
swiftscan_cached_compilation_t,
578+
swiftscan_string_ref_t *error);
579+
580+
/// Async version of `swiftscan_cache_load_cached_compilation` where result is
581+
/// returned via callback. \c ctx is an opaque value that passed to the callback
582+
/// and \c swiftscan_cache_cancellation_token_t will return an token that can be
583+
/// used to cancel the async operation. The token needs to be freed by caller
584+
/// using `swiftscan_cache_cancellation_token_dispose`.
585+
SWIFTSCAN_PUBLIC void swiftscan_cache_load_cached_compilation_async(
586+
swiftscan_cas_t cas, swiftscan_cached_compilation_t, void *ctx,
587+
void (*callback)(void *ctx, swiftscan_cache_result_t,
588+
swiftscan_string_ref_t error),
589+
swiftscan_cache_cancellation_token_t *);
590+
591+
/// Check if CachedCompilation is materialized locally and can be accessed
592+
/// without downloading.
593+
SWIFTSCAN_PUBLIC bool
594+
swiftscan_cache_compilation_is_loaded(swiftscan_cached_compilation_t);
595+
596+
/// Query the number of outputs from a cached compilation. The cached
597+
/// compilation needs to be loaded, otherwise will return error. The error
598+
/// message is returned via \c error parameter and its caller needs to free the
599+
/// message using `swiftscan_string_dispose`.
600+
SWIFTSCAN_PUBLIC unsigned
601+
swiftscan_cache_get_num_cached_outputs(swiftscan_cached_compilation_t,
602+
swiftscan_string_ref_t *error);
603+
604+
/// Query if the kind of otuput exists in a cached compilation. The cached
605+
/// compilation needs to be loaded, otherwise will return error. The error
606+
/// message is returned via \c error parameter and its caller needs to free the
607+
/// message using `swiftscan_string_dispose`.
608+
SWIFTSCAN_PUBLIC bool
609+
swiftscan_cache_compilation_has_output_kind(swiftscan_cached_compilation_t,
610+
swiftscan_output_kind_t,
611+
swiftscan_string_ref_t *error);
612+
613+
/// Make the cache compilation available globally. \c callback will be called
614+
/// on completion.
615+
/// \c swiftscan_cache_cancellation_token_t will return an token that can be
616+
/// used to cancel the async operation. The token needs to be freed by caller
617+
/// using `swiftscan_cache_cancellation_token_dispose`.
618+
SWIFTSCAN_PUBLIC void swiftscan_cache_make_global_async(
619+
swiftscan_cas_t cas, const char *key, void *ctx,
620+
void (*callback)(void *ctx, swiftscan_string_ref_t error),
621+
swiftscan_cache_cancellation_token_t *);
622+
623+
/// Cancel the async cache action that is associated with token.
624+
SWIFTSCAN_PUBLIC void
625+
swiftscan_cache_action_cancel(swiftscan_cache_cancellation_token_t);
626+
627+
/// Dispose the cancellation token.
628+
SWIFTSCAN_PUBLIC void swiftscan_cache_cancellation_token_dispose(
629+
swiftscan_cache_cancellation_token_t);
630+
631+
/// Create a swift cached compilation replay instance with its command-line
632+
/// invocation. Return nullptr when errors occurs and the error message is
633+
/// returned via \c error parameter and its caller needs to free the message
634+
/// using `swiftscan_string_dispose`.
635+
SWIFTSCAN_PUBLIC swiftscan_cache_replay_instance_t
636+
swiftscan_cache_replay_instance_create(int argc, const char **argv,
637+
swiftscan_string_ref_t *error);
638+
639+
/// Dispose swift cached compilation replay instance.
640+
SWIFTSCAN_PUBLIC void
641+
swiftscan_cache_replay_instance_dispose(swiftscan_cache_replay_instance_t);
642+
643+
/// Replay the cached compilation using cached compliation replay instance.
644+
/// If the return value is SWIFTSCAN_CACHE_RESULT_ERROR, the error message is
645+
/// returned via \c error parameter and its caller needs to free the message
646+
/// using `swiftscan_string_dispose`.
647+
SWIFTSCAN_PUBLIC swiftscan_cache_result_t swiftscan_cache_replay_compilation(
648+
swiftscan_cas_t cas, swiftscan_cache_replay_instance_t,
649+
swiftscan_cached_compilation_t, swiftscan_string_ref_t *error);
650+
651+
/// Async replay the cached compilation. Callback function will be provided
652+
/// by the cache replay result, and its stdout, stderr, and any error message.
653+
/// \c ctx is an opaque value that passed to the callback and
654+
/// \c swiftscan_cache_cancellation_token_t will return an token that can be
655+
/// used to cancel the async operation. The token needs to be freed by caller
656+
/// using `swiftscan_cache_cancellation_token_dispose`.
657+
SWIFTSCAN_PUBLIC void swiftscan_cache_replay_compilation_async(
658+
swiftscan_cas_t cas, swiftscan_cache_replay_instance_t,
659+
swiftscan_cached_compilation_t, void *ctx,
660+
void (*callback)(void *ctx, swiftscan_cache_result_t,
661+
swiftscan_string_ref_t std_out,
662+
swiftscan_string_ref_t std_err,
663+
swiftscan_string_ref_t error),
664+
swiftscan_cache_cancellation_token_t *);
665+
495666
//===----------------------------------------------------------------------===//
496667

497668
SWIFTSCAN_END_DECLS

0 commit comments

Comments
 (0)