[llvm] Treat ';' and '\n' as assembly instruction separators in collectAsmInstrs (PR #149365)

Rahman Lavaee via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 18 10:06:25 PDT 2025


================
@@ -60,17 +60,41 @@ FunctionType *InlineAsm::getFunctionType() const {
   return FTy;
 }
 
-void InlineAsm::collectAsmStrs(SmallVectorImpl<StringRef> &AsmStrs) const {
+SmallVector<StringRef> InlineAsm::collectAsmInstrs() const {
+  if (AsmString.empty())
+    return {};
   StringRef AsmStr(AsmString);
-  AsmStrs.clear();
-
-  // TODO: 1) Unify delimiter for inline asm, we also meet other delimiters
-  // for example "\0A", ";".
-  // 2) Enhance StringRef. Some of the special delimiter ("\0") can't be
-  // split in StringRef. Also empty StringRef can not call split (will stuck).
-  if (AsmStr.empty())
-    return;
-  AsmStr.split(AsmStrs, "\n\t", -1, false);
+  // First break the assembly string into lines.
+  SmallVector<StringRef, 4> AsmLines;
+  AsmStr.split(AsmLines, '\n', -1, false);
+
+  auto TrimComments = [&](StringRef &AsmLine) {
+    for (auto CommentMarker : {"#", "//"}) {
+      size_t MarkerPos = AsmLine.find(CommentMarker);
+      if (MarkerPos != StringRef::npos)
+        AsmLine = AsmLine.substr(0, MarkerPos);
+    }
+  };
+
+  SmallVector<StringRef, 4> AsmInstrs;
+  AsmInstrs.reserve(AsmLines.size());
+  for (StringRef &AsmLine : AsmLines) {
+    // First remove the comments. Note it's important to do this before breaking
+    // by ';' since the comment portion may include that character too.
+    TrimComments(AsmLine);
----------------
rlavaee wrote:

Great suggestion. I used `AsmLine = AsmLine.split('#').first.split("//").first`

https://github.com/llvm/llvm-project/pull/149365


More information about the llvm-commits mailing list