Skip to content

Commit

Permalink
[mlir][llvm] Tablegen based operation import from LLVM IR.
Browse files Browse the repository at this point in the history
The revision uses tablegen generated builders to convert the most common
LLVM IR instructions to MLIR LLVM dialect operations. All instructions
with special handlers, except for alloca and fence, still use manual
handlers. The revision also introduces an additional "instructions.ll"
test file to test the import of instructions that have tablegen builders
(except for the resume instruction whose test remains untouched). A part
of the test cases are new, for example the integer instruction test,
while others are migrated from the "basic.ll" test file.

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D135709
  • Loading branch information
gysit committed Oct 12, 2022
1 parent fbad5fd commit 8446f24
Show file tree
Hide file tree
Showing 7 changed files with 627 additions and 465 deletions.
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(LLVM_TARGET_DEFINITIONS LLVMOps.td)
mlir_tablegen(LLVMConversions.inc -gen-llvmir-conversions)
mlir_tablegen(LLVMConversionEnumsToLLVM.inc -gen-enum-to-llvmir-conversions)
mlir_tablegen(LLVMConversionEnumsFromLLVM.inc -gen-enum-from-llvmir-conversions)
mlir_tablegen(LLVMOpFromLLVMIRConversions.inc -gen-op-from-llvmir-conversions)
add_public_tablegen_target(MLIRLLVMConversionsIncGen)

set(LLVM_TARGET_DEFINITIONS LLVMIntrinsicOps.td)
Expand Down
35 changes: 19 additions & 16 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ class LLVM_ScalarOrVectorOf<Type element> :
AnyTypeOf<[element, LLVM_VectorOf<element>]>;

// Base class for LLVM operations. Defines the interface to the llvm::IRBuilder
// used to translate to LLVM IR proper.
// used to translate to proper LLVM IR and the interface to the mlir::OpBuilder
// used to import from LLVM IR.
class LLVM_OpBase<Dialect dialect, string mnemonic, list<Trait> traits = []> :
Op<dialect, mnemonic, traits> {
// A pattern for constructing the LLVM IR Instruction (or other Value) that
Expand All @@ -213,6 +214,23 @@ class LLVM_OpBase<Dialect dialect, string mnemonic, list<Trait> traits = []> :
// - $_location - mlir::Location object of the instruction.
// Additionally, `$$` can be used to produce the dollar character.
string llvmBuilder = "";

// A builder to construct the MLIR LLVM dialect operation given the matching
// LLVM IR instruction `inst` and its operands `llvmOperands`. The
// following $-variables exist:
// - $name - substituted by the remapped `inst` operand value at the index
// of the MLIR operation argument with the given name, or if the
// name matches the result name, by a reference to store the
// result of the newly created MLIR operation to;
// - $_int_attr - substituted by a call to an integer attribute matcher;
// - $_resultType - substituted with the MLIR result type;
// - $_location - substituted with the MLIR location;
// - $_builder - substituted with the MLIR builder;
// - $_qualCppClassName - substitiuted with the MLIR operation class name.
// Additionally, `$$` can be used to produce the dollar character.
// NOTE: The $name variable resolution assumes the MLIR and LLVM argument
// orders match and there are no optional or variadic arguments.
string mlirBuilder = "";
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -335,21 +353,6 @@ class LLVM_IntrOpBase<Dialect dialect, string opName, string enumName,
"(void) inst;")
# !if(!gt(numResults, 0), "$res = inst;", "");

// A builder to construct the MLIR LLVM dialect operation given the matching
// LLVM IR instruction `inst` and its operands `llvmOperands`. The
// following $-variables exist:
// - $name - substituted by the remapped `inst` operand value at the index
// of the MLIR operation argument with the given name, or if the
// name matches the result name, by a reference to store the
// result of the newly created MLIR operation to;
// - $_int_attr - substituted by a call to an integer attribute matcher;
// - $_resultType - substituted with the MLIR result type;
// - $_location - substituted with the MLIR location;
// - $_builder - substituted with the MLIR builder;
// - $_qualCppClassName - substitiuted with the MLIR operation class name.
// Additionally, `$$` can be used to produce the dollar character.
// NOTE: The $name variable resolution assumes the MLIR and LLVM argument
// orders match and there are no optional or variadic arguments.
string mlirBuilder = [{
SmallVector<Type> resultTypes =
}] # !if(!gt(numResults, 0), "{$_resultType};", "{};") # [{
Expand Down

0 comments on commit 8446f24

Please sign in to comment.