Skip to content

Commit f5dd785

Browse files
authored
Merge pull request #70357 from kubamracek/embedded-dep-noalloc
[embedded] In -no-allocations, avoid linking allocating/deallocating runtime functions
2 parents 561a686 + 23d6ee7 commit f5dd785

File tree

5 files changed

+76
-22
lines changed

5 files changed

+76
-22
lines changed

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,7 +2635,7 @@ FUNCTION(GenericAssignWithCopy,
26352635
RETURNS(Int8PtrTy),
26362636
ARGS(Int8PtrTy, Int8PtrTy, TypeMetadataPtrTy),
26372637
ATTRS(NoUnwind),
2638-
EFFECT(Refcounting, Deallocating),
2638+
EFFECT(RefCounting, Deallocating),
26392639
UNKNOWN_MEMEFFECTS)
26402640

26412641
// void *swift_generic_assignWithTake(opaque* dest, opaque* src, const Metadata* type);
@@ -2645,7 +2645,7 @@ FUNCTION(GenericAssignWithTake,
26452645
RETURNS(Int8PtrTy),
26462646
ARGS(Int8PtrTy, Int8PtrTy, TypeMetadataPtrTy),
26472647
ATTRS(NoUnwind),
2648-
EFFECT(Refcounting, Deallocating),
2648+
EFFECT(RefCounting, Deallocating),
26492649
UNKNOWN_MEMEFFECTS)
26502650

26512651
// void *swift_generic_initWithCopy(opaque* dest, opaque* src, const Metadata* type);
@@ -2655,7 +2655,7 @@ FUNCTION(GenericInitWithCopy,
26552655
RETURNS(Int8PtrTy),
26562656
ARGS(Int8PtrTy, Int8PtrTy, TypeMetadataPtrTy),
26572657
ATTRS(NoUnwind),
2658-
EFFECT(Refcounting),
2658+
EFFECT(RefCounting),
26592659
UNKNOWN_MEMEFFECTS)
26602660

26612661
// void *swift_generic_initWithTake(opaque* dest, opaque* src, const Metadata* type);
@@ -2665,7 +2665,7 @@ FUNCTION(GenericInitWithTake,
26652665
RETURNS(Int8PtrTy),
26662666
ARGS(Int8PtrTy, Int8PtrTy, TypeMetadataPtrTy),
26672667
ATTRS(NoUnwind),
2668-
EFFECT(Refcounting),
2668+
EFFECT(RefCounting),
26692669
UNKNOWN_MEMEFFECTS)
26702670

26712671
// void *swift_generic_initializeBufferWithCopyOfBuffer(ValueBuffer* dest, ValueBuffer* src, const Metadata* type);
@@ -2677,7 +2677,7 @@ FUNCTION(GenericInitializeBufferWithCopyOfBuffer,
26772677
getFixedBufferTy()->getPointerTo(),
26782678
TypeMetadataPtrTy),
26792679
ATTRS(NoUnwind),
2680-
EFFECT(Refcounting),
2680+
EFFECT(RefCounting),
26812681
UNKNOWN_MEMEFFECTS)
26822682

26832683
// unsigned swift_singletonEnum_getEnumTag(swift::OpaqueValue *address,

include/swift/SIL/RuntimeEffect.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ inline bool operator&(RuntimeEffect lhs, RuntimeEffect rhs) {
7979

8080
} // end swift namespace
8181

82+
namespace RuntimeConstants {
83+
const auto NoEffect = swift::RuntimeEffect::NoEffect;
84+
const auto Locking = swift::RuntimeEffect::Locking;
85+
const auto Allocating = swift::RuntimeEffect::Allocating;
86+
const auto Deallocating = swift::RuntimeEffect::Deallocating;
87+
const auto RefCounting = swift::RuntimeEffect::RefCounting;
88+
const auto ObjectiveC = swift::RuntimeEffect::ObjectiveC;
89+
const auto Concurrency = swift::RuntimeEffect::Concurrency;
90+
const auto AutoDiff = swift::RuntimeEffect::AutoDiff;
91+
const auto MetaData = swift::RuntimeEffect::MetaData;
92+
const auto Casting = swift::RuntimeEffect::Casting;
93+
const auto ExclusivityChecking = swift::RuntimeEffect::ExclusivityChecking;
94+
}
95+
8296
// Enable the following macro to perform validation check on the runtime effects
8397
// of instructions in IRGen.
8498
// #define CHECK_RUNTIME_EFFECT_ANALYSIS

