[flang-commits] [PATCH] D139519: [flang] Add hlfir.as_expr definition
Jean Perier via Phabricator via flang-commits
flang-commits at lists.llvm.org
Wed Dec 7 02:53:54 PST 2022
jeanPerier created this revision.
jeanPerier added a reviewer: clementval.
Herald added a subscriber: mehdi_amini.
Herald added projects: Flang, All.
jeanPerier requested review of this revision.
Herald added a subscriber: jdoerfert.
hlfir.as_expr allows taking a value from a character, derived type,
or array expressions. This will allow implementing parentheses.
Combining as_expr + hlfir.associate will allow creating a variable copy
into a new temporary variable.
A later patch will add the ability to "move" a variable into an
expression (to give ownership of the variable storage to the expression,
with the commitment that the variable will not be used anymore).
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D139519
Files:
flang/include/flang/Optimizer/HLFIR/HLFIROps.td
flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
flang/test/HLFIR/as_expr.fir
Index: flang/test/HLFIR/as_expr.fir
===================================================================
--- /dev/null
+++ flang/test/HLFIR/as_expr.fir
@@ -0,0 +1,35 @@
+// Test hlfir.as_expr operation parse, verify (no errors), and unparse.
+
+// RUN: fir-opt %s | fir-opt | FileCheck %s
+
+func.func @char_expr(%arg0: !fir.boxchar<1>) {
+ %0 = hlfir.as_expr %arg0 : (!fir.boxchar<1>) -> !hlfir.expr<!fir.char<1,?>>
+ return
+}
+// CHECK-LABEL: func.func @char_expr(
+// CHECK-SAME: %[[VAL_0:.*]]: !fir.boxchar<1>) {
+// CHECK: hlfir.as_expr %[[VAL_0]] : (!fir.boxchar<1>) -> !hlfir.expr<!fir.char<1,?>>
+
+func.func @char_expr_2(%arg0: !fir.ref<!fir.char<1,10>>) {
+ %0 = hlfir.as_expr %arg0 : (!fir.ref<!fir.char<1,10>>) -> !hlfir.expr<!fir.char<1,10>>
+ return
+}
+// CHECK-LABEL: func.func @char_expr_2(
+// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.char<1,10>>) {
+// CHECK: hlfir.as_expr %[[VAL_0]] : (!fir.ref<!fir.char<1,10>>) -> !hlfir.expr<!fir.char<1,10>>
+
+func.func @array_expr(%arg0: !fir.box<!fir.array<?xi32>>) {
+ %0 = hlfir.as_expr %arg0 : (!fir.box<!fir.array<?xi32>>) -> !hlfir.expr<?xi32>
+ return
+}
+// CHECK-LABEL: func.func @array_expr(
+// CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?xi32>>) {
+// CHECK: hlfir.as_expr %[[VAL_0]] : (!fir.box<!fir.array<?xi32>>) -> !hlfir.expr<?xi32>
+
+func.func @array_expr_2(%arg0: !fir.ref<!fir.array<10xi32>>) {
+ %0 = hlfir.as_expr %arg0 : (!fir.ref<!fir.array<10xi32>>) -> !hlfir.expr<10xi32>
+ return
+}
+// CHECK-LABEL: func.func @array_expr_2(
+// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<10xi32>>) {
+// CHECK: hlfir.as_expr %[[VAL_0]] : (!fir.ref<!fir.array<10xi32>>) -> !hlfir.expr<10xi32>
Index: flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
===================================================================
--- flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
+++ flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
@@ -415,5 +415,23 @@
associate.getMustFreeStrorageFlag());
}
+//===----------------------------------------------------------------------===//
+// AsExprOp
+//===----------------------------------------------------------------------===//
+
+void hlfir::AsExprOp::build(mlir::OpBuilder &builder,
+ mlir::OperationState &result, mlir::Value var) {
+ hlfir::ExprType::Shape typeShape;
+ mlir::Type type = getFortranElementOrSequenceType(var.getType());
+ if (auto seqType = type.dyn_cast<fir::SequenceType>()) {
+ typeShape.append(seqType.getShape().begin(), seqType.getShape().end());
+ type = seqType.getEleTy();
+ }
+
+ auto resultType = hlfir::ExprType::get(builder.getContext(), typeShape, type,
+ /*isPolymorphic: TODO*/ false);
+ return build(builder, result, resultType, var);
+}
+
#define GET_OP_CLASSES
#include "flang/Optimizer/HLFIR/HLFIROps.cpp.inc"
Index: flang/include/flang/Optimizer/HLFIR/HLFIROps.td
===================================================================
--- flang/include/flang/Optimizer/HLFIR/HLFIROps.td
+++ flang/include/flang/Optimizer/HLFIR/HLFIROps.td
@@ -300,4 +300,23 @@
let builders = [OpBuilder<(ins "hlfir::AssociateOp":$associate)>];
}
+def hlfir_AsExprOp : hlfir_Op<"as_expr", []> {
+ let summary = "Take the value of an array, character or derived expression";
+
+ let description = [{
+ Take the value of an array, character or derived expression.
+ }];
+
+ let arguments = (ins AnyFortranVariable:$var);
+ let results = (outs hlfir_ExprType);
+
+ let assemblyFormat = [{
+ $var attr-dict `:` functional-type(operands, results)
+ }];
+
+
+ let builders = [OpBuilder<(ins "mlir::Value":$var)>];
+}
+
+
#endif // FORTRAN_DIALECT_HLFIR_OPS
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139519.480824.patch
Type: text/x-patch
Size: 3711 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221207/34602e06/attachment.bin>
More information about the flang-commits
mailing list