Skip to content

Commit d9a8d20

Browse files
committed
Revert "[GlobalOpt][FIX] Do not embed initializers into AS!=0 globals"
This reverts commit 7dbba33. There seems to be a problem with the tests, investigating now: https://lab.llvm.org/buildbot/#/builders/61/builds/14574
1 parent 9f844ae commit d9a8d20

File tree

9 files changed

+15
-108
lines changed

9 files changed

+15
-108
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,6 @@ class TargetTransformInfo {
383383

384384
bool isNoopAddrSpaceCast(unsigned FromAS, unsigned ToAS) const;
385385

386-
/// Return true if globals in this address space can have initializers other
387-
/// than `undef`.
388-
bool canHaveNonUndefGlobalInitializerInAddressSpace(unsigned AS) const;
389-
390386
unsigned getAssumedAddrSpace(const Value *V) const;
391387

392388
/// Rewrite intrinsic call \p II such that \p OldV will be replaced with \p
@@ -1461,8 +1457,6 @@ class TargetTransformInfo::Concept {
14611457
virtual bool collectFlatAddressOperands(SmallVectorImpl<int> &OpIndexes,
14621458
Intrinsic::ID IID) const = 0;
14631459
virtual bool isNoopAddrSpaceCast(unsigned FromAS, unsigned ToAS) const = 0;
1464-
virtual bool
1465-
canHaveNonUndefGlobalInitializerInAddressSpace(unsigned AS) const = 0;
14661460
virtual unsigned getAssumedAddrSpace(const Value *V) const = 0;
14671461
virtual Value *rewriteIntrinsicWithAddressSpace(IntrinsicInst *II,
14681462
Value *OldV,
@@ -1788,11 +1782,6 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
17881782
return Impl.isNoopAddrSpaceCast(FromAS, ToAS);
17891783
}
17901784

1791-
bool
1792-
canHaveNonUndefGlobalInitializerInAddressSpace(unsigned AS) const override {
1793-
return Impl.canHaveNonUndefGlobalInitializerInAddressSpace(AS);
1794-
}
1795-
17961785
unsigned getAssumedAddrSpace(const Value *V) const override {
17971786
return Impl.getAssumedAddrSpace(V);
17981787
}

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,6 @@ class TargetTransformInfoImplBase {
105105
}
106106

107107
bool isNoopAddrSpaceCast(unsigned, unsigned) const { return false; }
108-
bool canHaveNonUndefGlobalInitializerInAddressSpace(unsigned AS) const {
109-
return AS == 0;
110-
};
111108

112109
unsigned getAssumedAddrSpace(const Value *V) const { return -1; }
113110

llvm/include/llvm/Transforms/Utils/GlobalStatus.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#ifndef LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
1010
#define LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
1111

12-
#include "llvm/IR/Instructions.h"
1312
#include "llvm/Support/AtomicOrdering.h"
1413

1514
namespace llvm {
@@ -46,7 +45,7 @@ struct GlobalStatus {
4645

4746
/// This global is stored to, but only its initializer and one other value
4847
/// is ever stored to it. If this global isStoredOnce, we track the value
49-
/// stored to it via StoredOnceStore below. This is only tracked for scalar
48+
/// stored to it in StoredOnceValue below. This is only tracked for scalar
5049
/// globals.
5150
StoredOnce,
5251

@@ -56,16 +55,8 @@ struct GlobalStatus {
5655
} StoredType = NotStored;
5756

5857
/// If only one value (besides the initializer constant) is ever stored to
59-
/// this global, keep track of what value it is via the store instruction.
60-
const StoreInst *StoredOnceStore = nullptr;
61-
62-
/// If only one value (besides the initializer constant) is ever stored to
63-
/// this global return the stored value.
64-
Value *getStoredOnceValue() const {
65-
return (StoredType == StoredOnce && StoredOnceStore)
66-
? StoredOnceStore->getOperand(0)
67-
: nullptr;
68-
}
58+
/// this global, keep track of what value it is.
59+
Value *StoredOnceValue = nullptr;
6960

7061
/// These start out null/false. When the first accessing function is noticed,
7162
/// it is recorded. When a second different accessing function is noticed,

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,6 @@ bool TargetTransformInfo::isNoopAddrSpaceCast(unsigned FromAS,
259259
return TTIImpl->isNoopAddrSpaceCast(FromAS, ToAS);
260260
}
261261

262-
bool TargetTransformInfo::canHaveNonUndefGlobalInitializerInAddressSpace(
263-
unsigned AS) const {
264-
return TTIImpl->canHaveNonUndefGlobalInitializerInAddressSpace(AS);
265-
}
266-
267262
unsigned TargetTransformInfo::getAssumedAddrSpace(const Value *V) const {
268263
return TTIImpl->getAssumedAddrSpace(V);
269264
}

llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,6 @@ class GCNTTIImpl final : public BasicTTIImplBase<GCNTTIImpl> {
182182

183183
bool collectFlatAddressOperands(SmallVectorImpl<int> &OpIndexes,
184184
Intrinsic::ID IID) const;
185-
186-
bool canHaveNonUndefGlobalInitializerInAddressSpace(unsigned AS) const {
187-
return AS != AMDGPUAS::LOCAL_ADDRESS && AS != AMDGPUAS::REGION_ADDRESS &&
188-
AS != AMDGPUAS::PRIVATE_ADDRESS;
189-
}
190-
191185
Value *rewriteIntrinsicWithAddressSpace(IntrinsicInst *II, Value *OldV,
192186
Value *NewV) const;
193187

llvm/lib/Target/NVPTX/NVPTXTargetTransformInfo.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ class NVPTXTTIImpl : public BasicTTIImplBase<NVPTXTTIImpl> {
4848
return AddressSpace::ADDRESS_SPACE_GENERIC;
4949
}
5050

51-
bool canHaveNonUndefGlobalInitializerInAddressSpace(unsigned AS) const {
52-
return AS != AddressSpace::ADDRESS_SPACE_SHARED &&
53-
AS != AddressSpace::ADDRESS_SPACE_LOCAL && AS != ADDRESS_SPACE_PARAM;
54-
}
55-
5651
Optional<Instruction *> instCombineIntrinsic(InstCombiner &IC,
5752
IntrinsicInst &II) const;
5853

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,6 @@ static void makeAllConstantUsesInstructions(Constant *C) {
15071507
/// it if possible. If we make a change, return true.
15081508
static bool
15091509
processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS,
1510-
function_ref<TargetTransformInfo &(Function &)> GetTTI,
15111510
function_ref<TargetLibraryInfo &(Function &)> GetTLI,
15121511
function_ref<DominatorTree &(Function &)> LookupDomTree) {
15131512
auto &DL = GV->getParent()->getDataLayout();
@@ -1606,26 +1605,18 @@ processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS,
16061605
if (SRAGlobal(GV, DL))
16071606
return true;
16081607
}
1609-
Value *StoredOnceValue = GS.getStoredOnceValue();
1610-
if (GS.StoredType == GlobalStatus::StoredOnce && StoredOnceValue) {
1608+
if (GS.StoredType == GlobalStatus::StoredOnce && GS.StoredOnceValue) {
16111609
// Avoid speculating constant expressions that might trap (div/rem).
1612-
auto *SOVConstant = dyn_cast<Constant>(StoredOnceValue);
1610+
auto *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue);
16131611
if (SOVConstant && SOVConstant->canTrap())
16141612
return Changed;
16151613

1616-
Function &StoreFn =
1617-
const_cast<Function &>(*GS.StoredOnceStore->getFunction());
16181614
// If the initial value for the global was an undef value, and if only
16191615
// one other value was stored into it, we can just change the
16201616
// initializer to be the stored value, then delete all stores to the
16211617
// global. This allows us to mark it constant.
1622-
// This is restricted to address spaces that allow globals to have
1623-
// initializers. NVPTX, for example, does not support initializers for
1624-
// shared memory (AS 3).
16251618
if (SOVConstant && SOVConstant->getType() == GV->getValueType() &&
1626-
isa<UndefValue>(GV->getInitializer()) &&
1627-
GetTTI(StoreFn).canHaveNonUndefGlobalInitializerInAddressSpace(
1628-
GV->getType()->getAddressSpace())) {
1619+
isa<UndefValue>(GV->getInitializer())) {
16291620
// Change the initial value here.
16301621
GV->setInitializer(SOVConstant);
16311622

@@ -1644,7 +1635,8 @@ processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS,
16441635

16451636
// Try to optimize globals based on the knowledge that only one value
16461637
// (besides its initializer) is ever stored to the global.
1647-
if (optimizeOnceStoredGlobal(GV, StoredOnceValue, GS.Ordering, DL, GetTLI))
1638+
if (optimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GS.Ordering, DL,
1639+
GetTLI))
16481640
return true;
16491641

16501642
// Otherwise, if the global was not a boolean, we can shrink it to be a
@@ -1664,7 +1656,6 @@ processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS,
16641656
/// make a change, return true.
16651657
static bool
16661658
processGlobal(GlobalValue &GV,
1667-
function_ref<TargetTransformInfo &(Function &)> GetTTI,
16681659
function_ref<TargetLibraryInfo &(Function &)> GetTLI,
16691660
function_ref<DominatorTree &(Function &)> LookupDomTree) {
16701661
if (GV.getName().startswith("llvm."))
@@ -1697,8 +1688,7 @@ processGlobal(GlobalValue &GV,
16971688
if (GVar->isConstant() || !GVar->hasInitializer())
16981689
return Changed;
16991690

1700-
return processInternalGlobal(GVar, GS, GetTTI, GetTLI, LookupDomTree) ||
1701-
Changed;
1691+
return processInternalGlobal(GVar, GS, GetTLI, LookupDomTree) || Changed;
17021692
}
17031693

17041694
/// Walk all of the direct calls of the specified function, changing them to
@@ -2001,7 +1991,7 @@ OptimizeFunctions(Module &M,
20011991
}
20021992
}
20031993

2004-
Changed |= processGlobal(*F, GetTTI, GetTLI, LookupDomTree);
1994+
Changed |= processGlobal(*F, GetTLI, LookupDomTree);
20051995

20061996
if (!F->hasLocalLinkage())
20071997
continue;
@@ -2070,7 +2060,6 @@ OptimizeFunctions(Module &M,
20702060

20712061
static bool
20722062
OptimizeGlobalVars(Module &M,
2073-
function_ref<TargetTransformInfo &(Function &)> GetTTI,
20742063
function_ref<TargetLibraryInfo &(Function &)> GetTLI,
20752064
function_ref<DominatorTree &(Function &)> LookupDomTree,
20762065
SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {
@@ -2099,7 +2088,7 @@ OptimizeGlobalVars(Module &M,
20992088
continue;
21002089
}
21012090

2102-
Changed |= processGlobal(*GV, GetTTI, GetTLI, LookupDomTree);
2091+
Changed |= processGlobal(*GV, GetTLI, LookupDomTree);
21032092
}
21042093
return Changed;
21052094
}
@@ -2677,8 +2666,8 @@ static bool optimizeGlobalsInModule(
26772666
});
26782667

26792668
// Optimize non-address-taken globals.
2680-
LocalChange |= OptimizeGlobalVars(M, GetTTI, GetTLI, LookupDomTree,
2681-
NotDiscardableComdats);
2669+
LocalChange |=
2670+
OptimizeGlobalVars(M, GetTLI, LookupDomTree, NotDiscardableComdats);
26822671

26832672
// Resolve aliases, when possible.
26842673
LocalChange |= OptimizeGlobalAliases(M, NotDiscardableComdats);

llvm/lib/Transforms/Utils/GlobalStatus.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
127127
GS.StoredType = GlobalStatus::InitializerStored;
128128
} else if (GS.StoredType < GlobalStatus::StoredOnce) {
129129
GS.StoredType = GlobalStatus::StoredOnce;
130-
GS.StoredOnceStore = SI;
130+
GS.StoredOnceValue = StoredVal;
131131
} else if (GS.StoredType == GlobalStatus::StoredOnce &&
132-
GS.getStoredOnceValue() == StoredVal) {
132+
GS.StoredOnceValue == StoredVal) {
133133
// noop.
134134
} else {
135135
GS.StoredType = GlobalStatus::Stored;

llvm/test/Transforms/GlobalOpt/address_space_initializer.ll

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)