[llvm] [LLVM][TableGen] Use StringRef for various members `CGIOperandList::OperandInfo` (PR #140625)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Tue May 20 08:21:48 PDT 2025


https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/140625

>From 89624c116a04b96fc3d1a1b5360cbaf3c8474c45 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Mon, 19 May 2025 07:56:58 -0700
Subject: [PATCH 1/2] [LLVM][TableGen] Use StringRef

---
 llvm/utils/TableGen/AsmMatcherEmitter.cpp     |  2 +-
 llvm/utils/TableGen/CodeEmitterGen.cpp        |  4 +-
 llvm/utils/TableGen/Common/AsmWriterInst.cpp  |  2 +-
 .../TableGen/Common/CodeGenDAGPatterns.cpp    | 41 ++++++++-----------
 .../TableGen/Common/CodeGenDAGPatterns.h      | 22 +++++-----
 .../TableGen/Common/CodeGenInstruction.cpp    | 18 ++++----
 .../TableGen/Common/CodeGenInstruction.h      | 18 ++++----
 llvm/utils/TableGen/Common/DAGISelMatcher.h   |  6 +--
 llvm/utils/TableGen/DAGISelMatcherGen.cpp     |  2 +-
 llvm/utils/TableGen/DecoderEmitter.cpp        | 36 ++++++++--------
 llvm/utils/TableGen/GlobalISelEmitter.cpp     | 10 ++---
 11 files changed, 79 insertions(+), 82 deletions(-)

diff --git a/llvm/utils/TableGen/AsmMatcherEmitter.cpp b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
index 2771facb004cc..9792eb41ea5d7 100644
--- a/llvm/utils/TableGen/AsmMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
@@ -1908,7 +1908,7 @@ void MatchableInfo::buildAliasResultOperands(bool AliasConstraintsAreChecked) {
     }
 
     // Handle all the suboperands for this operand.
-    const std::string &OpName = OpInfo.Name;
+    StringRef OpName = OpInfo.Name;
     for (; AliasOpNo < LastOpNo &&
            CGA.ResultInstOperandIndex[AliasOpNo].first == Idx;
          ++AliasOpNo) {
diff --git a/llvm/utils/TableGen/CodeEmitterGen.cpp b/llvm/utils/TableGen/CodeEmitterGen.cpp
index 83c0330f7d3eb..0e19b4d60c5c2 100644
--- a/llvm/utils/TableGen/CodeEmitterGen.cpp
+++ b/llvm/utils/TableGen/CodeEmitterGen.cpp
@@ -142,8 +142,8 @@ bool CodeEmitterGen::addCodeToMergeInOperand(const Record *R,
   }
 
   std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(OpIdx);
-  std::string &EncoderMethodName =
-      CGI.Operands[SO.first].EncoderMethodNames[SO.second];
+  const std::string EncoderMethodName =
+      CGI.Operands[SO.first].EncoderMethodNames[SO.second].str();
 
   if (UseAPInt)
     Case += "      op.clearAllBits();\n";
diff --git a/llvm/utils/TableGen/Common/AsmWriterInst.cpp b/llvm/utils/TableGen/Common/AsmWriterInst.cpp
index 1fa609e900051..3629247751d2f 100644
--- a/llvm/utils/TableGen/Common/AsmWriterInst.cpp
+++ b/llvm/utils/TableGen/Common/AsmWriterInst.cpp
@@ -177,7 +177,7 @@ AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned CGIIndex,
         CGIOperandList::OperandInfo OpInfo = CGI.Operands[OpNo];
 
         unsigned MIOp = OpInfo.MIOperandNo;
-        Operands.emplace_back(OpInfo.PrinterMethodName, MIOp, Modifier,
+        Operands.emplace_back(OpInfo.PrinterMethodName.str(), MIOp, Modifier,
                               AsmWriterOperand::isMachineInstrOperand,
                               OpInfo.OperandType == "MCOI::OPERAND_PCREL");
       }
diff --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
index f03cbd9a170e2..3029604adcc36 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
@@ -2096,7 +2096,7 @@ void TreePatternNode::RemoveAllTypes() {
 /// SubstituteFormalArguments - Replace the formal arguments in this tree
 /// with actual values specified by ArgMap.
 void TreePatternNode::SubstituteFormalArguments(
-    std::map<std::string, TreePatternNodePtr> &ArgMap) {
+    std::map<StringRef, TreePatternNodePtr> &ArgMap) {
   if (isLeaf())
     return;
 
@@ -2224,7 +2224,7 @@ void TreePatternNode::InlinePatternFragments(
     Scope = TP.getDAGPatterns().allocateScope();
 
   // Compute the map of formal to actual arguments.
-  std::map<std::string, TreePatternNodePtr> ArgMap;
+  std::map<StringRef, TreePatternNodePtr> ArgMap;
   for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) {
     TreePatternNodePtr Child = getChildShared(i);
     if (Scope != 0) {
@@ -3441,7 +3441,7 @@ void CodeGenDAGPatterns::ParseDefaultOperands() {
 /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an
 /// instruction input.  Return true if this is a real use.
 static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat,
-                      std::map<std::string, TreePatternNodePtr> &InstInputs) {
+                      std::map<StringRef, TreePatternNodePtr> &InstInputs) {
   // No name -> not interesting.
   if (Pat->getName().empty()) {
     if (Pat->isLeaf()) {
@@ -3495,11 +3495,8 @@ static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat,
 /// part of "I", the instruction), computing the set of inputs and outputs of
 /// the pattern.  Report errors if we see anything naughty.
 void CodeGenDAGPatterns::FindPatternInputsAndOutputs(
-    TreePattern &I, TreePatternNodePtr Pat,
-    std::map<std::string, TreePatternNodePtr> &InstInputs,
-    MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
-        &InstResults,
-    std::vector<const Record *> &InstImpResults) {
+    TreePattern &I, TreePatternNodePtr Pat, InstInputsTy &InstInputs,
+    InstResultsTy &InstResults, std::vector<const Record *> &InstImpResults) {
   // The instruction pattern still has unresolved fragments.  For *named*
   // nodes we must resolve those here.  This may not result in multiple
   // alternatives.
@@ -3816,11 +3813,11 @@ void CodeGenDAGPatterns::parseInstructionPattern(CodeGenInstruction &CGI,
 
   // InstInputs - Keep track of all of the inputs of the instruction, along
   // with the record they are declared as.
-  std::map<std::string, TreePatternNodePtr> InstInputs;
+  std::map<StringRef, TreePatternNodePtr> InstInputs;
 
   // InstResults - Keep track of all the virtual registers that are 'set'
   // in the instruction, including what reg class they are.
-  MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
+  MapVector<StringRef, TreePatternNodePtr, std::map<StringRef, unsigned>>
       InstResults;
 
   std::vector<const Record *> InstImpResults;
@@ -3862,18 +3859,17 @@ void CodeGenDAGPatterns::parseInstructionPattern(CodeGenInstruction &CGI,
   SmallVector<TreePatternNodePtr, 2> ResNodes;
   for (unsigned i = 0; i != NumResults; ++i) {
     if (i == CGI.Operands.size()) {
-      const std::string &OpName =
-          llvm::find_if(
-              InstResults,
-              [](const std::pair<std::string, TreePatternNodePtr> &P) {
-                return P.second;
-              })
+      StringRef OpName =
+          llvm::find_if(InstResults,
+                        [](const std::pair<StringRef, TreePatternNodePtr> &P) {
+                          return P.second;
+                        })
               ->first;
 
       I.error("'" + OpName + "' set but does not appear in operand list!");
     }
 
-    const std::string &OpName = CGI.Operands[i].Name;
+    StringRef OpName = CGI.Operands[i].Name;
 
     // Check that it exists in InstResults.
     auto InstResultIter = InstResults.find(OpName);
@@ -3906,7 +3902,7 @@ void CodeGenDAGPatterns::parseInstructionPattern(CodeGenInstruction &CGI,
   std::vector<const Record *> Operands;
   for (unsigned i = NumResults, e = CGI.Operands.size(); i != e; ++i) {
     CGIOperandList::OperandInfo &Op = CGI.Operands[i];
-    const std::string &OpName = Op.Name;
+    StringRef OpName = Op.Name;
     if (OpName.empty()) {
       I.error("Operand #" + Twine(i) + " in operands list has no name!");
       continue;
@@ -4050,7 +4046,7 @@ void CodeGenDAGPatterns::ParseInstructions() {
 typedef std::pair<TreePatternNode *, unsigned> NameRecord;
 
 static void FindNames(TreePatternNode &P,
-                      std::map<std::string, NameRecord> &Names,
+                      std::map<StringRef, NameRecord> &Names,
                       TreePattern *PatternTop) {
   if (!P.getName().empty()) {
     NameRecord &Rec = Names[P.getName()];
@@ -4088,7 +4084,7 @@ void CodeGenDAGPatterns::AddPatternToMatch(TreePattern *Pattern,
 
   // Find all of the named values in the input and output, ensure they have the
   // same type.
-  std::map<std::string, NameRecord> SrcNames, DstNames;
+  std::map<StringRef, NameRecord> SrcNames, DstNames;
   FindNames(PTM.getSrcPattern(), SrcNames, Pattern);
   FindNames(PTM.getDstPattern(), DstNames, Pattern);
 
@@ -4409,9 +4405,8 @@ void CodeGenDAGPatterns::ParsePatterns() {
                    "with temporaries yet!");
 
     // Validate that the input pattern is correct.
-    std::map<std::string, TreePatternNodePtr> InstInputs;
-    MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
-        InstResults;
+    InstInputsTy InstInputs;
+    InstResultsTy InstResults;
     std::vector<const Record *> InstImpResults;
     for (unsigned j = 0, ee = Pattern.getNumTrees(); j != ee; ++j)
       FindPatternInputsAndOutputs(Pattern, Pattern.getTree(j), InstInputs,
diff --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
index 364c82e1233bd..725414f8f68d9 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
@@ -633,7 +633,7 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
 
   /// Name - The name given to this node with the :$foo notation.
   ///
-  std::string Name;
+  StringRef Name;
 
   std::vector<ScopedName> NamesAsPredicateArg;
 
@@ -667,8 +667,8 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
   }
 
   bool hasName() const { return !Name.empty(); }
-  const std::string &getName() const { return Name; }
-  void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
+  StringRef getName() const { return Name; }
+  void setName(StringRef N) { Name = N; }
 
   const std::vector<ScopedName> &getNamesAsPredicateArg() const {
     return NamesAsPredicateArg;
@@ -826,7 +826,7 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
   /// SubstituteFormalArguments - Replace the formal arguments in this tree
   /// with actual values specified by ArgMap.
   void
-  SubstituteFormalArguments(std::map<std::string, TreePatternNodePtr> &ArgMap);
+  SubstituteFormalArguments(std::map<StringRef, TreePatternNodePtr> &ArgMap);
 
   /// InlinePatternFragments - If \p T pattern refers to any pattern
   /// fragments, return the set of inlined versions (this can be more than
@@ -1261,12 +1261,14 @@ class CodeGenDAGPatterns {
                        ArrayRef<const Record *> InstImpResults,
                        bool ShouldIgnore = false);
   void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
-  void FindPatternInputsAndOutputs(
-      TreePattern &I, TreePatternNodePtr Pat,
-      std::map<std::string, TreePatternNodePtr> &InstInputs,
-      MapVector<std::string, TreePatternNodePtr,
-                std::map<std::string, unsigned>> &InstResults,
-      std::vector<const Record *> &InstImpResults);
+
+  using InstInputsTy = std::map<StringRef, TreePatternNodePtr>;
+  using InstResultsTy =
+      MapVector<StringRef, TreePatternNodePtr, std::map<StringRef, unsigned>>;
+  void FindPatternInputsAndOutputs(TreePattern &I, TreePatternNodePtr Pat,
+                                   InstInputsTy &InstInputs,
+                                   InstResultsTy &InstResults,
+                                   std::vector<const Record *> &InstImpResults);
   unsigned getNewUID();
 };
 
diff --git a/llvm/utils/TableGen/Common/CodeGenInstruction.cpp b/llvm/utils/TableGen/Common/CodeGenInstruction.cpp
index 281df2302605e..0dfcf200d7e4b 100644
--- a/llvm/utils/TableGen/Common/CodeGenInstruction.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenInstruction.cpp
@@ -78,23 +78,23 @@ CGIOperandList::CGIOperandList(const Record *R) : TheDef(R) {
                                        "' instruction!");
 
     const Record *Rec = Arg->getDef();
-    std::string PrintMethod = "printOperand";
-    std::string EncoderMethod;
+    StringRef PrintMethod = "printOperand";
+    StringRef EncoderMethod;
     std::string OperandType = "OPERAND_UNKNOWN";
     std::string OperandNamespace = "MCOI";
     unsigned NumOps = 1;
     const DagInit *MIOpInfo = nullptr;
     if (Rec->isSubClassOf("RegisterOperand")) {
-      PrintMethod = Rec->getValueAsString("PrintMethod").str();
+      PrintMethod = Rec->getValueAsString("PrintMethod");
       OperandType = Rec->getValueAsString("OperandType").str();
       OperandNamespace = Rec->getValueAsString("OperandNamespace").str();
-      EncoderMethod = Rec->getValueAsString("EncoderMethod").str();
+      EncoderMethod = Rec->getValueAsString("EncoderMethod");
     } else if (Rec->isSubClassOf("Operand")) {
-      PrintMethod = Rec->getValueAsString("PrintMethod").str();
+      PrintMethod = Rec->getValueAsString("PrintMethod");
       OperandType = Rec->getValueAsString("OperandType").str();
       OperandNamespace = Rec->getValueAsString("OperandNamespace").str();
       // If there is an explicit encoder method, use it.
-      EncoderMethod = Rec->getValueAsString("EncoderMethod").str();
+      EncoderMethod = Rec->getValueAsString("EncoderMethod");
       MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
 
       // Verify that MIOpInfo has an 'ops' root value.
@@ -139,8 +139,8 @@ CGIOperandList::CGIOperandList(const Record *R) : TheDef(R) {
                           " has the same name as a previous operand!");
 
     OperandInfo &OpInfo = OperandList.emplace_back(
-        Rec, ArgName.str(), std::string(std::move(PrintMethod)),
-        OperandNamespace + "::" + OperandType, MIOperandNo, NumOps, MIOpInfo);
+        Rec, ArgName, PrintMethod, OperandNamespace + "::" + OperandType,
+        MIOperandNo, NumOps, MIOpInfo);
 
     if (SubArgDag) {
       if (SubArgDag->getNumArgs() != NumOps) {
@@ -182,7 +182,7 @@ CGIOperandList::CGIOperandList(const Record *R) : TheDef(R) {
     } else if (!EncoderMethod.empty()) {
       // If we have no explicit sub-op dag, but have an top-level encoder
       // method, the single encoder will multiple sub-ops, itself.
-      OpInfo.EncoderMethodNames[0] = std::move(EncoderMethod);
+      OpInfo.EncoderMethodNames[0] = EncoderMethod;
       for (unsigned j = 1; j < NumOps; ++j)
         OpInfo.DoNotEncode[j] = true;
     }
diff --git a/llvm/utils/TableGen/Common/CodeGenInstruction.h b/llvm/utils/TableGen/Common/CodeGenInstruction.h
index 44c0ab70dc615..e38979af3909d 100644
--- a/llvm/utils/TableGen/Common/CodeGenInstruction.h
+++ b/llvm/utils/TableGen/Common/CodeGenInstruction.h
@@ -77,18 +77,18 @@ class CGIOperandList {
 
     /// Name - If this operand was assigned a symbolic name, this is it,
     /// otherwise, it's empty.
-    std::string Name;
+    StringRef Name;
 
     /// The names of sub-operands, if given, otherwise empty.
-    std::vector<std::string> SubOpNames;
+    std::vector<StringRef> SubOpNames;
 
     /// PrinterMethodName - The method used to print operands of this type in
     /// the asmprinter.
-    std::string PrinterMethodName;
+    StringRef PrinterMethodName;
 
     /// The method used to get the machine operand value for binary
     /// encoding, per sub-operand. If empty, uses "getMachineOpValue".
-    std::vector<std::string> EncoderMethodNames;
+    std::vector<StringRef> EncoderMethodNames;
 
     /// OperandType - A value from MCOI::OperandType representing the type of
     /// the operand.
@@ -116,13 +116,13 @@ class CGIOperandList {
     /// track constraint info for each.
     std::vector<ConstraintInfo> Constraints;
 
-    OperandInfo(const Record *R, const std::string &N, const std::string &PMN,
+    OperandInfo(const Record *R, StringRef Name, StringRef PrinterMethodName,
                 const std::string &OT, unsigned MION, unsigned MINO,
                 const DagInit *MIOI)
-        : Rec(R), Name(N), SubOpNames(MINO), PrinterMethodName(PMN),
-          EncoderMethodNames(MINO), OperandType(OT), MIOperandNo(MION),
-          MINumOperands(MINO), DoNotEncode(MINO), MIOperandInfo(MIOI),
-          Constraints(MINO) {}
+        : Rec(R), Name(Name), SubOpNames(MINO),
+          PrinterMethodName(PrinterMethodName), EncoderMethodNames(MINO),
+          OperandType(OT), MIOperandNo(MION), MINumOperands(MINO),
+          DoNotEncode(MINO), MIOperandInfo(MIOI), Constraints(MINO) {}
 
     /// getTiedOperand - If this operand is tied to another one, return the
     /// other operand number.  Otherwise, return -1.
diff --git a/llvm/utils/TableGen/Common/DAGISelMatcher.h b/llvm/utils/TableGen/Common/DAGISelMatcher.h
index 07c00d3c8c53b..b11c1366ef5f0 100644
--- a/llvm/utils/TableGen/Common/DAGISelMatcher.h
+++ b/llvm/utils/TableGen/Common/DAGISelMatcher.h
@@ -714,7 +714,7 @@ class CheckComplexPatMatcher : public Matcher {
   unsigned MatchNumber;
 
   /// Name - The name of the node we're matching, for comment emission.
-  std::string Name;
+  StringRef Name;
 
   /// FirstResult - This is the first slot in the RecordedNodes list that the
   /// result of the match populates.
@@ -722,14 +722,14 @@ class CheckComplexPatMatcher : public Matcher {
 
 public:
   CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
-                         const std::string &name, unsigned firstresult)
+                         StringRef name, unsigned firstresult)
       : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
         Name(name), FirstResult(firstresult) {}
 
   const ComplexPattern &getPattern() const { return Pattern; }
   unsigned getMatchNumber() const { return MatchNumber; }
 
-  std::string getName() const { return Name; }
+  StringRef getName() const { return Name; }
   unsigned getFirstResult() const { return FirstResult; }
 
   static bool classof(const Matcher *N) {
diff --git a/llvm/utils/TableGen/DAGISelMatcherGen.cpp b/llvm/utils/TableGen/DAGISelMatcherGen.cpp
index aec9a8796870b..a1ab06defb456 100644
--- a/llvm/utils/TableGen/DAGISelMatcherGen.cpp
+++ b/llvm/utils/TableGen/DAGISelMatcherGen.cpp
@@ -508,7 +508,7 @@ void MatcherGen::EmitMatchCode(const TreePatternNode &N,
   // we already saw this in the pattern, emit code to verify dagness.
   SmallVector<std::string, 4> Names;
   if (!N.getName().empty())
-    Names.push_back(N.getName());
+    Names.push_back(N.getName().str());
 
   for (const ScopedName &Name : N.getNamesAsPredicateArg()) {
     Names.push_back(
diff --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index 3f00cd8229f7c..bf1802e5e06cd 100644
--- a/llvm/utils/TableGen/DecoderEmitter.cpp
+++ b/llvm/utils/TableGen/DecoderEmitter.cpp
@@ -1891,7 +1891,7 @@ static void debugDumpRecord(const Record &Rec) {
 /// constant-valued bit values, and OpInfo.Fields with the ranges of bits to
 /// insert from the decoded instruction.
 static void addOneOperandFields(const Record &EncodingDef, const BitsInit &Bits,
-                                std::map<std::string, std::string> &TiedNames,
+                                std::map<StringRef, StringRef> &TiedNames,
                                 StringRef OpName, OperandInfo &OpInfo) {
   // Some bits of the operand may be required to be 1 depending on the
   // instruction's encoding. Collect those bits.
@@ -1916,8 +1916,8 @@ static void addOneOperandFields(const Record &EncodingDef, const BitsInit &Bits,
       } else {
         Var = dyn_cast<VarInit>(Bits.getBit(J));
       }
-      if (!Var || (Var->getName() != OpName &&
-                   Var->getName() != TiedNames[OpName.str()]))
+      if (!Var ||
+          (Var->getName() != OpName && Var->getName() != TiedNames[OpName]))
         break;
     }
     if (I == J)
@@ -1972,22 +1972,22 @@ populateInstruction(const CodeGenTarget &Target, const Record &EncodingDef,
 
   // Search for tied operands, so that we can correctly instantiate
   // operands that are not explicitly represented in the encoding.
-  std::map<std::string, std::string> TiedNames;
-  for (const auto &[I, Op] : enumerate(CGI.Operands)) {
+  std::map<StringRef, StringRef> TiedNames;
+  for (const auto &Op : CGI.Operands) {
     for (const auto &[J, CI] : enumerate(Op.Constraints)) {
-      if (CI.isTied()) {
-        std::pair<unsigned, unsigned> SO =
-            CGI.Operands.getSubOperandNumber(CI.getTiedOperand());
-        std::string TiedName = CGI.Operands[SO.first].SubOpNames[SO.second];
-        if (TiedName.empty())
-          TiedName = CGI.Operands[SO.first].Name;
-        std::string MyName = Op.SubOpNames[J];
-        if (MyName.empty())
-          MyName = Op.Name;
-
-        TiedNames[MyName] = TiedName;
-        TiedNames[TiedName] = std::move(MyName);
-      }
+      if (!CI.isTied())
+        continue;
+      std::pair<unsigned, unsigned> SO =
+          CGI.Operands.getSubOperandNumber(CI.getTiedOperand());
+      StringRef TiedName = CGI.Operands[SO.first].SubOpNames[SO.second];
+      if (TiedName.empty())
+        TiedName = CGI.Operands[SO.first].Name;
+      StringRef MyName = Op.SubOpNames[J];
+      if (MyName.empty())
+        MyName = Op.Name;
+
+      TiedNames[MyName] = TiedName;
+      TiedNames[TiedName] = MyName;
     }
   }
 
diff --git a/llvm/utils/TableGen/GlobalISelEmitter.cpp b/llvm/utils/TableGen/GlobalISelEmitter.cpp
index d516879f088d3..edaf5299efc39 100644
--- a/llvm/utils/TableGen/GlobalISelEmitter.cpp
+++ b/llvm/utils/TableGen/GlobalISelEmitter.cpp
@@ -849,7 +849,7 @@ Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher(
     const Init *SrcInit = Src.getLeafValue();
     if (const IntInit *SrcIntInit = dyn_cast<IntInit>(SrcInit)) {
       OperandMatcher &OM =
-          InsnMatcher.addOperand(OpIdx++, Src.getName(), TempOpIdx);
+          InsnMatcher.addOperand(OpIdx++, Src.getName().str(), TempOpIdx);
       OM.addPredicate<LiteralIntOperandMatcher>(SrcIntInit->getValue());
     } else {
       return failedImport(
@@ -883,8 +883,8 @@ Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher(
         if (!CCDef || !CCDef->isSubClassOf("CondCode"))
           return failedImport("Unable to handle CondCode");
 
-        OperandMatcher &OM =
-            InsnMatcher.addOperand(OpIdx++, SrcChild.getName(), TempOpIdx);
+        OperandMatcher &OM = InsnMatcher.addOperand(
+            OpIdx++, SrcChild.getName().str(), TempOpIdx);
         StringRef PredType = IsFCmp ? CCDef->getValueAsString("FCmpPredicate")
                                     : CCDef->getValueAsString("ICmpPredicate");
 
@@ -930,8 +930,8 @@ Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher(
         // For G_INTRINSIC/G_INTRINSIC_W_SIDE_EFFECTS, the operand immediately
         // following the defs is an intrinsic ID.
         if (I == 0) {
-          OperandMatcher &OM =
-              InsnMatcher.addOperand(OpIdx++, SrcChild.getName(), TempOpIdx);
+          OperandMatcher &OM = InsnMatcher.addOperand(
+              OpIdx++, SrcChild.getName().str(), TempOpIdx);
           OM.addPredicate<IntrinsicIDOperandMatcher>(II);
           continue;
         }

>From 503401773f7502473a470184bf80263e8298d8c0 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Tue, 20 May 2025 08:21:10 -0700
Subject: [PATCH 2/2] Review feedback

---
 llvm/utils/TableGen/CodeEmitterGen.cpp | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/llvm/utils/TableGen/CodeEmitterGen.cpp b/llvm/utils/TableGen/CodeEmitterGen.cpp
index 0e19b4d60c5c2..2fe40450abfda 100644
--- a/llvm/utils/TableGen/CodeEmitterGen.cpp
+++ b/llvm/utils/TableGen/CodeEmitterGen.cpp
@@ -142,8 +142,8 @@ bool CodeEmitterGen::addCodeToMergeInOperand(const Record *R,
   }
 
   std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(OpIdx);
-  const std::string EncoderMethodName =
-      CGI.Operands[SO.first].EncoderMethodNames[SO.second].str();
+  StringRef EncoderMethodName =
+      CGI.Operands[SO.first].EncoderMethodNames[SO.second];
 
   if (UseAPInt)
     Case += "      op.clearAllBits();\n";
@@ -152,13 +152,14 @@ bool CodeEmitterGen::addCodeToMergeInOperand(const Record *R,
 
   // If the source operand has a custom encoder, use it.
   if (!EncoderMethodName.empty()) {
+    raw_string_ostream CaseOS(Case);
+    CaseOS << indent(6);
     if (UseAPInt) {
-      Case += "      " + EncoderMethodName + "(MI, " + utostr(OpIdx);
-      Case += ", op";
+      CaseOS << EncoderMethodName << "(MI, " + utostr(OpIdx) << ", op";
     } else {
-      Case += "      op = " + EncoderMethodName + "(MI, " + utostr(OpIdx);
+      CaseOS << "op = " << EncoderMethodName << "(MI, " << utostr(OpIdx);
     }
-    Case += ", Fixups, STI);\n";
+    CaseOS << ", Fixups, STI);\n";
   } else {
     if (UseAPInt) {
       Case +=



More information about the llvm-commits mailing list