From f0604af7ceb610e09a7915d07da9b36f7bca2b7e Mon Sep 17 00:00:00 2001 From: AidinT Date: Wed, 25 Mar 2026 19:56:13 +0100 Subject: [PATCH] [MLIR] Convert LLVMImportDialectInterface using ODS (#181923) This PR generates LLVMImportDialectInterface using ODS. --- .../include/mlir/Target/LLVMIR/CMakeLists.txt | 4 + .../LLVMIR/LLVMImportDialectInterface.td | 96 +++++++++++++++++++ .../mlir/Target/LLVMIR/LLVMImportInterface.h | 66 +------------ mlir/lib/Target/LLVMIR/CMakeLists.txt | 3 + .../llvm-project-overlay/mlir/BUILD.bazel | 11 +++ 5 files changed, 117 insertions(+), 63 deletions(-) create mode 100644 mlir/include/mlir/Target/LLVMIR/LLVMImportDialectInterface.td diff --git a/mlir/include/mlir/Target/LLVMIR/CMakeLists.txt b/mlir/include/mlir/Target/LLVMIR/CMakeLists.txt index 39b3674156da..8cf9a9cd864d 100644 --- a/mlir/include/mlir/Target/LLVMIR/CMakeLists.txt +++ b/mlir/include/mlir/Target/LLVMIR/CMakeLists.txt @@ -3,3 +3,7 @@ add_subdirectory(Transforms) set(LLVM_TARGET_DEFINITIONS LLVMTranslationDialectInterface.td) mlir_tablegen(LLVMTranslationDialectInterface.h.inc -gen-dialect-interface-decls) add_mlir_generic_tablegen_target(MLIRLLVMTranslationDialectInterfaceIncGen) + +set(LLVM_TARGET_DEFINITIONS LLVMImportDialectInterface.td) +mlir_tablegen(LLVMImportDialectInterface.h.inc -gen-dialect-interface-decls) +add_mlir_generic_tablegen_target(MLIRLLVMImportDialectInterfaceIncGen) diff --git a/mlir/include/mlir/Target/LLVMIR/LLVMImportDialectInterface.td b/mlir/include/mlir/Target/LLVMIR/LLVMImportDialectInterface.td new file mode 100644 index 000000000000..93e6fcf32708 --- /dev/null +++ b/mlir/include/mlir/Target/LLVMIR/LLVMImportDialectInterface.td @@ -0,0 +1,96 @@ +#ifndef MLIR_INTERFACES_LLVMIMPORTDIALECTINTERFACE +#define MLIR_INTERFACES_LLVMIMPORTDIALECTINTERFACE + +include "mlir/IR/Interfaces.td" + +def LLVMImportDialectInterface : DialectInterface<"LLVMImportDialectInterface"> { + let description = [{ + Base class for dialect interfaces used to import LLVM IR. Dialects that can + be imported should provide an implementation of this interface for the + supported intrinsics. The interface may be implemented in a separate library + to avoid the "main" dialect library depending on LLVM IR. The interface can + be attached using the delayed registration mechanism available in + DialectRegistry. + }]; + let cppNamespace = "::mlir"; + + let methods = [ + InterfaceMethod<[{ + Hook for derived dialect interfaces to implement the import of + intrinsics into MLIR. + }], + "::llvm::LogicalResult", "convertIntrinsic", + (ins "::mlir::OpBuilder &":$builder, "::llvm::CallInst *":$inst, + "::mlir::LLVM::ModuleImport &":$moduleImport), + [{ + return ::llvm::failure(); + }] + >, + InterfaceMethod<[{ + Hook for derived dialect interfaces to implement the import of + instructions into MLIR. + }], + "::llvm::LogicalResult", "convertInstruction", + (ins "::mlir::OpBuilder &":$builder, "::llvm::Instruction *":$inst, + "::mlir::ArrayRef":$llvmOperands, + "::mlir::LLVM::ModuleImport &":$moduleImport), + [{ + return ::llvm::failure(); + }] + >, + InterfaceMethod<[{ + Hook for derived dialect interfaces to implement the import of metadata + into MLIR. Attaches the converted metadata kind and node to the provided + operation. + }], + "::mlir::LogicalResult", "setMetadataAttrs", + (ins "::mlir::OpBuilder &":$builder, "unsigned":$kind, + "::llvm::MDNode *":$node, "::mlir::Operation *":$op, + "::mlir::LLVM::ModuleImport &":$moduleImport), + [{ + return ::llvm::failure(); + }] + >, + InterfaceMethod<[{ + Hook for derived dialect interfaces to publish the supported intrinsics. + As every LLVM IR intrinsic has a unique integer identifier, the function + returns the list of supported intrinsic identifiers. + }], + "::mlir::ArrayRef", "getSupportedIntrinsics", (ins), + [{ + return {}; + }] + >, + InterfaceMethod<[{ + Hook for derived dialect interfaces to publish the supported instructions. + As every LLVM IR instruction has a unique integer identifier, the function + returns the list of supported instruction identifiers. These identifiers + will then be used to match LLVM instructions to the appropriate import + interface and `convertInstruction` method. It is an error to have multiple + interfaces overriding the same instruction. + }], + "::mlir::ArrayRef", "getSupportedInstructions", (ins), + [{ + return {}; + }] + >, + InterfaceMethod<[{ + Hook for derived dialect interfaces to publish the supported metadata + kinds. As every metadata kind has a unique integer identifier, the + function returns the list of supported metadata identifiers. The + `llvmContext` parameter is used to obtain identifiers for metadata kinds + that do not have a fixed static identifier. Since different LLVM contexts + can assign different identifiers to these non-static metadata kinds, the + function must recompute the list of supported metadata identifiers on each + call. + }], + "::mlir::SmallVector", "getSupportedMetadata", + (ins "::llvm::LLVMContext &":$llvmContext), + [{ + return {}; + }] + > + ]; +} + +#endif diff --git a/mlir/include/mlir/Target/LLVMIR/LLVMImportInterface.h b/mlir/include/mlir/Target/LLVMIR/LLVMImportInterface.h index 0e50fac7e85d..1c1d56b720a2 100644 --- a/mlir/include/mlir/Target/LLVMIR/LLVMImportInterface.h +++ b/mlir/include/mlir/Target/LLVMIR/LLVMImportInterface.h @@ -31,71 +31,11 @@ namespace mlir { namespace LLVM { class ModuleImport; } // namespace LLVM +} // namespace mlir -/// Base class for dialect interfaces used to import LLVM IR. Dialects that can -/// be imported should provide an implementation of this interface for the -/// supported intrinsics. The interface may be implemented in a separate library -/// to avoid the "main" dialect library depending on LLVM IR. The interface can -/// be attached using the delayed registration mechanism available in -/// DialectRegistry. -class LLVMImportDialectInterface - : public DialectInterface::Base { -public: - LLVMImportDialectInterface(Dialect *dialect) : Base(dialect) {} - - /// Hook for derived dialect interfaces to implement the import of - /// intrinsics into MLIR. - virtual LogicalResult - convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst, - LLVM::ModuleImport &moduleImport) const { - return failure(); - } - - /// Hook for derived dialect interfaces to implement the import of - /// instructions into MLIR. - virtual LogicalResult - convertInstruction(OpBuilder &builder, llvm::Instruction *inst, - ArrayRef llvmOperands, - LLVM::ModuleImport &moduleImport) const { - return failure(); - } - - /// Hook for derived dialect interfaces to implement the import of metadata - /// into MLIR. Attaches the converted metadata kind and node to the provided - /// operation. - virtual LogicalResult - setMetadataAttrs(OpBuilder &builder, unsigned kind, llvm::MDNode *node, - Operation *op, LLVM::ModuleImport &moduleImport) const { - return failure(); - } - - /// Hook for derived dialect interfaces to publish the supported intrinsics. - /// As every LLVM IR intrinsic has a unique integer identifier, the function - /// returns the list of supported intrinsic identifiers. - virtual ArrayRef getSupportedIntrinsics() const { return {}; } - - /// Hook for derived dialect interfaces to publish the supported instructions. - /// As every LLVM IR instruction has a unique integer identifier, the function - /// returns the list of supported instruction identifiers. These identifiers - /// will then be used to match LLVM instructions to the appropriate import - /// interface and `convertInstruction` method. It is an error to have multiple - /// interfaces overriding the same instruction. - virtual ArrayRef getSupportedInstructions() const { return {}; } - - /// Hook for derived dialect interfaces to publish the supported metadata - /// kinds. As every metadata kind has a unique integer identifier, the - /// function returns the list of supported metadata identifiers. The - /// `llvmContext` parameter is used to obtain identifiers for metadata kinds - /// that do not have a fixed static identifier. Since different LLVM contexts - /// can assign different identifiers to these non-static metadata kinds, the - /// function must recompute the list of supported metadata identifiers on each - /// call. - virtual SmallVector - getSupportedMetadata(llvm::LLVMContext &llvmContext) const { - return {}; - } -}; +#include "mlir/Target/LLVMIR/LLVMImportDialectInterface.h.inc" +namespace mlir { /// Interface collection for the import of LLVM IR that dispatches to a concrete /// dialect interface implementation. Queries the dialect interfaces to obtain a /// list of the supported LLVM IR constructs and then builds a mapping for the diff --git a/mlir/lib/Target/LLVMIR/CMakeLists.txt b/mlir/lib/Target/LLVMIR/CMakeLists.txt index a73a78d17c13..db221697edd0 100644 --- a/mlir/lib/Target/LLVMIR/CMakeLists.txt +++ b/mlir/lib/Target/LLVMIR/CMakeLists.txt @@ -76,6 +76,9 @@ add_mlir_translation_library(MLIRTargetLLVMIRImport ADDITIONAL_HEADER_DIRS ${MLIR_MAIN_INCLUDE_DIR}/mlir/Target/LLVMIR + DEPENDS + MLIRLLVMImportDialectInterfaceIncGen + LINK_COMPONENTS Core IRReader diff --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel index 8998cf8b047a..40fdc056857d 100644 --- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel @@ -9255,6 +9255,16 @@ cc_library( ], ) +gentbl_cc_library( + name = "LLVMImportDialectInterfaceIncGen", + tbl_outs = { + "include/mlir/Target/LLVMIR/LLVMImportDialectInterface.h.inc": ["-gen-dialect-interface-decls"], + }, + tblgen = ":mlir-tblgen", + td_file = "include/mlir/Target/LLVMIR/LLVMImportDialectInterface.td", + deps = [":OpBaseTdFiles"], +) + cc_library( name = "FromLLVMIRTranslation", srcs = [ @@ -9282,6 +9292,7 @@ cc_library( ":IR", ":LLVMConversionIncGen", ":LLVMDialect", + ":LLVMImportDialectInterfaceIncGen", ":LLVMIntrinsicConversionIncGen", ":Support", ":TranslateLib",