lin
2025-03-21 36aaa54056c4f4e150f6ee0636610d9a68470a08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright 2016, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "RSSPIRVWriter.h"
#include "bcinfo/MetadataExtractor.h"
#include "spirit/file_utils.h"
 
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/DataStream.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/raw_ostream.h"
 
#include "Context.h"
#include "GlobalMergePass.h"
#include "RSSPIRVWriter.h"
 
#define DEBUG_TYPE "rs2spirv"
 
namespace kExt {
const char SPIRVBinary[] = ".spv";
} // namespace kExt
 
using namespace llvm;
 
static cl::opt<std::string> InputFile(cl::Positional, cl::desc("<input file>"),
                                      cl::init("-"));
 
static cl::opt<std::string> OutputFile("o",
                                       cl::desc("Override output filename"),
                                       cl::value_desc("filename"));
 
static cl::opt<std::string> OutputBitcodeFile("bc",
                                              cl::desc("Override output bitcode filename"),
                                              cl::value_desc("bitcode filename"));
 
static std::string removeExt(const std::string &FileName) {
  size_t Pos = FileName.find_last_of(".");
  if (Pos != std::string::npos)
    return FileName.substr(0, Pos);
  return FileName;
}
 
static bool WriteBitcode(rs2spirv::Context &Ctxt, Module *M,
                         raw_ostream &OS, std::string &ErrMsg) {
  llvm::legacy::PassManager PassMgr;
  PassMgr.add(rs2spirv::createGlobalMergePass(true));
  PassMgr.run(*M);
 
  WriteBitcodeToFile(M, OS);
 
  return true;
}
 
static int convertLLVMToSPIRV() {
  LLVMContext Context;
 
  std::string Err;
  auto DS = getDataFileStreamer(InputFile, &Err);
  if (!DS) {
    errs() << "Fails to open input file: " << Err;
    return -1;
  }
  ErrorOr<std::unique_ptr<Module>> MOrErr =
      getStreamedBitcodeModule(InputFile, std::move(DS), Context);
 
  if (std::error_code EC = MOrErr.getError()) {
    errs() << "Fails to load bitcode: " << EC.message();
    return -1;
  }
 
  std::unique_ptr<Module> M = std::move(*MOrErr);
 
  if (std::error_code EC = M->materializeAll()) {
    errs() << "Fails to materialize: " << EC.message();
    return -1;
  }
 
  std::error_code EC;
 
  std::vector<char> bitcode = android::spirit::readFile<char>(InputFile);
  std::unique_ptr<bcinfo::MetadataExtractor> ME(
      new bcinfo::MetadataExtractor(bitcode.data(), bitcode.size()));
 
  rs2spirv::Context &Ctxt = rs2spirv::Context::getInstance();
 
  if (!Ctxt.Initialize(std::move(ME))) {
    return -2;
  }
 
  if (!OutputBitcodeFile.empty()) {
    llvm::StringRef outBCFile(OutputBitcodeFile);
    llvm::raw_fd_ostream OFS_BC(outBCFile, EC, llvm::sys::fs::F_None);
    if (!WriteBitcode(Ctxt, M.get(), OFS_BC, Err)) {
      errs() << "compiler error: " << Err << '\n';
      return -3;
    }
    return 0;
  }
 
  if (OutputFile.empty()) {
    if (InputFile == "-")
      OutputFile = "-";
    else
      OutputFile = removeExt(InputFile) + kExt::SPIRVBinary;
  }
 
  llvm::StringRef outFile(OutputFile);
  llvm::raw_fd_ostream OFS(outFile, EC, llvm::sys::fs::F_None);
 
  if (!rs2spirv::WriteSPIRV(Ctxt, M.get(), OFS, Err)) {
    errs() << "compiler error: " << Err << '\n';
    return -4;
  }
 
  return 0;
}
 
int main(int ac, char **av) {
  EnablePrettyStackTrace();
  sys::PrintStackTraceOnErrorSignal(av[0]);
  PrettyStackTraceProgram X(ac, av);
 
  cl::ParseCommandLineOptions(ac, av, "RenderScript to SPIRV translator");
 
  return convertLLVMToSPIRV();
}