diff --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp --- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp +++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp @@ -244,6 +244,20 @@ } }; +struct NoReassocOpConversion + : public mlir::OpConversionPattern { + using mlir::OpConversionPattern::OpConversionPattern; + explicit NoReassocOpConversion(mlir::MLIRContext *ctx) + : mlir::OpConversionPattern{ctx} {} + mlir::LogicalResult + matchAndRewrite(hlfir::NoReassocOp noreassoc, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const override { + rewriter.replaceOpWithNewOp( + noreassoc, getBufferizedExprStorage(adaptor.getVal())); + return mlir::success(); + } +}; + class BufferizeHLFIR : public hlfir::impl::BufferizeHLFIRBase { public: void runOnOperation() override { @@ -257,9 +271,9 @@ auto module = this->getOperation(); auto *context = &getContext(); mlir::RewritePatternSet patterns(context); - patterns - .insert(context); + patterns.insert(context); mlir::ConversionTarget target(*context); target.addIllegalOp(); target.markUnknownOpDynamicallyLegal([](mlir::Operation *op) { 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 @@ -258,6 +258,21 @@ } }; +class NoReassocOpConversion + : public mlir::OpRewritePattern { +public: + explicit NoReassocOpConversion(mlir::MLIRContext *ctx) + : OpRewritePattern{ctx} {} + + mlir::LogicalResult + matchAndRewrite(hlfir::NoReassocOp noreassoc, + mlir::PatternRewriter &rewriter) const override { + rewriter.replaceOpWithNewOp(noreassoc, + noreassoc.getVal()); + return mlir::success(); + } +}; + class ConvertHLFIRtoFIR : public hlfir::impl::ConvertHLFIRtoFIRBase { public: @@ -269,9 +284,8 @@ auto module = this->getOperation(); auto *context = &getContext(); mlir::RewritePatternSet patterns(context); - patterns - .insert( - context); + patterns.insert(context); mlir::ConversionTarget target(*context); target.addIllegalDialect(); target.markUnknownOpDynamicallyLegal( @@ -284,7 +298,6 @@ } } }; - } // namespace std::unique_ptr hlfir::createConvertHLFIRtoFIRPass() { diff --git a/flang/test/HLFIR/no_reassoc-codegen.fir b/flang/test/HLFIR/no_reassoc-codegen.fir new file mode 100644 --- /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>, %len: index) { + %0:2 = hlfir.declare %addr typeparams %len {uniq_name = "c"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %1 = hlfir.as_expr %0#0 : (!fir.boxchar<1>) -> !hlfir.expr> + %2 = hlfir.no_reassoc %1 : !hlfir.expr> + 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>, %len: index) { + %0:2 = hlfir.declare %addr typeparams %len {uniq_name = "c"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %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