enhance(compiler): Python bindings: Forward llvm::Expected error messages in exceptions

The code in `lib/CAPI/Support/CompilerEngine.cpp` invokes several
functions returning an `llvm::Expected<T>`. When those fail, the error
message retrieved from the error object the `llvm::Expected<T>`
instance is written to the standard error stream via
`mlir::zamalang::log_error()` and an exception with a more generic
error message is thrown.

This causes errors to show up on the standard error stream in tests
generating errors on purpose and checking them, e.g.:

  ```
  tests/python/test_compiler_engine.py::test_compile_invalid[not
  @main] Compilation failed: cannot find the function for generate
  client parameters PASSED
  ```

This patch forwards the error message from an `llvm::Expected<T>`
instance in a runtime exception rather than writing it to the standard
error stream. Since exceptions are properly caught by the tests, no
errors show up during testing.
This commit is contained in:
Andi Drebes
2021-11-02 16:34:42 +01:00
parent 766b42fb49
commit 0cb5007c3c
3 changed files with 18 additions and 19 deletions

View File

@@ -3,7 +3,6 @@
#include "zamalang/Support/ExecutionArgument.h"
#include "zamalang/Support/Jit.h"
#include "zamalang/Support/JitCompilerEngine.h"
#include "zamalang/Support/logging.h"
// using mlir::zamalang::CompilerEngine;
using mlir::zamalang::ExecutionArgument;
@@ -15,11 +14,11 @@ mlir::zamalang::JitCompilerEngine::Lambda buildLambda(const char *module,
llvm::Expected<mlir::zamalang::JitCompilerEngine::Lambda> lambdaOrErr =
engine.buildLambda(module, funcName);
if (!lambdaOrErr) {
mlir::zamalang::log_error()
<< "Compilation failed: "
<< llvm::toString(std::move(lambdaOrErr.takeError())) << "\n";
throw std::runtime_error(
"failed compiling, see previous logs for more info");
std::string backingString;
llvm::raw_string_ostream os(backingString);
os << "Compilation failed: "
<< llvm::toString(std::move(lambdaOrErr.takeError()));
throw std::runtime_error(os.str());
}
return std::move(*lambdaOrErr);
}
@@ -53,11 +52,11 @@ uint64_t invokeLambda(lambda l, executionArguments args) {
delete lambdaArgumentsRef[i];
if (!resOrError) {
mlir::zamalang::log_error()
<< "Lambda invokation failed: "
<< llvm::toString(std::move(resOrError.takeError())) << "\n";
throw std::runtime_error(
"failed invoking lambda, see previous logs for more info");
std::string backingString;
llvm::raw_string_ostream os(backingString);
os << "Lambda invocation failed: "
<< llvm::toString(std::move(resOrError.takeError()));
throw std::runtime_error(os.str());
}
return *resOrError;
}
@@ -67,17 +66,17 @@ std::string roundTrip(const char *module) {
mlir::zamalang::CompilationContext::createShared();
mlir::zamalang::JitCompilerEngine ce{ccx};
std::string backingString;
llvm::raw_string_ostream os(backingString);
llvm::Expected<mlir::zamalang::CompilerEngine::CompilationResult> retOrErr =
ce.compile(module, mlir::zamalang::CompilerEngine::Target::ROUND_TRIP);
if (!retOrErr) {
mlir::zamalang::log_error()
<< llvm::toString(std::move(retOrErr.takeError())) << "\n";
throw std::runtime_error(
"mlir parsing failed, see previous logs for more info");
os << "MLIR parsing failed: "
<< llvm::toString(std::move(retOrErr.takeError()));
throw std::runtime_error(os.str());
}
std::string result;
llvm::raw_string_ostream os(result);
retOrErr->mlirModuleRef->get().print(os);
return os.str();
}