@@ -755,9 +755,7 @@ PrintIRInstrumentation::~PrintIRInstrumentation() {
755
755
" PassRunDescriptorStack is not empty at exit" );
756
756
}
757
757
758
- static SmallString<32 > getIRFileDisplayName (Any IR) {
759
- SmallString<32 > Result;
760
- raw_svector_ostream ResultStream (Result);
758
+ static void writeIRFileDisplayName (raw_ostream &ResultStream, Any IR) {
761
759
const Module *M = unwrapModule (IR, /* Force=*/ true );
762
760
assert (M && " should have unwrapped module" );
763
761
uint64_t NameHash = xxh3_64bits (M->getName ());
@@ -786,44 +784,44 @@ static SmallString<32> getIRFileDisplayName(Any IR) {
786
784
} else {
787
785
llvm_unreachable (" Unknown wrapped IR type" );
788
786
}
787
+ }
788
+
789
+ static std::string getIRFileDisplayName (Any IR) {
790
+ std::string Result;
791
+ raw_string_ostream ResultStream (Result);
792
+ writeIRFileDisplayName (ResultStream, IR);
789
793
return Result;
790
794
}
791
795
792
- std::string PrintIRInstrumentation::fetchDumpFilename (StringRef PassName,
793
- Any IR) {
794
- const StringRef RootDirectory = IRDumpDirectory;
795
- assert (!RootDirectory.empty () &&
796
+ StringRef PrintIRInstrumentation::getFileSuffix (IRDumpFileSuffixType Type) {
797
+ static constexpr std::array FileSuffixes = {" -before.ll" , " -after.ll" ,
798
+ " -invalidated.ll" };
799
+ return FileSuffixes[static_cast <size_t >(Type)];
800
+ }
801
+
802
+ std::string PrintIRInstrumentation::fetchDumpFilename (
803
+ StringRef PassName, StringRef IRFileDisplayName, unsigned PassNumber,
804
+ IRDumpFileSuffixType SuffixType) {
805
+ assert (!IRDumpDirectory.empty () &&
796
806
" The flag -ir-dump-directory must be passed to dump IR to files" );
797
- SmallString<128 > ResultPath;
798
- ResultPath += RootDirectory;
807
+
799
808
SmallString<64 > Filename;
800
809
raw_svector_ostream FilenameStream (Filename);
801
- FilenameStream << CurrentPassNumber;
802
- FilenameStream << " -" ;
803
- FilenameStream << getIRFileDisplayName (IR);
804
- FilenameStream << " -" ;
810
+ FilenameStream << PassNumber;
811
+ FilenameStream << ' -' << IRFileDisplayName << ' -' ;
805
812
FilenameStream << PassName;
806
- sys::path::append (ResultPath, Filename);
807
- return std::string (ResultPath);
808
- }
813
+ FilenameStream << getFileSuffix (SuffixType);
809
814
810
- enum class IRDumpFileSuffixType {
811
- Before,
812
- After,
813
- Invalidated,
814
- };
815
-
816
- static StringRef getFileSuffix (IRDumpFileSuffixType Type) {
817
- static constexpr std::array FileSuffixes = {" -before.ll" , " -after.ll" ,
818
- " -invalidated.ll" };
819
- return FileSuffixes[static_cast <size_t >(Type)];
815
+ SmallString<128 > ResultPath;
816
+ sys::path::append (ResultPath, IRDumpDirectory, Filename);
817
+ return std::string (ResultPath);
820
818
}
821
819
822
- void PrintIRInstrumentation::pushPassRunDescriptor (
823
- StringRef PassID, Any IR, std::string &DumpIRFilename ) {
820
+ void PrintIRInstrumentation::pushPassRunDescriptor (StringRef PassID, Any IR,
821
+ unsigned PassNumber ) {
824
822
const Module *M = unwrapModule (IR);
825
- PassRunDescriptorStack.emplace_back (
826
- PassRunDescriptor (M, DumpIRFilename, getIRName (IR), PassID) );
823
+ PassRunDescriptorStack.emplace_back (M, PassNumber, getIRFileDisplayName (IR),
824
+ getIRName (IR), PassID);
827
825
}
828
826
829
827
PrintIRInstrumentation::PassRunDescriptor
@@ -857,19 +855,12 @@ void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
857
855
if (isIgnored (PassID))
858
856
return ;
859
857
860
- std::string DumpIRFilename;
861
- if (!IRDumpDirectory.empty () &&
862
- (shouldPrintBeforePass (PassID) || shouldPrintAfterPass (PassID) ||
863
- shouldPrintBeforeCurrentPassNumber () ||
864
- shouldPrintAfterCurrentPassNumber ()))
865
- DumpIRFilename = fetchDumpFilename (PassID, IR);
866
-
867
858
// Saving Module for AfterPassInvalidated operations.
868
859
// Note: here we rely on a fact that we do not change modules while
869
860
// traversing the pipeline, so the latest captured module is good
870
861
// for all print operations that has not happen yet.
871
862
if (shouldPrintAfterPass (PassID))
872
- pushPassRunDescriptor (PassID, IR, DumpIRFilename );
863
+ pushPassRunDescriptor (PassID, IR, CurrentPassNumber );
873
864
874
865
if (!shouldPrintIR (IR))
875
866
return ;
@@ -881,7 +872,7 @@ void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
881
872
<< " on " << getIRName (IR) << " \n " ;
882
873
883
874
if (shouldPrintAfterCurrentPassNumber ())
884
- pushPassRunDescriptor (PassID, IR, DumpIRFilename );
875
+ pushPassRunDescriptor (PassID, IR, CurrentPassNumber );
885
876
886
877
if (!shouldPrintBeforePass (PassID) && !shouldPrintBeforeCurrentPassNumber ())
887
878
return ;
@@ -894,8 +885,10 @@ void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
894
885
unwrapAndPrint (Stream, IR);
895
886
};
896
887
897
- if (!DumpIRFilename.empty ()) {
898
- DumpIRFilename += getFileSuffix (IRDumpFileSuffixType::Before);
888
+ if (!IRDumpDirectory.empty ()) {
889
+ std::string DumpIRFilename =
890
+ fetchDumpFilename (PassID, getIRFileDisplayName (IR), CurrentPassNumber,
891
+ IRDumpFileSuffixType::Before);
899
892
llvm::raw_fd_ostream DumpIRFileStream{
900
893
prepareDumpIRFileDescriptor (DumpIRFilename), /* shouldClose */ true };
901
894
WriteIRToStream (DumpIRFileStream);
@@ -911,7 +904,8 @@ void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
911
904
if (!shouldPrintAfterPass (PassID) && !shouldPrintAfterCurrentPassNumber ())
912
905
return ;
913
906
914
- auto [M, DumpIRFilename, IRName, StoredPassID] = popPassRunDescriptor (PassID);
907
+ auto [M, PassNumber, IRFileDisplayName, IRName, StoredPassID] =
908
+ popPassRunDescriptor (PassID);
915
909
assert (StoredPassID == PassID && " mismatched PassID" );
916
910
917
911
if (!shouldPrintIR (IR) ||
@@ -927,12 +921,11 @@ void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
927
921
};
928
922
929
923
if (!IRDumpDirectory.empty ()) {
930
- assert (!DumpIRFilename.empty () && " DumpIRFilename must not be empty and "
931
- " should be set in printBeforePass" );
932
- const std::string DumpIRFilenameWithSuffix =
933
- DumpIRFilename + getFileSuffix (IRDumpFileSuffixType::After).str ();
924
+ std::string DumpIRFilename =
925
+ fetchDumpFilename (PassID, getIRFileDisplayName (IR), CurrentPassNumber,
926
+ IRDumpFileSuffixType::After);
934
927
llvm::raw_fd_ostream DumpIRFileStream{
935
- prepareDumpIRFileDescriptor (DumpIRFilenameWithSuffix ),
928
+ prepareDumpIRFileDescriptor (DumpIRFilename ),
936
929
/* shouldClose */ true };
937
930
WriteIRToStream (DumpIRFileStream, IRName);
938
931
} else {
@@ -947,7 +940,8 @@ void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
947
940
if (!shouldPrintAfterPass (PassID) && !shouldPrintAfterCurrentPassNumber ())
948
941
return ;
949
942
950
- auto [M, DumpIRFilename, IRName, StoredPassID] = popPassRunDescriptor (PassID);
943
+ auto [M, PassNumber, IRFileDisplayName, IRName, StoredPassID] =
944
+ popPassRunDescriptor (PassID);
951
945
assert (StoredPassID == PassID && " mismatched PassID" );
952
946
// Additional filtering (e.g. -filter-print-func) can lead to module
953
947
// printing being skipped.
@@ -965,13 +959,12 @@ void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
965
959
};
966
960
967
961
if (!IRDumpDirectory.empty ()) {
968
- assert (!DumpIRFilename.empty () && " DumpIRFilename must not be empty and "
969
- " should be set in printBeforePass" );
970
- const std::string DumpIRFilenameWithSuffix =
971
- DumpIRFilename + getFileSuffix (IRDumpFileSuffixType::Invalidated).str ();
962
+ std::string DumpIRFilename =
963
+ fetchDumpFilename (PassID, IRFileDisplayName, PassNumber,
964
+ IRDumpFileSuffixType::Invalidated);
972
965
llvm::raw_fd_ostream DumpIRFileStream{
973
- prepareDumpIRFileDescriptor (DumpIRFilenameWithSuffix ),
974
- /* shouldClose */ true };
966
+ prepareDumpIRFileDescriptor (DumpIRFilename ),
967
+ /* shouldClose= */ true };
975
968
WriteIRToStream (DumpIRFileStream, M, IRName);
976
969
} else {
977
970
WriteIRToStream (dbgs (), M, IRName);
0 commit comments