ExampleModules.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //===----- ExampleModules.h - IR modules for LLJIT examples -----*- C++ -*-===//
  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. //
  9. // Example modules for LLJIT examples
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_EXAMPLES_HOWTOUSELLJIT_EXAMPLEMODULES_H
  13. #define LLVM_EXAMPLES_HOWTOUSELLJIT_EXAMPLEMODULES_H
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
  16. #include "llvm/IR/LLVMContext.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/IRReader/IRReader.h"
  19. #include "llvm/Support/Error.h"
  20. #include "llvm/Support/SourceMgr.h"
  21. const llvm::StringRef Add1Example =
  22. R"(
  23. define i32 @add1(i32 %x) {
  24. entry:
  25. %r = add nsw i32 %x, 1
  26. ret i32 %r
  27. }
  28. )";
  29. inline llvm::Expected<llvm::orc::ThreadSafeModule>
  30. parseExampleModule(llvm::StringRef Source, llvm::StringRef Name) {
  31. using namespace llvm;
  32. using namespace llvm::orc;
  33. auto Ctx = std::make_unique<LLVMContext>();
  34. SMDiagnostic Err;
  35. auto M = parseIR(MemoryBufferRef(Source, Name), Err, *Ctx);
  36. if (!M) {
  37. std::string ErrMsg;
  38. {
  39. raw_string_ostream ErrStream(ErrMsg);
  40. Err.print("", ErrStream);
  41. }
  42. return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
  43. }
  44. return ThreadSafeModule(std::move(M), std::move(Ctx));
  45. }
  46. #endif // LLVM_EXAMPLES_HOWTOUSELLJIT_EXAMPLEMODULES_H