Skip to content

Commit 3f74334

Browse files
authored
[mlir][NFC] update flang create APIs (13/n) (#149913)
See #147168 for more info.
1 parent 77524bc commit 3f74334

File tree

15 files changed

+193
-181
lines changed

15 files changed

+193
-181
lines changed

flang/include/flang/Lower/DirectivesCommon.h

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -193,26 +193,27 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
193193
mlir::Value box =
194194
!fir::isBoxAddress(info.addr.getType())
195195
? info.addr
196-
: builder.create<fir::LoadOp>(loc, info.addr);
196+
: fir::LoadOp::create(builder, loc, info.addr);
197197
mlir::Value d =
198198
builder.createIntegerConstant(loc, idxTy, dimension);
199-
auto dimInfo = builder.create<fir::BoxDimsOp>(
200-
loc, idxTy, idxTy, idxTy, box, d);
201-
builder.create<fir::ResultOp>(loc, dimInfo.getByteStride());
199+
auto dimInfo = fir::BoxDimsOp::create(builder, loc, idxTy,
200+
idxTy, idxTy, box, d);
201+
fir::ResultOp::create(builder, loc,
202+
dimInfo.getByteStride());
202203
})
203204
.genElse([&] {
204205
mlir::Value zero =
205206
builder.createIntegerConstant(loc, idxTy, 0);
206-
builder.create<fir::ResultOp>(loc, zero);
207+
fir::ResultOp::create(builder, loc, zero);
207208
})
208209
.getResults()[0];
209210
} else {
210211
mlir::Value box = !fir::isBoxAddress(info.addr.getType())
211212
? info.addr
212-
: builder.create<fir::LoadOp>(loc, info.addr);
213+
: fir::LoadOp::create(builder, loc, info.addr);
213214
mlir::Value d = builder.createIntegerConstant(loc, idxTy, dimension);
214215
auto dimInfo =
215-
builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, d);
216+
fir::BoxDimsOp::create(builder, loc, idxTy, idxTy, idxTy, box, d);
216217
stride = dimInfo.getByteStride();
217218
}
218219
strideInBytes = true;
@@ -242,14 +243,14 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
242243
lbound = builder.createIntegerConstant(loc, idxTy, *lval - 1);
243244
} else {
244245
mlir::Value lb = builder.createIntegerConstant(loc, idxTy, *lval);
245-
lbound = builder.create<mlir::arith::SubIOp>(loc, lb, baseLb);
246+
lbound = mlir::arith::SubIOp::create(builder, loc, lb, baseLb);
246247
}
247248
asFortran << *lval;
248249
} else {
249250
mlir::Value lb =
250251
fir::getBase(converter.genExprValue(loc, *lower, stmtCtx));
251252
lb = builder.createConvert(loc, baseLb.getType(), lb);
252-
lbound = builder.create<mlir::arith::SubIOp>(loc, lb, baseLb);
253+
lbound = mlir::arith::SubIOp::create(builder, loc, lb, baseLb);
253254
asFortran << detail::peelOuterConvert(*lower).AsFortran();
254255
}
255256
} else {
@@ -276,14 +277,14 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
276277
ubound = builder.createIntegerConstant(loc, idxTy, *uval - 1);
277278
} else {
278279
mlir::Value ub = builder.createIntegerConstant(loc, idxTy, *uval);
279-
ubound = builder.create<mlir::arith::SubIOp>(loc, ub, baseLb);
280+
ubound = mlir::arith::SubIOp::create(builder, loc, ub, baseLb);
280281
}
281282
asFortran << *uval;
282283
} else {
283284
mlir::Value ub =
284285
fir::getBase(converter.genExprValue(loc, *upper, stmtCtx));
285286
ub = builder.createConvert(loc, baseLb.getType(), ub);
286-
ubound = builder.create<mlir::arith::SubIOp>(loc, ub, baseLb);
287+
ubound = mlir::arith::SubIOp::create(builder, loc, ub, baseLb);
287288
asFortran << detail::peelOuterConvert(*upper).AsFortran();
288289
}
289290
}
@@ -310,12 +311,12 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
310311
.genThen([&]() {
311312
mlir::Value ext = fir::factory::readExtent(
312313
builder, loc, dataExv, dimension);
313-
builder.create<fir::ResultOp>(loc, ext);
314+
fir::ResultOp::create(builder, loc, ext);
314315
})
315316
.genElse([&] {
316317
mlir::Value zero =
317318
builder.createIntegerConstant(loc, idxTy, 0);
318-
builder.create<fir::ResultOp>(loc, zero);
319+
fir::ResultOp::create(builder, loc, zero);
319320
})
320321
.getResults()[0];
321322
} else {
@@ -326,16 +327,16 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
326327
extent = zero;
327328
if (ubound && lbound) {
328329
mlir::Value diff =
329-
builder.create<mlir::arith::SubIOp>(loc, ubound, lbound);
330-
extent = builder.create<mlir::arith::AddIOp>(loc, diff, one);
330+
mlir::arith::SubIOp::create(builder, loc, ubound, lbound);
331+
extent = mlir::arith::AddIOp::create(builder, loc, diff, one);
331332
}
332333
if (!ubound)
333334
ubound = lbound;
334335
}
335336

