2013-03-26 10:25:37 +08:00
|
|
|
//===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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 10:25:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm-c/IRReader.h"
|
2014-01-07 20:34:26 +08:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2013-11-06 17:21:15 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2013-03-26 10:25:37 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2013-04-03 23:33:45 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2013-11-06 17:21:15 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2013-03-26 10:25:37 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-04-03 23:33:45 +08:00
|
|
|
namespace llvm {
|
|
|
|
extern bool TimePassesIsEnabled;
|
|
|
|
}
|
|
|
|
|
2016-11-19 03:43:18 +08:00
|
|
|
static const char *const TimeIRParsingGroupName = "irparse";
|
|
|
|
static const char *const TimeIRParsingGroupDescription = "LLVM IR Parsing";
|
|
|
|
static const char *const TimeIRParsingName = "parse";
|
|
|
|
static const char *const TimeIRParsingDescription = "Parse IR";
|
2013-04-03 23:33:45 +08:00
|
|
|
|
2019-02-12 06:01:13 +08:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
llvm::getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context, bool ShouldLazyLoadMetadata) {
|
2013-03-26 10:25:37 +08:00
|
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
|
2015-12-18 01:14:09 +08:00
|
|
|
std::move(Buffer), Context, ShouldLazyLoadMetadata);
|
2016-11-13 15:00:17 +08:00
|
|
|
if (Error E = ModuleOrErr.takeError()) {
|
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
|
|
|
|
EIB.message());
|
|
|
|
});
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
2015-06-17 06:27:55 +08:00
|
|
|
return std::move(ModuleOrErr.get());
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2014-08-27 05:49:01 +08:00
|
|
|
return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2014-08-27 01:29:46 +08:00
|
|
|
std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
|
|
|
|
SMDiagnostic &Err,
|
2015-12-18 01:14:09 +08:00
|
|
|
LLVMContext &Context,
|
|
|
|
bool ShouldLazyLoadMetadata) {
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Filename);
|
|
|
|
if (std::error_code EC = FileOrErr.getError()) {
|
2013-03-26 10:25:37 +08:00
|
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
2014-07-07 01:43:13 +08:00
|
|
|
"Could not open input file: " + EC.message());
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2015-12-18 01:14:09 +08:00
|
|
|
return getLazyIRModule(std::move(FileOrErr.get()), Err, Context,
|
|
|
|
ShouldLazyLoadMetadata);
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2014-08-27 05:49:01 +08:00
|
|
|
std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
|
2017-10-03 02:31:29 +08: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-15 03:59:45 +08:00
|
|
|
DataLayoutCallbackTy DataLayoutCallback) {
|
2016-11-19 03:43:18 +08:00
|
|
|
NamedRegionTimer T(TimeIRParsingName, TimeIRParsingDescription,
|
|
|
|
TimeIRParsingGroupName, TimeIRParsingGroupDescription,
|
2013-04-03 23:33:45 +08:00
|
|
|
TimePassesIsEnabled);
|
2014-08-27 05:49:01 +08:00
|
|
|
if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer.getBufferEnd())) {
|
2016-11-13 15:00:17 +08: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-15 03:59:45 +08:00
|
|
|
parseBitcodeFile(Buffer, Context, DataLayoutCallback);
|
2016-11-13 15:00:17 +08:00
|
|
|
if (Error E = ModuleOrErr.takeError()) {
|
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
|
|
|
|
EIB.message());
|
|
|
|
});
|
2014-08-27 01:29:46 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-06-17 06:27:55 +08:00
|
|
|
return std::move(ModuleOrErr.get());
|
2013-03-26 10:25:37 +08: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-15 03:59:45 +08:00
|
|
|
return parseAssembly(Buffer, Err, Context, nullptr, DataLayoutCallback);
|
2013-03-26 10:25:37 +08: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-15 03:59:45 +08:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
|
|
|
|
DataLayoutCallbackTy DataLayoutCallback) {
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Filename);
|
|
|
|
if (std::error_code EC = FileOrErr.getError()) {
|
2013-03-26 10:25:37 +08:00
|
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
2014-07-07 01:43:13 +08:00
|
|
|
"Could not open input file: " + EC.message());
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2017-10-03 02:31:29 +08: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-15 03:59:45 +08:00
|
|
|
DataLayoutCallback);
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
2013-11-06 17:21:15 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
|
|
|
|
LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
|
|
|
|
char **OutMessage) {
|
|
|
|
SMDiagnostic Diag;
|
|
|
|
|
2014-06-27 12:33:58 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
|
2014-08-27 05:49:01 +08:00
|
|
|
*OutM =
|
|
|
|
wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
|
2013-11-06 17:21:15 +08:00
|
|
|
|
|
|
|
if(!*OutM) {
|
|
|
|
if (OutMessage) {
|
2014-06-27 06:52:05 +08:00
|
|
|
std::string buf;
|
|
|
|
raw_string_ostream os(buf);
|
|
|
|
|
2014-04-15 14:32:26 +08:00
|
|
|
Diag.print(nullptr, os, false);
|
2014-06-27 06:52:05 +08:00
|
|
|
os.flush();
|
|
|
|
|
|
|
|
*OutMessage = strdup(buf.c_str());
|
2013-11-06 17:21:15 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|