[llvm-dev] BackendUtil's EmitAssemblyHelper::EmitAssembly generated call in wrong order

Craig Topper via llvm-dev llvm-dev at lists.llvm.org
Sat Dec 23 20:13:52 PST 2017


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>


More information about the llvm-dev mailing list