simd_construct_values.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "simd_common.h"
  6. #include "simd_construct_values.h"
  7. #include "../aot_emit_exception.h"
  8. #include "../interpreter/wasm_opcode.h"
  9. #include "../../aot/aot_runtime.h"
  10. bool
  11. aot_compile_simd_v128_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  12. const uint8 *imm_bytes)
  13. {
  14. uint64 imm1, imm2;
  15. LLVMValueRef first_long, agg1, second_long, agg2;
  16. wasm_runtime_read_v128(imm_bytes, &imm1, &imm2);
  17. /* %agg1 = insertelement <2 x i64> undef, i16 0, i64 ${*imm} */
  18. if (!(first_long = I64_CONST(imm1))) {
  19. HANDLE_FAILURE("LLVMConstInt");
  20. goto fail;
  21. }
  22. if (!(agg1 =
  23. LLVMBuildInsertElement(comp_ctx->builder, LLVM_CONST(i64x2_undef),
  24. first_long, I32_ZERO, "agg1"))) {
  25. HANDLE_FAILURE("LLVMBuildInsertElement");
  26. goto fail;
  27. }
  28. /* %agg2 = insertelement <2 x i64> %agg1, i16 1, i64 ${*(imm + 1)} */
  29. if (!(second_long = I64_CONST(imm2))) {
  30. HANDLE_FAILURE("LLVMGetUndef");
  31. goto fail;
  32. }
  33. if (!(agg2 = LLVMBuildInsertElement(comp_ctx->builder, agg1, second_long,
  34. I32_ONE, "agg2"))) {
  35. HANDLE_FAILURE("LLVMBuildInsertElement");
  36. goto fail;
  37. }
  38. PUSH_V128(agg2);
  39. return true;
  40. fail:
  41. return false;
  42. }
  43. bool
  44. aot_compile_simd_splat(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  45. uint8 opcode)
  46. {
  47. uint32 opcode_index = opcode - SIMD_i8x16_splat;
  48. LLVMValueRef value = NULL, base, new_vector;
  49. LLVMValueRef undefs[] = {
  50. LLVM_CONST(i8x16_undef), LLVM_CONST(i16x8_undef),
  51. LLVM_CONST(i32x4_undef), LLVM_CONST(i64x2_undef),
  52. LLVM_CONST(f32x4_undef), LLVM_CONST(f64x2_undef),
  53. };
  54. LLVMValueRef masks[] = {
  55. LLVM_CONST(i32x16_zero), LLVM_CONST(i32x8_zero), LLVM_CONST(i32x4_zero),
  56. LLVM_CONST(i32x2_zero), LLVM_CONST(i32x4_zero), LLVM_CONST(i32x2_zero),
  57. };
  58. switch (opcode) {
  59. case SIMD_i8x16_splat:
  60. {
  61. LLVMValueRef input;
  62. POP_I32(input);
  63. /* trunc i32 %input to i8 */
  64. value =
  65. LLVMBuildTrunc(comp_ctx->builder, input, INT8_TYPE, "trunc");
  66. break;
  67. }
  68. case SIMD_i16x8_splat:
  69. {
  70. LLVMValueRef input;
  71. POP_I32(input);
  72. /* trunc i32 %input to i16 */
  73. value =
  74. LLVMBuildTrunc(comp_ctx->builder, input, INT16_TYPE, "trunc");
  75. break;
  76. }
  77. case SIMD_i32x4_splat:
  78. {
  79. POP_I32(value);
  80. break;
  81. }
  82. case SIMD_i64x2_splat:
  83. {
  84. POP(value, VALUE_TYPE_I64);
  85. break;
  86. }
  87. case SIMD_f32x4_splat:
  88. {
  89. POP(value, VALUE_TYPE_F32);
  90. break;
  91. }
  92. case SIMD_f64x2_splat:
  93. {
  94. POP(value, VALUE_TYPE_F64);
  95. break;
  96. }
  97. default:
  98. {
  99. break;
  100. }
  101. }
  102. if (!value) {
  103. goto fail;
  104. }
  105. /* insertelement <n x ty> undef, ty %value, i32 0 */
  106. if (!(base = LLVMBuildInsertElement(comp_ctx->builder, undefs[opcode_index],
  107. value, I32_ZERO, "base"))) {
  108. HANDLE_FAILURE("LLVMBuildInsertElement");
  109. goto fail;
  110. }
  111. /* shufflevector <ty1> %base, <ty2> undef, <n x i32> zeroinitializer */
  112. if (!(new_vector = LLVMBuildShuffleVector(
  113. comp_ctx->builder, base, undefs[opcode_index],
  114. masks[opcode_index], "new_vector"))) {
  115. HANDLE_FAILURE("LLVMBuildShuffleVector");
  116. goto fail;
  117. }
  118. return simd_bitcast_and_push_v128(comp_ctx, func_ctx, new_vector, "result");
  119. fail:
  120. return false;
  121. }