[flang-commits] [flang] e38ef2f - [fir] Add fir.box_tdesc conversion
Valentin Clement via flang-commits
flang-commits at lists.llvm.org
Wed Nov 17 03:02:56 PST 2021
Author: Valentin Clement
Date: 2021-11-17T12:02:48+01:00
New Revision: e38ef2ff711ca1aa68c541e973e68fbbb8efc575
URL: https://github.com/llvm/llvm-project/commit/e38ef2ff711ca1aa68c541e973e68fbbb8efc575
DIFF: https://github.com/llvm/llvm-project/commit/e38ef2ff711ca1aa68c541e973e68fbbb8efc575.diff
LOG: [fir] Add fir.box_tdesc conversion
This patch adds the conversion pattern for
`fir.box_tdes`.
This patch is part of the upstreaming effort from fir-dev branch.
Reviewed By: kiranchandramohan
Differential Revision: https://reviews.llvm.org/D113931
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>
Added:
Modified:
flang/lib/Optimizer/CodeGen/CodeGen.cpp
flang/test/Fir/convert-to-llvm.fir
Removed:
################################################################################
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index 899315a1286e8..4e12005777b49 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -498,6 +498,26 @@ struct StringLitOpConversion : public FIROpConversion<fir::StringLitOp> {
}
};
+/// Lower `fir.box_tdesc` to the sequence of operations to extract the type
+/// descriptor from the box.
+struct BoxTypeDescOpConversion : public FIROpConversion<fir::BoxTypeDescOp> {
+ using FIROpConversion::FIROpConversion;
+
+ mlir::LogicalResult
+ matchAndRewrite(fir::BoxTypeDescOp boxtypedesc, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const override {
+ mlir::Value box = adaptor.getOperands()[0];
+ auto loc = boxtypedesc.getLoc();
+ mlir::Type typeTy =
+ fir::getDescFieldTypeModel<kTypePosInBox>()(boxtypedesc.getContext());
+ auto result = getValueFromBox(loc, box, typeTy, rewriter, kTypePosInBox);
+ auto typePtrTy = mlir::LLVM::LLVMPointerType::get(typeTy);
+ rewriter.replaceOpWithNewOp<mlir::LLVM::IntToPtrOp>(boxtypedesc, typePtrTy,
+ result);
+ return success();
+ }
+};
+
// `fir.call` -> `llvm.call`
struct CallOpConversion : public FIROpConversion<fir::CallOp> {
using FIROpConversion::FIROpConversion;
@@ -1664,18 +1684,18 @@ class FIRToLLVMLowering : public fir::FIRToLLVMLoweringBase<FIRToLLVMLowering> {
AllocaOpConversion, BoxAddrOpConversion, BoxCharLenOpConversion,
BoxDimsOpConversion, BoxEleSizeOpConversion, BoxIsAllocOpConversion,
BoxIsArrayOpConversion, BoxIsPtrOpConversion, BoxRankOpConversion,
- CallOpConversion, CmpcOpConversion, ConvertOpConversion,
- DispatchOpConversion, DispatchTableOpConversion, DTEntryOpConversion,
- DivcOpConversion, EmboxCharOpConversion, ExtractValueOpConversion,
- HasValueOpConversion, GenTypeDescOpConversion, GlobalLenOpConversion,
- GlobalOpConversion, InsertOnRangeOpConversion, InsertValueOpConversion,
- IsPresentOpConversion, LoadOpConversion, NegcOpConversion,
- MulcOpConversion, SelectCaseOpConversion, SelectOpConversion,
- SelectRankOpConversion, SelectTypeOpConversion, ShapeOpConversion,
- ShapeShiftOpConversion, ShiftOpConversion, SliceOpConversion,
- StoreOpConversion, StringLitOpConversion, SubcOpConversion,
- UnboxCharOpConversion, UndefOpConversion, UnreachableOpConversion,
- ZeroOpConversion>(typeConverter);
+ BoxTypeDescOpConversion, CallOpConversion, CmpcOpConversion,
+ ConvertOpConversion, DispatchOpConversion, DispatchTableOpConversion,
+ DTEntryOpConversion, DivcOpConversion, EmboxCharOpConversion,
+ ExtractValueOpConversion, HasValueOpConversion, GenTypeDescOpConversion,
+ GlobalLenOpConversion, GlobalOpConversion, InsertOnRangeOpConversion,
+ InsertValueOpConversion, IsPresentOpConversion, LoadOpConversion,
+ NegcOpConversion, MulcOpConversion, SelectCaseOpConversion,
+ SelectOpConversion, SelectRankOpConversion, SelectTypeOpConversion,
+ ShapeOpConversion, ShapeShiftOpConversion, ShiftOpConversion,
+ SliceOpConversion, StoreOpConversion, StringLitOpConversion,
+ SubcOpConversion, UnboxCharOpConversion, UndefOpConversion,
+ UnreachableOpConversion, ZeroOpConversion>(typeConverter);
mlir::populateStdToLLVMConversionPatterns(typeConverter, pattern);
mlir::arith::populateArithmeticToLLVMConversionPatterns(typeConverter,
pattern);
diff --git a/flang/test/Fir/convert-to-llvm.fir b/flang/test/Fir/convert-to-llvm.fir
index 5cbee81653820..8465063312c8d 100644
--- a/flang/test/Fir/convert-to-llvm.fir
+++ b/flang/test/Fir/convert-to-llvm.fir
@@ -1319,3 +1319,20 @@ func @dead_slice() {
// CHECK-NOT: fir.slice
// CHECK: %{{.*}} = llvm.mlir.constant(0 : index) : i{{.*}}
// CHECK-NEXT: llvm.return
+
+// -----
+
+// Test `fir.box_tdesc` conversion.
+
+func @box_tdesc(%arg0: !fir.box<f64>) {
+ %0 = fir.box_tdesc %arg0 : (!fir.box<f64>) -> !fir.tdesc<f64>
+ return
+}
+
+// CHECK-LABEL: llvm.func @box_tdesc(
+// CHECK-SAME: %[[ARG0:.*]]: !llvm.ptr<struct<(ptr<f64>, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}})>>) {
+// CHECK: %[[C0:.*]] = llvm.mlir.constant(0 : i32) : i32
+// CHECK: %[[TYPE_POS:.*]] = llvm.mlir.constant(4 : i32) : i32
+// CHECK: %[[GEP:.*]] = llvm.getelementptr %[[ARG0]][%[[C0]], %[[TYPE_POS]]] : (!llvm.ptr<struct<(ptr<f64>, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}})>>, i32, i32) -> !llvm.ptr<i8>
+// CHECK: %[[LOAD:.*]] = llvm.load %[[GEP]] : !llvm.ptr<i{{.*}}>
+// CHECK: %{{.*}} = llvm.inttoptr %[[LOAD]] : i{{.*}} to !llvm.ptr<i{{.*}}>
More information about the flang-commits
mailing list