Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Side by Side Diff: tools/pnacl-benchmark/pnacl-benchmark.cpp

Issue 939073008: Rebased PNaCl localmods in LLVM to 223109 (Closed)
Patch Set: undo localmod Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/pnacl-benchmark/Makefile ('k') | tools/pnacl-freeze/CMakeLists.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 //===-- pnacl-benchmark.cpp -----------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // pnacl-benchmark: various benchmarking tools for the PNaCl LLVM toolchain.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Bitcode/NaCl/NaClBitcodeAnalyzer.h"
15 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h"
16 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h"
17 #include "llvm/Bitcode/NaCl/NaClBitstreamReader.h"
18 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h"
19 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IRReader/IRReader.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/FormattedStream.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/PrettyStackTrace.h"
31 #include "llvm/Support/Signals.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Support/ToolOutputFile.h"
34 #include "llvm/Support/Timer.h"
35 #include <memory>
36 #include <system_error>
37 #include <vector>
38
39 using namespace llvm;
40
41
42 static cl::opt<std::string>
43 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
44
45 static cl::opt<unsigned>
46 NumRuns("num-runs", cl::desc("Number of runs"), cl::init(1));
47
48 static cl::opt<bool>
49 VerboseErrors(
50 "verbose-parse-errors",
51 cl::desc("Print out more descriptive PNaCl bitcode parse errors"),
52 cl::init(false));
53
54 /// Used in a lexical block to measure and report the block's execution time.
55 ///
56 /// \param N block name
57 /// \param InputSize optional size of input operated upon. If given, the
58 /// throughput will be reported as well in MB/sec.
59 class TimingOperationBlock {
60 public:
61 TimingOperationBlock(StringRef N, size_t InputSize=0)
62 : InputSize(InputSize) {
63 outs() << "Timing: " << N << "... ";
64 TStart = TimeRecord::getCurrentTime(true);
65 }
66
67 ~TimingOperationBlock() {
68 TimeRecord TEnd = TimeRecord::getCurrentTime(false);
69 double elapsed = TEnd.getWallTime() - TStart.getWallTime();
70 outs() << format("%.3lf", elapsed) << " sec";
71
72 if (InputSize != 0) {
73 double MBPerSec = (InputSize / elapsed) / 1000000.0;
74 outs() << format(" [%.3lf MB/sec]", MBPerSec);
75 }
76 outs() << "\n";
77 }
78 private:
79 TimeRecord TStart;
80 size_t InputSize;
81 };
82
83 /// Simple parsing of bitcode with some basic bookkeeping that simulates doing
84 /// "something" with it.
85 class DummyBitcodeParser : public NaClBitcodeParser {
86 public:
87 DummyBitcodeParser(NaClBitstreamCursor &Cursor)
88 : NaClBitcodeParser(Cursor) {
89 resetCounters();
90 }
91
92 DummyBitcodeParser(unsigned BlockID, DummyBitcodeParser *EnclosingBlock)
93 : NaClBitcodeParser(BlockID, EnclosingBlock) {
94 resetCounters();
95 }
96
97 virtual bool ParseBlock(unsigned BlockID) {
98 DummyBitcodeParser Parser(BlockID, this);
99 return Parser.ParseThisBlock();
100 }
101
102 virtual void EnterBlock(unsigned NumberWords) {
103 NumBlocks++;
104 BlockNames.push_back("<unknown>");
105 }
106
107 virtual void ProcessRecord() {
108 NumRecords++;
109 RecordCodes.push_back(Record.GetCode());
110
111 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues();
112 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
113 RecordValues.push_back((int64_t) Values[i]);
114 }
115 }
116
117 private:
118 void resetCounters() {
119 NumBlocks = NumRecords = 0;
120 BlockNames.clear();
121 RecordCodes.clear();
122 RecordValues.clear();
123 }
124
125 uint64_t NumBlocks, NumRecords;
126 std::vector<StringRef> BlockNames;
127 std::vector<unsigned> RecordCodes;
128 std::vector<StringRef> RecordCodeNames;
129 std::vector<int64_t> RecordValues;
130 };
131
132 void BenchmarkIRParsing() {
133 outs() << "Benchmarking IR parsing...\n";
134 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile =
135 MemoryBuffer::getFileOrSTDIN(InputFilename);
136 if (std::error_code EC = ErrOrFile.getError())
137 report_fatal_error("Could not open input file: " + EC.message());
138
139 std::unique_ptr<MemoryBuffer> FileBuf(ErrOrFile.get().release());
140 size_t BufSize = FileBuf->getBufferSize();
141 const uint8_t *BufPtr =
142 reinterpret_cast<const uint8_t *>(FileBuf->getBufferStart());
143 const uint8_t *EndBufPtr =
144 reinterpret_cast<const uint8_t *>(FileBuf->getBufferEnd());
145
146 // Since MemoryBuffer may use mmap, make sure to first touch all bytes in the
147 // input buffer to make sure it's actually in memory.
148 volatile uint8_t *Slot = new uint8_t;
149 for (const uint8_t *S = BufPtr; S != EndBufPtr; ++S) {
150 *Slot = *S;
151 }
152
153 delete Slot;
154 outs() << "Read bitcode into buffer. Size=" << BufSize << "\n";
155
156 // Trivial copy into a new buffer with a cascading XOR that simulates
157 // "touching" every byte in the buffer in a simple way.
158 {
159 TimingOperationBlock T("Simple XOR copy", BufSize);
160 volatile uint8_t *OutBuf = new uint8_t[BufSize];
161 OutBuf[0] = 1;
162 size_t N = 1;
163 // Run over the input buffer from start to end-1; run over the output buffer
164 // from 1 to end.
165 for (const uint8_t *S = BufPtr; S != EndBufPtr - 1; ++S, ++N) {
166 OutBuf[N] = OutBuf[N - 1] ^ *S;
167 }
168 delete[] OutBuf;
169 }
170
171 // Simulate simple bitcode parsing. See DummyBitcodeParser for more details.
172 {
173 TimingOperationBlock T("Bitcode block parsing", BufSize);
174 NaClBitcodeHeader Header;
175
176 if (Header.Read(BufPtr, EndBufPtr)) {
177 report_fatal_error("Invalid PNaCl bitcode header");
178 }
179
180 if (!Header.IsSupported()) {
181 errs() << "Warning: " << Header.Unsupported() << "\n";
182 }
183
184 if (!Header.IsReadable()) {
185 report_fatal_error("Bitcode file is not readable");
186 }
187
188 NaClBitstreamReader StreamFile(BufPtr, EndBufPtr);
189 NaClBitstreamCursor Stream(StreamFile);
190 DummyBitcodeParser Parser(Stream);
191 while (!Stream.AtEndOfStream()) {
192 if (Parser.Parse()) {
193 report_fatal_error("Parsing failed");
194 }
195 }
196 }
197
198 // Running bitcode analysis (what bcanalyzer does).
199 // Note that quite a bit of time here is spent on emitting I/O into nulls().
200 {
201 TimingOperationBlock T("Running bitcode analysis", BufSize);
202
203 AnalysisDumpOptions DumpOptions;
204 AnalyzeBitcodeInBuffer(FileBuf, nulls(), DumpOptions);
205 }
206
207 // Actual LLVM IR parsing and formation from the bitcode
208 {
209 TimingOperationBlock T("LLVM IR parsing", BufSize);
210 SMDiagnostic Err;
211 raw_ostream *Verbose = VerboseErrors ? &errs() : nullptr;
212 std::unique_ptr<Module> M = NaClParseIRFile(
213 InputFilename, PNaClFormat,
214 Err, Verbose, getGlobalContext());
215
216 if (!M) {
217 report_fatal_error("Unable to NaClParseIRFile");
218 }
219 }
220 }
221
222 int main(int argc, char **argv) {
223 sys::PrintStackTraceOnErrorSignal();
224 PrettyStackTraceProgram X(argc, argv);
225
226 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
227 cl::ParseCommandLineOptions(argc, argv, "pnacl-benchmark\n");
228
229 for (unsigned i = 0; i < NumRuns; i++) {
230 BenchmarkIRParsing();
231 }
232
233 return 0;
234 }
OLDNEW
« no previous file with comments | « tools/pnacl-benchmark/Makefile ('k') | tools/pnacl-freeze/CMakeLists.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698