Skip to content

Commit 60ee056

Browse files
[flang] Fix replaceAllUsesWith API violations (1/N) (#154698)
`replaceAllUsesWith` is not safe to use in a dialect conversion and will be deactivated soon (#154112). Fix commit fixes some API violations. Also some general improvements.
1 parent 0c480dd commit 60ee056

File tree

9 files changed

+10
-34
lines changed

9 files changed

+10
-34
lines changed

flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,8 +1900,7 @@ hlfir::ShapeOfOp::canonicalize(ShapeOfOp shapeOf,
19001900
// shape information is not available at compile time
19011901
return llvm::LogicalResult::failure();
19021902

1903-
rewriter.replaceAllUsesWith(shapeOf.getResult(), shape);
1904-
rewriter.eraseOp(shapeOf);
1903+
rewriter.replaceOp(shapeOf, shape);
19051904
return llvm::LogicalResult::success();
19061905
}
19071906

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,8 @@ struct AssociateOpConversion
455455

456456
mlir::Type associateHlfirVarType = associate.getResultTypes()[0];
457457
hlfirVar = adjustVar(hlfirVar, associateHlfirVarType);
458-
associate.getResult(0).replaceAllUsesWith(hlfirVar);
459-
460458
mlir::Type associateFirVarType = associate.getResultTypes()[1];
461459
firVar = adjustVar(firVar, associateFirVarType);
462-
associate.getResult(1).replaceAllUsesWith(firVar);
463-
associate.getResult(2).replaceAllUsesWith(flag);
464460
// FIXME: note that the AssociateOp that is being erased
465461
// here will continue to be a user of the original Source
466462
// operand (e.g. a result of hlfir.elemental), because
@@ -472,7 +468,7 @@ struct AssociateOpConversion
472468
// the conversions, so that we can analyze HLFIR in its
473469
// original form and decide which of the AssociateOp
474470
// users of hlfir.expr can reuse the buffer (if it can).
475-
rewriter.eraseOp(associate);
471+
rewriter.replaceOp(associate, {hlfirVar, firVar, flag});
476472
};
477473

478474
// If this is the last use of the expression value and this is an hlfir.expr

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ class InlineElementalConversion
101101
elemental.getLoc(), builder, elemental, apply.getIndices());
102102

103103
// remove the old elemental and all of the bookkeeping
104-
rewriter.replaceAllUsesWith(apply.getResult(), yield.getElementValue());
104+
rewriter.replaceOp(apply, {yield.getElementValue()});
105105
rewriter.eraseOp(yield);
106-
rewriter.eraseOp(apply);
107106
rewriter.eraseOp(destroy);
108107
rewriter.eraseOp(elemental);
109108

flang/lib/Optimizer/OpenMP/SimdOnly.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,13 @@ class SimdOnlyConversionPattern : public mlir::RewritePattern {
9595
return mlir::success();
9696
}
9797
if (auto mapInfoOp = mlir::dyn_cast<mlir::omp::MapInfoOp>(op)) {
98-
mapInfoOp.getResult().replaceAllUsesWith(mapInfoOp.getVarPtr());
99-
rewriter.eraseOp(mapInfoOp);
98+
rewriter.replaceOp(mapInfoOp, {mapInfoOp.getVarPtr()});
10099
return mlir::success();
101100
}
102101

103102
// Might be leftover after parse tree rewriting
104103
if (auto threadPrivateOp = mlir::dyn_cast<mlir::omp::ThreadprivateOp>(op)) {
105-
threadPrivateOp.getTlsAddr().replaceAllUsesWith(
106-
threadPrivateOp.getSymAddr());
107-
rewriter.eraseOp(threadPrivateOp);
104+
rewriter.replaceOp(threadPrivateOp, {threadPrivateOp.getSymAddr()});
108105
return mlir::success();
109106
}
110107

flang/lib/Optimizer/Transforms/AffineDemotion.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ class ConvertConversion : public mlir::OpRewritePattern<fir::ConvertOp> {
117117
op.getValue());
118118
return success();
119119
}
120-
rewriter.startOpModification(op->getParentOp());
121-
op.getResult().replaceAllUsesWith(op.getValue());
122-
rewriter.finalizeOpModification(op->getParentOp());
123-
rewriter.eraseOp(op);
120+
rewriter.replaceOp(op, op.getValue());
124121
}
125122
return success();
126123
}

flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,6 @@ class ArrayUpdateConversion : public ArrayUpdateConversionBase<ArrayUpdateOp> {
12641264
auto lhsEltRefType = toRefType(update.getMerge().getType());
12651265
auto [_, lhsLoadResult] = materializeAssignment(
12661266
loc, rewriter, update, assignElement, lhsEltRefType);
1267-
update.replaceAllUsesWith(lhsLoadResult);
12681267
rewriter.replaceOp(update, lhsLoadResult);
12691268
return mlir::success();
12701269
}
@@ -1287,7 +1286,6 @@ class ArrayModifyConversion : public ArrayUpdateConversionBase<ArrayModifyOp> {
12871286
auto lhsEltRefType = modify.getResult(0).getType();
12881287
auto [lhsEltCoor, lhsLoadResult] = materializeAssignment(
12891288
loc, rewriter, modify, assignElement, lhsEltRefType);
1290-
modify.replaceAllUsesWith(mlir::ValueRange{lhsEltCoor, lhsLoadResult});
12911289
rewriter.replaceOp(modify, mlir::ValueRange{lhsEltCoor, lhsLoadResult});
12921290
return mlir::success();
12931291
}
@@ -1339,7 +1337,6 @@ class ArrayAccessConversion : public ArrayUpdateConversionBase<ArrayAccessOp> {
13391337
// This array_access is associated with an array_amend and there is a
13401338
// conflict. Make a copy to store into.
13411339
auto result = referenceToClone(loc, rewriter, access);
1342-
access.replaceAllUsesWith(result);
13431340
rewriter.replaceOp(access, result);
13441341
return mlir::success();
13451342
}

flang/lib/Optimizer/Transforms/FIRToSCF.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "flang/Optimizer/Dialect/FIRDialect.h"
1010
#include "flang/Optimizer/Transforms/Passes.h"
1111
#include "mlir/Dialect/SCF/IR/SCF.h"
12-
#include "mlir/Transforms/DialectConversion.h"
12+
#include "mlir/Transforms/WalkPatternRewriteDriver.h"
1313

1414
namespace fir {
1515
#define GEN_PASS_DEF_FIRTOSCFPASS
@@ -201,12 +201,7 @@ void FIRToSCFPass::runOnOperation() {
201201
mlir::RewritePatternSet patterns(&getContext());
202202
patterns.add<DoLoopConversion, IterWhileConversion, IfConversion>(
203203
patterns.getContext());
204-
mlir::ConversionTarget target(getContext());
205-
target.addIllegalOp<fir::DoLoopOp, fir::IterWhileOp, fir::IfOp>();
206-
target.markUnknownOpDynamicallyLegal([](mlir::Operation *) { return true; });
207-
if (failed(
208-
applyPartialConversion(getOperation(), target, std::move(patterns))))
209-
signalPassFailure();
204+
walkAndApplyPatterns(getOperation(), std::move(patterns));
210205
}
211206

212207
std::unique_ptr<mlir::Pass> fir::createFIRToSCFPass() {

flang/lib/Optimizer/Transforms/OptimizeArrayRepacking.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ PackingOfContiguous::matchAndRewrite(fir::PackArrayOp op,
5858
mlir::PatternRewriter &rewriter) const {
5959
mlir::Value box = op.getArray();
6060
if (hlfir::isSimplyContiguous(box, !op.getInnermost())) {
61-
rewriter.replaceAllUsesWith(op, box);
62-
rewriter.eraseOp(op);
61+
rewriter.replaceOp(op, box);
6362
return mlir::success();
6463
}
6564
return mlir::failure();

flang/lib/Optimizer/Transforms/StackArrays.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,7 @@ AllocMemConversion::matchAndRewrite(fir::AllocMemOp allocmem,
600600
// replace references to heap allocation with references to stack allocation
601601
mlir::Value newValue = convertAllocationType(
602602
rewriter, allocmem.getLoc(), allocmem.getResult(), alloca->getResult());
603-
rewriter.replaceAllUsesWith(allocmem.getResult(), newValue);
604-
605-
// remove allocmem operation
606-
rewriter.eraseOp(allocmem.getOperation());
603+
rewriter.replaceOp(allocmem, newValue);
607604

608605
return mlir::success();
609606
}

0 commit comments

Comments
 (0)