Skip to content

Commit 7d9c38a

Browse files
authored
[flang][NFC] Remove hardcoded attr name for fir.global op (#81347)
These hardcoded attribute name are a leftover from the upstreaming period when there was no way to get the attribute name without an instance of the operation. It is since possible to do without them and they should be removed to avoid duplication. This PR cleanup the fir.global op of these hardcoded attribute name and use their generated getters. Some other PRs will follow to cleanup other operations.
1 parent 6a3a5ca commit 7d9c38a

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

flang/include/flang/Optimizer/Dialect/FIROps.td

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,17 +2769,6 @@ def fir_GlobalOp : fir_Op<"global", [IsolatedFromAbove, Symbol]> {
27692769
];
27702770

27712771
let extraClassDeclaration = [{
2772-
static constexpr llvm::StringRef getSymbolAttrNameStr() { return "symref"; }
2773-
static constexpr llvm::StringRef getConstantAttrNameStr() {
2774-
return "constant";
2775-
}
2776-
static constexpr llvm::StringRef getLinkageAttrNameStr() {
2777-
return "linkName";
2778-
}
2779-
static constexpr llvm::StringRef getTargetAttrNameStr() {
2780-
return "target";
2781-
}
2782-
27832772
/// The semantic type of the global
27842773
mlir::Type resultType();
27852774

flang/lib/Optimizer/Analysis/AliasAnalysis.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,10 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v) {
333333
else
334334
type = SourceKind::Global;
335335

336-
if (fir::valueHasFirAttribute(v,
337-
fir::GlobalOp::getTargetAttrNameStr()))
336+
auto globalOpName = mlir::OperationName(
337+
fir::GlobalOp::getOperationName(), defOp->getContext());
338+
if (fir::valueHasFirAttribute(
339+
v, fir::GlobalOp::getTargetAttrName(globalOpName)))
338340
attributes.set(Attribute::Target);
339341

340342
// TODO: Take followBoxAddr into account when setting the pointer

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,12 +1325,14 @@ mlir::ParseResult fir::GlobalOp::parse(mlir::OpAsmParser &parser,
13251325
if (fir::GlobalOp::verifyValidLinkage(linkage))
13261326
return mlir::failure();
13271327
mlir::StringAttr linkAttr = builder.getStringAttr(linkage);
1328-
result.addAttribute(fir::GlobalOp::getLinkageAttrNameStr(), linkAttr);
1328+
result.addAttribute(fir::GlobalOp::getLinkNameAttrName(result.name),
1329+
linkAttr);
13291330
}
13301331

13311332
// Parse the name as a symbol reference attribute.
13321333
mlir::SymbolRefAttr nameAttr;
1333-
if (parser.parseAttribute(nameAttr, fir::GlobalOp::getSymbolAttrNameStr(),
1334+
if (parser.parseAttribute(nameAttr,
1335+
fir::GlobalOp::getSymrefAttrName(result.name),
13341336
result.attributes))
13351337
return mlir::failure();
13361338
result.addAttribute(mlir::SymbolTable::getSymbolAttrName(),
@@ -1339,7 +1341,8 @@ mlir::ParseResult fir::GlobalOp::parse(mlir::OpAsmParser &parser,
13391341
bool simpleInitializer = false;
13401342
if (mlir::succeeded(parser.parseOptionalLParen())) {
13411343
mlir::Attribute attr;
1342-
if (parser.parseAttribute(attr, "initVal", result.attributes) ||
1344+
if (parser.parseAttribute(attr, getInitValAttrName(result.name),
1345+
result.attributes) ||
13431346
parser.parseRParen())
13441347
return mlir::failure();
13451348
simpleInitializer = true;
@@ -1348,13 +1351,15 @@ mlir::ParseResult fir::GlobalOp::parse(mlir::OpAsmParser &parser,
13481351
if (parser.parseOptionalAttrDict(result.attributes))
13491352
return mlir::failure();
13501353

1351-
if (succeeded(parser.parseOptionalKeyword(getConstantAttrNameStr()))) {
1354+
if (succeeded(
1355+
parser.parseOptionalKeyword(getConstantAttrName(result.name)))) {
13521356
// if "constant" keyword then mark this as a constant, not a variable
1353-
result.addAttribute(getConstantAttrNameStr(), builder.getUnitAttr());
1357+
result.addAttribute(getConstantAttrName(result.name),
1358+
builder.getUnitAttr());
13541359
}
13551360

1356-
if (succeeded(parser.parseOptionalKeyword(getTargetAttrNameStr())))
1357-
result.addAttribute(getTargetAttrNameStr(), builder.getUnitAttr());
1361+
if (succeeded(parser.parseOptionalKeyword(getTargetAttrName(result.name))))
1362+
result.addAttribute(getTargetAttrName(result.name), builder.getUnitAttr());
13581363

13591364
mlir::Type globalType;
13601365
if (parser.parseColonType(globalType))
@@ -1389,9 +1394,9 @@ void fir::GlobalOp::print(mlir::OpAsmPrinter &p) {
13891394
getTargetAttrName(), getLinkNameAttrName(),
13901395
getInitValAttrName()});
13911396
if (getOperation()->getAttr(getConstantAttrName()))
1392-
p << " " << getConstantAttrNameStr();
1397+
p << " " << getConstantAttrName().strref();
13931398
if (getOperation()->getAttr(getTargetAttrName()))
1394-
p << " " << getTargetAttrNameStr();
1399+
p << " " << getTargetAttrName().strref();
13951400
p << " : ";
13961401
p.printType(getType());
13971402
if (hasInitializationBody()) {
@@ -1415,7 +1420,7 @@ void fir::GlobalOp::build(mlir::OpBuilder &builder,
14151420
result.addAttribute(getTypeAttrName(result.name), mlir::TypeAttr::get(type));
14161421
result.addAttribute(mlir::SymbolTable::getSymbolAttrName(),
14171422
builder.getStringAttr(name));
1418-
result.addAttribute(getSymbolAttrNameStr(),
1423+
result.addAttribute(getSymrefAttrName(result.name),
14191424
mlir::SymbolRefAttr::get(builder.getContext(), name));
14201425
if (isConstant)
14211426
result.addAttribute(getConstantAttrName(result.name),
@@ -1425,7 +1430,7 @@ void fir::GlobalOp::build(mlir::OpBuilder &builder,
14251430
if (initialVal)
14261431
result.addAttribute(getInitValAttrName(result.name), initialVal);
14271432
if (linkage)
1428-
result.addAttribute(getLinkageAttrNameStr(), linkage);
1433+
result.addAttribute(getLinkNameAttrName(result.name), linkage);
14291434
result.attributes.append(attrs.begin(), attrs.end());
14301435
}
14311436

flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,8 @@ static bool conflictOnLoad(llvm::ArrayRef<mlir::Operation *> reach,
573573
for (auto *op : reach)
574574
if (auto ld = mlir::dyn_cast<ArrayLoadOp>(op)) {
575575
mlir::Type ldTy = ld.getMemref().getType();
576+
auto globalOpName = mlir::OperationName(fir::GlobalOp::getOperationName(),
577+
ld.getContext());
576578
if (ld.getMemref() == addr) {
577579
if (mutuallyExclusiveSliceRange(ld, st))
578580
continue;
@@ -588,14 +590,17 @@ static bool conflictOnLoad(llvm::ArrayRef<mlir::Operation *> reach,
588590
if (optimize && !hasPointerType(ldTy) &&
589591
!valueMayHaveFirAttributes(
590592
ld.getMemref(),
591-
{getTargetAttrName(), GlobalOp::getTargetAttrNameStr()}))
593+
{getTargetAttrName(),
594+
fir::GlobalOp::getTargetAttrName(globalOpName).strref()}))
592595
continue;
593596

594597
return true;
595598
} else if (hasPointerType(ldTy)) {
596599
if (optimize && !storeHasPointerType &&
597600
!valueMayHaveFirAttributes(
598-
addr, {getTargetAttrName(), GlobalOp::getTargetAttrNameStr()}))
601+
addr,
602+
{getTargetAttrName(),
603+
fir::GlobalOp::getTargetAttrName(globalOpName).strref()}))
599604
continue;
600605

601606
return true;

0 commit comments

Comments
 (0)