Skip to content

GlobalOpt: Use the correct address space when creating a "*.init" global. #118562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 4, 2024

Conversation

resistor
Copy link
Collaborator

@resistor resistor commented Dec 4, 2024

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Dec 4, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Owen Anderson (resistor)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/118562.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/GlobalOpt.cpp (+2-1)
  • (added) llvm/test/Transforms/GlobalOpt/malloc-promote-addrspace.ll (+60)
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
+
+@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)

Copy link

github-actions bot commented Dec 4, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@resistor resistor merged commit 14a259f into llvm:main Dec 4, 2024
8 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 4, 2024

LLVM Buildbot has detected a new failure on builder libc-x86_64-debian-fullbuild-dbg-asan running on libc-x86_64-debian-fullbuild while building llvm at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/171/builds/11553

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure)
...
[       OK ] LlvmLibcRemapFilePagesTest.ErrorInvalidAddress (5 us)
Ran 3 tests.  PASS: 3  FAIL: 0
[939/1099] Running unit test libc.test.src.sys.mman.linux.shm_test
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcShmTest.Basic
[       OK ] LlvmLibcShmTest.Basic (192 us)
[ RUN      ] LlvmLibcShmTest.NameConversion
[       OK ] LlvmLibcShmTest.NameConversion (35 us)
Ran 2 tests.  PASS: 2  FAIL: 0
[940/1099] Running unit test libc.test.src.sys.mman.linux.process_mrelease_test
FAILED: projects/libc/test/src/sys/mman/linux/CMakeFiles/libc.test.src.sys.mman.linux.process_mrelease_test /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/projects/libc/test/src/sys/mman/linux/CMakeFiles/libc.test.src.sys.mman.linux.process_mrelease_test 
cd /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/projects/libc/test/src/sys/mman/linux && /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/projects/libc/test/src/sys/mman/linux/libc.test.src.sys.mman.linux.process_mrelease_test.__build__
[==========] Running 3 tests from 1 test suite.
[ RUN      ] LlvmLibcProcessMReleaseTest.NoError
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/llvm-project/libc/test/src/sys/mman/linux/process_mrelease_test.cpp:44: FAILURE
Failed to match LIBC_NAMESPACE::process_mrelease(pidfd, 0) against Succeeds().
Expected return value to be equal to 0 but got -1.
Expected errno to be equal to "Success" but got "No such process".
[  FAILED  ] LlvmLibcProcessMReleaseTest.NoError
[ RUN      ] LlvmLibcProcessMReleaseTest.ErrorNotKilled
[       OK ] LlvmLibcProcessMReleaseTest.ErrorNotKilled (960 us)
[ RUN      ] LlvmLibcProcessMReleaseTest.ErrorNonExistingPidfd
[       OK ] LlvmLibcProcessMReleaseTest.ErrorNonExistingPidfd (14 us)
Ran 3 tests.  PASS: 2  FAIL: 1
[941/1099] Running unit test libc.test.src.sys.random.linux.getrandom_test
[==========] Running 4 tests from 1 test suite.
[ RUN      ] LlvmLibcGetRandomTest.InvalidFlag
[       OK ] LlvmLibcGetRandomTest.InvalidFlag (14 us)
[ RUN      ] LlvmLibcGetRandomTest.InvalidBuffer
[       OK ] LlvmLibcGetRandomTest.InvalidBuffer (8 us)
[ RUN      ] LlvmLibcGetRandomTest.ReturnsSize
[       OK ] LlvmLibcGetRandomTest.ReturnsSize (22 us)
[ RUN      ] LlvmLibcGetRandomTest.CheckValue
[       OK ] LlvmLibcGetRandomTest.CheckValue (5 us)
Ran 4 tests.  PASS: 4  FAIL: 0
[942/1099] Running unit test libc.test.src.sys.random.linux.getrandom_test.__NO_FMA_OPT.__NO_MISC_MATH_BASIC_OPS_OPT
[==========] Running 4 tests from 1 test suite.
[ RUN      ] LlvmLibcGetRandomTest.InvalidFlag
[       OK ] LlvmLibcGetRandomTest.InvalidFlag (5 us)
[ RUN      ] LlvmLibcGetRandomTest.InvalidBuffer
[       OK ] LlvmLibcGetRandomTest.InvalidBuffer (8 us)
[ RUN      ] LlvmLibcGetRandomTest.ReturnsSize
[       OK ] LlvmLibcGetRandomTest.ReturnsSize (24 us)
[ RUN      ] LlvmLibcGetRandomTest.CheckValue
[       OK ] LlvmLibcGetRandomTest.CheckValue (11 us)
Ran 4 tests.  PASS: 4  FAIL: 0
[943/1099] Running unit test libc.test.src.sys.resource.getrlimit_setrlimit_test
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcResourceLimitsTest.SetNoFileLimit
Step 8 (libc-unit-tests) failure: libc-unit-tests (failure)
...
[       OK ] LlvmLibcRemapFilePagesTest.ErrorInvalidAddress (5 us)
Ran 3 tests.  PASS: 3  FAIL: 0
[939/1099] Running unit test libc.test.src.sys.mman.linux.shm_test
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcShmTest.Basic
[       OK ] LlvmLibcShmTest.Basic (192 us)
[ RUN      ] LlvmLibcShmTest.NameConversion
[       OK ] LlvmLibcShmTest.NameConversion (35 us)
Ran 2 tests.  PASS: 2  FAIL: 0
[940/1099] Running unit test libc.test.src.sys.mman.linux.process_mrelease_test
FAILED: projects/libc/test/src/sys/mman/linux/CMakeFiles/libc.test.src.sys.mman.linux.process_mrelease_test /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/projects/libc/test/src/sys/mman/linux/CMakeFiles/libc.test.src.sys.mman.linux.process_mrelease_test 
cd /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/projects/libc/test/src/sys/mman/linux && /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/build/projects/libc/test/src/sys/mman/linux/libc.test.src.sys.mman.linux.process_mrelease_test.__build__
[==========] Running 3 tests from 1 test suite.
[ RUN      ] LlvmLibcProcessMReleaseTest.NoError
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg-asan/llvm-project/libc/test/src/sys/mman/linux/process_mrelease_test.cpp:44: FAILURE
Failed to match LIBC_NAMESPACE::process_mrelease(pidfd, 0) against Succeeds().
Expected return value to be equal to 0 but got -1.
Expected errno to be equal to "Success" but got "No such process".
[  FAILED  ] LlvmLibcProcessMReleaseTest.NoError
[ RUN      ] LlvmLibcProcessMReleaseTest.ErrorNotKilled
[       OK ] LlvmLibcProcessMReleaseTest.ErrorNotKilled (960 us)
[ RUN      ] LlvmLibcProcessMReleaseTest.ErrorNonExistingPidfd
[       OK ] LlvmLibcProcessMReleaseTest.ErrorNonExistingPidfd (14 us)
Ran 3 tests.  PASS: 2  FAIL: 1
[941/1099] Running unit test libc.test.src.sys.random.linux.getrandom_test
[==========] Running 4 tests from 1 test suite.
[ RUN      ] LlvmLibcGetRandomTest.InvalidFlag
[       OK ] LlvmLibcGetRandomTest.InvalidFlag (14 us)
[ RUN      ] LlvmLibcGetRandomTest.InvalidBuffer
[       OK ] LlvmLibcGetRandomTest.InvalidBuffer (8 us)
[ RUN      ] LlvmLibcGetRandomTest.ReturnsSize
[       OK ] LlvmLibcGetRandomTest.ReturnsSize (22 us)
[ RUN      ] LlvmLibcGetRandomTest.CheckValue
[       OK ] LlvmLibcGetRandomTest.CheckValue (5 us)
Ran 4 tests.  PASS: 4  FAIL: 0
[942/1099] Running unit test libc.test.src.sys.random.linux.getrandom_test.__NO_FMA_OPT.__NO_MISC_MATH_BASIC_OPS_OPT
[==========] Running 4 tests from 1 test suite.
[ RUN      ] LlvmLibcGetRandomTest.InvalidFlag
[       OK ] LlvmLibcGetRandomTest.InvalidFlag (5 us)
[ RUN      ] LlvmLibcGetRandomTest.InvalidBuffer
[       OK ] LlvmLibcGetRandomTest.InvalidBuffer (8 us)
[ RUN      ] LlvmLibcGetRandomTest.ReturnsSize
[       OK ] LlvmLibcGetRandomTest.ReturnsSize (24 us)
[ RUN      ] LlvmLibcGetRandomTest.CheckValue
[       OK ] LlvmLibcGetRandomTest.CheckValue (11 us)
Ran 4 tests.  PASS: 4  FAIL: 0
[943/1099] Running unit test libc.test.src.sys.resource.getrlimit_setrlimit_test
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcResourceLimitsTest.SetNoFileLimit

@resistor
Copy link
Collaborator Author

resistor commented Dec 9, 2024

Tagging @jrtc27 and @arichardson for awareness on CHERI-derived PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants