Skip to content

Commit

Permalink
[flang][CodeGen] Transform fir.boxchar_len to a sequence of LLVM MLIR
Browse files Browse the repository at this point in the history
This patch extends the `FIRToLLVMLowering` pass in Flang by adding a
hook to transform `fir.boxchar_len` to a sequence of LLVM MLIR
instructions.

This is part of the upstreaming effort from the `fir-dev` branch in [1].

[1] https://github.com/flang-compiler/f18-llvm-project

Differential Revision: https://reviews.llvm.org/D113763

Originally written by:
Co-authored-by: Eric Schweitz <eschweitz@nvidia.com>
  • Loading branch information
banach-space committed Nov 16, 2021
1 parent aa9bbb6 commit 6c3d7fd
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
25 changes: 24 additions & 1 deletion flang/lib/Optimizer/CodeGen/CodeGen.cpp
Expand Up @@ -1517,6 +1517,29 @@ genExtractValueWithIndex(mlir::Location loc, mlir::Value tuple, mlir::Type ty,
return rewriter.create<mlir::LLVM::ExtractValueOp>(loc, xty, tuple, cx);
}

/// Convert `!fir.boxchar_len` to `!llvm.extractvalue` for the 2nd part of the
/// boxchar.
struct BoxCharLenOpConversion : public FIROpConversion<fir::BoxCharLenOp> {
using FIROpConversion::FIROpConversion;

mlir::LogicalResult
matchAndRewrite(fir::BoxCharLenOp boxCharLen, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
mlir::Value boxChar = adaptor.getOperands()[0];
mlir::Location loc = boxChar.getLoc();
mlir::MLIRContext *ctx = boxChar.getContext();
mlir::Type returnValTy = boxCharLen.getResult().getType();

constexpr int boxcharLenIdx = 1;
mlir::LLVM::ExtractValueOp len = genExtractValueWithIndex(
loc, boxChar, boxChar.getType(), rewriter, ctx, boxcharLenIdx);
mlir::Value lenAfterCast = integerCast(loc, rewriter, returnValTy, len);
rewriter.replaceOp(boxCharLen, lenAfterCast);

return success();
}
};

