2003-10-20 19:52:11 +02:00
|
|
|
//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
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
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
2003-08-28 23:32:57 +02:00
|
|
|
// llvm-as --help - Output information about command line switches
|
2007-05-06 11:29:57 +02:00
|
|
|
// llvm-as [options] - Read LLVM asm from stdin, write bitcode to stdout
|
|
|
|
// llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode
|
2003-08-28 23:32:57 +02:00
|
|
|
// to the x.bc file.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2006-05-29 20:52:52 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2014-01-07 13:34:26 +01:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2016-11-11 06:34:58 +01:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2016-04-04 23:06:17 +02:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Module.h"
|
2018-06-26 15:56:49 +02:00
|
|
|
#include "llvm/IR/ModuleSummaryIndex.h"
|
2014-01-13 10:26:24 +01:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-04-30 01:26:49 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2018-04-13 20:26:06 +02:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2006-12-06 02:18:01 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2009-07-03 00:46:18 +02:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2005-01-02 01:08:46 +01:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
2010-10-07 22:32:40 +02:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2002-01-20 23:54:45 +01:00
|
|
|
#include <memory>
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2019-05-27 10:24:06 +02:00
|
|
|
cl::OptionCategory AsCat("llvm-as Options");
|
|
|
|
|
2016-04-04 23:06:17 +02:00
|
|
|
static cl::opt<std::string> InputFilename(cl::Positional,
|
|
|
|
cl::desc("<input .llvm file>"),
|
|
|
|
cl::init("-"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2016-04-04 23:06:17 +02:00
|
|
|
static cl::opt<std::string> OutputFilename("o",
|
|
|
|
cl::desc("Override output filename"),
|
2019-05-27 10:24:06 +02:00
|
|
|
cl::value_desc("filename"),
|
|
|
|
cl::cat(AsCat));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2019-05-27 10:24:06 +02:00
|
|
|
static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"),
|
|
|
|
cl::cat(AsCat));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2016-04-04 23:06:17 +02:00
|
|
|
static cl::opt<bool> DisableOutput("disable-output", cl::desc("Disable output"),
|
2019-05-27 10:24:06 +02:00
|
|
|
cl::init(false), cl::cat(AsCat));
|
2008-02-21 02:41:25 +01:00
|
|
|
|
2016-04-01 07:33:11 +02:00
|
|
|
static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
|
2019-05-27 10:24:06 +02:00
|
|
|
cl::init(false), cl::cat(AsCat));
|
2016-04-01 07:33:11 +02:00
|
|
|
|
2016-04-04 23:06:17 +02:00
|
|
|
static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
|
2019-05-27 10:24:06 +02:00
|
|
|
cl::Hidden, cl::cat(AsCat));
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-08-18 22:47:13 +02:00
|
|
|
static cl::opt<bool>
|
2016-04-04 23:06:17 +02:00
|
|
|
DisableVerify("disable-verify", cl::Hidden,
|
2019-05-27 10:24:06 +02:00
|
|
|
cl::desc("Do not run verifier on input LLVM (dangerous!)"),
|
|
|
|
cl::cat(AsCat));
|
2003-08-18 22:47:13 +02:00
|
|
|
|
2015-04-15 05:14:06 +02:00
|
|
|
static cl::opt<bool> PreserveBitcodeUseListOrder(
|
|
|
|
"preserve-bc-uselistorder",
|
|
|
|
cl::desc("Preserve use-list order when writing LLVM bitcode."),
|
2019-05-27 10:24:06 +02:00
|
|
|
cl::init(true), cl::Hidden, cl::cat(AsCat));
|
2015-04-15 05:14:06 +02:00
|
|
|
|
2018-01-30 23:32:39 +01:00
|
|
|
static cl::opt<std::string> ClDataLayout("data-layout",
|
|
|
|
cl::desc("data layout string to use"),
|
|
|
|
cl::value_desc("layout-string"),
|
2019-05-27 10:24:06 +02:00
|
|
|
cl::init(""), cl::cat(AsCat));
|
2018-01-30 23:32:39 +01:00
|
|
|
|
2018-06-26 15:56:49 +02:00
|
|
|
static void WriteOutputFile(const Module *M, const ModuleSummaryIndex *Index) {
|
2009-08-23 04:51:22 +02:00
|
|
|
// Infer the output filename if needed.
|
|
|
|
if (OutputFilename.empty()) {
|
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2001-07-23 04:35:57 +02:00
|
|
|
} else {
|
2015-05-11 23:20:20 +02:00
|
|
|
StringRef IFN = InputFilename;
|
|
|
|
OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str();
|
2009-08-23 04:51:22 +02:00
|
|
|
OutputFilename += ".bc";
|
2002-01-20 23:54:45 +01:00
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
2009-09-11 22:46:33 +02:00
|
|
|
|
2014-08-25 20:16:47 +02:00
|
|
|
std::error_code EC;
|
2017-09-23 03:03:17 +02:00
|
|
|
std::unique_ptr<ToolOutputFile> Out(
|
2019-08-05 07:43:48 +02:00
|
|
|
new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
|
2014-08-25 20:16:47 +02:00
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
2009-10-17 05:28:28 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-07-08 22:20:34 +02:00
|
|
|
if (Force || !CheckBitcodeOutputToConsole(Out->os())) {
|
2018-06-26 15:56:49 +02:00
|
|
|
const ModuleSummaryIndex *IndexToWrite = nullptr;
|
2020-02-26 14:34:11 +01:00
|
|
|
// Don't attempt to write a summary index unless it contains any entries or
|
|
|
|
// has non-zero flags. The latter is used to assemble dummy index files for
|
|
|
|
// skipping modules by distributed ThinLTO backends. Otherwise we get an empty
|
|
|
|
// summary section.
|
|
|
|
if (Index && (Index->begin() != Index->end() || Index->getFlags()))
|
2018-06-26 15:56:49 +02:00
|
|
|
IndexToWrite = Index;
|
|
|
|
if (!IndexToWrite || (M && (!M->empty() || !M->global_empty())))
|
|
|
|
// If we have a non-empty Module, then we write the Module plus
|
|
|
|
// any non-null Index along with it as a per-module Index.
|
|
|
|
// If both are empty, this will give an empty module block, which is
|
|
|
|
// the expected behavior.
|
|
|
|
WriteBitcodeToFile(*M, Out->os(), PreserveBitcodeUseListOrder,
|
|
|
|
IndexToWrite, EmitModuleHash);
|
|
|
|
else
|
|
|
|
// Otherwise, with an empty Module but non-empty Index, we write a
|
|
|
|
// combined index.
|
|
|
|
WriteIndexToFile(*IndexToWrite, Out->os());
|
|
|
|
}
|
2010-08-20 03:07:01 +02:00
|
|
|
|
|
|
|
// Declare success.
|
|
|
|
Out->keep();
|
2009-10-17 05:28:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2018-04-13 20:26:06 +02:00
|
|
|
InitLLVM X(argc, argv);
|
2016-04-14 23:59:01 +02:00
|
|
|
LLVMContext Context;
|
2019-05-27 10:24:06 +02:00
|
|
|
cl::HideUnrelatedOptions(AsCat);
|
2009-10-17 05:28:28 +02:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
|
|
|
|
|
|
|
|
// Parse the file now...
|
|
|
|
SMDiagnostic Err;
|
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
|
|
|
auto SetDataLayout = [](StringRef) -> Optional<std::string> {
|
|
|
|
if (ClDataLayout.empty())
|
|
|
|
return None;
|
|
|
|
return ClDataLayout;
|
|
|
|
};
|
|
|
|
ParsedModuleAndIndex ModuleAndIndex;
|
|
|
|
if (DisableVerify) {
|
|
|
|
ModuleAndIndex = parseAssemblyFileWithIndexNoUpgradeDebugInfo(
|
|
|
|
InputFilename, Err, Context, nullptr, SetDataLayout);
|
|
|
|
} else {
|
|
|
|
ModuleAndIndex = parseAssemblyFileWithIndex(InputFilename, Err, Context,
|
|
|
|
nullptr, SetDataLayout);
|
|
|
|
}
|
2018-06-26 15:56:49 +02:00
|
|
|
std::unique_ptr<Module> M = std::move(ModuleAndIndex.Mod);
|
2014-04-25 06:24:47 +02:00
|
|
|
if (!M.get()) {
|
2011-10-16 06:47:35 +02:00
|
|
|
Err.print(argv[0], errs());
|
2009-08-23 04:51:22 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2018-06-26 15:56:49 +02:00
|
|
|
std::unique_ptr<ModuleSummaryIndex> Index = std::move(ModuleAndIndex.Index);
|
2009-08-23 04:51:22 +02:00
|
|
|
|
2009-10-17 05:28:28 +02:00
|
|
|
if (!DisableVerify) {
|
2014-01-19 03:22:18 +01:00
|
|
|
std::string ErrorStr;
|
|
|
|
raw_string_ostream OS(ErrorStr);
|
|
|
|
if (verifyModule(*M.get(), &OS)) {
|
2009-10-17 05:28:28 +02:00
|
|
|
errs() << argv[0]
|
|
|
|
<< ": assembly parsed, but does not verify as correct!\n";
|
2014-01-19 03:22:18 +01:00
|
|
|
errs() << OS.str();
|
2009-10-17 05:28:28 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2018-06-26 15:56:49 +02:00
|
|
|
// TODO: Implement and call summary index verifier.
|
2009-10-17 05:28:28 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 15:56:49 +02:00
|
|
|
if (DumpAsm) {
|
2016-04-04 23:06:17 +02:00
|
|
|
errs() << "Here's the assembly:\n" << *M.get();
|
2018-06-26 15:56:49 +02:00
|
|
|
if (Index.get() && Index->begin() != Index->end())
|
|
|
|
Index->print(errs());
|
|
|
|
}
|
2009-10-17 05:28:28 +02:00
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
if (!DisableOutput)
|
2018-06-26 15:56:49 +02:00
|
|
|
WriteOutputFile(M.get(), Index.get());
|
2009-10-17 05:28:28 +02:00
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
return 0;
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|