Skip to content

Commit 32a8bd5

Browse files
committed
[CodeGen][NPM] Port XRayInstrumentation to NPM
1 parent 0ba6ca6 commit 32a8bd5

10 files changed

+110
-22
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===- llvm/CodeGen/XRayInstrumentation.h --------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CODEGEN_XRAYINSTRUMENTATION_H
10+
#define LLVM_CODEGEN_XRAYINSTRUMENTATION_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
16+
class XRayInstrumentationPass : public PassInfoMixin<XRayInstrumentationPass> {
17+
public:
18+
PreservedAnalyses run(MachineFunction &MF,
19+
MachineFunctionAnalysisManager &MFAM);
20+
};
21+
22+
} // namespace llvm
23+
24+
#endif // LLVM_CODEGEN_XRAYINSTRUMENTATION_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ void initializeVirtRegRewriterPass(PassRegistry &);
320320
void initializeWasmEHPreparePass(PassRegistry &);
321321
void initializeWinEHPreparePass(PassRegistry &);
322322
void initializeWriteBitcodePassPass(PassRegistry &);
323-
void initializeXRayInstrumentationPass(PassRegistry &);
323+
void initializeXRayInstrumentationLegacyPass(PassRegistry &);
324324

325325
} // end namespace llvm
326326

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#include "llvm/CodeGen/UnreachableBlockElim.h"
8686
#include "llvm/CodeGen/WasmEHPrepare.h"
8787
#include "llvm/CodeGen/WinEHPrepare.h"
88+
#include "llvm/CodeGen/XRayInstrumentation.h"
8889
#include "llvm/IR/PassManager.h"
8990
#include "llvm/IR/Verifier.h"
9091
#include "llvm/IRPrinter/IRPrintingPasses.h"

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
188188
MACHINE_FUNCTION_PASS("two-address-instruction", TwoAddressInstructionPass())
189189
MACHINE_FUNCTION_PASS("verify", MachineVerifierPass())
190190
MACHINE_FUNCTION_PASS("verify<machine-trace-metrics>", MachineTraceMetricsVerifierPass())
191+
MACHINE_FUNCTION_PASS("xray-instrumentation", XRayInstrumentationPass())
191192
#undef MACHINE_FUNCTION_PASS
192193

193194
#ifndef MACHINE_FUNCTION_PASS_WITH_PARAMS
@@ -296,5 +297,4 @@ DUMMY_MACHINE_FUNCTION_PASS("stack-frame-layout", StackFrameLayoutAnalysisPass)
296297
DUMMY_MACHINE_FUNCTION_PASS("stackmap-liveness", StackMapLivenessPass)
297298
DUMMY_MACHINE_FUNCTION_PASS("unpack-mi-bundles", UnpackMachineBundlesPass)
298299
DUMMY_MACHINE_FUNCTION_PASS("virtregrewriter", VirtRegRewriterPass)
299-
DUMMY_MACHINE_FUNCTION_PASS("xray-instrumentation", XRayInstrumentationPass)
300300
#undef DUMMY_MACHINE_FUNCTION_PASS

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,5 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
144144
initializeVirtRegRewriterPass(Registry);
145145
initializeWasmEHPreparePass(Registry);
146146
initializeWinEHPreparePass(Registry);
147-
initializeXRayInstrumentationPass(Registry);
147+
initializeXRayInstrumentationLegacyPass(Registry);
148148
}

llvm/lib/CodeGen/FEntryInserter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bool FEntryInserter::run(MachineFunction &MF) {
5959
return true;
6060
}
6161

62-
char FEntryInserter::ID = 0;
63-
char &llvm::FEntryInserterID = FEntryInserter::ID;
64-
INITIALIZE_PASS(FEntryInserter, "fentry-insert", "Insert fentry calls", false,
65-
false)
62+
char FEntryInserterLegacy::ID = 0;
63+
char &llvm::FEntryInserterID = FEntryInserterLegacy::ID;
64+
INITIALIZE_PASS(FEntryInserterLegacy, "fentry-insert", "Insert fentry calls",
65+
false, false)

llvm/lib/CodeGen/XRayInstrumentation.cpp

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
//
1414
//===---------------------------------------------------------------------===//
1515