336337
if (!ubound) {
337338
// ub = extent - 1
338-
ubound = builder.create<mlir::arith::SubIOp>(loc, extent, one);
339+
ubound = mlir::arith::SubIOp::create(builder, loc, extent, one);
339340
}
340341
}
341342

@@ -347,8 +348,9 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
347348
loc, cumulativeExtent, extent);
348349
}
349350

350-
mlir::Value bound = builder.create<BoundsOp>(
351-
loc, boundTy, lbound, ubound, extent, stride, strideInBytes, baseLb);
351+
mlir::Value bound =
352+
BoundsOp::create(builder, loc, boundTy, lbound, ubound, extent,
353+
stride, strideInBytes, baseLb);
352354
bounds.push_back(bound);
353355
++dimension;
354356
}
@@ -461,8 +463,8 @@ fir::factory::AddrAndBoundsInfo gatherDataOperandAddrAndBounds(
461463
asFortran << designator.AsFortran();
462464

463465
if (semantics::IsOptional(compRef->GetLastSymbol())) {
464-
info.isPresent = builder.create<fir::IsPresentOp>(
465-
operandLocation, builder.getI1Type(), info.rawInput);
466+
info.isPresent = fir::IsPresentOp::create(
467+
builder, operandLocation, builder.getI1Type(), info.rawInput);
466468
}
467469

468470
if (unwrapFirBox) {
@@ -472,7 +474,7 @@ fir::factory::AddrAndBoundsInfo gatherDataOperandAddrAndBounds(
472474
fir::isPointerType(loadOp.getType())) {
473475
info.boxType = info.addr.getType();
474476
info.addr =
475-
builder.create<fir::BoxAddrOp>(operandLocation, info.addr);
477+
fir::BoxAddrOp::create(builder, operandLocation, info.addr);
476478
}
477479
info.rawInput = info.addr;
478480
}

flang/include/flang/Lower/Support/ReductionProcessor.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ ReductionProcessor::getReductionOperation(fir::FirOpBuilder &builder,
142142
assert(type.isIntOrIndexOrFloat() &&
143143
"only integer, float and complex types are currently supported");
144144
if (type.isIntOrIndex())
145-
return builder.create<IntegerOp>(loc, op1, op2);
146-
return builder.create<FloatOp>(loc, op1, op2);
145+
return IntegerOp::create(builder, loc, op1, op2);
146+
return FloatOp::create(builder, loc, op1, op2);
147147
}
148148

149149
template <typename FloatOp, typename IntegerOp, typename ComplexOp>
@@ -154,10 +154,10 @@ ReductionProcessor::getReductionOperation(fir::FirOpBuilder &builder,
154154
assert((type.isIntOrIndexOrFloat() || fir::isa_complex(type)) &&
155155
"only integer, float and complex types are currently supported");
156156
if (type.isIntOrIndex())
157-
return builder.create<IntegerOp>(loc, op1, op2);
157+
return IntegerOp::create(builder, loc, op1, op2);
158158
if (fir::isa_real(type))
159-
return builder.create<FloatOp>(loc, op1, op2);
160-
return builder.create<ComplexOp>(loc, op1, op2);
159+
return FloatOp::create(builder, loc, op1, op2);
160+
return ComplexOp::create(builder, loc, op1, op2);
161161
}
162162

163163
} // namespace omp

