Skip to content

Commit 2c24c12

Browse files
committed
[embedded] In -no-allocations, avoid linking allocating/deallocating runtime functions
1 parent c266644 commit 2c24c12

File tree

5 files changed

+81
-25
lines changed

5 files changed

+81
-25
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: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,33 @@ class SILLinker : public SILModuleTransform {
4343
// In embedded Swift, the stdlib contains all the runtime functions needed
4444
// (swift_retain, etc.). Link them in so they can be referenced in IRGen.
4545
if (M.getOptions().EmbeddedSwift && LinkEmbeddedRuntime) {
46-
linkEmbeddedRuntimeFromStdlib();
46+
linkEmbeddedRuntimeFromStdlib(!M.getOptions().NoAllocations);
4747
}
4848
}
4949

50-
void linkEmbeddedRuntimeFromStdlib() {
51-
#define FUNCTION(ID, NAME, CC, AVAILABILITY, RETURNS, ARGS, ATTRS, EFFECT, \
52-
MEMORY_EFFECTS) \
53-
linkEmbeddedRuntimeFunctionByName(#NAME);
50+
void linkEmbeddedRuntimeFromStdlib(bool includeAllocatingFunctions) {
51+
#define FUNCTION(ID, NAME, CC, AVAILABILITY, RETURNS, ARGS, ATTRS, EFFECT, \
52+
MEMORY_EFFECTS) \
53+
{ \
54+
using namespace RuntimeConstants; \
55+
ArrayRef<RuntimeEffect> effects = {EFFECT}; \
56+
bool allocating = false; \
57+
for (RuntimeEffect rt : effects) { \
58+
if (rt == RuntimeEffect::Allocating || \
59+
rt == RuntimeEffect::Deallocating) \
60+
allocating = true; \
61+
} \
62+
bool include = true; \
63+
if (allocating) include = includeAllocatingFunctions; \
64+
if (include) linkEmbeddedRuntimeFunctionByName(#NAME); \
65+
}
5466

5567
#define RETURNS(...)
5668
#define ARGS(...)
5769
#define NO_ARGS
5870
#define ATTRS(...)
5971
#define NO_ATTRS
60-
#define EFFECT(...)
72+
#define EFFECT(...) __VA_ARGS__
6173
#define MEMORY_EFFECTS(...)
6274
#define UNKNOWN_MEMEFFECTS
6375

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)