[flang-commits] [flang] [flang][PPC] Improve vector type names in expression diagnostics (NFC) (PR #199383)

Vir Patel via flang-commits flang-commits at lists.llvm.org
Mon May 25 08:30:33 PDT 2026


https://github.com/virsworld updated https://github.com/llvm/llvm-project/pull/199383

>From df77a4a1e3a29a9584ea30fbe0e87ff45f9ac4d3 Mon Sep 17 00:00:00 2001
From: virsworld <virpatel at mac.home>
Date: Sat, 23 May 2026 18:24:15 -0400
Subject: [PATCH] [flang] Improve vector type names in expression diagnostics

---
 flang/include/flang/Support/Fortran.h         |  4 ++
 flang/lib/Evaluate/formatting.cpp             | 16 +++++++
 flang/lib/Semantics/expression.cpp            |  3 +-
 flang/lib/Semantics/type.cpp                  | 46 +++----------------
 flang/lib/Support/Fortran.cpp                 | 37 ++++++++++++++-
 .../PowerPC/ppc-vector-diagnostics.f90        | 15 ++++++
 6 files changed, 79 insertions(+), 42 deletions(-)
 create mode 100644 flang/test/Semantics/PowerPC/ppc-vector-diagnostics.f90

diff --git a/flang/include/flang/Support/Fortran.h b/flang/include/flang/Support/Fortran.h
index c71e1164ef3ab..057b0c3da8c47 100644
--- a/flang/include/flang/Support/Fortran.h
+++ b/flang/include/flang/Support/Fortran.h
@@ -103,6 +103,10 @@ bool AreCompatibleCUDADataAttrs(std::optional<CUDADataAttr>,
     bool isHostDeviceProcedure,
     const LanguageFeatureControl *features = nullptr);
 
+// Format vector type as Fortran string
+std::string FormatVectorTypeAsFortran(
+    int category, int64_t elementCategory, int64_t elementKind);
+
 static constexpr char blankCommonObjectName[] = "__BLNK__";
 
 // Get the assembly name for a non BIND(C) external symbol other than the blank
diff --git a/flang/lib/Evaluate/formatting.cpp b/flang/lib/Evaluate/formatting.cpp
index f6595baee260d..d6a72a5e246ba 100644
--- a/flang/lib/Evaluate/formatting.cpp
+++ b/flang/lib/Evaluate/formatting.cpp
@@ -676,6 +676,21 @@ llvm::raw_ostream &StructureConstructor::AsFortran(llvm::raw_ostream &o) const {
 std::string DynamicType::AsFortran() const {
   if (derived_) {
     CHECK(category_ == TypeCategory::Derived);
+    if (derived_->IsVectorType()) {
+      int64_t vecElemKind{0};
+      int64_t vecElemCategory{-1};
+      if (derived_->category() == semantics::DerivedTypeSpec::Category::IntrinsicVector) {
+        for (const auto &pair : derived_->parameters()) {
+          if (pair.first == "element_category") {
+            vecElemCategory = ToInt64(pair.second.GetExplicit()).value_or(-1);
+          } else if (pair.first == "element_kind") {
+            vecElemKind = ToInt64(pair.second.GetExplicit()).value_or(0);
+          }
+        }
+      }
+      return common::FormatVectorTypeAsFortran(
+          static_cast<int>(derived_->category()), vecElemCategory, vecElemKind);
+    }
     std::string result{DerivedTypeSpecAsFortran(*derived_)};
     if (IsPolymorphic()) {
       result = "CLASS("s + result + ')';
@@ -884,6 +899,7 @@ llvm::raw_ostream &Assignment::AsFortran(llvm::raw_ostream &o) const {
   return o;
 }
 
+
 #ifdef _MSC_VER // disable bogus warning about missing definitions
 #pragma warning(disable : 4661)
 #endif
diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index 8ee0613bdfc5f..34810ce0e4c01 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -5538,7 +5538,8 @@ std::string ArgumentAnalyzer::TypeAsFortran(std::size_t i) {
         : type->IsUnlimitedPolymorphic() ? "CLASS(*)"s
         : type->IsPolymorphic()          ? type->AsFortran()
         : type->category() == TypeCategory::Derived
-        ? "TYPE("s + type->AsFortran() + ')'
+        ? (type->GetDerivedTypeSpec().IsVectorType() ? type->AsFortran()
+                                                     : "TYPE("s + type->AsFortran() + ')')
         : type->category() == TypeCategory::Character
         ? "CHARACTER(KIND="s + std::to_string(type->kind()) + ')'
         : ToUpperCase(type->AsFortran());
diff --git a/flang/lib/Semantics/type.cpp b/flang/lib/Semantics/type.cpp
index f2c1637a604a9..cddb83fe6aca6 100644
--- a/flang/lib/Semantics/type.cpp
+++ b/flang/lib/Semantics/type.cpp
@@ -758,15 +758,10 @@ const DeclTypeSpec *CloneDerivedTypeForUseDevice(Scope &containingScope,
 }
 
 std::string DerivedTypeSpec::VectorTypeAsFortran() const {
-  std::string buf;
-  llvm::raw_string_ostream ss{buf};
-
-  switch (category()) {
-    SWITCH_COVERS_ALL_CASES
-  case (Fortran::semantics::DerivedTypeSpec::Category::IntrinsicVector): {
-    int64_t vecElemKind;
-    int64_t vecElemCategory;
+  int64_t vecElemKind{0};
+  int64_t vecElemCategory{-1};
 
+  if (category() == Category::IntrinsicVector) {
     for (const auto &pair : parameters()) {
       if (pair.first == "element_category") {
         vecElemCategory =
@@ -776,39 +771,10 @@ std::string DerivedTypeSpec::VectorTypeAsFortran() const {
             Fortran::evaluate::ToInt64(pair.second.GetExplicit()).value_or(0);
       }
     }
-
-    assert((vecElemCategory >= 0 &&
-               static_cast<size_t>(vecElemCategory) <
-                   Fortran::common::VectorElementCategory_enumSize) &&
-        "Vector element type is not specified");
-    assert(vecElemKind && "Vector element kind is not specified");
-
-    ss << "vector(";
-    switch (static_cast<common::VectorElementCategory>(vecElemCategory)) {
-      SWITCH_COVERS_ALL_CASES
-    case common::VectorElementCategory::Integer:
-      ss << "integer(" << vecElemKind << ")";
-      break;
-    case common::VectorElementCategory::Unsigned:
-      ss << "unsigned(" << vecElemKind << ")";
-      break;
-    case common::VectorElementCategory::Real:
-      ss << "real(" << vecElemKind << ")";
-      break;
-    }
-    ss << ")";
-    break;
   }
-  case (Fortran::semantics::DerivedTypeSpec::Category::PairVector):
-    ss << "__vector_pair";
-    break;
-  case (Fortran::semantics::DerivedTypeSpec::Category::QuadVector):
-    ss << "__vector_quad";
-    break;
-  case (Fortran::semantics::DerivedTypeSpec::Category::DerivedType):
-    Fortran::common::die("Vector element type not implemented");
-  }
-  return buf;
+
+  return common::FormatVectorTypeAsFortran(
+      static_cast<int>(category()), vecElemCategory, vecElemKind);
 }
 
 std::string DerivedTypeSpec::AsFortran() const {
diff --git a/flang/lib/Support/Fortran.cpp b/flang/lib/Support/Fortran.cpp
index 39a3d64a464fc..9c86b94795225 100644
--- a/flang/lib/Support/Fortran.cpp
+++ b/flang/lib/Support/Fortran.cpp
@@ -181,4 +181,39 @@ bool AreCompatibleCUDADataAttrs(std::optional<CUDADataAttr> x,
   }
 }
 
-} // namespace Fortran::common
+std::string FormatVectorTypeAsFortran(
+    int category, int64_t elementCategory, int64_t elementKind) {
+  std::string buf;
+  llvm::raw_string_ostream ss{buf};
+  
+  switch (category) {
+  case 1: { // IntrinsicVector
+    CHECK(elementCategory >= 0 && elementKind > 0);
+    ss << "vector(";
+    switch (static_cast<VectorElementCategory>(elementCategory)) {
+    case VectorElementCategory::Integer:
+      ss << "integer(" << elementKind << ")";
+      break;
+    case VectorElementCategory::Unsigned:
+      ss << "unsigned(" << elementKind << ")";
+      break;
+    case VectorElementCategory::Real:
+      ss << "real(" << elementKind << ")";
+      break;
+    }
+    ss << ")";
+    break;
+  }
+  case 2: // PairVector
+    ss << "__vector_pair";
+    break;
+  case 3: // QuadVector
+    ss << "__vector_quad";
+    break;
+  default:
+    CHECK(false && "Vector element type not implemented");
+  }
+  return buf;
+}
+
+} // namespace Fortran::common
\ No newline at end of file
diff --git a/flang/test/Semantics/PowerPC/ppc-vector-diagnostics.f90 b/flang/test/Semantics/PowerPC/ppc-vector-diagnostics.f90
new file mode 100644
index 0000000000000..636c3a764f055
--- /dev/null
+++ b/flang/test/Semantics/PowerPC/ppc-vector-diagnostics.f90
@@ -0,0 +1,15 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1
+! REQUIRES: target=powerpc{{.*}}
+
+subroutine test_vector_add()
+  vector(integer(4)) :: v1, v2
+  !ERROR: Operands of + must be numeric; have vector(integer(4)) and vector(integer(4))
+  v1 = v1 + v2
+end subroutine
+
+subroutine test_vector_assignment()
+  vector(integer(4)) :: v1
+  vector(real(4)) :: v2
+  !ERROR: No intrinsic or user-defined ASSIGNMENT(=) matches operand types vector(integer(4)) and vector(real(4))
+  v1 = v2
+end subroutine



More information about the flang-commits mailing list