CompileUtils.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //===------ CompileUtils.cpp - Utilities for compiling IR in the JIT ------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/ExecutionEngine/ObjectCache.h"
  11. #include "llvm/IR/LegacyPassManager.h"
  12. #include "llvm/IR/Module.h"
  13. #include "llvm/MC/MCContext.h"
  14. #include "llvm/Object/ObjectFile.h"
  15. #include "llvm/Support/Error.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. #include "llvm/Support/SmallVectorMemoryBuffer.h"
  19. #include "llvm/Target/TargetMachine.h"
  20. #include <algorithm>
  21. namespace llvm {
  22. namespace orc {
  23. IRSymbolMapper::ManglingOptions
  24. irManglingOptionsFromTargetOptions(const TargetOptions &Opts) {
  25. IRSymbolMapper::ManglingOptions MO;
  26. MO.EmulatedTLS = Opts.EmulatedTLS;
  27. return MO;
  28. }
  29. /// Compile a Module to an ObjectFile.
  30. Expected<SimpleCompiler::CompileResult> SimpleCompiler::operator()(Module &M) {
  31. CompileResult CachedObject = tryToLoadFromObjectCache(M);
  32. if (CachedObject)
  33. return std::move(CachedObject);
  34. SmallVector<char, 0> ObjBufferSV;
  35. {
  36. raw_svector_ostream ObjStream(ObjBufferSV);
  37. legacy::PassManager PM;
  38. MCContext *Ctx;
  39. if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
  40. return make_error<StringError>("Target does not support MC emission",
  41. inconvertibleErrorCode());
  42. PM.run(M);
  43. }
  44. auto ObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
  45. std::move(ObjBufferSV), M.getModuleIdentifier() + "-jitted-objectbuffer",
  46. /*RequiresNullTerminator=*/false);
  47. auto Obj = object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
  48. if (!Obj)
  49. return Obj.takeError();
  50. notifyObjectCompiled(M, *ObjBuffer);
  51. return std::move(ObjBuffer);
  52. }
  53. SimpleCompiler::CompileResult
  54. SimpleCompiler::tryToLoadFromObjectCache(const Module &M) {
  55. if (!ObjCache)
  56. return CompileResult();
  57. return ObjCache->getObject(&M);
  58. }
  59. void SimpleCompiler::notifyObjectCompiled(const Module &M,
  60. const MemoryBuffer &ObjBuffer) {
  61. if (ObjCache)
  62. ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
  63. }
  64. ConcurrentIRCompiler::ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
  65. ObjectCache *ObjCache)
  66. : IRCompiler(irManglingOptionsFromTargetOptions(JTMB.getOptions())),
  67. JTMB(std::move(JTMB)), ObjCache(ObjCache) {}
  68. Expected<std::unique_ptr<MemoryBuffer>>
  69. ConcurrentIRCompiler::operator()(Module &M) {
  70. auto TM = cantFail(JTMB.createTargetMachine());
  71. SimpleCompiler C(*TM, ObjCache);
  72. return C(M);
  73. }
  74. } // end namespace orc
  75. } // end namespace llvm