@@ -152,8 +152,10 @@ bool OperationFolder::insertKnownConstant(Operation *op, Attribute constValue) {
152
152
// anything. Otherwise, we move the constant to the insertion block.
153
153
Block *insertBlock = &insertRegion->front ();
154
154
if (opBlock != insertBlock || (&insertBlock->front () != op &&
155
- !isFolderOwnedConstant (op->getPrevNode ())))
155
+ !isFolderOwnedConstant (op->getPrevNode ()))) {
156
156
op->moveBefore (&insertBlock->front ());
157
+ appendFoldedLocation (op, insertBlock->getParent ()->getLoc ());
158
+ }
157
159
158
160
folderConstOp = op;
159
161
referencedDialects[op].push_back (op->getDialect ());
@@ -237,6 +239,7 @@ OperationFolder::processFoldResults(Operation *op,
237
239
auto *insertRegion = getInsertionRegion (interfaces, op->getBlock ());
238
240
auto &entry = insertRegion->front ();
239
241
rewriter.setInsertionPoint (&entry, entry.begin ());
242
+ Location loc = getFusedLocation (op->getLoc (), insertRegion->getLoc ());
240
243
241
244
// Get the constant map for the insertion region of this operation.
242
245
auto &uniquedConstants = foldScopes[insertRegion];
@@ -259,8 +262,8 @@ OperationFolder::processFoldResults(Operation *op,
259
262
// Check to see if there is a canonicalized version of this constant.
260
263
auto res = op->getResult (i);
261
264
Attribute attrRepl = foldResults[i].get <Attribute>();
262
- if (auto *constOp = tryGetOrCreateConstant (
263
- uniquedConstants, dialect, attrRepl, res.getType (), op-> getLoc () )) {
265
+ if (auto *constOp = tryGetOrCreateConstant (uniquedConstants, dialect,
266
+ attrRepl, res.getType (), loc )) {
264
267
// Ensure that this constant dominates the operation we are replacing it
265
268
// with. This may not automatically happen if the operation being folded
266
269
// was inserted before the constant within the insertion block.
@@ -364,31 +367,36 @@ static Location FlattenFusedLocationRecursively(const Location loc) {
364
367
return loc;
365
368
}
366
369
367
- void OperationFolder::appendFoldedLocation (Operation *retainedOp ,
370
+ Location OperationFolder::getFusedLocation (Location originalLocation ,
368
371
Location foldedLocation) {
372
+ // If they're already equal, no need to fuse.
373
+ if (originalLocation == foldedLocation)
374
+ return originalLocation;
375
+
369
376
// Append into existing fused location if it has the same tag.
370
377
if (auto existingFusedLoc =
371
- dyn_cast<FusedLocWith<StringAttr>>(retainedOp-> getLoc () )) {
378
+ dyn_cast<FusedLocWith<StringAttr>>(originalLocation )) {
372
379
StringAttr existingMetadata = existingFusedLoc.getMetadata ();
373
380
if (existingMetadata == fusedLocationTag) {
374
381
ArrayRef<Location> existingLocations = existingFusedLoc.getLocations ();
375
382
SetVector<Location> locations (existingLocations.begin (),
376
383
existingLocations.end ());
377
384
locations.insert (foldedLocation);
378
- Location newFusedLoc = FusedLoc::get (
379
- retainedOp ->getContext (), locations.takeVector (), existingMetadata);
380
- retainedOp-> setLoc ( FlattenFusedLocationRecursively (newFusedLoc) );
381
- return ;
385
+ Location newFusedLoc =
386
+ FusedLoc::get (originalLocation ->getContext (), locations.takeVector (),
387
+ existingMetadata );
388
+ return FlattenFusedLocationRecursively (newFusedLoc) ;
382
389
}
383
390
}
384
391
385
392
// Create a new fusedloc with retainedOp's loc and foldedLocation.
386
- // If they're already equal, no need to fuse.
387
- if (retainedOp->getLoc () == foldedLocation)
388
- return ;
389
-
390
393
Location newFusedLoc =
391
- FusedLoc::get (retainedOp->getContext (),
392
- {retainedOp->getLoc (), foldedLocation}, fusedLocationTag);
393
- retainedOp->setLoc (FlattenFusedLocationRecursively (newFusedLoc));
394
+ FusedLoc::get (originalLocation->getContext (),
395
+ {originalLocation, foldedLocation}, fusedLocationTag);
396
+ return FlattenFusedLocationRecursively (newFusedLoc);
397
+ }
398
+
399
+ void OperationFolder::appendFoldedLocation (Operation *retainedOp,
400
+ Location foldedLocation) {
401
+ retainedOp->setLoc (getFusedLocation (retainedOp->getLoc (), foldedLocation));
394
402
}
0 commit comments