Craig Topper via llvm-dev
2017-Dec-24 04:13 UTC
[llvm-dev] BackendUtil's EmitAssemblyHelper::EmitAssembly generated call in wrong order
I think this is a problem with your expectations The code in your main function is evalualated equivalent like this. ((std::cout << "return: ") << bar()) << std::endl; The << operators are evaluated left to right one at a time. Just as they would be in the case of something like "foo() + bar() + baz()" The << operator for cout prints and then return a reference to its left hand side so that multiple << can be chained together. Due to that, another way to write this is: std::cout << "return: "; std::cout << bar(); std::cout << std::endl; ~Craig On Sat, Dec 23, 2017 at 7:47 PM, Leslie Zhai via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi LLVM developers, > > A C++ testcase for PR35737 > > #include <iostream> > > int bar() { > std::cout << "bar" << std::endl; > return 1; > } > > int main() { > std::cout << "return: " << bar() << std::endl; > return 0; > } > > Actual output: > > return: bar > 1 > > Expected output: > > bar > return: 1 > > Workaround to edit LLVM IR: > > --- PR35737.ll 2017-12-24 11:20:12.012346163 +0800 > +++ PR35737-workaround.ll 2017-12-24 11:42:25.303256968 +0800 > @@ -62,8 +62,8 @@ > entry: > %retval = alloca i32, align 4 > store i32 0, i32* %retval, align 4 > - %call = call dereferenceable(272) %"class.std::basic_ostream"* > @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%"class.std::basic_ostream"* > dereferenceable(272) @_ZSt4cout, i8* getelementptr inbounds ([9 x i8], [9 x > i8]* @.str.1, i32 0, i32 0)) > %call1 = call i32 @_Z3barv() > + %call = call dereferenceable(272) %"class.std::basic_ostream"* > @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%"class.std::basic_ostream"* > dereferenceable(272) @_ZSt4cout, i8* getelementptr inbounds ([9 x i8], [9 x > i8]* @.str.1, i32 0, i32 0)) > %call2 = call dereferenceable(272) %"class.std::basic_ostream"* > @_ZNSolsEi(%"class.std::basic_ostream"* %call, i32 %call1) > %call3 = call dereferenceable(272) %"class.std::basic_ostream"* > @_ZNSolsEPFRSoS_E(%"class.std::basic_ostream"* %call2, > %"class.std::basic_ostream"* (%"class.std::basic_ostream"*)* > @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_) > ret i32 0 > > > Then, > > $ llc PR35737-workaround.ll > > $ clang++ PR35737-workaround.s > > $ ./a.out > > bar > return: 1 > > Please give me some advice, thanks a lot! > > -- > Regards, > Leslie Zhai - https://reviews.llvm.org/p/xiangzhai/ > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://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/20171223/634e6b27/attachment.html>
Leslie Zhai via llvm-dev
2017-Dec-24 04:23 UTC
[llvm-dev] BackendUtil's EmitAssemblyHelper::EmitAssembly generated call in wrong order
Hi Craig, Thanks for your response! But GCC generated call in my expected order https://bugs.llvm.org/show_bug.cgi?id=35737#c1 Yes, it is my problem, I can write compatible and correct code like: int r = bar(); std::cout << "return:" << r << std::endl; 在 2017年12月24日 12:13, Craig Topper 写道:> I think this is a problem with your expectations > > The code in your main function is evalualated equivalent like this. > > ((std::cout << "return: ") << bar()) << std::endl; > > The << operators are evaluated left to right one at a time. Just as > they would be in the case of something like "foo() + bar() + baz()" > > The << operator for cout prints and then return a reference to its > left hand side so that multiple << can be chained together. Due to > that, another way to write this is: > > std::cout << "return: "; > std::cout << bar(); > std::cout << std::endl; > > > > ~Craig > > On Sat, Dec 23, 2017 at 7:47 PM, Leslie Zhai via llvm-dev > <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote: > > Hi LLVM developers, > > A C++ testcase for PR35737 > > #include <iostream> > > int bar() { > std::cout << "bar" << std::endl; > return 1; > } > > int main() { > std::cout << "return: " << bar() << std::endl; > return 0; > } > > Actual output: > > return: bar > 1 > > Expected output: > > bar > return: 1 > > Workaround to edit LLVM IR: > > --- PR35737.ll 2017-12-24 11:20:12.012346163 <tel:12.012346163> +0800 > +++ PR35737-workaround.ll 2017-12-24 11:42:25.303256968 +0800 > @@ -62,8 +62,8 @@ > entry: > %retval = alloca i32, align 4 > store i32 0, i32* %retval, align 4 > - %call = call dereferenceable(272) %"class.std::basic_ostream"* > @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%"class.std::basic_ostream"* > dereferenceable(272) @_ZSt4cout, i8* getelementptr inbounds ([9 x > i8], [9 x i8]* @.str.1, i32 0, i32 0)) > %call1 = call i32 @_Z3barv() > + %call = call dereferenceable(272) %"class.std::basic_ostream"* > @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%"class.std::basic_ostream"* > dereferenceable(272) @_ZSt4cout, i8* getelementptr inbounds ([9 x > i8], [9 x i8]* @.str.1, i32 0, i32 0)) > %call2 = call dereferenceable(272) %"class.std::basic_ostream"* > @_ZNSolsEi(%"class.std::basic_ostream"* %call, i32 %call1) > %call3 = call dereferenceable(272) %"class.std::basic_ostream"* > @_ZNSolsEPFRSoS_E(%"class.std::basic_ostream"* %call2, > %"class.std::basic_ostream"* (%"class.std::basic_ostream"*)* > @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_) > ret i32 0 > > > Then, > > $ llc PR35737-workaround.ll > > $ clang++ PR35737-workaround.s > > $ ./a.out > > bar > return: 1 > > Please give me some advice, thanks a lot! > > -- > Regards, > Leslie Zhai - https://reviews.llvm.org/p/xiangzhai/ > <https://reviews.llvm.org/p/xiangzhai/> > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org> > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > <http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev> > >-- Regards, Leslie Zhai - https://reviews.llvm.org/p/xiangzhai/