1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
|
//===--- Callee.h - Information about a physical callee ---------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the Callee type, which stores all necessary
// information about a physical callee.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_CALLEE_H
#define SWIFT_IRGEN_CALLEE_H
#include <type_traits>
#include "swift/AST/IRGenOptions.h"
#include "llvm/IR/DerivedTypes.h"
#include "swift/SIL/SILType.h"
#include "IRGen.h"
#include "Signature.h"
namespace llvm {
class ConstantInt;
}
namespace swift {
namespace irgen {
class Callee;
class IRGenFunction;
class PointerAuthEntity;
class CalleeInfo {
public:
/// The unsubstituted function type being called.
CanSILFunctionType OrigFnType;
/// The substituted result type of the function being called.
CanSILFunctionType SubstFnType;
/// The archetype substitutions under which the function is being
/// called.
SubstitutionMap Substitutions;
CalleeInfo(CanSILFunctionType origFnType,
CanSILFunctionType substFnType,
SubstitutionMap substitutions)
: OrigFnType(origFnType), SubstFnType(substFnType),
Substitutions(substitutions) {
}
};
/// Information necessary for pointer authentication.
class PointerAuthInfo {
unsigned Signed : 1;
unsigned Key : 31;
llvm::Value *Discriminator;
public:
PointerAuthInfo() {
Signed = false;
}
PointerAuthInfo(unsigned key, llvm::Value *discriminator)
: Discriminator(discriminator) {
assert(discriminator->getType()->isIntegerTy() ||
discriminator->getType()->isPointerTy());
Signed = true;
Key = key;
}
static PointerAuthInfo emit(IRGenFunction &IGF,
const PointerAuthSchema &schema,
llvm::Value *storageAddress,
const PointerAuthEntity &entity);
static PointerAuthInfo emit(IRGenFunction &IGF,
clang::PointerAuthQualifier pointerAuthQual,
llvm::Value *storageAddress);
static PointerAuthInfo emit(IRGenFunction &IGF,
const PointerAuthSchema &schema,
llvm::Value *storageAddress,
llvm::ConstantInt *otherDiscriminator);
static PointerAuthInfo forFunctionPointer(IRGenModule &IGM,
CanSILFunctionType fnType);
static llvm::ConstantInt *getOtherDiscriminator(IRGenModule &IGM,
const PointerAuthSchema &schema,
const PointerAuthEntity &entity);
explicit operator bool() const {
return isSigned();
}
bool isSigned() const {
return Signed;
}
bool isConstant() const {
return (!isSigned() || isa<llvm::Constant>(Discriminator));
}
unsigned getKey() const {
assert(isSigned());
return Key;
}
bool hasCodeKey() const {
assert(isSigned());
return (getKey() == (unsigned)PointerAuthSchema::ARM8_3Key::ASIA) ||
(getKey() == (unsigned)PointerAuthSchema::ARM8_3Key::ASIB);
}
bool hasDataKey() const {
assert(isSigned());
return (getKey() == (unsigned)PointerAuthSchema::ARM8_3Key::ASDA) ||
(getKey() == (unsigned)PointerAuthSchema::ARM8_3Key::ASDB);
}
bool getCorrespondingCodeKey() const {
assert(hasDataKey());
switch (getKey()) {
case (unsigned)PointerAuthSchema::ARM8_3Key::ASDA:
return (unsigned)PointerAuthSchema::ARM8_3Key::ASIA;
case (unsigned)PointerAuthSchema::ARM8_3Key::ASDB:
return (unsigned)PointerAuthSchema::ARM8_3Key::ASIB;
}
llvm_unreachable("unhandled case");
}
unsigned getCorrespondingDataKey() const {
assert(hasCodeKey());
switch (getKey()) {
case (unsigned)PointerAuthSchema::ARM8_3Key::ASIA:
return (unsigned)PointerAuthSchema::ARM8_3Key::ASDA;
case (unsigned)PointerAuthSchema::ARM8_3Key::ASIB:
return (unsigned)PointerAuthSchema::ARM8_3Key::ASDB;
}
llvm_unreachable("unhandled case");
}
llvm::Value *getDiscriminator() const {
assert(isSigned());
return Discriminator;
}
PointerAuthInfo getCorrespondingCodeAuthInfo() const {
if (auto authInfo = *this) {
return PointerAuthInfo(authInfo.getCorrespondingCodeKey(),
authInfo.getDiscriminator());
}
return *this;
}
/// Are the auth infos obviously the same?
friend bool operator==(const PointerAuthInfo &lhs,
const PointerAuthInfo &rhs) {
if (!lhs.Signed)
return !rhs.Signed;
if (!rhs.Signed)
return false;
return (lhs.Key == rhs.Key && lhs.Discriminator == rhs.Discriminator);
}
friend bool operator!=(const PointerAuthInfo &lhs,
const PointerAuthInfo &rhs) {
return !(lhs == rhs);
}
};
class FunctionPointerKind {
public:
enum class BasicKind {
Function,
AsyncFunctionPointer
};
enum class SpecialKind {
TaskFutureWait,
TaskFutureWaitThrowing,
AsyncLetWait,
AsyncLetWaitThrowing,
AsyncLetGet,
AsyncLetGetThrowing,
AsyncLetFinish,
TaskGroupWaitNext,
TaskGroupWaitAll,
DistributedExecuteTarget,
KeyPathAccessor,
};
private:
static constexpr unsigned SpecialOffset = 2;
unsigned value;
public:
static constexpr BasicKind Function =
BasicKind::Function;
static constexpr BasicKind AsyncFunctionPointer =
BasicKind::AsyncFunctionPointer;
FunctionPointerKind(BasicKind kind)
: value(unsigned(kind)) {}
FunctionPointerKind(SpecialKind kind)
: value(unsigned(kind) + SpecialOffset) {}
FunctionPointerKind(CanSILFunctionType fnType)
: FunctionPointerKind(fnType->isAsync()
? BasicKind::AsyncFunctionPointer
: BasicKind::Function) {}
static FunctionPointerKind defaultSync() {
return BasicKind::Function;
}
static FunctionPointerKind defaultAsync() {
return BasicKind::AsyncFunctionPointer;
}
BasicKind getBasicKind() const {
return value < SpecialOffset ? BasicKind(value) : BasicKind::Function;
}
bool isAsyncFunctionPointer() const {
return value == unsigned(BasicKind::AsyncFunctionPointer);
}
bool isSpecial() const {
return value >= SpecialOffset;
}
SpecialKind getSpecialKind() const {
assert(isSpecial());
return SpecialKind(value - SpecialOffset);
}
/// Given that this is an async function, does it have a
/// statically-specified size for its async context?
///
/// Returning a non-None value is necessary for special functions
/// defined in the runtime. Without this, we'll attempt to load
/// the context size from an async FP symbol which the runtime
/// doesn't actually emit.
std::optional<Size> getStaticAsyncContextSize(IRGenModule &IGM) const;
/// Given that this is an async function, should we pass the
/// continuation function pointer and context directly to it
/// rather than building a frame?
///
/// This is a micro-optimization that is reasonable for functions
/// that are expected to return immediately in a common fast path.
/// Other functions should not do this.
bool shouldPassContinuationDirectly() const {
if (!isSpecial()) return false;
switch (getSpecialKind()) {
case SpecialKind::TaskFutureWaitThrowing:
case SpecialKind::TaskFutureWait:
case SpecialKind::AsyncLetWait:
case SpecialKind::AsyncLetWaitThrowing:
case SpecialKind::AsyncLetGet:
case SpecialKind::AsyncLetGetThrowing:
case SpecialKind::AsyncLetFinish:
case SpecialKind::TaskGroupWaitNext:
case SpecialKind::TaskGroupWaitAll:
return true;
case SpecialKind::DistributedExecuteTarget:
case SpecialKind::KeyPathAccessor:
return false;
}
llvm_unreachable("covered switch");
}
/// Should we suppress passing arguments associated with the generic
/// signature from the given function?
///
/// This is a micro-optimization for certain runtime functions that
/// are known to not need the generic arguments, probably because
/// they've already been stored elsewhere.
///
/// This may only work for async function types right now. If so,
/// that's a totally unnecessary restriction which should be easy
/// to lift, if you have a sync runtime function that would benefit
/// from this.
bool shouldSuppressPolymorphicArguments() const {
if (!isSpecial()) return false;
switch (getSpecialKind()) {
case SpecialKind::TaskFutureWaitThrowing:
case SpecialKind::TaskFutureWait:
case SpecialKind::AsyncLetWait:
case SpecialKind::AsyncLetWaitThrowing:
case SpecialKind::AsyncLetGet:
case SpecialKind::AsyncLetGetThrowing:
case SpecialKind::AsyncLetFinish:
case SpecialKind::TaskGroupWaitNext:
case SpecialKind::TaskGroupWaitAll:
// KeyPath accessor functions receive their generic arguments
// as part of indices buffer.
case SpecialKind::KeyPathAccessor:
return true;
case SpecialKind::DistributedExecuteTarget:
return false;
}
llvm_unreachable("covered switch");
}
friend bool operator==(FunctionPointerKind lhs, FunctionPointerKind rhs) {
return lhs.value == rhs.value;
}
friend bool operator!=(FunctionPointerKind lhs, FunctionPointerKind rhs) {
return !(lhs == rhs);
}
};
/// A function pointer value.
class FunctionPointer {
public:
using Kind = FunctionPointerKind;
using BasicKind = Kind::BasicKind;
using SpecialKind = Kind::SpecialKind;
private:
Kind kind;
/// The actual pointer, either to the function or to its descriptor.
llvm::Value *Value;
/// An additional value whose meaning varies by the FunctionPointer's Kind:
/// - Kind::AsyncFunctionPointer -> pointer to the corresponding function
/// if the FunctionPointer was created via
/// forDirect; nullptr otherwise.
llvm::Value *SecondaryValue;
PointerAuthInfo AuthInfo;
Signature Sig;
// If this is an await function pointer contains the signature of the await
// call (without return values).
llvm::Type *awaitSignature = nullptr;
bool useSignature = false;
// True when this function pointer points to a non-throwing foreign
// function.
bool isForeignNoThrow = false;
// True when this function pointer points to a foreign function that traps
// on exception in the always_inline thunk.
bool foreignCallCatchesExceptionInThunk = false;
explicit FunctionPointer(Kind kind, llvm::Value *value,
const Signature &signature)
: FunctionPointer(kind, value, PointerAuthInfo(), signature) {}
explicit FunctionPointer(Kind kind, llvm::Value *value,
PointerAuthInfo authInfo,
const Signature &signature)
: FunctionPointer(kind, value, nullptr, authInfo, signature){};
/// Construct a FunctionPointer for an arbitrary pointer value.
/// We may add more arguments to this; try to use the other
/// constructors/factories if possible.
explicit FunctionPointer(Kind kind, llvm::Value *value,
llvm::Value *secondaryValue,
PointerAuthInfo authInfo,
const Signature &signature,
llvm::Type *awaitSignature = nullptr)
: kind(kind), Value(value), SecondaryValue(secondaryValue),
AuthInfo(authInfo), Sig(signature), awaitSignature(awaitSignature) {
// TODO: maybe assert similarity to signature.getType()?
if (authInfo) {
if (kind == Kind::Function) {
assert(authInfo.hasCodeKey());
} else {
assert(authInfo.hasDataKey());
}
}
}
public:
FunctionPointer()
: kind(FunctionPointer::Kind::Function), Value(nullptr),
SecondaryValue(nullptr) {}
static FunctionPointer createForAsyncCall(llvm::Value *value,
PointerAuthInfo authInfo,
const Signature &signature,
llvm::Type *awaitCallSignature) {
return FunctionPointer(FunctionPointer::Kind::Function, value, nullptr,
authInfo, signature, awaitCallSignature);
}
static FunctionPointer createSigned(Kind kind, llvm::Value *value,
PointerAuthInfo authInfo,
const Signature &signature,
bool useSignature = false) {
auto res = FunctionPointer(kind, value, authInfo, signature);
res.useSignature = useSignature;
return res;
}
static FunctionPointer createSignedClosure(Kind kind, llvm::Value *value,
PointerAuthInfo authInfo,
const Signature &signature) {
auto res = FunctionPointer(kind, value, authInfo, signature);
res.useSignature = true;
return res;
}
static FunctionPointer createUnsigned(Kind kind, llvm::Value *value,
const Signature &signature,
bool useSignature = false) {
auto res = FunctionPointer(kind, value, signature);
res.useSignature = useSignature;
return res;
}
static FunctionPointer forDirect(IRGenModule &IGM, llvm::Constant *value,
llvm::Constant *secondaryValue,
CanSILFunctionType fnType);
static FunctionPointer forDirect(Kind kind, llvm::Constant *value,
llvm::Constant *secondaryValue,
const Signature &signature,
bool useSignature = false) {
auto res = FunctionPointer(kind, value, secondaryValue, PointerAuthInfo(),
signature);
res.useSignature = useSignature;
return res;
}
static FunctionPointer forExplosionValue(IRGenFunction &IGF,
llvm::Value *fnPtr,
CanSILFunctionType fnType);
/// Is this function pointer completely constant? That is, can it
/// be safely moved to a different function context?
bool isConstant() const {
return (isa<llvm::Constant>(Value) && AuthInfo.isConstant());
}
Kind getKind() const { return kind; }
BasicKind getBasicKind() const { return kind.getBasicKind(); }
/// Given that this value is known to have been constructed from a direct
/// function, Return the name of that function.
StringRef getName(IRGenModule &IGM) const;
/// Return the actual function pointer.
llvm::Value *getPointer(IRGenFunction &IGF) const;
/// Return the actual function pointer.
llvm::Value *getRawPointer() const { return Value; }
/// Assuming that the receiver is of kind AsyncFunctionPointer, returns the
/// pointer to the corresponding function if available.
llvm::Value *getRawAsyncFunction() const {
assert(kind.isAsyncFunctionPointer());
return SecondaryValue;
}
/// Given that this value is known to have been constructed from
/// a direct function, return the function pointer.
llvm::Constant *getDirectPointer() const {
return cast<llvm::Constant>(Value);
}
llvm::FunctionType *getFunctionType() const;
const PointerAuthInfo &getAuthInfo() const {
return AuthInfo;
}
const Signature &getSignature() const {
return Sig;
}
llvm::CallingConv::ID getCallingConv() const {
return Sig.getCallingConv();
}
llvm::AttributeList getAttributes() const {
return Sig.getAttributes();
}
llvm::AttributeList &getMutableAttributes() & {
return Sig.getMutableAttributes();
}
ForeignFunctionInfo getForeignInfo() const {
return Sig.getForeignInfo();
}
llvm::Value *getExplosionValue(IRGenFunction &IGF,
CanSILFunctionType fnType) const;
/// Form a FunctionPointer whose Kind is ::Function.
FunctionPointer getAsFunction(IRGenFunction &IGF) const;
std::optional<Size> getStaticAsyncContextSize(IRGenModule &IGM) const {
return kind.getStaticAsyncContextSize(IGM);
}
bool shouldPassContinuationDirectly() const {
return kind.shouldPassContinuationDirectly();
}
bool shouldSuppressPolymorphicArguments() const {
return kind.shouldSuppressPolymorphicArguments();
}
void setForeignNoThrow() { isForeignNoThrow = true; }
bool canThrowForeignException() const {
return getForeignInfo().canThrow && !isForeignNoThrow;
}
void setForeignCallCatchesExceptionInThunk() {
foreignCallCatchesExceptionInThunk = true;
}
bool doesForeignCallCatchExceptionInThunk() {
return foreignCallCatchesExceptionInThunk;
}
bool shouldUseInvoke() const {
return canThrowForeignException() && !foreignCallCatchesExceptionInThunk;
}
};
class Callee {
CalleeInfo Info;
/// The actual function pointer to invoke.
FunctionPointer Fn;
/// The first data pointer required by the function invocation.
llvm::Value *FirstData;
/// The second data pointer required by the function invocation.
llvm::Value *SecondData;
public:
Callee(const Callee &other) = delete;
Callee &operator=(const Callee &other) = delete;
Callee(Callee &&other) = default;
Callee &operator=(Callee &&other) = default;
Callee(CalleeInfo &&info, const FunctionPointer &fn,
llvm::Value *firstData = nullptr,
llvm::Value *secondData = nullptr);
SILFunctionTypeRepresentation getRepresentation() const {
return Info.OrigFnType->getRepresentation();
}
CanSILFunctionType getOrigFunctionType() const {
return Info.OrigFnType;
}
CanSILFunctionType getSubstFunctionType() const {
return Info.SubstFnType;
}
bool hasSubstitutions() const {
return Info.Substitutions.hasAnySubstitutableParams();
}
SubstitutionMap getSubstitutions() const { return Info.Substitutions; }
const FunctionPointer &getFunctionPointer() const { return Fn; }
llvm::FunctionType *getLLVMFunctionType() {
return Fn.getFunctionType();
}
llvm::AttributeList getAttributes() const {
return Fn.getAttributes();
}
llvm::AttributeList &getMutableAttributes() & {
return Fn.getMutableAttributes();
}
ForeignFunctionInfo getForeignInfo() const {
return Fn.getForeignInfo();
}
const Signature &getSignature() const {
return Fn.getSignature();
}
std::optional<Size> getStaticAsyncContextSize(IRGenModule &IGM) const {
return Fn.getStaticAsyncContextSize(IGM);
}
bool shouldPassContinuationDirectly() const {
return Fn.shouldPassContinuationDirectly();
}
bool shouldSuppressPolymorphicArguments() const {
return Fn.shouldSuppressPolymorphicArguments();
}
/// If this callee has a value for the Swift context slot, return
/// it; otherwise return non-null.
llvm::Value *getSwiftContext() const;
/// Given that this callee is a block, return the block pointer.
llvm::Value *getBlockObject() const;
/// Given that this callee is a C++ method, return the self argument.
llvm::Value *getCXXMethodSelf() const;
/// Given that this callee is an ObjC method, return the receiver
/// argument. This might not be 'self' anymore.
llvm::Value *getObjCMethodReceiver() const;
/// Given that this callee is an ObjC method, return the receiver
/// argument. This might not be 'self' anymore.
llvm::Value *getObjCMethodSelector() const;
bool isDirectObjCMethod() const;
};
FunctionPointer::Kind classifyFunctionPointerKind(SILFunction *fn);
} // end namespace irgen
} // end namespace swift
#endif
|