flang/include/flang/Optimizer/Builder/Complex.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ class Complex {
5858
protected:
5959
template <Part partId>
6060
mlir::Value extract(mlir::Value cplx) {
61-
return builder.create<fir::ExtractValueOp>(
62-
loc, getComplexPartType(cplx), cplx,
61+
return fir::ExtractValueOp::create(
62+
builder, loc, getComplexPartType(cplx), cplx,
6363
builder.getArrayAttr({builder.getIntegerAttr(
6464
builder.getIndexType(), static_cast<int>(partId))}));
6565
}
6666

6767
template <Part partId>
6868
mlir::Value insert(mlir::Value cplx, mlir::Value part) {
69-
return builder.create<fir::InsertValueOp>(
70-
loc, cplx.getType(), cplx, part,
69+
return fir::InsertValueOp::create(
70+
builder, loc, cplx.getType(), cplx, part,
7171
builder.getArrayAttr({builder.getIntegerAttr(
7272
builder.getIndexType(), static_cast<int>(partId))}));
7373
}

flang/include/flang/Optimizer/Builder/DirectivesCommon.h

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ inline AddrAndBoundsInfo getDataOperandBaseAddr(fir::FirOpBuilder &builder,
7171
mlir::Value isPresent;
7272
if (isOptional)
7373
isPresent =
74-
builder.create<fir::IsPresentOp>(loc, builder.getI1Type(), rawInput);
74+
fir::IsPresentOp::create(builder, loc, builder.getI1Type(), rawInput);
7575

7676
if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(
7777
fir::unwrapRefType(symAddr.getType()))) {
@@ -82,7 +82,7 @@ inline AddrAndBoundsInfo getDataOperandBaseAddr(fir::FirOpBuilder &builder,
8282
// if branches.
8383
if (unwrapFirBox && mlir::isa<fir::ReferenceType>(symAddr.getType()) &&
8484
!isOptional) {
85-
mlir::Value addr = builder.create<fir::LoadOp>(loc, symAddr);
85+
mlir::Value addr = fir::LoadOp::create(builder, loc, symAddr);
8686
return AddrAndBoundsInfo(addr, rawInput, isPresent, boxTy);
8787
}
8888

@@ -94,7 +94,7 @@ inline AddrAndBoundsInfo getDataOperandBaseAddr(fir::FirOpBuilder &builder,
9494
if (auto boxCharType = mlir::dyn_cast<fir::BoxCharType>(
9595
fir::unwrapRefType((symAddr.getType())))) {
9696
if (!isOptional && mlir::isa<fir::ReferenceType>(symAddr.getType())) {
97-
mlir::Value boxChar = builder.create<fir::LoadOp>(loc, symAddr);
97+
mlir::Value boxChar = fir::LoadOp::create(builder, loc, symAddr);
9898
return AddrAndBoundsInfo(boxChar, rawInput, isPresent);
9999
}
100100
}
@@ -117,10 +117,10 @@ gatherBoundsOrBoundValues(fir::FirOpBuilder &builder, mlir::Location loc,
117117
mlir::Value baseLb =
118118
fir::factory::readLowerBound(builder, loc, dataExv, dim, one);
119119
auto dimInfo =
120-
builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, d);
120+
fir::BoxDimsOp::create(builder, loc, idxTy, idxTy, idxTy, box, d);
121121
mlir::Value lb = builder.createIntegerConstant(loc, idxTy, 0);
122122
mlir::Value ub =
123-
builder.create<mlir::arith::SubIOp>(loc, dimInfo.getExtent(), one);
123+
mlir::arith::SubIOp::create(builder, loc, dimInfo.getExtent(), one);
124124
if (dim == 0) // First stride is the element size.
125125
byteStride = dimInfo.getByteStride();
126126
if (collectValuesOnly) {
@@ -130,13 +130,14 @@ gatherBoundsOrBoundValues(fir::FirOpBuilder &builder, mlir::Location loc,
130130
values.push_back(byteStride);
131131
values.push_back(baseLb);
132132
} else {
133-
mlir::Value bound = builder.create<BoundsOp>(
134-
loc, boundTy, lb, ub, dimInfo.getExtent(), byteStride, true, baseLb);
133+
mlir::Value bound =
134+
BoundsOp::create(builder, loc, boundTy, lb, ub, dimInfo.getExtent(),
135+
byteStride, true, baseLb);
135136
values.push_back(bound);
136137
}
137138
// Compute the stride for the next dimension.
138-
byteStride = builder.create<mlir::arith::MulIOp>(loc, byteStride,
139-
dimInfo.getExtent());
139+
byteStride = mlir::arith::MulIOp::create(builder, loc, byteStride,
140+
dimInfo.getExtent());
140141
}
141142
return values;
142143
}
@@ -162,46 +163,46 @@ genBoundsOpFromBoxChar(fir::FirOpBuilder &builder, mlir::Location loc,
162163
.genThen([&]() {
163164
mlir::Value boxChar =
164165
fir::isa_ref_type(info.addr.getType())
165-
? builder.create<fir::LoadOp>(loc, info.addr)
166+
? fir::LoadOp::create(builder, loc, info.addr)
166167
: info.addr;
167168
fir::BoxCharType boxCharType =
168169
mlir::cast<fir::BoxCharType>(boxChar.getType());
169170
mlir::Type refType = builder.getRefType(boxCharType.getEleTy());
170-
auto unboxed = builder.create<fir::UnboxCharOp>(
171-
loc, refType, lenType, boxChar);
171+
auto unboxed = fir::UnboxCharOp::create(builder, loc, refType,
172+
lenType, boxChar);
172173
mlir::SmallVector<mlir::Value> results = {unboxed.getResult(1),
173174
one};
174-
builder.create<fir::ResultOp>(loc, results);
175+
fir::ResultOp::create(builder, loc, results);
175176
})
176177
.genElse([&]() {
177178
mlir::SmallVector<mlir::Value> results = {zero, zero};
178-
builder.create<fir::ResultOp>(loc, results);
179+
fir::ResultOp::create(builder, loc, results);
179180
})
180181
.getResults();
181182
return {ifRes[0], ifRes[1]};
182183
}
183184
// We have already established that info.addr.getType() is a boxchar
184185
// or a boxchar address. If an address, load the boxchar.
185186
mlir::Value boxChar = fir::isa_ref_type(info.addr.getType())
186-
? builder.create<fir::LoadOp>(loc, info.addr)
187+
? fir::LoadOp::create(builder, loc, info.addr)
187188
: info.addr;
188189
fir::BoxCharType boxCharType =
189190
mlir::cast<fir::BoxCharType>(boxChar.getType());
190191
mlir::Type refType = builder.getRefType(boxCharType.getEleTy());
191192
auto unboxed =
192-
builder.create<fir::UnboxCharOp>(loc, refType, lenType, boxChar);
193+
fir::UnboxCharOp::create(builder, loc, refType, lenType, boxChar);
193194
return {unboxed.getResult(1), one};
194195
}();
195196

196-
mlir::Value ub = builder.create<mlir::arith::SubIOp>(loc, extent, one);
197+
mlir::Value ub = mlir::arith::SubIOp::create(builder, loc, extent, one);
197198
mlir::Type boundTy = builder.getType<BoundsType>();
198-
return builder.create<BoundsOp>(loc, boundTy,
199-
/*lower_bound=*/zero,
200-
/*upper_bound=*/ub,
201-
/*extent=*/extent,
202-
/*stride=*/stride,
203-
/*stride_in_bytes=*/true,
204-
/*start_idx=*/zero);
199+
return BoundsOp::create(builder, loc, boundTy,
200+
/*lower_bound=*/zero,
201+
/*upper_bound=*/ub,
202+
/*extent=*/extent,
203+
/*stride=*/stride,
204+
/*stride_in_bytes=*/true,
205+
/*start_idx=*/zero);
205206
}
206207

207208
/// Generate the bounds operation from the descriptor information.
@@ -230,12 +231,12 @@ genBoundsOpsFromBox(fir::FirOpBuilder &builder, mlir::Location loc,
230231
mlir::Value box =
231232
!fir::isBoxAddress(info.addr.getType())
232233
? info.addr
233-
: builder.create<fir::LoadOp>(loc, info.addr);
234+
: fir::LoadOp::create(builder, loc, info.addr);
234235
llvm::SmallVector<mlir::Value> boundValues =
235236
gatherBoundsOrBoundValues<BoundsOp, BoundsType>(
236237
builder, loc, dataExv, box,
237238
/*collectValuesOnly=*/true);
238-
builder.create<fir::ResultOp>(loc, boundValues);
239+
fir::ResultOp::create(builder, loc, boundValues);
239240
})
240241
.genElse([&] {
241242
// Box is not present. Populate bound values with default values.
@@ -249,21 +250,21 @@ genBoundsOpsFromBox(fir::FirOpBuilder &builder, mlir::Location loc,
249250
boundValues.push_back(zero); // byteStride
250251
boundValues.push_back(zero); // baseLb
251252
}
252-
builder.create<fir::ResultOp>(loc, boundValues);
253+
fir::ResultOp::create(builder, loc, boundValues);
253254
})
254255
.getResults();
255256
// Create the bound operations outside the if-then-else with the if op
256257
// results.
257258
for (unsigned i = 0; i < ifRes.size(); i += nbValuesPerBound) {
258-
mlir::Value bound = builder.create<BoundsOp>(
259-
loc, boundTy, ifRes[i], ifRes[i + 1], ifRes[i + 2], ifRes[i + 3],
260-
true, ifRes[i + 4]);
259+
mlir::Value bound =
260+
BoundsOp::create(builder, loc, boundTy, ifRes[i], ifRes[i + 1],
261+
ifRes[i + 2], ifRes[i + 3], true, ifRes[i + 4]);
261262
bounds.push_back(bound);
262263
}
263264
} else {
264265
mlir::Value box = !fir::isBoxAddress(info.addr.getType())
265266
? info.addr
266-
: builder.create<fir::LoadOp>(loc, info.addr);
267+
: fir::LoadOp::create(builder, loc, info.addr);
267268
bounds = gatherBoundsOrBoundValues<BoundsOp, BoundsType>(builder, loc,
268269
dataExv, box);
269270
}
@@ -299,7 +300,7 @@ genBaseBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
299300
ub = lb;
300301
} else {
301302
// ub = extent - 1
302-
ub = builder.create<mlir::arith::SubIOp>(loc, extent, one);
303+
ub = mlir::arith::SubIOp::create(builder, loc, extent, one);
303304
}
304305
mlir::Value stride = one;
305306
if (strideIncludeLowerExtent) {
@@ -308,8 +309,8 @@ genBaseBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
308309
loc, cumulativeExtent, extent);
309310
}
310311

