[llvm] 16890e0 - [GlobalOpt] Check stored once value's type before setting global initializer

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 17 14:39:18 PDT 2021


Author: Arthur Eubanks
Date: 2021-08-17T14:34:29-07:00
New Revision: 16890e004085454b573d81cce9734b74175ac0b0

URL: https://github.com/llvm/llvm-project/commit/16890e004085454b573d81cce9734b74175ac0b0
DIFF: https://github.com/llvm/llvm-project/commit/16890e004085454b573d81cce9734b74175ac0b0.diff

LOG: [GlobalOpt] Check stored once value's type before setting global initializer

In the provided test case, we were trying to set the global's
initializer to `i32* null` when the global's value type was `@0`.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D108232

Added: 
    llvm/test/Transforms/GlobalOpt/stored-once-value-type.ll

Modified: 
    llvm/lib/Transforms/IPO/GlobalOpt.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index 5904277fa6a60..7fa819ec08ad3 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -1610,7 +1610,8 @@ processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS,
     // initializer to be the stored value, then delete all stores to the
     // global.  This allows us to mark it constant.
     if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
-      if (isa<UndefValue>(GV->getInitializer())) {
+      if (SOVConstant->getType() == GV->getValueType() &&
+          isa<UndefValue>(GV->getInitializer())) {
         // Change the initial value here.
         GV->setInitializer(SOVConstant);
 

diff  --git a/llvm/test/Transforms/GlobalOpt/stored-once-value-type.ll b/llvm/test/Transforms/GlobalOpt/stored-once-value-type.ll
new file mode 100644
index 0000000000000..c4d5b46ccd332
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/stored-once-value-type.ll
@@ -0,0 +1,22 @@
+; RUN: opt -passes=globalopt < %s -S | FileCheck %s
+
+; Check that we don't try to set a global initializer to a value of a 
diff erent type.
+; In this case, we were trying to set @0's initializer to be i32* null.
+
+%0 = type { i32* }
+
+ at 0 = internal global %0* null
+; CHECK: global %0 undef
+
+define void @a() {
+  %1 = tail call i8* @_Znwm(i64 8)
+  %2 = bitcast i8* %1 to %0*
+  %3 = getelementptr inbounds %0, %0* %2, i64 0, i32 0
+  store i32* null, i32** %3, align 8
+  store i8* %1, i8** bitcast (%0** @0 to i8**), align 8
+  %4 = load i64*, i64** bitcast (%0** @0 to i64**), align 8
+  %5 = load atomic i64, i64* %4 acquire, align 8
+  ret void
+}
+
+declare i8* @_Znwm(i64)


        


More information about the llvm-commits mailing list