aot_llvm_test.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "bh_read_file.h"
  8. #include "aot_llvm.h"
  9. #include "aot_compiler.h"
  10. static std::string CWD;
  11. static std::string MAIN_WASM = "/main.wasm";
  12. static char *WASM_FILE;
  13. class aot_llvm_test_suite : public testing::Test
  14. {
  15. protected:
  16. // You should make the members protected s.t. they can be
  17. // accessed from sub-classes.
  18. // virtual void SetUp() will be called before each test is run. You
  19. // should define it if you need to initialize the variables.
  20. // Otherwise, this can be skipped.
  21. virtual void SetUp() {}
  22. static void SetUpTestCase()
  23. {
  24. CWD = get_test_binary_dir();
  25. WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
  26. }
  27. // virtual void TearDown() will be called after each test is run.
  28. // You should define it if there is cleanup work to do. Otherwise,
  29. // you don't have to provide it.
  30. //
  31. virtual void TearDown() {}
  32. static void TearDownTestCase() { free(WASM_FILE); }
  33. WAMRRuntimeRAII<512 * 1024> runtime;
  34. };
  35. TEST_F(aot_llvm_test_suite, aot_functions)
  36. {
  37. const char *wasm_file = WASM_FILE;
  38. unsigned int wasm_file_size = 0;
  39. unsigned char *wasm_file_buf = nullptr;
  40. char error_buf[128] = { 0 };
  41. wasm_module_t wasm_module = nullptr;
  42. struct AOTCompData *comp_data = nullptr;
  43. struct AOTCompContext *comp_ctx = nullptr;
  44. AOTCompOption option = { 0 };
  45. AOTFuncContext *func_ctx = nullptr;
  46. WASMValue wasm_value;
  47. LLVMTypeRef param_types[1];
  48. option.opt_level = 3;
  49. option.size_level = 3;
  50. option.output_format = AOT_FORMAT_FILE;
  51. /* default value, enable or disable depends on the platform */
  52. option.bounds_checks = 2;
  53. option.enable_simd = true;
  54. option.enable_aux_stack_check = true;
  55. option.enable_bulk_memory = true;
  56. option.enable_ref_types = true;
  57. wasm_file_buf =
  58. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  59. EXPECT_NE(wasm_file_buf, nullptr);
  60. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  61. sizeof(error_buf));
  62. EXPECT_NE(wasm_module, nullptr);
  63. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  64. EXPECT_NE(nullptr, comp_data);
  65. comp_ctx = aot_create_comp_context(comp_data, &option);
  66. EXPECT_NE(comp_ctx, nullptr);
  67. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  68. func_ctx = comp_ctx->func_ctxes[1];
  69. param_types[0] = F64_TYPE;
  70. EXPECT_TRUE(aot_call_llvm_intrinsic(comp_ctx, func_ctx, "f32_demote_f64",
  71. F32_TYPE, param_types, 0));
  72. /* Test function aot_get_native_symbol_index. */
  73. AOTNativeSymbol elem_insert_1;
  74. elem_insert_1.index = -1;
  75. bh_list_insert(&comp_ctx->native_symbols, &elem_insert_1);
  76. AOTNativeSymbol elem_insert_2;
  77. strcpy(elem_insert_2.symbol, "f64#_test");
  78. elem_insert_2.index = -1;
  79. bh_list_insert(&comp_ctx->native_symbols, &elem_insert_2);
  80. comp_ctx->pointer_size = sizeof(uint32);
  81. strcpy(comp_ctx->target_arch, "i386");
  82. EXPECT_NE(-1, aot_get_native_symbol_index(comp_ctx, "f64#_test"));
  83. }
  84. TEST_F(aot_llvm_test_suite, wasm_type_to_llvm_type) {}
  85. TEST_F(aot_llvm_test_suite, aot_build_zero_function_ret)
  86. {
  87. const char *wasm_file = WASM_FILE;
  88. unsigned int wasm_file_size = 0;
  89. unsigned char *wasm_file_buf = nullptr;
  90. char error_buf[128] = { 0 };
  91. wasm_module_t wasm_module = nullptr;
  92. struct AOTCompData *comp_data = nullptr;
  93. struct AOTCompContext *comp_ctx = nullptr;
  94. AOTCompOption option = { 0 };
  95. AOTFuncContext *func_ctx = nullptr;
  96. AOTFuncType func_type;
  97. option.opt_level = 3;
  98. option.size_level = 3;
  99. option.output_format = AOT_FORMAT_FILE;
  100. /* default value, enable or disable depends on the platform */
  101. option.bounds_checks = 2;
  102. option.enable_simd = true;
  103. option.enable_aux_stack_check = true;
  104. option.enable_bulk_memory = true;
  105. option.enable_ref_types = false;
  106. wasm_file_buf =
  107. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  108. EXPECT_NE(wasm_file_buf, nullptr);
  109. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  110. sizeof(error_buf));
  111. EXPECT_NE(wasm_module, nullptr);
  112. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  113. EXPECT_NE(nullptr, comp_data);
  114. comp_ctx = aot_create_comp_context(comp_data, &option);
  115. EXPECT_NE(comp_ctx, nullptr);
  116. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  117. func_ctx = comp_ctx->func_ctxes[1];
  118. func_type.result_count = 1;
  119. func_type.param_count = 0;
  120. func_type.types[func_type.param_count] = VALUE_TYPE_I32;
  121. EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
  122. func_type.types[func_type.param_count] = VALUE_TYPE_I64;
  123. EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
  124. func_type.types[func_type.param_count] = VALUE_TYPE_F32;
  125. EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
  126. func_type.types[func_type.param_count] = VALUE_TYPE_F64;
  127. EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
  128. func_type.types[func_type.param_count] = VALUE_TYPE_V128;
  129. EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
  130. /* THe current optimization, if not actually use ref_types in wasm module,
  131. * it will set to false, so test false condition */
  132. func_type.types[func_type.param_count] = VALUE_TYPE_FUNCREF;
  133. EXPECT_DEATH(aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type),
  134. ".*");
  135. func_type.types[func_type.param_count] = VALUE_TYPE_EXTERNREF;
  136. EXPECT_DEATH(aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type),
  137. ".*");
  138. func_type.types[func_type.param_count] = 0xFF;
  139. EXPECT_DEATH(aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type),
  140. ".*");
  141. }
  142. TEST_F(aot_llvm_test_suite, aot_destroy_comp_context)
  143. {
  144. const char *wasm_file = WASM_FILE;
  145. unsigned int wasm_file_size = 0;
  146. unsigned char *wasm_file_buf = nullptr;
  147. char error_buf[128] = { 0 };
  148. wasm_module_t wasm_module = nullptr;
  149. struct AOTCompData *comp_data = nullptr;
  150. struct AOTCompContext *comp_ctx = nullptr;
  151. AOTCompOption option = { 0 };
  152. AOTFuncContext *func_ctx = nullptr;
  153. AOTFuncType func_type;
  154. option.opt_level = 3;
  155. option.size_level = 3;
  156. option.output_format = AOT_FORMAT_FILE;
  157. /* default value, enable or disable depends on the platform */
  158. option.bounds_checks = 2;
  159. option.enable_simd = true;
  160. option.enable_aux_stack_check = true;
  161. option.enable_bulk_memory = true;
  162. option.enable_ref_types = true;
  163. wasm_file_buf =
  164. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  165. EXPECT_NE(wasm_file_buf, nullptr);
  166. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  167. sizeof(error_buf));
  168. EXPECT_NE(wasm_module, nullptr);
  169. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  170. EXPECT_NE(nullptr, comp_data);
  171. comp_ctx = aot_create_comp_context(comp_data, &option);
  172. EXPECT_NE(comp_ctx, nullptr);
  173. EXPECT_TRUE(aot_compile_wasm(comp_ctx));
  174. AOTNativeSymbol elem_insert_1;
  175. elem_insert_1.index = -1;
  176. bh_list_insert(&comp_ctx->native_symbols, &elem_insert_1);
  177. aot_destroy_comp_context(comp_ctx);
  178. aot_destroy_comp_context(nullptr);
  179. }
  180. TEST_F(aot_llvm_test_suite, aot_create_comp_context)
  181. {
  182. const char *wasm_file = WASM_FILE;
  183. unsigned int wasm_file_size = 0;
  184. unsigned char *wasm_file_buf = nullptr;
  185. char error_buf[128] = { 0 };
  186. wasm_module_t wasm_module = nullptr;
  187. struct AOTCompData *comp_data = nullptr;
  188. struct AOTCompContext *comp_ctx = nullptr;
  189. AOTCompOption option = { 0 };
  190. option.opt_level = 3;
  191. option.size_level = 3;
  192. option.output_format = AOT_FORMAT_FILE;
  193. /* default value, enable or disable depends on the platform */
  194. option.bounds_checks = 2;
  195. option.enable_simd = true;
  196. option.enable_aux_stack_check = true;
  197. option.enable_bulk_memory = true;
  198. option.enable_ref_types = true;
  199. wasm_file_buf =
  200. (unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
  201. EXPECT_NE(wasm_file_buf, nullptr);
  202. wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  203. sizeof(error_buf));
  204. EXPECT_NE(wasm_module, nullptr);
  205. comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
  206. EXPECT_NE(nullptr, comp_data);
  207. option.enable_thread_mgr = true;
  208. option.enable_tail_call = true;
  209. option.is_indirect_mode = true;
  210. option.disable_llvm_intrinsics = true;
  211. option.disable_llvm_lto = true;
  212. option.is_jit_mode = true;
  213. option.target_arch = (char *)"arm";
  214. comp_ctx = aot_create_comp_context(comp_data, &option);
  215. EXPECT_NE(comp_ctx, nullptr);
  216. option.output_format = 100;
  217. comp_ctx = aot_create_comp_context(comp_data, &option);
  218. // Test every target_arch.
  219. option.is_jit_mode = false;
  220. option.target_arch = (char *)"arm";
  221. comp_ctx = aot_create_comp_context(comp_data, &option);
  222. option.target_arch = (char *)"armeb";
  223. comp_ctx = aot_create_comp_context(comp_data, &option);
  224. option.target_arch = (char *)"thumb";
  225. comp_ctx = aot_create_comp_context(comp_data, &option);
  226. option.target_arch = (char *)"thumbeb";
  227. comp_ctx = aot_create_comp_context(comp_data, &option);
  228. option.target_arch = (char *)"aarch64";
  229. comp_ctx = aot_create_comp_context(comp_data, &option);
  230. option.target_arch = (char *)"aarch64_be";
  231. comp_ctx = aot_create_comp_context(comp_data, &option);
  232. option.target_arch = (char *)"help";
  233. comp_ctx = aot_create_comp_context(comp_data, &option);
  234. // Test every target_abi.
  235. option.target_arch = (char *)"arm";
  236. option.target_abi = (char *)"test";
  237. comp_ctx = aot_create_comp_context(comp_data, &option);
  238. option.target_abi = (char *)"help";
  239. comp_ctx = aot_create_comp_context(comp_data, &option);
  240. option.target_abi = (char *)"msvc";
  241. option.target_arch = (char *)"i386";
  242. comp_ctx = aot_create_comp_context(comp_data, &option);
  243. option.cpu_features = (char *)"test";
  244. comp_ctx = aot_create_comp_context(comp_data, &option);
  245. option.is_sgx_platform = true;
  246. comp_ctx = aot_create_comp_context(comp_data, &option);
  247. comp_data->func_count = 0;
  248. comp_ctx = aot_create_comp_context(comp_data, &option);
  249. }