1//===- verify-uselistorder.cpp - The LLVM Modular Optimizer ---------------===//
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-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Verify that use-list order can be serialized correctly. After reading the
10// provided IR, this tool shuffles the use-lists and then writes and reads to a
11// separate Module whose use-list orders are compared to the original.
12//
13// The shuffles are deterministic, but guarantee that use-lists will change.
14// The algorithm per iteration is as follows:
15//
16// 1. Seed the random number generator. The seed is different for each
17// shuffle. Shuffle 0 uses default+0, shuffle 1 uses default+1, and so on.
18//
19// 2. Visit every Value in a deterministic order.
20//
21// 3. Assign a random number to each Use in the Value's use-list in order.
22//
23// 4. If the numbers are already in order, reassign numbers until they aren't.
24//
25// 5. Sort the use-list using Value::sortUseList(), which is a stable sort.
26//
27//===----------------------------------------------------------------------===//
28
29#include "llvm/ADT/DenseMap.h"
30#include "llvm/ADT/DenseSet.h"
31#include "llvm/AsmParser/Parser.h"
32#include "llvm/Bitcode/BitcodeReader.h"
33#include "llvm/Bitcode/BitcodeWriter.h"
34#include "llvm/IR/LLVMContext.h"
35#include "llvm/IR/Module.h"
36#include "llvm/IR/UseListOrder.h"
37#include "llvm/IR/Verifier.h"
38#include "llvm/IRReader/IRReader.h"
39#include "llvm/Support/CommandLine.h"
40#include "llvm/Support/Debug.h"
41#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/FileSystem.h"
43#include "llvm/Support/FileUtilities.h"
44#include "llvm/Support/InitLLVM.h"
45#include "llvm/Support/LibraryOptionsContext.h"
46#include "llvm/Support/MemoryBuffer.h"
47#include "llvm/Support/SourceMgr.h"
48#include "llvm/Support/SystemUtils.h"
49#include "llvm/Support/raw_ostream.h"
50#include <random>
51#include <vector>
52
53using namespace llvm;
54
55#define DEBUG_TYPE "uselistorder"
56
57static cl::OptionCategory Cat("verify-uselistorder Options");
58
59static cl::opt<std::string> InputFilename(cl::Positional,
60 cl::desc("<input bitcode file>"),
61 cl::init(Val: "-"),
62 cl::value_desc("filename"));
63
64static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temp files"),
65 cl::cat(Cat));
66
67static cl::opt<unsigned>
68 NumShuffles("num-shuffles",
69 cl::desc("Number of times to shuffle and verify use-lists"),
70 cl::init(Val: 1), cl::cat(Cat));
71
72namespace {
73
74struct TempFile {
75 std::string Filename;
76 FileRemover Remover;
77 bool init(const std::string &Ext, bool IsText = false);
78 bool writeBitcode(const Module &M) const;
79 bool writeAssembly(Module &M) const;
80 std::unique_ptr<Module> readBitcode(LLVMContext &Context) const;
81 std::unique_ptr<Module> readAssembly(LLVMContext &Context) const;
82};
83
84struct ValueMapping {
85 DenseMap<const Value *, unsigned> IDs;
86 std::vector<const Value *> Values;
87
88 /// Construct a value mapping for module.
89 ///
90 /// Creates mapping from every value in \c M to an ID. This mapping includes
91 /// un-referencable values.
92 ///
93 /// Every \a Value that gets serialized in some way should be represented
94 /// here. The order needs to be deterministic, but it's unnecessary to match
95 /// the value-ids in the bitcode writer.
96 ///
97 /// All constants that are referenced by other values are included in the
98 /// mapping, but others -- which wouldn't be serialized -- are not.
99 ValueMapping(const Module &M);
100
101 /// Map a value.
102 ///
103 /// Maps a value. If it's a constant, maps all of its operands first.
104 void map(const Value *V);
105 unsigned lookup(const Value *V) const { return IDs.lookup(Val: V); }
106};
107
108} // end namespace
109
110bool TempFile::init(const std::string &Ext, bool IsText) {
111 SmallVector<char, 64> Vector;
112 LLVM_DEBUG(dbgs() << " - create-temp-file\n");
113 if (auto EC = sys::fs::createTemporaryFile(Prefix: "uselistorder", Suffix: Ext, ResultPath&: Vector,
114 Flags: IsText ? sys::fs::OF_Text
115 : sys::fs::OF_None)) {
116 errs() << "verify-uselistorder: error: " << EC.message() << "\n";
117 return true;
118 }
119 assert(!Vector.empty());
120
121 Filename.assign(first: Vector.data(), last: Vector.data() + Vector.size());
122 Remover.setFile(filename: Filename, deleteIt: !SaveTemps);
123 if (SaveTemps)
124 outs() << " - filename = " << Filename << "\n";
125 return false;
126}
127
128bool TempFile::writeBitcode(const Module &M) const {
129 LLVM_DEBUG(dbgs() << " - write bitcode\n");
130 std::error_code EC;
131 raw_fd_ostream OS(Filename, EC, sys::fs::OF_None);
132 if (EC) {
133 errs() << "verify-uselistorder: error: " << EC.message() << "\n";
134 return true;
135 }
136
137 WriteBitcodeToFile(M, Out&: OS, /* ShouldPreserveUseListOrder */ true);
138 return false;
139}
140
141bool TempFile::writeAssembly(Module &M) const {
142 LLVM_DEBUG(dbgs() << " - write assembly\n");
143 std::error_code EC;
144 raw_fd_ostream OS(Filename, EC, sys::fs::OF_TextWithCRLF);
145 if (EC) {
146 errs() << "verify-uselistorder: error: " << EC.message() << "\n";
147 return true;
148 }
149
150 M.renumberMetadataForAssembly();
151 M.print(OS, AAW: nullptr, /* ShouldPreserveUseListOrder */ true);
152 return false;
153}
154
155std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
156 LLVM_DEBUG(dbgs() << " - read bitcode\n");
157 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOr =
158 MemoryBuffer::getFile(Filename);
159 if (!BufferOr) {
160 errs() << "verify-uselistorder: error: " << BufferOr.getError().message()
161 << "\n";
162 return nullptr;
163 }
164
165 MemoryBuffer *Buffer = BufferOr.get().get();
166 Expected<std::unique_ptr<Module>> ModuleOr =
167 parseBitcodeFile(Buffer: Buffer->getMemBufferRef(), Context);
168 if (!ModuleOr) {
169 logAllUnhandledErrors(E: ModuleOr.takeError(), OS&: errs(),
170 ErrorBanner: "verify-uselistorder: error: ");
171 return nullptr;
172 }
173
174 return std::move(ModuleOr.get());
175}
176
177std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {
178 LLVM_DEBUG(dbgs() << " - read assembly\n");
179 SMDiagnostic Err;
180 std::unique_ptr<Module> M = parseAssemblyFile(Filename, Err, Context);
181 if (!M)
182 Err.print(ProgName: "verify-uselistorder", S&: errs());
183 return M;
184}
185
186ValueMapping::ValueMapping(const Module &M) {
187 // Every value should be mapped, including things like void instructions and
188 // basic blocks that are kept out of the ValueEnumerator.
189 //
190 // The current mapping order makes it easier to debug the tables. It happens
191 // to be similar to the ID mapping when writing ValueEnumerator, but they
192 // aren't (and needn't be) in sync.
193
194 // Globals.
195 for (const GlobalVariable &G : M.globals())
196 map(V: &G);
197 for (const GlobalAlias &A : M.aliases())
198 map(V: &A);
199 for (const GlobalIFunc &IF : M.ifuncs())
200 map(V: &IF);
201 for (const Function &F : M)
202 map(V: &F);
203
204 // Constants used by globals.
205 for (const GlobalVariable &G : M.globals())
206 if (G.hasInitializer())
207 map(V: G.getInitializer());
208 for (const GlobalAlias &A : M.aliases())
209 map(V: A.getAliasee());
210 for (const GlobalIFunc &IF : M.ifuncs())
211 map(V: IF.getResolver());
212 for (const Function &F : M)
213 for (Value *Op : F.operands())
214 map(V: Op);
215
216 // Function bodies.
217 for (const Function &F : M) {
218 for (const Argument &A : F.args())
219 map(V: &A);
220 for (const BasicBlock &BB : F)
221 map(V: &BB);
222 for (const BasicBlock &BB : F)
223 for (const Instruction &I : BB)
224 map(V: &I);
225
226 // Constants used by instructions.
227 for (const BasicBlock &BB : F) {
228 for (const Instruction &I : BB) {
229 for (const DbgVariableRecord &DVR :
230 filterDbgVars(R: I.getDbgRecordRange())) {
231 for (Value *Op : DVR.location_ops())
232 map(V: Op);
233 if (DVR.isDbgAssign())
234 map(V: DVR.getAddress());
235 }
236 for (const Value *Op : I.operands()) {
237 // Look through a metadata wrapper.
238 if (const auto *MAV = dyn_cast<MetadataAsValue>(Val: Op))
239 if (const auto *VAM = dyn_cast<ValueAsMetadata>(Val: MAV->getMetadata()))
240 Op = VAM->getValue();
241
242 if ((isa<Constant>(Val: Op) && !isa<GlobalValue>(Val: *Op)) ||
243 isa<InlineAsm>(Val: Op))
244 map(V: Op);
245 }
246 }
247 }
248 }
249}
250
251void ValueMapping::map(const Value *V) {
252 if (!V->hasUseList())
253 return;
254
255 if (IDs.lookup(Val: V))
256 return;
257
258 if (auto *C = dyn_cast<Constant>(Val: V))
259 if (!isa<GlobalValue>(Val: C))
260 for (const Value *Op : C->operands())
261 map(V: Op);
262
263 Values.push_back(x: V);
264 IDs[V] = Values.size();
265}
266
267#ifndef NDEBUG
268static void dumpMapping(const ValueMapping &VM) {
269 dbgs() << "value-mapping (size = " << VM.Values.size() << "):\n";
270 for (unsigned I = 0, E = VM.Values.size(); I != E; ++I) {
271 dbgs() << " - id = " << I << ", value = ";
272 VM.Values[I]->dump();
273 }
274}
275
276static void debugValue(const ValueMapping &M, unsigned I, StringRef Desc) {
277 const Value *V = M.Values[I];
278 dbgs() << " - " << Desc << " value = ";
279 V->dump();
280 for (const Use &U : V->uses()) {
281 dbgs() << " => use: op = " << U.getOperandNo()
282 << ", user-id = " << M.IDs.lookup(U.getUser()) << ", user = ";
283 U.getUser()->dump();
284 }
285}
286
287static void debugUserMismatch(const ValueMapping &L, const ValueMapping &R,
288 unsigned I) {
289 dbgs() << " - fail: user mismatch: ID = " << I << "\n";
290 debugValue(L, I, "LHS");
291 debugValue(R, I, "RHS");
292
293 dbgs() << "\nlhs-";
294 dumpMapping(L);
295 dbgs() << "\nrhs-";
296 dumpMapping(R);
297}
298
299static void debugSizeMismatch(const ValueMapping &L, const ValueMapping &R) {
300 dbgs() << " - fail: map size: " << L.Values.size()
301 << " != " << R.Values.size() << "\n";
302 dbgs() << "\nlhs-";
303 dumpMapping(L);
304 dbgs() << "\nrhs-";
305 dumpMapping(R);
306}
307#endif
308
309static bool matches(const ValueMapping &LM, const ValueMapping &RM) {
310 LLVM_DEBUG(dbgs() << "compare value maps\n");
311 if (LM.Values.size() != RM.Values.size()) {
312 LLVM_DEBUG(debugSizeMismatch(LM, RM));
313 return false;
314 }
315
316 // This mapping doesn't include dangling constant users, since those don't
317 // get serialized. However, checking if users are constant and calling
318 // isConstantUsed() on every one is very expensive. Instead, just check if
319 // the user is mapped.
320 auto skipUnmappedUsers =
321 [&](Value::const_use_iterator &U, Value::const_use_iterator E,
322 const ValueMapping &M) {
323 while (U != E && !M.lookup(V: U->getUser()))
324 ++U;
325 };
326
327 // Iterate through all values, and check that both mappings have the same
328 // users.
329 for (unsigned I = 0, E = LM.Values.size(); I != E; ++I) {
330 const Value *L = LM.Values[I];
331 const Value *R = RM.Values[I];
332 auto LU = L->use_begin(), LE = L->use_end();
333 auto RU = R->use_begin(), RE = R->use_end();
334 skipUnmappedUsers(LU, LE, LM);
335 skipUnmappedUsers(RU, RE, RM);
336
337 while (LU != LE) {
338 if (RU == RE) {
339 LLVM_DEBUG(debugUserMismatch(LM, RM, I));
340 return false;
341 }
342 if (LM.lookup(V: LU->getUser()) != RM.lookup(V: RU->getUser())) {
343 LLVM_DEBUG(debugUserMismatch(LM, RM, I));
344 return false;
345 }
346 if (LU->getOperandNo() != RU->getOperandNo()) {
347 LLVM_DEBUG(debugUserMismatch(LM, RM, I));
348 return false;
349 }
350 skipUnmappedUsers(++LU, LE, LM);
351 skipUnmappedUsers(++RU, RE, RM);
352 }
353 if (RU != RE) {
354 LLVM_DEBUG(debugUserMismatch(LM, RM, I));
355 return false;
356 }
357 }
358
359 return true;
360}
361
362static void verifyAfterRoundTrip(const Module &M,
363 std::unique_ptr<Module> OtherM) {
364 if (!OtherM)
365 report_fatal_error(reason: "parsing failed");
366 if (verifyModule(M: *OtherM, OS: &errs()))
367 report_fatal_error(reason: "verification failed");
368 if (!matches(LM: ValueMapping(M), RM: ValueMapping(*OtherM)))
369 report_fatal_error(reason: "use-list order changed");
370}
371
372static void verifyBitcodeUseListOrder(const Module &M) {
373 TempFile F;
374 if (F.init(Ext: "bc", /*IsText=*/false))
375 report_fatal_error(reason: "failed to initialize bitcode file");
376
377 if (F.writeBitcode(M))
378 report_fatal_error(reason: "failed to write bitcode");
379
380 LLVMContext Context;
381 Context.getLibraryOptions().assign(Other: M.getContext().getLibraryOptions());
382 verifyAfterRoundTrip(M, OtherM: F.readBitcode(Context));
383}
384
385static void verifyAssemblyUseListOrder(Module &M) {
386 TempFile F;
387 if (F.init(Ext: "ll", /*IsText=*/true))
388 report_fatal_error(reason: "failed to initialize assembly file");
389
390 if (F.writeAssembly(M))
391 report_fatal_error(reason: "failed to write assembly");
392
393 LLVMContext Context;
394 Context.getLibraryOptions().assign(Other: M.getContext().getLibraryOptions());
395 verifyAfterRoundTrip(M, OtherM: F.readAssembly(Context));
396}
397
398static void verifyUseListOrder(Module &M) {
399 outs() << "verify bitcode\n";
400 verifyBitcodeUseListOrder(M);
401 outs() << "verify assembly\n";
402 verifyAssemblyUseListOrder(M);
403}
404
405static void shuffleValueUseLists(Value *V, std::minstd_rand0 &Gen,
406 DenseSet<Value *> &Seen) {
407 if (!V->hasUseList())
408 return;
409
410 if (!Seen.insert(V).second)
411 return;
412
413 if (auto *C = dyn_cast<Constant>(Val: V))
414 if (!isa<GlobalValue>(Val: C))
415 for (Value *Op : C->operands())
416 shuffleValueUseLists(V: Op, Gen, Seen);
417
418 if (V->use_empty() || std::next(x: V->use_begin()) == V->use_end())
419 // Nothing to shuffle for 0 or 1 users.
420 return;
421
422 // Generate random numbers between 10 and 99, which will line up nicely in
423 // debug output. We're not worried about collisions here.
424 LLVM_DEBUG(dbgs() << "V = "; V->dump());
425 std::uniform_int_distribution<short> Dist(10, 99);
426 SmallDenseMap<const Use *, short, 16> Order;
427 auto compareUses =
428 [&Order](const Use &L, const Use &R) { return Order[&L] < Order[&R]; };
429 do {
430 for (const Use &U : V->uses()) {
431 auto I = Dist(Gen);
432 Order[&U] = I;
433 LLVM_DEBUG(dbgs() << " - order: " << I << ", op = " << U.getOperandNo()
434 << ", U = ";
435 U.getUser()->dump());
436 }
437 } while (llvm::is_sorted(Range: V->uses(), C: compareUses));
438
439 LLVM_DEBUG(dbgs() << " => shuffle\n");
440 V->sortUseList(Cmp: compareUses);
441
442 LLVM_DEBUG({
443 for (const Use &U : V->uses()) {
444 dbgs() << " - order: " << Order.lookup(&U)
445 << ", op = " << U.getOperandNo() << ", U = ";
446 U.getUser()->dump();
447 }
448 });
449}
450
451static void reverseValueUseLists(Value *V, DenseSet<Value *> &Seen) {
452 if (!V->hasUseList())
453 return;
454
455 if (!Seen.insert(V).second)
456 return;
457
458 if (auto *C = dyn_cast<Constant>(Val: V))
459 if (!isa<GlobalValue>(Val: C))
460 for (Value *Op : C->operands())
461 reverseValueUseLists(V: Op, Seen);
462
463 if (V->use_empty() || std::next(x: V->use_begin()) == V->use_end())
464 // Nothing to shuffle for 0 or 1 users.
465 return;
466
467 LLVM_DEBUG({
468 dbgs() << "V = ";
469 V->dump();
470 for (const Use &U : V->uses()) {
471 dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
472 U.getUser()->dump();
473 }
474 dbgs() << " => reverse\n";
475 });
476
477 V->reverseUseList();
478
479 LLVM_DEBUG({
480 for (const Use &U : V->uses()) {
481 dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
482 U.getUser()->dump();
483 }
484 });
485}
486
487template <class Changer>
488static void changeUseLists(Module &M, Changer changeValueUseList) {
489 // Visit every value that would be serialized to an IR file.
490 //
491 // Globals.
492 for (GlobalVariable &G : M.globals())
493 changeValueUseList(&G);
494 for (GlobalAlias &A : M.aliases())
495 changeValueUseList(&A);
496 for (GlobalIFunc &IF : M.ifuncs())
497 changeValueUseList(&IF);
498 for (Function &F : M)
499 changeValueUseList(&F);
500
501 // Constants used by globals.
502 for (GlobalVariable &G : M.globals())
503 if (G.hasInitializer())
504 changeValueUseList(G.getInitializer());
505 for (GlobalAlias &A : M.aliases())
506 changeValueUseList(A.getAliasee());
507 for (GlobalIFunc &IF : M.ifuncs())
508 changeValueUseList(IF.getResolver());
509 for (Function &F : M)
510 for (Value *Op : F.operands())
511 changeValueUseList(Op);
512
513 // Function bodies.
514 for (Function &F : M) {
515 for (Argument &A : F.args())
516 changeValueUseList(&A);
517 for (BasicBlock &BB : F)
518 changeValueUseList(&BB);
519 for (BasicBlock &BB : F)
520 for (Instruction &I : BB)
521 changeValueUseList(&I);
522
523 // Constants used by instructions.
524 for (BasicBlock &BB : F)
525 for (Instruction &I : BB)
526 for (Value *Op : I.operands()) {
527 // Look through a metadata wrapper.
528 if (auto *MAV = dyn_cast<MetadataAsValue>(Val: Op))
529 if (auto *VAM = dyn_cast<ValueAsMetadata>(Val: MAV->getMetadata()))
530 Op = VAM->getValue();
531 if ((isa<Constant>(Val: Op) && !isa<GlobalValue>(Val: *Op)) ||
532 isa<InlineAsm>(Val: Op))
533 changeValueUseList(Op);
534 }
535 }
536
537 if (verifyModule(M, OS: &errs()))
538 report_fatal_error(reason: "verification failed");
539}
540
541static void shuffleUseLists(Module &M, unsigned SeedOffset) {
542 std::minstd_rand0 Gen(std::minstd_rand0::default_seed + SeedOffset);
543 DenseSet<Value *> Seen;
544 changeUseLists(M, changeValueUseList: [&](Value *V) { shuffleValueUseLists(V, Gen, Seen); });
545 LLVM_DEBUG(dbgs() << "\n");
546}
547
548static void reverseUseLists(Module &M) {
549 DenseSet<Value *> Seen;
550 changeUseLists(M, changeValueUseList: [&](Value *V) { reverseValueUseLists(V, Seen); });
551 LLVM_DEBUG(dbgs() << "\n");
552}
553
554int main(int argc, char **argv) {
555 InitLLVM X(argc, argv);
556
557 // Enable debug stream buffering.
558 EnableDebugBuffering = true;
559
560 cl::HideUnrelatedOptions(Category&: Cat);
561 LLVMContext Context;
562 cl::parseCommandLineOptions(Context&: Context.getLibraryOptions(), Args: ArrayRef(argv, argc),
563 Overview: "llvm tool to verify use-list order\n");
564
565 SMDiagnostic Err;
566
567 // Load the input module...
568 std::unique_ptr<Module> M = parseIRFile(Filename: InputFilename, Err, Context);
569
570 if (!M) {
571 Err.print(ProgName: argv[0], S&: errs());
572 return 1;
573 }
574 if (verifyModule(M: *M, OS: &errs())) {
575 errs() << argv[0] << ": " << InputFilename
576 << ": error: input module is broken!\n";
577 return 1;
578 }
579
580 // Verify the use lists now and after reversing them.
581 outs() << "*** verify-uselistorder ***\n";
582 verifyUseListOrder(M&: *M);
583 outs() << "reverse\n";
584 reverseUseLists(M&: *M);
585 verifyUseListOrder(M&: *M);
586
587 for (unsigned I = 0, E = NumShuffles; I != E; ++I) {
588 outs() << "\n";
589
590 // Shuffle with a different (deterministic) seed each time.
591 outs() << "shuffle (" << I + 1 << " of " << E << ")\n";
592 shuffleUseLists(M&: *M, SeedOffset: I);
593
594 // Verify again before and after reversing.
595 verifyUseListOrder(M&: *M);
596 outs() << "reverse\n";
597 reverseUseLists(M&: *M);
598 verifyUseListOrder(M&: *M);
599 }
600
601 return 0;
602}
603