[llvm] [TargetLowering][COFF] Allow GlobalAlias in lowerRelativeReference (PR #220674)

Andy Lim via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 14 11:11:47 PDT 2026


https://github.com/andysjlim updated https://github.com/llvm/llvm-project/pull/220674

>From b8bb0abf7d9e113a65e57fb23e22aa8927e236c4 Mon Sep 17 00:00:00 2001
From: Andrew Lim <andysjlim at chromium.org>
Date: Wed, 2 Sep 2026 17:44:07 +0000
Subject: [PATCH 1/2] [TargetLowering][COFF] Allow GlobalAlias in
 lowerRelativeReference In commit 2b321e2f293a (#203171),
 splitAndWriteThinLTOBitcode was updated to create a GlobalAlias with external
 linkage for promoted internal globals instead of renaming them directly. When
 generating COFF object files on Windows (such as MSVC RTTI
 CompleteObjectLocator structures), relative references against @__ImageBase
 (e.g. sub (ptrtoint @alias to i64), (ptrtoint @__ImageBase to i64)) are
 lowered to IMAGE_REL_AMD64_ADDR32NB (VK_COFF_IMGREL32) relocations in
 TargetLoweringObjectFileCOFF::lowerRelativeReference. which rejected
 GlobalAlias instances. As a result, lowerRelativeReference returned nullptr,
 and AsmPrinter fell back to emitting a raw MC subtraction expression. This
 caused WinCOFFObjectWriter to fail with:   "error: symbol '__ImageBase' can
 not be undefined in a subtraction expression" because @__ImageBase is an
 undefined external symbol resolved at link time. aliasee is a GlobalObject to
 be lowered to VK_COFF_IMGREL32.

---
 llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 2 +-
 llvm/test/MC/COFF/ir-to-imgrel.ll                 | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 4643ef995afec..a5a4174e4af91 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -2125,7 +2125,7 @@ const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference(
   // defined.
   //
   // It should look something like this: @__ImageBase = external constant i8
-  if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
+  if (!LHS->getAliaseeObject() || !isa<GlobalVariable>(RHS) ||
       LHS->isThreadLocal() || RHS->isThreadLocal() ||
       RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
       cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
diff --git a/llvm/test/MC/COFF/ir-to-imgrel.ll b/llvm/test/MC/COFF/ir-to-imgrel.ll
index c087f02213d22..b195b3577e115 100644
--- a/llvm/test/MC/COFF/ir-to-imgrel.ll
+++ b/llvm/test/MC/COFF/ir-to-imgrel.ll
@@ -9,3 +9,10 @@ declare void @f()
 
 ; X64: .quad   f at IMGREL
 @fp = global i64 sub nsw (i64 ptrtoint (ptr @f to i64), i64 ptrtoint (ptr @__ImageBase to i64)), align 8
+
+ at target = internal global i32 42
+ at alias = hidden alias i32, ptr @target
+
+; X64: .quad   alias at IMGREL
+ at alias_ref = global i64 sub nsw (i64 ptrtoint (ptr @alias to i64), i64 ptrtoint (ptr @__ImageBase to i64)), align 8
+

>From 529b61e49d15d615b5e4b5ed8b72641775335c95 Mon Sep 17 00:00:00 2001
From: Andrew Lim <andysjlim at chromium.org>
Date: Mon, 14 Sep 2026 18:10:35 +0000
Subject: [PATCH 2/2] [TargetLowering][COFF] Address review comments: check
 dso_local and direct aliases

---
 .../CodeGen/TargetLoweringObjectFileImpl.cpp  | 12 +++++--
 llvm/test/MC/COFF/ir-to-imgrel.ll             | 31 +++++++++++++++----
 2 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index a5a4174e4af91..065f84f6b9fca 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -2118,15 +2118,21 @@ const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference(
       RHS->getType()->getPointerAddressSpace() != 0)
     return nullptr;
 
+  const auto *GA = dyn_cast<GlobalAlias>(LHS);
+  const GlobalObject *GO =
+      GA ? dyn_cast_or_null<GlobalObject>(GA->getAliasee())
+         : dyn_cast<GlobalObject>(LHS);
+
   // Both ptrtoint instructions must wrap global objects:
-  // - Only global variables are eligible for image relative relocations.
+  // - Only dso_local global variables/functions (or direct aliases thereof) are
+  //   eligible for image relative relocations.
   // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
   // We expect __ImageBase to be a global variable without a section, externally
   // defined.
   //
   // It should look something like this: @__ImageBase = external constant i8
-  if (!LHS->getAliaseeObject() || !isa<GlobalVariable>(RHS) ||
-      LHS->isThreadLocal() || RHS->isThreadLocal() ||
+  if (!GO || !TM.shouldAssumeDSOLocal(LHS) || GO->isThreadLocal() ||
+      !isa<GlobalVariable>(RHS) || RHS->isThreadLocal() ||
       RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
       cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
     return nullptr;
diff --git a/llvm/test/MC/COFF/ir-to-imgrel.ll b/llvm/test/MC/COFF/ir-to-imgrel.ll
index b195b3577e115..1e10f2c8731dc 100644
--- a/llvm/test/MC/COFF/ir-to-imgrel.ll
+++ b/llvm/test/MC/COFF/ir-to-imgrel.ll
@@ -1,18 +1,37 @@
 ; RUN: llc -mtriple=x86_64-pc-win32 %s -o - | FileCheck %s --check-prefix=X64
+; RUN: llc -mtriple=x86_64-pc-win32 -filetype=obj %s -o - | llvm-readobj -r - | FileCheck %s --check-prefix=RELOC
 
 @__ImageBase = external global i8
 
-; X64: .quad   "?x@@3HA"@IMGREL
-@"\01?x@@3HA" = global i64 sub nsw (i64 ptrtoint (ptr @"\01?x@@3HA" to i64), i64 ptrtoint (ptr @__ImageBase to i64)), align 8
+; X64: .long   "?x@@3HA"@IMGREL
+; RELOC: IMAGE_REL_AMD64_ADDR32NB ?x@@3HA
+@"\01?x@@3HA" = global i32 trunc (i64 sub nsw (i64 ptrtoint (ptr @"\01?x@@3HA" to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32), align 4
 
 declare void @f()
 
-; X64: .quad   f at IMGREL
- at fp = global i64 sub nsw (i64 ptrtoint (ptr @f to i64), i64 ptrtoint (ptr @__ImageBase to i64)), align 8
+; X64: .long   f at IMGREL
+; RELOC: IMAGE_REL_AMD64_ADDR32NB f
+ at fp = global i32 trunc (i64 sub nsw (i64 ptrtoint (ptr @f to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32), align 4
 
 @target = internal global i32 42
 @alias = hidden alias i32, ptr @target
 
-; X64: .quad   alias at IMGREL
- at alias_ref = global i64 sub nsw (i64 ptrtoint (ptr @alias to i64), i64 ptrtoint (ptr @__ImageBase to i64)), align 8
+; X64: .long   alias at IMGREL
+; RELOC: IMAGE_REL_AMD64_ADDR32NB alias
+ at alias_ref = global i32 trunc (i64 sub nsw (i64 ptrtoint (ptr @alias to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32), align 4
+
+define internal void @func() {
+  ret void
+}
+ at func_alias = hidden alias void (), ptr @func
+
+; X64: .long   func_alias at IMGREL
+; RELOC: IMAGE_REL_AMD64_ADDR32NB func_alias
+ at func_alias_ref = global i32 trunc (i64 sub nsw (i64 ptrtoint (ptr @func_alias to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32), align 4
+
+; X64: .long   alias at IMGREL+4
+; RELOC: IMAGE_REL_AMD64_ADDR32NB alias
+ at alias_addend_ref = global i32 trunc (i64 sub nsw (i64 ptrtoint (ptr getelementptr (i32, ptr @alias, i32 1) to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32), align 4
+
+
 



More information about the llvm-commits mailing list