Skip to content

Commit be7ef9a

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 11f7682 commit be7ef9a

26 files changed

+222
-42
lines changed

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

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

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

167169
/// 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
@@ -56,6 +56,7 @@ struct MachineIRBuilderState {
5656
MDNode *PCSections = nullptr;
5757
/// MMRA Metadata to be set on any instruction we create.
5858
MDNode *MMRA = nullptr;
59+
Value *DS = nullptr;
5960

6061
/// \name Fields describing the insertion point.
6162
/// @{
@@ -369,6 +370,7 @@ class LLVM_ABI MachineIRBuilder {
369370
State.II = MI.getIterator();
370371
setPCSections(MI.getPCSections());
371372
setMMRAMetadata(MI.getMMRAMetadata());
373+
setDeactivationSymbol(MI.getDeactivationSymbol());
372374
}
373375
/// @}
374376

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

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

llvm/include/llvm/CodeGen/ISDOpcodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,8 @@ enum NodeType {
15631563
// Outputs: Output Chain
15641564
CLEAR_CACHE,
15651565

1566+
DEACTIVATION_SYMBOL,
1567+
15661568
/// BUILTIN_OP_END - This must be the last enum value in this list.
15671569
/// The target-specific pre-isel opcode values start here.
15681570
BUILTIN_OP_END

llvm/include/llvm/CodeGen/MachineInstr.h

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

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

llvm/include/llvm/CodeGen/MachineInstrBuilder.h

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

7272
/// Set of metadata that should be preserved when using BuildMI(). This provides
73-
/// a more convenient way of preserving DebugLoc, PCSections and MMRA.
73+
/// a more convenient way of preserving certain data from the original
74+
/// instruction.
7475
class MIMetadata {
7576
public:
7677
MIMetadata() = default;
77-
MIMetadata(DebugLoc DL, MDNode *PCSections = nullptr, MDNode *MMRA = nullptr)
78-
: DL(std::move(DL)), PCSections(PCSections), MMRA(MMRA) {}
78+
MIMetadata(DebugLoc DL, MDNode *PCSections = nullptr, MDNode *MMRA = nullptr,
79+
Value *DeactivationSymbol = nullptr)
80+
: DL(std::move(DL)), PCSections(PCSections), MMRA(MMRA),
81+
DeactivationSymbol(DeactivationSymbol) {}
7982
MIMetadata(const DILocation *DI, MDNode *PCSections = nullptr,
8083
MDNode *MMRA = nullptr)
8184
: DL(DI), PCSections(PCSections), MMRA(MMRA) {}
8285
explicit MIMetadata(const Instruction &From)
8386
: DL(From.getDebugLoc()),
84-
PCSections(From.getMetadata(LLVMContext::MD_pcsections)) {}
87+
PCSections(From.getMetadata(LLVMContext::MD_pcsections)),
88+
DeactivationSymbol(getDeactivationSymbol(&From)) {}
8589
explicit MIMetadata(const MachineInstr &From)
86-
: DL(From.getDebugLoc()), PCSections(From.getPCSections()) {}
90+
: DL(From.getDebugLoc()), PCSections(From.getPCSections()),
91+
DeactivationSymbol(From.getDeactivationSymbol()) {}
8792

8893
const DebugLoc &getDL() const { return DL; }
8994
MDNode *getPCSections() const { return PCSections; }
9095
MDNode *getMMRAMetadata() const { return MMRA; }
96+
Value *getDeactivationSymbol() const { return DeactivationSymbol; }
9197

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

98113
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
@@ -763,6 +763,7 @@ class SelectionDAG {
763763
int64_t offset = 0, unsigned TargetFlags = 0) {
764764
return getGlobalAddress(GV, DL, VT, offset, true, TargetFlags);
765765
}
766+
LLVM_ABI SDValue getDeactivationSymbol(const GlobalValue *GV);
766767
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false);
767768
SDValue getTargetFrameIndex(int FI, EVT VT) {
768769
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
@@ -150,6 +150,7 @@ class SelectionDAGISel {
150150
OPC_RecordChild7,
151151
OPC_RecordMemRef,
152152
OPC_CaptureGlueInput,
153+
OPC_CaptureDeactivationSymbol,
153154
OPC_MoveChild,
154155
OPC_MoveChild0,
155156
OPC_MoveChild1,

llvm/include/llvm/CodeGen/SelectionDAGNodes.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,23 @@ class GlobalAddressSDNode : public SDNode {
19801980
}
19811981
};
19821982

1983+
class DeactivationSymbolSDNode : public SDNode {
1984+
friend class SelectionDAG;
1985+
1986+
const GlobalValue *TheGlobal;
1987+
1988+
DeactivationSymbolSDNode(const GlobalValue *GV, SDVTList VTs)
1989+
: SDNode(ISD::DEACTIVATION_SYMBOL, 0, DebugLoc(), VTs),
1990+
TheGlobal(GV) {}
1991+
1992+
public:
1993+
const GlobalValue *getGlobal() const { return TheGlobal; }
1994+
1995+
static bool classof(const SDNode *N) {
1996+
return N->getOpcode() == ISD::DEACTIVATION_SYMBOL;
1997+
}
1998+
};
1999+
19832000
class FrameIndexSDNode : public SDNode {
19842001
friend class SelectionDAG;
19852002

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4716,6 +4716,7 @@ class LLVM_ABI TargetLowering : public TargetLoweringBase {
47164716
SmallVector<SDValue, 4> InVals;
47174717
const ConstantInt *CFIType = nullptr;
47184718
SDValue ConvergenceControlToken;
4719+
GlobalValue *DeactivationSymbol = nullptr;
47194720

47204721
std::optional<PtrAuthInfo> PAI;
47214722

@@ -4861,6 +4862,11 @@ class LLVM_ABI TargetLowering : public TargetLoweringBase {
48614862
return *this;
48624863
}
48634864

4865+
CallLoweringInfo &setDeactivationSymbol(GlobalValue *Sym) {
4866+
DeactivationSymbol = Sym;
4867+
return *this;
4868+
}
4869+
48644870
ArgListTy &getArgs() {
48654871
return Args;
48664872
}

llvm/include/llvm/IR/LLVMContext.h

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

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

0 commit comments

Comments
 (0)