@@ -854,6 +854,46 @@ static bool performCompile(CompilerInstance &Instance,
854
854
SM->verify ();
855
855
}
856
856
857
+ // This is the action to be used to serialize SILModule.
858
+ // It may be invoked multiple times, but it will perform
859
+ // serialization only once. The serialization may either happen
860
+ // after high-level optimizations or after all optimizations are
861
+ // done, depending on the compiler setting.
862
+
863
+ auto SerializeSILModuleAction = [&]() {
864
+ if (!opts.ModuleOutputPath .empty () || !opts.ModuleDocOutputPath .empty ()) {
865
+ auto DC = PrimarySourceFile ? ModuleOrSourceFile (PrimarySourceFile)
866
+ : Instance.getMainModule ();
867
+ if (!opts.ModuleOutputPath .empty ()) {
868
+ SerializationOptions serializationOpts;
869
+ serializationOpts.OutputPath = opts.ModuleOutputPath .c_str ();
870
+ serializationOpts.DocOutputPath = opts.ModuleDocOutputPath .c_str ();
871
+ serializationOpts.GroupInfoPath = opts.GroupInfoPath .c_str ();
872
+ if (opts.SerializeBridgingHeader )
873
+ serializationOpts.ImportedHeader = opts.ImplicitObjCHeaderPath ;
874
+ serializationOpts.ModuleLinkName = opts.ModuleLinkName ;
875
+ serializationOpts.ExtraClangOptions =
876
+ Invocation.getClangImporterOptions ().ExtraArgs ;
877
+ serializationOpts.EnableNestedTypeLookupTable =
878
+ opts.EnableSerializationNestedTypeLookupTable ;
879
+ if (!IRGenOpts.ForceLoadSymbolName .empty ())
880
+ serializationOpts.AutolinkForceLoad = true ;
881
+
882
+ // Options contain information about the developer's computer,
883
+ // so only serialize them if the module isn't going to be shipped to
884
+ // the public.
885
+ serializationOpts.SerializeOptionsForDebugging =
886
+ !moduleIsPublic || opts.AlwaysSerializeDebuggingOptions ;
887
+
888
+ serialize (DC, serializationOpts, SM.get ());
889
+ }
890
+ }
891
+ };
892
+
893
+ // Set the serialization action, so that the SIL module
894
+ // can be serialized at any moment, e.g. during the optimization pipeline.
895
+ SM->setSerializeSILAction (SerializeSILModuleAction);
896
+
857
897
// Perform SIL optimization passes if optimizations haven't been disabled.
858
898
// These may change across compiler versions.
859
899
{
@@ -921,32 +961,9 @@ static bool performCompile(CompilerInstance &Instance,
921
961
}
922
962
923
963
if (!opts.ModuleOutputPath .empty () || !opts.ModuleDocOutputPath .empty ()) {
924
- auto DC = PrimarySourceFile ? ModuleOrSourceFile (PrimarySourceFile) :
925
- Instance.getMainModule ();
926
- if (!opts.ModuleOutputPath .empty ()) {
927
- SerializationOptions serializationOpts;
928
- serializationOpts.OutputPath = opts.ModuleOutputPath .c_str ();
929
- serializationOpts.DocOutputPath = opts.ModuleDocOutputPath .c_str ();
930
- serializationOpts.GroupInfoPath = opts.GroupInfoPath .c_str ();
931
- if (opts.SerializeBridgingHeader )
932
- serializationOpts.ImportedHeader = opts.ImplicitObjCHeaderPath ;
933
- serializationOpts.ModuleLinkName = opts.ModuleLinkName ;
934
- serializationOpts.ExtraClangOptions =
935
- Invocation.getClangImporterOptions ().ExtraArgs ;
936
- serializationOpts.EnableNestedTypeLookupTable =
937
- opts.EnableSerializationNestedTypeLookupTable ;
938
- if (!IRGenOpts.ForceLoadSymbolName .empty ())
939
- serializationOpts.AutolinkForceLoad = true ;
940
-
941
- // Options contain information about the developer's computer,
942
- // so only serialize them if the module isn't going to be shipped to
943
- // the public.
944
- serializationOpts.SerializeOptionsForDebugging =
945
- !moduleIsPublic || opts.AlwaysSerializeDebuggingOptions ;
946
-
947
- serialize (DC, serializationOpts, SM.get ());
948
- }
949
-
964
+ // Serialize the SILModule if it was not serialized yet.
965
+ if (!SM.get ()->isSerialized ())
966
+ SM.get ()->serialize ();
950
967
if (Action == FrontendOptions::MergeModules ||
951
968
Action == FrontendOptions::EmitModuleOnly) {
952
969
if (shouldIndex) {
0 commit comments