aot_compiler_test.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "test_helper.h"
  6. #include "gtest/gtest.h"
  7. #include "wasm_export.h"
  8. #include "aot_export.h"
  9. #include "bh_read_file.h"
  10. static std::string CWD;
  11. static std::string MAIN_WASM = "/main.wasm";
  12. static char *WASM_FILE;
  13. extern "C" {
  14. char *
  15. aot_generate_tempfile_name(const char *prefix, const char *extension,
  16. char *buffer, uint32 len);
  17. }
  18. class aot_compiler_test_suit : public testing::Test
  19. {
  20. protected:
  21. // You should make the members protected s.t. they can be
  22. // accessed from sub-classes.
  23. // virtual void SetUp() will be called before each test is run. You
  24. // should define it if you need to initialize the variables.
  25. // Otherwise, this can be skipped.
  26. virtual void SetUp() {}
  27. static void SetUpTestCase()
  28. {
  29. CWD = get_test_binary_dir();
  30. WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
  31. }
  32. // virtual void TearDown() will be called after each test is run.
  33. // You should define it if there is cleanup work to do. Otherwise,
  34. // you don't have to provide it.
  35. //
  36. virtual void TearDown() {}
  37. static void TearDownTestCase() { free(WASM_FILE); }
  38. WAMRRuntimeRAII<512 * 1024> runtime;
  39. };
  40. void
  41. test_aot_emit_object_file_with_option(AOTCompOption *option_ptr)
  42. {
  43. const char *wasm_file = WASM_FILE;
  44. unsigned int wasm_file_size = 0;
  45. unsigned char *wasm_file_buf = nullptr;
  46. char error_buf[128] = { 0 };
  47. wasm_module_t wasm_module = nullptr;
  48. aot_comp_data_t comp_data = nullptr;
  49. aot_comp_context_t comp_ctx = nullptr;
  50. char out_file_name[] = "test.aot";
  51. wasm_file_buf =
  52. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  53. EXPECT_NE(wasm_file_buf, nullptr);
  54. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  55. sizeof(error_buf));
  56. EXPECT_NE(wasm_module, nullptr);
  57. comp_data = aot_create_comp_data(wasm_module, NULL, false);
  58. EXPECT_NE(nullptr, comp_data);
  59. comp_ctx = aot_create_comp_context(comp_data, option_ptr);
  60. EXPECT_NE(comp_ctx, nullptr);
  61. EXPECT_STREQ(aot_get_last_error(), "");
  62. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  63. EXPECT_TRUE(aot_emit_object_file(comp_ctx, out_file_name));
  64. }
  65. TEST_F(aot_compiler_test_suit, aot_emit_object_file)
  66. {
  67. AOTCompOption option = { 0 };
  68. uint32_t i = 0;
  69. option.opt_level = 3;
  70. option.size_level = 3;
  71. option.output_format = AOT_FORMAT_FILE;
  72. option.bounds_checks = 2;
  73. option.enable_simd = true;
  74. option.enable_aux_stack_check = true;
  75. option.enable_bulk_memory = true;
  76. option.enable_ref_types = true;
  77. // Test opt_level in range from 0 to 3.
  78. for (i = 0; i <= 3; i++) {
  79. option.opt_level = i;
  80. test_aot_emit_object_file_with_option(&option);
  81. }
  82. // Test size_level in range from 0 to 3.
  83. option.opt_level = 3;
  84. #if defined(__aarch64__) || defined(_M_ARM64)
  85. std::vector<int> size_levels = { 0, 3 };
  86. #else
  87. std::vector<int> size_levels = { 0, 1, 2, 3 };
  88. #endif
  89. for (auto size_level : size_levels) {
  90. option.size_level = size_level;
  91. test_aot_emit_object_file_with_option(&option);
  92. }
  93. // Test output_format in range from AOT_FORMAT_FILE to AOT_LLVMIR_OPT_FILE.
  94. option.size_level = 3;
  95. for (i = AOT_FORMAT_FILE; i <= AOT_LLVMIR_OPT_FILE; i++) {
  96. option.output_format = i;
  97. test_aot_emit_object_file_with_option(&option);
  98. }
  99. // Test bounds_checks in range 0 to 2.
  100. option.output_format = AOT_FORMAT_FILE;
  101. for (i = 0; i <= 2; i++) {
  102. option.bounds_checks = i;
  103. test_aot_emit_object_file_with_option(&option);
  104. }
  105. // Test all enable option is false.
  106. option.bounds_checks = 2;
  107. option.enable_simd = false;
  108. option.enable_aux_stack_check = false;
  109. option.enable_bulk_memory = false;
  110. option.enable_ref_types = false;
  111. test_aot_emit_object_file_with_option(&option);
  112. }
  113. TEST_F(aot_compiler_test_suit, aot_emit_llvm_file)
  114. {
  115. const char *wasm_file = WASM_FILE;
  116. unsigned int wasm_file_size = 0;
  117. unsigned char *wasm_file_buf = nullptr;
  118. char error_buf[128] = { 0 };
  119. wasm_module_t wasm_module = nullptr;
  120. aot_comp_data_t comp_data = nullptr;
  121. aot_comp_context_t comp_ctx = nullptr;
  122. AOTCompOption option = { 0 };
  123. char out_file_name[] = "out_file_name_test";
  124. option.opt_level = 3;
  125. option.size_level = 3;
  126. option.output_format = AOT_FORMAT_FILE;
  127. /* default value, enable or disable depends on the platform */
  128. option.bounds_checks = 2;
  129. option.enable_simd = true;
  130. option.enable_aux_stack_check = true;
  131. option.enable_bulk_memory = true;
  132. option.enable_ref_types = true;
  133. wasm_file_buf =
  134. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  135. EXPECT_NE(wasm_file_buf, nullptr);
  136. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  137. sizeof(error_buf));
  138. EXPECT_NE(wasm_module, nullptr);
  139. comp_data = aot_create_comp_data(wasm_module, NULL, false);
  140. EXPECT_NE(nullptr, comp_data);
  141. comp_ctx = aot_create_comp_context(comp_data, &option);
  142. EXPECT_NE(comp_ctx, nullptr);
  143. EXPECT_STREQ(aot_get_last_error(), "");
  144. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  145. EXPECT_EQ(true, aot_emit_llvm_file(comp_ctx, out_file_name));
  146. }
  147. TEST_F(aot_compiler_test_suit, aot_generate_tempfile_name)
  148. {
  149. char obj_file_name[64];
  150. // Test common case.
  151. aot_generate_tempfile_name("wamrc-obj", "o", obj_file_name,
  152. sizeof(obj_file_name));
  153. EXPECT_NE(nullptr, strstr(obj_file_name, ".o"));
  154. // Test abnormal cases.
  155. EXPECT_EQ(nullptr,
  156. aot_generate_tempfile_name("wamrc-obj", "o", obj_file_name, 0));
  157. char obj_file_name_1[20];
  158. EXPECT_EQ(nullptr, aot_generate_tempfile_name(
  159. "wamrc-obj", "12345678901234567890", obj_file_name_1,
  160. sizeof(obj_file_name_1)));
  161. }