@@ -1518,17 +1518,23 @@ void SSANameState::shadowRegionArgs(Region ®ion, ValueRange namesToUse) {
1518
1518
}
1519
1519
}
1520
1520
1521
+ namespace {
1522
+ // / Try to get value name from value's location, fallback to `name`.
1523
+ StringRef maybeGetValueNameFromLoc (Value value, StringRef name) {
1524
+ if (auto maybeNameLoc = value.getLoc ()->findInstanceOf <NameLoc>())
1525
+ return maybeNameLoc.getName ();
1526
+ return name;
1527
+ }
1528
+ } // namespace
1529
+
1521
1530
void SSANameState::numberValuesInRegion (Region ®ion) {
1522
1531
auto setBlockArgNameFn = [&](Value arg, StringRef name) {
1523
1532
assert (!valueIDs.count (arg) && " arg numbered multiple times" );
1524
1533
assert (llvm::cast<BlockArgument>(arg).getOwner ()->getParent () == ®ion &&
1525
1534
" arg not defined in current region" );
1526
- if (printerFlags.shouldUseNameLocAsPrefix () && isa<NameLoc>(arg.getLoc ())) {
1527
- auto nameLoc = cast<NameLoc>(arg.getLoc ());
1528
- setValueName (arg, nameLoc.getName ());
1529
- } else {
1530
- setValueName (arg, name);
1531
- }
1535
+ if (LLVM_UNLIKELY (printerFlags.shouldUseNameLocAsPrefix ()))
1536
+ name = maybeGetValueNameFromLoc (arg, name);
1537
+ setValueName (arg, name);
1532
1538
};
1533
1539
1534
1540
if (!printerFlags.shouldPrintGenericOpForm ()) {
@@ -1570,12 +1576,10 @@ void SSANameState::numberValuesInBlock(Block &block) {
1570
1576
specialNameBuffer.resize (strlen (" arg" ));
1571
1577
specialName << nextArgumentID++;
1572
1578
}
1573
- if (printerFlags.shouldUseNameLocAsPrefix () && isa<NameLoc>(arg.getLoc ())) {
1574
- auto nameLoc = cast<NameLoc>(arg.getLoc ());
1575
- setValueName (arg, nameLoc.getName ());
1576
- } else {
1577
- setValueName (arg, specialName.str ());
1578
- }
1579
+ StringRef specialNameStr = specialName.str ();
1580
+ if (LLVM_UNLIKELY (printerFlags.shouldUseNameLocAsPrefix ()))
1581
+ specialNameStr = maybeGetValueNameFromLoc (arg, specialNameStr);
1582
+ setValueName (arg, specialNameStr);
1579
1583
}
1580
1584
1581
1585
// Number the operations in this block.
@@ -1589,13 +1593,9 @@ void SSANameState::numberValuesInOp(Operation &op) {
1589
1593
auto setResultNameFn = [&](Value result, StringRef name) {
1590
1594
assert (!valueIDs.count (result) && " result numbered multiple times" );
1591
1595
assert (result.getDefiningOp () == &op && " result not defined by 'op'" );
1592
- if (printerFlags.shouldUseNameLocAsPrefix () &&
1593
- isa<NameLoc>(result.getLoc ())) {
1594
- auto nameLoc = cast<NameLoc>(result.getLoc ());
1595
- setValueName (result, nameLoc.getName ());
1596
- } else {
1597
- setValueName (result, name);
1598
- }
1596
+ if (LLVM_UNLIKELY (printerFlags.shouldUseNameLocAsPrefix ()))
1597
+ name = maybeGetValueNameFromLoc (result, name);
1598
+ setValueName (result, name);
1599
1599
1600
1600
// Record the result number for groups not anchored at 0.
1601
1601
if (int resultNo = llvm::cast<OpResult>(result).getResultNumber ())
@@ -1636,9 +1636,8 @@ void SSANameState::numberValuesInOp(Operation &op) {
1636
1636
Value resultBegin = op.getResult (0 );
1637
1637
1638
1638
if (printerFlags.shouldUseNameLocAsPrefix () && !valueIDs.count (resultBegin)) {
1639
- if (isa<NameLoc>(resultBegin.getLoc ())) {
1640
- auto nameLoc = cast<NameLoc>(resultBegin.getLoc ());
1641
- setResultNameFn (resultBegin, nameLoc.getName ());
1639
+ if (auto nameLoc = resultBegin.getLoc ()->findInstanceOf <NameLoc>()) {
1640
+ setValueName (resultBegin, nameLoc.getName ());
1642
1641
}
1643
1642
}
1644
1643
0 commit comments