llvm / llvm/llvm-project

[MLIR][TranslateToLLVM] cannot link module during translation

Open
#169,097 1 comment 0 reactions 0 assignees View on GitHub
mlir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Consider:
```c++
struct ReussirLLVMTranslation : public mlir::LLVMTranslationDialectInterface {
using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;

LogicalResult
convertOperation(Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &state) const override {
if (auto polyffi = dyn_cast(op)) {
if (!polyffi.getCompiledModule())
return op->emitError("PolyFFI operation has no compiled module");
auto denseI8array =
dyn_cast(*polyffi.getCompiledModule());
if (!denseI8array)
return op->emitError("compiledModule is not a DenseElementsAttr");
llvm::ArrayRef bitcodeData = denseI8array.getRawData();
std::unique_ptr memBuffer =
llvm::MemoryBuffer::getMemBuffer(
llvm::StringRef(bitcodeData.data(), bitcodeData.size()));
llvm::Expected> moduleOrErr =
llvm::parseBitcodeFile(memBuffer->getMemBufferRef(),
state.getLLVMContext());
if (!moduleOrErr)
return op->emitError("PolyFFI operation has invalid bitcode");
std::unique_ptr innerModule = std::move(*moduleOrErr);
auto layout = state.getLLVMModule()->getDataLayout();
innerModule->setDataLayout(layout);
bool flag = llvm::Linker::linkModules(*state.getLLVMModule(),
std::move(innerModule));
if (flag)
return op->emitError("PolyFFI operation cannot be linked");
return success();
}
return failure();
}
};
```

This approach will leave the ModuleTranslation in a broken state where further operation translation can abort the program.

I wonder if we can have a way to recover the state after linker invocation.

Of cuz, a way to avoid this is to use customized translation rather than the LLVMTranslationDialectInterface, but that seems to be requiring more efforts.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.