|
33 | 33 | #include "llvm/Support/FormatVariadic.h"
|
34 | 34 | #include "llvm/Support/GraphWriter.h"
|
35 | 35 | #include "llvm/Support/MemoryBuffer.h"
|
| 36 | +#include "llvm/Support/Path.h" |
36 | 37 | #include "llvm/Support/Program.h"
|
37 | 38 | #include "llvm/Support/Regex.h"
|
38 | 39 | #include "llvm/Support/Signals.h"
|
@@ -830,6 +831,182 @@ void PrintIRInstrumentation::registerCallbacks(
|
830 | 831 | }
|
831 | 832 | }
|
832 | 833 |
|
| 834 | +void DumpIRInstrumentation::registerCallbacks( |
| 835 | + PassInstrumentationCallbacks &PIC) { |
| 836 | + |
| 837 | + if (!(shouldDumpBeforeSomePass() || shouldDumpAfterSomePass())) |
| 838 | + return; |
| 839 | + |
| 840 | + this->PIC = &PIC; |
| 841 | + |
| 842 | + PIC.registerBeforeNonSkippedPassCallback( |
| 843 | + [this](StringRef P, Any IR) { this->pushPass(P, IR); }); |
| 844 | + |
| 845 | + if (shouldDumpBeforeSomePass()) |
| 846 | + PIC.registerBeforeNonSkippedPassCallback( |
| 847 | + [this](StringRef P, Any IR) { this->dumpBeforePass(P, IR); }); |
| 848 | + |
| 849 | + if (shouldDumpAfterSomePass()) { |
| 850 | + PIC.registerAfterPassCallback( |
| 851 | + [this](StringRef P, Any IR, const PreservedAnalyses &) { |
| 852 | + this->dumpAfterPass(P, IR); |
| 853 | + }); |
| 854 | + } |
| 855 | + |
| 856 | + // It is important the the "popPass" callback fires after the dumpAfterPass |
| 857 | + // callback |
| 858 | + PIC.registerAfterPassCallback( |
| 859 | + [this](StringRef P, Any IR, const PreservedAnalyses &) { |
| 860 | + this->popPass(P); |
| 861 | + }); |
| 862 | +} |
| 863 | + |
| 864 | +void DumpIRInstrumentation::dumpBeforePass(StringRef PassID, Any IR) { |
| 865 | + if (isIgnored(PassID)) |
| 866 | + return; |
| 867 | + |
| 868 | + if (!shouldDumpBeforePass(PassID)) { |
| 869 | + return; |
| 870 | + } |
| 871 | + |
| 872 | + SmallString<16> OutputPath = |
| 873 | + fetchCurrentInstrumentationDumpFile("-before.ll"); |
| 874 | + std::error_code EC; |
| 875 | + llvm::raw_fd_ostream OS(OutputPath, EC, llvm::sys::fs::CD_CreateAlways); |
| 876 | + if (EC) |
| 877 | + report_fatal_error(Twine("Failed to open ") + OutputPath + |
| 878 | + " to support dump-before: " + EC.message()); |
| 879 | + |
| 880 | + OS << "*** IR Dump Before " << PassID << " on " << getIRName(IR) << " ***\n"; |
| 881 | + unwrapAndPrint(OS, IR); |
| 882 | +} |
| 883 | + |
| 884 | +void DumpIRInstrumentation::dumpAfterPass(StringRef PassID, Any IR) { |
| 885 | + if (isIgnored(PassID)) |
| 886 | + return; |
| 887 | + |
| 888 | + if (!shouldDumpAfterPass(PassID)) |
| 889 | + return; |
| 890 | + |
| 891 | + SmallString<16> OutputPath = fetchCurrentInstrumentationDumpFile("-after.ll"); |
| 892 | + std::error_code EC; |
| 893 | + llvm::raw_fd_ostream OS(OutputPath, EC, llvm::sys::fs::CD_CreateAlways); |
| 894 | + if (EC) |
| 895 | + report_fatal_error(Twine("Failed to open ") + OutputPath + |
| 896 | + " to support -dump-after: " + EC.message()); |
| 897 | + |
| 898 | + OS << "*** IR Dump After " << PassID << " on " << getIRName(IR) << " ***\n"; |
| 899 | + unwrapAndPrint(OS, IR); |
| 900 | +} |
| 901 | + |
| 902 | +bool DumpIRInstrumentation::shouldDumpBeforePass(StringRef PassID) { |
| 903 | + if (shouldDumpBeforeAll()) |
| 904 | + return true; |
| 905 | + |
| 906 | + StringRef PassName = PIC->getPassNameForClassName(PassID); |
| 907 | + return is_contained(dumpBeforePasses(), PassName); |
| 908 | +} |
| 909 | + |
| 910 | +bool DumpIRInstrumentation::shouldDumpAfterPass(StringRef PassID) { |
| 911 | + if (shouldDumpAfterAll()) |
| 912 | + return true; |
| 913 | + |
| 914 | + StringRef PassName = PIC->getPassNameForClassName(PassID); |
| 915 | + return is_contained(dumpAfterPasses(), PassName); |
| 916 | +} |
| 917 | + |
| 918 | +void DumpIRInstrumentation::pushPass(StringRef PassID, Any IR) { |
| 919 | + const Module *M = unwrapModule(IR); |
| 920 | + if (CurrentModule != M) { |
| 921 | + // If currentModule is nullptr, or is not equal to M, we are starting to |
| 922 | + // process a new module. |
| 923 | + |
| 924 | + // The first frame of the stack should maintain a frequency table |
| 925 | + // for module level passes. |
| 926 | + PipelineStateStack.clear(); |
| 927 | + PipelineStateStack.push_back(PipelineStateStackFrame(M->getName())); |
| 928 | + |
| 929 | + CurrentModule = M; |
| 930 | + } |
| 931 | + |
| 932 | + PassRunsFrequencyTableT &FreqTable = PipelineStateStack.back().FreqTable; |
| 933 | + if (FreqTable.find(PassID) == FreqTable.end()) |
| 934 | + FreqTable[PassID] = 0; |
| 935 | + |
| 936 | + PipelineStateStack.push_back(PipelineStateStackFrame(PassID)); |
| 937 | +} |
| 938 | + |
| 939 | +void DumpIRInstrumentation::popPass(StringRef PassID) { |
| 940 | + PipelineStateStack.pop_back(); |
| 941 | + assert(!PipelineStateStack.empty()); |
| 942 | + |
| 943 | + PassRunsFrequencyTableT &FreqTable = PipelineStateStack.back().FreqTable; |
| 944 | + assert(FreqTable.find(PassID) != FreqTable.end()); |
| 945 | + FreqTable[PassID]++; |
| 946 | + PipelineStateStack.back().PassCount++; |
| 947 | +} |
| 948 | + |
| 949 | +StringRef DumpIRInstrumentation::fetchInstrumentationDumpDirectory() { |
| 950 | + if (!InstrumentationDumpDirectory.empty()) |
| 951 | + return InstrumentationDumpDirectory; |
| 952 | + |
| 953 | + if (!irInstrumentationDumpDirectory().empty()) |
| 954 | + return irInstrumentationDumpDirectory(); |
| 955 | + |
| 956 | + std::error_code EC = |
| 957 | + sys::fs::createUniqueDirectory("dumped-ir", InstrumentationDumpDirectory); |
| 958 | + if (EC) |
| 959 | + report_fatal_error( |
| 960 | + Twine("Failed to create unique directory for IR dumping: ") + |
| 961 | + EC.message()); |
| 962 | + |
| 963 | + return InstrumentationDumpDirectory; |
| 964 | +} |
| 965 | + |
| 966 | +SmallString<16> |
| 967 | +DumpIRInstrumentation::fetchCurrentInstrumentationDumpFile(StringRef Suffix) { |
| 968 | + SmallString<16> OutputPath; |
| 969 | + sys::path::append(OutputPath, fetchInstrumentationDumpDirectory()); |
| 970 | + assert(CurrentModule); |
| 971 | + sys::path::append(OutputPath, CurrentModule->getName()); |
| 972 | + auto *StateStackIt = PipelineStateStack.begin(); |
| 973 | + // Skip over the first frame in the stack which represents the module being |
| 974 | + // processed |
| 975 | + for (++StateStackIt; StateStackIt != PipelineStateStack.end(); |
| 976 | + ++StateStackIt) { |
| 977 | + SmallString<8> PathComponentName; |
| 978 | + // Check the previous frame's pass count to see how many passes of |
| 979 | + // any kind have run on this "nesting level". |
| 980 | + unsigned int PassCount = (StateStackIt - 1)->PassCount; |
| 981 | + PathComponentName += std::to_string(PassCount); |
| 982 | + PathComponentName += "."; |
| 983 | + // Check the previous frame's frequency table to see how many times |
| 984 | + // this pass has run at this "nesting level". |
| 985 | + PassRunsFrequencyTableT &FreqTable = (StateStackIt - 1)->FreqTable; |
| 986 | + StringRef FramePassID = StateStackIt->PassID; |
| 987 | + PathComponentName += FramePassID; |
| 988 | + PathComponentName += "."; |
| 989 | + assert(FreqTable.find(FramePassID) != FreqTable.end() && |
| 990 | + "DumpIRInstrumentation pass frequency table missing entry"); |
| 991 | + // Make sure the uint is converted to a character and not interpretted as |
| 992 | + // one |
| 993 | + PathComponentName += std::to_string(FreqTable[FramePassID]); |
| 994 | + sys::path::append(OutputPath, PathComponentName); |
| 995 | + } |
| 996 | + OutputPath += Suffix; |
| 997 | + |
| 998 | + // Make sure the directory we wish to write our log file into exists. |
| 999 | + StringRef ParentDirectory = sys::path::parent_path(OutputPath); |
| 1000 | + if (!ParentDirectory.empty()) { |
| 1001 | + if (auto EC = llvm::sys::fs::create_directories(ParentDirectory)) { |
| 1002 | + report_fatal_error(Twine("Failed to create directory '") + |
| 1003 | + ParentDirectory + "' :" + EC.message()); |
| 1004 | + } |
| 1005 | + } |
| 1006 | + |
| 1007 | + return OutputPath; |
| 1008 | +} |
| 1009 | + |
833 | 1010 | void OptNoneInstrumentation::registerCallbacks(
|
834 | 1011 | PassInstrumentationCallbacks &PIC) {
|
835 | 1012 | PIC.registerShouldRunOptionalPassCallback(
|
@@ -2288,6 +2465,7 @@ void PrintCrashIRInstrumentation::registerCallbacks(
|
2288 | 2465 | void StandardInstrumentations::registerCallbacks(
|
2289 | 2466 | PassInstrumentationCallbacks &PIC, ModuleAnalysisManager *MAM) {
|
2290 | 2467 | PrintIR.registerCallbacks(PIC);
|
| 2468 | + DumpIR.registerCallbacks(PIC); |
2291 | 2469 | PrintPass.registerCallbacks(PIC);
|
2292 | 2470 | TimePasses.registerCallbacks(PIC);
|
2293 | 2471 | OptNone.registerCallbacks(PIC);
|
|
0 commit comments