diff --git a/compiler/include/concretelang-c/Support/CompilerEngine.h b/compiler/include/concretelang-c/Support/CompilerEngine.h index eb447abc8..28012b345 100644 --- a/compiler/include/concretelang-c/Support/CompilerEngine.h +++ b/compiler/include/concretelang-c/Support/CompilerEngine.h @@ -63,7 +63,9 @@ struct LibrarySupport_C { typedef struct LibrarySupport_C LibrarySupport_C; MLIR_CAPI_EXPORTED LibrarySupport_C -library_support(const char *outputPath, const char *runtimeLibraryPath); +library_support(const char *outputPath, const char *runtimeLibraryPath, + bool generateSharedLib, bool generateStaticLib, + bool generateClientParameters, bool generateCppHeader); MLIR_CAPI_EXPORTED std::unique_ptr library_compile(LibrarySupport_C support, const char *module, diff --git a/compiler/include/concretelang/Support/CompilerEngine.h b/compiler/include/concretelang/Support/CompilerEngine.h index b94274c21..23db449f1 100644 --- a/compiler/include/concretelang/Support/CompilerEngine.h +++ b/compiler/include/concretelang/Support/CompilerEngine.h @@ -102,7 +102,8 @@ public: llvm::Expected addCompilation(CompilationResult &compilation); /** Emit the library artifacts with the previously added compilation result */ - llvm::Error emitArtifacts(); + llvm::Error emitArtifacts(bool sharedLib, bool staticLib, + bool clientParameters, bool cppHeader); /** After a shared library has been emitted, its path is here */ std::string sharedLibraryPath; /** After a static library has been emitted, its path is here */ @@ -202,13 +203,17 @@ public: llvm::Expected compile(std::vector inputs, std::string libraryPath, - std::string runtimeLibraryPath = ""); + std::string runtimeLibraryPath = "", bool generateSharedLib = true, + bool generateStaticLib = true, bool generateClientParameters = true, + bool generateCppHeader = true); /// Compile and emit artifact to the given libraryPath from an LLVM source /// manager. llvm::Expected compile(llvm::SourceMgr &sm, std::string libraryPath, - std::string runtimeLibraryPath = ""); + std::string runtimeLibraryPath = "", bool generateSharedLib = true, + bool generateStaticLib = true, bool generateClientParameters = true, + bool generateCppHeader = true); void setCompilationOptions(CompilationOptions &options) { compilerOptions = options; diff --git a/compiler/include/concretelang/Support/LibrarySupport.h b/compiler/include/concretelang/Support/LibrarySupport.h index 370684e4b..d643fb84d 100644 --- a/compiler/include/concretelang/Support/LibrarySupport.h +++ b/compiler/include/concretelang/Support/LibrarySupport.h @@ -33,8 +33,15 @@ class LibrarySupport : public LambdaSupport { public: - LibrarySupport(std::string outputPath, std::string runtimeLibraryPath = "") - : outputPath(outputPath), runtimeLibraryPath(runtimeLibraryPath) {} + LibrarySupport(std::string outputPath, std::string runtimeLibraryPath = "", + bool generateSharedLib = true, bool generateStaticLib = true, + bool generateClientParameters = true, + bool generateCppHeader = true) + : outputPath(outputPath), runtimeLibraryPath(runtimeLibraryPath), + generateSharedLib(generateSharedLib), + generateStaticLib(generateStaticLib), + generateClientParameters(generateClientParameters), + generateCppHeader(generateCppHeader) {} llvm::Expected> compile(llvm::SourceMgr &program, CompilationOptions options) override { @@ -44,7 +51,9 @@ public: engine.setCompilationOptions(options); // Compile to a library - auto library = engine.compile(program, outputPath, runtimeLibraryPath); + auto library = engine.compile(program, outputPath, runtimeLibraryPath, + generateSharedLib, generateStaticLib, + generateClientParameters, generateCppHeader); if (auto err = library.takeError()) { return std::move(err); } @@ -100,6 +109,11 @@ public: private: std::string outputPath; std::string runtimeLibraryPath; + // Flags to select generated artifacts + bool generateSharedLib; + bool generateStaticLib; + bool generateClientParameters; + bool generateCppHeader; }; } // namespace concretelang diff --git a/compiler/lib/Bindings/Python/CompilerAPIModule.cpp b/compiler/lib/Bindings/Python/CompilerAPIModule.cpp index 4c1d0d3a4..4aad197b3 100644 --- a/compiler/lib/Bindings/Python/CompilerAPIModule.cpp +++ b/compiler/lib/Bindings/Python/CompilerAPIModule.cpp @@ -104,10 +104,15 @@ void mlir::concretelang::python::populateCompilerAPISubmodule( })); pybind11::class_(m, "LibraryLambda"); pybind11::class_(m, "LibrarySupport") - .def(pybind11::init([](std::string outputPath, - std::string runtimeLibraryPath) { - return library_support(outputPath.c_str(), runtimeLibraryPath.c_str()); - })) + .def(pybind11::init( + [](std::string outputPath, std::string runtimeLibraryPath, + bool generateSharedLib, bool generateStaticLib, + bool generateClientParameters, bool generateCppHeader) { + return library_support(outputPath.c_str(), + runtimeLibraryPath.c_str(), + generateSharedLib, generateStaticLib, + generateClientParameters, generateCppHeader); + })) .def("compile", [](LibrarySupport_C &support, std::string mlir_program, mlir::concretelang::CompilationOptions options) { diff --git a/compiler/lib/Bindings/Python/concrete/compiler/library_support.py b/compiler/lib/Bindings/Python/concrete/compiler/library_support.py index aade24dfd..6c767e1b8 100644 --- a/compiler/lib/Bindings/Python/concrete/compiler/library_support.py +++ b/compiler/lib/Bindings/Python/concrete/compiler/library_support.py @@ -67,6 +67,10 @@ class LibrarySupport(WrapperCpp): def new( output_path: str = DEFAULT_OUTPUT_PATH, runtime_library_path: Optional[str] = None, + generateSharedLib: bool = True, + generateStaticLib: bool = False, + generateClientParameters: bool = True, + generateCppHeader: bool = False, ) -> "LibrarySupport": """Build a LibrarySupport. @@ -74,10 +78,15 @@ class LibrarySupport(WrapperCpp): output_path (str, optional): path where to store compiled libraries. Defaults to DEFAULT_OUTPUT_PATH. runtime_library_path (Optional[str], optional): path to the runtime library. Defaults to None. + generateSharedLib (bool): whether to emit shared library or not. Default to True. + generateStaticLib (bool): whether to emit static library or not. Default to False. + generateClientParameters (bool): whether to emit client parameters or not. Default to True. + generateCppHeader (bool): whether to emit cpp header or not. Default to False. Raises: TypeError: if output_path is not of type str TypeError: if runtime_library_path is not of type str + TypeError: if one of the generation flags is not of type bool Returns: LibrarySupport @@ -90,8 +99,23 @@ class LibrarySupport(WrapperCpp): raise TypeError( f"runtime_library_path must be of type str, not {type(runtime_library_path)}" ) + for name, value in [ + ("generateSharedLib", generateSharedLib), + ("generateStaticLib", generateStaticLib), + ("generateClientParameters", generateClientParameters), + ("generateCppHeader", generateCppHeader), + ]: + if not isinstance(value, bool): + raise TypeError(f"{name} must be of type bool, not {type(value)}") library_support = LibrarySupport.wrap( - _LibrarySupport(output_path, runtime_library_path) + _LibrarySupport( + output_path, + runtime_library_path, + generateSharedLib, + generateStaticLib, + generateClientParameters, + generateCppHeader, + ) ) library_support.library_path = output_path return library_support diff --git a/compiler/lib/CAPI/Support/CompilerEngine.cpp b/compiler/lib/CAPI/Support/CompilerEngine.cpp index 9c3868a26..e45f7132b 100644 --- a/compiler/lib/CAPI/Support/CompilerEngine.cpp +++ b/compiler/lib/CAPI/Support/CompilerEngine.cpp @@ -60,9 +60,12 @@ jit_server_call(JITSupport_C support, mlir::concretelang::JITLambda &lambda, // Library Support bindings /////////////////////////////////////////////////// MLIR_CAPI_EXPORTED LibrarySupport_C -library_support(const char *outputPath, const char *runtimeLibraryPath) { - return LibrarySupport_C{ - mlir::concretelang::LibrarySupport(outputPath, runtimeLibraryPath)}; +library_support(const char *outputPath, const char *runtimeLibraryPath, + bool generateSharedLib, bool generateStaticLib, + bool generateClientParameters, bool generateCppHeader) { + return LibrarySupport_C{mlir::concretelang::LibrarySupport( + outputPath, runtimeLibraryPath, generateSharedLib, generateStaticLib, + generateClientParameters, generateCppHeader)}; } std::unique_ptr diff --git a/compiler/lib/Support/CompilerEngine.cpp b/compiler/lib/Support/CompilerEngine.cpp index 454e77bba..ecc4fb3c2 100644 --- a/compiler/lib/Support/CompilerEngine.cpp +++ b/compiler/lib/Support/CompilerEngine.cpp @@ -372,8 +372,9 @@ CompilerEngine::compile(std::unique_ptr buffer, llvm::Expected CompilerEngine::compile(std::vector inputs, - std::string libraryPath, - std::string runtimeLibraryPath) { + std::string libraryPath, std::string runtimeLibraryPath, + bool generateSharedLib, bool generateStaticLib, + bool generateClientParameters, bool generateCppHeader) { using Library = mlir::concretelang::CompilerEngine::Library; auto outputLib = std::make_shared(libraryPath, runtimeLibraryPath); auto target = CompilerEngine::Target::LIBRARY; @@ -384,7 +385,9 @@ CompilerEngine::compile(std::vector inputs, << llvm::toString(compilation.takeError()); } } - if (auto err = outputLib->emitArtifacts()) { + if (auto err = outputLib->emitArtifacts(generateSharedLib, generateStaticLib, + generateClientParameters, + generateCppHeader)) { return StreamStringError("Can't emit artifacts: ") << llvm::toString(std::move(err)); } @@ -393,7 +396,9 @@ CompilerEngine::compile(std::vector inputs, llvm::Expected CompilerEngine::compile(llvm::SourceMgr &sm, std::string libraryPath, - std::string runtimeLibraryPath) { + std::string runtimeLibraryPath, bool generateSharedLib, + bool generateStaticLib, bool generateClientParameters, + bool generateCppHeader) { using Library = mlir::concretelang::CompilerEngine::Library; auto outputLib = std::make_shared(libraryPath, runtimeLibraryPath); auto target = CompilerEngine::Target::LIBRARY; @@ -404,7 +409,9 @@ CompilerEngine::compile(llvm::SourceMgr &sm, std::string libraryPath, << llvm::toString(compilation.takeError()); } - if (auto err = outputLib->emitArtifacts()) { + if (auto err = outputLib->emitArtifacts(generateSharedLib, generateStaticLib, + generateClientParameters, + generateCppHeader)) { return StreamStringError("Can't emit artifacts: ") << llvm::toString(std::move(err)); } @@ -680,18 +687,29 @@ llvm::Expected CompilerEngine::Library::emitStatic() { return path; } -llvm::Error CompilerEngine::Library::emitArtifacts() { - if (auto err = emitShared().takeError()) { - return err; +llvm::Error CompilerEngine::Library::emitArtifacts(bool sharedLib, + bool staticLib, + bool clientParameters, + bool cppHeader) { + if (sharedLib) { + if (auto err = emitShared().takeError()) { + return err; + } } - if (auto err = emitStatic().takeError()) { - return err; + if (staticLib) { + if (auto err = emitStatic().takeError()) { + return err; + } } - if (auto err = emitClientParametersJSON().takeError()) { - return err; + if (clientParameters) { + if (auto err = emitClientParametersJSON().takeError()) { + return err; + } } - if (auto err = emitCppHeader().takeError()) { - return err; + if (cppHeader) { + if (auto err = emitCppHeader().takeError()) { + return err; + } } return llvm::Error::success(); } diff --git a/compiler/src/main.cpp b/compiler/src/main.cpp index 2f1cb9356..ef64bbd2b 100644 --- a/compiler/src/main.cpp +++ b/compiler/src/main.cpp @@ -458,7 +458,9 @@ mlir::LogicalResult compilerMain(int argc, char **argv) { } if (cmdline::action == Action::COMPILE) { - auto err = outputLib->emitArtifacts(); + auto err = + outputLib->emitArtifacts(/*sharedLib=*/true, /*staticLib=*/true, + /*clientParameters=*/true, /*cppHeader=*/true); if (err) { return mlir::failure(); }