16+
#include "llvm/CodeGen/XRayInstrumentation.h"
1617
#include "llvm/ADT/STLExtras.h"
1718
#include "llvm/ADT/SmallVector.h"
1819
#include "llvm/CodeGen/MachineBasicBlock.h"
1920
#include "llvm/CodeGen/MachineDominators.h"
2021
#include "llvm/CodeGen/MachineFunction.h"
22+
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
2123
#include "llvm/CodeGen/MachineFunctionPass.h"
2224
#include "llvm/CodeGen/MachineInstrBuilder.h"
2325
#include "llvm/CodeGen/MachineLoopInfo.h"
26+
#include "llvm/CodeGen/MachinePassManager.h"
2427
#include "llvm/CodeGen/TargetInstrInfo.h"
2528
#include "llvm/CodeGen/TargetSubtargetInfo.h"
2629
#include "llvm/IR/Attributes.h"
@@ -44,11 +47,11 @@ struct InstrumentationOptions {
4447
bool HandleAllReturns;
4548
};
4649

47-
struct XRayInstrumentation : public MachineFunctionPass {
50+
struct XRayInstrumentationLegacy : public MachineFunctionPass {
4851
static char ID;
4952

50-
XRayInstrumentation() : MachineFunctionPass(ID) {
51-
initializeXRayInstrumentationPass(*PassRegistry::getPassRegistry());
53+
XRayInstrumentationLegacy() : MachineFunctionPass(ID) {
54+
initializeXRayInstrumentationLegacyPass(*PassRegistry::getPassRegistry());
5255
}
5356

5457
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -59,6 +62,27 @@ struct XRayInstrumentation : public MachineFunctionPass {
5962
}
6063

6164
bool runOnMachineFunction(MachineFunction &MF) override;
65+
};
66+
67+
struct XRayInstrumentation {
68+
XRayInstrumentation(MachineDominatorTree *MDT, MachineLoopInfo *MLI)
69+
: MDT(MDT), MLI(MLI) {}
70+
71+
bool run(MachineFunction &MF);
72+
73+
// Methods for use in the NPM and legacy passes, can be removed once migration
74+
// is complete.
75+
static bool alwaysInstrument(Function &F) {
76+
auto InstrAttr = F.getFnAttribute("function-instrument");
77+
return InstrAttr.isStringAttribute() &&
78+
InstrAttr.getValueAsString() == "xray-always";
79+
}
80+
81+
static bool needMDTAndMLIAnalyses(Function &F) {
82+
auto IgnoreLoopsAttr = F.getFnAttribute("xray-ignore-loops");
83+
auto AlwaysInstrument = XRayInstrumentation::alwaysInstrument(F);
84+
return !AlwaysInstrument && !IgnoreLoopsAttr.isValid();
85+
}
6286

6387
private:
6488
// Replace the original RET instruction with the exit sled code ("patchable
@@ -82,6 +106,9 @@ struct XRayInstrumentation : public MachineFunctionPass {
82106
void prependRetWithPatchableExit(MachineFunction &MF,
83107
const TargetInstrInfo *TII,
84108
InstrumentationOptions);
109+
110+
MachineDominatorTree *MDT;
111+
MachineLoopInfo *MLI;
85112
};
86113

87114
} // end anonymous namespace
@@ -143,11 +170,43 @@ void XRayInstrumentation::prependRetWithPatchableExit(
143170
}
144171
}
145172

146-
bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
173+
PreservedAnalyses
174+
XRayInstrumentationPass::run(MachineFunction &MF,
175+
MachineFunctionAnalysisManager &MFAM) {
176+
MachineDominatorTree *MDT = nullptr;
177+
MachineLoopInfo *MLI = nullptr;
178+
179+
if (XRayInstrumentation::needMDTAndMLIAnalyses(MF.getFunction())) {
180+
MDT = MFAM.getCachedResult<MachineDominatorTreeAnalysis>(MF);
181+
MLI = MFAM.getCachedResult<MachineLoopAnalysis>(MF);
182+
}
183+
184+
if (!XRayInstrumentation(MDT, MLI).run(MF))
185+
return PreservedAnalyses::all();
186+
187+
return getMachineFunctionPassPreservedAnalyses()
188+
.preserve<MachineDominatorTreeAnalysis>()
189+
.preserve<MachineLoopAnalysis>()
190+
.preserveSet<CFGAnalyses>();
191+
}
192+
193+
bool XRayInstrumentationLegacy::runOnMachineFunction(MachineFunction &MF) {
194+
MachineDominatorTree *MDT = nullptr;
195+
MachineLoopInfo *MLI = nullptr;
196+
if (XRayInstrumentation::needMDTAndMLIAnalyses(MF.getFunction())) {
197+
auto *MDTWrapper =
198+
getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
199+
MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
200+
auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
201+
MLI = MLIWrapper ? &MLIWrapper->getLI() : nullptr;
202+
}
203+
return XRayInstrumentation(MDT, MLI).run(MF);
204+
}
205+
206+
bool XRayInstrumentation::run(MachineFunction &MF) {
147207
auto &F = MF.getFunction();
148208
auto InstrAttr = F.getFnAttribute("function-instrument");
149-
bool AlwaysInstrument = InstrAttr.isStringAttribute() &&
150-
InstrAttr.getValueAsString() == "xray-always";
209+
bool AlwaysInstrument = alwaysInstrument(F);
151210
bool NeverInstrument = InstrAttr.isStringAttribute() &&
152211
InstrAttr.getValueAsString() == "xray-never";
153212
if (NeverInstrument && !AlwaysInstrument)
@@ -171,18 +230,19 @@ bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
171230

172231
if (!IgnoreLoops) {
173232
// Get MachineDominatorTree or compute it on the fly if it's unavailable
174-
auto *MDTWrapper =
175-
getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
176-
auto *MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
233+
// auto *MDTWrapper =
234+
// getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
235+
// auto *MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
177236
MachineDominatorTree ComputedMDT;
178237
if (!MDT) {
179238
ComputedMDT.recalculate(MF);
180239
MDT = &ComputedMDT;
181240
}
182241

183242
// Get MachineLoopInfo or compute it on the fly if it's unavailable
184-
auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
185-
auto *MLI = MLIWrapper ? &MLIWrapper->getLI() : nullptr;
243+
// auto *MLIWrapper =
244+
// getAnalysisIfAvailable<MachineLoopInfoWrapperPass>(); auto *MLI =
245+
// MLIWrapper ? &MLIWrapper->getLI() : nullptr;
186246
MachineLoopInfo ComputedMLI;
187247
if (!MLI) {
188248
ComputedMLI.analyze(*MDT);
@@ -272,10 +332,10 @@ bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
272332
return true;
273333
}
274334

275-
char XRayInstrumentation::ID = 0;
276-
char &llvm::XRayInstrumentationID = XRayInstrumentation::ID;
277-
INITIALIZE_PASS_BEGIN(XRayInstrumentation, "xray-instrumentation",
335+
char XRayInstrumentationLegacy::ID = 0;
336+
char &llvm::XRayInstrumentationID = XRayInstrumentationLegacy::ID;
337+
INITIALIZE_PASS_BEGIN(XRayInstrumentationLegacy, "xray-instrumentation",
278338
"Insert XRay ops", false, false)
279339
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
280-
INITIALIZE_PASS_END(XRayInstrumentation, "xray-instrumentation",
340+
INITIALIZE_PASS_END(XRayInstrumentationLegacy, "xray-instrumentation",
281341
"Insert XRay ops", false, false)

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
#include "llvm/CodeGen/VirtRegMap.h"
160160
#include "llvm/CodeGen/WasmEHPrepare.h"
161161
#include "llvm/CodeGen/WinEHPrepare.h"
162+
#include "llvm/CodeGen/XRayInstrumentation.h"
162163
#include "llvm/IR/DebugInfo.h"
163164
#include "llvm/IR/Dominators.h"
164165
#include "llvm/IR/PassManager.h"

llvm/test/CodeGen/X86/xray-empty-firstmbb.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -run-pass=xray-instrumentation -mtriple=x86_64-unknown-linux-gnu -o - %s | FileCheck %s
2+
# RUN: llc -passes=xray-instrumentation -mtriple=x86_64-unknown-linux-gnu -o - %s | FileCheck %s
23
#
34
# Make sure we can handle empty first basic blocks.
45

llvm/test/CodeGen/X86/xray-multiplerets-in-blocks.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -run-pass=xray-instrumentation -mtriple=x86_64-unknown-linux-gnu -o - %s | FileCheck %s
2+
# RUN: llc -passes=xray-instrumentation -mtriple=x86_64-unknown-linux-gnu -o - %s | FileCheck %s
23
#
34
# Make sure we can handle multiple ret instructions in a single basic block for
45
# XRay.

0 commit comments

Comments
 (0)