[PATCH] D123160: [AArch64] Limit folded address offsets to 2^20 for COFF

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 5 14:49:32 PDT 2022


mstorsjo created this revision.
mstorsjo added a reviewer: efriedma.
Herald added subscribers: hiraditya, kristof.beyls.
Herald added a project: All.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

In COFF, the immediates in IMAGE_REL_ARM64_PAGEBASE_REL21 relocations
are limited to 21 bit signed, i.e. the offset has to be less than
(1 << 20).

This fixes issue https://github.com/llvm/llvm-project/issues/54753.

Posting this for initial comments on the approach - I didn't add
tests for this yet; will do after a first round of comments.

Do you agree we should make this dependent on the target OS (or
more precisely, object file format), or should we just reduce the
universal limit and keep it object file format agnostic?


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123160

Files:
  llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
  llvm/lib/Target/AArch64/GISel/AArch64PreLegalizerCombiner.cpp


Index: llvm/lib/Target/AArch64/GISel/AArch64PreLegalizerCombiner.cpp
===================================================================
--- llvm/lib/Target/AArch64/GISel/AArch64PreLegalizerCombiner.cpp
+++ llvm/lib/Target/AArch64/GISel/AArch64PreLegalizerCombiner.cpp
@@ -161,15 +161,19 @@
   if (NewOffset <= CurrOffset)
     return false;
 
+  uint64_t OffsetLimit = (1 << 21);
+  if (MF.getSubtarget<AArch64Subtarget>().isTargetWindows())
+    OffsetLimit = (1 << 20);
+
   // Check whether folding this offset is legal. It must not go out of bounds of
   // the referenced object to avoid violating the code model, and must be
-  // smaller than 2^21 because this is the largest offset expressible in all
-  // object formats.
+  // smaller than OffsetLimit because this is the largest offset expressible in
+  // the current object format.
   //
   // This check also prevents us from folding negative offsets, which will end
   // up being treated in the same way as large positive ones. They could also
   // cause code model violations, and aren't really common enough to matter.
-  if (NewOffset >= (1 << 21))
+  if (NewOffset >= OffsetLimit)
     return false;
 
   Type *T = GV->getValueType();
Index: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
===================================================================
--- llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -17918,15 +17918,18 @@
   if (Offset <= uint64_t(GN->getOffset()))
     return SDValue();
 
+  uint64_t OffsetLimit = (1 << 21);
+  if (Subtarget->isTargetWindows())
+    OffsetLimit = (1 << 20);
   // Check whether folding this offset is legal. It must not go out of bounds of
   // the referenced object to avoid violating the code model, and must be
-  // smaller than 2^21 because this is the largest offset expressible in all
-  // object formats.
+  // smaller than OffsetLimit because this is the largest offset expressible in
+  // the current object format.
   //
   // This check also prevents us from folding negative offsets, which will end
   // up being treated in the same way as large positive ones. They could also
   // cause code model violations, and aren't really common enough to matter.
-  if (Offset >= (1 << 21))
+  if (Offset >= OffsetLimit)
     return SDValue();
 
   const GlobalValue *GV = GN->getGlobal();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123160.420643.patch
Type: text/x-patch
Size: 2374 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220405/591dc197/attachment.bin>


More information about the llvm-commits mailing list