lib/IRGen/IRGenModule.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -787,20 +787,6 @@ namespace RuntimeConstants {
787787
const auto FirstParamReturned = llvm::Attribute::Returned;
788788
const auto WillReturn = llvm::Attribute::WillReturn;
789789

790-
#ifdef CHECK_RUNTIME_EFFECT_ANALYSIS
791-
const auto NoEffect = RuntimeEffect::NoEffect;
792-
const auto Locking = RuntimeEffect::Locking;
793-
const auto Allocating = RuntimeEffect::Allocating;
794-
const auto Deallocating = RuntimeEffect::Deallocating;
795-
const auto RefCounting = RuntimeEffect::RefCounting;
796-
const auto ObjectiveC = RuntimeEffect::ObjectiveC;
797-
const auto Concurrency = RuntimeEffect::Concurrency;
798-
const auto AutoDiff = RuntimeEffect::AutoDiff;
799-
const auto MetaData = RuntimeEffect::MetaData;
800-
const auto Casting = RuntimeEffect::Casting;
801-
const auto ExclusivityChecking = RuntimeEffect::ExclusivityChecking;
802-
#endif
803-
804790
RuntimeAvailability AlwaysAvailable(ASTContext &Context) {
805791
return RuntimeAvailability::AlwaysAvailable;
806792
}

lib/SILOptimizer/UtilityPasses/Link.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,35 @@ class SILLinker : public SILModuleTransform {
4848
}
4949

5050
void linkEmbeddedRuntimeFromStdlib() {
51+
using namespace RuntimeConstants;
5152
#define FUNCTION(ID, NAME, CC, AVAILABILITY, RETURNS, ARGS, ATTRS, EFFECT, \
5253
MEMORY_EFFECTS) \
53-
linkEmbeddedRuntimeFunctionByName(#NAME);
54+
linkEmbeddedRuntimeFunctionByName(#NAME, EFFECT);
5455

5556
#define RETURNS(...)
5657
#define ARGS(...)
5758
#define NO_ARGS
5859
#define ATTRS(...)
5960
#define NO_ATTRS
60-
#define EFFECT(...)
61+
#define EFFECT(...) { __VA_ARGS__ }
6162
#define MEMORY_EFFECTS(...)
6263
#define UNKNOWN_MEMEFFECTS
6364

6465
#include "swift/Runtime/RuntimeFunctions.def"
6566
}
6667

67-
void linkEmbeddedRuntimeFunctionByName(StringRef name) {
68+
void linkEmbeddedRuntimeFunctionByName(StringRef name,
69+
ArrayRef<RuntimeEffect> effects) {
6870
SILModule &M = *getModule();
6971

72+
bool allocating = false;
73+
for (RuntimeEffect rt : effects)
74+
if (rt == RuntimeEffect::Allocating || rt == RuntimeEffect::Deallocating)
75+
allocating = true;
76+
77+
// Don't link allocating runtime functions in -no-allocations mode.
78+
if (M.getOptions().NoAllocations && allocating) return;
79+
7080
// Bail if runtime function is already loaded.
7181
if (M.lookUpFunction(name)) return;
7282

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -enable-experimental-feature Embedded -no-allocations %s -c -o %t/a.o
3+
4+
// RUN: grep DEP\: %s | sed 's#// DEP\: ##' | sort > %t/allowed-dependencies.txt
5+
6+
// Linux/ELF doesn't use the "_" prefix in symbol mangling.
7+
// RUN: if [ %target-os == "linux-gnu" ]; then sed -E -i -e 's/^_(.*)$/\1/' %t/allowed-dependencies.txt; fi
8+
9+
// RUN: %llvm-nm --undefined-only --format=just-symbols %t/a.o | sort | tee %t/actual-dependencies.txt
10+
11+
// Fail if there is any entry in actual-dependencies.txt that's not in allowed-dependencies.txt
12+
// RUN: test -z "`comm -13 %t/allowed-dependencies.txt %t/actual-dependencies.txt`"
13+
14+
// DEP: ___stack_chk_fail
15+
// DEP: ___stack_chk_guard
16+
// DEP: _memset
17+
// DEP: _putchar
18+
19+
// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o
20+
// RUN: %target-clang %t/a.o %t/print.o -o %t/a.out
21+
// RUN: %target-run %t/a.out | %FileCheck %s
22+
23+
// REQUIRES: swift_in_compiler
24+
// REQUIRES: executable_test
25+
// REQUIRES: optimized_stdlib
26+
// REQUIRES: OS=macosx || OS=linux-gnu
27+
28+
@_silgen_name("putchar")
29+
func putchar(_: UInt8)
30+
31+
public func print(_ s: StaticString, terminator: StaticString = "\n") {
32+
var p = s.utf8Start
33+
while p.pointee != 0 {
34+
putchar(p.pointee)
35+
p += 1
36+
}
37+
p = terminator.utf8Start
38+
while p.pointee != 0 {
39+
putchar(p.pointee)
40+
p += 1
41+
}
42+
}
43+
44+
print("Hello Embedded Swift!") // CHECK: Hello Embedded Swift!

0 commit comments

Comments
 (0)