brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · 40a5445 Raw
117 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/Bitcode/BitcodeReader.h"10#include "llvm/Bitcode/BitcodeWriter.h"11 12#include "llvm/Config/llvm-config.h"13#include "llvm/IR/Function.h"14#include "llvm/IR/GlobalVariable.h"15#include "llvm/IR/LLVMContext.h"16#include "llvm/IR/Module.h"17#include "llvm/IRReader/IRReader.h"18#include "llvm/Support/CommandLine.h"19#include "llvm/Support/ErrorOr.h"20#include "llvm/Support/FileSystem.h"21#include "llvm/Support/ManagedStatic.h"22#include "llvm/Support/MemoryBuffer.h"23#include "llvm/Support/SourceMgr.h"24#include "llvm/Support/ToolOutputFile.h"25#include "llvm/Support/raw_ostream.h"26 27#include <system_error>28 29using namespace llvm;30 31static ExitOnError ExitOnErr;32 33static cl::opt<std::string>34InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));35 36static cl::opt<std::string>37OutputFilename("o", cl::desc("Output filename"),38               cl::value_desc("filename"));39 40static cl::opt<bool> TextualOut("S", cl::desc("Emit LLVM textual assembly"),41                                cl::init(false));42 43int main(int argc, char **argv) {44  LLVMContext Context;45  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.46 47  cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");48 49  std::string ErrorMessage;50  Module *M = nullptr;51 52  {53    ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =54      MemoryBuffer::getFile(InputFilename);55    if (std::error_code  ec = BufferOrErr.getError()) {56      ErrorMessage = ec.message();57    } else {58      std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();59      SMDiagnostic Err;60      std::unique_ptr<llvm::Module> MPtr =61          ExitOnErr(Expected<std::unique_ptr<llvm::Module>>(62              parseIR(BufferPtr.get()->getMemBufferRef(), Err, Context)));63      M = MPtr.release();64    }65  }66 67  if (!M) {68    errs() << argv[0] << ": ";69    if (ErrorMessage.size())70      errs() << ErrorMessage << "\n";71    else72      errs() << "bitcode didn't read correctly.\n";73    return 1;74  }75 76  // Strip the OpenCL version metadata. There are a lot of linked77  // modules in the library build, each spamming the same78  // version. This may also report a different version than the user79  // program is using. This should probably be uniqued when linking.80  if (NamedMDNode *OCLVersion = M->getNamedMetadata("opencl.ocl.version"))81      M->eraseNamedMetadata(OCLVersion);82 83  // Set linkage of every external definition to linkonce_odr.84  for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {85    if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)86      i->setLinkage(GlobalValue::LinkOnceODRLinkage);87  }88 89  for (Module::global_iterator i = M->global_begin(), e = M->global_end();90       i != e; ++i) {91    if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)92      i->setLinkage(GlobalValue::LinkOnceODRLinkage);93  }94 95  if (OutputFilename.empty()) {96    errs() << "no output file\n";97    return 1;98  }99 100  std::error_code EC;101  std::unique_ptr<ToolOutputFile> Out(102      new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));103  if (EC) {104    errs() << EC.message() << '\n';105    exit(1);106  }107 108  if (TextualOut)109    M->print(Out->os(), nullptr, true);110  else111    WriteBitcodeToFile(*M, Out->os());112 113  // Declare success.114  Out->keep();115  return 0;116}117