[flang-commits] [flang] 72079d9 - [flang] Handle typeless (BOZ) arguments in AreCompatibleTypes()

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Aug 29 07:46:37 PDT 2023


Author: Peter Klausler
Date: 2023-08-29T07:46:24-07:00
New Revision: 72079d922588b615dc5b2178c268362128ff7b3b

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

LOG: [flang] Handle typeless (BOZ) arguments in AreCompatibleTypes()

The current code can crash due to the representation's use of a negative
INTEGER kind code to signify a typeless (BOZ) argument's "type" as a
DynamicType.  Detect and handle that case, and change some direct
uses of the kind_ data member into kind() accessor references in
places that shouldn't be confronted with BOZ.

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

Added: 
    

Modified: 
    flang/lib/Evaluate/type.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/type.cpp b/flang/lib/Evaluate/type.cpp
index cdded9677d73c6..1497c037d9cc6e 100644
--- a/flang/lib/Evaluate/type.cpp
+++ b/flang/lib/Evaluate/type.cpp
@@ -179,7 +179,7 @@ std::size_t DynamicType::GetAlignment(
       }
     }
   } else {
-    return targetCharacteristics.GetAlignment(category_, kind_);
+    return targetCharacteristics.GetAlignment(category_, kind());
   }
   return 1; // needs to be after switch to dodge a bogus gcc warning
 }
@@ -193,14 +193,14 @@ std::optional<Expr<SubscriptInteger>> DynamicType::MeasureSizeInBytes(
   case TypeCategory::Complex:
   case TypeCategory::Logical:
     return Expr<SubscriptInteger>{
-        context.targetCharacteristics().GetByteSize(category_, kind_)};
+        context.targetCharacteristics().GetByteSize(category_, kind())};
   case TypeCategory::Character:
     if (auto len{charLength ? Expr<SubscriptInteger>{Constant<SubscriptInteger>{
                                   *charLength}}
                             : GetCharLength()}) {
       return Fold(context,
           Expr<SubscriptInteger>{
-              context.targetCharacteristics().GetByteSize(category_, kind_)} *
+              context.targetCharacteristics().GetByteSize(category_, kind())} *
               std::move(*len));
     }
     break;
@@ -540,7 +540,11 @@ static bool AreCompatibleTypes(const DynamicType &x, const DynamicType &y,
     return x.kind() == y.kind() &&
         (ignoreLengths || !xLen || !yLen || *xLen == *yLen);
   } else if (x.category() != TypeCategory::Derived) {
-    return x.kind() == y.kind();
+    if (x.IsTypelessIntrinsicArgument()) {
+      return y.IsTypelessIntrinsicArgument();
+    } else {
+      return !y.IsTypelessIntrinsicArgument() && x.kind() == y.kind();
+    }
   } else {
     const auto *xdt{GetDerivedTypeSpec(x)};
     const auto *ydt{GetDerivedTypeSpec(y)};
@@ -651,7 +655,7 @@ DynamicType DynamicType::ResultTypeForMultiply(const DynamicType &that) const {
   case TypeCategory::Integer:
     switch (that.category_) {
     case TypeCategory::Integer:
-      return DynamicType{TypeCategory::Integer, std::max(kind_, that.kind_)};
+      return DynamicType{TypeCategory::Integer, std::max(kind(), that.kind())};
     case TypeCategory::Real:
     case TypeCategory::Complex:
       return that;
@@ -664,9 +668,9 @@ DynamicType DynamicType::ResultTypeForMultiply(const DynamicType &that) const {
     case TypeCategory::Integer:
       return *this;
     case TypeCategory::Real:
-      return DynamicType{TypeCategory::Real, std::max(kind_, that.kind_)};
+      return DynamicType{TypeCategory::Real, std::max(kind(), that.kind())};
     case TypeCategory::Complex:
-      return DynamicType{TypeCategory::Complex, std::max(kind_, that.kind_)};
+      return DynamicType{TypeCategory::Complex, std::max(kind(), that.kind())};
     default:
       CRASH_NO_CASE;
     }
@@ -677,7 +681,7 @@ DynamicType DynamicType::ResultTypeForMultiply(const DynamicType &that) const {
       return *this;
     case TypeCategory::Real:
     case TypeCategory::Complex:
-      return DynamicType{TypeCategory::Complex, std::max(kind_, that.kind_)};
+      return DynamicType{TypeCategory::Complex, std::max(kind(), that.kind())};
     default:
       CRASH_NO_CASE;
     }
@@ -685,7 +689,7 @@ DynamicType DynamicType::ResultTypeForMultiply(const DynamicType &that) const {
   case TypeCategory::Logical:
     switch (that.category_) {
     case TypeCategory::Logical:
-      return DynamicType{TypeCategory::Logical, std::max(kind_, that.kind_)};
+      return DynamicType{TypeCategory::Logical, std::max(kind(), that.kind())};
     default:
       CRASH_NO_CASE;
     }


        


More information about the flang-commits mailing list