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

翟翔 via llvm-dev llvm-dev at lists.llvm.org
Sun Dec 24 05:25:58 PST 2017


Thanks and happy Christmas Eve :)


发自我的iPhone

------------------ Original ------------------
From: Eric Christopher <echristo at gmail.com>
Date: 周日,12月 24,2017 0:45 下午
To: Leslie Zhai <lesliezhai at llvm.org.cn>
Cc: LLVM Developers <llvm-dev at lists.llvm.org>, chandlerc <chandlerc at gmail.com>, davide <davide at freebsd.org>
Subject: Re: BackendUtil's EmitAssemblyHelper::EmitAssembly generated call inwrong order



This is a difference in evaluation order between gcc and clang. Basically this is implementation defined behavior that you're depending upon. 

On Sat, Dec 23, 2017, 7:47 PM Leslie Zhai <lesliezhai at llvm.org.cn> 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/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20171224/b2446d0b/attachment.html>


More information about the llvm-dev mailing list