[flang-commits] [flang] cf73fae - [flang] Add hlfir.as_expr definition
Jean Perier via flang-commits
flang-commits at lists.llvm.org
Wed Dec 7 05:44:21 PST 2022
Author: Jean Perier
Date: 2022-12-07T14:43:45+01:00
New Revision: cf73faef9b9e4016304d3938c774bd9fed1916db
URL: https://github.com/llvm/llvm-project/commit/cf73faef9b9e4016304d3938c774bd9fed1916db
DIFF: https://github.com/llvm/llvm-project/commit/cf73faef9b9e4016304d3938c774bd9fed1916db.diff
LOG: [flang] Add hlfir.as_expr definition
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).
Differential Revision: https://reviews.llvm.org/D139519
Added:
flang/test/HLFIR/as_expr.fir
Modified:
flang/include/flang/Optimizer/HLFIR/HLFIROps.td
flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
Removed:
################################################################################
diff --git a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
index d576df57477ea..6bc744b8d312e 100644
--- a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
+++ b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
@@ -300,4 +300,23 @@ def hlfir_EndAssociateOp : hlfir_Op<"end_associate", []> {
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
diff --git a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
index 5bfa0a660be17..51876d8f4281b 100644
--- a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
+++ b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
@@ -415,5 +415,23 @@ void hlfir::EndAssociateOp::build(mlir::OpBuilder &builder,
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"
diff --git a/flang/test/HLFIR/as_expr.fir b/flang/test/HLFIR/as_expr.fir
new file mode 100644
index 0000000000000..c04e248ed2c9d
--- /dev/null
+++ b/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>
More information about the flang-commits
mailing list