feat: parameterize artifact generation in lib compilation

This commit is contained in:
youben11
2022-04-28 13:57:11 +01:00
committed by Ayoub Benaissa
parent 61052a237b
commit f223f02ab7
8 changed files with 103 additions and 30 deletions

View File

@@ -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<mlir::concretelang::LibraryCompilationResult>
library_compile(LibrarySupport_C support, const char *module,

View File

@@ -102,7 +102,8 @@ public:
llvm::Expected<std::string> 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<CompilerEngine::Library>
compile(std::vector<std::string> 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<CompilerEngine::Library>
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;

View File

@@ -33,8 +33,15 @@ class LibrarySupport
: public LambdaSupport<serverlib::ServerLambda, LibraryCompilationResult> {
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<std::unique_ptr<LibraryCompilationResult>>
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

View File

@@ -104,10 +104,15 @@ void mlir::concretelang::python::populateCompilerAPISubmodule(
}));
pybind11::class_<concretelang::serverlib::ServerLambda>(m, "LibraryLambda");
pybind11::class_<LibrarySupport_C>(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) {

View File

@@ -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

View File

@@ -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<mlir::concretelang::LibraryCompilationResult>

View File

@@ -372,8 +372,9 @@ CompilerEngine::compile(std::unique_ptr<llvm::MemoryBuffer> buffer,
llvm::Expected<CompilerEngine::Library>
CompilerEngine::compile(std::vector<std::string> 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<Library>(libraryPath, runtimeLibraryPath);
auto target = CompilerEngine::Target::LIBRARY;
@@ -384,7 +385,9 @@ CompilerEngine::compile(std::vector<std::string> 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<std::string> inputs,
llvm::Expected<CompilerEngine::Library>
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<Library>(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<std::string> 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();
}

View File

@@ -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();
}