[llvm] GlobalOpt: Use the correct address space when creating a "*.init" global. (PR #118562)

Owen Anderson via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 3 16:02:43 PST 2024


https://github.com/resistor created https://github.com/llvm/llvm-project/pull/118562

None

>From 560635a2b598cecd1566f41b83f5aa3e3148ec8b Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Wed, 4 Dec 2024 13:00:42 +1300
Subject: [PATCH] GlobalOpt: Use the correct address space when creating a
 "*.init" global.

---
 llvm/lib/Transforms/IPO/GlobalOpt.cpp         |  3 +-
 .../GlobalOpt/malloc-promote-addrspace.ll     | 60 +++++++++++++++++++
 2 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/GlobalOpt/malloc-promote-addrspace.ll

diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index 3381b5f77683b5..72d332ee316ca3 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -950,7 +950,8 @@ OptimizeGlobalAddressOfAllocation(GlobalVariable *GV, CallInst *CI,
     new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,
                        GlobalValue::InternalLinkage,
                        ConstantInt::getFalse(GV->getContext()),
-                       GV->getName()+".init", GV->getThreadLocalMode());
+                       GV->getName()+".init", GV->getThreadLocalMode(),
+                       GV->getType()->getAddressSpace());
   bool InitBoolUsed = false;
 
   // Loop over all instruction uses of GV, processing them in turn.
diff --git a/llvm/test/Transforms/GlobalOpt/malloc-promote-addrspace.ll b/llvm/test/Transforms/GlobalOpt/malloc-promote-addrspace.ll
new file mode 100644
index 00000000000000..054c60bd235bd4
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/malloc-promote-addrspace.ll
@@ -0,0 +1,60 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-globals
+; RUN: opt -S -passes=globalopt -o - < %s | FileCheck %s
+
+ at g = internal addrspace(200) global ptr null, align 8
+
+;.
+; CHECK: @g.init = internal unnamed_addr addrspace(200) global i1 false
+;.
+define internal i32 @f1() {
+; CHECK-LABEL: define {{[^@]+}}@f1() unnamed_addr {
+; CHECK-NEXT:    [[G_INIT_VAL:%.*]] = load i1, ptr addrspace(200) @g.init, align 1
+; CHECK-NEXT:    call fastcc void @f2()
+; CHECK-NEXT:    [[NOTINIT:%.*]] = xor i1 [[G_INIT_VAL]], true
+; CHECK-NEXT:    br i1 [[NOTINIT]], label [[TMP1:%.*]], label [[TMP2:%.*]]
+; CHECK:       1:
+; CHECK-NEXT:    br label [[TMP3:%.*]]
+; CHECK:       2:
+; CHECK-NEXT:    br label [[TMP3]]
+; CHECK:       3:
+; CHECK-NEXT:    [[TMP4:%.*]] = phi i32 [ -1, [[TMP1]] ], [ 1, [[TMP2]] ]
+; CHECK-NEXT:    ret i32 [[TMP4]]
+;
+  %1 = load ptr addrspace(200), ptr addrspace(200) @g, align 8
+  call void @f2();
+  %2 = icmp eq ptr addrspace(200) %1, null
+  br i1 %2, label %3, label %4
+
+3:                                          ; preds = %0
+  br label %5
+
+4:                                          ; preds = %0
+  br label %5
+
+5:                                          ; preds = %3, %4
+  %6 = phi i32 [ -1, %3 ], [ 1, %4 ]
+  ret i32 %6
+}
+
+define internal void @f2() {
+; CHECK-LABEL: define {{[^@]+}}@f2() unnamed_addr {
+; CHECK-NEXT:    store i1 true, ptr addrspace(200) @g.init, align 1
+; CHECK-NEXT:    ret void
+;
+  %1 = call noalias ptr @malloc(i64 4)
+  store ptr %1, ptr addrspace(200) @g, align 8
+  ret void
+}
+
+define dso_local i32 @main() {
+; CHECK-LABEL: define {{[^@]+}}@main() local_unnamed_addr {
+; CHECK-NEXT:    store i1 false, ptr addrspace(200) @g.init, align 1
+; CHECK-NEXT:    [[TMP1:%.*]] = call fastcc i32 @f1()
+; CHECK-NEXT:    ret i32 [[TMP1]]
+;
+  store ptr null, ptr addrspace(200) @g, align 8
+  %1 = call i32 @f1()
+  ret i32 %1
+}
+
+declare dso_local noalias ptr @malloc(i64) allockind("alloc,uninitialized") allocsize(0)



More information about the llvm-commits mailing list