Skip to content

Commit cad9057

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 85a1228 commit cad9057

25 files changed

+197
-24
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
@@ -1524,6 +1524,8 @@ enum NodeType {
15241524
// Outputs: Output Chain
15251525
CLEAR_CACHE,
15261526

1527+
DEACTIVATION_SYMBOL,
1528+
15271529
/// BUILTIN_OP_END - This must be the last enum value in this list.
15281530
/// The target-specific pre-isel opcode values start here.
15291531
BUILTIN_OP_END

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,15 @@ class MachineInstr
878878
return nullptr;
879879
}
880880

881+
// FIXME: Move to Info.
882+
Value *DeactivationSymbol = nullptr;
883+
Value *getDeactivationSymbol() const {
884+
return DeactivationSymbol;
885+
}
886+
void setDeactivationSymbol(MachineFunction &MF, Value *DeactivationSymbol) {
887+
this->DeactivationSymbol = DeactivationSymbol;
888+
}
889+
881890
/// Helper to extract a CFI type hash if one has been added.
882891
uint32_t getCFIType() const {
883892
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 {
@@ -348,6 +363,8 @@ class MachineInstrBuilder {
348363
MI->setPCSections(*MF, MIMD.getPCSections());
349364
if (MIMD.getMMRAMetadata())
350365
MI->setMMRAMetadata(*MF, MIMD.getMMRAMetadata());
366+
if (MIMD.getDeactivationSymbol())
367+
MI->setDeactivationSymbol(*MF, MIMD.getDeactivationSymbol());
351368
return *this;
352369
}
353370

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
@@ -1928,6 +1928,23 @@ class GlobalAddressSDNode : public SDNode {
19281928
}
19291929
};
19301930

1931+
class DeactivationSymbolSDNode : public SDNode {
1932+
friend class SelectionDAG;
1933+
1934+
const GlobalValue *TheGlobal;
1935+
1936+
DeactivationSymbolSDNode(const GlobalValue *GV, SDVTList VTs)
1937+
: SDNode(ISD::DEACTIVATION_SYMBOL, 0, DebugLoc(), VTs),
1938+
TheGlobal(GV) {}
1939+
1940+
public:
1941+
const GlobalValue *getGlobal() const { return TheGlobal; }
1942+
1943+
static bool classof(const SDNode *N) {
1944+
return N->getOpcode() == ISD::DEACTIVATION_SYMBOL;
1945+
}
1946+
};
1947+
19311948
class FrameIndexSDNode : public SDNode {
19321949
friend class SelectionDAG;
19331950

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4612,6 +4612,7 @@ class TargetLowering : public TargetLoweringBase {
46124612
SmallVector<SDValue, 4> InVals;
46134613
const ConstantInt *CFIType = nullptr;
46144614
SDValue ConvergenceControlToken;
4615+
GlobalValue *DeactivationSymbol = nullptr;
46154616

46164617
std::optional<PtrAuthInfo> PAI;
46174618

@@ -4757,6 +4758,11 @@ class TargetLowering : public TargetLoweringBase {
47574758
return *this;
47584759
}
47594760

4761+
CallLoweringInfo &setDeactivationSymbol(GlobalValue *Sym) {
4762+
DeactivationSymbol = Sym;
4763+
return *this;
4764+
}
4765+
47604766
ArgListTy &getArgs() {
47614767
return Args;
47624768
}

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)