Skip to content

Commit 4f39a4f

Browse files
[acc][flang] Add getInitRegion() to GlobalVariableOpInterface (#169569)
Some globals (e.g., fir.global) have initialization regions that may transitively reference other globals or type descriptors. Add getInitRegion() to GlobalVariableOpInterface to retrieve these regions, returning Region* (nullptr if the global uses attributes for initialization, as with memref.global).
1 parent 49828c2 commit 4f39a4f

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCOpsInterfaces.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct GlobalVariableModel
6565
: public mlir::acc::GlobalVariableOpInterface::ExternalModel<
6666
GlobalVariableModel, fir::GlobalOp> {
6767
bool isConstant(mlir::Operation *op) const;
68+
mlir::Region *getInitRegion(mlir::Operation *op) const;
6869
};
6970

7071
template <typename Op>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ bool GlobalVariableModel::isConstant(mlir::Operation *op) const {
7171
return globalOp.getConstant().has_value();
7272
}
7373

74+
mlir::Region *GlobalVariableModel::getInitRegion(mlir::Operation *op) const {
75+
auto globalOp = mlir::cast<fir::GlobalOp>(op);
76+
return globalOp.hasInitializationBody() ? &globalOp.getRegion() : nullptr;
77+
}
78+
7479
// Helper to recursively process address-of operations in derived type
7580
// descriptors and collect all needed fir.globals.
7681
static void processAddrOfOpInDerivedTypeDescriptor(

mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def GlobalVariableOpInterface : OpInterface<"GlobalVariableOpInterface"> {
7272
"isConstant", (ins), [{
7373
return false;
7474
}]>,
75+
InterfaceMethod<"Get the initialization region (returns nullptr if none)",
76+
"::mlir::Region*", "getInitRegion", (ins)>,
7577
];
7678
}
7779

mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ struct MemrefGlobalVariableModel
227227
auto globalOp = cast<memref::GlobalOp>(op);
228228
return globalOp.getConstant();
229229
}
230+
231+
Region *getInitRegion(Operation *op) const {
232+
// GlobalOp uses attributes for initialization, not regions
233+
return nullptr;
234+
}
230235
};
231236

232237
/// Helper function for any of the times we need to modify an ArrayAttr based on

mlir/unittests/Dialect/OpenACC/OpenACCOpsInterfacesTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ TEST_F(OpenACCOpsInterfacesTest, GlobalVariableOpInterfaceConstant) {
7575
EXPECT_TRUE(globalVarIface.isConstant());
7676
}
7777

78+
TEST_F(OpenACCOpsInterfacesTest, GlobalVariableOpInterfaceInitRegion) {
79+
// Test that memref::GlobalOp returns nullptr for getInitRegion()
80+
// since it uses attributes for initialization, not regions
81+
82+
auto memrefType = MemRefType::get({10}, builder.getF32Type());
83+
OwningOpRef<memref::GlobalOp> globalOp = memref::GlobalOp::create(
84+
builder, loc,
85+
/*sym_name=*/builder.getStringAttr("test_global"),
86+
/*sym_visibility=*/builder.getStringAttr("private"),
87+
/*type=*/TypeAttr::get(memrefType),
88+
/*initial_value=*/Attribute(),
89+
/*constant=*/UnitAttr(),
90+
/*alignment=*/IntegerAttr());
91+
92+
auto globalVarIface =
93+
dyn_cast<GlobalVariableOpInterface>(globalOp->getOperation());
94+
ASSERT_TRUE(globalVarIface != nullptr);
95+
96+
// memref::GlobalOp doesn't have regions for initialization
97+
EXPECT_EQ(globalVarIface.getInitRegion(), nullptr);
98+
}
99+
78100
//===----------------------------------------------------------------------===//
79101
// AddressOfGlobalOpInterface Tests
80102
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)