[flang-commits] [flang] 0453650 - [flang] add hlfir.noreassoc codegen

Jean Perier via flang-commits flang-commits at lists.llvm.org
Thu Dec 8 00:34:21 PST 2022


Author: Jean Perier
Date: 2022-12-08T09:33:32+01:00
New Revision: 045365036d3233cfb22ffbb4981f10632e1dd7af

URL: https://github.com/llvm/llvm-project/commit/045365036d3233cfb22ffbb4981f10632e1dd7af
DIFF: https://github.com/llvm/llvm-project/commit/045365036d3233cfb22ffbb4981f10632e1dd7af.diff

LOG: [flang] add hlfir.noreassoc codegen

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

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

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

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
index b0015a1ea01ce..c8a3536cf802d 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
@@ -244,6 +244,20 @@ struct EndAssociateOpConversion
   }
 };
 
+struct NoReassocOpConversion
+    : public mlir::OpConversionPattern<hlfir::NoReassocOp> {
+  using mlir::OpConversionPattern<hlfir::NoReassocOp>::OpConversionPattern;
+  explicit NoReassocOpConversion(mlir::MLIRContext *ctx)
+      : mlir::OpConversionPattern<hlfir::NoReassocOp>{ctx} {}
+  mlir::LogicalResult
+  matchAndRewrite(hlfir::NoReassocOp noreassoc, OpAdaptor adaptor,
+                  mlir::ConversionPatternRewriter &rewriter) const override {
+    rewriter.replaceOpWithNewOp<hlfir::NoReassocOp>(
+        noreassoc, getBufferizedExprStorage(adaptor.getVal()));
+    return mlir::success();
+  }
+};
+
 class BufferizeHLFIR : public hlfir::impl::BufferizeHLFIRBase<BufferizeHLFIR> {
 public:
   void runOnOperation() override {
@@ -257,9 +271,9 @@ class BufferizeHLFIR : public hlfir::impl::BufferizeHLFIRBase<BufferizeHLFIR> {
     auto module = this->getOperation();
     auto *context = &getContext();
     mlir::RewritePatternSet patterns(context);
-    patterns
-        .insert<AsExprOpConversion, AssignOpConversion, AssociateOpConversion,
-                ConcatOpConversion, EndAssociateOpConversion>(context);
+    patterns.insert<AsExprOpConversion, AssignOpConversion,
+                    AssociateOpConversion, ConcatOpConversion,
+                    EndAssociateOpConversion, NoReassocOpConversion>(context);
     mlir::ConversionTarget target(*context);
     target.addIllegalOp<hlfir::AssociateOp, hlfir::EndAssociateOp>();
     target.markUnknownOpDynamicallyLegal([](mlir::Operation *op) {

diff  --git a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
index 9c0e516e453f4..9f64eb41e6528 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
@@ -258,6 +258,21 @@ class DesignateOpConversion
   }
 };
 
+class NoReassocOpConversion
+    : public mlir::OpRewritePattern<hlfir::NoReassocOp> {
+public:
+  explicit NoReassocOpConversion(mlir::MLIRContext *ctx)
+      : OpRewritePattern{ctx} {}
+
+  mlir::LogicalResult
+  matchAndRewrite(hlfir::NoReassocOp noreassoc,
+                  mlir::PatternRewriter &rewriter) const override {
+    rewriter.replaceOpWithNewOp<fir::NoReassocOp>(noreassoc,
+                                                  noreassoc.getVal());
+    return mlir::success();
+  }
+};
+
 class ConvertHLFIRtoFIR
     : public hlfir::impl::ConvertHLFIRtoFIRBase<ConvertHLFIRtoFIR> {
 public:
@@ -269,9 +284,8 @@ class ConvertHLFIRtoFIR
     auto module = this->getOperation();
     auto *context = &getContext();
     mlir::RewritePatternSet patterns(context);
-    patterns
-        .insert<AssignOpConversion, DeclareOpConversion, DesignateOpConversion>(
-            context);
+    patterns.insert<AssignOpConversion, DeclareOpConversion,
+                    DesignateOpConversion, NoReassocOpConversion>(context);
     mlir::ConversionTarget target(*context);
     target.addIllegalDialect<hlfir::hlfirDialect>();
     target.markUnknownOpDynamicallyLegal(
@@ -284,7 +298,6 @@ class ConvertHLFIRtoFIR
     }
   }
 };
-
 } // namespace
 
 std::unique_ptr<mlir::Pass> hlfir::createConvertHLFIRtoFIRPass() {

diff  --git a/flang/test/HLFIR/no_reassoc-codegen.fir b/flang/test/HLFIR/no_reassoc-codegen.fir
new file mode 100644
index 0000000000000..f5686863b331c
--- /dev/null
+++ b/flang/test/HLFIR/no_reassoc-codegen.fir
@@ -0,0 +1,31 @@
+// Test hlfir.noreassoc code generation to FIR.
+
+// RUN: fir-opt %s -bufferize-hlfir -convert-hlfir-to-fir | FileCheck %s
+
+func.func @no_reassoc_expr(%addr: !fir.ref<!fir.char<1,?>>, %len: index) {
+  %0:2 = hlfir.declare %addr typeparams %len {uniq_name = "c"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+  %1 = hlfir.as_expr %0#0 : (!fir.boxchar<1>) -> !hlfir.expr<!fir.char<1,?>>
+  %2 = hlfir.no_reassoc %1 : !hlfir.expr<!fir.char<1,?>>
+  return
+}
+// CHECK:  %[[VAL_4:.*]] = fir.alloca !fir.char<1,?>
+// CHECK:  %[[VAL_5:.*]] = fir.declare %[[VAL_4]] {{.*}}
+// CHECK:  %[[VAL_6:.*]] = fir.emboxchar %[[VAL_5]], %{{.*}}
+// CHECK:  fir.no_reassoc %[[VAL_6]] : !fir.boxchar<1>
+
+func.func @no_reassoc_var(%addr: !fir.ref<!fir.char<1,?>>, %len: index) {
+  %0:2 = hlfir.declare %addr typeparams %len {uniq_name = "c"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+  %1 = hlfir.no_reassoc %0#0 : !fir.boxchar<1>
+  return
+}
+// CHECK-LABEL:   func.func @no_reassoc_var(
+// CHECK:  %[[VAL_3:.*]] = fir.emboxchar
+// CHECK:  fir.no_reassoc %[[VAL_3]] : !fir.boxchar<1>
+
+func.func @no_reassoc_value(%arg0 : i32) {
+  %0 = hlfir.no_reassoc %arg0 : i32
+  return
+}
+// CHECK-LABEL:   func.func @no_reassoc_value(
+// CHECK-SAME:    %[[VAL_0:.*]]: i32) {
+// CHECK:  fir.no_reassoc %[[VAL_0]] : i32


        


More information about the flang-commits mailing list