[MLIR] Convert LLVMImportDialectInterface using ODS (#181923)

This PR generates LLVMImportDialectInterface using ODS.
This commit is contained in:
AidinT 2026-03-25 19:56:13 +01:00 committed by GitHub
parent 4380ae6dbb
commit f0604af7ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 117 additions and 63 deletions

View File

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

View File

@ -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<llvm::Value *>":$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<unsigned>", "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<unsigned>", "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<unsigned>", "getSupportedMetadata",
(ins "::llvm::LLVMContext &":$llvmContext),
[{
return {};
}]
>
];
}
#endif

View File

@ -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<LLVMImportDialectInterface> {
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<llvm::Value *> 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<unsigned> 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<unsigned> 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<unsigned>
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

View File

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

View File

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