diff --git a/flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp b/flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp index 41383fb8af9c1..9bf10b53108c0 100644 --- a/flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp +++ b/flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp @@ -353,6 +353,14 @@ getBaseRef(mlir::TypedValue varPtr) { // calculation op. mlir::Value baseRef = llvm::TypeSwitch(op) + .Case([&](auto op) { + // If this declare binds a view with an underlying storage operand, + // treat that storage as the base reference. Otherwise, fall back + // to the declared memref. + if (auto storage = op.getStorage()) + return storage; + return mlir::Value(varPtr); + }) .Case([&](auto op) { // Get the base object. return op.getMemref(); diff --git a/flang/test/Fir/OpenACC/openacc-type-categories-declare-storage.mlir b/flang/test/Fir/OpenACC/openacc-type-categories-declare-storage.mlir new file mode 100644 index 0000000000000..fabfe4caf3924 --- /dev/null +++ b/flang/test/Fir/OpenACC/openacc-type-categories-declare-storage.mlir @@ -0,0 +1,24 @@ +// Use --mlir-disable-threading so that the diagnostic printing is serialized. +// RUN: fir-opt %s -pass-pipeline='builtin.module(test-fir-openacc-interfaces)' -split-input-file --mlir-disable-threading 2>&1 | FileCheck %s + +module { + // Build a scalar view via fir.declare with a storage operand into an array of i8 + func.func @_QPdeclare_with_storage_is_nonscalar() { + %c0 = arith.constant 0 : index + %arr = fir.alloca !fir.array<4xi8> + %elem_i8 = fir.coordinate_of %arr, %c0 : (!fir.ref>, index) -> !fir.ref + %elem_f32 = fir.convert %elem_i8 : (!fir.ref) -> !fir.ref + %view = fir.declare %elem_f32 storage(%arr[0]) {uniq_name = "_QFpi"} + : (!fir.ref, !fir.ref>) -> !fir.ref + // Force interface query through an acc op that prints type category + %cp = acc.copyin varPtr(%view : !fir.ref) -> !fir.ref {name = "pi", structured = false} + acc.enter_data dataOperands(%cp : !fir.ref) + return + } + + // CHECK: Visiting: %{{.*}} = acc.copyin varPtr(%{{.*}} : !fir.ref) -> !fir.ref {name = "pi", structured = false} + // CHECK: Pointer-like and Mappable: !fir.ref + // CHECK: Type category: array +} + +