[flang-commits] [flang] [flang][openacc] Avoid crash when variable is not declared in reduction (PR #114603)

via flang-commits flang-commits at lists.llvm.org
Fri Nov 1 13:52:43 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Valentin Clement (バレンタイン クレメン) (clementval)

<details>
<summary>Changes</summary>

Do not check redutction type when the variable has not been declared. Correct error is then emitted. 

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


2 Files Affected:

- (modified) flang/lib/Semantics/check-acc-structure.cpp (+21-19) 
- (modified) flang/test/Semantics/OpenACC/acc-reduction-validity.f90 (+5) 


``````````diff
diff --git a/flang/lib/Semantics/check-acc-structure.cpp b/flang/lib/Semantics/check-acc-structure.cpp
index 623e31341a7c20..7afc285e7b9ae3 100644
--- a/flang/lib/Semantics/check-acc-structure.cpp
+++ b/flang/lib/Semantics/check-acc-structure.cpp
@@ -674,26 +674,28 @@ void AccStructureChecker::Enter(const parser::AccClause::Reduction &reduction) {
         common::visitors{
             [&](const parser::Designator &designator) {
               if (const auto *name = getDesignatorNameIfDataRef(designator)) {
-                const auto *type{name->symbol->GetType()};
-                if (type->IsNumeric(TypeCategory::Integer) &&
-                    !reductionIntegerSet.test(op.v)) {
-                  context_.Say(GetContext().clauseSource,
-                      "reduction operator not supported for integer type"_err_en_US);
-                } else if (type->IsNumeric(TypeCategory::Real) &&
-                    !reductionRealSet.test(op.v)) {
-                  context_.Say(GetContext().clauseSource,
-                      "reduction operator not supported for real type"_err_en_US);
-                } else if (type->IsNumeric(TypeCategory::Complex) &&
-                    !reductionComplexSet.test(op.v)) {
-                  context_.Say(GetContext().clauseSource,
-                      "reduction operator not supported for complex type"_err_en_US);
-                } else if (type->category() ==
-                        Fortran::semantics::DeclTypeSpec::Category::Logical &&
-                    !reductionLogicalSet.test(op.v)) {
-                  context_.Say(GetContext().clauseSource,
-                      "reduction operator not supported for logical type"_err_en_US);
+                if (name->symbol) {
+                  const auto *type{name->symbol->GetType()};
+                  if (type->IsNumeric(TypeCategory::Integer) &&
+                      !reductionIntegerSet.test(op.v)) {
+                    context_.Say(GetContext().clauseSource,
+                        "reduction operator not supported for integer type"_err_en_US);
+                  } else if (type->IsNumeric(TypeCategory::Real) &&
+                      !reductionRealSet.test(op.v)) {
+                    context_.Say(GetContext().clauseSource,
+                        "reduction operator not supported for real type"_err_en_US);
+                  } else if (type->IsNumeric(TypeCategory::Complex) &&
+                      !reductionComplexSet.test(op.v)) {
+                    context_.Say(GetContext().clauseSource,
+                        "reduction operator not supported for complex type"_err_en_US);
+                  } else if (type->category() ==
+                          Fortran::semantics::DeclTypeSpec::Category::Logical &&
+                      !reductionLogicalSet.test(op.v)) {
+                    context_.Say(GetContext().clauseSource,
+                        "reduction operator not supported for logical type"_err_en_US);
+                  }
+                  // TODO: check composite type.
                 }
-                // TODO: check composite type.
               }
             },
             [&](const Fortran::parser::Name &name) {
diff --git a/flang/test/Semantics/OpenACC/acc-reduction-validity.f90 b/flang/test/Semantics/OpenACC/acc-reduction-validity.f90
index ccd009bffa2e24..cecc7e0f507b25 100644
--- a/flang/test/Semantics/OpenACC/acc-reduction-validity.f90
+++ b/flang/test/Semantics/OpenACC/acc-reduction-validity.f90
@@ -3,6 +3,7 @@
 ! Check OpenACC reduction validity.
 
 program openacc_reduction_validity
+  implicit none
 
   integer :: i
   real :: r
@@ -168,5 +169,9 @@ program openacc_reduction_validity
   !$acc parallel reduction(ieor:l)
   !$acc end parallel
 
+  !ERROR: No explicit type declared for 'xyz'
+  !$acc parallel reduction(+:xyz)
+  !$acc end parallel  
+
 
 end program

``````````

</details>


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


More information about the flang-commits mailing list