@@ -1518,17 +1518,23 @@ void SSANameState::shadowRegionArgs(Region ®ion, ValueRange namesToUse) {
15181518 }
15191519}
15201520
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+
15211530void SSANameState::numberValuesInRegion (Region ®ion) {
15221531 auto setBlockArgNameFn = [&](Value arg, StringRef name) {
15231532 assert (!valueIDs.count (arg) && " arg numbered multiple times" );
15241533 assert (llvm::cast<BlockArgument>(arg).getOwner ()->getParent () == ®ion &&
15251534 " 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);
15321538 };
15331539
15341540 if (!printerFlags.shouldPrintGenericOpForm ()) {
@@ -1570,12 +1576,10 @@ void SSANameState::numberValuesInBlock(Block &block) {
15701576 specialNameBuffer.resize (strlen (" arg" ));
15711577 specialName << nextArgumentID++;
15721578 }
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);
15791583 }
15801584
15811585 // Number the operations in this block.
@@ -1589,13 +1593,9 @@ void SSANameState::numberValuesInOp(Operation &op) {
15891593 auto setResultNameFn = [&](Value result, StringRef name) {
15901594 assert (!valueIDs.count (result) && " result numbered multiple times" );
15911595 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);
15991599
16001600 // Record the result number for groups not anchored at 0.
16011601 if (int resultNo = llvm::cast<OpResult>(result).getResultNumber ())
@@ -1636,9 +1636,8 @@ void SSANameState::numberValuesInOp(Operation &op) {
16361636 Value resultBegin = op.getResult (0 );
16371637
16381638 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 ());
16421641 }
16431642 }
16441643
0 commit comments