311-
mlir::Value bound = builder.create<BoundsOp>(loc, boundTy, lb, ub, extent,
312-
stride, false, baseLb);
312+
mlir::Value bound = BoundsOp::create(builder, loc, boundTy, lb, ub, extent,
313+
stride, false, baseLb);
313314
bounds.push_back(bound);
314315
}
315316
return bounds;

flang/include/flang/Optimizer/CodeGen/FIROpPatterns.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class ConvertFIRToLLVMPattern : public mlir::ConvertToLLVMPattern {
144144
llvm::SmallVector<mlir::LLVM::GEPArg> cv = {args...};
145145
auto llvmPtrTy =
146146
mlir::LLVM::LLVMPointerType::get(ty.getContext(), /*addressSpace=*/0);
147-
return rewriter.create<mlir::LLVM::GEPOp>(loc, llvmPtrTy, ty, base, cv);
147+
return mlir::LLVM::GEPOp::create(rewriter, loc, llvmPtrTy, ty, base, cv);
148148
}
149149

150150
// Find the Block in which the alloca should be inserted.

flang/unittests/Frontend/CodeGenActionTest.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ class FakeOp : public ::mlir::Op<FakeOp> {
5050

5151
static void build(
5252
::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState) {}
53+
54+
static FakeOp create(
55+
::mlir::OpBuilder &odsBuilder, ::mlir::Location location) {
56+
::mlir::OperationState state(location, getOperationName());
57+
build(odsBuilder, state);
58+
auto res = ::llvm::dyn_cast<FakeOp>(odsBuilder.create(state));
59+
assert(res && "builder didn't return the right type");
60+
return res;
61+
}
5362
};
5463
} // namespace dummy
5564
} // namespace test
@@ -77,7 +86,7 @@ class LLVMConversionFailureCodeGenAction : public CodeGenAction {
7786
mlir::OpBuilder builder(mlirCtx.get());
7887
builder.setInsertionPointToStart(&mlirModule->getRegion().front());
7988
// Create a fake op to trip conversion to LLVM.
80-
builder.create<test::dummy::FakeOp>(loc);
89+
test::dummy::FakeOp::create(builder, loc);
8190

8291
llvmCtx = std::make_unique<llvm::LLVMContext>();
8392
}

0 commit comments

Comments
 (0)