Skip to content

Commit 2bd55f6

Browse files
authored
[Autodiff upstream] Add build-script flag for differentiable programming. (#27595)
Add `--enable-experimental-differentiable-programming` build-script flag. The build-script flag enables/disables standard library additions related to differentiable programming. This will allow official Swift releases to disable these additions. The build-script flag is on by default to ensure testing of differentiable programming standard library additions. An additional driver flag must be enabled to use differentiable programming features: #27446
1 parent b0bca5f commit 2bd55f6

File tree

7 files changed

+54
-3
lines changed

7 files changed

+54
-3
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ option(SWIFT_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING
377377
"Build stdlibCore with exclusivity checking enabled"
378378
FALSE)
379379

380+
option(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING
381+
"Enable experimental Swift differentiable programming features"
382+
FALSE)
383+
380384
#
381385
# End of user-configurable options.
382386
#

cmake/modules/AddSwift.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ function(_add_variant_swift_compile_flags
422422
list(APPEND result "-D" "SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS")
423423
endif()
424424

425+
if(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING)
426+
list(APPEND result "-D" "SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING")
427+
endif()
428+
425429
set("${result_var_name}" "${result}" PARENT_SCOPE)
426430
endfunction()
427431

stdlib/public/core/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ set(SWIFTLIB_ESSENTIAL_GYB_SOURCES
196196
UnsafeRawBufferPointer.swift.gyb
197197
)
198198

199+
# Compile differentiable programming sources only if enabled.
200+
set(SWIFTLIB_DIFFERENTIABLE_PROGRAMMING_SOURCES)
201+
if(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING)
202+
# TODO: Add `_Differentiable` protocol.
203+
message(STATUS "Differentiable programming standard library additions enabled.")
204+
endif()
205+
199206
# The complete list of sources in the core standard library. Includes
200207
# all the essential sources listed above.
201208
set(SWIFTLIB_SOURCES
@@ -214,6 +221,7 @@ set(SWIFTLIB_SOURCES
214221
VarArgs.swift
215222
Zip.swift
216223
"${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"
224+
${SWIFTLIB_DIFFERENTIABLE_PROGRAMMING_SOURCES}
217225
)
218226

219227
set(SWIFTLIB_GYB_SOURCES

utils/build_swift/driver_arguments.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,14 @@ def create_argument_parser():
10081008
'Currently only armv7 and aarch64 are supported. '
10091009
'%(default)s is the default.')
10101010

1011+
# -------------------------------------------------------------------------
1012+
in_group('Experimental language features')
1013+
1014+
option('--enable-experimental-differentiable-programming', toggle_true,
1015+
default=True,
1016+
help='Enable experimental Swift differentiable programming language'
1017+
' features.')
1018+
10111019
# -------------------------------------------------------------------------
10121020
in_group('Unsupported options')
10131021

utils/build_swift/tests/expected_options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
'distcc': False,
124124
'dry_run': False,
125125
'enable_asan': False,
126+
'enable_experimental_differentiable_programming': True,
126127
'enable_lsan': False,
127128
'enable_sanitize_coverage': False,
128129
'disable_guaranteed_normal_arguments': False,
@@ -448,6 +449,7 @@ class IgnoreOption(_BaseOption):
448449
EnableOption('--build-swift-stdlib-unittest-extra'),
449450
EnableOption('--distcc'),
450451
EnableOption('--enable-asan'),
452+
EnableOption('--enable-experimental-differentiable-programming'),
451453
EnableOption('--enable-lsan'),
452454
EnableOption('--enable-sanitize-coverage'),
453455
EnableOption('--enable-tsan'),

utils/swift_build_support/swift_build_support/products/swift.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def __init__(self, args, toolchain, source_dir, build_dir):
4141
# Add any exclusivity checking flags for stdlibcore.
4242
self.cmake_options.extend(self._stdlibcore_exclusivity_checking_flags)
4343

44+
# Add experimental differentiable programming flag.
45+
self.cmake_options.extend(
46+
self._enable_experimental_differentiable_programming)
47+
4448
@property
4549
def _runtime_sanitizer_flags(self):
4650
sanitizer_list = []
@@ -112,3 +116,8 @@ def _force_optimized_typechecker_flags(self):
112116
def _stdlibcore_exclusivity_checking_flags(self):
113117
return [('SWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING:BOOL',
114118
self.args.enable_stdlibcore_exclusivity_checking)]
119+
120+
@property
121+
def _enable_experimental_differentiable_programming(self):
122+
return [('SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING:BOOL',
123+
self.args.enable_experimental_differentiable_programming)]

utils/swift_build_support/tests/products/test_swift.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def setUp(self):
5757
benchmark_num_o_iterations=3,
5858
disable_guaranteed_normal_arguments=True,
5959
force_optimized_typechecker=False,
60-
enable_stdlibcore_exclusivity_checking=False)
60+
enable_stdlibcore_exclusivity_checking=False,
61+
enable_experimental_differentiable_programming=False)
6162

6263
# Setup shell
6364
shell.dry_run = True
@@ -87,7 +88,8 @@ def test_by_default_no_cmake_options(self):
8788
expected = [
8889
'-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE',
8990
'-DSWIFT_FORCE_OPTIMIZED_TYPECHECKER:BOOL=FALSE',
90-
'-DSWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING:BOOL=FALSE'
91+
'-DSWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING:BOOL=FALSE',
92+
'-DSWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING:BOOL=FALSE'
9193
]
9294
self.assertEqual(set(swift.cmake_options), set(expected))
9395

@@ -102,7 +104,8 @@ def test_swift_runtime_tsan(self):
102104
'-DSWIFT_RUNTIME_USE_SANITIZERS=Thread',
103105
'-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE',
104106
'-DSWIFT_FORCE_OPTIMIZED_TYPECHECKER:BOOL=FALSE',
105-
'-DSWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING:BOOL=FALSE'
107+
'-DSWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING:BOOL=FALSE',
108+
'-DSWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING:BOOL=FALSE'
106109
]
107110
self.assertEqual(set(swift.cmake_options), set(flags_set))
108111

@@ -302,3 +305,16 @@ def test_exclusivity_checking_flags(self):
302305
'TRUE'],
303306
[x for x in swift.cmake_options
304307
if 'SWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING' in x])
308+
309+
def test_experimental_differentiable_programming_flags(self):
310+
self.args.enable_experimental_differentiable_programming = True
311+
swift = Swift(
312+
args=self.args,
313+
toolchain=self.toolchain,
314+
source_dir='/path/to/src',
315+
build_dir='/path/to/build')
316+
self.assertEqual(
317+
['-DSWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING:BOOL='
318+
'TRUE'],
319+
[x for x in swift.cmake_options
320+
if 'DSWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING' in x])

0 commit comments

Comments
 (0)