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