Skip to content

Commit

Permalink
[LLVM][PM] Make JuliaLICM NewPM compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Feb 18, 2022
1 parent 8db9654 commit 7600bfd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,16 @@ static void registerCallbacks(PassBuilder &PB) {
}
return false;
});

PB.registerPipelineParsingCallback(
[](StringRef Name, LoopPassManager &PM,
ArrayRef<PassBuilder::PipelineElement> InnerPipeline) {
if (Name == "JuliaLICM") {
PM.addPass(JuliaLICMPass());
return true;
}
return false;
});
}

extern "C" JL_DLLEXPORT ::llvm::PassPluginLibraryInfo
Expand Down
63 changes: 50 additions & 13 deletions src/llvm-julia-licm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file is a part of Julia. License is MIT: https://julialang.org/license

#include "llvm-version.h"
#include "passes.h"

#include <llvm/Analysis/LoopInfo.h>
#include <llvm/Analysis/LoopPass.h>
Expand Down Expand Up @@ -28,11 +29,27 @@ using namespace llvm;

namespace {

struct JuliaLICMPass : public LoopPass, public JuliaPassContext {
struct JuliaLICMPassLegacy : public LoopPass {
static char ID;
JuliaLICMPass() : LoopPass(ID) {};
JuliaLICMPassLegacy() : LoopPass(ID) {};

bool runOnLoop(Loop *L, LPPassManager &LPM) override
bool runOnLoop(Loop *L, LPPassManager &LPM) override;

protected:
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<LoopInfoWrapperPass>();
AU.setPreservesAll();
}
};

struct JuliaLICM : public JuliaPassContext {
function_ref<DominatorTree &()> GetDT;
function_ref<LoopInfo &()> GetLI;
JuliaLICM(function_ref<DominatorTree &()> GetDT,
function_ref<LoopInfo &()> GetLI) : GetDT(GetDT), GetLI(GetLI) {}

bool runOnLoop(Loop *L)
{
// Get the preheader block to move instructions into,
// required to run this pass.
Expand All @@ -48,8 +65,8 @@ struct JuliaLICMPass : public LoopPass, public JuliaPassContext {
// We also hoist write barriers here, so we don't exit if write_barrier_func exists
if (!gc_preserve_begin_func && !write_barrier_func && !alloc_obj_func)
return false;
auto LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
auto LI = &GetLI();
auto DT = &GetDT();

// Lazy initialization of exit blocks insertion points.
bool exit_pts_init = false;
Expand Down Expand Up @@ -159,22 +176,42 @@ struct JuliaLICMPass : public LoopPass, public JuliaPassContext {
}
return changed;
}

void getAnalysisUsage(AnalysisUsage &AU) const override
{
getLoopAnalysisUsage(AU);
}
};

char JuliaLICMPass::ID = 0;
static RegisterPass<JuliaLICMPass>
bool JuliaLICMPassLegacy::runOnLoop(Loop *L, LPPassManager &LPM) {
auto GetDT = [this]() -> DominatorTree & {
return getAnalysis<DominatorTreeWrapperPass>().getDomTree();
};
auto GetLI = [this]() -> LoopInfo & {
return getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
};
auto juliaLICM = JuliaLICM(GetDT, GetLI);
return juliaLICM.runOnLoop(L);
}

char JuliaLICMPassLegacy::ID = 0;
static RegisterPass<JuliaLICMPassLegacy>
Y("JuliaLICM", "LICM for julia specific intrinsics.",
false, false);
} //namespace

PreservedAnalyses JuliaLICMPass::run(Loop &L, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR, LPMUpdater &U)
{
auto GetDT = [&AR]() -> DominatorTree & {
return AR.DT;
};
auto GetLI = [&AR]() -> LoopInfo & {
return AR.LI;
};
auto juliaLICM = JuliaLICM(GetDT, GetLI);
juliaLICM.runOnLoop(&L);
return PreservedAnalyses::all();
}

Pass *createJuliaLICMPass()
{
return new JuliaLICMPass();
return new JuliaLICMPassLegacy();
}

extern "C" JL_DLLEXPORT void LLVMExtraJuliaLICMPass_impl(LLVMPassManagerRef PM)
Expand Down
7 changes: 7 additions & 0 deletions src/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define JL_PASSES_H

#include <llvm/IR/PassManager.h>
#include <llvm/Transforms/Scalar/LoopPassManager.h>

using namespace llvm;

Expand Down Expand Up @@ -33,4 +34,10 @@ struct LowerSIMDLoop : PassInfoMixin<LowerSIMDLoop> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};

// Loop Passes
struct JuliaLICMPass : PassInfoMixin<JuliaLICMPass> {
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR, LPMUpdater &U);
};

#endif
1 change: 1 addition & 0 deletions test/llvmpasses/julia-licm.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -enable-new-pm=0 -load libjulia-codegen%shlibext -JuliaLICM -S %s | FileCheck %s
; RUN: opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='JuliaLICM' -S %s | FileCheck %s

@tag = external addrspace(10) global {}, align 16

Expand Down

0 comments on commit 7600bfd

Please sign in to comment.