diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp --- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -465,6 +465,26 @@ } }; +/// Lower `fir.box_tdesc` to the sequence of operation to extract the type +/// descriptor from the box. +struct BoxTypeDescOpConversion : public FIROpConversion { + 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()(boxtypedesc.getContext()); + auto result = getValueFromBox(loc, box, typeTy, rewriter, kTypePosInBox); + auto typePtrTy = mlir::LLVM::LLVMPointerType::get(typeTy); + rewriter.replaceOpWithNewOp(boxtypedesc, typePtrTy, + result); + return success(); + } +}; + // `fir.call` -> `llvm.call` struct CallOpConversion : public FIROpConversion { using FIROpConversion::FIROpConversion; @@ -1513,16 +1533,17 @@ AbsentOpConversion, AddcOpConversion, AddrOfOpConversion, AllocaOpConversion, BoxAddrOpConversion, BoxDimsOpConversion, BoxEleSizeOpConversion, BoxIsAllocOpConversion, BoxIsArrayOpConversion, - BoxIsPtrOpConversion, BoxRankOpConversion, CallOpConversion, - ConvertOpConversion, DispatchOpConversion, DispatchTableOpConversion, - DTEntryOpConversion, DivcOpConversion, EmboxCharOpConversion, - ExtractValueOpConversion, HasValueOpConversion, GlobalLenOpConversion, - GlobalOpConversion, InsertOnRangeOpConversion, InsertValueOpConversion, - IsPresentOpConversion, LoadOpConversion, NegcOpConversion, - MulcOpConversion, SelectCaseOpConversion, SelectOpConversion, - SelectRankOpConversion, SelectTypeOpConversion, StoreOpConversion, - SubcOpConversion, UnboxCharOpConversion, UndefOpConversion, - UnreachableOpConversion, ZeroOpConversion>(typeConverter); + BoxIsPtrOpConversion, BoxRankOpConversion, BoxTypeDescOpConversion, + CallOpConversion, ConvertOpConversion, DispatchOpConversion, + DispatchTableOpConversion, DTEntryOpConversion, DivcOpConversion, + EmboxCharOpConversion, ExtractValueOpConversion, HasValueOpConversion, + GlobalLenOpConversion, GlobalOpConversion, InsertOnRangeOpConversion, + InsertValueOpConversion, IsPresentOpConversion, LoadOpConversion, + NegcOpConversion, MulcOpConversion, SelectCaseOpConversion, + SelectOpConversion, SelectRankOpConversion, SelectTypeOpConversion, + StoreOpConversion, 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 --- a/flang/test/Fir/convert-to-llvm.fir +++ b/flang/test/Fir/convert-to-llvm.fir @@ -1200,3 +1200,20 @@ // CHECK-NEXT: %[[ptr:.*]] = llvm.mlir.null : !llvm.ptr // CHECK-NEXT: %[[ret_val:.*]] = llvm.call @is_present(%[[ptr]]) : (!llvm.ptr) -> i1 // CHECK-NEXT: llvm.return %[[ret_val]] : i1 + +// ----- + +// Test `fir.box_tdesc` conversion. + +func @box_tdesc(%arg0: !fir.box) { + %0 = fir.box_tdesc %arg0 : (!fir.box) -> !fir.tdesc + return +} + +// CHECK-LABEL: llvm.func @box_tdesc( +// CHECK-SAME: %[[ARG0:.*]]: !llvm.ptr, 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, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}})>>, i32, i32) -> !llvm.ptr +// CHECK: %[[LOAD:.*]] = llvm.load %[[GEP]] : !llvm.ptr +// CHECK: %{{.*}} = llvm.inttoptr %[[LOAD]] : i{{.*}} to !llvm.ptr