Skip to content

Commit 5c4ef6f

Browse files
committed
Revert "[MLGO][IR2Vec] Integrating IR2Vec with MLInliner (#143479)"
This reverts commit af2c06e.
1 parent 3782eb6 commit 5c4ef6f

File tree

8 files changed

+24
-338
lines changed

8 files changed

+24
-338
lines changed

llvm/include/llvm/Analysis/FunctionPropertiesAnalysis.h

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#define LLVM_ANALYSIS_FUNCTIONPROPERTIESANALYSIS_H
1616

1717
#include "llvm/ADT/DenseSet.h"
18-
#include "llvm/Analysis/IR2Vec.h"
1918
#include "llvm/IR/Dominators.h"
2019
#include "llvm/IR/PassManager.h"
2120
#include "llvm/Support/Compiler.h"
@@ -33,19 +32,17 @@ class FunctionPropertiesInfo {
3332
void updateAggregateStats(const Function &F, const LoopInfo &LI);
3433
void reIncludeBB(const BasicBlock &BB);
3534

36-
ir2vec::Embedding FunctionEmbedding = ir2vec::Embedding(0.0);
37-
std::optional<ir2vec::Vocab> IR2VecVocab;
38-
3935
public:
4036
LLVM_ABI static FunctionPropertiesInfo
4137
getFunctionPropertiesInfo(const Function &F, const DominatorTree &DT,
42-
const LoopInfo &LI,
43-
const IR2VecVocabResult *VocabResult);
38+
const LoopInfo &LI);
4439

4540
LLVM_ABI static FunctionPropertiesInfo
4641
getFunctionPropertiesInfo(Function &F, FunctionAnalysisManager &FAM);
4742

48-
bool operator==(const FunctionPropertiesInfo &FPI) const;
43+
bool operator==(const FunctionPropertiesInfo &FPI) const {
44+
return std::memcmp(this, &FPI, sizeof(FunctionPropertiesInfo)) == 0;
45+
}
4946

5047
bool operator!=(const FunctionPropertiesInfo &FPI) const {
5148
return !(*this == FPI);
@@ -140,19 +137,6 @@ class FunctionPropertiesInfo {
140137
int64_t CallReturnsVectorPointerCount = 0;
141138
int64_t CallWithManyArgumentsCount = 0;
142139
int64_t CallWithPointerArgumentCount = 0;
143-
144-
const ir2vec::Embedding &getFunctionEmbedding() const {
145-
return FunctionEmbedding;
146-
}
147-
148-
const std::optional<ir2vec::Vocab> &getIR2VecVocab() const {
149-
return IR2VecVocab;
150-
}
151-
152-
// Helper intended to be useful for unittests
153-
void setFunctionEmbeddingForTest(const ir2vec::Embedding &Embedding) {
154-
FunctionEmbedding = Embedding;
155-
}
156140
};
157141

158142
// Analysis pass
@@ -208,7 +192,7 @@ class FunctionPropertiesUpdater {
208192

209193
DominatorTree &getUpdatedDominatorTree(FunctionAnalysisManager &FAM) const;
210194

211-
DenseSet<const BasicBlock *> Successors, CallUsers;
195+
DenseSet<const BasicBlock *> Successors;
212196

213197
// Edges we might potentially need to remove from the dominator tree.
214198
SmallVector<DominatorTree::UpdateType, 2> DomTreeUpdates;

llvm/include/llvm/Analysis/InlineAdvisor.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,6 @@ class InlineAdvisorAnalysis : public AnalysisInfoMixin<InlineAdvisorAnalysis> {
331331
};
332332

333333
Result run(Module &M, ModuleAnalysisManager &MAM) { return Result(M, MAM); }
334-
335-
private:
336-
static bool initializeIR2VecVocabIfRequested(Module &M,
337-
ModuleAnalysisManager &MAM);
338334
};
339335

340336
/// Printer pass for the InlineAdvisorAnalysis results.

llvm/include/llvm/Analysis/InlineModelFeatureMaps.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,6 @@ enum class FeatureIndex : size_t {
142142
INLINE_FEATURE_ITERATOR(POPULATE_INDICES)
143143
#undef POPULATE_INDICES
144144

145-
// IR2Vec embeddings
146-
// Dimensions of embeddings are not known in the compile time (until vocab is
147-
// read). Hence macros cannot be used here.
148-
callee_embedding,
149-
caller_embedding,
150-
151145
NumberOfFeatures
152146
};
153147
// clang-format on
@@ -160,7 +154,7 @@ inlineCostFeatureToMlFeature(InlineCostFeatureIndex Feature) {
160154
constexpr size_t NumberOfFeatures =
161155
static_cast<size_t>(FeatureIndex::NumberOfFeatures);
162156

163-
LLVM_ABI extern std::vector<TensorSpec> FeatureMap;
157+
LLVM_ABI extern const std::vector<TensorSpec> FeatureMap;
164158

165159
LLVM_ABI extern const char *const DecisionName;
166160
LLVM_ABI extern const TensorSpec InlineDecisionSpec;

llvm/include/llvm/Analysis/MLInlineAdvisor.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ class MLInlineAdvisor : public InlineAdvisor {
8282
int64_t NodeCount = 0;
8383
int64_t EdgeCount = 0;
8484
int64_t EdgesOfLastSeenNodes = 0;
85-
const bool UseIR2Vec;
8685

8786
std::map<const LazyCallGraph::Node *, unsigned> FunctionLevels;
8887
const int32_t InitialIRSize = 0;

llvm/lib/Analysis/FunctionPropertiesAnalysis.cpp

Lines changed: 3 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -199,29 +199,6 @@ void FunctionPropertiesInfo::updateForBB(const BasicBlock &BB,
199199
#undef CHECK_OPERAND
200200
}
201201
}
202-
203-
if (IR2VecVocab) {
204-
// We instantiate the IR2Vec embedder each time, as having an unique
205-
// pointer to the embedder as member of the class would make it
206-
// non-copyable. Instantiating the embedder in itself is not costly.
207-
auto EmbOrErr = ir2vec::Embedder::create(IR2VecKind::Symbolic,
208-
*BB.getParent(), *IR2VecVocab);
209-
if (Error Err = EmbOrErr.takeError()) {
210-
handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
211-
BB.getContext().emitError("Error creating IR2Vec embeddings: " +
212-
EI.message());
213-
});
214-
return;
215-
}
216-
auto Embedder = std::move(*EmbOrErr);
217-
const auto &BBEmbedding = Embedder->getBBVector(BB);
218-
// Subtract BBEmbedding from Function embedding if the direction is -1,
219-
// and add it if the direction is +1.
220-
if (Direction == -1)
221-
FunctionEmbedding -= BBEmbedding;
222-
else
223-
FunctionEmbedding += BBEmbedding;
224-
}
225202
}
226203

