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 GlobalIFunc &IF : M.ifuncs()) 195 map(&IF); 196 for (const Function &F : M) 197 map(&F); 198 199 // Constants used by globals. 200 for (const GlobalVariable &G : M.globals()) 201 if (G.hasInitializer()) 202 map(G.getInitializer()); 203 for (const GlobalAlias &A : M.aliases()) 204 map(A.getAliasee()); 205 for (const GlobalIFunc &IF : M.ifuncs()) 206 map(IF.getResolver()); 207 for (const Function &F : M) { 208 if (F.hasPrefixData()) 209 map(F.getPrefixData()); 210 if (F.hasPrologueData()) 211 map(F.getPrologueData()); 212 if (F.hasPersonalityFn()) 213 map(F.getPersonalityFn()); 214 } 215 216 // Function bodies. 217 for (const Function &F : M) { 218 for (const Argument &A : F.args()) 219 map(&A); 220 for (const BasicBlock &BB : F) 221 map(&BB); 222 for (const BasicBlock &BB : F) 223 for (const Instruction &I : BB) 224 map(&I); 225 226 // Constants used by instructions. 227 for (const BasicBlock &BB : F) 228 for (const Instruction &I : BB) 229 for (const Value *Op : I.operands()) 230 if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) || 231 isa<InlineAsm>(Op)) 232 map(Op); 233 } 234 } 235 236 void ValueMapping::map(const Value *V) { 237 if (IDs.lookup(V)) 238 return; 239 240 if (auto *C = dyn_cast<Constant>(V)) 241 if (!isa<GlobalValue>(C)) 242 for (const Value *Op : C->operands()) 243 map(Op); 244 245 Values.push_back(V); 246 IDs[V] = Values.size(); 247 } 248 249 #ifndef NDEBUG 250 static void dumpMapping(const ValueMapping &VM) { 251 dbgs() << "value-mapping (size = " << VM.Values.size() << "):\n"; 252 for (unsigned I = 0, E = VM.Values.size(); I != E; ++I) { 253 dbgs() << " - id = " << I << ", value = "; 254 VM.Values[I]->dump(); 255 } 256 } 257 258 static void debugValue(const ValueMapping &M, unsigned I, StringRef Desc) { 259 const Value *V = M.Values[I]; 260 dbgs() << " - " << Desc << " value = "; 261 V->dump(); 262 for (const Use &U : V->uses()) { 263 dbgs() << " => use: op = " << U.getOperandNo() 264 << ", user-id = " << M.IDs.lookup(U.getUser()) << ", user = "; 265 U.getUser()->dump(); 266 } 267 } 268 269 static void debugUserMismatch(const ValueMapping &L, const ValueMapping &R, 270 unsigned I) { 271 dbgs() << " - fail: user mismatch: ID = " << I << "\n"; 272 debugValue(L, I, "LHS"); 273 debugValue(R, I, "RHS"); 274 275 dbgs() << "\nlhs-"; 276 dumpMapping(L); 277 dbgs() << "\nrhs-"; 278 dumpMapping(R); 279 } 280 281 static void debugSizeMismatch(const ValueMapping &L, const ValueMapping &R) { 282 dbgs() << " - fail: map size: " << L.Values.size() 283 << " != " << R.Values.size() << "\n"; 284 dbgs() << "\nlhs-"; 285 dumpMapping(L); 286 dbgs() << "\nrhs-"; 287 dumpMapping(R); 288 } 289 #endif 290 291 static bool matches(const ValueMapping &LM, const ValueMapping &RM) { 292 DEBUG(dbgs() << "compare value maps\n"); 293 if (LM.Values.size() != RM.Values.size()) { 294 DEBUG(debugSizeMismatch(LM, RM)); 295 return false; 296 } 297 298 // This mapping doesn't include dangling constant users, since those don't 299 // get serialized. However, checking if users are constant and calling 300 // isConstantUsed() on every one is very expensive. Instead, just check if 301 // the user is mapped. 302 auto skipUnmappedUsers = 303 [&](Value::const_use_iterator &U, Value::const_use_iterator E, 304 const ValueMapping &M) { 305 while (U != E && !M.lookup(U->getUser())) 306 ++U; 307 }; 308 309 // Iterate through all values, and check that both mappings have the same 310 // users. 311 for (unsigned I = 0, E = LM.Values.size(); I != E; ++I) { 312 const Value *L = LM.Values[I]; 313 const Value *R = RM.Values[I]; 314 auto LU = L->use_begin(), LE = L->use_end(); 315 auto RU = R->use_begin(), RE = R->use_end(); 316 skipUnmappedUsers(LU, LE, LM); 317 skipUnmappedUsers(RU, RE, RM); 318 319 while (LU != LE) { 320 if (RU == RE) { 321 DEBUG(debugUserMismatch(LM, RM, I)); 322 return false; 323 } 324 if (LM.lookup(LU->getUser()) != RM.lookup(RU->getUser())) { 325 DEBUG(debugUserMismatch(LM, RM, I)); 326 return false; 327 } 328 if (LU->getOperandNo() != RU->getOperandNo()) { 329 DEBUG(debugUserMismatch(LM, RM, I)); 330 return false; 331 } 332 skipUnmappedUsers(++LU, LE, LM); 333 skipUnmappedUsers(++RU, RE, RM); 334 } 335 if (RU != RE) { 336 DEBUG(debugUserMismatch(LM, RM, I)); 337 return false; 338 } 339 } 340 341 return true; 342 } 343 344 static void verifyAfterRoundTrip(const Module &M, 345 std::unique_ptr<Module> OtherM) { 346 if (!OtherM) 347 report_fatal_error("parsing failed"); 348 if (verifyModule(*OtherM, &errs())) 349 report_fatal_error("verification failed"); 350 if (!matches(ValueMapping(M), ValueMapping(*OtherM))) 351 report_fatal_error("use-list order changed"); 352 } 353 354 static void verifyBitcodeUseListOrder(const Module &M) { 355 TempFile F; 356 if (F.init("bc")) 357 report_fatal_error("failed to initialize bitcode file"); 358 359 if (F.writeBitcode(M)) 360 report_fatal_error("failed to write bitcode"); 361 362 LLVMContext Context; 363 verifyAfterRoundTrip(M, F.readBitcode(Context)); 364 } 365 366 static void verifyAssemblyUseListOrder(const Module &M) { 367 TempFile F; 368 if (F.init("ll")) 369 report_fatal_error("failed to initialize assembly file"); 370 371 if (F.writeAssembly(M)) 372 report_fatal_error("failed to write assembly"); 373 374 LLVMContext Context; 375 verifyAfterRoundTrip(M, F.readAssembly(Context)); 376 } 377 378 static void verifyUseListOrder(const Module &M) { 379 outs() << "verify bitcode\n"; 380 verifyBitcodeUseListOrder(M); 381 outs() << "verify assembly\n"; 382 verifyAssemblyUseListOrder(M); 383 } 384 385 static void shuffleValueUseLists(Value *V, std::minstd_rand0 &Gen, 386 DenseSet<Value *> &Seen) { 387 if (!Seen.insert(V).second) 388 return; 389 390 if (auto *C = dyn_cast<Constant>(V)) 391 if (!isa<GlobalValue>(C)) 392 for (Value *Op : C->operands()) 393 shuffleValueUseLists(Op, Gen, Seen); 394 395 if (V->use_empty() || std::next(V->use_begin()) == V->use_end()) 396 // Nothing to shuffle for 0 or 1 users. 397 return; 398 399 // Generate random numbers between 10 and 99, which will line up nicely in 400 // debug output. We're not worried about collisons here. 401 DEBUG(dbgs() << "V = "; V->dump()); 402 std::uniform_int_distribution<short> Dist(10, 99); 403 SmallDenseMap<const Use *, short, 16> Order; 404 auto compareUses = 405 [&Order](const Use &L, const Use &R) { return Order[&L] < Order[&R]; }; 406 do { 407 for (const Use &U : V->uses()) { 408 auto I = Dist(Gen); 409 Order[&U] = I; 410 DEBUG(dbgs() << " - order: " << I << ", op = " << U.getOperandNo() 411 << ", U = "; 412 U.getUser()->dump()); 413 } 414 } while (std::is_sorted(V->use_begin(), V->use_end(), compareUses)); 415 416 DEBUG(dbgs() << " => shuffle\n"); 417 V->sortUseList(compareUses); 418 419 DEBUG({ 420 for (const Use &U : V->uses()) { 421 dbgs() << " - order: " << Order.lookup(&U) 422 << ", op = " << U.getOperandNo() << ", U = "; 423 U.getUser()->dump(); 424 } 425 }); 426 } 427 428 static void reverseValueUseLists(Value *V, DenseSet<Value *> &Seen) { 429 if (!Seen.insert(V).second) 430 return; 431 432 if (auto *C = dyn_cast<Constant>(V)) 433 if (!isa<GlobalValue>(C)) 434 for (Value *Op : C->operands()) 435 reverseValueUseLists(Op, Seen); 436 437 if (V->use_empty() || std::next(V->use_begin()) == V->use_end()) 438 // Nothing to shuffle for 0 or 1 users. 439 return; 440 441 DEBUG({ 442 dbgs() << "V = "; 443 V->dump(); 444 for (const Use &U : V->uses()) { 445 dbgs() << " - order: op = " << U.getOperandNo() << ", U = "; 446 U.getUser()->dump(); 447 } 448 dbgs() << " => reverse\n"; 449 }); 450 451 V->reverseUseList(); 452 453 DEBUG({ 454 for (const Use &U : V->uses()) { 455 dbgs() << " - order: op = " << U.getOperandNo() << ", U = "; 456 U.getUser()->dump(); 457 } 458 }); 459 } 460 461 template <class Changer> 462 static void changeUseLists(Module &M, Changer changeValueUseList) { 463 // Visit every value that would be serialized to an IR file. 464 // 465 // Globals. 466 for (GlobalVariable &G : M.globals()) 467 changeValueUseList(&G); 468 for (GlobalAlias &A : M.aliases()) 469 changeValueUseList(&A); 470 for (GlobalIFunc &IF : M.ifuncs()) 471 changeValueUseList(&IF); 472 for (Function &F : M) 473 changeValueUseList(&F); 474 475 // Constants used by globals. 476 for (GlobalVariable &G : M.globals()) 477 if (G.hasInitializer()) 478 changeValueUseList(G.getInitializer()); 479 for (GlobalAlias &A : M.aliases()) 480 changeValueUseList(A.getAliasee()); 481 for (GlobalIFunc &IF : M.ifuncs()) 482 changeValueUseList(IF.getResolver()); 483 for (Function &F : M) { 484 if (F.hasPrefixData()) 485 changeValueUseList(F.getPrefixData()); 486 if (F.hasPrologueData()) 487 changeValueUseList(F.getPrologueData()); 488 if (F.hasPersonalityFn()) 489 changeValueUseList(F.getPersonalityFn()); 490 } 491 492 // Function bodies. 493 for (Function &F : M) { 494 for (Argument &A : F.args()) 495 changeValueUseList(&A); 496 for (BasicBlock &BB : F) 497 changeValueUseList(&BB); 498 for (BasicBlock &BB : F) 499 for (Instruction &I : BB) 500 changeValueUseList(&I); 501 502 // Constants used by instructions. 503 for (BasicBlock &BB : F) 504 for (Instruction &I : BB) 505 for (Value *Op : I.operands()) 506 if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) || 507 isa<InlineAsm>(Op)) 508 changeValueUseList(Op); 509 } 510 511 if (verifyModule(M, &errs())) 512 report_fatal_error("verification failed"); 513 } 514 515 static void shuffleUseLists(Module &M, unsigned SeedOffset) { 516 std::minstd_rand0 Gen(std::minstd_rand0::default_seed + SeedOffset); 517 DenseSet<Value *> Seen; 518 changeUseLists(M, [&](Value *V) { shuffleValueUseLists(V, Gen, Seen); }); 519 DEBUG(dbgs() << "\n"); 520 } 521 522 static void reverseUseLists(Module &M) { 523 DenseSet<Value *> Seen; 524 changeUseLists(M, [&](Value *V) { reverseValueUseLists(V, Seen); }); 525 DEBUG(dbgs() << "\n"); 526 } 527 528 int main(int argc, char **argv) { 529 sys::PrintStackTraceOnErrorSignal(argv[0]); 530 llvm::PrettyStackTraceProgram X(argc, argv); 531 532 // Enable debug stream buffering. 533 EnableDebugBuffering = true; 534 535 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 536 LLVMContext Context; 537 538 cl::ParseCommandLineOptions(argc, argv, 539 "llvm tool to verify use-list order\n"); 540 541 SMDiagnostic Err; 542 543 // Load the input module... 544 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context); 545 546 if (!M.get()) { 547 Err.print(argv[0], errs()); 548 return 1; 549 } 550 if (verifyModule(*M, &errs())) { 551 errs() << argv[0] << ": " << InputFilename 552 << ": error: input module is broken!\n"; 553 return 1; 554 } 555 556 // Verify the use lists now and after reversing them. 557 outs() << "*** verify-uselistorder ***\n"; 558 verifyUseListOrder(*M); 559 outs() << "reverse\n"; 560 reverseUseLists(*M); 561 verifyUseListOrder(*M); 562 563 for (unsigned I = 0, E = NumShuffles; I != E; ++I) { 564 outs() << "\n"; 565 566 // Shuffle with a different (deterministic) seed each time. 567 outs() << "shuffle (" << I + 1 << " of " << E << ")\n"; 568 shuffleUseLists(*M, I); 569 570 // Verify again before and after reversing. 571 verifyUseListOrder(*M); 572 outs() << "reverse\n"; 573 reverseUseLists(*M); 574 verifyUseListOrder(*M); 575 } 576 577 return 0; 578 } 579