Skip to content

Commit b197bec

Browse files
authored
Add a new WeakVH value handle; NFC (microsoft#6703)
Originally @lizhengxing's PR. Retargeting main. This PR pulls 2 upstream changes, Add a new WeakVH value handle; NFC (llvm/llvm-project@f1c0eaf) and Use a 2 bit pointer in ValueHandleBase::PrevPair; NFC (llvm/llvm-project@b297bff), into DXC. Here's the summary of the changes: Add a new WeakVH value handle; NFC > WeakVH nulls itself out if the value it was tracking gets deleted, but it does not track RAUW. > > Reviewers: dblaikie, davide > > Subscribers: mcrosier, llvm-commits > > Differential Revision: https://reviews.llvm.org/D32267 Use a 2 bit pointer in ValueHandleBase::PrevPair; NFC > This was an omission in r301813. I had made the supporting changes to make this happen, but I forgot to actually update the > > PrevPair declaration. This is part 4 and 5 of the fix for microsoft#6659.
1 parent 8b18659 commit b197bec

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

include/llvm/IR/ValueHandle.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ValueHandleBase {
4545
///
4646
/// This is to avoid having a vtable for the light-weight handle pointers. The
4747
/// fully general Callback version does have a vtable.
48-
enum HandleBaseKind { Assert, Callback, WeakTracking };
48+
enum HandleBaseKind { Assert, Callback, Weak, WeakTracking };
4949

5050
private:
5151
PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
@@ -134,6 +134,37 @@ class ValueHandleBase {
134134
void RemoveFromUseList();
135135
};
136136

137+
/// \brief A nullable Value handle that is nullable.
138+
///
139+
/// This is a value handle that points to a value, and nulls itself
140+
/// out if that value is deleted.
141+
class WeakVH : public ValueHandleBase {
142+
public:
143+
WeakVH() : ValueHandleBase(Weak) {}
144+
WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
145+
WeakVH(const WeakVH &RHS) : ValueHandleBase(Weak, RHS) {}
146+
147+
WeakVH &operator=(const WeakVH &RHS) = default;
148+
149+
Value *operator=(Value *RHS) { return ValueHandleBase::operator=(RHS); }
150+
Value *operator=(const ValueHandleBase &RHS) {
151+
return ValueHandleBase::operator=(RHS);
152+
}
153+
154+
operator Value *() const { return getValPtr(); }
155+
};
156+
157+
// Specialize simplify_type to allow WeakVH to participate in
158+
// dyn_cast, isa, etc.
159+
template <> struct simplify_type<WeakVH> {
160+
typedef Value *SimpleType;
161+
static SimpleType getSimplifiedValue(WeakVH &WVH) { return WVH; }
162+
};
163+
template <> struct simplify_type<const WeakVH> {
164+
typedef Value *SimpleType;
165+
static SimpleType getSimplifiedValue(const WeakVH &WVH) { return WVH; }
166+
};
167+
137168
/// \brief Value handle that is nullable, but tries to track the Value.
138169
///
139170
/// This is a value handle that tries hard to point to a Value, even across

lib/IR/Value.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ void ValueHandleBase::ValueIsDeleted(Value *V) {
672672
switch (Entry->getKind()) {
673673
case Assert:
674674
break;
675+
case Weak:
675676
case WeakTracking:
676677
// WeakTracking just goes to null, which will unlink it from the list.
677678
Entry->operator=(nullptr);
@@ -722,7 +723,8 @@ void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
722723

723724
switch (Entry->getKind()) {
724725
case Assert:
725-
// Asserting handle does not follow RAUW implicitly.
726+
case Weak:
727+
// Asserting and Weak handles do not follow RAUW implicitly.
726728
break;
727729
case WeakTracking:
728730
// Weak goes to the new value, which will unlink it from Old's list.

unittests/IR/ValueHandleTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ class ConcreteCallbackVH : public CallbackVH {
3434
ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
3535
};
3636

37+
TEST_F(ValueHandle, WeakVH_BasicOperation) {
38+
WeakVH WVH(BitcastV.get());
39+
EXPECT_EQ(BitcastV.get(), WVH);
40+
WVH = ConstantV;
41+
EXPECT_EQ(ConstantV, WVH);
42+
43+
// Make sure I can call a method on the underlying Value. It
44+
// doesn't matter which method.
45+
EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), WVH->getType());
46+
EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*WVH).getType());
47+
48+
WVH = BitcastV.get();
49+
BitcastV->replaceAllUsesWith(ConstantV);
50+
EXPECT_EQ(WVH, BitcastV.get());
51+
BitcastV.reset();
52+
EXPECT_EQ(WVH, nullptr);
53+
}
54+
3755
TEST_F(ValueHandle, WeakTrackingVH_BasicOperation) {
3856
WeakTrackingVH WVH(BitcastV.get());
3957
EXPECT_EQ(BitcastV.get(), WVH);

0 commit comments

Comments
 (0)