Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang][runtime] Handle type code synonyms in CFI_... runtime #66231

Merged
merged 1 commit into from
Sep 13, 2023

Conversation

klausler
Copy link
Contributor

Some CFI_type_... type codes are synonyms; ensure that they are treated as equivalent when validating inputs to CFI_... runtime routines.

@klausler klausler requested a review from a team as a code owner September 13, 2023 16:30
Some CFI_type_... type codes are synonyms; ensure that they are
treated as equivalent when validating inputs to CFI_... runtime
routines.

Pull request: llvm#66231
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Sep 13, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 13, 2023

@llvm/pr-subscribers-flang-runtime

Changes Some CFI_type_... type codes are synonyms; ensure that they are treated as equivalent when validating inputs to CFI_... runtime routines. -- Full diff: https://github.com//pull/66231.diff

2 Files Affected:

  • (modified) flang/include/flang/Runtime/type-code.h (+12-3)
  • (modified) flang/runtime/ISO_Fortran_binding.cpp (+6-4)
diff --git a/flang/include/flang/Runtime/type-code.h b/flang/include/flang/Runtime/type-code.h
index df38611ab8760a5..fb18dba54980f69 100644
--- a/flang/include/flang/Runtime/type-code.h
+++ b/flang/include/flang/Runtime/type-code.h
@@ -53,10 +53,19 @@ class TypeCode {
   RT_API_ATTRS std::optional<std::pair<TypeCategory, int>>
   GetCategoryAndKind() const;
 
-  RT_API_ATTRS bool operator==(const TypeCode &that) const {
-    return raw_ == that.raw_;
+  RT_API_ATTRS bool operator==(TypeCode that) const {
+    if (raw_ == that.raw_) { // fast path
+      return true;
+    } else {
+      // Multiple raw CFI_type_... codes can represent the same Fortran
+      // type category + kind type parameter, e.g. CFI_type_int and
+      // CFI_type_int32_t.
+      auto thisCK{GetCategoryAndKind()};
+      auto thatCK{that.GetCategoryAndKind()};
+      return thisCK && thatCK && *thisCK == *thatCK;
+    }
   }
-  bool operator!=(const TypeCode &that) const { return raw_ != that.raw_; }
+  bool operator!=(TypeCode that) const { return !(*this == that); }
 
 private:
   ISO::CFI_type_t raw_{CFI_type_other};
diff --git a/flang/runtime/ISO_Fortran_binding.cpp b/flang/runtime/ISO_Fortran_binding.cpp
index 8130875e47360f4..45b4d0ae3f569ab 100644
--- a/flang/runtime/ISO_Fortran_binding.cpp
+++ b/flang/runtime/ISO_Fortran_binding.cpp
@@ -148,9 +148,11 @@ int CFI_section(CFI_cdesc_t *result, const CFI_cdesc_t *source,
   if (IsAssumedSize(source) && !upper_bounds) {
     return CFI_INVALID_DESCRIPTOR;
   }
-  if ((result->type != source->type) ||
-      (result->elem_len != source->elem_len)) {
-    return CFI_INVALID_DESCRIPTOR;
+  if (runtime::TypeCode{result->type} != runtime::TypeCode{source->type}) {
+    return CFI_INVALID_TYPE;
+  }
+  if (source->elem_len != result->elem_len) {
+    return CFI_INVALID_ELEM_LEN;
   }
   if (result->attribute == CFI_attribute_allocatable) {
     return CFI_INVALID_ATTRIBUTE;
@@ -256,7 +258,7 @@ int CFI_setpointer(CFI_cdesc_t *result, const CFI_cdesc_t *source,
   if (source->rank != result->rank) {
     return CFI_INVALID_RANK;
   }
-  if (source->type != result->type) {
+  if (runtime::TypeCode{source->type} != runtime::TypeCode{result->type}) {
     return CFI_INVALID_TYPE;
   }
   if (source->elem_len != result->elem_len) {

Copy link
Contributor

@psteinfeld psteinfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All builds and tests correctly and looks good.

@klausler klausler merged commit f5592f3 into llvm:main Sep 13, 2023
2 checks passed
@klausler klausler deleted the bug1343 branch September 13, 2023 22:25
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
…6231)

Some CFI_type_... type codes are synonyms; ensure that they are treated
as equivalent when validating inputs to CFI_... runtime routines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants