[flang-commits] [flang] 4b604cc - [flang] Update fir.select_type op to fit design document
Valentin Clement via flang-commits
flang-commits at lists.llvm.org
Fri Nov 4 07:00:22 PDT 2022
Author: Valentin Clement
Date: 2022-11-04T15:00:14+01:00
New Revision: 4b604cc27e28a0edcb22a5ae5f29b8ff3bd0eacf
URL: https://github.com/llvm/llvm-project/commit/4b604cc27e28a0edcb22a5ae5f29b8ff3bd0eacf
DIFF: https://github.com/llvm/llvm-project/commit/4b604cc27e28a0edcb22a5ae5f29b8ff3bd0eacf.diff
LOG: [flang] Update fir.select_type op to fit design document
Update the already existing fir.select_type operation to
reflect decisions made in the polymorphic entities design document.
The verifier is updated to check that the select is polymorphic.
The case attributes name are changed and some tests are added.
Reviewed By: jeanPerier
Differential Revision: https://reviews.llvm.org/D137403
Added:
Modified:
flang/include/flang/Optimizer/Dialect/FIRAttr.h
flang/include/flang/Optimizer/Dialect/FIROps.td
flang/lib/Optimizer/Dialect/FIROps.cpp
flang/test/Fir/convert-to-llvm-invalid.fir
flang/test/Fir/fir-ops.fir
flang/test/Fir/invalid.fir
Removed:
################################################################################
diff --git a/flang/include/flang/Optimizer/Dialect/FIRAttr.h b/flang/include/flang/Optimizer/Dialect/FIRAttr.h
index 92b3f7a8e6f62..f88d6c6a4f97f 100644
--- a/flang/include/flang/Optimizer/Dialect/FIRAttr.h
+++ b/flang/include/flang/Optimizer/Dialect/FIRAttr.h
@@ -38,7 +38,7 @@ class ExactTypeAttr
using Base::Base;
using ValueType = mlir::Type;
- static constexpr llvm::StringRef getAttrName() { return "instance"; }
+ static constexpr llvm::StringRef getAttrName() { return "type_is"; }
static ExactTypeAttr get(mlir::Type value);
mlir::Type getType() const;
@@ -51,7 +51,7 @@ class SubclassAttr
using Base::Base;
using ValueType = mlir::Type;
- static constexpr llvm::StringRef getAttrName() { return "subsumed"; }
+ static constexpr llvm::StringRef getAttrName() { return "class_is"; }
static SubclassAttr get(mlir::Type value);
mlir::Type getType() const;
diff --git a/flang/include/flang/Optimizer/Dialect/FIROps.td b/flang/include/flang/Optimizer/Dialect/FIROps.td
index 12938525b7062..76a117d78e73e 100644
--- a/flang/include/flang/Optimizer/Dialect/FIROps.td
+++ b/flang/include/flang/Optimizer/Dialect/FIROps.td
@@ -651,10 +651,10 @@ def fir_SelectTypeOp : fir_SwitchTerminatorOp<"select_type"> {
```mlir
fir.select_type %arg : !fir.box<()> [
- #fir.instance<!fir.type<type1>>, ^bb1(%0 : i32),
- #fir.instance<!fir.type<type2>>, ^bb2(%2 : i32),
- #fir.subsumed<!fir.type<type3>>, ^bb3(%2 : i32),
- #fir.instance<!fir.type<type4>>, ^bb4(%1,%3 : i32,f32),
+ #fir.type_is<!fir.type<type1>>, ^bb1(%0 : i32),
+ #fir.type_is<!fir.type<type2>>, ^bb2(%2 : i32),
+ #fir.class_is<!fir.type<type3>>, ^bb3(%2 : i32),
+ #fir.type_is<!fir.type<type4>>, ^bb4(%1,%3 : i32,f32),
unit, ^bb5]
```
}];
diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp
index 450900b351029..86628b792068b 100644
--- a/flang/lib/Optimizer/Dialect/FIROps.cpp
+++ b/flang/lib/Optimizer/Dialect/FIROps.cpp
@@ -2970,8 +2970,11 @@ void fir::SelectTypeOp::print(mlir::OpAsmPrinter &p) {
}
mlir::LogicalResult fir::SelectTypeOp::verify() {
- if (!(getSelector().getType().isa<fir::BoxType>()))
- return emitOpError("must be a boxed type");
+ if (!(getSelector().getType().isa<fir::BaseBoxType>()))
+ return emitOpError("must be a fir.class or fir.box type");
+ if (auto boxType = getSelector().getType().dyn_cast<fir::BoxType>())
+ if (!boxType.getEleTy().isa<mlir::NoneType>())
+ return emitOpError("selector must be polymorphic");
auto cases =
getOperation()->getAttrOfType<mlir::ArrayAttr>(getCasesAttr()).getValue();
auto count = getNumDest();
diff --git a/flang/test/Fir/convert-to-llvm-invalid.fir b/flang/test/Fir/convert-to-llvm-invalid.fir
index 7cba7fc55b8e3..bdc2525876ac2 100644
--- a/flang/test/Fir/convert-to-llvm-invalid.fir
+++ b/flang/test/Fir/convert-to-llvm-invalid.fir
@@ -71,14 +71,14 @@ func.func @shape_shift_not_dead(%arg0: !fir.ref<!fir.array<?x?xf32>>, %i: index,
// Test `fir.select_type` conversion to llvm.
// Should have been converted.
-func.func @bar_select_type(%arg : !fir.box<!fir.ref<f32>>) -> i32 {
+func.func @bar_select_type(%arg : !fir.class<!fir.type<derivedst{a:f32}>>) -> i32 {
%0 = arith.constant 1 : i32
%2 = arith.constant 3 : i32
// expected-error at +2{{fir.select_type should have already been converted}}
// expected-error at +1{{failed to legalize operation 'fir.select_type'}}
- fir.select_type %arg : !fir.box<!fir.ref<f32>> [
- #fir.instance<!fir.int<4>>,^bb1(%0:i32),
- #fir.instance<!fir.int<8>>,^bb2(%2:i32),
+ fir.select_type %arg : !fir.class<!fir.type<derivedst{a:f32}>> [
+ #fir.type_is<!fir.int<4>>,^bb1(%0:i32),
+ #fir.type_is<!fir.int<8>>,^bb2(%2:i32),
unit,^bb5 ]
^bb1(%a : i32) :
return %a : i32
diff --git a/flang/test/Fir/fir-ops.fir b/flang/test/Fir/fir-ops.fir
index 5052a06d90bcd..486c7ee809910 100644
--- a/flang/test/Fir/fir-ops.fir
+++ b/flang/test/Fir/fir-ops.fir
@@ -322,8 +322,8 @@ func.func @bar_select_rank(%arg : i32, %arg2 : i32) -> i32 {
}
// CHECK-LABEL: func @bar_select_type(
-// CHECK-SAME: [[VAL_101:%.*]]: !fir.box<!fir.type<name(param1:i32){fld:!fir.char<1>}>>) -> i32 {
-func.func @bar_select_type(%arg : !fir.box<!fir.type<name(param1:i32){fld:!fir.char<1>}>>) -> i32 {
+// CHECK-SAME: [[VAL_101:%.*]]: !fir.class<!fir.type<name(param1:i32){fld:!fir.char<1>}>>) -> i32 {
+func.func @bar_select_type(%arg : !fir.class<!fir.type<name(param1:i32){fld:!fir.char<1>}>>) -> i32 {
// CHECK: [[VAL_102:%.*]] = arith.constant 1 : i32
// CHECK: [[VAL_103:%.*]] = arith.constant 2 : i32
@@ -334,8 +334,8 @@ func.func @bar_select_type(%arg : !fir.box<!fir.type<name(param1:i32){fld:!fir.c
%2 = arith.constant 3 : i32
%3 = arith.constant 4 : i32
-// CHECK: fir.select_type [[VAL_101]] : !fir.box<!fir.type<name(param1:i32){fld:!fir.char<1>}>> [#fir.instance<!fir.int<4>>, ^bb1([[VAL_102]] : i32), #fir.instance<!fir.int<8>>, ^bb2([[VAL_104]] : i32), #fir.subsumed<!fir.int<2>>, ^bb3([[VAL_104]] : i32), #fir.instance<!fir.int<1>>, ^bb4([[VAL_103]] : i32), unit, ^bb5]
- fir.select_type %arg : !fir.box<!fir.type<name(param1:i32){fld:!fir.char<1>}>> [ #fir.instance<!fir.int<4>>,^bb1(%0:i32), #fir.instance<!fir.int<8>>,^bb2(%2:i32), #fir.subsumed<!fir.int<2>>,^bb3(%2:i32), #fir.instance<!fir.int<1>>,^bb4(%1:i32), unit,^bb5 ]
+// CHECK: fir.select_type [[VAL_101]] : !fir.class<!fir.type<name(param1:i32){fld:!fir.char<1>}>> [#fir.type_is<!fir.int<4>>, ^bb1([[VAL_102]] : i32), #fir.type_is<!fir.int<8>>, ^bb2([[VAL_104]] : i32), #fir.class_is<!fir.int<2>>, ^bb3([[VAL_104]] : i32), #fir.type_is<!fir.int<1>>, ^bb4([[VAL_103]] : i32), unit, ^bb5]
+ fir.select_type %arg : !fir.class<!fir.type<name(param1:i32){fld:!fir.char<1>}>> [ #fir.type_is<!fir.int<4>>,^bb1(%0:i32), #fir.type_is<!fir.int<8>>,^bb2(%2:i32), #fir.class_is<!fir.int<2>>,^bb3(%2:i32), #fir.type_is<!fir.int<1>>,^bb4(%1:i32), unit,^bb5 ]
// CHECK: ^bb1([[VAL_106:%.*]]: i32):
// CHECK: return [[VAL_106]] : i32
diff --git a/flang/test/Fir/invalid.fir b/flang/test/Fir/invalid.fir
index 5d0ac39184211..fdb4249c4ad81 100644
--- a/flang/test/Fir/invalid.fir
+++ b/flang/test/Fir/invalid.fir
@@ -928,3 +928,22 @@ func.func @bad_array_declare_unlimited_polymorphic_boxaddr(%arg0: !fir.ref<!fir.
%0 = fir.declare %arg0(%shape) {uniq_name = "x"} : (!fir.ref<!fir.class<!fir.ptr<!fir.array<?x?xnone>>>>, !fir.shift<2>) -> !fir.ref<!fir.class<!fir.ptr<!fir.array<?x?xnone>>>>
return
}
+
+// -----
+
+func.func @invalid_selector(%arg : !fir.box<!fir.ref<i32>>) -> i32 {
+ %0 = arith.constant 1 : i32
+ %2 = arith.constant 3 : i32
+ // expected-error at +1{{'fir.select_type' op selector must be polymorphic}}
+ fir.select_type %arg : !fir.box<!fir.ref<i32>> [
+ #fir.type_is<!fir.int<4>>,^bb1(%0:i32),
+ #fir.type_is<!fir.int<8>>,^bb2(%2:i32),
+ unit,^bb5 ]
+^bb1(%a : i32) :
+ return %a : i32
+^bb2(%b : i32) :
+ return %b : i32
+^bb5 :
+ %zero = arith.constant 0 : i32
+ return %zero : i32
+}
More information about the flang-commits
mailing list