Skip to content

Commit 987fbae

Browse files
committed
[mlir] StandardToLLVM: make one-to-one convresion pattern publicly available
Summary: The Standard-to-LLVM dialect convresion has a set of utility classes that simplify conversions, including patterns that provide one-to-one conversion operation conversion with optional result packing. Expose these classes in a public header so that conversions other than Standard-to-LLVM (e.g. vectors, or LLVM-based intrinsics) could also use them. Since the patterns are implemented as class templates and in order to keep the code size limited, keep the implementation private by resorting to op identifiers instead of template-based builders. Differential Revision: https://reviews.llvm.org/D76864
1 parent abcb9bb commit 987fbae

File tree

2 files changed

+207
-157
lines changed

2 files changed

+207
-157
lines changed

mlir/include/mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class LLVMTypeConverter : public TypeConverter {
9090
/// to each of the MLIR types converted with `convertType`.
9191
Type packFunctionResults(ArrayRef<Type> types);
9292

93+
/// Returns the MLIR context.
94+
MLIRContext &getContext();
95+
9396
/// Returns the LLVM context.
9497
llvm::LLVMContext &getLLVMContext();
9598

@@ -356,6 +359,7 @@ class UnrankedMemRefDescriptor : public StructBuilder {
356359
/// `unpack`.
357360
static unsigned getNumUnpackedValues() { return 2; }
358361
};
362+
359363
/// Base class for operation conversions targeting the LLVM IR dialect. Provides
360364
/// conversion patterns with access to an LLVMTypeConverter.
361365
class ConvertToLLVMPattern : public ConversionPattern {
@@ -364,11 +368,79 @@ class ConvertToLLVMPattern : public ConversionPattern {
364368
LLVMTypeConverter &typeConverter,
365369
PatternBenefit benefit = 1);
366370

371+
/// Returns the LLVM dialect.
372+
LLVM::LLVMDialect &getDialect() const;
373+
374+
/// Returns the LLVM IR context.
375+
llvm::LLVMContext &getContext() const;
376+
377+
/// Returns the LLVM IR module associated with the LLVM dialect.
378+
llvm::Module &getModule() const;
379+
380+
/// Gets the MLIR type wrapping the LLVM integer type whose bit width is
381+
/// defined by the pointer size used in the LLVM module.
382+
LLVM::LLVMType getIndexType() const;
383+
384+
/// Gets the MLIR type wrapping the LLVM void type.
385+
LLVM::LLVMType getVoidType() const;
386+
387+
/// Get the MLIR type wrapping the LLVM i8* type.
388+
LLVM::LLVMType getVoidPtrType() const;
389+
390+
/// Create an LLVM dialect operation defining the given index constant.
391+
Value createIndexConstant(ConversionPatternRewriter &builder, Location loc,
392+
uint64_t value) const;
393+
367394
protected:
368395
/// Reference to the type converter, with potential extensions.
369396
LLVMTypeConverter &typeConverter;
370397
};
371398

399+
/// Utility class for operation conversions targeting the LLVM dialect that
400+
/// match exactly one source operation.
401+
template <typename OpTy>
402+
class ConvertOpToLLVMPattern : public ConvertToLLVMPattern {
403+
public:
404+
ConvertOpToLLVMPattern(LLVMTypeConverter &typeConverter,
405+
PatternBenefit benefit = 1)
406+
: ConvertToLLVMPattern(OpTy::getOperationName(),
407+
&typeConverter.getContext(), typeConverter,
408+
benefit) {}
409+
};
410+
411+
namespace LLVM {
412+
namespace detail {
413+
/// Replaces the given operaiton "op" with a new operation of type "targetOp"
414+
/// and given operands.
415+
LogicalResult oneToOneRewrite(Operation *op, StringRef targetOp,
416+
ValueRange operands,
417+
LLVMTypeConverter &typeConverter,
418+
ConversionPatternRewriter &rewriter);
419+
} // namespace detail
420+
} // namespace LLVM
421+
422+
/// Generic implementation of one-to-one conversion from "SourceOp" to
423+
/// "TargetOp" where the latter belongs to the LLVM dialect or an equivalent.
424+
/// Upholds a convention that multi-result operations get converted into an
425+
/// operation returning the LLVM IR structure type, in which case individual
426+
/// values must be extacted from using LLVM::ExtractValueOp before being used.
427+
template <typename SourceOp, typename TargetOp>
428+
class OneToOneConvertToLLVMPattern : public ConvertOpToLLVMPattern<SourceOp> {
429+
public:
430+
using ConvertOpToLLVMPattern<SourceOp>::ConvertOpToLLVMPattern;
431+
using Super = OneToOneConvertToLLVMPattern<SourceOp, TargetOp>;
432+
433+
/// Converts the type of the result to an LLVM type, pass operands as is,
434+
/// preserve attributes.
435+
LogicalResult
436+
matchAndRewrite(Operation *op, ArrayRef<Value> operands,
437+
ConversionPatternRewriter &rewriter) const override {
438+
return LLVM::detail::oneToOneRewrite(op, TargetOp::getOperationName(),
439+
operands, this->typeConverter,
440+
rewriter);
441+
}
442+
};
443+
372444
/// Derived class that automatically populates legalization information for
373445
/// different LLVM ops.
374446
class LLVMConversionTarget : public ConversionTarget {

0 commit comments

Comments
 (0)