@@ -90,6 +90,9 @@ class LLVMTypeConverter : public TypeConverter {
90
90
// / to each of the MLIR types converted with `convertType`.
91
91
Type packFunctionResults (ArrayRef<Type> types);
92
92
93
+ // / Returns the MLIR context.
94
+ MLIRContext &getContext ();
95
+
93
96
// / Returns the LLVM context.
94
97
llvm::LLVMContext &getLLVMContext ();
95
98
@@ -356,6 +359,7 @@ class UnrankedMemRefDescriptor : public StructBuilder {
356
359
// / `unpack`.
357
360
static unsigned getNumUnpackedValues () { return 2 ; }
358
361
};
362
+
359
363
// / Base class for operation conversions targeting the LLVM IR dialect. Provides
360
364
// / conversion patterns with access to an LLVMTypeConverter.
361
365
class ConvertToLLVMPattern : public ConversionPattern {
@@ -364,11 +368,79 @@ class ConvertToLLVMPattern : public ConversionPattern {
364
368
LLVMTypeConverter &typeConverter,
365
369
PatternBenefit benefit = 1 );
366
370
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
+
367
394
protected:
368
395
// / Reference to the type converter, with potential extensions.
369
396
LLVMTypeConverter &typeConverter;
370
397
};
371
398
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
+
372
444
// / Derived class that automatically populates legalization information for
373
445
// / different LLVM ops.
374
446
class LLVMConversionTarget : public ConversionTarget {
0 commit comments