[flang][PPC] Improve vector type names in expression diagnostics (NFC)#199383
[flang][PPC] Improve vector type names in expression diagnostics (NFC)#199383virsworld wants to merge 1 commit into
Conversation
|
@llvm/pr-subscribers-flang-semantics Author: Vir Patel (virsworld) ChangesContinuation of #197821Updated ChangeThis PR implements vector type formatting in The function formats vector types based on their category:
The change only affects vector types; all other types preserve their existing formatting behavior. TestingRan Also successfully built with Full diff: https://github.com/llvm/llvm-project/pull/199383.diff 3 Files Affected:
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
|
|
Thanks for reworking. Since |
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
c4c9bbd to
fda9ad5
Compare
Refactored to use new function |
Continuation of #197821
Updated Change
This PR implements vector type formatting in
DynamicType::AsFortran()informatting.cppusing a static helper functionFormatVectorType().The function formats vector types based on their category:
vector(integer(4)),vector(real(8)), etc.__vector_pair__vector_quadThe change only affects vector types; all other types preserve their existing formatting behavior.
Testing
Ran
ninja check-flangandninja check-flang-newAlso successfully built with
ENABLE_SHARED_LIBS=ONto ensure no link errors.