Skip to content

[flang][OpenMP] Fix copyprivate crash with unlimited polymorphic pointer#199768

Open
ceseo wants to merge 1 commit into
llvm:mainfrom
ceseo:llvm-198770
Open

[flang][OpenMP] Fix copyprivate crash with unlimited polymorphic pointer#199768
ceseo wants to merge 1 commit into
llvm:mainfrom
ceseo:llvm-198770

Conversation

@ceseo
Copy link
Copy Markdown
Contributor

@ceseo ceseo commented May 26, 2026

Lowering a copyprivate clause whose list item is an unlimited polymorphic pointer (class(*), pointer) crashed in TypeInfo::typeScan. The scan descends through the fir.class box and the fir.ptr, reaching a none element type, which the terminal assertion did not allow.

Fixes #198770

Lowering a copyprivate clause whose list item is an unlimited polymorphic
pointer (class(*), pointer) crashed in TypeInfo::typeScan. The scan descends
through the fir.class box and the fir.ptr, reaching a `none` element type,
which the terminal assertion did not allow.

Fixes llvm#198770
@llvmorg-github-actions llvmorg-github-actions Bot added flang Flang issues not falling into any other category flang:fir-hlfir flang:openmp labels May 26, 2026
@llvmorg-github-actions
Copy link
Copy Markdown

llvmorg-github-actions Bot commented May 26, 2026

@llvm/pr-subscribers-flang-openmp

@llvm/pr-subscribers-flang-fir-hlfir

Author: Carlos Seo (ceseo)

Changes

Lowering a copyprivate clause whose list item is an unlimited polymorphic pointer (class(*), pointer) crashed in TypeInfo::typeScan. The scan descends through the fir.class box and the fir.ptr, reaching a none element type, which the terminal assertion did not allow.

Fixes #198770


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

2 Files Affected:

  • (modified) flang/lib/Lower/OpenMP/ClauseProcessor.cpp (+5-1)
  • (added) flang/test/Lower/OpenMP/copyprivate6.f90 (+26)
diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
index 213b5f783430e..aa6d2e6753941 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
@@ -1179,9 +1179,13 @@ void TypeInfo::typeScan(mlir::Type ty) {
     typeScan(pty.getEleTy());
   } else {
     // The scan ends when reaching any built-in, record or boxproc type.
+    // A `none` element type is reached for unlimited polymorphic entities
+    // (e.g. `class(*)`), which are always inside a box; the copy is then
+    // performed through the descriptor, so no scalar type info is needed.
     assert(ty.isIntOrIndexOrFloat() || mlir::isa<mlir::ComplexType>(ty) ||
            mlir::isa<fir::LogicalType>(ty) || mlir::isa<fir::RecordType>(ty) ||
-           mlir::isa<fir::BoxProcType>(ty));
+           mlir::isa<fir::BoxProcType>(ty) ||
+           (inBox && mlir::isa<mlir::NoneType>(ty)));
   }
 }
 
diff --git a/flang/test/Lower/OpenMP/copyprivate6.f90 b/flang/test/Lower/OpenMP/copyprivate6.f90
new file mode 100644
index 0000000000000..39b33946b2cee
--- /dev/null
+++ b/flang/test/Lower/OpenMP/copyprivate6.f90
@@ -0,0 +1,26 @@
+! Test lowering of COPYPRIVATE with an unlimited polymorphic pointer.
+! RUN: %flang_fc1 -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s
+
+! Testcase from: https://github.com/llvm/llvm-project/issues/198770
+
+! CHECK-LABEL: func.func private @_copy_ref_class_ptr_none(
+! CHECK-SAME:    %arg0: [[TYPE:!fir.ref<!fir.class<!fir.ptr<none>>>]],
+! CHECK-SAME:    %arg1: [[TYPE]]) attributes {llvm.linkage = #llvm.linkage<internal>} {
+! CHECK:   %[[DST:.*]]:2 = hlfir.declare %arg0 {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_copy_ref_class_ptr_none_dst"}
+! CHECK:   %[[SRC:.*]]:2 = hlfir.declare %arg1 {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_copy_ref_class_ptr_none_src"}
+! CHECK:   %[[LD:.*]] = fir.load %[[SRC]]#0 : [[TYPE]]
+! CHECK:   fir.store %[[LD]] to %[[DST]]#0 : [[TYPE]]
+! CHECK:   return
+! CHECK: }
+
+! CHECK-LABEL: func.func @_QQmain()
+! CHECK: omp.single copyprivate(%{{.*}} -> @_copy_ref_class_ptr_none : !fir.ref<!fir.class<!fir.ptr<none>>>) {
+! CHECK:   omp.terminator
+! CHECK: }
+
+class(*), pointer, save :: aa
+!$omp threadprivate(aa)
+!$omp single
+!$omp end single copyprivate(aa)
+end
+

@ceseo ceseo requested a review from tblah May 26, 2026 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:fir-hlfir flang:openmp flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Flang][OpenMP] Crash with unlimited polymorphic pointers in copyprivate clauses

1 participant