Skip to content

Commit 3cfac11

Browse files
author
Razvan Lupusoru
committed
Add constant dimension array test for FIR
1 parent c9e740c commit 3cfac11

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

flang/test/Fir/OpenACC/pointer-like-interface-load.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ func.func @test_load_derived_type() {
6060

6161
// -----
6262

63+
func.func @test_load_constant_array() {
64+
%ptr = fir.alloca !fir.array<10xf32> {test.ptr}
65+
// CHECK: Successfully generated load for operation: %{{.*}} = fir.alloca !fir.array<10xf32> {test.ptr}
66+
// CHECK: Loaded value type: !fir.array<10xf32>
67+
// CHECK: Generated: %{{.*}} = fir.load %{{.*}} : !fir.ref<!fir.array<10xf32>>
68+
return
69+
}
70+
71+
// -----
72+
6373
func.func @test_load_dynamic_array_fails() {
6474
%c10 = arith.constant 10 : index
6575
%ptr = fir.alloca !fir.array<?xf32>, %c10 {test.ptr}

flang/test/Fir/OpenACC/pointer-like-interface-store.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ func.func @test_store_with_type_conversion() {
5050

5151
// -----
5252

53+
func.func @test_store_constant_array() {
54+
%val = fir.undefined !fir.array<10xf32> {test.value}
55+
%ptr = fir.alloca !fir.array<10xf32> {test.ptr}
56+
// CHECK: Successfully generated store for operation: %{{.*}} = fir.alloca !fir.array<10xf32> {test.ptr}
57+
// CHECK: Generated: fir.store %{{.*}} to %{{.*}} : !fir.ref<!fir.array<10xf32>>
58+
return
59+
}
60+
61+
// -----
62+
5363
func.func @test_store_dynamic_array_fails() {
5464
%c10 = arith.constant 10 : index
5565
%ptr = fir.alloca !fir.array<?xf32>, %c10 {test.ptr}

mlir/test/lib/Dialect/OpenACC/TestPointerLikeTypeInterface.cpp

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct TestPointerLikeTypeInterfacePass
7878
void testGenLoad(Operation *op, Value result, PointerLikeType pointerType,
7979
OpBuilder &builder);
8080
void testGenStore(Operation *op, Value result, PointerLikeType pointerType,
81-
OpBuilder &builder);
81+
OpBuilder &builder, Value providedValue = {});
8282

8383
struct PointerCandidate {
8484
Operation *op;
@@ -100,6 +100,8 @@ void TestPointerLikeTypeInterfacePass::runOnOperation() {
100100
testMode == "store") {
101101
// Collect all candidates first
102102
SmallVector<PointerCandidate> candidates;
103+
// For store mode, also look for a test value to use
104+
Value testValue;
103105
func.walk([&](Operation *op) {
104106
if (op->hasAttr("test.ptr")) {
105107
for (auto result : op->getResults()) {
@@ -110,6 +112,11 @@ void TestPointerLikeTypeInterfacePass::runOnOperation() {
110112
}
111113
}
112114
}
115+
// Collect value marked with test.value for store tests
116+
if (testMode == "store" && op->hasAttr("test.value")) {
117+
if (op->getNumResults() > 0)
118+
testValue = op->getResult(0);
119+
}
113120
});
114121

115122
// Now test all candidates
@@ -125,7 +132,7 @@ void TestPointerLikeTypeInterfacePass::runOnOperation() {
125132
builder);
126133
else if (testMode == "store")
127134
testGenStore(candidate.op, candidate.result, candidate.pointerType,
128-
builder);
135+
builder, testValue);
129136
}
130137
} else if (testMode == "copy") {
131138
// Collect all source and destination candidates
@@ -341,7 +348,8 @@ void TestPointerLikeTypeInterfacePass::testGenLoad(Operation *op, Value result,
341348

342349
void TestPointerLikeTypeInterfacePass::testGenStore(Operation *op, Value result,
343350
PointerLikeType pointerType,
344-
OpBuilder &builder) {
351+
OpBuilder &builder,
352+
Value providedValue) {
345353
Location loc = op->getLoc();
346354

347355
// Create a new builder with the listener and set insertion point
@@ -350,28 +358,32 @@ void TestPointerLikeTypeInterfacePass::testGenStore(Operation *op, Value result,
350358
newBuilder.setListener(&tracker);
351359
newBuilder.setInsertionPointAfter(op);
352360

353-
// Create a test value to store - use a constant matching the element type
354-
Type elementType = pointerType.getElementType();
355-
if (!elementType) {
356-
llvm::errs() << "Failed to generate store for operation: ";
357-
op->print(llvm::errs());
358-
llvm::errs() << "\n";
359-
return;
360-
}
361+
// Use provided value if available, otherwise create a constant
362+
Value valueToStore = providedValue;
363+
if (!valueToStore) {
364+
// Create a test value to store - use a constant matching the element type
365+
Type elementType = pointerType.getElementType();
366+
if (!elementType) {
367+
llvm::errs() << "Failed to generate store for operation: ";
368+
op->print(llvm::errs());
369+
llvm::errs() << "\n";
370+
return;
371+
}
361372

362-
Value valueToStore;
363-
if (elementType.isIntOrIndex()) {
364-
auto attr = newBuilder.getIntegerAttr(elementType, 42);
365-
valueToStore =
366-
arith::ConstantOp::create(newBuilder, loc, elementType, attr);
367-
} else if (auto floatType = dyn_cast<FloatType>(elementType)) {
368-
auto attr = newBuilder.getFloatAttr(floatType, 42.0);
369-
valueToStore = arith::ConstantOp::create(newBuilder, loc, floatType, attr);
370-
} else {
371-
llvm::errs() << "Failed to generate store for operation: ";
372-
op->print(llvm::errs());
373-
llvm::errs() << "\n";
374-
return;
373+
if (elementType.isIntOrIndex()) {
374+
auto attr = newBuilder.getIntegerAttr(elementType, 42);
375+
valueToStore =
376+
arith::ConstantOp::create(newBuilder, loc, elementType, attr);
377+
} else if (auto floatType = dyn_cast<FloatType>(elementType)) {
378+
auto attr = newBuilder.getFloatAttr(floatType, 42.0);
379+
valueToStore =
380+
arith::ConstantOp::create(newBuilder, loc, floatType, attr);
381+
} else {
382+
llvm::errs() << "Failed to generate store for operation: ";
383+
op->print(llvm::errs());
384+
llvm::errs() << "\n";
385+
return;
386+
}
375387
}
376388

377389
// Call the genStore API

0 commit comments

Comments
 (0)