[llvm] Account for inline assembly instructions in inlining cost. (PR #146628)

David Li via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 2 11:42:42 PDT 2025


================
@@ -777,6 +786,42 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
 
     addCost(SwitchCost);
   }
+
+  // Parses the inline assembly argument to account for its cost. Inline
+  // assembly instructions incur higher costs for inlining since they cannot be
+  // analyzed and optimized.
+  void onInlineAsm(InlineAsm &Arg) override {
+    SmallVector<StringRef, 4> Fragments;
+    Arg.getAsmString().split(Fragments, "\n");
+    int SectionLevel = 0;
+    int InlineAsmInstrCount = 0;
+    for (const auto &Fragment : Fragments) {
+      // Trim whitespaces and comments.
+      auto Trimmed = Fragment.trim();
+      size_t hashPos = Trimmed.find('#');
+      if (hashPos != StringRef::npos)
+        Trimmed = Trimmed.substr(0, hashPos);
+      // Ignore comments.
+      if (Trimmed.empty())
+        continue;
+      if (Trimmed.starts_with(".pushsection")) {
+        ++SectionLevel;
+        continue;
+      }
+      if (Trimmed.starts_with(".popsection")) {
+        --SectionLevel;
+        continue;
+      }
+      // Ignore directives and labels.
+      if (Trimmed.starts_with(".") || Trimmed.contains(":"))
+        continue;
+      if (SectionLevel == 0)
+        ++InlineAsmInstrCount;
----------------
david-xl wrote:

It can be for instructions in another text section (like .text.unlikely).  Skipping data or text.unlikely sections makes sense, but in general it may not (as there will be code duplication in the destination section too).

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


More information about the llvm-commits mailing list