Skip to content

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

Open
virsworld wants to merge 1 commit into
llvm:mainfrom
virsworld:vector-op-msgs
Open

[flang][PPC] Improve vector type names in expression diagnostics (NFC)#199383
virsworld wants to merge 1 commit into
llvm:mainfrom
virsworld:vector-op-msgs

Conversation

@virsworld
Copy link
Copy Markdown
Contributor

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:

  • IntrinsicVectorvector(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.

@llvmorg-github-actions llvmorg-github-actions Bot added flang Flang issues not falling into any other category flang:semantics labels May 23, 2026
@llvmorg-github-actions
Copy link
Copy Markdown

@llvm/pr-subscribers-flang-semantics

Author: Vir Patel (virsworld)

Changes

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:

  • IntrinsicVectorvector(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 --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

@virsworld
Copy link
Copy Markdown
Contributor Author

@kparzysz I had trouble implementing a fix using your suggestion from #197821, if you believe this PR doesn't appropriately address the problem, I'd like more details about a better approach.

Copy link
Copy Markdown
Contributor

@kparzysz kparzysz left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.

@kkwli kkwli requested review from cenewcombe and kkwli May 25, 2026 11:29
@kkwli
Copy link
Copy Markdown
Contributor

kkwli commented May 25, 2026

Thanks for reworking. Since FormatVectorType and DerivedTypeSpec::VectorTypeAsFortran in flang/lib/Semantics/type.cpp are very similar, I think it is reasonable to refactor that and consider to put the common code in flang/lib/Support/Fortran.cpp. That may require some adjustment in what is being passed to the method.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 25, 2026

✅ With the latest revision this PR passed the C/C++ code formatter.

@virsworld virsworld marked this pull request as draft May 25, 2026 15:22
@virsworld virsworld force-pushed the vector-op-msgs branch 5 times, most recently from c4c9bbd to fda9ad5 Compare May 25, 2026 15:50
@virsworld
Copy link
Copy Markdown
Contributor Author

Thanks for reworking. Since FormatVectorType and DerivedTypeSpec::VectorTypeAsFortran in flang/lib/Semantics/type.cpp are very similar, I think it is reasonable to refactor that and consider to put the common code in flang/lib/Support/Fortran.cpp. That may require some adjustment in what is being passed to the method.

Refactored to use new function FormatVectorTypeAsFortran from flang/lib/Support/Fortran.cpp in flang/lib/Semantics/type.cpp and flang/lib/Evaluate/formatting.cpp.

@virsworld virsworld marked this pull request as ready for review May 25, 2026 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:semantics flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants