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

via flang-commits flang-commits at lists.llvm.org
Sat May 23 16:19:54 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Vir Patel (virsworld)

<details>
<summary>Changes</summary>

Continuation of #<!-- -->197821 
---

## Updated Change
This PR implements vector type formatting in `DynamicType::AsFortran()` in `formatting.cpp` using a static helper function `FormatVectorType()`.

The function formats vector types based on their category:

- **IntrinsicVector** → `vector(integer(4))`, `vector(real(8))`, etc.
- **PairVector** → `__vector_pair`
- **QuadVector** → `__vector_quad`

The change only affects vector types; all other types preserve their existing formatting behavior.

---

## Testing
Ran ``ninja check-flang`` and ``ninja check-flang-new``

Also successfully built with ``ENABLE_SHARED_LIBS=ON`` to ensure no link errors.


---
Full diff: https://github.com/llvm/llvm-project/pull/199383.diff


3 Files Affected:

- (modified) flang/lib/Evaluate/formatting.cpp (+48) 
- (modified) flang/lib/Semantics/expression.cpp (+3-1) 
- (added) flang/test/Semantics/PowerPC/ppc-vector-diagnostics.f90 (+15) 


``````````diff
diff --git a/flang/lib/Evaluate/formatting.cpp b/flang/lib/Evaluate/formatting.cpp
index f6595baee260d..5955e87914fc9 100644
--- a/flang/lib/Evaluate/formatting.cpp
+++ b/flang/lib/Evaluate/formatting.cpp
@@ -20,6 +20,9 @@
 
 namespace Fortran::evaluate {
 
+// Forward declaration for static helper function
+static std::string FormatVectorType(const semantics::DerivedTypeSpec &);
+
 // Constant arrays can have non-default lower bounds, but this can't be
 // expressed in Fortran syntax directly, only implied through the use of
 // named constant (PARAMETER) definitions.  For debugging, setting this flag
@@ -676,6 +679,9 @@ llvm::raw_ostream &StructureConstructor::AsFortran(llvm::raw_ostream &o) const {
 std::string DynamicType::AsFortran() const {
   if (derived_) {
     CHECK(category_ == TypeCategory::Derived);
+    if (derived_->IsVectorType()) {
+      return FormatVectorType(*derived_);
+    }
     std::string result{DerivedTypeSpecAsFortran(*derived_)};
     if (IsPolymorphic()) {
       result = "CLASS("s + result + ')';
@@ -884,6 +890,48 @@ llvm::raw_ostream &Assignment::AsFortran(llvm::raw_ostream &o) const {
   return o;
 }
 
+static std::string FormatVectorType(const semantics::DerivedTypeSpec &derived) {
+  std::string buf;
+  llvm::raw_string_ostream ss{buf};
+  switch (derived.category()) {
+  case semantics::DerivedTypeSpec::Category::IntrinsicVector: {
+    int64_t vecElemKind{0};
+    int64_t vecElemCategory{-1};
+    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);
+      }
+    }
+    CHECK(vecElemCategory >= 0 && vecElemKind > 0);
+    ss << "vector(";
+    switch (static_cast<common::VectorElementCategory>(vecElemCategory)) {
+    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 semantics::DerivedTypeSpec::Category::PairVector:
+    ss << "__vector_pair";
+    break;
+  case semantics::DerivedTypeSpec::Category::QuadVector:
+    ss << "__vector_quad";
+    break;
+  case semantics::DerivedTypeSpec::Category::DerivedType:
+    CHECK(false && "Vector element type not implemented");
+  }
+  return buf;
+}
+
 #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..ceca9436e2672 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -5538,7 +5538,9 @@ 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/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

``````````

</details>


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


More information about the flang-commits mailing list