Skip to content

Commit f87901d

Browse files
committed
Add IR and codegen support for deactivation symbols.
Deactivation symbols are a mechanism for allowing object files to disable specific instructions in other object files at link time. The initial use case is for pointer field protection. For more information, see the RFC: https://discourse.llvm.org/t/rfc-deactivation-symbols/85556 TODO: - Add tests. Pull Request: llvm#133536
1 parent 5c814f0 commit f87901d

26 files changed

+232
-42
lines changed

llvm/include/llvm/CodeGen/GlobalISel/CallLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ class CallLowering {
161161

162162
/// True if this call results in convergent operations.
163163
bool IsConvergent = true;
164+
165+
GlobalValue *DeactivationSymbol = nullptr;
164166
};
165167

166168
/// Argument handling is mostly uniform between the four places that

llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct MachineIRBuilderState {
5555
MDNode *PCSections = nullptr;
5656
/// MMRA Metadata to be set on any instruction we create.
5757
MDNode *MMRA = nullptr;
58+
Value *DS = nullptr;
5859

5960
/// \name Fields describing the insertion point.
6061
/// @{
@@ -368,6 +369,7 @@ class MachineIRBuilder {
368369
State.II = MI.getIterator();
369370
setPCSections(MI.getPCSections());
370371
setMMRAMetadata(MI.getMMRAMetadata());
372+
setDeactivationSymbol(MI.getDeactivationSymbol());
371373
}
372374
/// @}
373375

@@ -404,6 +406,9 @@ class MachineIRBuilder {
404406
/// Set the PC sections metadata to \p MD for all the next build instructions.
405407
void setMMRAMetadata(MDNode *MMRA) { State.MMRA = MMRA; }
406408

409+
Value *getDeactivationSymbol() { return State.DS; }
410+
void setDeactivationSymbol(Value *DS) { State.DS = DS; }
411+
407412
/// Get the current instruction's MMRA metadata.
408413
MDNode *getMMRAMetadata() { return State.MMRA; }
409414

llvm/include/llvm/CodeGen/ISDOpcodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,8 @@ enum NodeType {
15471547
// Outputs: Output Chain
15481548
CLEAR_CACHE,
15491549

1550+
DEACTIVATION_SYMBOL,
1551+
15501552
/// BUILTIN_OP_END - This must be the last enum value in this list.
15511553
/// The target-specific pre-isel opcode values start here.
15521554
BUILTIN_OP_END

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,15 @@ class MachineInstr
864864
return nullptr;
865865
}
866866

867+
// FIXME: Move to Info.
868+
Value *DeactivationSymbol = nullptr;
869+
Value *getDeactivationSymbol() const {
870+
return DeactivationSymbol;
871+
}
872+
void setDeactivationSymbol(MachineFunction &MF, Value *DeactivationSymbol) {
873+
this->DeactivationSymbol = DeactivationSymbol;
874+
}
875+
867876
/// Helper to extract a CFI type hash if one has been added.
868877
uint32_t getCFIType() const {
869878
if (!Info)

llvm/include/llvm/CodeGen/MachineInstrBuilder.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,44 @@ enum {
6969
} // end namespace RegState
7070

7171
/// Set of metadata that should be preserved when using BuildMI(). This provides
72-
/// a more convenient way of preserving DebugLoc, PCSections and MMRA.
72+
/// a more convenient way of preserving certain data from the original
73+
/// instruction.
7374
class MIMetadata {
7475
public:
7576
MIMetadata() = default;
76-
MIMetadata(DebugLoc DL, MDNode *PCSections = nullptr, MDNode *MMRA = nullptr)
77-
: DL(std::move(DL)), PCSections(PCSections), MMRA(MMRA) {}
77+
MIMetadata(DebugLoc DL, MDNode *PCSections = nullptr, MDNode *MMRA = nullptr,
78+
Value *DeactivationSymbol = nullptr)
79+
: DL(std::move(DL)), PCSections(PCSections), MMRA(MMRA),
80+
DeactivationSymbol(DeactivationSymbol) {}
7881
MIMetadata(const DILocation *DI, MDNode *PCSections = nullptr,
7982
MDNode *MMRA = nullptr)
8083
: DL(DI), PCSections(PCSections), MMRA(MMRA) {}
8184
explicit MIMetadata(const Instruction &From)
8285
: DL(From.getDebugLoc()),
83-
PCSections(From.getMetadata(LLVMContext::MD_pcsections)) {}
86+
PCSections(From.getMetadata(LLVMContext::MD_pcsections)),
87+
DeactivationSymbol(getDeactivationSymbol(&From)) {}
8488
explicit MIMetadata(const MachineInstr &From)
85-
: DL(From.getDebugLoc()), PCSections(From.getPCSections()) {}
89+
: DL(From.getDebugLoc()), PCSections(From.getPCSections()),
90+
DeactivationSymbol(From.getDeactivationSymbol()) {}
8691

8792
const DebugLoc &getDL() const { return DL; }
8893
MDNode *getPCSections() const { return PCSections; }
8994
MDNode *getMMRAMetadata() const { return MMRA; }
95+
Value *getDeactivationSymbol() const { return DeactivationSymbol; }
9096

9197
private:
9298
DebugLoc DL;
9399
MDNode *PCSections = nullptr;
94100
MDNode *MMRA = nullptr;
101+
Value *DeactivationSymbol = nullptr;
102+
103+
static inline Value *getDeactivationSymbol(const Instruction *I) {
104+
if (auto *CB = dyn_cast<CallBase>(I))
105+
if (auto Bundle =
106+
CB->getOperandBundle(llvm::LLVMContext::OB_deactivation_symbol))
107+
return Bundle->Inputs[0].get();
108+
return nullptr;
109+
}
95110
};
96111

97112
class MachineInstrBuilder {
@@ -347,6 +362,8 @@ class MachineInstrBuilder {
347362
MI->setPCSections(*MF, MIMD.getPCSections());
348363
if (MIMD.getMMRAMetadata())
349364
MI->setMMRAMetadata(*MF, MIMD.getMMRAMetadata());
365+
if (MIMD.getDeactivationSymbol())
366+
MI->setDeactivationSymbol(*MF, MIMD.getDeactivationSymbol());
350367
return *this;
351368
}
352369

llvm/include/llvm/CodeGen/SelectionDAG.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ class SelectionDAG {
752752
int64_t offset = 0, unsigned TargetFlags = 0) {
753753
return getGlobalAddress(GV, DL, VT, offset, true, TargetFlags);
754754
}
755+
SDValue getDeactivationSymbol(const GlobalValue *GV);
755756
SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false);
756757
SDValue getTargetFrameIndex(int FI, EVT VT) {
757758
return getFrameIndex(FI, VT, true);

llvm/include/llvm/CodeGen/SelectionDAGISel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class SelectionDAGISel {
152152
OPC_RecordChild7,
153153
OPC_RecordMemRef,
154154
OPC_CaptureGlueInput,
155+
OPC_CaptureDeactivationSymbol,
155156
OPC_MoveChild,
156157
OPC_MoveChild0,
157158
OPC_MoveChild1,

llvm/include/llvm/CodeGen/SelectionDAGNodes.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,6 +1932,23 @@ class GlobalAddressSDNode : public SDNode {
19321932
}
19331933
};
19341934

1935+
class DeactivationSymbolSDNode : public SDNode {
1936+
friend class SelectionDAG;
1937+
1938+
const GlobalValue *TheGlobal;
1939+
1940+
DeactivationSymbolSDNode(const GlobalValue *GV, SDVTList VTs)
1941+
: SDNode(ISD::DEACTIVATION_SYMBOL, 0, DebugLoc(), VTs),
1942+
TheGlobal(GV) {}
1943+
1944+
public:
1945+
const GlobalValue *getGlobal() const { return TheGlobal; }
1946+
1947+
static bool classof(const SDNode *N) {
1948+
return N->getOpcode() == ISD::DEACTIVATION_SYMBOL;
1949+
}
1950+
};
1951+
19351952
class FrameIndexSDNode : public SDNode {
19361953
friend class SelectionDAG;
19371954

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4690,6 +4690,7 @@ class TargetLowering : public TargetLoweringBase {
46904690
SmallVector<SDValue, 4> InVals;
46914691
const ConstantInt *CFIType = nullptr;
46924692
SDValue ConvergenceControlToken;
4693+
GlobalValue *DeactivationSymbol = nullptr;
46934694

46944695
std::optional<PtrAuthInfo> PAI;
46954696

@@ -4835,6 +4836,11 @@ class TargetLowering : public TargetLoweringBase {
48354836
return *this;
48364837
}
48374838

4839+
CallLoweringInfo &setDeactivationSymbol(GlobalValue *Sym) {
4840+
DeactivationSymbol = Sym;
4841+
return *this;
4842+
}
4843+
48384844
ArgListTy &getArgs() {
48394845
return Args;
48404846
}

llvm/include/llvm/IR/LLVMContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class LLVMContext {
9696
OB_ptrauth = 7, // "ptrauth"
9797
OB_kcfi = 8, // "kcfi"
9898
OB_convergencectrl = 9, // "convergencectrl"
99+
OB_deactivation_symbol = 10, // "deactivation-symbol"
99100
};
100101

101102
/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.

0 commit comments

Comments
 (0)