2013-03-26 03:25:37 +01:00
|
|
|
//===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-03-26 03:25:37 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2014-01-07 12:48:04 +01:00
|
|
|
#include "llvm-c/IRReader.h"
|
2014-01-07 13:34:26 +01:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2016-11-11 06:34:58 +01:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2013-11-06 10:21:15 +01:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2013-03-26 03:25:37 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2013-04-03 17:33:45 +02:00
|
|
|
#include "llvm/Support/Timer.h"
|
2013-11-06 10:21:15 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-06-12 19:38:55 +02:00
|
|
|
#include <system_error>
|
2013-03-26 03:25:37 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-04-03 17:33:45 +02:00
|
|
|
namespace llvm {
|
|
|
|
extern bool TimePassesIsEnabled;
|
|
|
|
}
|
|
|
|
|
2020-12-01 19:33:18 +01:00
|
|
|
const char TimeIRParsingGroupName[] = "irparse";
|
|
|
|
const char TimeIRParsingGroupDescription[] = "LLVM IR Parsing";
|
|
|
|
const char TimeIRParsingName[] = "parse";
|
|
|
|
const char TimeIRParsingDescription[] = "Parse IR";
|
2013-04-03 17:33:45 +02:00
|
|
|
|
2019-02-11 23:01:13 +01:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
llvm::getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context, bool ShouldLazyLoadMetadata) {
|
2013-03-26 03:25:37 +01:00
|
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
2016-11-13 08:00:17 +01:00
|
|
|
Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
|
2015-12-17 18:14:09 +01:00
|
|
|
std::move(Buffer), Context, ShouldLazyLoadMetadata);
|
2016-11-13 08:00:17 +01:00
|
|
|
if (Error E = ModuleOrErr.takeError()) {
|
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
|
|
|
|
EIB.message());
|
|
|
|
});
|
2014-04-15 08:32:26 +02:00
|
|
|
return nullptr;
|
2013-03-26 03:25:37 +01:00
|
|
|
}
|
2015-06-17 00:27:55 +02:00
|
|
|
return std::move(ModuleOrErr.get());
|
2013-03-26 03:25:37 +01:00
|
|
|
}
|
|
|
|
|
2014-08-26 23:49:01 +02:00
|
|
|
return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
|
2013-03-26 03:25:37 +01:00
|
|
|
}
|
|
|
|
|
2014-08-26 19:29:46 +02:00
|
|
|
std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
|
|
|
|
SMDiagnostic &Err,
|
2015-12-17 18:14:09 +01:00
|
|
|
LLVMContext &Context,
|
|
|
|
bool ShouldLazyLoadMetadata) {
|
2014-07-06 19:43:13 +02:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Filename);
|
|
|
|
if (std::error_code EC = FileOrErr.getError()) {
|
2013-03-26 03:25:37 +01:00
|
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
2014-07-06 19:43:13 +02:00
|
|
|
"Could not open input file: " + EC.message());
|
2014-04-15 08:32:26 +02:00
|
|
|
return nullptr;
|
2013-03-26 03:25:37 +01:00
|
|
|
}
|
|
|
|
|
2015-12-17 18:14:09 +01:00
|
|
|
return getLazyIRModule(std::move(FileOrErr.get()), Err, Context,
|
|
|
|
ShouldLazyLoadMetadata);
|
2013-03-26 03:25:37 +01:00
|
|
|
}
|
|
|
|
|
2014-08-26 23:49:01 +02:00
|
|
|
std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
|
2017-10-02 20:31:29 +02:00
|
|
|
LLVMContext &Context,
|
Infer alignment of unmarked loads in IR/bitcode parsing.
For IR generated by a compiler, this is really simple: you just take the
datalayout from the beginning of the file, and apply it to all the IR
later in the file. For optimization testcases that don't care about the
datalayout, this is also really simple: we just use the default
datalayout.
The complexity here comes from the fact that some LLVM tools allow
overriding the datalayout: some tools have an explicit flag for this,
some tools will infer a datalayout based on the code generation target.
Supporting this properly required plumbing through a bunch of new
machinery: we want to allow overriding the datalayout after the
datalayout is parsed from the file, but before we use any information
from it. Therefore, IR/bitcode parsing now has a callback to allow tools
to compute the datalayout at the appropriate time.
Not sure if I covered all the LLVM tools that want to use the callback.
(clang? lli? Misc IR manipulation tools like llvm-link?). But this is at
least enough for all the LLVM regression tests, and IR without a
datalayout is not something frontends should generate.
This change had some sort of weird effects for certain CodeGen
regression tests: if the datalayout is overridden with a datalayout with
a different program or stack address space, we now parse IR based on the
overridden datalayout, instead of the one written in the file (or the
default one, if none is specified). This broke a few AVR tests, and one
AMDGPU test.
Outside the CodeGen tests I mentioned, the test changes are all just
fixing CHECK lines and moving around datalayout lines in weird places.
Differential Revision: https://reviews.llvm.org/D78403
2020-05-14 21:59:45 +02:00
|
|
|
DataLayoutCallbackTy DataLayoutCallback) {
|
2016-11-18 20:43:18 +01:00
|
|
|
NamedRegionTimer T(TimeIRParsingName, TimeIRParsingDescription,
|
|
|
|
TimeIRParsingGroupName, TimeIRParsingGroupDescription,
|
2013-04-03 17:33:45 +02:00
|
|
|
TimePassesIsEnabled);
|
2014-08-26 23:49:01 +02:00
|
|
|
if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer.getBufferEnd())) {
|
2016-11-13 08:00:17 +01:00
|
|
|
Expected<std::unique_ptr<Module>> ModuleOrErr =
|
Infer alignment of unmarked loads in IR/bitcode parsing.
For IR generated by a compiler, this is really simple: you just take the
datalayout from the beginning of the file, and apply it to all the IR
later in the file. For optimization testcases that don't care about the
datalayout, this is also really simple: we just use the default
datalayout.
The complexity here comes from the fact that some LLVM tools allow
overriding the datalayout: some tools have an explicit flag for this,
some tools will infer a datalayout based on the code generation target.
Supporting this properly required plumbing through a bunch of new
machinery: we want to allow overriding the datalayout after the
datalayout is parsed from the file, but before we use any information
from it. Therefore, IR/bitcode parsing now has a callback to allow tools
to compute the datalayout at the appropriate time.
Not sure if I covered all the LLVM tools that want to use the callback.
(clang? lli? Misc IR manipulation tools like llvm-link?). But this is at
least enough for all the LLVM regression tests, and IR without a
datalayout is not something frontends should generate.
This change had some sort of weird effects for certain CodeGen
regression tests: if the datalayout is overridden with a datalayout with
a different program or stack address space, we now parse IR based on the
overridden datalayout, instead of the one written in the file (or the
default one, if none is specified). This broke a few AVR tests, and one
AMDGPU test.
Outside the CodeGen tests I mentioned, the test changes are all just
fixing CHECK lines and moving around datalayout lines in weird places.
Differential Revision: https://reviews.llvm.org/D78403
2020-05-14 21:59:45 +02:00
|
|
|
parseBitcodeFile(Buffer, Context, DataLayoutCallback);
|
2016-11-13 08:00:17 +01:00
|
|
|
if (Error E = ModuleOrErr.takeError()) {
|
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
|
|
|
|
EIB.message());
|
|
|
|
});
|
2014-08-26 19:29:46 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-06-17 00:27:55 +02:00
|
|
|
return std::move(ModuleOrErr.get());
|
2013-03-26 03:25:37 +01:00
|
|
|
}
|
|
|
|
|
Infer alignment of unmarked loads in IR/bitcode parsing.
For IR generated by a compiler, this is really simple: you just take the
datalayout from the beginning of the file, and apply it to all the IR
later in the file. For optimization testcases that don't care about the
datalayout, this is also really simple: we just use the default
datalayout.
The complexity here comes from the fact that some LLVM tools allow
overriding the datalayout: some tools have an explicit flag for this,
some tools will infer a datalayout based on the code generation target.
Supporting this properly required plumbing through a bunch of new
machinery: we want to allow overriding the datalayout after the
datalayout is parsed from the file, but before we use any information
from it. Therefore, IR/bitcode parsing now has a callback to allow tools
to compute the datalayout at the appropriate time.
Not sure if I covered all the LLVM tools that want to use the callback.
(clang? lli? Misc IR manipulation tools like llvm-link?). But this is at
least enough for all the LLVM regression tests, and IR without a
datalayout is not something frontends should generate.
This change had some sort of weird effects for certain CodeGen
regression tests: if the datalayout is overridden with a datalayout with
a different program or stack address space, we now parse IR based on the
overridden datalayout, instead of the one written in the file (or the
default one, if none is specified). This broke a few AVR tests, and one
AMDGPU test.
Outside the CodeGen tests I mentioned, the test changes are all just
fixing CHECK lines and moving around datalayout lines in weird places.
Differential Revision: https://reviews.llvm.org/D78403
2020-05-14 21:59:45 +02:00
|
|
|
return parseAssembly(Buffer, Err, Context, nullptr, DataLayoutCallback);
|
2013-03-26 03:25:37 +01:00
|
|
|
}
|
|
|
|
|
Infer alignment of unmarked loads in IR/bitcode parsing.
For IR generated by a compiler, this is really simple: you just take the
datalayout from the beginning of the file, and apply it to all the IR
later in the file. For optimization testcases that don't care about the
datalayout, this is also really simple: we just use the default
datalayout.
The complexity here comes from the fact that some LLVM tools allow
overriding the datalayout: some tools have an explicit flag for this,
some tools will infer a datalayout based on the code generation target.
Supporting this properly required plumbing through a bunch of new
machinery: we want to allow overriding the datalayout after the
datalayout is parsed from the file, but before we use any information
from it. Therefore, IR/bitcode parsing now has a callback to allow tools
to compute the datalayout at the appropriate time.
Not sure if I covered all the LLVM tools that want to use the callback.
(clang? lli? Misc IR manipulation tools like llvm-link?). But this is at
least enough for all the LLVM regression tests, and IR without a
datalayout is not something frontends should generate.
This change had some sort of weird effects for certain CodeGen
regression tests: if the datalayout is overridden with a datalayout with
a different program or stack address space, we now parse IR based on the
overridden datalayout, instead of the one written in the file (or the
default one, if none is specified). This broke a few AVR tests, and one
AMDGPU test.
Outside the CodeGen tests I mentioned, the test changes are all just
fixing CHECK lines and moving around datalayout lines in weird places.
Differential Revision: https://reviews.llvm.org/D78403
2020-05-14 21:59:45 +02:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
|
|
|
|
DataLayoutCallbackTy DataLayoutCallback) {
|
2014-07-06 19:43:13 +02:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
[NFC] Reordering parameters in getFile and getFileOrSTDIN
In future patches I will be setting the IsText parameter frequently so I will refactor the args to be in the following order. I have removed the FileSize parameter because it is never used.
```
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true, bool IsVolatile = false);
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true);
static ErrorOr<std::unique_ptr<MB>>
getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsText, bool RequiresNullTerminator, bool IsVolatile);
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFile(const Twine &Filename, bool IsVolatile = false);
```
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D99182
2021-03-25 14:47:25 +01:00
|
|
|
MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
|
2014-07-06 19:43:13 +02:00
|
|
|
if (std::error_code EC = FileOrErr.getError()) {
|
2013-03-26 03:25:37 +01:00
|
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
2014-07-06 19:43:13 +02:00
|
|
|
"Could not open input file: " + EC.message());
|
2014-04-15 08:32:26 +02:00
|
|
|
return nullptr;
|
2013-03-26 03:25:37 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 20:31:29 +02:00
|
|
|
return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context,
|
Infer alignment of unmarked loads in IR/bitcode parsing.
For IR generated by a compiler, this is really simple: you just take the
datalayout from the beginning of the file, and apply it to all the IR
later in the file. For optimization testcases that don't care about the
datalayout, this is also really simple: we just use the default
datalayout.
The complexity here comes from the fact that some LLVM tools allow
overriding the datalayout: some tools have an explicit flag for this,
some tools will infer a datalayout based on the code generation target.
Supporting this properly required plumbing through a bunch of new
machinery: we want to allow overriding the datalayout after the
datalayout is parsed from the file, but before we use any information
from it. Therefore, IR/bitcode parsing now has a callback to allow tools
to compute the datalayout at the appropriate time.
Not sure if I covered all the LLVM tools that want to use the callback.
(clang? lli? Misc IR manipulation tools like llvm-link?). But this is at
least enough for all the LLVM regression tests, and IR without a
datalayout is not something frontends should generate.
This change had some sort of weird effects for certain CodeGen
regression tests: if the datalayout is overridden with a datalayout with
a different program or stack address space, we now parse IR based on the
overridden datalayout, instead of the one written in the file (or the
default one, if none is specified). This broke a few AVR tests, and one
AMDGPU test.
Outside the CodeGen tests I mentioned, the test changes are all just
fixing CHECK lines and moving around datalayout lines in weird places.
Differential Revision: https://reviews.llvm.org/D78403
2020-05-14 21:59:45 +02:00
|
|
|
DataLayoutCallback);
|
2013-03-26 03:25:37 +01:00
|
|
|
}
|
2013-11-06 10:21:15 +01:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
|
|
|
|
LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
|
|
|
|
char **OutMessage) {
|
|
|
|
SMDiagnostic Diag;
|
|
|
|
|
2014-06-27 06:33:58 +02:00
|
|
|
std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
|
2014-08-26 23:49:01 +02:00
|
|
|
*OutM =
|
|
|
|
wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
|
2013-11-06 10:21:15 +01:00
|
|
|
|
|
|
|
if(!*OutM) {
|
|
|
|
if (OutMessage) {
|
2014-06-27 00:52:05 +02:00
|
|
|
std::string buf;
|
|
|
|
raw_string_ostream os(buf);
|
|
|
|
|
2014-04-15 08:32:26 +02:00
|
|
|
Diag.print(nullptr, os, false);
|
2014-06-27 00:52:05 +02:00
|
|
|
os.flush();
|
|
|
|
|
|
|
|
*OutMessage = strdup(buf.c_str());
|
2013-11-06 10:21:15 +01:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|