Skip to content

Commit 4fb4435

Browse files
committed
IRGen: add an option to disable runtime calls for dynamic replacements.
With the option -Xllvm -basic-dynamic-replacement the runtime functions are not called (so it works with an old swift library). But calling the original of a replaced function is not supported in this case.
1 parent 7a5de47 commit 4fb4435

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
using namespace swift;
7676
using namespace irgen;
7777

78+
llvm::cl::opt<bool> UseBasicDynamicReplacement(
79+
"basic-dynamic-replacement", llvm::cl::init(false),
80+
llvm::cl::desc("Basic implementation of dynamic replacement"));
81+
7882
namespace {
7983

8084
/// Add methods, properties, and protocol conformances from a JITed extension
@@ -2208,12 +2212,20 @@ void IRGenModule::createReplaceableProlog(IRGenFunction &IGF, SILFunction *f) {
22082212
auto *FnAddr = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
22092213
IGF.CurFn, FunctionPtrTy);
22102214

2211-
// Call swift_getFunctionReplacement to check which function to call.
2212-
auto *ReplFn = IGF.Builder.CreateCall(getGetReplacementFn(),
2213-
{ ReplAddr, FnAddr });
2214-
ReplFn->setDoesNotThrow();
2215-
auto *hasReplFn = IGF.Builder.CreateICmpEQ(ReplFn,
2216-
llvm::ConstantExpr::getNullValue(ReplFn->getType()));
2215+
llvm::Value *ReplFn = nullptr, *rhs = nullptr;
2216+
2217+
if (UseBasicDynamicReplacement) {
2218+
ReplFn = IGF.Builder.CreateLoad(fnPtrAddr, getPointerAlignment());
2219+
rhs = FnAddr;
2220+
} else {
2221+
// Call swift_getFunctionReplacement to check which function to call.
2222+
auto *callRTFunc = IGF.Builder.CreateCall(getGetReplacementFn(),
2223+
{ ReplAddr, FnAddr });
2224+
callRTFunc->setDoesNotThrow();
2225+
ReplFn = callRTFunc;
2226+
rhs = llvm::ConstantExpr::getNullValue(ReplFn->getType());
2227+
}
2228+
auto *hasReplFn = IGF.Builder.CreateICmpEQ(ReplFn, rhs);
22172229

22182230
auto *replacedBB = IGF.createBasicBlock("forward_to_replaced");
22192231
auto *origEntryBB = IGF.createBasicBlock("original_entry");
@@ -2344,6 +2356,9 @@ void IRGenModule::emitOpaqueTypeDescriptorAccessor(OpaqueTypeDecl *opaque) {
23442356
void IRGenModule::emitDynamicReplacementOriginalFunctionThunk(SILFunction *f) {
23452357
assert(f->getDynamicallyReplacedFunction());
23462358

2359+
if (UseBasicDynamicReplacement)
2360+
return;
2361+
23472362
auto entity = LinkEntity::forSILFunction(f, true);
23482363

23492364
Signature signature = getSignature(f->getLoweredFunctionType());

lib/IRGen/GenDecl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ namespace irgen {
5656
}
5757
}
5858

59+
extern llvm::cl::opt<bool> UseBasicDynamicReplacement;
60+
5961
#endif

lib/IRGen/IRGenSIL.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include "GenCast.h"
6161
#include "GenClass.h"
6262
#include "GenConstant.h"
63+
#include "GenDecl.h"
6364
#include "GenEnum.h"
6465
#include "GenExistential.h"
6566
#include "GenFunc.h"
@@ -1874,6 +1875,11 @@ void IRGenSILFunction::visitDynamicFunctionRefInst(DynamicFunctionRefInst *i) {
18741875

18751876
void IRGenSILFunction::visitPreviousDynamicFunctionRefInst(
18761877
PreviousDynamicFunctionRefInst *i) {
1878+
if (UseBasicDynamicReplacement) {
1879+
IGM.unimplemented(i->getLoc().getSourceLoc(),
1880+
": calling the original implementation of a dynamic function is not "
1881+
"supported with -Xllvm -basic-dynamic-replacement");
1882+
}
18771883
visitFunctionRefBaseInst(i);
18781884
}
18791885

validation-test/stdlib/MicroStdlib/MicroStdlib.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -c -force-single-frontend-invocation -parse-as-library -parse-stdlib -module-name Swift -emit-module -emit-module-path %t/Swift.swiftmodule -o %t/Swift.o %S/Inputs/Swift.swift
2+
// RUN: %target-build-swift -c -force-single-frontend-invocation -parse-as-library -parse-stdlib -Xllvm -basic-dynamic-replacement -module-name Swift -emit-module -emit-module-path %t/Swift.swiftmodule -o %t/Swift.o %S/Inputs/Swift.swift
33
// RUN: ls %t/Swift.swiftmodule
44
// RUN: ls %t/Swift.swiftdoc
55
// RUN: ls %t/Swift.o

0 commit comments

Comments
 (0)