Skip to content

[MLIR][TranslateToLLVM] cannot link module during translation #169097

@SchrodingerZhu

Description

@SchrodingerZhu

Consider:

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<ReussirPolyFFIOp>(op)) {
      if (!polyffi.getCompiledModule())
        return op->emitError("PolyFFI operation has no compiled module");
      auto denseI8array =
          dyn_cast<DenseElementsAttr>(*polyffi.getCompiledModule());
      if (!denseI8array)
        return op->emitError("compiledModule is not a DenseElementsAttr");
      llvm::ArrayRef<char> bitcodeData = denseI8array.getRawData();
      std::unique_ptr<llvm::MemoryBuffer> memBuffer =
          llvm::MemoryBuffer::getMemBuffer(
              llvm::StringRef(bitcodeData.data(), bitcodeData.size()));
      llvm::Expected<std::unique_ptr<llvm::Module>> moduleOrErr =
          llvm::parseBitcodeFile(memBuffer->getMemBufferRef(),
                                 state.getLLVMContext());
      if (!moduleOrErr)
        return op->emitError("PolyFFI operation has invalid bitcode");
      std::unique_ptr<llvm::Module> 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions