Skip to content

Commit 83552a2

Browse files
authored
[NFC] Move big builders to cpp file
1 parent ae9218c commit 83552a2

File tree

2 files changed

+101
-72
lines changed

2 files changed

+101
-72
lines changed

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

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -743,34 +743,7 @@ def fir_SelectTypeOp : fir_SwitchTerminatorOp<"select_type"> {
743743
"llvm::ArrayRef<mlir::Attribute>":$typeOperands,
744744
"llvm::ArrayRef<mlir::Block *>":$destinations,
745745
CArg<"llvm::ArrayRef<mlir::ValueRange>", "{}">:$destOperands,
746-
CArg<"llvm::ArrayRef<mlir::NamedAttribute>", "{}">:$attributes),
747-
[{
748-
$_state.addOperands(selector);
749-
$_state.addAttribute(getCasesAttr(),
750-
$_builder.getArrayAttr(typeOperands));
751-
const auto count = destinations.size();
752-
for (auto d : destinations)
753-
$_state.addSuccessors(d);
754-
const auto opCount = destOperands.size();
755-
llvm::SmallVector<int32_t> argOffs;
756-
int32_t sumArgs = 0;
757-
for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) {
758-
if (i < opCount) {
759-
$_state.addOperands(destOperands[i]);
760-
const auto argSz = destOperands[i].size();
761-
argOffs.push_back(argSz);
762-
sumArgs += argSz;
763-
} else {
764-
argOffs.push_back(0);
765-
}
766-
}
767-
$_state.addAttribute(getOperandSegmentSizeAttr(),
768-
$_builder.getI32VectorAttr({1, 0, sumArgs}));
769-
$_state.addAttribute(getTargetOffsetAttr(),
770-
$_builder.getI32VectorAttr(argOffs));
771-
$_state.addAttributes(attributes);
772-
}]
773-
>];
746+
CArg<"llvm::ArrayRef<mlir::NamedAttribute>", "{}">:$attributes)>];
774747

775748
let parser = "return parseSelectType(parser, result);";
776749

@@ -1695,14 +1668,7 @@ def fir_FieldIndexOp : fir_OneResultOp<"field_index", [NoSideEffect]> {
16951668
let printer = "::print(p, *this);";
16961669

16971670
let builders = [OpBuilder<(ins "llvm::StringRef":$fieldName,
1698-
"mlir::Type":$recTy, CArg<"mlir::ValueRange","{}">:$operands),
1699-
[{
1700-
$_state.addAttribute(fieldAttrName(),
1701-
$_builder.getStringAttr(fieldName));
1702-
$_state.addAttribute(typeAttrName(), TypeAttr::get(recTy));
1703-
$_state.addOperands(operands);
1704-
}]
1705-
>];
1671+
"mlir::Type":$recTy, CArg<"mlir::ValueRange","{}">:$operands)>];
17061672

17071673
let extraClassDeclaration = [{
17081674
static constexpr llvm::StringRef fieldAttrName() { return "field_id"; }
@@ -2285,21 +2251,10 @@ def fir_CallOp : fir_Op<"call", [CallOpInterface]> {
22852251

22862252
let builders = [
22872253
OpBuilder<(ins "mlir::FuncOp":$callee,
2288-
CArg<"mlir::ValueRange", "{}">:$operands),
2289-
[{
2290-
$_state.addOperands(operands);
2291-
$_state.addAttribute(calleeAttrName(),
2292-
$_builder.getSymbolRefAttr(callee));
2293-
$_state.addTypes(callee.getType().getResults());
2294-
}]>,
2254+
CArg<"mlir::ValueRange", "{}">:$operands)>,
22952255
OpBuilder<(ins "mlir::SymbolRefAttr":$callee,
22962256
"llvm::ArrayRef<mlir::Type>":$results,
2297-
CArg<"mlir::ValueRange", "{}">:$operands),
2298-
[{
2299-
$_state.addOperands(operands);
2300-
$_state.addAttribute(calleeAttrName(), callee);
2301-
$_state.addTypes(results);
2302-
}]>,
2257+
CArg<"mlir::ValueRange", "{}">:$operands)>,
23032258
OpBuilder<(ins "llvm::StringRef":$callee,
23042259
"llvm::ArrayRef<mlir::Type>":$results,
23052260
CArg<"mlir::ValueRange", "{}">:$operands),

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 97 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,22 @@ static mlir::ParseResult parseCallOp(mlir::OpAsmParser &parser,
566566
return mlir::success();
567567
}
568568

569+
void fir::CallOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
570+
mlir::FuncOp callee, mlir::ValueRange operands) {
571+
result.addOperands(operands);
572+
result.addAttribute(calleeAttrName(), builder.getSymbolRefAttr(callee));
573+
result.addTypes(callee.getType().getResults());
574+
}
575+
576+
void fir::CallOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
577+
mlir::SymbolRefAttr callee,
578+
llvm::ArrayRef<mlir::Type> results,
579+
mlir::ValueRange operands) {
580+
result.addOperands(operands);
581+
result.addAttribute(calleeAttrName(), callee);
582+
result.addTypes(results);
583+
}
584+
569585
//===----------------------------------------------------------------------===//
570586
// CharConvertOp
571587
//===----------------------------------------------------------------------===//
@@ -681,11 +697,15 @@ static mlir::ParseResult parseConstcOp(mlir::OpAsmParser &parser,
681697

682698
static void print(mlir::OpAsmPrinter &p, fir::ConstcOp &op) {
683699
p << op.getOperationName() << " (0x";
684-
auto f1 = op.getOperation()->getAttr(fir::ConstcOp::realAttrName()).cast<mlir::FloatAttr>();
700+
auto f1 = op.getOperation()
701+
->getAttr(fir::ConstcOp::realAttrName())
702+
.cast<mlir::FloatAttr>();
685703
auto i1 = f1.getValue().bitcastToAPInt();
686704
p.getStream().write_hex(i1.getZExtValue());
687705
p << ", 0x";
688-
auto f2 = op.getOperation()->getAttr(fir::ConstcOp::imagAttrName()).cast<mlir::FloatAttr>();
706+
auto f2 = op.getOperation()
707+
->getAttr(fir::ConstcOp::imagAttrName())
708+
.cast<mlir::FloatAttr>();
689709
auto i2 = f2.getValue().bitcastToAPInt();
690710
p.getStream().write_hex(i2.getZExtValue());
691711
p << ") : ";
@@ -905,14 +925,16 @@ static mlir::ParseResult parseDispatchTableOp(mlir::OpAsmParser &parser,
905925
}
906926

907927
static void print(mlir::OpAsmPrinter &p, fir::DispatchTableOp &op) {
908-
auto tableName = op.getOperation()->getAttrOfType<StringAttr>(
909-
mlir::SymbolTable::getSymbolAttrName()).getValue();
928+
auto tableName =
929+
op.getOperation()
930+
->getAttrOfType<StringAttr>(mlir::SymbolTable::getSymbolAttrName())
931+
.getValue();
910932
p << op.getOperationName() << " @" << tableName;
911933

912934
Region &body = op.getOperation()->getRegion(0);
913935
if (!body.empty())
914936
p.printRegion(body, /*printEntryBlockArgs=*/false,
915-
/*printBlockTerminators=*/false);
937+
/*printBlockTerminators=*/false);
916938
}
917939

918940
static mlir::LogicalResult verify(fir::DispatchTableOp &op) {
@@ -1132,16 +1154,17 @@ static void print(mlir::OpAsmPrinter &p, fir::GlobalOp &op) {
11321154
if (op.linkName().hasValue())
11331155
p << ' ' << op.linkName().getValue();
11341156
p << ' ';
1135-
p.printAttributeWithoutType(op.getOperation()->getAttr(fir::GlobalOp::symbolAttrName()));
1157+
p.printAttributeWithoutType(
1158+
op.getOperation()->getAttr(fir::GlobalOp::symbolAttrName()));
11361159
if (auto val = op.getValueOrNull())
11371160
p << '(' << val << ')';
11381161
if (op.getOperation()->getAttr(fir::GlobalOp::constantAttrName()))
11391162
p << " constant";
11401163
p << " : ";
11411164
p.printType(op.getType());
11421165
if (op.hasInitializationBody())
1143-
p.printRegion(op.getOperation()->getRegion(0),
1144-
/*printEntryBlockArgs=*/false,
1166+
p.printRegion(op.getOperation()->getRegion(0),
1167+
/*printEntryBlockArgs=*/false,
11451168
/*printBlockTerminators=*/true);
11461169
}
11471170

@@ -1332,6 +1355,15 @@ static void print(mlir::OpAsmPrinter &p, fir::FieldIndexOp &op) {
13321355
}
13331356
}
13341357

1358+
void fir::FieldIndexOp::build(mlir::OpBuilder &builder,
1359+
mlir::OperationState &result,
1360+
llvm::StringRef fieldName, mlir::Type recTy,
1361+
mlir::ValueRange operands) {
1362+
result.addAttribute(fieldAttrName(), builder.getStringAttr(fieldName));
1363+
result.addAttribute(typeAttrName(), TypeAttr::get(recTy));
1364+
result.addOperands(operands);
1365+
}
1366+
13351367
//===----------------------------------------------------------------------===//
13361368
// InsertOnRangeOp
13371369
//===----------------------------------------------------------------------===//
@@ -1987,17 +2019,18 @@ static mlir::ParseResult parseDTEntryOp(mlir::OpAsmParser &parser,
19872019
return mlir::failure();
19882020
} else {
19892021
result.addAttribute(fir::DTEntryOp::methodAttrName(),
1990-
parser.getBuilder().getStringAttr(methodName));
2022+
parser.getBuilder().getStringAttr(methodName));
19912023
}
19922024
mlir::SymbolRefAttr calleeAttr;
19932025
if (parser.parseComma() ||
1994-
parser.parseAttribute(calleeAttr, fir::DTEntryOp::procAttrName(), result.attributes))
2026+
parser.parseAttribute(calleeAttr, fir::DTEntryOp::procAttrName(),
2027+
result.attributes))
19952028
return mlir::failure();
19962029
return mlir::success();
19972030
}
19982031

19992032
static void print(mlir::OpAsmPrinter &p, fir::DTEntryOp &op) {
2000-
p << op.getOperationName() << ' '
2033+
p << op.getOperationName() << ' '
20012034
<< op.getOperation()->getAttr(fir::DTEntryOp::methodAttrName()) << ", "
20022035
<< op.getOperation()->getAttr(fir::DTEntryOp::procAttrName());
20032036
}
@@ -2347,7 +2380,9 @@ static void print(mlir::OpAsmPrinter &p, fir::SelectCaseOp &op) {
23472380
p << op.getOperationName() << ' ';
23482381
p.printOperand(op.getSelector());
23492382
p << " : " << op.getSelector().getType() << " [";
2350-
auto cases = op.getOperation()->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr()).getValue();
2383+
auto cases = op.getOperation()
2384+
->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr())
2385+
.getValue();
23512386
auto count = op.getNumConditions();
23522387
for (decltype(count) i = 0; i != count; ++i) {
23532388
if (i)
@@ -2365,8 +2400,10 @@ static void print(mlir::OpAsmPrinter &p, fir::SelectCaseOp &op) {
23652400
op.printSuccessorAtIndex(p, i);
23662401
}
23672402
p << ']';
2368-
p.printOptionalAttrDict(op.getOperation()->getAttrs(), {op.getCasesAttr(), getCompareOffsetAttr(),
2369-
getTargetOffsetAttr(), op.getOperandSegmentSizeAttr()});
2403+
p.printOptionalAttrDict(op.getOperation()->getAttrs(),
2404+
{op.getCasesAttr(), getCompareOffsetAttr(),
2405+
getTargetOffsetAttr(),
2406+
op.getOperandSegmentSizeAttr()});
23702407
}
23712408

23722409
unsigned fir::SelectCaseOp::compareOffsetSize() {
@@ -2464,7 +2501,9 @@ static mlir::LogicalResult verify(fir::SelectCaseOp &op) {
24642501
op.getSelector().getType().isa<fir::LogicalType>() ||
24652502
op.getSelector().getType().isa<fir::CharacterType>()))
24662503
return op.emitOpError("must be an integer, character, or logical");
2467-
auto cases = op.getOperation()->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr()).getValue();
2504+
auto cases = op.getOperation()
2505+
->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr())
2506+
.getValue();
24682507
auto count = op.getNumDest();
24692508
if (count == 0)
24702509
return op.emitOpError("must have at least one successor");
@@ -2477,10 +2516,8 @@ static mlir::LogicalResult verify(fir::SelectCaseOp &op) {
24772516
for (decltype(count) i = 0; i != count; ++i) {
24782517
auto &attr = cases[i];
24792518
if (!(attr.isa<fir::PointIntervalAttr>() ||
2480-
attr.isa<fir::LowerBoundAttr>() ||
2481-
attr.isa<fir::UpperBoundAttr>() ||
2482-
attr.isa<fir::ClosedIntervalAttr>() ||
2483-
attr.isa<mlir::UnitAttr>()))
2519+
attr.isa<fir::LowerBoundAttr>() || attr.isa<fir::UpperBoundAttr>() ||
2520+
attr.isa<fir::ClosedIntervalAttr>() || attr.isa<mlir::UnitAttr>()))
24842521
return op.emitOpError("incorrect select case attribute type");
24852522
}
24862523
return mlir::success();
@@ -2605,7 +2642,9 @@ static void print(mlir::OpAsmPrinter &p, fir::SelectTypeOp &op) {
26052642
p << op.getOperationName() << ' ';
26062643
p.printOperand(op.getSelector());
26072644
p << " : " << op.getSelector().getType() << " [";
2608-
auto cases = op.getOperation()->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr()).getValue();
2645+
auto cases = op.getOperation()
2646+
->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr())
2647+
.getValue();
26092648
auto count = op.getNumConditions();
26102649
for (decltype(count) i = 0; i != count; ++i) {
26112650
if (i)
@@ -2614,14 +2653,18 @@ static void print(mlir::OpAsmPrinter &p, fir::SelectTypeOp &op) {
26142653
op.printSuccessorAtIndex(p, i);
26152654
}
26162655
p << ']';
2617-
p.printOptionalAttrDict(op.getOperation()->getAttrs(), {op.getCasesAttr(), getCompareOffsetAttr(),
2618-
getTargetOffsetAttr(), fir::SelectTypeOp::getOperandSegmentSizeAttr()});
2656+
p.printOptionalAttrDict(op.getOperation()->getAttrs(),
2657+
{op.getCasesAttr(), getCompareOffsetAttr(),
2658+
getTargetOffsetAttr(),
2659+
fir::SelectTypeOp::getOperandSegmentSizeAttr()});
26192660
}
26202661

26212662
static mlir::LogicalResult verify(fir::SelectTypeOp &op) {
26222663
if (!(op.getSelector().getType().isa<fir::BoxType>()))
26232664
return op.emitOpError("must be a boxed type");
2624-
auto cases = op.getOperation()->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr()).getValue();
2665+
auto cases = op.getOperation()
2666+
->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr())
2667+
.getValue();
26252668
auto count = op.getNumDest();
26262669
if (count == 0)
26272670
return op.emitOpError("must have at least one successor");
@@ -2638,6 +2681,37 @@ static mlir::LogicalResult verify(fir::SelectTypeOp &op) {
26382681
return mlir::success();
26392682
}
26402683

2684+
void fir::SelectTypeOp::build(mlir::OpBuilder &builder,
2685+
mlir::OperationState &result,
2686+
mlir::Value selector,
2687+
llvm::ArrayRef<mlir::Attribute> typeOperands,
2688+
llvm::ArrayRef<mlir::Block *> destinations,
2689+
llvm::ArrayRef<mlir::ValueRange> destOperands,
2690+
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
2691+
result.addOperands(selector);
2692+
result.addAttribute(getCasesAttr(), builder.getArrayAttr(typeOperands));
2693+
const auto count = destinations.size();
2694+
for (auto d : destinations)
2695+
result.addSuccessors(d);
2696+
const auto opCount = destOperands.size();
2697+
llvm::SmallVector<int32_t> argOffs;
2698+
int32_t sumArgs = 0;
2699+
for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) {
2700+
if (i < opCount) {
2701+
result.addOperands(destOperands[i]);
2702+
const auto argSz = destOperands[i].size();
2703+
argOffs.push_back(argSz);
2704+
sumArgs += argSz;
2705+
} else {
2706+
argOffs.push_back(0);
2707+
}
2708+
}
2709+
result.addAttribute(getOperandSegmentSizeAttr(),
2710+
builder.getI32VectorAttr({1, 0, sumArgs}));
2711+
result.addAttribute(getTargetOffsetAttr(), builder.getI32VectorAttr(argOffs));
2712+
result.addAttributes(attributes);
2713+
}
2714+
26412715
//===----------------------------------------------------------------------===//
26422716
// ShapeOp
26432717
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)