From: Tim Northover Date: Tue, 14 May 2019 11:03:13 +0000 (+0000) Subject: GlobalOpt: do not promote globals used atomically to constants. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0f7b4d78118dcf0b341db0486cdfb9fdd77abf85;p=llvm GlobalOpt: do not promote globals used atomically to constants. Some atomic loads are implemented as cmpxchg (particularly if large or floating), and that usually requires write access to the memory involved or it will segfault. We can still propagate the constant value to users we understand though. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360662 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index 2a869444d6e..ca0e5807b5f 100644 --- a/lib/Transforms/IPO/GlobalOpt.cpp +++ b/lib/Transforms/IPO/GlobalOpt.cpp @@ -1980,7 +1980,12 @@ static bool processInternalGlobal( } if (GS.StoredType <= GlobalStatus::InitializerStored) { LLVM_DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n"); - GV->setConstant(true); + + // Don't actually mark a global constant if it's atomic because atomic loads + // are implemented by a trivial cmpxchg in some edge-cases and that usually + // requires write access to the variable even if it's not actually changed. + if (GS.Ordering == AtomicOrdering::NotAtomic) + GV->setConstant(true); // Clean up any obviously simplifiable users now. CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI); diff --git a/test/Transforms/GlobalOpt/atomic.ll b/test/Transforms/GlobalOpt/atomic.ll index 563c1fec7d2..7597e0f03ba 100644 --- a/test/Transforms/GlobalOpt/atomic.ll +++ b/test/Transforms/GlobalOpt/atomic.ll @@ -3,7 +3,7 @@ @GV1 = internal global i64 1 @GV2 = internal global i32 0 -; CHECK: @GV1 = internal unnamed_addr constant i64 1 +; CHECK: @GV1 = internal unnamed_addr global i64 1 ; CHECK: @GV2 = internal unnamed_addr global i32 0 define void @test1() { @@ -23,3 +23,12 @@ entry: %atomic-load = load atomic i32, i32* @GV2 seq_cst, align 4 ret i32 %atomic-load } + + +define i64 @test3() { +; CHECK-LABEL: @test3 +; CHECK: ret i64 1 + + %val = load atomic i64, i64* @GV1 acquire, align 8 + ret i64 %val +}