Skip to content

Commit

Permalink
Load LLVM IR from string
Browse files Browse the repository at this point in the history
  • Loading branch information
certik committed Sep 5, 2019
1 parent 5ea3148 commit 54fa33f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
23 changes: 19 additions & 4 deletions src/lfortran/tests/test_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -44,11 +47,23 @@ int main() {
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();

auto context = std::make_shared<llvm::LLVMContext>();
std::shared_ptr<llvm::LLVMContext> context
= std::make_shared<llvm::LLVMContext>();

llvm::SMDiagnostic err;
std::string asm_string = R"""(;
define i64 @f1()
{
ret i64 4
}
)""";
std::unique_ptr<llvm::Module> module
= llvm::make_unique<llvm::Module>("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;
}

0 comments on commit 54fa33f

Please sign in to comment.