[Day2-2] 体验华为方舟编译器:IR测试

上一篇我们讲到完成了方舟编译器的编译过程。目前华为只开源了方舟编译器的IR部分,我们来通过一段简单的代码来测试一下吧。

测试C++代码:

#include <iostream>

using namespace std;

int main() {
    int n;
    cout << "The Answer to Life, the Universe and Everything is:  ";
    cin >> n;
    if (n == 42) {
        cout << "Find highly intelligent lifeform! Start the destruction process!" << endl;
    }
    // 这里为了测试编译器效果
    else if (n != 42) {
        cout << "Yes, you are right!" << endl;
    }
    return 0;
}

文件保存为HelloWorld.cpp。

先用g++测试一下:

g++ HelloUniverse.cpp -o Hello
./Hello

分别测试输入42和10086的情况:

The Answer to Life, the Universe and Everything is:  42
Find highly intelligent lifeform! Start the destruction process!
The Answer to Life, the Universe and Everything is:  42
Find highly intelligent lifeform! Start the destruction process!

看起来是正常运行的。

我们来看一下LLVM下这段代码的IR:

运行

clang -S -emit-llvm HelloUniverse.cpp

输出了ll文件,我们重点看一下cpp代码IR的部分:

; Function Attrs: noinline norecurse optnone uwtable
define dso_local i32 @main() #4 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  store i32 0, i32* %1, align 4
  %3 = call dereferenceable(272) %"class.std::basic_ostream"* @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%"class.std::basic_ostream"* dereferenceable(272) @_ZSt4cout, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i32 0, i32 0))
  %4 = call dereferenceable(280) %"class.std::basic_istream"* @_ZNSirsERi(%"class.std::basic_istream"* @_ZSt3cin, i32* dereferenceable(4) %2)
  %5 = load i32, i32* %2, align 4
  %6 = icmp eq i32 %5, 42
  br i1 %6, label %7, label %10

; <label>:7:                                      ; preds = %0
  %8 = call dereferenceable(272) %"class.std::basic_ostream"* @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%"class.std::basic_ostream"* dereferenceable(272) @_ZSt4cout, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i32 0, i32 0))
  %9 = call dereferenceable(272) %"class.std::basic_ostream"* @_ZNSolsEPFRSoS_E(%"class.std::basic_ostream"* %8, %"class.std::basic_ostream"* (%"class.std::basic_ostream"*)* @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  br label %17

; <label>:10:                                     ; preds = %0
  %11 = load i32, i32* %2, align 4
  %12 = icmp ne i32 %11, 42
  br i1 %12, label %13, label %16

; <label>:13:                                     ; preds = %10
  %14 = call dereferenceable(272) %"class.std::basic_ostream"* @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%"class.std::basic_ostream"* dereferenceable(272) @_ZSt4cout, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i32 0, i32 0))
  %15 = call dereferenceable(272) %"class.std::basic_ostream"* @_ZNSolsEPFRSoS_E(%"class.std::basic_ostream"* %14, %"class.std::basic_ostream"* (%"class.std::basic_ostream"*)* @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  br label %16

; <label>:16:                                     ; preds = %13, %10
  br label %17

; <label>:17:                                     ; preds = %16, %7
  ret i32 0
}


经过测试,方舟编译器似乎不支持C++,或者是没有开源?我上网查证了一下,少数几篇似乎有意义的文档都在说似乎C++的RT没有放出来(这就很神奇了),所以我们测试一下Java:

以上文件保存为HelloUniverse.java,然后通过OpenJDK编译运行:

javac HelloUniverse.java
java Main

同C++进行测试,输出相同,略过。
然而,使用方舟编译器总是不能成功运行,且输出都是:

Error 40: Init Fail!

有趣的是,如果执行方舟编译器时指定了错误的参数,错误码也是40。

那么今天先到这里辣,我再测试一下!

“[Day2-2] 体验华为方舟编译器:IR测试”的6个回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据