Frank Winter via llvm-dev
2020-Apr-15 02:07 UTC
[llvm-dev] Backend emitting to string instead of file
I can use llc to compile my IR module to amdgcn with some non-zero output. However, if try to write the output (assembly or object) to a string (via buffer_ostream) the resulting string has always zero length. Here the code changes I do: Original llc: if (Target->addPassesToEmitFile(PM, *OS, DwoOut ? &DwoOut->os() : nullptr, FileType, NoVerify, MMIWP)) Now, with this tiny change I was hoping to write to a string instead of the output stream defined by the command line parameters: std::string outStr; raw_string_ostream stream(outStr); buffer_ostream pstream(stream); if (Target->addPassesToEmitFile(PM, pstream, DwoOut ? &DwoOut->os() : nullptr, FileType, NoVerify, MMIWP)) later, e.g., after PM.run(*M), I check the string for content with WithColor::warning(errs(), argv[0]) << "with flush, output size: " << outStr.length() << "\n"; But this always yields "with flush, output size: 0" I tried to flush the raw_string_ostream before using the string, same result. The used command line was llc -march=amdgcn -mcpu=gfx906 < module.ll Anyone see what's wrong with the code? Frank
Craig Topper via llvm-dev
2020-Apr-15 02:27 UTC
[llvm-dev] Backend emitting to string instead of file
I think buffer_ostream has an internal buffer that doesn't flush until its destructor runs. What happens if you just pass "stream" instead of pstream? ~Craig On Tue, Apr 14, 2020 at 7:08 PM Frank Winter via llvm-dev < llvm-dev at lists.llvm.org> wrote:> I can use llc to compile my IR module to amdgcn with some non-zero > output. However, if try to write the output (assembly or object) to a > string (via buffer_ostream) the resulting string has always zero length. > Here the code changes I do: > > Original llc: > > if (Target->addPassesToEmitFile(PM, *OS, > DwoOut ? &DwoOut->os() : nullptr, > FileType, NoVerify, MMIWP)) > > > Now, with this tiny change I was hoping to write to a string instead of > the output stream defined by the command line parameters: > > std::string outStr; > raw_string_ostream stream(outStr); > buffer_ostream pstream(stream); > if (Target->addPassesToEmitFile(PM, pstream, > DwoOut ? &DwoOut->os() : nullptr, > FileType, NoVerify, MMIWP)) > > > later, e.g., after PM.run(*M), I check the string for content with > > WithColor::warning(errs(), argv[0]) << "with flush, output size: " << > outStr.length() << "\n"; > > > But this always yields "with flush, output size: 0" > > I tried to flush the raw_string_ostream before using the string, same > result. > > The used command line was > > llc -march=amdgcn -mcpu=gfx906 < module.ll > > > Anyone see what's wrong with the code? > > > Frank > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200414/5ce7911d/attachment.html>
Frank Winter via llvm-dev
2020-Apr-15 13:58 UTC
[llvm-dev] [EXTERNAL] Re: Backend emitting to string instead of file
When passing in "stream" instead I get error: non-const lvalue reference to type 'llvm::raw_pwrite_stream' cannot bind to a value of unrelated type 'llvm::raw_string_ostream' I am not sure what else to try. Frank On 4/14/20 10:27 PM, Craig Topper wrote:> I think buffer_ostream has an internal buffer that doesn't flush until > its destructor runs. What happens > if you just pass "stream" instead of pstream? > > ~Craig > > > On Tue, Apr 14, 2020 at 7:08 PM Frank Winter via llvm-dev > <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote: > > I can use llc to compile my IR module to amdgcn with some non-zero > output. However, if try to write the output (assembly or object) to a > string (via buffer_ostream) the resulting string has always zero > length. > Here the code changes I do: > > Original llc: > > if (Target->addPassesToEmitFile(PM, *OS, > DwoOut ? &DwoOut->os() : > nullptr, > FileType, NoVerify, MMIWP)) > > > Now, with this tiny change I was hoping to write to a string > instead of > the output stream defined by the command line parameters: > > std::string outStr; > raw_string_ostream stream(outStr); > buffer_ostream pstream(stream); > if (Target->addPassesToEmitFile(PM, pstream, > DwoOut ? &DwoOut->os() : nullptr, > FileType, NoVerify, MMIWP)) > > > later, e.g., after PM.run(*M), I check the string for content with > > WithColor::warning(errs(), argv[0]) << "with flush, output size: " << > outStr.length() << "\n"; > > > But this always yields "with flush, output size: 0" > > I tried to flush the raw_string_ostream before using the string, same > result. > > The used command line was > > llc -march=amdgcn -mcpu=gfx906 < module.ll > > > Anyone see what's wrong with the code? > > > Frank > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org> > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > <https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_llvm-2Ddev&d=DwMFaQ&c=CJqEzB1piLOyyvZjb8YUQw&r=tFpAzszScTWMAFcrGFW5xg&m=oviBDHJqEC2vv35g8O4RBcGJEiOrougT5L0aM0gH3Dg&s=KDhhMM6rb9PhZnhzJYUujXsFibjWX29VxkIGd3ln_iY&e=> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200415/78a53b70/attachment-0001.html>
Frank Winter via llvm-dev
2020-Apr-15 14:37 UTC
[llvm-dev] [EXTERNAL] Re: Backend emitting to string instead of file
Yes, the destructor hint was golden. Frank On 4/14/20 10:27 PM, Craig Topper wrote:> I think buffer_ostream has an internal buffer that doesn't flush until > its destructor runs. What happens > if you just pass "stream" instead of pstream? > > ~Craig > > > On Tue, Apr 14, 2020 at 7:08 PM Frank Winter via llvm-dev > <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote: > > I can use llc to compile my IR module to amdgcn with some non-zero > output. However, if try to write the output (assembly or object) to a > string (via buffer_ostream) the resulting string has always zero > length. > Here the code changes I do: > > Original llc: > > if (Target->addPassesToEmitFile(PM, *OS, > DwoOut ? &DwoOut->os() : > nullptr, > FileType, NoVerify, MMIWP)) > > > Now, with this tiny change I was hoping to write to a string > instead of > the output stream defined by the command line parameters: > > std::string outStr; > raw_string_ostream stream(outStr); > buffer_ostream pstream(stream); > if (Target->addPassesToEmitFile(PM, pstream, > DwoOut ? &DwoOut->os() : nullptr, > FileType, NoVerify, MMIWP)) > > > later, e.g., after PM.run(*M), I check the string for content with > > WithColor::warning(errs(), argv[0]) << "with flush, output size: " << > outStr.length() << "\n"; > > > But this always yields "with flush, output size: 0" > > I tried to flush the raw_string_ostream before using the string, same > result. > > The used command line was > > llc -march=amdgcn -mcpu=gfx906 < module.ll > > > Anyone see what's wrong with the code? > > > Frank > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org> > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > <https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_llvm-2Ddev&d=DwMFaQ&c=CJqEzB1piLOyyvZjb8YUQw&r=tFpAzszScTWMAFcrGFW5xg&m=oviBDHJqEC2vv35g8O4RBcGJEiOrougT5L0aM0gH3Dg&s=KDhhMM6rb9PhZnhzJYUujXsFibjWX29VxkIGd3ln_iY&e=> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200415/b7670636/attachment.html>