12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- """Functionality releated to detecting whole-module optimization."""
15
+ """Detection of various optimization settings ."""
16
16
17
17
load (
18
18
":feature_names.bzl" ,
19
+ "SWIFT_FEATURE_COMPILE_IN_PARALLEL" ,
19
20
"SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS" ,
20
21
"SWIFT_FEATURE__WMO_IN_SWIFTCOPTS" ,
21
22
)
@@ -28,29 +29,14 @@ _WMO_FLAGS = {
28
29
"-force-single-frontend-invocation" : True ,
29
30
}
30
31
31
- def features_from_swiftcopts (swiftcopts ):
32
- """Returns a list of features to enable based on `--swiftcopt` flags.
33
-
34
- Since `--swiftcopt` flags are hooked into the action configuration when the
35
- toolchain is configured, it's not possible for individual actions to query
36
- them easily if those flags may determine the nature of outputs (for example,
37
- single- vs. multi-threaded WMO). The toolchain can call this function to map
38
- those flags to private features that can be queried instead.
39
-
40
- Args:
41
- swiftcopts: The list of command line flags that were passed using
42
- `--swiftcopt`.
43
-
44
- Returns:
45
- A list (possibly empty) of strings denoting feature names that should be
46
- enabled on the toolchain.
47
- """
48
- features = []
49
- if is_wmo_manually_requested (user_compile_flags = swiftcopts ):
50
- features .append (SWIFT_FEATURE__WMO_IN_SWIFTCOPTS )
51
- if find_num_threads_flag_value (user_compile_flags = swiftcopts ) == 0 :
52
- features .append (SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS )
53
- return features
32
+ # Swift command line flags in the `O` group that enable some kind of
33
+ # optimization. This explicitly excludes `-Onone`.
34
+ _OPT_FLAGS = {
35
+ "-O" : True ,
36
+ "-Oplayground" : True ,
37
+ "-Osize" : True ,
38
+ "-Ounchecked" : True ,
39
+ }
54
40
55
41
def find_num_threads_flag_value (user_compile_flags ):
56
42
"""Finds the value of the `-num-threads` flag.
@@ -75,6 +61,20 @@ def find_num_threads_flag_value(user_compile_flags):
75
61
saw_num_threads = True
76
62
return num_threads
77
63
64
+ def is_optimization_manually_requested (user_compile_flags ):
65
+ """Returns `True` if some optimization flag is in the given list of flags.
66
+
67
+ Args:
68
+ user_compile_flags: A list of compiler flags to scan for optimization.
69
+
70
+ Returns:
71
+ True if some optimization is enabled in the given list of flags.
72
+ """
73
+ for copt in user_compile_flags :
74
+ if copt in _OPT_FLAGS :
75
+ return True
76
+ return False
77
+
78
78
def is_wmo_manually_requested (user_compile_flags ):
79
79
"""Returns `True` if a WMO flag is in the given list of compiler flags.
80
80
@@ -89,8 +89,8 @@ def is_wmo_manually_requested(user_compile_flags):
89
89
return True
90
90
return False
91
91
92
- def wmo_features_from_swiftcopts (swiftcopts ):
93
- """Returns a list of features to enable based on `--swiftcopt` flags.
92
+ def optimization_features_from_swiftcopts (swiftcopts ):
93
+ """Returns features to enable or disable based on `--swiftcopt` flags.
94
94
95
95
Since `--swiftcopt` flags are hooked into the action configuration when the
96
96
toolchain is configured, it's not possible for individual actions to query
@@ -103,15 +103,29 @@ def wmo_features_from_swiftcopts(swiftcopts):
103
103
`--swiftcopt`.
104
104
105
105
Returns:
106
- A list (possibly empty) of strings denoting feature names that should be
107
- enabled on the toolchain.
106
+ A tuple containing two lists:
107
+
108
+ 1. A list (possibly empty) of strings denoting feature names that
109
+ should be enabled on the toolchain.
110
+ 2. A list (possibly empty) of strings denoting feature names that
111
+ should be disabled on the toolchain.
108
112
"""
109
- features = []
113
+ requested_features = []
114
+ unsupported_features = []
115
+
116
+ if is_optimization_manually_requested (user_compile_flags = swiftcopts ):
117
+ # Disable parallel compilation early if we know we're going to be doing
118
+ # an optimized compile, because the driver will not emit separate jobs
119
+ # unless we also explicitly disable cross-module optimization. See
120
+ # https://github.com/swiftlang/swift-driver/blob/c647e91574122f2b104d294ab1ec5baadaa1aa95/Sources/SwiftDriver/Jobs/EmitModuleJob.swift#L156-L181.
121
+ unsupported_features .append (SWIFT_FEATURE_COMPILE_IN_PARALLEL )
122
+
110
123
if is_wmo_manually_requested (user_compile_flags = swiftcopts ):
111
- features .append (SWIFT_FEATURE__WMO_IN_SWIFTCOPTS )
124
+ requested_features .append (SWIFT_FEATURE__WMO_IN_SWIFTCOPTS )
112
125
if find_num_threads_flag_value (user_compile_flags = swiftcopts ) == 1 :
113
- features .append (SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS )
114
- return features
126
+ requested_features .append (SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS )
127
+
128
+ return requested_features , unsupported_features
115
129
116
130
def _safe_int (s ):
117
131
"""Returns the base-10 integer value of `s` or `None` if it is invalid.
0 commit comments