diff --git a/flang/include/flang/Optimizer/Builder/HLFIRTools.h b/flang/include/flang/Optimizer/Builder/HLFIRTools.h --- a/flang/include/flang/Optimizer/Builder/HLFIRTools.h +++ b/flang/include/flang/Optimizer/Builder/HLFIRTools.h @@ -166,6 +166,14 @@ Entity entity, llvm::SmallVectorImpl &result); +/// Return the fir base, shape, and type parameters for a variable. Note that +/// type parameters are only added if the entity is not a box and the type +/// parameters is not a constant in the base type. This matches the arguments +/// expected by fir.embox/fir.array_coor. +std::pair genVariableFirBaseShapeAndParams( + mlir::Location loc, fir::FirOpBuilder &builder, Entity entity, + llvm::SmallVectorImpl &typeParams); + } // namespace hlfir #endif // FORTRAN_OPTIMIZER_BUILDER_HLFIRTOOLS_H diff --git a/flang/lib/Optimizer/Builder/HLFIRTools.cpp b/flang/lib/Optimizer/Builder/HLFIRTools.cpp --- a/flang/lib/Optimizer/Builder/HLFIRTools.cpp +++ b/flang/lib/Optimizer/Builder/HLFIRTools.cpp @@ -225,3 +225,19 @@ } TODO(loc, "inquire PDTs length parameters in HLFIR"); } + +std::pair hlfir::genVariableFirBaseShapeAndParams( + mlir::Location loc, fir::FirOpBuilder &builder, Entity entity, + llvm::SmallVectorImpl &typeParams) { + auto [exv, cleanup] = translateToExtendedValue(loc, builder, entity); + assert(!cleanup && "variable to Exv should not produce cleanup"); + if (entity.hasLengthParameters()) { + auto params = fir::getTypeParams(exv); + typeParams.append(params.begin(), params.end()); + } + if (entity.isScalar()) + return {fir::getBase(exv), mlir::Value{}}; + if (auto variableInterface = entity.getIfVariableInterface()) + return {fir::getBase(exv), variableInterface.getShape()}; + return {fir::getBase(exv), builder.createShape(loc, exv)}; +} diff --git a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp --- a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp +++ b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp @@ -91,6 +91,94 @@ } }; +class DesignateOpConversion + : public mlir::OpRewritePattern { +public: + explicit DesignateOpConversion(mlir::MLIRContext *ctx) + : OpRewritePattern{ctx} {} + + mlir::LogicalResult + matchAndRewrite(hlfir::DesignateOp designate, + mlir::PatternRewriter &rewriter) const override { + mlir::Location loc = designate.getLoc(); + auto module = designate->getParentOfType(); + fir::FirOpBuilder builder(rewriter, fir::getKindMapping(module)); + + if (designate.getComponent() || designate.getComplexPart() || + !designate.getSubstring().empty()) { + // build path. + TODO(loc, "hlfir::designate with complex part or substring or component"); + } + + hlfir::Entity baseEntity(designate.getMemref()); + if (baseEntity.isMutableBox()) + TODO(loc, "hlfir::designate load of pointer or allocatable"); + + mlir::Type designateResultType = designate.getResult().getType(); + llvm::SmallVector firBaseTypeParameters; + auto [base, shape] = hlfir::genVariableFirBaseShapeAndParams( + loc, builder, baseEntity, firBaseTypeParameters); + if (designateResultType.isa()) { + // Generate embox or rebox. + if (designate.getIndices().empty()) + TODO(loc, "hlfir::designate whole part"); + // Otherwise, this is an array section with triplets. + llvm::SmallVector triples; + auto undef = builder.create(loc, builder.getIndexType()); + auto subscripts = designate.getIndices(); + unsigned i = 0; + for (auto isTriplet : designate.getIsTriplet()) { + triples.push_back(subscripts[i++]); + if (isTriplet) { + triples.push_back(subscripts[i++]); + triples.push_back(subscripts[i++]); + } else { + triples.push_back(undef); + triples.push_back(undef); + } + } + mlir::Value slice = builder.create( + loc, triples, /*path=*/mlir::ValueRange{}); + llvm::SmallVector resultType{designateResultType}; + mlir::Value resultBox; + if (base.getType().isa()) + resultBox = + builder.create(loc, resultType, base, shape, slice); + else + resultBox = builder.create(loc, resultType, base, shape, + slice, firBaseTypeParameters); + rewriter.replaceOp(designate, resultBox); + return mlir::success(); + } + + // Indexing a single element (use fir.array_coor of fir.coordinate_of). + + if (designate.getIndices().empty()) { + // Scalar substring or complex part. + // generate fir.coordinate_of. + TODO(loc, "hlfir::designate to fir.coordinate_of"); + } + + // Generate fir.array_coor + mlir::Type resultType = designateResultType; + if (auto boxCharType = designateResultType.dyn_cast()) + resultType = fir::ReferenceType::get(boxCharType.getEleTy()); + auto arrayCoor = builder.create( + loc, resultType, base, shape, + /*slice=*/mlir::Value{}, designate.getIndices(), firBaseTypeParameters); + if (designateResultType.isa()) { + assert(designate.getTypeparams().size() == 1 && + "must have character length"); + auto emboxChar = builder.create( + loc, designateResultType, arrayCoor, designate.getTypeparams()[0]); + rewriter.replaceOp(designate, emboxChar.getResult()); + } else { + rewriter.replaceOp(designate, arrayCoor.getResult()); + } + return mlir::success(); + } +}; + class ConvertHLFIRtoFIR : public hlfir::impl::ConvertHLFIRtoFIRBase { public: @@ -98,7 +186,7 @@ auto func = this->getOperation(); auto *context = &getContext(); mlir::RewritePatternSet patterns(context); - patterns.insert(context); + patterns.insert(context); mlir::ConversionTarget target(*context); target.addIllegalDialect(); target.markUnknownOpDynamicallyLegal( diff --git a/flang/test/HLFIR/designate-codegen.fir b/flang/test/HLFIR/designate-codegen.fir new file mode 100644 --- /dev/null +++ b/flang/test/HLFIR/designate-codegen.fir @@ -0,0 +1,192 @@ +// Test hlfir.designate operation code generation to FIR +// RUN: fir-opt %s -convert-hlfir-to-fir | FileCheck %s + +func.func @array_ref(%arg0: !fir.box>, %arg1: !fir.ref) { + %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %1:2 = hlfir.declare %arg0 {uniq_name = "x"} : (!fir.box>) -> (!fir.box>, !fir.box>) + %2 = fir.load %0#0 : !fir.ref + %3 = hlfir.designate %1#0 (%2) : (!fir.box>, i64) -> !fir.ref + return +} +// CHECK-LABEL: func.func @array_ref( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.box>, +// CHECK-SAME: %[[VAL_1:.*]]: !fir.ref) { +// CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref) -> !fir.ref +// CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_0]] {uniq_name = "x"} : (!fir.box>) -> !fir.box> +// CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_2]] : !fir.ref +// CHECK: %[[VAL_6:.*]] = fir.array_coor %[[VAL_3]] %[[VAL_5]] : (!fir.box>, i64) -> !fir.ref + + +func.func @char_array_ref(%arg0: !fir.box>>, %arg1: !fir.ref) { + %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %1:2 = hlfir.declare %arg0 {uniq_name = "x"} : (!fir.box>>) -> (!fir.box>>, !fir.box>>) + %2 = fir.box_elesize %1#1 : (!fir.box>>) -> index + %c10 = arith.constant 10 : index + %3 = hlfir.designate %1#0 (%c10) typeparams %2 : (!fir.box>>, index, index) -> !fir.boxchar<1> + return +} +// CHECK-LABEL: func.func @char_array_ref( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.box>>, +// CHECK-SAME: %[[VAL_1:.*]]: !fir.ref) { +// CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref) -> !fir.ref +// CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_0]] {uniq_name = "x"} : (!fir.box>>) -> !fir.box>> +// CHECK: %[[VAL_5:.*]] = fir.box_elesize %[[VAL_3]] : (!fir.box>>) -> index +// CHECK: %[[VAL_6:.*]] = arith.constant 10 : index +// CHECK: %[[VAL_7:.*]] = fir.array_coor %[[VAL_3]] %[[VAL_6]] : (!fir.box>>, index) -> !fir.ref> +// CHECK: %[[VAL_8:.*]] = fir.emboxchar %[[VAL_7]], %[[VAL_5]] : (!fir.ref>, index) -> !fir.boxchar<1> + + +func.func @char_array_ref_cst_len(%arg0: !fir.box>>, %arg1: !fir.ref) { + %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %c5 = arith.constant 5 : index + %1:2 = hlfir.declare %arg0 typeparams %c5 {uniq_name = "x"} : (!fir.box>>, index) -> (!fir.box>>, !fir.box>>) + %c10 = arith.constant 10 : index + %2 = hlfir.designate %1#0 (%c10) typeparams %c5 : (!fir.box>>, index, index) -> !fir.ref> + return +} +// CHECK-LABEL: func.func @char_array_ref_cst_len( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.box>>, +// CHECK-SAME: %[[VAL_1:.*]]: !fir.ref) { +// CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref) -> !fir.ref +// CHECK: %[[VAL_3:.*]] = arith.constant 5 : index +// CHECK: %[[VAL_4:.*]] = fir.declare %[[VAL_0]] typeparams %[[VAL_3]] {uniq_name = "x"} : (!fir.box>>, index) -> !fir.box>> +// CHECK: %[[VAL_6:.*]] = arith.constant 10 : index +// CHECK: %[[VAL_7:.*]] = fir.array_coor %[[VAL_4]] %[[VAL_6]] : (!fir.box>>, index) -> !fir.ref> + + +func.func @char_array_ref_3(%arg0: !fir.ref>>, %arg1: !fir.ref) { + %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %c5 = arith.constant 5 : index + %c100 = arith.constant 100 : index + %sh = fir.shape %c100 : (index) -> !fir.shape<1> + %1:2 = hlfir.declare %arg0(%sh) typeparams %c5 {uniq_name = "x"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.box>>, !fir.ref>>) + %c10 = arith.constant 10 : index + %3 = hlfir.designate %1#0 (%c10) typeparams %c5 : (!fir.box>>, index, index) -> !fir.boxchar<1> + return +} +// CHECK-LABEL: func.func @char_array_ref_3( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>>, +// CHECK-SAME: %[[VAL_1:.*]]: !fir.ref) { +// CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref) -> !fir.ref +// CHECK: %[[VAL_3:.*]] = arith.constant 5 : index +// CHECK: %[[VAL_4:.*]] = arith.constant 100 : index +// CHECK: %[[VAL_5:.*]] = fir.shape %[[VAL_4]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_6:.*]] = fir.declare %[[VAL_0]](%[[VAL_5]]) typeparams %[[VAL_3]] {uniq_name = "x"} : (!fir.ref>>, !fir.shape<1>, index) -> !fir.ref>> +// CHECK: %[[VAL_8:.*]] = arith.constant 10 : index +// CHECK: %[[VAL_9:.*]] = fir.array_coor %[[VAL_6]](%[[VAL_5]]) %[[VAL_8]] typeparams %[[VAL_3]] : (!fir.ref>>, !fir.shape<1>, index, index) -> !fir.ref> +// CHECK: %[[VAL_10:.*]] = fir.emboxchar %[[VAL_9]], %[[VAL_3]] : (!fir.ref>, index) -> !fir.boxchar<1> + + +func.func @array_section(%arg0: !fir.ref>) { + %c10 = arith.constant 10 : index + %0 = fir.shape %c10 : (index) -> !fir.shape<1> + %1:2 = hlfir.declare %arg0(%0) {uniq_name = "x"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) + %c2 = arith.constant 2 : index + %c8 = arith.constant 8 : index + %c3 = arith.constant 3 : index + %2 = fir.shape %c3 : (index) -> !fir.shape<1> + %3 = hlfir.designate %1#0 (%c2:%c8:%c3) shape %2 : (!fir.ref>, index, index, index, !fir.shape<1>) -> !fir.box> + return +} +// CHECK-LABEL: func.func @array_section( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>) { +// CHECK: %[[VAL_1:.*]] = arith.constant 10 : index +// CHECK: %[[VAL_2:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_0]](%[[VAL_2]]) {uniq_name = "x"} : (!fir.ref>, !fir.shape<1>) -> !fir.ref> +// CHECK: %[[VAL_4:.*]] = arith.constant 2 : index +// CHECK: %[[VAL_5:.*]] = arith.constant 8 : index +// CHECK: %[[VAL_6:.*]] = arith.constant 3 : index +// CHECK: %[[VAL_7:.*]] = fir.shape %[[VAL_6]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_8:.*]] = fir.undefined index +// CHECK: %[[VAL_9:.*]] = fir.slice %[[VAL_4]], %[[VAL_5]], %[[VAL_6]] : (index, index, index) -> !fir.slice<1> +// CHECK: %[[VAL_10:.*]] = fir.embox %[[VAL_3]](%[[VAL_2]]) {{\[}}%[[VAL_9]]] : (!fir.ref>, !fir.shape<1>, !fir.slice<1>) -> !fir.box> + + +func.func @array_section_2(%arg0: !fir.box>, %arg1: !fir.ref) { + %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %1:2 = hlfir.declare %arg0 {uniq_name = "x"} : (!fir.box>) -> (!fir.box>, !fir.box>) + %2 = fir.load %0#0 : !fir.ref + %c0 = arith.constant 0 : index + %3:3 = fir.box_dims %1#1, %c0 : (!fir.box>, index) -> (index, index, index) + %4 = fir.convert %2 : (i64) -> index + %c3 = arith.constant 3 : index + %c42 = arith.constant 42 : index + %5 = fir.shape %c42 : (index) -> !fir.shape<1> + %6 = hlfir.designate %1#0 (%4:%3#1:%c3) shape %5 : (!fir.box>, index, index, index, !fir.shape<1>) -> !fir.box> + return +} +// CHECK-LABEL: func.func @array_section_2( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.box>, +// CHECK-SAME: %[[VAL_1:.*]]: !fir.ref) { +// CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref) -> !fir.ref +// CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_0]] {uniq_name = "x"} : (!fir.box>) -> !fir.box> +// CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_2]] : !fir.ref +// CHECK: %[[VAL_6:.*]] = arith.constant 0 : index +// CHECK: %[[VAL_7:.*]]:3 = fir.box_dims %[[VAL_3]], %[[VAL_6]] : (!fir.box>, index) -> (index, index, index) +// CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_5]] : (i64) -> index +// CHECK: %[[VAL_9:.*]] = arith.constant 3 : index +// CHECK: %[[VAL_10:.*]] = arith.constant 42 : index +// CHECK: %[[VAL_11:.*]] = fir.shape %[[VAL_10]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_12:.*]] = fir.undefined index +// CHECK: %[[VAL_13:.*]] = fir.slice %[[VAL_8]], %[[VAL_7]]#1, %[[VAL_9]] : (index, index, index) -> !fir.slice<1> +// CHECK: %[[VAL_14:.*]] = fir.rebox %[[VAL_3]] {{\[}}%[[VAL_13]]] : (!fir.box>, !fir.slice<1>) -> !fir.box> + + +func.func @char_array_section(%arg0: !fir.box>>, %arg1: !fir.ref) { + %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %1:2 = hlfir.declare %arg0 {uniq_name = "x"} : (!fir.box>>) -> (!fir.box>>, !fir.box>>) + %2 = fir.box_elesize %1#1 : (!fir.box>>) -> index + %c1 = arith.constant 1 : index + %c0 = arith.constant 0 : index + %3:3 = fir.box_dims %1#1, %c0 : (!fir.box>>, index) -> (index, index, index) + %c3 = arith.constant 3 : index + %c42 = arith.constant 42 : index + %4 = fir.shape %c42 : (index) -> !fir.shape<1> + %5 = hlfir.designate %1#0 (%c1:%3#1:%c3) shape %4 typeparams %2 : (!fir.box>>, index, index, index, !fir.shape<1>, index) -> !fir.box>> + return +} +// CHECK-LABEL: func.func @char_array_section( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.box>>, +// CHECK-SAME: %[[VAL_1:.*]]: !fir.ref) { +// CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref) -> !fir.ref +// CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_0]] {uniq_name = "x"} : (!fir.box>>) -> !fir.box>> +// CHECK: %[[VAL_5:.*]] = fir.box_elesize %[[VAL_3]] : (!fir.box>>) -> index +// CHECK: %[[VAL_6:.*]] = arith.constant 1 : index +// CHECK: %[[VAL_7:.*]] = arith.constant 0 : index +// CHECK: %[[VAL_8:.*]]:3 = fir.box_dims %[[VAL_3]], %[[VAL_7]] : (!fir.box>>, index) -> (index, index, index) +// CHECK: %[[VAL_9:.*]] = arith.constant 3 : index +// CHECK: %[[VAL_10:.*]] = arith.constant 42 : index +// CHECK: %[[VAL_11:.*]] = fir.shape %[[VAL_10]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_12:.*]] = fir.undefined index +// CHECK: %[[VAL_13:.*]] = fir.slice %[[VAL_6]], %[[VAL_8]]#1, %[[VAL_9]] : (index, index, index) -> !fir.slice<1> +// CHECK: %[[VAL_14:.*]] = fir.rebox %[[VAL_3]] {{\[}}%[[VAL_13]]] : (!fir.box>>, !fir.slice<1>) -> !fir.box>> + + +func.func @char_array_section_cst_len(%arg0: !fir.box>>, %arg1: !fir.ref) { + %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %c5 = arith.constant 5 : index + %1:2 = hlfir.declare %arg0 typeparams %c5 {uniq_name = "x"} : (!fir.box>>, index) -> (!fir.box>>, !fir.box>>) + %c1 = arith.constant 1 : index + %c0 = arith.constant 0 : index + %2:3 = fir.box_dims %1#1, %c0 : (!fir.box>>, index) -> (index, index, index) + %c3 = arith.constant 3 : index + %c42 = arith.constant 42 : index + %3 = fir.shape %c42 : (index) -> !fir.shape<1> + %4 = hlfir.designate %1#0 (%c1:%2#1:%c3) shape %3 typeparams %c5 : (!fir.box>>, index, index, index, !fir.shape<1>, index) -> !fir.box>> + return +} +// CHECK-LABEL: func.func @char_array_section_cst_len( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.box>>, +// CHECK-SAME: %[[VAL_1:.*]]: !fir.ref) { +// CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref) -> !fir.ref +// CHECK: %[[VAL_3:.*]] = arith.constant 5 : index +// CHECK: %[[VAL_4:.*]] = fir.declare %[[VAL_0]] typeparams %[[VAL_3]] {uniq_name = "x"} : (!fir.box>>, index) -> !fir.box>> +// CHECK: %[[VAL_6:.*]] = arith.constant 1 : index +// CHECK: %[[VAL_7:.*]] = arith.constant 0 : index +// CHECK: %[[VAL_8:.*]]:3 = fir.box_dims %[[VAL_4]], %[[VAL_7]] : (!fir.box>>, index) -> (index, index, index) +// CHECK: %[[VAL_9:.*]] = arith.constant 3 : index +// CHECK: %[[VAL_10:.*]] = arith.constant 42 : index +// CHECK: %[[VAL_11:.*]] = fir.shape %[[VAL_10]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_12:.*]] = fir.undefined index +// CHECK: %[[VAL_13:.*]] = fir.slice %[[VAL_6]], %[[VAL_8]]#1, %[[VAL_9]] : (index, index, index) -> !fir.slice<1> +// CHECK: %[[VAL_14:.*]] = fir.rebox %[[VAL_4]] {{\[}}%[[VAL_13]]] : (!fir.box>>, !fir.slice<1>) -> !fir.box>>