/// Convert `fir.unboxchar` into two `llvm.extractvalue` instructions. One for
/// the character buffer and one for the buffer length.
struct UnboxCharOpConversion : public FIROpConversion<fir::UnboxCharOp> {
Expand Down Expand Up @@ -1569,7 +1592,7 @@ class FIRToLLVMLowering : public fir::FIRToLLVMLoweringBase<FIRToLLVMLowering> {
mlir::OwningRewritePatternList pattern(context);
pattern.insert<
AbsentOpConversion, AddcOpConversion, AddrOfOpConversion,
AllocaOpConversion, BoxAddrOpConversion, BoxDimsOpConversion,
AllocaOpConversion, BoxAddrOpConversion, BoxCharLenOpConversion, BoxDimsOpConversion,
BoxEleSizeOpConversion, BoxIsAllocOpConversion, BoxIsArrayOpConversion,
BoxIsPtrOpConversion, BoxRankOpConversion, CallOpConversion,
CmpcOpConversion, ConvertOpConversion, DispatchOpConversion,
Expand Down
76 changes: 76 additions & 0 deletions flang/test/Fir/convert-to-llvm-target.fir
Expand Up @@ -71,3 +71,79 @@ func @unboxchar_i32(%arg0 : !fir.boxchar<4>) -> () {
// INT32: %[[len_unextended:.*]] = llvm.extractvalue %[[box_char]][1 : i32] : !llvm.struct<(ptr<i32>, i32)>
// INT32: %{{.*}} = llvm.sext %[[len_unextended]] : i32 to i64
// INT32-NEXT: llvm.return

// -----

// Test fir.boxchar_len

func @boxchar_len_i8_i32(%arg0 : !fir.boxchar<1>) -> () {
fir.boxchar_len %arg0 : (!fir.boxchar<1>) -> i32
return
}

// INT64-LABEL: llvm.func @boxchar_len_i8_i32
// INT64-SAME: %[[box_char:.*]]: !llvm.struct<(ptr<i8>, i64)>
// INT64: %[[len:.*]] = llvm.extractvalue %[[box_char]][1 : i32] : !llvm.struct<(ptr<i8>, i64)>
// INT64: %{{.*}} = llvm.trunc %[[len]] : i64 to i32
// INT64-NEXT: llvm.return

// INT32-LABEL: llvm.func @boxchar_len_i8_i32
// INT32-SAME: %[[box_char:.*]]: !llvm.struct<(ptr<i8>, i32)>
// INT32: %{{.*}} = llvm.extractvalue %[[box_char]][1 : i32] : !llvm.struct<(ptr<i8>, i32)>
// INT32-NOT: llvm.trunc
// INT32-NOT: llvm.sext
// INT32-NEXT: llvm.return

func @boxchar_len_i8_i64(%arg0 : !fir.boxchar<1>) -> () {
fir.boxchar_len %arg0 : (!fir.boxchar<1>) -> i64
return
}

// INT64-LABEL: llvm.func @boxchar_len_i8_i64
// INT64-SAME: %[[box_char:.*]]: !llvm.struct<(ptr<i8>, i64)>
// INT64: %{{.*}} = llvm.extractvalue %[[box_char]][1 : i32] : !llvm.struct<(ptr<i8>, i64)>
// INT64-NOT: llvm.trunc
// INT64-NOT: llvm.sext
// INT64-NEXT: llvm.return

// INT32-LABEL: llvm.func @boxchar_len_i8_i64
// INT32-SAME: %[[box_char:.*]]: !llvm.struct<(ptr<i8>, i32)>
// INT32: %[[len:.*]] = llvm.extractvalue %[[box_char]][1 : i32] : !llvm.struct<(ptr<i8>, i32)>
// INT32: %{{.*}} = llvm.sext %0 : i32 to i64
// INT32-NEXT: llvm.return

func @boxchar_len_i32_i32(%arg0 : !fir.boxchar<4>) -> () {
fir.boxchar_len %arg0 : (!fir.boxchar<4>) -> i32
return
}

// INT64-LABEL: llvm.func @boxchar_len_i32_i32
// INT64-SAME: %[[box_char:.*]]: !llvm.struct<(ptr<i32>, i64)>
// INT64: %[[len:.*]] = llvm.extractvalue %[[box_char]][1 : i32] : !llvm.struct<(ptr<i32>, i64)>
// INT64: %{{.*}} = llvm.trunc %[[len]] : i64 to i32
// INT64-NEXT: llvm.return

// INT32-LABEL: llvm.func @boxchar_len_i32_i32
// INT32-SAME: %[[box_char:.*]]: !llvm.struct<(ptr<i32>, i32)>
// INT32: %{{.*}} = llvm.extractvalue %[[box_char]][1 : i32] : !llvm.struct<(ptr<i32>, i32)>
// INT32-NOT: llvm.trunc
// INT32-NOT: llvm.sext
// INT32-NEXT: llvm.return

func @boxchar_len_i32_i64(%arg0 : !fir.boxchar<4>) -> (i64) {
%0 = fir.boxchar_len %arg0 : (!fir.boxchar<4>) -> i64
return %0 : i64
}

// INT64-LABEL: llvm.func @boxchar_len_i32_i64
// INT64-SAME: %[[box_char:.*]]: !llvm.struct<(ptr<i32>, i64)>
// INT64: %{{.*}} = llvm.extractvalue %[[box_char]][1 : i32] : !llvm.struct<(ptr<i32>, i64)>
// INT64-NOT: llvm.trunc
// INT64-NOT: llvm.sext
// INT64-NEXT: llvm.return

// INT32-LABEL: llvm.func @boxchar_len_i32_i64
// INT32-SAME: %[[box_char:.*]]: !llvm.struct<(ptr<i32>, i32)>
// INT32: %[[len:.*]] = llvm.extractvalue %[[box_char]][1 : i32] : !llvm.struct<(ptr<i32>, i32)>
// INT32: %{{.*}} = llvm.sext %0 : i32 to i64
// INT32-NEXT: llvm.return

0 comments on commit 6c3d7fd

Please sign in to comment.