| 1 | //===---- IRReader.cpp - Reader for LLVM IR files -------------------------===// |
| 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 | #include "llvm/IRReader/IRReader.h" |
| 10 | #include "llvm-c/IRReader.h" |
| 11 | #include "llvm/AsmParser/AsmParserContext.h" |
| 12 | #include "llvm/AsmParser/Parser.h" |
| 13 | #include "llvm/Bitcode/BitcodeReader.h" |
| 14 | #include "llvm/IR/LLVMContext.h" |
| 15 | #include "llvm/IR/Module.h" |
| 16 | #include "llvm/IR/PassTimingInfo.h" |
| 17 | #include "llvm/Support/MemoryBuffer.h" |
| 18 | #include "llvm/Support/SourceMgr.h" |
| 19 | #include "llvm/Support/Timer.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | #include <optional> |
| 22 | #include <system_error> |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | const char TimeIRParsingGroupName[] = "irparse" ; |
| 27 | const char TimeIRParsingGroupDescription[] = "LLVM IR Parsing" ; |
| 28 | const char TimeIRParsingName[] = "parse" ; |
| 29 | const char TimeIRParsingDescription[] = "Parse IR" ; |
| 30 | |
| 31 | std::unique_ptr<Module> |
| 32 | llvm::getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err, |
| 33 | LLVMContext &Context, bool ShouldLazyLoadMetadata) { |
| 34 | if (isBitcode(BufPtr: (const unsigned char *)Buffer->getBufferStart(), |
| 35 | BufEnd: (const unsigned char *)Buffer->getBufferEnd())) { |
| 36 | Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule( |
| 37 | Buffer: std::move(Buffer), Context, ShouldLazyLoadMetadata); |
| 38 | if (Error E = ModuleOrErr.takeError()) { |
| 39 | handleAllErrors(E: std::move(E), Handlers: [&](ErrorInfoBase &EIB) { |
| 40 | Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, |
| 41 | EIB.message()); |
| 42 | }); |
| 43 | return nullptr; |
| 44 | } |
| 45 | return std::move(ModuleOrErr.get()); |
| 46 | } |
| 47 | |
| 48 | return parseAssembly(F: Buffer->getMemBufferRef(), Err, Context); |
| 49 | } |
| 50 | |
| 51 | std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename, |
| 52 | SMDiagnostic &Err, |
| 53 | LLVMContext &Context, |
| 54 | bool ShouldLazyLoadMetadata) { |
| 55 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 56 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 57 | if (std::error_code EC = FileOrErr.getError()) { |
| 58 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 59 | "Could not open input file: " + EC.message()); |
| 60 | return nullptr; |
| 61 | } |
| 62 | |
| 63 | return getLazyIRModule(Buffer: std::move(FileOrErr.get()), Err, Context, |
| 64 | ShouldLazyLoadMetadata); |
| 65 | } |
| 66 | |
| 67 | std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err, |
| 68 | LLVMContext &Context, |
| 69 | ParserCallbacks Callbacks, |
| 70 | llvm::AsmParserContext *ParserContext) { |
| 71 | NamedRegionTimer T(TimeIRParsingName, TimeIRParsingDescription, |
| 72 | TimeIRParsingGroupName, TimeIRParsingGroupDescription, |
| 73 | TimePassesIsEnabled); |
| 74 | if (isBitcode(BufPtr: (const unsigned char *)Buffer.getBufferStart(), |
| 75 | BufEnd: (const unsigned char *)Buffer.getBufferEnd())) { |
| 76 | Expected<std::unique_ptr<Module>> ModuleOrErr = |
| 77 | parseBitcodeFile(Buffer, Context, Callbacks); |
| 78 | if (Error E = ModuleOrErr.takeError()) { |
| 79 | handleAllErrors(E: std::move(E), Handlers: [&](ErrorInfoBase &EIB) { |
| 80 | Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error, |
| 81 | EIB.message()); |
| 82 | }); |
| 83 | return nullptr; |
| 84 | } |
| 85 | return std::move(ModuleOrErr.get()); |
| 86 | } |
| 87 | |
| 88 | return parseAssembly(F: Buffer, Err, Context, Slots: nullptr, |
| 89 | DataLayoutCallback: Callbacks.DataLayout.value_or( |
| 90 | u: [](StringRef, StringRef) { return std::nullopt; }), |
| 91 | ParserContext); |
| 92 | } |
| 93 | |
| 94 | std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err, |
| 95 | LLVMContext &Context, |
| 96 | ParserCallbacks Callbacks, |
| 97 | AsmParserContext *ParserContext) { |
| 98 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 99 | MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true); |
| 100 | if (std::error_code EC = FileOrErr.getError()) { |
| 101 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 102 | "Could not open input file: " + EC.message()); |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | return parseIR(Buffer: FileOrErr.get()->getMemBufferRef(), Err, Context, Callbacks, |
| 107 | ParserContext); |
| 108 | } |
| 109 | |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | // C API. |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | |
| 114 | LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef, |
| 115 | LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, |
| 116 | char **OutMessage) { |
| 117 | std::unique_ptr<MemoryBuffer> MB(unwrap(P: MemBuf)); |
| 118 | return LLVMParseIRInContext2(ContextRef, MemBuf: wrap(P: MB.get()), OutM, OutMessage); |
| 119 | } |
| 120 | |
| 121 | LLVMBool LLVMParseIRInContext2(LLVMContextRef ContextRef, |
| 122 | LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, |
| 123 | char **OutMessage) { |
| 124 | SMDiagnostic Diag; |
| 125 | |
| 126 | *OutM = wrap(P: parseIR(Buffer: *unwrap(P: MemBuf), Err&: Diag, Context&: *unwrap(P: ContextRef)).release()); |
| 127 | |
| 128 | if (*OutM) |
| 129 | return 0; |
| 130 | |
| 131 | if (OutMessage) { |
| 132 | std::string Buf; |
| 133 | raw_string_ostream OS(Buf); |
| 134 | Diag.print(ProgName: nullptr, S&: OS, /*ShowColors=*/false); |
| 135 | *OutMessage = strdup(s: Buf.c_str()); |
| 136 | } |
| 137 | |
| 138 | return 1; |
| 139 | } |
| 140 | |