The right usage of llvm::ArrayRef

Hi All,

I debug a Release build LLVM crash, and find the culprit. The code snippet below,

llvm::ArrayRef Ops = { Lo, Hi }; // should be SDValue Ops[2] = { Lo, Hi };
return DAG.getMergeValues(Ops, dl);

Turns out a nullptr is dereferenced in DAG.getMergeValues, thus I have segfault.

From the comment on [1], it says:

This class does not own the underlying data, it is expected to be used in situations
where the data resides in some other buffer, whose lifetime extends past that of the
ArrayRef. For this reason, it is not in general safe to store an ArrayRef.

I feel somehow the comment explain why I get the segfault, but not sure about that.
Would someone kindly explain why?




llvm::ArrayRef Ops = { Lo, Hi };

{Lo, Hi} is a temporary object in stack and it is supposed to vanish after the statement.
You may write like ; (See X86ISelLowering.cpp:21709)

SDValue Ops = { Lo, Hi };
return DAG.getMergeValues(Ops, dl);

ArrayRef’s constructor recognizes and infers array.