Skip to content

Commit

Permalink
Merge pull request #44214 from JuliaLang/vc/new_pm_plugin
Browse files Browse the repository at this point in the history
[NewPM] add pass plugin
  • Loading branch information
vchuravy authored Feb 17, 2022
2 parents 072c041 + 3887e01 commit 68bae54
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ PUBLIC_HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h)
ifeq ($(OS),WINNT)
PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,win32_ucontext.h)
endif
HEADERS := $(PUBLIC_HEADERS) $(addprefix $(SRCDIR)/,julia_internal.h options.h timing.h) $(addprefix $(BUILDDIR)/,$(DTRACE_HEADERS) jl_internal_funcs.inc)
HEADERS := $(PUBLIC_HEADERS) $(addprefix $(SRCDIR)/,julia_internal.h options.h timing.h passes.h) $(addprefix $(BUILDDIR)/,$(DTRACE_HEADERS) jl_internal_funcs.inc)
PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,julia_gcext.h)
PUBLIC_HEADER_TARGETS := $(addprefix $(build_includedir)/julia/,$(notdir $(PUBLIC_HEADERS)) $(UV_HEADERS))

Expand Down
52 changes: 47 additions & 5 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <llvm/Analysis/BasicAliasAnalysis.h>
#include <llvm/Analysis/TypeBasedAliasAnalysis.h>
#include <llvm/Analysis/ScopedNoAliasAA.h>
#include <llvm/IR/PassManager.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Transforms/IPO.h>
#include <llvm/Transforms/Scalar.h>
Expand All @@ -31,6 +32,8 @@
#include <llvm/Transforms/InstCombine/InstCombine.h>
#include <llvm/Transforms/Scalar/InstSimplifyPass.h>
#include <llvm/Transforms/Utils/SimplifyCFGOptions.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/PassPlugin.h>
#if defined(USE_POLLY)
#include <polly/RegisterPasses.h>
#include <polly/LinkAllPasses.h>
Expand All @@ -52,11 +55,6 @@

using namespace llvm;

// our passes
namespace llvm {
extern Pass *createLowerSimdLoopPass();
}

#include "julia.h"
#include "julia_internal.h"
#include "jitlayers.h"
Expand Down Expand Up @@ -872,6 +870,50 @@ void jl_add_optimization_passes_impl(LLVMPassManagerRef PM, int opt_level, int l
addOptimizationPasses(unwrap(PM), opt_level, lower_intrinsics);
}

// new pass manager plugin

// NOTE: Instead of exporting all the constructors in passes.h we could
// forward the callbacks to the respective passes. LLVM seems to prefer this,
// and when we add the full pass builder having them directly will be helpful.
static void registerCallbacks(PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, FunctionPassManager &PM,
ArrayRef<PassBuilder::PipelineElement> InnerPipeline) {
if (Name == "DemoteFloat16") {
PM.addPass(DemoteFloat16());
return true;
}
if (Name == "CombineMulAdd") {
PM.addPass(CombineMulAdd());
return true;
}
return false;
});

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

extern "C" JL_DLLEXPORT ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "Julia", "1", registerCallbacks};
}

// --- native code info, and dump function to IR and ASM ---
// Get pointer to llvm::Function instance, compiling if necessary
// for use in reflection from Julia.
Expand Down
6 changes: 6 additions & 0 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <llvm/IR/Constants.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Value.h>
#include <llvm/IR/PassManager.h>
#include "llvm/IR/LegacyPassManager.h"

#include <llvm/ExecutionEngine/Orc/IRCompileLayer.h>
Expand Down Expand Up @@ -260,6 +261,11 @@ Pass *createMultiVersioningPass();
Pass *createAllocOptPass();
Pass *createDemoteFloat16Pass();
Pass *createCPUFeaturesPass();
Pass *createLowerSimdLoopPass();

// NewPM
#include "passes.h"

// Whether the Function is an llvm or julia intrinsic.
static inline bool isIntrinsicFunction(Function *F)
{
Expand Down
2 changes: 1 addition & 1 deletion src/julia.expmap
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
jlbacktrace;
jlbacktracet;
_IO_stdin_used;
__ZN4llvm23createLowerSimdLoopPassEv;
_Z24jl_coverage_data_pointerN4llvm9StringRefEi;
_Z22jl_coverage_alloc_lineN4llvm9StringRefEi;
_Z22jl_malloc_data_pointerN4llvm9StringRefEi;
LLVMExtra*;
llvmGetPassPluginInfo;

/* freebsd */
environ;
Expand Down
5 changes: 1 addition & 4 deletions src/llvm-cpufeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// instead of using the global target machine?

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

#include <llvm/IR/Module.h>
#include <llvm/IR/Constants.h>
Expand Down Expand Up @@ -108,10 +109,6 @@ bool lowerCPUFeatures(Module &M)
}
}

struct CPUFeatures : PassInfoMixin<CPUFeatures> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};

PreservedAnalyses CPUFeatures::run(Module &M, ModuleAnalysisManager &AM)
{
lowerCPUFeatures(M);
Expand Down
5 changes: 1 addition & 4 deletions src/llvm-demote-float16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define DEBUG_TYPE "demote_float16"

#include "support/dtypes.h"
#include "passes.h"

#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LegacyPassManager.h>
Expand Down Expand Up @@ -127,10 +128,6 @@ static bool demoteFloat16(Function &F)

} // end anonymous namespace

