[llvm] Prevent GlobalOpt from folding away addrspacecasts which may be runtime operations. (PR #153753)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 15 01:42:50 PDT 2025


================
@@ -0,0 +1,31 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
+; RUN: opt -passes=globalopt < %s -S | FileCheck %s
+
+; Test that we do not fold away addresscasts when optimizing once-stored
+; globals, as these may be runtime operations.
+
+%T = type { ptr }
+
+ at 0 = internal global ptr null
+ at 1 = internal addrspace(1) global i32 0
+
+;.
+; CHECK: @[[GLOB0:[0-9]+]] = internal unnamed_addr global ptr null
+; CHECK: @[[GLOB1:[0-9]+]] = internal addrspace(1) global i32 0
+;.
+define void @a() {
+; CHECK-LABEL: @a(
+; CHECK-NEXT:    [[TMP1:%.*]] = addrspacecast ptr addrspace(1) @[[GLOB1]] to ptr
----------------
nikic wrote:

The optimization would still work if this were an addrspacecast constant expression, right? Maybe add that as a test variant, as that's how this would actually appear in canonical IR?

https://github.com/llvm/llvm-project/pull/153753


More information about the llvm-commits mailing list