This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Fix incorrect use of MCInstBuilder in RISCVMCCodeEmitter
AbandonedPublic

Authored by shiva0217 on Dec 3 2018, 11:28 PM.

Details

Reviewers
asb
Summary

As Roger pointed out in https://reviews.llvm.org/rL339654, MCInst return by MCInstBuilder has lifetime limitation.

Diff Detail

Repository
rL LLVM

Event Timeline

shiva0217 created this revision.Dec 3 2018, 11:28 PM

Hi, thanks for the patch. I don't think this usage is wrong so we don't have to change it.

MCInstBuilder has an implicit conversion to MCInst&, but the referenced object does not outlive the object of the MCInstBuilder (i.e. MCInstBuilder owns that MCInst). So it would be an error to bind the returned reference in a way that is used after the owning object (MCInstBuilder) dies. This is exactly the mistake I did in https://reviews.llvm.org/rL339654 (note that there, AUIPC and ADDI are references, if I had not used a reference I would have worked on copies instead and the original code would have been OK too).

In this case, though, we're not binding any referenced to the MCInst owned by MCInstBuilder. Instead we're assigning to TmpInst. This involves the default copy-assignment operator of MCInst so the overall usage is fine because we work on a copy.

I think the code is more readable by using TmpInst because avoids us to propagate upwards the call to getBinaryCodeForInstr.

Hi @rogfer01, got it. Thanks to pointing out the difference. I'll drop the patch.

shiva0217 abandoned this revision.Dec 4 2018, 12:59 AM