From: Duncan P. N. Exon Smith Date: Sat, 27 Jun 2015 00:38:26 +0000 (+0000) Subject: IR: Expose ModuleSlotTracker in Value::print() X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=2a5fda92b4ed2ddbaa03e1e6bb2107d8fe7f2cea;p=oota-llvm.git IR: Expose ModuleSlotTracker in Value::print() Allow callers of `Value::print()` and `Metadata::print()` to pass in a `ModuleSlotTracker`. This allows them to pay only once for calculating module-level slots (such as Metadata). This is related to PR23865, where there was a huge cost for `MachineFunction::print()`. Although I don't have a *particular* user in mind for this new code, I have hit big slowdowns before when running `opt -debug`, and I think this will be useful. Going forward, if someone hits a big slowdown with `print()` statements, they can create a `ModuleSlotTracker` and send it through. Similarly, adding support to `Value::dump()` and `Metadata::dump()` should be trivial. I added unit tests to be sure the `print()` functions actually behave the same way with and without the slot tracker. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240867 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/IR/Metadata.h b/include/llvm/IR/Metadata.h index ac9912e5f8e..8af014ac461 100644 --- a/include/llvm/IR/Metadata.h +++ b/include/llvm/IR/Metadata.h @@ -124,7 +124,11 @@ public: /// /// If \c M is provided, metadata nodes will be numbered canonically; /// otherwise, pointer addresses are substituted. + /// @{ void print(raw_ostream &OS, const Module *M = nullptr) const; + void print(raw_ostream &OS, ModuleSlotTracker &MST, + const Module *M = nullptr) const; + /// @} /// \brief Print as operand. /// diff --git a/include/llvm/IR/Value.h b/include/llvm/IR/Value.h index 931eb48a6d5..484afc6d232 100644 --- a/include/llvm/IR/Value.h +++ b/include/llvm/IR/Value.h @@ -200,7 +200,10 @@ public: void dump() const; /// \brief Implement operator<< on Value. + /// @{ void print(raw_ostream &O) const; + void print(raw_ostream &O, ModuleSlotTracker &MST) const; + /// @} /// \brief Print the name of this Value out to the specified raw_ostream. /// diff --git a/lib/IR/AsmWriter.cpp b/lib/IR/AsmWriter.cpp index fdfed9ee5ad..38aa00c9dc7 100644 --- a/lib/IR/AsmWriter.cpp +++ b/lib/IR/AsmWriter.cpp @@ -3193,21 +3193,35 @@ static bool isReferencingMDNode(const Instruction &I) { } void Value::print(raw_ostream &ROS) const { + bool ShouldInitializeAllMetadata = false; + if (auto *I = dyn_cast(this)) + ShouldInitializeAllMetadata = isReferencingMDNode(*I); + else if (isa(this) || isa(this)) + ShouldInitializeAllMetadata = true; + + ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata); + print(ROS, MST); +} + +void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST) const { formatted_raw_ostream OS(ROS); + SlotTracker EmptySlotTable(static_cast(nullptr)); + SlotTracker &SlotTable = + MST.getMachine() ? *MST.getMachine() : EmptySlotTable; + auto incorporateFunction = [&](const Function *F) { + if (F) + MST.incorporateFunction(*F); + }; + if (const Instruction *I = dyn_cast(this)) { - const Function *F = I->getParent() ? I->getParent()->getParent() : nullptr; - SlotTracker SlotTable( - F, - /* ShouldInitializeAllMetadata */ isReferencingMDNode(*I)); + incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr); AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr); W.printInstruction(*I); } else if (const BasicBlock *BB = dyn_cast(this)) { - SlotTracker SlotTable(BB->getParent()); + incorporateFunction(BB->getParent()); AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr); W.printBasicBlock(BB); } else if (const GlobalValue *GV = dyn_cast(this)) { - SlotTracker SlotTable(GV->getParent(), - /* ShouldInitializeAllMetadata */ isa(GV)); AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr); if (const GlobalVariable *V = dyn_cast(GV)) W.printGlobal(V); @@ -3216,14 +3230,14 @@ void Value::print(raw_ostream &ROS) const { else W.printAlias(cast(GV)); } else if (const MetadataAsValue *V = dyn_cast(this)) { - V->getMetadata()->print(ROS, getModuleFromVal(V)); + V->getMetadata()->print(ROS, MST, getModuleFromVal(V)); } else if (const Constant *C = dyn_cast(this)) { TypePrinting TypePrinter; TypePrinter.print(C->getType(), OS); OS << ' '; - WriteConstantInternal(OS, C, TypePrinter, nullptr, nullptr); + WriteConstantInternal(OS, C, TypePrinter, MST.getMachine(), nullptr); } else if (isa(this) || isa(this)) { - this->printAsOperand(OS); + this->printAsOperand(OS, /* PrintType */ true, MST); } else { llvm_unreachable("Unknown value to print out!"); } @@ -3315,6 +3329,11 @@ void Metadata::print(raw_ostream &OS, const Module *M) const { printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false); } +void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST, + const Module *M) const { + printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false); +} + // Value::dump - allow easy printing of Values from the debugger. LLVM_DUMP_METHOD void Value::dump() const { print(dbgs()); dbgs() << '\n'; } diff --git a/unittests/IR/MetadataTest.cpp b/unittests/IR/MetadataTest.cpp index b255ba8e034..c1da06814ca 100644 --- a/unittests/IR/MetadataTest.cpp +++ b/unittests/IR/MetadataTest.cpp @@ -16,6 +16,7 @@ #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" +#include "llvm/IR/ModuleSlotTracker.h" #include "llvm/IR/Type.h" #include "llvm/IR/Verifier.h" #include "llvm/Support/raw_ostream.h" @@ -356,6 +357,10 @@ TEST_F(MDNodeTest, PrintFromFunction) { EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M)); EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M)); + + ModuleSlotTracker MST(&M); + EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST)); + EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST)); } TEST_F(MDNodeTest, PrintFromMetadataAsValue) { @@ -384,6 +389,14 @@ TEST_F(MDNodeTest, PrintFromMetadataAsValue) { EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false)); EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true)); EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true)); + + ModuleSlotTracker MST(&M); + EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST)); + EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST)); + EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST)); + EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST)); + EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST)); + EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST)); } #undef EXPECT_PRINTER_EQ diff --git a/unittests/IR/ValueTest.cpp b/unittests/IR/ValueTest.cpp index 4dd0c2c36cf..32d66a1b472 100644 --- a/unittests/IR/ValueTest.cpp +++ b/unittests/IR/ValueTest.cpp @@ -11,6 +11,7 @@ #include "llvm/IR/Function.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" +#include "llvm/IR/ModuleSlotTracker.h" #include "llvm/IR/Value.h" #include "llvm/Support/SourceMgr.h" #include "gtest/gtest.h" @@ -106,4 +107,72 @@ TEST(GlobalTest, AlignDeath) { #endif #endif +TEST(ValueTest, printSlots) { + // Check that Value::print() and Value::printAsOperand() work with and + // without a slot tracker. + LLVMContext C; + + const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n" + "entry:\n" + " %0 = add i32 %y, 1\n" + " %1 = add i32 %y, 1\n" + " ret void\n" + "}\n"; + SMDiagnostic Err; + std::unique_ptr M = parseAssemblyString(ModuleString, Err, C); + + Function *F = M->getFunction("f"); + ASSERT_TRUE(F); + ASSERT_FALSE(F->empty()); + BasicBlock &BB = F->getEntryBlock(); + ASSERT_EQ(3u, BB.size()); + + Instruction *I0 = BB.begin(); + ASSERT_TRUE(I0); + Instruction *I1 = ++BB.begin(); + ASSERT_TRUE(I1); + + ModuleSlotTracker MST(M.get()); + +#define CHECK_PRINT(INST, STR) \ + do { \ + { \ + std::string S; \ + raw_string_ostream OS(S); \ + INST->print(OS); \ + EXPECT_EQ(STR, OS.str()); \ + } \ + { \ + std::string S; \ + raw_string_ostream OS(S); \ + INST->print(OS, MST); \ + EXPECT_EQ(STR, OS.str()); \ + } \ + } while (false) + CHECK_PRINT(I0, " %0 = add i32 %y, 1"); + CHECK_PRINT(I1, " %1 = add i32 %y, 1"); +#undef CHECK_PRINT + +#define CHECK_PRINT_AS_OPERAND(INST, TYPE, STR) \ + do { \ + { \ + std::string S; \ + raw_string_ostream OS(S); \ + INST->printAsOperand(OS, TYPE); \ + EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \ + } \ + { \ + std::string S; \ + raw_string_ostream OS(S); \ + INST->printAsOperand(OS, TYPE, MST); \ + EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \ + } \ + } while (false) + CHECK_PRINT_AS_OPERAND(I0, false, "%0"); + CHECK_PRINT_AS_OPERAND(I1, false, "%1"); + CHECK_PRINT_AS_OPERAND(I0, true, "i32 %0"); + CHECK_PRINT_AS_OPERAND(I1, true, "i32 %1"); +#undef CHECK_PRINT_AS_OPERAND +} + } // end anonymous namespace