[flang-commits] [flang] b0de872 - [flang] Retrieve the correct scope when lowering SELECT TYPE

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Thu Feb 16 00:05:40 PST 2023


Author: Valentin Clement
Date: 2023-02-16T09:05:35+01:00
New Revision: b0de87268a60e9e755b34b2fb505589e01aab14c

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

LOG: [flang] Retrieve the correct scope when lowering SELECT TYPE

Scope to retrieve the associating entity is needed to map the
symbol to the IR value. The scope can be found with a source
information. For the type case in SELECT TYPE construct, the source
information is on the Statement<TypeCase>. This patch updates
the lowering so the scopes for each type guards is retrieved
before the processing.

Reviewed By: PeteSteinfeld, vdonaldson

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

Added: 
    

Modified: 
    flang/lib/Lower/Bridge.cpp
    flang/test/Lower/select-type.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index a87027e9d19d6..717bdeccd5572 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -2179,6 +2179,19 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     unsigned typeGuardIdx = 0;
     std::size_t defaultAttrPos = std::numeric_limits<size_t>::max();
     bool hasLocalScope = false;
+    llvm::SmallVector<const Fortran::semantics::Scope *> typeCaseScopes;
+
+    const auto &typeCaseList =
+        std::get<std::list<Fortran::parser::SelectTypeConstruct::TypeCase>>(
+            selectTypeConstruct.t);
+    for (const auto &typeCase : typeCaseList) {
+      const auto &stmt =
+          std::get<Fortran::parser::Statement<Fortran::parser::TypeGuardStmt>>(
+              typeCase.t);
+      const Fortran::semantics::Scope &scope =
+          bridge.getSemanticsContext().FindScope(stmt.source);
+      typeCaseScopes.push_back(&scope);
+    }
 
     for (Fortran::lower::pft::Evaluation &eval :
          getEval().getNestedEvaluations()) {
@@ -2275,13 +2288,11 @@ class FirConverter : public Fortran::lower::AbstractConverter {
                "TypeGuard attribute missing");
         mlir::Attribute typeGuardAttr = attrList[typeGuardIdx];
         mlir::Block *typeGuardBlock = blockList[typeGuardIdx];
-        const Fortran::semantics::Scope &guardScope =
-            bridge.getSemanticsContext().FindScope(eval.position);
         mlir::OpBuilder::InsertPoint crtInsPt = builder->saveInsertionPoint();
         builder->setInsertionPointToStart(typeGuardBlock);
 
         auto addAssocEntitySymbol = [&](fir::ExtendedValue exv) {
-          for (auto &symbol : guardScope.GetSymbols()) {
+          for (auto &symbol : typeCaseScopes[typeGuardIdx]->GetSymbols()) {
             if (symbol->GetUltimate()
                     .detailsIf<Fortran::semantics::AssocEntityDetails>()) {
               addSymbol(symbol, exv);

diff  --git a/flang/test/Lower/select-type.f90 b/flang/test/Lower/select-type.f90
index 846758281be84..b58518500cdeb 100644
--- a/flang/test/Lower/select-type.f90
+++ b/flang/test/Lower/select-type.f90
@@ -756,6 +756,23 @@ subroutine select_type13(a)
 ! CHECK: ^bb6:
 ! CHECK: ^bb7:
 
+  subroutine select_type14(a, b)
+    class(p1) :: a, b
+
+    select type(a)
+      type is (p2)
+        select type (b)
+          type is (p2)
+            print*,a%c,b%C
+        end select
+      class default
+        print*,a%a
+    end select
+  end subroutine
+
+  ! Just makes sure the example can be lowered.
+  ! CHECK-LABEL: func.func @_QMselect_type_lower_testPselect_type14
+   
 end module
 
 program test_select_type


        


More information about the flang-commits mailing list