[flang-commits] [flang] 61a124d - [flang] hlfir.apply codegen

Jean Perier via flang-commits flang-commits at lists.llvm.org
Fri Dec 16 00:29:02 PST 2022


Author: Jean Perier
Date: 2022-12-16T09:27:58+01:00
New Revision: 61a124db5647f83422476797a761d2ec22a2d17f

URL: https://github.com/llvm/llvm-project/commit/61a124db5647f83422476797a761d2ec22a2d17f
DIFF: https://github.com/llvm/llvm-project/commit/61a124db5647f83422476797a761d2ec22a2d17f.diff

LOG: [flang] hlfir.apply codegen

Lower hlfir.apply to hlfir.designate in bufferization.

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

Added: 
    flang/test/HLFIR/apply-codegen.fir

Modified: 
    flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
index 4a35b73113112..722a269683220 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
@@ -177,6 +177,26 @@ struct AsExprOpConversion : public mlir::OpConversionPattern<hlfir::AsExprOp> {
   }
 };
 
+struct ApplyOpConversion : public mlir::OpConversionPattern<hlfir::ApplyOp> {
+  using mlir::OpConversionPattern<hlfir::ApplyOp>::OpConversionPattern;
+  explicit ApplyOpConversion(mlir::MLIRContext *ctx)
+      : mlir::OpConversionPattern<hlfir::ApplyOp>{ctx} {}
+  mlir::LogicalResult
+  matchAndRewrite(hlfir::ApplyOp apply, OpAdaptor adaptor,
+                  mlir::ConversionPatternRewriter &rewriter) const override {
+    mlir::Location loc = apply->getLoc();
+    hlfir::Entity bufferizedExpr{getBufferizedExprStorage(adaptor.getExpr())};
+    mlir::Type resultType = hlfir::getVariableElementType(bufferizedExpr);
+    mlir::Value result = rewriter.create<hlfir::DesignateOp>(
+        loc, resultType, bufferizedExpr, adaptor.getIndices(),
+        adaptor.getTypeparams());
+    if (fir::isa_trivial(apply.getType()))
+      result = rewriter.create<fir::LoadOp>(loc, result);
+    rewriter.replaceOp(apply, result);
+    return mlir::success();
+  }
+};
+
 struct AssignOpConversion : public mlir::OpConversionPattern<hlfir::AssignOp> {
   using mlir::OpConversionPattern<hlfir::AssignOp>::OpConversionPattern;
   explicit AssignOpConversion(mlir::MLIRContext *ctx)
@@ -398,12 +418,12 @@ class BufferizeHLFIR : public hlfir::impl::BufferizeHLFIRBase<BufferizeHLFIR> {
     auto module = this->getOperation();
     auto *context = &getContext();
     mlir::RewritePatternSet patterns(context);
-    patterns
-        .insert<AsExprOpConversion, AssignOpConversion, AssociateOpConversion,
-                ConcatOpConversion, ElementalOpConversion,
-                EndAssociateOpConversion, NoReassocOpConversion>(context);
+    patterns.insert<ApplyOpConversion, AsExprOpConversion, AssignOpConversion,
+                    AssociateOpConversion, ConcatOpConversion,
+                    ElementalOpConversion, EndAssociateOpConversion,
+                    NoReassocOpConversion>(context);
     mlir::ConversionTarget target(*context);
-    target.addIllegalOp<hlfir::AssociateOp, hlfir::ElementalOp,
+    target.addIllegalOp<hlfir::ApplyOp, hlfir::AssociateOp, hlfir::ElementalOp,
                         hlfir::EndAssociateOp, hlfir::YieldElementOp>();
     target.markUnknownOpDynamicallyLegal([](mlir::Operation *op) {
       return llvm::all_of(

diff  --git a/flang/test/HLFIR/apply-codegen.fir b/flang/test/HLFIR/apply-codegen.fir
new file mode 100644
index 0000000000000..400637dc99eb6
--- /dev/null
+++ b/flang/test/HLFIR/apply-codegen.fir
@@ -0,0 +1,27 @@
+// Test hlfir.apply code generation
+// RUN: fir-opt %s -bufferize-hlfir | FileCheck %s
+
+func.func @numeric_apply(%arg0 : !fir.ref<!fir.array<100xi32>>) {
+  %expr = hlfir.as_expr %arg0 : (!fir.ref<!fir.array<100xi32>>) -> !hlfir.expr<100xi32>
+  %c42 = arith.constant 42 : index
+  %elem = hlfir.apply %expr, %c42 : (!hlfir.expr<100xi32>, index) -> i32
+  return
+}
+// CHECK-LABEL:   func.func @numeric_apply(
+// CHECK:    %[[VAL_5:.*]]:2 = hlfir.declare %{{.*}}(%{{.*}}) {uniq_name = ".tmp"}
+// CHECK:    %[[VAL_9:.*]] = arith.constant 42 : index
+// CHECK:    %[[VAL_10:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_9]])  : (!fir.heap<!fir.array<100xi32>>, index) -> !fir.ref<i32>
+// CHECK:    %[[VAL_11:.*]] = fir.load %[[VAL_10]] : !fir.ref<i32>
+
+func.func @character_apply(%arg0 : !fir.ref<!fir.array<?x!fir.char<2,?>>>, %l: index, %n: index) {
+  %shape = fir.shape %n : (index) -> !fir.shape<1>
+  %c:2 = hlfir.declare %arg0(%shape) typeparams %l {uniq_name = "c"} : (!fir.ref<!fir.array<?x!fir.char<2,?>>>, !fir.shape<1>, index) -> (!fir.box<!fir.array<?x!fir.char<2,?>>>, !fir.ref<!fir.array<?x!fir.char<2,?>>>)
+  %expr = hlfir.as_expr %c#0 : (!fir.box<!fir.array<?x!fir.char<2,?>>>) -> !hlfir.expr<?x!fir.char<2,?>>
+  %c42 = arith.constant 42 : index
+  %elem = hlfir.apply %expr, %c42 typeparams %l: (!hlfir.expr<?x!fir.char<2,?>>, index, index) -> !hlfir.expr<!fir.char<2,?>>
+  return
+}
+// CHECK-LABEL:   func.func @character_apply(
+// CHECK:    %[[VAL_7:.*]]:2 = hlfir.declare %{{.*}}(%{{.*}}) typeparams %[[VAL_1:.*]] {uniq_name = ".tmp"}
+// CHECK:    %[[VAL_11:.*]] = arith.constant 42 : index
+// CHECK:    %[[VAL_12:.*]] = hlfir.designate %[[VAL_7]]#0 (%[[VAL_11]])  typeparams %[[VAL_1]] : (!fir.box<!fir.array<?x!fir.char<2,?>>>, index, index) -> !fir.boxchar<2>


        


More information about the flang-commits mailing list