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