[PATCH] D76114: [MC] Recalculate fragment offsets after relaxation

Jian Cai via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 12 19:04:11 PDT 2020


jcai19 created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
jcai19 added a reviewer: jyknight.
jcai19 added subscribers: MaskRay, nickdesaulniers, manojgupta, llozano.
jcai19 updated this revision to Diff 250118.
jcai19 added a comment.

Attach bug id.


While finalizing the layout of sections, relaxation of a fragment may
change the size of other fragments depending on it. Therefore, force to
calculate the offsets of fragments each time relaxation happens.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76114

Files:
  llvm/lib/MC/MCAssembler.cpp
  llvm/test/MC/X86/relax-offset.s


Index: llvm/test/MC/X86/relax-offset.s
===================================================================
--- /dev/null
+++ llvm/test/MC/X86/relax-offset.s
@@ -0,0 +1,15 @@
+# RUN: llvm-mc -triple i386-unknown-unknown %s -filetype=obj -o /dev/null 2>&1 | FileCheck --allow-empty %s
+
+.macro ALTERNATIVE newinstr
+ .skip 144f-143f,0x0
+ .pushsection .altinstr_replacement,"ax"
+143:
+ \newinstr
+144:
+ .popsection
+.endm
+
+ .section .entry.text, "ax"
+  ALTERNATIVE "jmp .Lint80_keep_stack"
+.Lint80_keep_stack:
+# CHECK-NOT: error: Assertion `OS.tell() - Start == Layout.getSectionAddressSize(Sec)' failed
Index: llvm/lib/MC/MCAssembler.cpp
===================================================================
--- llvm/lib/MC/MCAssembler.cpp
+++ llvm/lib/MC/MCAssembler.cpp
@@ -389,14 +389,15 @@
 void MCAsmLayout::layoutFragment(MCFragment *F) {
   MCFragment *Prev = F->getPrevNode();
 
-  // We should never try to recompute something which is valid.
-  assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
   // We should never try to compute the fragment layout if its predecessor
   // isn't valid.
   assert((!Prev || isFragmentValid(Prev)) &&
          "Attempt to compute fragment before its predecessor!");
 
-  ++stats::FragmentLayouts;
+  // We may need to recompute the offset of valid framents if relaxation changes
+  // the offset of some fragments. PR#45190
+  if (!isFragmentValid(F))
+    ++stats::FragmentLayouts;
 
   // Compute fragment offset and size.
   if (Prev)
@@ -785,9 +786,14 @@
   }
 
   // Layout until everything fits.
-  while (layoutOnce(Layout))
+  while (layoutOnce(Layout)) {
     if (getContext().hadError())
       return;
+    // recalculate the offsets of fragments if relaxation happens. PR#45190
+    for (MCSection &Sec : *this)
+      for (MCFragment &F : Sec)
+        Layout.layoutFragment(&F);
+  }
 
   DEBUG_WITH_TYPE("mc-dump", {
       errs() << "assembler backend - post-relaxation\n--\n";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76114.250118.patch
Type: text/x-patch
Size: 1968 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200313/d278f538/attachment.bin>


More information about the llvm-commits mailing list