227204
void FunctionPropertiesInfo::updateAggregateStats(const Function &F,
@@ -243,91 +220,21 @@ void FunctionPropertiesInfo::updateAggregateStats(const Function &F,
243220

244221
FunctionPropertiesInfo FunctionPropertiesInfo::getFunctionPropertiesInfo(
245222
Function &F, FunctionAnalysisManager &FAM) {
246-
// We use the cached result of the IR2VecVocabAnalysis run by
247-
// InlineAdvisorAnalysis. If the IR2VecVocabAnalysis is not run, we don't
248-
// use IR2Vec embeddings.
249-
auto VocabResult = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F)
250-
.getCachedResult<IR2VecVocabAnalysis>(*F.getParent());
251223
return getFunctionPropertiesInfo(F, FAM.getResult<DominatorTreeAnalysis>(F),
252-
FAM.getResult<LoopAnalysis>(F), VocabResult);
224+
FAM.getResult<LoopAnalysis>(F));
253225
}
254226

255227
FunctionPropertiesInfo FunctionPropertiesInfo::getFunctionPropertiesInfo(
256-
const Function &F, const DominatorTree &DT, const LoopInfo &LI,
257-
const IR2VecVocabResult *VocabResult) {
228+
const Function &F, const DominatorTree &DT, const LoopInfo &LI) {
258229

259230
FunctionPropertiesInfo FPI;
260-
if (VocabResult && VocabResult->isValid()) {
261-
FPI.IR2VecVocab = VocabResult->getVocabulary();
262-
FPI.FunctionEmbedding = ir2vec::Embedding(VocabResult->getDimension(), 0.0);
263-
}
264231
for (const auto &BB : F)
265232
if (DT.isReachableFromEntry(&BB))
266233
FPI.reIncludeBB(BB);
267234
FPI.updateAggregateStats(F, LI);
268235
return FPI;
269236
}
270237

271-
bool FunctionPropertiesInfo::operator==(
272-
const FunctionPropertiesInfo &FPI) const {
273-
if (BasicBlockCount != FPI.BasicBlockCount ||
274-
BlocksReachedFromConditionalInstruction !=
275-
FPI.BlocksReachedFromConditionalInstruction ||
276-
Uses != FPI.Uses ||
277-
DirectCallsToDefinedFunctions != FPI.DirectCallsToDefinedFunctions ||
278-
LoadInstCount != FPI.LoadInstCount ||
279-
StoreInstCount != FPI.StoreInstCount ||
280-
MaxLoopDepth != FPI.MaxLoopDepth ||
281-
TopLevelLoopCount != FPI.TopLevelLoopCount ||
282-
TotalInstructionCount != FPI.TotalInstructionCount ||
283-
BasicBlocksWithSingleSuccessor != FPI.BasicBlocksWithSingleSuccessor ||
284-
BasicBlocksWithTwoSuccessors != FPI.BasicBlocksWithTwoSuccessors ||
285-
BasicBlocksWithMoreThanTwoSuccessors !=
286-
FPI.BasicBlocksWithMoreThanTwoSuccessors ||
287-
BasicBlocksWithSinglePredecessor !=
288-
FPI.BasicBlocksWithSinglePredecessor ||
289-
BasicBlocksWithTwoPredecessors != FPI.BasicBlocksWithTwoPredecessors ||
290-
BasicBlocksWithMoreThanTwoPredecessors !=
291-
FPI.BasicBlocksWithMoreThanTwoPredecessors ||
292-
BigBasicBlocks != FPI.BigBasicBlocks ||
293-
MediumBasicBlocks != FPI.MediumBasicBlocks ||
294-
SmallBasicBlocks != FPI.SmallBasicBlocks ||
295-
CastInstructionCount != FPI.CastInstructionCount ||
296-
FloatingPointInstructionCount != FPI.FloatingPointInstructionCount ||
297-
IntegerInstructionCount != FPI.IntegerInstructionCount ||
298-
ConstantIntOperandCount != FPI.ConstantIntOperandCount ||
299-
ConstantFPOperandCount != FPI.ConstantFPOperandCount ||
300-
ConstantOperandCount != FPI.ConstantOperandCount ||
301-
InstructionOperandCount != FPI.InstructionOperandCount ||
302-
BasicBlockOperandCount != FPI.BasicBlockOperandCount ||
303-
GlobalValueOperandCount != FPI.GlobalValueOperandCount ||
304-
InlineAsmOperandCount != FPI.InlineAsmOperandCount ||
305-
ArgumentOperandCount != FPI.ArgumentOperandCount ||
306-
UnknownOperandCount != FPI.UnknownOperandCount ||
307-
CriticalEdgeCount != FPI.CriticalEdgeCount ||
308-
ControlFlowEdgeCount != FPI.ControlFlowEdgeCount ||
309-
UnconditionalBranchCount != FPI.UnconditionalBranchCount ||
310-
IntrinsicCount != FPI.IntrinsicCount ||
311-
DirectCallCount != FPI.DirectCallCount ||
312-
IndirectCallCount != FPI.IndirectCallCount ||
313-
CallReturnsIntegerCount != FPI.CallReturnsIntegerCount ||
314-
CallReturnsFloatCount != FPI.CallReturnsFloatCount ||
315-
CallReturnsPointerCount != FPI.CallReturnsPointerCount ||
316-
CallReturnsVectorIntCount != FPI.CallReturnsVectorIntCount ||
317-
CallReturnsVectorFloatCount != FPI.CallReturnsVectorFloatCount ||
318-
CallReturnsVectorPointerCount != FPI.CallReturnsVectorPointerCount ||
319-
CallWithManyArgumentsCount != FPI.CallWithManyArgumentsCount ||
320-
CallWithPointerArgumentCount != FPI.CallWithPointerArgumentCount) {
321-
return false;
322-
}
323-
// Check the equality of the function embeddings. We don't check the equality
324-
// of Vocabulary as it remains the same.
325-
if (!FunctionEmbedding.approximatelyEquals(FPI.FunctionEmbedding))
326-
return false;
327-
328-
return true;
329-
}
330-
331238
void FunctionPropertiesInfo::print(raw_ostream &OS) const {
332239
#define PRINT_PROPERTY(PROP_NAME) OS << #PROP_NAME ": " << PROP_NAME << "\n";
333240

@@ -415,16 +322,6 @@ FunctionPropertiesUpdater::FunctionPropertiesUpdater(
415322
// The caller's entry BB may change due to new alloca instructions.
416323
LikelyToChangeBBs.insert(&*Caller.begin());
417324

418-
// The users of the value returned by call instruction can change
419-
// leading to the change in embeddings being computed, when used.
420-
// We conservatively add the BBs with such uses to LikelyToChangeBBs.
421-
for (const auto *User : CB.users())
422-
CallUsers.insert(dyn_cast<Instruction>(User)->getParent());
423-
// CallSiteBB can be removed from CallUsers if present, it's taken care
424-
// separately.
425-
CallUsers.erase(&CallSiteBB);
426-
LikelyToChangeBBs.insert_range(CallUsers);
427-
428325
// The successors may become unreachable in the case of `invoke` inlining.
429326
// We track successors separately, too, because they form a boundary, together
430327
// with the CB BB ('Entry') between which the inlined callee will be pasted.
@@ -538,9 +435,6 @@ void FunctionPropertiesUpdater::finish(FunctionAnalysisManager &FAM) const {
538435
if (&CallSiteBB != &*Caller.begin())
539436
Reinclude.insert(&*Caller.begin());
540437

541-
// Reinclude the BBs which use the values returned by call instruction
542-
Reinclude.insert_range(CallUsers);
543-
544438
// Distribute the successors to the 2 buckets.
545439
for (const auto *Succ : Successors)
546440
if (DT.isReachableFromEntry(Succ))
@@ -592,9 +486,6 @@ bool FunctionPropertiesUpdater::isUpdateValid(Function &F,
592486
return false;
593487
DominatorTree DT(F);
594488
LoopInfo LI(DT);
595-
auto VocabResult = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F)
596-
.getCachedResult<IR2VecVocabAnalysis>(*F.getParent());
597-
auto Fresh =
598-
FunctionPropertiesInfo::getFunctionPropertiesInfo(F, DT, LI, VocabResult);
489+
auto Fresh = FunctionPropertiesInfo::getFunctionPropertiesInfo(F, DT, LI);
599490
return FPI == Fresh;
600491
}

llvm/lib/Analysis/InlineAdvisor.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "llvm/ADT/StringExtras.h"
1717
#include "llvm/Analysis/AssumptionCache.h"
1818
#include "llvm/Analysis/EphemeralValuesCache.h"
19-
#include "llvm/Analysis/IR2Vec.h"
2019
#include "llvm/Analysis/InlineCost.h"
2120
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
2221
#include "llvm/Analysis/ProfileSummaryInfo.h"
@@ -65,13 +64,6 @@ static cl::opt<bool>
6564
cl::desc("If true, annotate inline advisor remarks "
6665
"with LTO and pass information."));
6766

68-
// This flag is used to enable IR2Vec embeddings in the ML inliner; Only valid
69-
// with ML inliner. The vocab file is used to initialize the embeddings.
70-
static cl::opt<std::string> IR2VecVocabFile(
71-
"ml-inliner-ir2vec-vocab-file", cl::Hidden,
72-
cl::desc("Vocab file for IR2Vec; Setting this enables "
73-
"configuring the model to use IR2Vec embeddings."));
74-
7567
namespace llvm {
7668
extern cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats;
7769
} // namespace llvm
@@ -214,20 +206,6 @@ void InlineAdvice::recordInliningWithCalleeDeleted() {
214206
AnalysisKey InlineAdvisorAnalysis::Key;
215207
AnalysisKey PluginInlineAdvisorAnalysis::Key;
216208

217-
bool InlineAdvisorAnalysis::initializeIR2VecVocabIfRequested(
218-
Module &M, ModuleAnalysisManager &MAM) {
219-
if (!IR2VecVocabFile.empty()) {
220-
auto IR2VecVocabResult = MAM.getResult<IR2VecVocabAnalysis>(M);
221-
if (!IR2VecVocabResult.isValid()) {
222-
M.getContext().emitError("Failed to load IR2Vec vocabulary");
223-
return false;
224-
}
225-
}
226-
// No vocab file specified is OK; We just don't use IR2Vec
227-
// embeddings.
228-
return true;
229-
}
230-
231209
bool InlineAdvisorAnalysis::Result::tryCreate(
232210
InlineParams Params, InliningAdvisorMode Mode,
233211
const ReplayInlinerSettings &ReplaySettings, InlineContext IC) {
@@ -253,21 +231,14 @@ bool InlineAdvisorAnalysis::Result::tryCreate(
253231
/* EmitRemarks =*/true, IC);
254232
}
255233
break;
256-
// Run IR2VecVocabAnalysis once per module to get the vocabulary.
257-
// We run it here because it is immutable and we want to avoid running it
258-
// multiple times.
259234
case InliningAdvisorMode::Development:
260235
#ifdef LLVM_HAVE_TFLITE
261236
LLVM_DEBUG(dbgs() << "Using development-mode inliner policy.\n");
262-
if (!InlineAdvisorAnalysis::initializeIR2VecVocabIfRequested(M, MAM))
263-
return false;
264237
Advisor = llvm::getDevelopmentModeAdvisor(M, MAM, GetDefaultAdvice);
265238
#endif
266239
break;
267240
case InliningAdvisorMode::Release:
268241
LLVM_DEBUG(dbgs() << "Using release-mode inliner policy.\n");
269-
if (!InlineAdvisorAnalysis::initializeIR2VecVocabIfRequested(M, MAM))
270-
return false;
271242
Advisor = llvm::getReleaseModeAdvisor(M, MAM, GetDefaultAdvice);
272243
break;
273244
}

llvm/lib/Analysis/MLInlineAdvisor.cpp

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static cl::opt<bool> KeepFPICache(
107107
cl::init(false));
108108

109109
// clang-format off
110-
std::vector<TensorSpec> llvm::FeatureMap{
110+
const std::vector<TensorSpec> llvm::FeatureMap{
111111
#define POPULATE_NAMES(DTYPE, SHAPE, NAME, __) TensorSpec::createSpec<DTYPE>(#NAME, SHAPE),
112112
// InlineCost features - these must come first
113113
INLINE_COST_FEATURE_ITERATOR(POPULATE_NAMES)
@@ -144,7 +144,6 @@ MLInlineAdvisor::MLInlineAdvisor(
144144
M, MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()),
145145
ModelRunner(std::move(Runner)), GetDefaultAdvice(GetDefaultAdvice),
146146
CG(MAM.getResult<LazyCallGraphAnalysis>(M)),
147-
UseIR2Vec(MAM.getCachedResult<IR2VecVocabAnalysis>(M) != nullptr),
148147
InitialIRSize(getModuleIRSize()), CurrentIRSize(InitialIRSize),
149148
PSI(MAM.getResult<ProfileSummaryAnalysis>(M)) {
150149
assert(ModelRunner);
@@ -187,19 +186,6 @@ MLInlineAdvisor::MLInlineAdvisor(
187186
EdgeCount += getLocalCalls(KVP.first->getFunction());
188187
}
189188
NodeCount = AllNodes.size();
190-
191-
if (auto IR2VecVocabResult = MAM.getCachedResult<IR2VecVocabAnalysis>(M)) {
192-
if (!IR2VecVocabResult->isValid()) {
193-
M.getContext().emitError("IR2VecVocabAnalysis is not valid");
194-
return;
195-
}
196-
// Add the IR2Vec features to the feature map
197-
auto IR2VecDim = IR2VecVocabResult->getDimension();
198-
FeatureMap.push_back(
199-
TensorSpec::createSpec<float>("callee_embedding", {IR2VecDim}));
200-
FeatureMap.push_back(
201-
TensorSpec::createSpec<float>("caller_embedding", {IR2VecDim}));
202-
}
203189
}
204190

205191
unsigned MLInlineAdvisor::getInitialFunctionLevel(const Function &F) const {
@@ -447,24 +433,6 @@ std::unique_ptr<InlineAdvice> MLInlineAdvisor::getAdviceImpl(CallBase &CB) {
447433
*ModelRunner->getTensor<int64_t>(FeatureIndex::is_caller_avail_external) =
448434
Caller.hasAvailableExternallyLinkage();
449435

450-
if (UseIR2Vec) {
451-
// Python side expects float embeddings. The IR2Vec embeddings are doubles
452-
// as of now due to the restriction of fromJSON method used by the
453-
// readVocabulary method in ir2vec::Embeddings.
454-
auto setEmbedding = [&](const ir2vec::Embedding &Embedding,
455-
FeatureIndex Index) {
456-
auto Embedding_float =
457-
std::vector<float>(Embedding.begin(), Embedding.end());
458-
std::memcpy(ModelRunner->getTensor<float>(Index), Embedding_float.data(),
459-
Embedding.size() * sizeof(float));
460-
};
461-
462-
setEmbedding(CalleeBefore.getFunctionEmbedding(),
463-
FeatureIndex::callee_embedding);
464-
setEmbedding(CallerBefore.getFunctionEmbedding(),
465-
FeatureIndex::caller_embedding);
466-
}
467-
468436
// Add the cost features
469437
for (size_t I = 0;
470438
I < static_cast<size_t>(InlineCostFeatureIndex::NumberOfFeatures); ++I) {

0 commit comments

Comments
 (0)