Skip to content

[CodeGen][ARM64EC] Add support for hybrid_patchable attribute. #92965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/include/llvm/Bitcode/LLVMBitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ enum AttributeKindCodes {
ATTR_KIND_RANGE = 92,
ATTR_KIND_SANITIZE_NUMERICAL_STABILITY = 93,
ATTR_KIND_INITIALIZES = 94,
ATTR_KIND_HYBRID_PATCHABLE = 95,
};

enum ComdatSelectionKindCodes {
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/CodeGen/AsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -892,14 +892,14 @@ class AsmPrinter : public MachineFunctionPass {
virtual void emitModuleCommandLines(Module &M);

GCMetadataPrinter *getOrCreateGCPrinter(GCStrategy &S);
virtual void emitGlobalAlias(const Module &M, const GlobalAlias &GA);
void emitGlobalIFunc(Module &M, const GlobalIFunc &GI);

private:
/// This method decides whether the specified basic block requires a label.
bool shouldEmitLabelForBasicBlock(const MachineBasicBlock &MBB) const;

protected:
virtual void emitGlobalAlias(const Module &M, const GlobalAlias &GA);
virtual bool shouldEmitWeakSwiftAsyncExtendedFramePointerFlags() const {
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/IR/Attributes.td
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def ElementType : TypeAttr<"elementtype", [ParamAttr]>;
/// symbol.
def FnRetThunkExtern : EnumAttr<"fn_ret_thunk_extern", [FnAttr]>;

/// Function has a hybrid patchable thunk.
def HybridPatchable : EnumAttr<"hybrid_patchable", [FnAttr]>;

/// Pass structure in an alloca.
def InAlloca : TypeAttr<"inalloca", [ParamAttr]>;

Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
return bitc::ATTR_KIND_HOT;
case Attribute::ElementType:
return bitc::ATTR_KIND_ELEMENTTYPE;
case Attribute::HybridPatchable:
return bitc::ATTR_KIND_HYBRID_PATCHABLE;
case Attribute::InlineHint:
return bitc::ATTR_KIND_INLINE_HINT;
case Attribute::InReg:
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2859,8 +2859,8 @@ bool AsmPrinter::emitSpecialLLVMGlobal(const GlobalVariable *GV) {
auto *Arr = cast<ConstantArray>(GV->getInitializer());
for (auto &U : Arr->operands()) {
auto *C = cast<Constant>(U);
auto *Src = cast<Function>(C->getOperand(0)->stripPointerCasts());
auto *Dst = cast<Function>(C->getOperand(1)->stripPointerCasts());
auto *Src = cast<GlobalValue>(C->getOperand(0)->stripPointerCasts());
auto *Dst = cast<GlobalValue>(C->getOperand(1)->stripPointerCasts());
int Kind = cast<ConstantInt>(C->getOperand(2))->getZExtValue();

if (Src->hasDLLImportStorageClass()) {
Expand Down
138 changes: 131 additions & 7 deletions llvm/lib/Target/AArch64/AArch64Arm64ECCallLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Mangler.h"
Expand Down Expand Up @@ -70,15 +71,21 @@ class AArch64Arm64ECCallLowering : public ModulePass {
Function *buildEntryThunk(Function *F);
void lowerCall(CallBase *CB);
Function *buildGuestExitThunk(Function *F);
bool processFunction(Function &F, SetVector<Function *> &DirectCalledFns);
Function *buildPatchableThunk(GlobalAlias *UnmangledAlias,
GlobalAlias *MangledAlias);
bool processFunction(Function &F, SetVector<GlobalValue *> &DirectCalledFns,
DenseMap<GlobalAlias *, GlobalAlias *> &FnsMap);
bool runOnModule(Module &M) override;

private:
int cfguard_module_flag = 0;
FunctionType *GuardFnType = nullptr;
PointerType *GuardFnPtrType = nullptr;
FunctionType *DispatchFnType = nullptr;
PointerType *DispatchFnPtrType = nullptr;
Constant *GuardFnCFGlobal = nullptr;
Constant *GuardFnGlobal = nullptr;
Constant *DispatchFnGlobal = nullptr;
Module *M = nullptr;

Type *PtrTy;
Expand Down Expand Up @@ -672,6 +679,66 @@ Function *AArch64Arm64ECCallLowering::buildGuestExitThunk(Function *F) {
return GuestExit;
}

Function *
AArch64Arm64ECCallLowering::buildPatchableThunk(GlobalAlias *UnmangledAlias,
GlobalAlias *MangledAlias) {
llvm::raw_null_ostream NullThunkName;
FunctionType *Arm64Ty, *X64Ty;
Function *F = cast<Function>(MangledAlias->getAliasee());
SmallVector<ThunkArgTranslation> ArgTranslations;
getThunkType(F->getFunctionType(), F->getAttributes(),
Arm64ECThunkType::GuestExit, NullThunkName, Arm64Ty, X64Ty,
ArgTranslations);
std::string ThunkName(MangledAlias->getName());
if (ThunkName[0] == '?' && ThunkName.find("@") != std::string::npos) {
ThunkName.insert(ThunkName.find("@"), "$hybpatch_thunk");
} else {
ThunkName.append("$hybpatch_thunk");
}

Function *GuestExit =
Function::Create(Arm64Ty, GlobalValue::WeakODRLinkage, 0, ThunkName, M);
GuestExit->setComdat(M->getOrInsertComdat(ThunkName));
GuestExit->setSection(".wowthk$aa");
BasicBlock *BB = BasicBlock::Create(M->getContext(), "", GuestExit);
IRBuilder<> B(BB);

// Load the global symbol as a pointer to the check function.
LoadInst *DispatchLoad = B.CreateLoad(DispatchFnPtrType, DispatchFnGlobal);

// Create new dispatch call instruction.
Function *ExitThunk =
buildExitThunk(F->getFunctionType(), F->getAttributes());
CallInst *Dispatch =
B.CreateCall(DispatchFnType, DispatchLoad,
{UnmangledAlias, ExitThunk, UnmangledAlias->getAliasee()});

// Ensure that the first arguments are passed in the correct registers.
Dispatch->setCallingConv(CallingConv::CFGuard_Check);

Value *DispatchRetVal = B.CreateBitCast(Dispatch, PtrTy);
SmallVector<Value *> Args;
for (Argument &Arg : GuestExit->args())
Args.push_back(&Arg);
CallInst *Call = B.CreateCall(Arm64Ty, DispatchRetVal, Args);
Call->setTailCallKind(llvm::CallInst::TCK_MustTail);

if (Call->getType()->isVoidTy())
B.CreateRetVoid();
else
B.CreateRet(Call);

auto SRetAttr = F->getAttributes().getParamAttr(0, Attribute::StructRet);
auto InRegAttr = F->getAttributes().getParamAttr(0, Attribute::InReg);
if (SRetAttr.isValid() && !InRegAttr.isValid()) {
GuestExit->addParamAttr(0, SRetAttr);
Call->addParamAttr(0, SRetAttr);
}

MangledAlias->setAliasee(GuestExit);
return GuestExit;
}

// Lower an indirect call with inline code.
void AArch64Arm64ECCallLowering::lowerCall(CallBase *CB) {
assert(Triple(CB->getModule()->getTargetTriple()).isOSWindows() &&
Expand Down Expand Up @@ -727,17 +794,57 @@ bool AArch64Arm64ECCallLowering::runOnModule(Module &Mod) {

GuardFnType = FunctionType::get(PtrTy, {PtrTy, PtrTy}, false);
GuardFnPtrType = PointerType::get(GuardFnType, 0);
DispatchFnType = FunctionType::get(PtrTy, {PtrTy, PtrTy, PtrTy}, false);
DispatchFnPtrType = PointerType::get(DispatchFnType, 0);
GuardFnCFGlobal =
M->getOrInsertGlobal("__os_arm64x_check_icall_cfg", GuardFnPtrType);
GuardFnGlobal =
M->getOrInsertGlobal("__os_arm64x_check_icall", GuardFnPtrType);
DispatchFnGlobal =
M->getOrInsertGlobal("__os_arm64x_dispatch_call", DispatchFnPtrType);

DenseMap<GlobalAlias *, GlobalAlias *> FnsMap;
SetVector<GlobalAlias *> PatchableFns;

SetVector<Function *> DirectCalledFns;
for (Function &F : Mod) {
if (!F.hasFnAttribute(Attribute::HybridPatchable) || F.isDeclaration() ||
F.hasLocalLinkage() || F.getName().ends_with("$hp_target"))
continue;

// Rename hybrid patchable functions and change callers to use a global
// alias instead.
if (std::optional<std::string> MangledName =
getArm64ECMangledFunctionName(F.getName().str())) {
std::string OrigName(F.getName());
F.setName(MangledName.value() + "$hp_target");

// The unmangled symbol is a weak alias to an undefined symbol with the
// "EXP+" prefix. This undefined symbol is resolved by the linker by
// creating an x86 thunk that jumps back to the actual EC target. Since we
// can't represent that in IR, we create an alias to the target instead.
// The "EXP+" symbol is set as metadata, which is then used by
// emitGlobalAlias to emit the right alias.
auto *A =
GlobalAlias::create(GlobalValue::LinkOnceODRLinkage, OrigName, &F);
F.replaceAllUsesWith(A);
F.setMetadata("arm64ec_exp_name",
MDNode::get(M->getContext(),
MDString::get(M->getContext(),
"EXP+" + MangledName.value())));
A->setAliasee(&F);

FnsMap[A] = GlobalAlias::create(GlobalValue::LinkOnceODRLinkage,
MangledName.value(), &F);
PatchableFns.insert(A);
}
}

SetVector<GlobalValue *> DirectCalledFns;
for (Function &F : Mod)
if (!F.isDeclaration() &&
F.getCallingConv() != CallingConv::ARM64EC_Thunk_Native &&
F.getCallingConv() != CallingConv::ARM64EC_Thunk_X64)
processFunction(F, DirectCalledFns);
processFunction(F, DirectCalledFns, FnsMap);

struct ThunkInfo {
Constant *Src;
Expand All @@ -755,14 +862,20 @@ bool AArch64Arm64ECCallLowering::runOnModule(Module &Mod) {
{&F, buildEntryThunk(&F), Arm64ECThunkType::Entry});
}
}
for (Function *F : DirectCalledFns) {
for (GlobalValue *O : DirectCalledFns) {
auto GA = dyn_cast<GlobalAlias>(O);
auto F = dyn_cast<Function>(GA ? GA->getAliasee() : O);
ThunkMapping.push_back(
{F, buildExitThunk(F->getFunctionType(), F->getAttributes()),
{O, buildExitThunk(F->getFunctionType(), F->getAttributes()),
Arm64ECThunkType::Exit});
if (!F->hasDLLImportStorageClass())
if (!GA && !F->hasDLLImportStorageClass())
ThunkMapping.push_back(
{buildGuestExitThunk(F), F, Arm64ECThunkType::GuestExit});
}
for (GlobalAlias *A : PatchableFns) {
Function *Thunk = buildPatchableThunk(A, FnsMap[A]);
ThunkMapping.push_back({Thunk, A, Arm64ECThunkType::GuestExit});
}

if (!ThunkMapping.empty()) {
SmallVector<Constant *> ThunkMappingArrayElems;
Expand All @@ -785,7 +898,8 @@ bool AArch64Arm64ECCallLowering::runOnModule(Module &Mod) {
}

bool AArch64Arm64ECCallLowering::processFunction(
Function &F, SetVector<Function *> &DirectCalledFns) {
Function &F, SetVector<GlobalValue *> &DirectCalledFns,
DenseMap<GlobalAlias *, GlobalAlias *> &FnsMap) {
SmallVector<CallBase *, 8> IndirectCalls;

// For ARM64EC targets, a function definition's name is mangled differently
Expand Down Expand Up @@ -837,6 +951,16 @@ bool AArch64Arm64ECCallLowering::processFunction(
continue;
}

// Use mangled global alias for direct calls to patchable functions.
if (GlobalAlias *A = dyn_cast<GlobalAlias>(CB->getCalledOperand())) {
auto I = FnsMap.find(A);
if (I != FnsMap.end()) {
CB->setCalledOperand(I->second);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels a little weird to rewrite all references to the function to aliases, then rewrite them again, but I guess it works.

DirectCalledFns.insert(I->first);
continue;
}
}

IndirectCalls.push_back(CB);
++Arm64ECCallsLowered;
}
Expand Down
27 changes: 27 additions & 0 deletions llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class AArch64AsmPrinter : public AsmPrinter {
void PrintDebugValueComment(const MachineInstr *MI, raw_ostream &OS);

void emitFunctionBodyEnd() override;
void emitGlobalAlias(const Module &M, const GlobalAlias &GA) override;

MCSymbol *GetCPISymbol(unsigned CPID) const override;
void emitEndOfAsmFile(Module &M) override;
Expand Down Expand Up @@ -1263,6 +1264,32 @@ void AArch64AsmPrinter::emitFunctionEntryLabel() {
}
}

void AArch64AsmPrinter::emitGlobalAlias(const Module &M,
const GlobalAlias &GA) {
if (auto F = dyn_cast_or_null<Function>(GA.getAliasee())) {
// Global aliases must point to a definition, but unmangled patchable
// symbols are special and need to point to an undefined symbol with "EXP+"
// prefix. Such undefined symbol is resolved by the linker by creating
// x86 thunk that jumps back to the actual EC target.
if (MDNode *Node = F->getMetadata("arm64ec_exp_name")) {
StringRef ExpStr = cast<MDString>(Node->getOperand(0))->getString();
MCSymbol *ExpSym = MMI->getContext().getOrCreateSymbol(ExpStr);
MCSymbol *Sym = MMI->getContext().getOrCreateSymbol(GA.getName());
OutStreamer->beginCOFFSymbolDef(Sym);
OutStreamer->emitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_EXTERNAL);
OutStreamer->emitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
<< COFF::SCT_COMPLEX_TYPE_SHIFT);
OutStreamer->endCOFFSymbolDef();
OutStreamer->emitSymbolAttribute(Sym, MCSA_Weak);
OutStreamer->emitAssignment(
Sym, MCSymbolRefExpr::create(ExpSym, MCSymbolRefExpr::VK_None,
MMI->getContext()));
return;
}
}
AsmPrinter::emitGlobalAlias(M, GA);
}

/// Small jump tables contain an unsigned byte or half, representing the offset
/// from the lowest-addressed possible destination to the desired basic
/// block. Since all instructions are 4-byte aligned, this is further compressed
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AArch64CallingConvention.td
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def CC_AArch64_Win64_CFGuard_Check : CallingConv<[

let Entry = 1 in
def CC_AArch64_Arm64EC_CFGuard_Check : CallingConv<[
CCIfType<[i64], CCAssignToReg<[X11, X10]>>
CCIfType<[i64], CCAssignToReg<[X11, X10, X9]>>
]>;

let Entry = 1 in
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Transforms/Utils/CodeExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
case Attribute::DisableSanitizerInstrumentation:
case Attribute::FnRetThunkExtern:
case Attribute::Hot:
case Attribute::HybridPatchable:
case Attribute::NoRecurse:
case Attribute::InlineHint:
case Attribute::MinSize:
Expand Down
Loading
Loading