Path: blob/main/contrib/llvm-project/llvm/lib/Passes/PassBuilderBindings.cpp
35262 views
//===-------------- PassBuilder bindings for LLVM-C -----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7/// \file8///9/// This file defines the C bindings to the new pass manager10///11//===----------------------------------------------------------------------===//1213#include "llvm-c/Transforms/PassBuilder.h"14#include "llvm/IR/Module.h"15#include "llvm/IR/Verifier.h"16#include "llvm/Passes/PassBuilder.h"17#include "llvm/Passes/StandardInstrumentations.h"18#include "llvm/Support/CBindingWrapping.h"1920using namespace llvm;2122namespace llvm {23/// Helper struct for holding a set of builder options for LLVMRunPasses. This24/// structure is used to keep LLVMRunPasses backwards compatible with future25/// versions in case we modify the options the new Pass Manager utilizes.26class LLVMPassBuilderOptions {27public:28explicit LLVMPassBuilderOptions(29bool DebugLogging = false, bool VerifyEach = false,30PipelineTuningOptions PTO = PipelineTuningOptions())31: DebugLogging(DebugLogging), VerifyEach(VerifyEach), PTO(PTO) {}3233bool DebugLogging;34bool VerifyEach;35PipelineTuningOptions PTO;36};37} // namespace llvm3839static TargetMachine *unwrap(LLVMTargetMachineRef P) {40return reinterpret_cast<TargetMachine *>(P);41}4243DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMPassBuilderOptions,44LLVMPassBuilderOptionsRef)4546LLVMErrorRef LLVMRunPasses(LLVMModuleRef M, const char *Passes,47LLVMTargetMachineRef TM,48LLVMPassBuilderOptionsRef Options) {49TargetMachine *Machine = unwrap(TM);50LLVMPassBuilderOptions *PassOpts = unwrap(Options);51bool Debug = PassOpts->DebugLogging;52bool VerifyEach = PassOpts->VerifyEach;5354Module *Mod = unwrap(M);55PassInstrumentationCallbacks PIC;56PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC);5758LoopAnalysisManager LAM;59FunctionAnalysisManager FAM;60CGSCCAnalysisManager CGAM;61ModuleAnalysisManager MAM;62PB.registerLoopAnalyses(LAM);63PB.registerFunctionAnalyses(FAM);64PB.registerCGSCCAnalyses(CGAM);65PB.registerModuleAnalyses(MAM);66PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);6768StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach);69SI.registerCallbacks(PIC, &MAM);70ModulePassManager MPM;71if (VerifyEach) {72MPM.addPass(VerifierPass());73}74if (auto Err = PB.parsePassPipeline(MPM, Passes)) {75return wrap(std::move(Err));76}7778MPM.run(*Mod, MAM);79return LLVMErrorSuccess;80}8182LLVMPassBuilderOptionsRef LLVMCreatePassBuilderOptions() {83return wrap(new LLVMPassBuilderOptions());84}8586void LLVMPassBuilderOptionsSetVerifyEach(LLVMPassBuilderOptionsRef Options,87LLVMBool VerifyEach) {88unwrap(Options)->VerifyEach = VerifyEach;89}9091void LLVMPassBuilderOptionsSetDebugLogging(LLVMPassBuilderOptionsRef Options,92LLVMBool DebugLogging) {93unwrap(Options)->DebugLogging = DebugLogging;94}9596void LLVMPassBuilderOptionsSetLoopInterleaving(97LLVMPassBuilderOptionsRef Options, LLVMBool LoopInterleaving) {98unwrap(Options)->PTO.LoopInterleaving = LoopInterleaving;99}100101void LLVMPassBuilderOptionsSetLoopVectorization(102LLVMPassBuilderOptionsRef Options, LLVMBool LoopVectorization) {103unwrap(Options)->PTO.LoopVectorization = LoopVectorization;104}105106void LLVMPassBuilderOptionsSetSLPVectorization(107LLVMPassBuilderOptionsRef Options, LLVMBool SLPVectorization) {108unwrap(Options)->PTO.SLPVectorization = SLPVectorization;109}110111void LLVMPassBuilderOptionsSetLoopUnrolling(LLVMPassBuilderOptionsRef Options,112LLVMBool LoopUnrolling) {113unwrap(Options)->PTO.LoopUnrolling = LoopUnrolling;114}115116void LLVMPassBuilderOptionsSetForgetAllSCEVInLoopUnroll(117LLVMPassBuilderOptionsRef Options, LLVMBool ForgetAllSCEVInLoopUnroll) {118unwrap(Options)->PTO.ForgetAllSCEVInLoopUnroll = ForgetAllSCEVInLoopUnroll;119}120121void LLVMPassBuilderOptionsSetLicmMssaOptCap(LLVMPassBuilderOptionsRef Options,122unsigned LicmMssaOptCap) {123unwrap(Options)->PTO.LicmMssaOptCap = LicmMssaOptCap;124}125126void LLVMPassBuilderOptionsSetLicmMssaNoAccForPromotionCap(127LLVMPassBuilderOptionsRef Options, unsigned LicmMssaNoAccForPromotionCap) {128unwrap(Options)->PTO.LicmMssaNoAccForPromotionCap =129LicmMssaNoAccForPromotionCap;130}131132void LLVMPassBuilderOptionsSetCallGraphProfile(133LLVMPassBuilderOptionsRef Options, LLVMBool CallGraphProfile) {134unwrap(Options)->PTO.CallGraphProfile = CallGraphProfile;135}136137void LLVMPassBuilderOptionsSetMergeFunctions(LLVMPassBuilderOptionsRef Options,138LLVMBool MergeFunctions) {139unwrap(Options)->PTO.MergeFunctions = MergeFunctions;140}141142void LLVMPassBuilderOptionsSetInlinerThreshold(143LLVMPassBuilderOptionsRef Options, int Threshold) {144unwrap(Options)->PTO.InlinerThreshold = Threshold;145}146147void LLVMDisposePassBuilderOptions(LLVMPassBuilderOptionsRef Options) {148delete unwrap(Options);149}150151152