@@ -791,6 +791,13 @@ void AliasState::printAliases(raw_ostream &os, NewLineCounter &newLine,
791
791
// ===----------------------------------------------------------------------===//
792
792
793
793
namespace {
794
+ // / Info about block printing: a number which is its position in the visitation
795
+ // / order, and a name that is used to print reference to it, e.g. ^bb42.
796
+ struct BlockInfo {
797
+ int ordering;
798
+ StringRef name;
799
+ };
800
+
794
801
// / This class manages the state of SSA value names.
795
802
class SSANameState {
796
803
public:
@@ -808,8 +815,8 @@ class SSANameState {
808
815
// / operation, or empty if none exist.
809
816
ArrayRef<int > getOpResultGroups (Operation *op);
810
817
811
- // / Get the ID for the given block.
812
- unsigned getBlockID (Block *block);
818
+ // / Get the info for the given block.
819
+ BlockInfo getBlockInfo (Block *block);
813
820
814
821
// / Renumber the arguments for the specified region to the same names as the
815
822
// / SSA values in namesToUse. See OperationPrinter::shadowRegionArgs for
@@ -846,8 +853,9 @@ class SSANameState {
846
853
// / value of this map are the result numbers that start a result group.
847
854
DenseMap<Operation *, SmallVector<int , 1 >> opResultGroups;
848
855
849
- // / This is the block ID for each block in the current.
850
- DenseMap<Block *, unsigned > blockIDs;
856
+ // / This maps blocks to there visitation number in the current region as well
857
+ // / as the string representing their name.
858
+ DenseMap<Block *, BlockInfo> blockNames;
851
859
852
860
// / This keeps track of all of the non-numeric names that are in flight,
853
861
// / allowing us to check for duplicates.
@@ -967,9 +975,10 @@ ArrayRef<int> SSANameState::getOpResultGroups(Operation *op) {
967
975
return it == opResultGroups.end () ? ArrayRef<int >() : it->second ;
968
976
}
969
977
970
- unsigned SSANameState::getBlockID (Block *block) {
971
- auto it = blockIDs.find (block);
972
- return it != blockIDs.end () ? it->second : NameSentinel;
978
+ BlockInfo SSANameState::getBlockInfo (Block *block) {
979
+ auto it = blockNames.find (block);
980
+ BlockInfo invalidBlock{-1 , " INVALIDBLOCK" };
981
+ return it != blockNames.end () ? it->second : invalidBlock;
973
982
}
974
983
975
984
void SSANameState::shadowRegionArgs (Region ®ion, ValueRange namesToUse) {
@@ -1021,7 +1030,16 @@ void SSANameState::numberValuesInRegion(Region ®ion) {
1021
1030
for (auto &block : region) {
1022
1031
// Each block gets a unique ID, and all of the operations within it get
1023
1032
// numbered as well.
1024
- blockIDs[&block] = nextBlockID++;
1033
+ auto blockInfoIt = blockNames.insert ({&block, {-1 , " " }});
1034
+ if (blockInfoIt.second ) {
1035
+ // This block hasn't been named through `getAsmBlockArgumentNames`, use
1036
+ // default `^bbNNN` format.
1037
+ std::string name;
1038
+ llvm::raw_string_ostream (name) << " ^bb" << nextBlockID;
1039
+ blockInfoIt.first ->second .name = StringRef (name).copy (usedNameAllocator);
1040
+ }
1041
+ blockInfoIt.first ->second .ordering = nextBlockID++;
1042
+
1025
1043
numberValuesInBlock (block);
1026
1044
}
1027
1045
}
@@ -1048,11 +1066,6 @@ void SSANameState::numberValuesInBlock(Block &block) {
1048
1066
}
1049
1067
1050
1068
void SSANameState::numberValuesInOp (Operation &op) {
1051
- unsigned numResults = op.getNumResults ();
1052
- if (numResults == 0 )
1053
- return ;
1054
- Value resultBegin = op.getResult (0 );
1055
-
1056
1069
// Function used to set the special result names for the operation.
1057
1070
SmallVector<int , 2 > resultGroups (/* Size=*/ 1 , /* Value=*/ 0 );
1058
1071
auto setResultNameFn = [&](Value result, StringRef name) {
@@ -1064,11 +1077,34 @@ void SSANameState::numberValuesInOp(Operation &op) {
1064
1077
if (int resultNo = result.cast <OpResult>().getResultNumber ())
1065
1078
resultGroups.push_back (resultNo);
1066
1079
};
1080
+ // Operations can customize the printing of block names in OpAsmOpInterface.
1081
+ auto setBlockNameFn = [&](Block *block, StringRef name) {
1082
+ assert (block->getParentOp () == &op &&
1083
+ " getAsmBlockArgumentNames callback invoked on a block not directly "
1084
+ " nested under the current operation" );
1085
+ assert (!blockNames.count (block) && " block numbered multiple times" );
1086
+ SmallString<16 > tmpBuffer{" ^" };
1087
+ name = sanitizeIdentifier (name, tmpBuffer);
1088
+ if (name.data () != tmpBuffer.data ()) {
1089
+ tmpBuffer.append (name);
1090
+ name = tmpBuffer.str ();
1091
+ }
1092
+ name = name.copy (usedNameAllocator);
1093
+ blockNames[block] = {-1 , name};
1094
+ };
1095
+
1067
1096
if (!printerFlags.shouldPrintGenericOpForm ()) {
1068
- if (OpAsmOpInterface asmInterface = dyn_cast<OpAsmOpInterface>(&op))
1097
+ if (OpAsmOpInterface asmInterface = dyn_cast<OpAsmOpInterface>(&op)) {
1098
+ asmInterface.getAsmBlockNames (setBlockNameFn);
1069
1099
asmInterface.getAsmResultNames (setResultNameFn);
1100
+ }
1070
1101
}
1071
1102
1103
+ unsigned numResults = op.getNumResults ();
1104
+ if (numResults == 0 )
1105
+ return ;
1106
+ Value resultBegin = op.getResult (0 );
1107
+
1072
1108
// If the first result wasn't numbered, give it a default number.
1073
1109
if (valueIDs.try_emplace (resultBegin, nextValueID).second )
1074
1110
++nextValueID;
@@ -2609,11 +2645,7 @@ void OperationPrinter::printGenericOp(Operation *op, bool printOpName) {
2609
2645
}
2610
2646
2611
2647
void OperationPrinter::printBlockName (Block *block) {
2612
- auto id = state->getSSANameState ().getBlockID (block);
2613
- if (id != SSANameState::NameSentinel)
2614
- os << " ^bb" << id;
2615
- else
2616
- os << " ^INVALIDBLOCK" ;
2648
+ os << state->getSSANameState ().getBlockInfo (block).name ;
2617
2649
}
2618
2650
2619
2651
void OperationPrinter::print (Block *block, bool printBlockArgs,
@@ -2647,18 +2679,18 @@ void OperationPrinter::print(Block *block, bool printBlockArgs,
2647
2679
os << " // pred: " ;
2648
2680
printBlockName (pred);
2649
2681
} else {
2650
- // We want to print the predecessors in increasing numeric order, not in
2682
+ // We want to print the predecessors in a stable order, not in
2651
2683
// whatever order the use-list is in, so gather and sort them.
2652
- SmallVector<std::pair< unsigned , Block *> , 4 > predIDs;
2684
+ SmallVector<BlockInfo , 4 > predIDs;
2653
2685
for (auto *pred : block->getPredecessors ())
2654
- predIDs.push_back ({state->getSSANameState ().getBlockID (pred), pred});
2655
- llvm::array_pod_sort (predIDs.begin (), predIDs.end ());
2686
+ predIDs.push_back (state->getSSANameState ().getBlockInfo (pred));
2687
+ llvm::sort (predIDs, [](BlockInfo lhs, BlockInfo rhs) {
2688
+ return lhs.ordering < rhs.ordering ;
2689
+ });
2656
2690
2657
2691
os << " // " << predIDs.size () << " preds: " ;
2658
2692
2659
- interleaveComma (predIDs, [&](std::pair<unsigned , Block *> pred) {
2660
- printBlockName (pred.second );
2661
- });
2693
+ interleaveComma (predIDs, [&](BlockInfo pred) { os << pred.name ; });
2662
2694
}
2663
2695
os << newLine;
2664
2696
}
0 commit comments