Skip to content

Commit 6d334d7

Browse files
committed
Address Tom's review comments
Signed-off-by: Kajetan Puchalski <[email protected]>
1 parent 154b758 commit 6d334d7

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

flang/lib/Optimizer/HLFIR/Transforms/InlineHLFIRAssign.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,24 @@ InlineCopyInConversion::matchAndRewrite(hlfir::CopyInOp copyIn,
158158
"CopyInOp's WasCopied has no uses");
159159
// The copy out should always be present, either to actually copy or just
160160
// deallocate memory.
161-
auto *copyOut =
162-
copyIn.getWasCopied().getUsers().begin().getCurrent().getUser();
161+
auto copyOut = mlir::dyn_cast<hlfir::CopyOutOp>(
162+
copyIn.getWasCopied().getUsers().begin().getCurrent().getUser());
163163

164-
if (!mlir::isa<hlfir::CopyOutOp>(copyOut))
164+
if (!copyOut)
165165
return rewriter.notifyMatchFailure(copyIn,
166166
"CopyInOp has no direct CopyOut");
167167

168168
// Only inline the copy_in when copy_out does not need to be done, i.e. in
169169
// case of intent(in).
170-
if (::llvm::cast<hlfir::CopyOutOp>(copyOut).getVar())
170+
if (copyOut.getVar())
171171
return rewriter.notifyMatchFailure(copyIn, "CopyIn needs a copy-out");
172172

173173
inputVariable =
174174
hlfir::derefPointersAndAllocatables(loc, builder, inputVariable);
175175
mlir::Type resultAddrType = copyIn.getCopiedIn().getType();
176176
mlir::Value isContiguous =
177177
builder.create<fir::IsContiguousBoxOp>(loc, inputVariable);
178-
auto results =
178+
mlir::Operation::result_range results =
179179
builder
180180
.genIfOp(loc, {resultAddrType, builder.getI1Type()}, isContiguous,
181181
/*withElseRegion=*/true)
@@ -195,11 +195,11 @@ InlineCopyInConversion::matchAndRewrite(hlfir::CopyInOp copyIn,
195195
loc, builder, extents, /*isUnordered=*/true,
196196
flangomp::shouldUseWorkshareLowering(copyIn));
197197
builder.setInsertionPointToStart(loopNest.body);
198-
auto elem = hlfir::getElementAt(loc, builder, inputVariable,
199-
loopNest.oneBasedIndices);
198+
hlfir::Entity elem = hlfir::getElementAt(
199+
loc, builder, inputVariable, loopNest.oneBasedIndices);
200200
elem = hlfir::loadTrivialScalar(loc, builder, elem);
201-
auto tempElem = hlfir::getElementAt(loc, builder, temp,
202-
loopNest.oneBasedIndices);
201+
hlfir::Entity tempElem = hlfir::getElementAt(
202+
loc, builder, temp, loopNest.oneBasedIndices);
203203
builder.create<hlfir::AssignOp>(loc, elem, tempElem);
204204
builder.setInsertionPointAfter(loopNest.outerOp);
205205

@@ -209,9 +209,9 @@ InlineCopyInConversion::matchAndRewrite(hlfir::CopyInOp copyIn,
209209
if (mlir::isa<fir::BaseBoxType>(temp.getType())) {
210210
result = temp;
211211
} else {
212-
auto refTy =
212+
fir::ReferenceType refTy =
213213
fir::ReferenceType::get(temp.getElementOrSequenceType());
214-
auto refVal = builder.createConvert(loc, refTy, temp);
214+
mlir::Value refVal = builder.createConvert(loc, refTy, temp);
215215
result =
216216
builder.create<fir::EmboxOp>(loc, resultAddrType, refVal);
217217
}
@@ -221,25 +221,30 @@ InlineCopyInConversion::matchAndRewrite(hlfir::CopyInOp copyIn,
221221
})
222222
.getResults();
223223

224-
auto addr = results[0];
225-
auto needsCleanup = results[1];
224+
mlir::OpResult addr = results[0];
225+
mlir::OpResult needsCleanup = results[1];
226226

227227
builder.setInsertionPoint(copyOut);
228-
builder.genIfOp(loc, {}, needsCleanup, false).genThen([&] {
228+
builder.genIfOp(loc, {}, needsCleanup, /*withElseRegion=*/false).genThen([&] {
229229
auto boxAddr = builder.create<fir::BoxAddrOp>(loc, addr);
230-
auto heapType = fir::HeapType::get(fir::BoxValue(addr).getBaseTy());
231-
auto heapVal = builder.createConvert(loc, heapType, boxAddr.getResult());
230+
fir::HeapType heapType =
231+
fir::HeapType::get(fir::BoxValue(addr).getBaseTy());
232+
mlir::Value heapVal =
233+
builder.createConvert(loc, heapType, boxAddr.getResult());
232234
builder.create<fir::FreeMemOp>(loc, heapVal);
233235
});
234236
rewriter.eraseOp(copyOut);
235237

236-
auto tempBox = copyIn.getTempBox();
238+
mlir::Value tempBox = copyIn.getTempBox();
237239

238240
rewriter.replaceOp(copyIn, {addr, builder.genNot(loc, isContiguous)});
239241

240242
// The TempBox is only needed for flang-rt calls which we're no longer
241-
// generating.
243+
// generating. It should have no uses left at this stage.
244+
if (!tempBox.getUses().empty())
245+
return mlir::failure();
242246
rewriter.eraseOp(tempBox.getDefiningOp());
247+
243248
return mlir::success();
244249
}
245250

0 commit comments

Comments
 (0)