Skip to content

Commit 70596be

Browse files
authored
Merge pull request #2436 from swiftwasm/main
[pull] swiftwasm from main
2 parents f88d8e2 + 597c4e8 commit 70596be

File tree

15 files changed

+101
-52
lines changed

15 files changed

+101
-52
lines changed

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,12 @@ if(SWIFT_PATH_TO_CMARK_BUILD)
486486
endif()
487487
message(STATUS "")
488488

489-
if("${SWIFT_NATIVE_LLVM_TOOLS_PATH}" STREQUAL "")
490-
set(SWIFT_CROSS_COMPILING FALSE)
489+
# Check if a prebuilt clang path was passed in, as this variable will be
490+
# assigned if not, in SwiftSharedCMakeConfig.
491+
if("${SWIFT_NATIVE_CLANG_TOOLS_PATH}" STREQUAL "")
492+
set(SWIFT_PREBUILT_CLANG FALSE)
491493
else()
492-
set(SWIFT_CROSS_COMPILING TRUE)
494+
set(SWIFT_PREBUILT_CLANG TRUE)
493495
endif()
494496

495497
include(SwiftSharedCMakeConfig)

cmake/modules/SwiftSharedCMakeConfig.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ macro(swift_common_standalone_build_config_llvm product)
5858
fix_imported_targets_for_xcode("${LLVM_EXPORTED_TARGETS}")
5959
endif()
6060

61-
if(NOT CMAKE_CROSSCOMPILING AND NOT SWIFT_CROSS_COMPILING)
61+
if(NOT CMAKE_CROSSCOMPILING)
6262
set(${product}_NATIVE_LLVM_TOOLS_PATH "${LLVM_TOOLS_BINARY_DIR}")
6363
endif()
6464

@@ -159,7 +159,7 @@ endmacro()
159159
macro(swift_common_standalone_build_config_clang product)
160160
find_package(Clang CONFIG REQUIRED NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
161161

162-
if (NOT CMAKE_CROSSCOMPILING)
162+
if (NOT CMAKE_CROSSCOMPILING AND NOT SWIFT_PREBUILT_CLANG)
163163
set(${product}_NATIVE_CLANG_TOOLS_PATH "${LLVM_TOOLS_BINARY_DIR}")
164164
endif()
165165

lib/ClangImporter/ClangImporter.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -555,23 +555,28 @@ importer::getNormalInvocationArguments(
555555
invocationArgStrs.push_back(
556556
"-Werror=non-modular-include-in-framework-module");
557557

558+
bool EnableCXXInterop = LangOpts.EnableCXXInterop;
559+
558560
if (LangOpts.EnableObjCInterop) {
559-
bool EnableCXXInterop = LangOpts.EnableCXXInterop;
560-
invocationArgStrs.insert(
561-
invocationArgStrs.end(),
562-
{"-x", EnableCXXInterop ? "objective-c++" : "objective-c",
563-
EnableCXXInterop ? "-std=gnu++17" : "-std=gnu11", "-fobjc-arc"});
561+
invocationArgStrs.insert(invocationArgStrs.end(), {"-fobjc-arc"});
564562
// TODO: Investigate whether 7.0 is a suitable default version.
565563
if (!triple.isOSDarwin())
566564
invocationArgStrs.insert(invocationArgStrs.end(),
567565
{"-fobjc-runtime=ios-7.0"});
566+
567+
invocationArgStrs.insert(invocationArgStrs.end(), {
568+
"-x", EnableCXXInterop ? "objective-c++" : "objective-c",
569+
});
568570
} else {
569-
bool EnableCXXInterop = LangOpts.EnableCXXInterop;
570-
invocationArgStrs.insert(invocationArgStrs.end(),
571-
{"-x", EnableCXXInterop ? "c++" : "c",
572-
EnableCXXInterop ? "-std=gnu++17" : "-std=gnu11"});
571+
invocationArgStrs.insert(invocationArgStrs.end(), {
572+
"-x", EnableCXXInterop ? "c++" : "c",
573+
});
573574
}
574575

576+
invocationArgStrs.insert(invocationArgStrs.end(), {
577+
EnableCXXInterop ? "-std=gnu++17" : "-std=gnu11",
578+
});
579+
575580
// Set C language options.
576581
if (triple.isOSDarwin()) {
577582
invocationArgStrs.insert(invocationArgStrs.end(), {

lib/SILOptimizer/Transforms/CSE.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,6 @@ class CSE {
581581

582582
SILOptFunctionBuilder &FuncBuilder;
583583

584-
SILOpenedArchetypesTracker &OpenedArchetypesTracker;
585-
586-
InstructionCloner &Cloner;
587-
588584
DeadEndBlocks &DeadEndBBs;
589585

590586
OwnershipFixupContext &FixupCtx;
@@ -594,16 +590,10 @@ class CSE {
594590
llvm::SmallVector<ApplyInst *, 8> lazyPropertyGetters;
595591

596592
CSE(bool RunsOnHighLevelSil, SideEffectAnalysis *SEA,
597-
SILOptFunctionBuilder &FuncBuilder,
598-
SILOpenedArchetypesTracker &OpenedArchetypesTracker,
599-
InstructionCloner &Cloner, DeadEndBlocks &DeadEndBBs,
593+
SILOptFunctionBuilder &FuncBuilder, DeadEndBlocks &DeadEndBBs,
600594
OwnershipFixupContext &FixupCtx)
601-
: SEA(SEA), FuncBuilder(FuncBuilder),
602-
OpenedArchetypesTracker(OpenedArchetypesTracker), Cloner(Cloner),
603-
DeadEndBBs(DeadEndBBs), FixupCtx(FixupCtx),
604-
RunsOnHighLevelSil(RunsOnHighLevelSil) {
605-
Cloner.getBuilder().setOpenedArchetypesTracker(&OpenedArchetypesTracker);
606-
}
595+
: SEA(SEA), FuncBuilder(FuncBuilder), DeadEndBBs(DeadEndBBs),
596+
FixupCtx(FixupCtx), RunsOnHighLevelSil(RunsOnHighLevelSil) {}
607597

608598
bool processFunction(SILFunction &F, DominanceInfo *DT);
609599

@@ -843,11 +833,16 @@ bool CSE::processOpenExistentialRef(OpenExistentialRefInst *Inst,
843833
}
844834

845835
// Now process candidates.
836+
SILOpenedArchetypesTracker OpenedArchetypesTracker(Inst->getFunction());
846837
// Register the new archetype to be used.
847838
OpenedArchetypesTracker.registerOpenedArchetypes(VI);
839+
// Use a cloner. It makes copying the instruction and remapping of
840+
// opened archetypes trivial.
841+
InstructionCloner Cloner(Inst->getFunction());
848842
Cloner.registerOpenedExistentialRemapping(
849843
OldOpenedArchetype->castTo<ArchetypeType>(), NewOpenedArchetype);
850844
auto &Builder = Cloner.getBuilder();
845+
Builder.setOpenedArchetypesTracker(&OpenedArchetypesTracker);
851846

852847
llvm::SmallPtrSet<SILInstruction *, 16> Processed;
853848
// Now clone each candidate and replace the opened archetype
@@ -1401,14 +1396,11 @@ class SILCSE : public SILFunctionTransform {
14011396
SILOptFunctionBuilder FuncBuilder(*this);
14021397

14031398
auto *Fn = getFunction();
1404-
SILOpenedArchetypesTracker OpenedArchetypesTracker(Fn);
1405-
InstructionCloner Cloner(Fn);
14061399
DeadEndBlocks DeadEndBBs(Fn);
14071400
JointPostDominanceSetComputer Computer(DeadEndBBs);
14081401
InstModCallbacks callbacks;
14091402
OwnershipFixupContext FixupCtx{callbacks, DeadEndBBs, Computer};
1410-
CSE C(RunsOnHighLevelSil, SEA, FuncBuilder, OpenedArchetypesTracker, Cloner,
1411-
DeadEndBBs, FixupCtx);
1403+
CSE C(RunsOnHighLevelSil, SEA, FuncBuilder, DeadEndBBs, FixupCtx);
14121404
bool Changed = false;
14131405

14141406
// Perform the traditional CSE.

stdlib/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ else()
116116
# If we use Clang-cl or MSVC, CMake provides default compiler and linker flags that are incompatible
117117
# with the frontend of Clang or Clang++.
118118
if(SWIFT_COMPILER_IS_MSVC_LIKE)
119-
set(CMAKE_CXX_COMPILER "${SWIFT_NATIVE_LLVM_TOOLS_PATH}/clang-cl")
120-
set(CMAKE_C_COMPILER "${SWIFT_NATIVE_LLVM_TOOLS_PATH}/clang-cl")
119+
set(CMAKE_CXX_COMPILER "${SWIFT_NATIVE_CLANG_TOOLS_PATH}/clang-cl")
120+
set(CMAKE_C_COMPILER "${SWIFT_NATIVE_CLANG_TOOLS_PATH}/clang-cl")
121121
else()
122-
set(CMAKE_CXX_COMPILER "${SWIFT_NATIVE_LLVM_TOOLS_PATH}/clang++")
123-
set(CMAKE_C_COMPILER "${SWIFT_NATIVE_LLVM_TOOLS_PATH}/clang")
122+
set(CMAKE_CXX_COMPILER "${SWIFT_NATIVE_CLANG_TOOLS_PATH}/clang++")
123+
set(CMAKE_C_COMPILER "${SWIFT_NATIVE_CLANG_TOOLS_PATH}/clang")
124124
endif()
125125

126126
if(CMAKE_C_COMPILER_LAUNCHER MATCHES ".*distcc")

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,8 @@ function(add_swift_target_library name)
16581658
list(APPEND SWIFTLIB_SWIFT_COMPILE_FLAGS "-warn-implicit-overrides")
16591659
endif()
16601660

1661-
if(NOT SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER AND NOT BUILD_STANDALONE)
1661+
if(NOT SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER AND NOT BUILD_STANDALONE AND
1662+
NOT SWIFT_PREBUILT_CLANG)
16621663
list(APPEND SWIFTLIB_DEPENDS clang)
16631664
endif()
16641665

stdlib/public/SwiftShims/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ endif()
111111
# First extract the "version" used for Clang's resource directory.
112112
string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
113113
"${LLVM_PACKAGE_VERSION}")
114-
if(NOT SWIFT_INCLUDE_TOOLS AND SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER)
114+
if(NOT SWIFT_INCLUDE_TOOLS AND
115+
(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER OR SWIFT_PREBUILT_CLANG))
115116
if(SWIFT_COMPILER_IS_MSVC_LIKE)
116117
execute_process(COMMAND ${CMAKE_C_COMPILER} /clang:-print-resource-dir
117118
OUTPUT_VARIABLE clang_headers_location

test/SILOptimizer/cse.sil

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,32 @@ bb3:
12441244
return %20 : $()
12451245
}
12461246

1247+
// This test is not a repro but used to show why we need a local Cloner object in CSE::processOpenExistentialRef.
1248+
// Discussion:
1249+
// Since the CSE pass can delete instructions and allocate instructions whose state can be cached in the Cloner, we have to use local Cloner object so that we don't accidentally refer to stale state
1250+
// In this test users of %9 are all cloned and original instructions are deleted. Users of %13 are similarly cloned and original instructions are deleted so the types can be remapped.
1251+
// Instructions can further get deleted in the main CSE pass as well as during SimplifyInstruction.
1252+
// For some heap allocation patterns newly allocated instructions can get the same address of a previously allocated instruction, and can end up referring to a stale state in the Cloner.
1253+
//
1254+
// CHECK-LABEL: sil @cse_open_existential_ref_local_cloner :
1255+
// CHECK: open_existential_ref
1256+
// CHECK-NOT: open_existential_ref
1257+
// CHECK-LABEL: } // end sil function 'cse_open_existential_ref_local_cloner'
1258+
sil @cse_open_existential_ref_local_cloner : $@convention(thin) (@guaranteed Proto, Bool) -> () {
1259+
bb0(%0 : $Proto, %1 : $Bool):
1260+
%4 = open_existential_ref %0 : $Proto to $@opened("1B68354A-4796-11E6-B7DF-B8E856428C60") Proto
1261+
%5 = witness_method $@opened("1B68354A-4796-11E6-B7DF-B8E856428C60") Proto, #Proto.doThis, %4 : $@opened("1B68354A-4796-11E6-B7DF-B8E856428C60") Proto : $@convention(witness_method: Proto) <τ_0_0 where τ_0_0 : Proto> (@guaranteed τ_0_0) -> ()
1262+
%6 = apply %5<@opened("1B68354A-4796-11E6-B7DF-B8E856428C60") Proto>(%4) : $@convention(witness_method: Proto) <τ_0_0 where τ_0_0 : Proto> (@guaranteed τ_0_0) -> ()
1263+
%9 = open_existential_ref %0 : $Proto to $@opened("1B685052-4796-11E6-B7DF-B8E856428C60") Proto
1264+
%10 = witness_method $@opened("1B685052-4796-11E6-B7DF-B8E856428C60") Proto, #Proto.doThat, %9 : $@opened("1B685052-4796-11E6-B7DF-B8E856428C60") Proto : $@convention(witness_method: Proto) <τ_0_0 where τ_0_0 : Proto> (@guaranteed τ_0_0) -> ()
1265+
%11 = apply %10<@opened("1B685052-4796-11E6-B7DF-B8E856428C60") Proto>(%9) : $@convention(witness_method: Proto) <τ_0_0 where τ_0_0 : Proto> (@guaranteed τ_0_0) -> ()
1266+
%13 = open_existential_ref %0 : $Proto to $@opened("1B6851A6-4796-11E6-B7DF-B8E856428C60") Proto
1267+
%14 = witness_method $@opened("1B6851A6-4796-11E6-B7DF-B8E856428C60") Proto, #Proto.doThis, %13 : $@opened("1B6851A6-4796-11E6-B7DF-B8E856428C60") Proto : $@convention(witness_method: Proto) <τ_0_0 where τ_0_0 : Proto> (@guaranteed τ_0_0) -> ()
1268+
%15 = apply %14<@opened("1B6851A6-4796-11E6-B7DF-B8E856428C60") Proto>(%13) : $@convention(witness_method: Proto) <τ_0_0 where τ_0_0 : Proto> (@guaranteed τ_0_0) -> ()
1269+
%res = tuple ()
1270+
return %res : $()
1271+
}
1272+
12471273
// Check that we don't CSE open_existential_ref if they are not compeltely equal.
12481274
// CHECK-LABEL: sil @dont_cse_open_existential_ref
12491275
// CHECK: open_existential_ref

unittests/runtime/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
1313
# If we use Clang-cl or MSVC, CMake provides default compiler and linker flags that are incompatible
1414
# with the frontend of Clang or Clang++.
1515
if(SWIFT_COMPILER_IS_MSVC_LIKE)
16-
set(CMAKE_CXX_COMPILER "${SWIFT_NATIVE_LLVM_TOOLS_PATH}/clang-cl")
17-
set(CMAKE_C_COMPILER "${SWIFT_NATIVE_LLVM_TOOLS_PATH}/clang-cl")
16+
set(CMAKE_CXX_COMPILER "${SWIFT_NATIVE_CLANG_TOOLS_PATH}/clang-cl")
17+
set(CMAKE_C_COMPILER "${SWIFT_NATIVE_CLANG_TOOLS_PATH}/clang-cl")
1818
else()
19-
set(CMAKE_CXX_COMPILER "${SWIFT_NATIVE_LLVM_TOOLS_PATH}/clang++")
20-
set(CMAKE_C_COMPILER "${SWIFT_NATIVE_LLVM_TOOLS_PATH}/clang")
19+
set(CMAKE_CXX_COMPILER "${SWIFT_NATIVE_CLANG_TOOLS_PATH}/clang++")
20+
set(CMAKE_C_COMPILER "${SWIFT_NATIVE_CLANG_TOOLS_PATH}/clang")
2121
endif()
2222

2323
if(CMAKE_C_COMPILER_LAUNCHER MATCHES ".*distcc")

utils/build-presets.ini

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,10 +2283,9 @@ skip-build-cmark
22832283
skip-build-benchmarks
22842284
skip-test-cmark
22852285

2286-
# This triggers the stdlib standalone build: Don't build tools (the compiler),
2287-
# assume we are working with the host compiler.
2286+
# This triggers the stdlib standalone build: don't build the native tools from
2287+
# scratch, ie the compiler.
22882288
build-swift-tools=0
2289-
build-runtime-with-host-compiler=1
22902289

22912290
# Then set the paths to our native tools. If compiling against a toolchain,
22922291
# these should all be the ./usr/bin directory.

utils/build-script

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,18 @@ class BuildScriptInvocation(object):
720720
impl_args += [
721721
"--host-libtool", toolchain.libtool,
722722
]
723+
if args.native_clang_tools_path is not None:
724+
impl_args += [
725+
"--native-clang-tools-path=%s" % args.native_clang_tools_path
726+
]
727+
if args.native_llvm_tools_path is not None:
728+
impl_args += [
729+
"--native-llvm-tools-path=%s" % args.native_llvm_tools_path
730+
]
731+
if args.native_swift_tools_path is not None:
732+
impl_args += [
733+
"--native-swift-tools-path=%s" % args.native_swift_tools_path
734+
]
723735

724736
# If we have extra_swift_args, combine all of them together and then
725737
# add them as one command.

utils/build-script-impl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,13 +1493,6 @@ for host in "${ALL_HOSTS[@]}"; do
14931493
fi
14941494
fi
14951495

1496-
if [[ "${NATIVE_CLANG_TOOLS_PATH}" ]] ; then
1497-
common_cmake_options_host+=(
1498-
-DCMAKE_C_COMPILER="${NATIVE_CLANG_TOOLS_PATH}/clang"
1499-
-DCMAKE_CXX_COMPILER="${NATIVE_CLANG_TOOLS_PATH}/clang++"
1500-
)
1501-
fi
1502-
15031496
llvm_cmake_options=(
15041497
"${llvm_cmake_options[@]}"
15051498
-DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"

utils/build_swift/build_swift/driver_arguments.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,15 @@ def create_argument_parser():
389389
option('--host-cxx', store_path(executable=True),
390390
help='the absolute path to CXX, the "clang++" compiler for the '
391391
'host platform. Default is auto detected.')
392+
option('--native-swift-tools-path', store_path,
393+
help='the path to a directory that contains prebuilt Swift tools '
394+
'that are executable on the host platform')
395+
option('--native-clang-tools-path', store_path,
396+
help='the path to a directory that contains prebuilt Clang tools '
397+
'that are executable on the host platform')
398+
option('--native-llvm-tools-path', store_path,
399+
help='the path to a directory that contains prebuilt LLVM tools '
400+
'that are executable on the host platform')
392401
option('--cmake-c-launcher', store_path(executable=True),
393402
default=os.environ.get('C_COMPILER_LAUNCHER', None),
394403
help='the absolute path to set CMAKE_C_COMPILER_LAUNCHER')

utils/build_swift/tests/expected_options.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@
189189
'lto_type': None,
190190
'maccatalyst': False,
191191
'maccatalyst_ios_tests': False,
192+
'native_clang_tools_path': None,
193+
'native_llvm_tools_path': None,
194+
'native_swift_tools_path': None,
192195
'dump_config': False,
193196
'show_sdks': False,
194197
'skip_build': False,
@@ -653,6 +656,9 @@ class BuildScriptImplOption(_BaseOption):
653656
PathOption('--install-symroot'),
654657
PathOption('--install-destdir'),
655658
EnableOption('--install-all'),
659+
PathOption('--native-clang-tools-path'),
660+
PathOption('--native-llvm-tools-path'),
661+
PathOption('--native-swift-tools-path'),
656662
PathOption('--symbols-package'),
657663
PathOption('--cmake-c-launcher'),
658664
PathOption('--cmake-cxx-launcher'),

utils/swift_build_support/swift_build_support/products/product.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,11 @@ def install_toolchain_path(self, host_target):
167167
"""toolchain_path() -> string
168168
169169
Returns the path to the toolchain that is being created as part of this
170-
build.
170+
build, or to a native prebuilt toolchain that was passed in.
171171
"""
172+
if self.args.native_swift_tools_path is not None:
173+
return os.path.split(self.args.native_swift_tools_path)[0]
174+
172175
install_destdir = self.args.install_destdir
173176
if self.args.cross_compile_hosts:
174177
build_root = os.path.dirname(self.build_dir)

0 commit comments

Comments
 (0)