struct DemoteFloat16 : PassInfoMixin<DemoteFloat16> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};

PreservedAnalyses DemoteFloat16::run(Function &F, FunctionAnalysisManager &AM)
{
demoteFloat16(F);
Expand Down
5 changes: 1 addition & 4 deletions src/llvm-muladd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define DEBUG_TYPE "combine_muladd"
#undef DEBUG
#include "llvm-version.h"
#include "passes.h"

#include <llvm-c/Core.h>
#include <llvm-c/Types.h>
Expand Down Expand Up @@ -82,10 +83,6 @@ static bool combineMulAdd(Function &F)
return true;
}

struct CombineMulAdd : PassInfoMixin<CombineMulAdd> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};

PreservedAnalyses CombineMulAdd::run(Function &F, FunctionAnalysisManager &AM)
{
combineMulAdd(F);
Expand Down
5 changes: 1 addition & 4 deletions src/llvm-remove-ni.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/IR/Module.h>
#include <llvm/IR/PassManager.h>
Expand Down Expand Up @@ -34,10 +35,6 @@ static bool removeNI(Module &M)
}
}

struct RemoveNI : PassInfoMixin<RemoveNI> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};

PreservedAnalyses RemoveNI::run(Module &M, ModuleAnalysisManager &AM)
{
removeNI(M);
Expand Down
8 changes: 2 additions & 6 deletions src/llvm-simdloop.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"

#define DEBUG_TYPE "lower_simd_loop"

Expand Down Expand Up @@ -28,7 +29,7 @@

#include "julia_assert.h"

namespace llvm {
using namespace llvm;

namespace {

Expand Down Expand Up @@ -213,9 +214,6 @@ static bool markLoopInfo(Module &M, Function *marker, function_ref<LoopInfo &(Fu
/// This pass should run after reduction variables have been converted to phi nodes,
/// otherwise floating-point reductions might not be recognized as such and
/// prevent SIMDization.
struct LowerSIMDLoop : PassInfoMixin<LowerSIMDLoop> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};


PreservedAnalyses LowerSIMDLoop::run(Module &M, ModuleAnalysisManager &AM)
Expand Down Expand Up @@ -288,5 +286,3 @@ extern "C" JL_DLLEXPORT void LLVMExtraAddLowerSimdLoopPass_impl(LLVMPassManagerR
{
unwrap(PM)->add(createLowerSimdLoopPass());
}

} // namespace llvm
32 changes: 32 additions & 0 deletions src/passes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This file is a part of Julia. License is MIT: https://julialang.org/license

#ifndef JL_PASSES_H
#define JL_PASSES_H

#include <llvm/IR/PassManager.h>

using namespace llvm;

// Function Passes
struct DemoteFloat16 : PassInfoMixin<DemoteFloat16> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};

struct CombineMulAdd : PassInfoMixin<CombineMulAdd> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};

// Module Passes
struct CPUFeatures : PassInfoMixin<CPUFeatures> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};

struct RemoveNI : PassInfoMixin<RemoveNI> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};

struct LowerSIMDLoop : PassInfoMixin<LowerSIMDLoop> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};

#endif
1 change: 1 addition & 0 deletions test/llvmpasses/cpu-features.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 -CPUFeatures -simplifycfg -S %s | FileCheck %s
; RUN: opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='CPUFeatures,simplifycfg' -S %s | FileCheck %s

declare i1 @julia.cpu.have_fma.f64()
declare double @with_fma(double %0, double %1, double %2)
Expand Down
1 change: 1 addition & 0 deletions test/llvmpasses/loopinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# RUN: julia --startup-file=no %s %t && llvm-link -S %t/* -o %t/module.ll
# RUN: cat %t/module.ll | FileCheck %s
# RUN: cat %t/module.ll | opt -enable-new-pm=0 -load libjulia-codegen%shlibext -LowerSIMDLoop -S - | FileCheck %s -check-prefix=LOWER
# RUN: cat %t/module.ll | opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='LowerSIMDLoop' -S - | FileCheck %s -check-prefix=LOWER
# RUN: julia --startup-file=no %s %t -O && llvm-link -S %t/* -o %t/module.ll
# RUN: cat %t/module.ll | FileCheck %s -check-prefix=FINAL

Expand Down
2 changes: 2 additions & 0 deletions test/llvmpasses/muladd.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
; RUN: opt -enable-new-pm=0 -load libjulia-codegen%shlibext -CombineMulAdd -S %s | FileCheck %s
; RUN: opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='CombineMulAdd' -S %s | FileCheck %s


define double @fast_muladd1(double %a, double %b, double %c) {
top:
Expand Down
1 change: 1 addition & 0 deletions test/llvmpasses/simdloop.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 -LowerSIMDLoop -S %s | FileCheck %s
; RUN: opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='LowerSIMDLoop' -S %s | FileCheck %s

declare void @julia.loopinfo_marker()

Expand Down

0 comments on commit 68bae54

Please sign in to comment.