Skip to content

[mlir][llvm][x86vector] One-to-one intrinsic op interface #140055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions mlir/include/mlir/Conversion/LLVMCommon/Pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ LogicalResult oneToOneRewrite(
const LLVMTypeConverter &typeConverter, ConversionPatternRewriter &rewriter,
IntegerOverflowFlags overflowFlags = IntegerOverflowFlags::none);

/// Replaces the given operation "op" with a call to an LLVM intrinsic with the
/// specified name "intrinsic" and operands.
///
/// The rewrite performs a simple one-to-one matching between the op and LLVM
/// intrinsic. For example:
///
/// ```mlir
/// %res = intr.op %val : vector<16xf32>
/// ```
///
/// can be converted to
///
/// ```mlir
/// %res = llvm.call_intrinsic "intrinsic"(%val)
/// ```
///
/// The provided operands must be LLVM-compatible.
///
/// Upholds a convention that multi-result operations get converted into an
/// operation returning the LLVM IR structure type, in which case individual
/// values are first extracted before replacing the original results.
LogicalResult intrinsicRewrite(Operation *op, StringRef intrinsic,
ValueRange operands,
const LLVMTypeConverter &typeConverter,
RewriterBase &rewriter);

} // namespace detail

/// Decomposes a `src` value into a set of values of type `dstType` through
Expand Down
4 changes: 4 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMInterfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"

namespace mlir {

class LLVMTypeConverter;
class RewriterBase;

namespace LLVM {
namespace detail {

Expand Down
51 changes: 51 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,57 @@ def RoundingModeOpInterface : OpInterface<"RoundingModeOpInterface"> {
];
}

def OneToOneIntrinsicOpInterface : OpInterface<"OneToOneIntrinsicOpInterface"> {
let description = [{
An interface for operations modelling LLVM intrinsics suitable for
1-to-1 conversion.

An op implementing this interface can be directly replaced by a call
to a matching intrinsic function.
The op must ensure that the combinations of its arguments and results
have valid intrinsic counterparts.

For example, an operation supporting different inputs:
```mlir
%res_v8 = intr.op %value_v8 : vector<8xf32>
%res_v16 = intr.op %value_v16 : vector<16xf32>
```
can be converted to the following intrinsic calls:
```mlir
%res_v8 = llvm.call_intrinsic "llvm.x86.op.intr.256"(%value_v8)
%res_v16 = llvm.call_intrinsic "llvm.x86.op.intr.512"(%value_v16)
```
}];

let cppNamespace = "::mlir::LLVM";

let methods = [
InterfaceMethod<
/*desc=*/[{
Returns mangled LLVM intrinsic function name matching the operation
variant.
}],
/*retType=*/"std::string",
/*methodName=*/"getIntrinsicName"
>,
InterfaceMethod<
/*desc=*/[{
Returns operands for a corresponding LLVM intrinsic.

Additional operations may be created to facilitate mapping
between the source operands and the target intrinsic.
}],
/*retType=*/"SmallVector<Value>",
/*methodName=*/"getIntrinsicOperands",
/*args=*/(ins "::mlir::ArrayRef<Value>":$operands,
"const ::mlir::LLVMTypeConverter &":$typeConverter,
"::mlir::RewriterBase &":$rewriter),
/*methodBody=*/"",
/*defaultImplementation=*/"return SmallVector<Value>(operands);"
>,
];
}

//===----------------------------------------------------------------------===//
// LLVM dialect type interfaces.
//===----------------------------------------------------------------------===//
Expand Down
Loading