diff --git a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp --- a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp +++ b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp @@ -2402,6 +2402,22 @@ } }; +struct RankOpLowering : public ConvertOpToLLVMPattern { + using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; + + LogicalResult + matchAndRewrite(Operation *op, ArrayRef operands, + ConversionPatternRewriter &rewriter) const override { + Value operand = cast(op).memrefOrTensor(); + // RankOp cannot be folded away for UnrankedMemRefType. + if (!operand.getType().isa()) + return failure(); + UnrankedMemRefDescriptor desc(RankOp::Adaptor(operands).memrefOrTensor()); + rewriter.replaceOp(op, {desc.rank(rewriter, op->getLoc())}); + return success(); + } +}; + // Common base for load and store operations on MemRefs. Restricts the match // to supported MemRef types. Provides functionality to emit code accessing a // specific element of the underlying data buffer. @@ -3272,6 +3288,7 @@ DimOpLowering, LoadOpLowering, MemRefCastOpLowering, + RankOpLowering, StoreOpLowering, SubViewOpLowering, ViewOpLowering, diff --git a/mlir/test/Conversion/StandardToLLVM/convert-to-llvmir.mlir b/mlir/test/Conversion/StandardToLLVM/convert-to-llvmir.mlir --- a/mlir/test/Conversion/StandardToLLVM/convert-to-llvmir.mlir +++ b/mlir/test/Conversion/StandardToLLVM/convert-to-llvmir.mlir @@ -1291,3 +1291,17 @@ func @memref_index(%arg0: memref<32xindex>) -> memref<32xindex> { return %arg0 : memref<32xindex> } + +// ----- + +// CHECK-LABEL: func @rank_op +// CHECK32-LABEL: func @rank_op +func @rank_op(%in: memref<*xi32>) { + %rank = rank %in : memref<*xi32> + return +} +// CHECK-NEXT: llvm.mlir.undef +// CHECK-NEXT: llvm.insertvalue +// CHECK-NEXT: llvm.insertvalue +// CHECK-NEXT: llvm.extractvalue %{{.*}}[0] : !llvm<"{ i64, i8* }"> +// CHECK32: llvm.extractvalue %{{.*}}[0] : !llvm<"{ i64, i8* }">