MCPcopy Create free account
hub / github.com/NVIDIA/cuda-tile / updateSymbolName

Method updateSymbolName

lib/Dialect/CudaTile/Transforms/TransformsUtils.cpp:17–73  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15using namespace cuda_tile;
16
17LogicalResult mlir::cuda_tile::updateSymbolName(StringAttr newName,
18 SymbolOpInterface sym,
19 Operation *scopeForReplacement,
20 SymbolTable *symtab) {
21 // Check the precondition - the scope for replacement must be an ancestor of
22 // sym if sym has a parent op. If sym has no parent, then there's no such
23 // requirement.
24 if (sym->getParentOp() && !scopeForReplacement->isAncestor(sym)) {
25 auto diag = emitError(sym->getLoc())
26 << "attempted to update symbol names in a scope that did not "
27 "enclose the symbol itself";
28 diag.attachNote(scopeForReplacement->getLoc()) << "scope defined here";
29 return diag;
30 }
31
32 // If the symbol table was provided, we promised to rename all usages. Do that
33 // now.
34 if (symtab) {
35 if (failed(symtab->rename(sym, newName)))
36 return failure();
37 }
38
39 // At this point, we can update the symbol's name. If the rename happened
40 // above, then this will basically do nothing.
41 sym.setName(newName);
42
43 // Grab the debuginfo location. If we don't have one, then there's nothing to
44 // do besides just update the symbol's name.
45 auto diLoc = dyn_cast<DILocAttr>(sym.getLoc());
46 if (!diLoc)
47 return success();
48
49 // We don't have any notion of referencing a separate attribute...but there
50 // may be multiple ops/locations that refer to this subprogram. We can handle
51 // this by simply writing and running an AttrTypeReplacer.
52 auto sp = dyn_cast_if_present<DISubprogramAttr>(diLoc.getScope());
53 // If there's no subprogram on this, then we don't have to edit anything.
54 if (!sp)
55 return success();
56
57 // Create the new subprogram with the new linkage name.
58 auto newSP = sp.cloneWithNewLinkageName(newName);
59
60 // Now, we have to replace *all* references to the old subprogram with the new
61 // subprogram.
62 AttrTypeReplacer replacer;
63 replacer.addReplacement([oldSP = sp, newSP](DISubprogramAttr subprogram) {
64 return subprogram == oldSP ? newSP : subprogram;
65 });
66
67 // Do the replacement within the scope.
68 replacer.recursivelyReplaceElementsIn(scopeForReplacement,
69 /*replaceAttrs=*/true,
70 /*replaceLocs=*/true);
71
72 return success();
73}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected