aot_llvm_extra.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <llvm/ADT/SmallVector.h>
  6. #include <llvm/ADT/Twine.h>
  7. #include <llvm/ADT/Triple.h>
  8. #include <llvm/ExecutionEngine/ExecutionEngine.h>
  9. #include <llvm/MC/MCSubtargetInfo.h>
  10. #include <llvm/Support/TargetSelect.h>
  11. #include <llvm/Target/TargetMachine.h>
  12. #include <llvm-c/Core.h>
  13. #include <llvm-c/ExecutionEngine.h>
  14. #include <llvm/ExecutionEngine/GenericValue.h>
  15. #include <llvm/ExecutionEngine/JITEventListener.h>
  16. #include <llvm/ExecutionEngine/RTDyldMemoryManager.h>
  17. #include <llvm/IR/DerivedTypes.h>
  18. #include <llvm/IR/Module.h>
  19. #include <llvm/Support/ErrorHandling.h>
  20. #include <llvm/Target/CodeGenCWrappers.h>
  21. #include <llvm/Target/TargetOptions.h>
  22. #include <cstring>
  23. using namespace llvm;
  24. extern "C" LLVMBool
  25. WAMRCreateMCJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
  26. LLVMModuleRef M,
  27. LLVMMCJITCompilerOptions *PassedOptions,
  28. size_t SizeOfPassedOptions, char **OutError);
  29. extern "C" bool
  30. aot_check_simd_compatibility(const char *arch_c_str, const char *cpu_c_str);
  31. LLVMBool
  32. WAMRCreateMCJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
  33. LLVMModuleRef M,
  34. LLVMMCJITCompilerOptions *PassedOptions,
  35. size_t SizeOfPassedOptions, char **OutError)
  36. {
  37. LLVMMCJITCompilerOptions options;
  38. // If the user passed a larger sized options struct, then they were compiled
  39. // against a newer LLVM. Tell them that something is wrong.
  40. if (SizeOfPassedOptions > sizeof(options)) {
  41. *OutError = strdup("Refusing to use options struct that is larger than "
  42. "my own; assuming LLVM library mismatch.");
  43. return 1;
  44. }
  45. // Defend against the user having an old version of the API by ensuring that
  46. // any fields they didn't see are cleared. We must defend against fields
  47. // being set to the bitwise equivalent of zero, and assume that this means
  48. // "do the default" as if that option hadn't been available.
  49. LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
  50. memcpy(&options, PassedOptions, SizeOfPassedOptions);
  51. TargetOptions targetOptions;
  52. targetOptions.EnableFastISel = options.EnableFastISel;
  53. std::unique_ptr<Module> Mod(unwrap(M));
  54. if (Mod) {
  55. // Set function attribute "frame-pointer" based on
  56. // NoFramePointerElim.
  57. for (auto &F : *Mod) {
  58. auto Attrs = F.getAttributes();
  59. StringRef Value = options.NoFramePointerElim ? "all" : "none";
  60. Attrs =
  61. Attrs.addAttribute(F.getContext(), AttributeList::FunctionIndex,
  62. "frame-pointer", Value);
  63. F.setAttributes(Attrs);
  64. }
  65. }
  66. std::string Error;
  67. bool JIT;
  68. char *host_cpu = LLVMGetHostCPUName();
  69. if (!host_cpu) {
  70. *OutError = NULL;
  71. return false;
  72. }
  73. std::string mcpu(host_cpu);
  74. LLVMDisposeMessage(host_cpu);
  75. EngineBuilder builder(std::move(Mod));
  76. builder.setEngineKind(EngineKind::JIT)
  77. .setErrorStr(&Error)
  78. .setMCPU(mcpu)
  79. .setOptLevel((CodeGenOpt::Level)options.OptLevel)
  80. .setTargetOptions(targetOptions);
  81. if (Optional<CodeModel::Model> CM = unwrap(options.CodeModel, JIT))
  82. builder.setCodeModel(*CM);
  83. if (options.MCJMM)
  84. builder.setMCJITMemoryManager(
  85. std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM)));
  86. if (ExecutionEngine *JIT = builder.create()) {
  87. *OutJIT = wrap(JIT);
  88. return 0;
  89. }
  90. *OutError = strdup(Error.c_str());
  91. return 1;
  92. }
  93. bool
  94. aot_check_simd_compatibility(const char *arch_c_str, const char *cpu_c_str)
  95. {
  96. #if WASM_ENABLE_SIMD != 0
  97. if (!arch_c_str || !cpu_c_str) {
  98. return false;
  99. }
  100. llvm::SmallVector<std::string, 1> targetAttributes;
  101. llvm::Triple targetTriple(arch_c_str, "", "");
  102. auto targetMachine =
  103. std::unique_ptr<llvm::TargetMachine>(llvm::EngineBuilder().selectTarget(
  104. targetTriple, "", std::string(cpu_c_str), targetAttributes));
  105. if (!targetMachine) {
  106. return false;
  107. }
  108. const llvm::Triple::ArchType targetArch =
  109. targetMachine->getTargetTriple().getArch();
  110. const llvm::MCSubtargetInfo *subTargetInfo =
  111. targetMachine->getMCSubtargetInfo();
  112. if (subTargetInfo == nullptr) {
  113. return false;
  114. }
  115. if (targetArch == llvm::Triple::x86_64) {
  116. return subTargetInfo->checkFeatures("+sse4.1");
  117. }
  118. else if (targetArch == llvm::Triple::aarch64) {
  119. return subTargetInfo->checkFeatures("+neon");
  120. }
  121. else {
  122. return false;
  123. }
  124. #else
  125. (void)arch_c_str;
  126. (void)cpu_c_str;
  127. return true;
  128. #endif /* WASM_ENABLE_SIMD */
  129. }