Issue with a SetVector of WeakVH-s

Hello;

I was writing code to use WeakVH instead of Instruction*, to get rid
of a dangling pointer error. I have the following datastructure:

SetVector<WeakVH, SmallVector<WeakVH, 8>> DeadInsts;

But I am seeing the following error while inserting a value into this SetVector:

clang-10: /home/for_upload/llvm-project_1141/llvm/include/llvm/ADT/DenseMap.h:378:
void llvm::DenseMapBase<DerivedT, KeyT, ValueT, KeyInfoT,

::moveFromOldBuckets(BucketT*, BucketT*) [with DerivedT =

llvm::DenseMap<llvm::WeakVH, llvm::detail::DenseSetEmpty,
llvm::DenseMapInfo<llvm::WeakVH>,
llvm::detail::DenseSetPair<llvm::WeakVH> >; KeyT = llvm::WeakVH;
ValueT = llvm::detail::DenseSetEmpty; KeyInfoT =
llvm::DenseMapInfo<llvm::WeakVH>; BucketT =
llvm::detail::DenseSetPair<llvm::WeakVH>]: Assertion `!FoundVal &&
"Key already in new map?"' failed.

Stack dump:
0. Program arguments:
/home/for_upload/llvm-project_1141/install/bin/clang-10 -cc1 -triple
aarch64-unknown-linux-gnu -emit-obj -disable-free -main-file-name
test.c -mrelocation-model static -mthread-model posix -mllvm
-pass-remarks= -mllvm -pass-remarks-missed= -mllvm
-pass-remarks-analysis= -mframe-pointer=non-leaf -fmath-errno
-ffp-contract=fast -fno-rounding-math -masm-verbose
-mconstructor-aliases -target-feature +v8.2a -target-feature +fp-armv8
-target-feature +neon -target-feature +crc -target-feature +crypto
-target-feature +dotprod -target-feature +fp16fml -target-feature +spe
-target-feature +ras -target-feature +lse -target-feature +rdm
-target-feature +complxnum -target-feature +fullfp16 -target-feature
+sha2 -target-feature +aes -target-abi aapcs
-fallow-half-arguments-and-returns -dwarf-column-info
-fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir
/home/for_upload/llvm-project_1141/install/lib/clang/10.0.1
-internal-isystem /usr/local/include -internal-isystem
/home/for_upload/llvm-project_1141/install/lib/clang/10.0.1/include
-internal-externc-isystem /include -internal-externc-isystem
/usr/include -O3 -w -fdebug-compilation-dir /home/bug/1141
-ferror-limit 19 -fmessage-length 0 -fno-signed-char
-fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option
-vectorize-loops -vectorize-slp -faddrsig -o /tmp/test-523a20.o -x c
test.c
1. <eof> parser at end of file
2. Per-module optimization passes
3. Running pass 'CallGraph Pass Manager' on module 'test.c'.
4. Running pass 'SROA' on function '@g'

#0 0x0000aaaad333ed40 llvm::sys::PrintStackTrace(llvm::raw_ostream&)
(/home/for_upload/llvm-project_1141/install/bin/clang-10+0x2045d40)
#1 0x0000aaaad333cf08 llvm::sys::RunSignalHandlers()
(/home/for_upload/llvm-project_1141/install/bin/clang-10+0x2043f08)
#2 0x0000aaaad333d6d8 SignalHandler(int)
(/home/for_upload/llvm-project_1141/install/bin/clang-10+0x20446d8)
#3 0x0000ffff805c0698 (linux-vdso.so.1+0x698)
#4 0x0000ffff80285140 raise (/lib64/libc.so.6+0x33140)
#5 0x0000ffff802864ec abort (/lib64/libc.so.6+0x344ec)
#6 0x0000ffff8027e54c __assert_fail_base (/lib64/libc.so.6+0x2c54c)
#7 0x0000ffff8027e5cc (/lib64/libc.so.6+0x2c5cc)
#8 0x0000aaaad323efb8 llvm::DenseMap<llvm::WeakVH,
llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::WeakVH>,
llvm::detail::DenseSetPair<llvm::WeakVH> >::grow(unsigned int)
(/home/for_upload/llvm-project_1141/install/bin/clang-10+0x1f45fb8)
#9 0x0000aaaad323f170 llvm::SetVector<llvm::WeakVH,
llvm::SmallVector<llvm::WeakVH, 8u>, llvm::DenseSet<llvm::WeakVH,
llvm::DenseMapInfo<llvm::WeakVH> > >::insert(llvm::WeakVH const&)
(/home/for_upload/llvm-project_1141/install/bin/clang-10+0x1f46170)

This error is non-deterministic (does not show up in all compilation).

Could anyone tell me what might be going on?

PS: In all the use cases of WeakVH, I see a Vector type is used
(never a SetVector). Is the error something to do with that? Should
I switch to use a Vector of WeakVH instead?

Thank you so much for your help.

Hello;

I was writing code to use WeakVH instead of Instruction*, to get rid
of a dangling pointer error. I have the following datastructure:

SetVector<WeakVH, SmallVector<WeakVH, 8>> DeadInsts;

But I am seeing the following error while inserting a value into this SetVector:

clang-10: /home/for_upload/llvm-project_1141/llvm/include/llvm/ADT/DenseMap.h:378:
void llvm::DenseMapBase<DerivedT, KeyT, ValueT, KeyInfoT,
>::moveFromOldBuckets(BucketT*, BucketT*) [with DerivedT =
llvm::DenseMap<llvm::WeakVH, llvm::detail::DenseSetEmpty,
llvm::DenseMapInfo<llvm::WeakVH>,
llvm::detail::DenseSetPair<llvm::WeakVH> >; KeyT = llvm::WeakVH;
ValueT = llvm::detail::DenseSetEmpty; KeyInfoT =
llvm::DenseMapInfo<llvm::WeakVH>; BucketT =
llvm::detail::DenseSetPair<llvm::WeakVH>]: Assertion `!FoundVal &&
"Key already in new map?"' failed.

Stack dump:
0. Program arguments:
/home/for_upload/llvm-project_1141/install/bin/clang-10 -cc1 -triple
aarch64-unknown-linux-gnu -emit-obj -disable-free -main-file-name
test.c -mrelocation-model static -mthread-model posix -mllvm
-pass-remarks= -mllvm -pass-remarks-missed= -mllvm
-pass-remarks-analysis= -mframe-pointer=non-leaf -fmath-errno
-ffp-contract=fast -fno-rounding-math -masm-verbose
-mconstructor-aliases -target-feature +v8.2a -target-feature +fp-armv8
-target-feature +neon -target-feature +crc -target-feature +crypto
-target-feature +dotprod -target-feature +fp16fml -target-feature +spe
-target-feature +ras -target-feature +lse -target-feature +rdm
-target-feature +complxnum -target-feature +fullfp16 -target-feature
+sha2 -target-feature +aes -target-abi aapcs
-fallow-half-arguments-and-returns -dwarf-column-info
-fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir
/home/for_upload/llvm-project_1141/install/lib/clang/10.0.1
-internal-isystem /usr/local/include -internal-isystem
/home/for_upload/llvm-project_1141/install/lib/clang/10.0.1/include
-internal-externc-isystem /include -internal-externc-isystem
/usr/include -O3 -w -fdebug-compilation-dir /home/bug/1141
-ferror-limit 19 -fmessage-length 0 -fno-signed-char
-fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option
-vectorize-loops -vectorize-slp -faddrsig -o /tmp/test-523a20.o -x c
test.c
1. <eof> parser at end of file
2. Per-module optimization passes
3. Running pass 'CallGraph Pass Manager' on module 'test.c'.
4. Running pass 'SROA' on function '@g'

#0 0x0000aaaad333ed40 llvm::sys::PrintStackTrace(llvm::raw_ostream&)
(/home/for_upload/llvm-project_1141/install/bin/clang-10+0x2045d40)
#1 0x0000aaaad333cf08 llvm::sys::RunSignalHandlers()
(/home/for_upload/llvm-project_1141/install/bin/clang-10+0x2043f08)
#2 0x0000aaaad333d6d8 SignalHandler(int)
(/home/for_upload/llvm-project_1141/install/bin/clang-10+0x20446d8)
#3 0x0000ffff805c0698 (linux-vdso.so.1+0x698)
#4 0x0000ffff80285140 raise (/lib64/libc.so.6+0x33140)
#5 0x0000ffff802864ec abort (/lib64/libc.so.6+0x344ec)
#6 0x0000ffff8027e54c __assert_fail_base (/lib64/libc.so.6+0x2c54c)
#7 0x0000ffff8027e5cc (/lib64/libc.so.6+0x2c5cc)
#8 0x0000aaaad323efb8 llvm::DenseMap<llvm::WeakVH,
llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::WeakVH>,
llvm::detail::DenseSetPair<llvm::WeakVH> >::grow(unsigned int)
(/home/for_upload/llvm-project_1141/install/bin/clang-10+0x1f45fb8)
#9 0x0000aaaad323f170 llvm::SetVector<llvm::WeakVH,
llvm::SmallVector<llvm::WeakVH, 8u>, llvm::DenseSet<llvm::WeakVH,
llvm::DenseMapInfo<llvm::WeakVH> > >::insert(llvm::WeakVH const&)
(/home/for_upload/llvm-project_1141/install/bin/clang-10+0x1f46170)

This error is non-deterministic (does not show up in all compilation).

Could anyone tell me what might be going on?

PS: In all the use cases of WeakVH, I see a Vector type is used
(never a SetVector). Is the error something to do with that? Should
I switch to use a Vector of WeakVH instead?

Yes. In general, you can't change the key of an map entry,
and ValueHandle obviously is changed by it's callbacks,
so it can't be used as key in maps/sets. Switching to a plain vector
is the solution, although i'm not sure how it will affect
the performance of that code, but that seems irrelevant.

Thank you so much for your help.

Roman