Skip to content

Commit 203f21b

Browse files
[flang][acc] Support fir.convert in type categorization (#160403)
Ensure that casting operations do not prevent mis-categorization. This pattern is seen when variables are casted to be passed by raw pointers. I encountered this problem because Fortran runtime calls take file name by pointer.
1 parent 5c1df39 commit 203f21b

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,14 @@ getBaseRef(mlir::TypedValue<mlir::acc::PointerLikeType> varPtr) {
365365
// object, get the base object.
366366
return op.getRef();
367367
})
368+
.Case<fir::ConvertOp>([&](auto op) -> mlir::Value {
369+
// Strip the conversion and recursively check the operand
370+
if (auto ptrLikeOperand = mlir::dyn_cast_if_present<
371+
mlir::TypedValue<mlir::acc::PointerLikeType>>(
372+
op.getValue()))
373+
return getBaseRef(ptrLikeOperand);
374+
return varPtr;
375+
})
368376
.Default([&](mlir::Operation *) { return varPtr; });
369377

370378
return baseRef;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Use --mlir-disable-threading so that the diagnostic printing is serialized.
2+
// RUN: fir-opt %s -pass-pipeline='builtin.module(test-fir-openacc-interfaces)' -split-input-file --mlir-disable-threading 2>&1 | FileCheck %s
3+
4+
module {
5+
fir.global linkonce @test_string constant : !fir.char<1,26> {
6+
%0 = fir.string_lit "hello_world_test_string\00"(26) : !fir.char<1,26>
7+
fir.has_value %0 : !fir.char<1,26>
8+
}
9+
10+
// Test global constant string with pointer conversion
11+
func.func @_QPtest_global_string_ptr() {
12+
%0 = fir.address_of(@test_string) : !fir.ref<!fir.char<1,26>>
13+
%1 = fir.convert %0 : (!fir.ref<!fir.char<1,26>>) -> !fir.ref<i8>
14+
%2 = acc.copyin varPtr(%1 : !fir.ref<i8>) -> !fir.ref<i8> {name = "test_string", structured = false}
15+
acc.enter_data dataOperands(%2 : !fir.ref<i8>)
16+
return
17+
}
18+
19+
// CHECK: Visiting: %{{.*}} = acc.copyin varPtr(%{{.*}} : !fir.ref<i8>) -> !fir.ref<i8> {name = "test_string", structured = false}
20+
// CHECK: Pointer-like and Mappable: !fir.ref<i8>
21+
// CHECK: Type category: nonscalar
22+
23+
// Test array with pointer conversion
24+
func.func @_QPtest_alloca_array_ptr() {
25+
%c10 = arith.constant 10 : index
26+
%0 = fir.alloca !fir.array<10xf32> {bindc_name = "local_array", uniq_name = "_QFtest_alloca_array_ptrElocal_array"}
27+
%1 = fir.convert %0 : (!fir.ref<!fir.array<10xf32>>) -> !fir.ref<i8>
28+
%2 = acc.copyin varPtr(%1 : !fir.ref<i8>) -> !fir.ref<i8> {name = "local_array", structured = false}
29+
acc.enter_data dataOperands(%2 : !fir.ref<i8>)
30+
return
31+
}
32+
33+
// CHECK: Visiting: %{{.*}} = acc.copyin varPtr(%{{.*}} : !fir.ref<i8>) -> !fir.ref<i8> {name = "local_array", structured = false}
34+
// CHECK: Pointer-like and Mappable: !fir.ref<i8>
35+
// CHECK: Type category: array
36+
}

0 commit comments

Comments
 (0)