From 54fa33f544ec807466c10289af9bc413745cc752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Thu, 5 Sep 2019 10:25:07 -0600 Subject: [PATCH] Load LLVM IR from string --- CMakeLists.txt | 2 +- src/lfortran/tests/test_llvm.cpp | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index adc6f04b23..0be5048f58 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,7 @@ set(WITH_LFORTRAN_ASSERT ${WITH_LFORTRAN_ASSERT_DEFAULT} # LLVM set(WITH_LLVM no CACHE BOOL "Build with LLVM support") if (WITH_LLVM) - set(LFORTRAN_LLVM_COMPONENTS core support mcjit native) + set(LFORTRAN_LLVM_COMPONENTS core support mcjit native asmparser) find_package(LLVM REQUIRED) message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") diff --git a/src/lfortran/tests/test_llvm.cpp b/src/lfortran/tests/test_llvm.cpp index 974d0d79f2..94e1283104 100644 --- a/src/lfortran/tests/test_llvm.cpp +++ b/src/lfortran/tests/test_llvm.cpp @@ -34,6 +34,9 @@ #include "llvm/ExecutionEngine/ObjectCache.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" +#include "llvm/AsmParser/Parser.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/ADT/StringRef.h" int main() { @@ -44,11 +47,23 @@ int main() { llvm::InitializeNativeTargetAsmPrinter(); llvm::InitializeNativeTargetAsmParser(); - auto context = std::make_shared(); + std::shared_ptr context + = std::make_shared(); + + llvm::SMDiagnostic err; + std::string asm_string = R"""(; +define i64 @f1() +{ + ret i64 4 +} + )"""; std::unique_ptr module - = llvm::make_unique("SymEngine", *context.get()); - module->setDataLayout(""); - auto mod = module.get(); + = llvm::parseAssemblyString(asm_string, err, *context); + if (llvm::verifyModule(*module)) { + std::cerr << "Error: module failed verification." << std::endl; + }; + std::cout << "The loaded module" << std::endl; + llvm::outs() << *module; return 0; }