[llvm] [TargetLowering][COFF] Allow GlobalAlias in lowerRelativeReference (PR #220674)
Andy Lim via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 16 10:57:33 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/3] [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/3] [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
+
+
>From 05d095af33e91a2daf9571160b46874662fe152c Mon Sep 17 00:00:00 2001
From: Andrew Lim <andysjlim at chromium.org>
Date: Wed, 16 Sep 2026 15:38:15 +0000
Subject: [PATCH 3/3] [TargetLowering][COFF] Address review comments: add
negative tests, verify immediate encoding, and clang-format
---
.../CodeGen/TargetLoweringObjectFileImpl.cpp | 11 +++--
llvm/test/MC/COFF/ir-to-imgrel-reject.ll | 45 +++++++++++++++++++
llvm/test/MC/COFF/ir-to-imgrel.ll | 9 ++--
3 files changed, 56 insertions(+), 9 deletions(-)
create mode 100644 llvm/test/MC/COFF/ir-to-imgrel-reject.ll
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index caf32e1c3fc68..2756ea19fb7f6 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -2119,16 +2119,15 @@ const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference(
return nullptr;
const auto *GA = dyn_cast<GlobalAlias>(LHS);
- const GlobalObject *GO =
- GA ? dyn_cast_or_null<GlobalObject>(GA->getAliasee())
- : dyn_cast<GlobalObject>(LHS);
+ const GlobalObject *GO = GA ? dyn_cast_or_null<GlobalObject>(GA->getAliasee())
+ : dyn_cast<GlobalObject>(LHS);
// Both ptrtoint instructions must wrap global objects:
// - 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.
+ // - 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 (!GO || !TM.shouldAssumeDSOLocal(LHS) || GO->isThreadLocal() ||
diff --git a/llvm/test/MC/COFF/ir-to-imgrel-reject.ll b/llvm/test/MC/COFF/ir-to-imgrel-reject.ll
new file mode 100644
index 0000000000000..c7a7f79928488
--- /dev/null
+++ b/llvm/test/MC/COFF/ir-to-imgrel-reject.ll
@@ -0,0 +1,45 @@
+; RUN: llc -mtriple=x86_64-pc-win32 %s -o - | FileCheck %s --implicit-check-not="@IMGREL"
+; RUN: not llc -mtriple=x86_64-pc-win32 -filetype=obj %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR
+
+; Test that we reject forms of aliases that are not direct aliases to a GlobalObject
+; (e.g. aliases with GEP/offsets, chained aliases, or aliases to thread-local globals),
+; as well as non-dso_local targets (such as dllimport).
+; When rejected, lowerRelativeReference returns nullptr, so the reference falls back
+; to a raw subtraction (symbol - __ImageBase) in text assembly, and fails object emission
+; because __ImageBase cannot be undefined in a subtraction expression in COFF.
+
+ at __ImageBase = external global i8
+
+ at target = internal global i32 42
+ at alias = hidden alias i32, ptr @target
+
+ at alias_gep = hidden alias i32, getelementptr (i32, ptr @target, i32 1)
+ at alias_to_alias = hidden alias i32, ptr @alias
+
+ at target_tls = thread_local global i32 42
+ at alias_tls = hidden alias i32, ptr @target_tls
+
+ at imported_var = external dllimport global i32
+
+; Rejected: aliasee has an offset (GEP)
+; CHECK: alias_gep_ref:
+; CHECK-NEXT: .long alias_gep-__ImageBase
+ at alias_gep_ref = global i32 trunc (i64 sub nsw (i64 ptrtoint (ptr @alias_gep to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32), align 4
+
+; Rejected: aliasee is another alias (chained alias)
+; CHECK: alias_to_alias_ref:
+; CHECK-NEXT: .long alias_to_alias-__ImageBase
+ at alias_to_alias_ref = global i32 trunc (i64 sub nsw (i64 ptrtoint (ptr @alias_to_alias to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32), align 4
+
+; Rejected: aliasee is thread-local
+; CHECK: alias_tls_ref:
+; CHECK-NEXT: .long alias_tls-__ImageBase
+ at alias_tls_ref = global i32 trunc (i64 sub nsw (i64 ptrtoint (ptr @alias_tls to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32), align 4
+
+; Rejected: dllimport variable is not dso_local
+; CHECK: imported_var_ref:
+; CHECK-NEXT: .long imported_var-__ImageBase
+ at imported_var_ref = global i32 trunc (i64 sub nsw (i64 ptrtoint (ptr @imported_var to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32), align 4
+
+; ERR: error: symbol '__ImageBase' can not be undefined in a subtraction expression
+
diff --git a/llvm/test/MC/COFF/ir-to-imgrel.ll b/llvm/test/MC/COFF/ir-to-imgrel.ll
index 1e10f2c8731dc..7c313933071d8 100644
--- a/llvm/test/MC/COFF/ir-to-imgrel.ll
+++ b/llvm/test/MC/COFF/ir-to-imgrel.ll
@@ -1,5 +1,6 @@
; 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
+; RUN: llc -mtriple=x86_64-pc-win32 -filetype=obj %s -o - | llvm-readobj -S --section-data - | FileCheck %s --check-prefix=DATA
@__ImageBase = external global i8
@@ -31,7 +32,9 @@ define internal void @func() {
; X64: .long alias at IMGREL+4
; RELOC: IMAGE_REL_AMD64_ADDR32NB alias
+; DATA: Name: .data
+; DATA: SectionData (
+; DATA-NEXT: 0000: {{[0-9A-F ]+}}
+; DATA-NEXT: 0010: 00000000 04000000 |
+; DATA-NEXT: )
@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