1fe6060f1SDimitry Andric //===-------------- PassBuilder bindings for LLVM-C -----------------------===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric /// \file 9fe6060f1SDimitry Andric /// 10fe6060f1SDimitry Andric /// This file defines the C bindings to the new pass manager 11fe6060f1SDimitry Andric /// 12fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 13fe6060f1SDimitry Andric 14fe6060f1SDimitry Andric #include "llvm-c/Transforms/PassBuilder.h" 15fe6060f1SDimitry Andric #include "llvm/IR/Verifier.h" 16fe6060f1SDimitry Andric #include "llvm/Passes/PassBuilder.h" 17fe6060f1SDimitry Andric #include "llvm/Passes/StandardInstrumentations.h" 18fe6060f1SDimitry Andric #include "llvm/Support/CBindingWrapping.h" 19fe6060f1SDimitry Andric 20fe6060f1SDimitry Andric using namespace llvm; 21fe6060f1SDimitry Andric 22fe6060f1SDimitry Andric namespace llvm { 23fe6060f1SDimitry Andric /// Helper struct for holding a set of builder options for LLVMRunPasses. This 24fe6060f1SDimitry Andric /// structure is used to keep LLVMRunPasses backwards compatible with future 25fe6060f1SDimitry Andric /// versions in case we modify the options the new Pass Manager utilizes. 26fe6060f1SDimitry Andric class LLVMPassBuilderOptions { 27fe6060f1SDimitry Andric public: 28fe6060f1SDimitry Andric explicit LLVMPassBuilderOptions( 29fe6060f1SDimitry Andric bool DebugLogging = false, bool VerifyEach = false, 30fe6060f1SDimitry Andric PipelineTuningOptions PTO = PipelineTuningOptions()) 31fe6060f1SDimitry Andric : DebugLogging(DebugLogging), VerifyEach(VerifyEach), PTO(PTO) {} 32fe6060f1SDimitry Andric 33fe6060f1SDimitry Andric bool DebugLogging; 34fe6060f1SDimitry Andric bool VerifyEach; 35fe6060f1SDimitry Andric PipelineTuningOptions PTO; 36fe6060f1SDimitry Andric }; 37fe6060f1SDimitry Andric } // namespace llvm 38fe6060f1SDimitry Andric 39fe6060f1SDimitry Andric static TargetMachine *unwrap(LLVMTargetMachineRef P) { 40fe6060f1SDimitry Andric return reinterpret_cast<TargetMachine *>(P); 41fe6060f1SDimitry Andric } 42fe6060f1SDimitry Andric 43fe6060f1SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMPassBuilderOptions, 44fe6060f1SDimitry Andric LLVMPassBuilderOptionsRef) 45fe6060f1SDimitry Andric 46fe6060f1SDimitry Andric LLVMErrorRef LLVMRunPasses(LLVMModuleRef M, const char *Passes, 47fe6060f1SDimitry Andric LLVMTargetMachineRef TM, 48fe6060f1SDimitry Andric LLVMPassBuilderOptionsRef Options) { 49fe6060f1SDimitry Andric TargetMachine *Machine = unwrap(TM); 50fe6060f1SDimitry Andric LLVMPassBuilderOptions *PassOpts = unwrap(Options); 51fe6060f1SDimitry Andric bool Debug = PassOpts->DebugLogging; 52fe6060f1SDimitry Andric bool VerifyEach = PassOpts->VerifyEach; 53fe6060f1SDimitry Andric 54fe6060f1SDimitry Andric Module *Mod = unwrap(M); 55fe6060f1SDimitry Andric PassInstrumentationCallbacks PIC; 56*bdd1243dSDimitry Andric PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC); 57fe6060f1SDimitry Andric 58fe6060f1SDimitry Andric LoopAnalysisManager LAM; 59fe6060f1SDimitry Andric FunctionAnalysisManager FAM; 60fe6060f1SDimitry Andric CGSCCAnalysisManager CGAM; 61fe6060f1SDimitry Andric ModuleAnalysisManager MAM; 62fe6060f1SDimitry Andric PB.registerLoopAnalyses(LAM); 63fe6060f1SDimitry Andric PB.registerFunctionAnalyses(FAM); 64fe6060f1SDimitry Andric PB.registerCGSCCAnalyses(CGAM); 65fe6060f1SDimitry Andric PB.registerModuleAnalyses(MAM); 66fe6060f1SDimitry Andric PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); 67fe6060f1SDimitry Andric 68*bdd1243dSDimitry Andric StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach); 69fe6060f1SDimitry Andric SI.registerCallbacks(PIC, &FAM); 70fe6060f1SDimitry Andric ModulePassManager MPM; 71fe6060f1SDimitry Andric if (VerifyEach) { 72fe6060f1SDimitry Andric MPM.addPass(VerifierPass()); 73fe6060f1SDimitry Andric } 74fe6060f1SDimitry Andric if (auto Err = PB.parsePassPipeline(MPM, Passes)) { 75fe6060f1SDimitry Andric return wrap(std::move(Err)); 76fe6060f1SDimitry Andric } 77fe6060f1SDimitry Andric 78fe6060f1SDimitry Andric MPM.run(*Mod, MAM); 79fe6060f1SDimitry Andric return LLVMErrorSuccess; 80fe6060f1SDimitry Andric } 81fe6060f1SDimitry Andric 82fe6060f1SDimitry Andric LLVMPassBuilderOptionsRef LLVMCreatePassBuilderOptions() { 83fe6060f1SDimitry Andric return wrap(new LLVMPassBuilderOptions()); 84fe6060f1SDimitry Andric } 85fe6060f1SDimitry Andric 86fe6060f1SDimitry Andric void LLVMPassBuilderOptionsSetVerifyEach(LLVMPassBuilderOptionsRef Options, 87fe6060f1SDimitry Andric LLVMBool VerifyEach) { 88fe6060f1SDimitry Andric unwrap(Options)->VerifyEach = VerifyEach; 89fe6060f1SDimitry Andric } 90fe6060f1SDimitry Andric 91fe6060f1SDimitry Andric void LLVMPassBuilderOptionsSetDebugLogging(LLVMPassBuilderOptionsRef Options, 92fe6060f1SDimitry Andric LLVMBool DebugLogging) { 93fe6060f1SDimitry Andric unwrap(Options)->DebugLogging = DebugLogging; 94fe6060f1SDimitry Andric } 95fe6060f1SDimitry Andric 96fe6060f1SDimitry Andric void LLVMPassBuilderOptionsSetLoopInterleaving( 97fe6060f1SDimitry Andric LLVMPassBuilderOptionsRef Options, LLVMBool LoopInterleaving) { 98fe6060f1SDimitry Andric unwrap(Options)->PTO.LoopInterleaving = LoopInterleaving; 99fe6060f1SDimitry Andric } 100fe6060f1SDimitry Andric 101fe6060f1SDimitry Andric void LLVMPassBuilderOptionsSetLoopVectorization( 102fe6060f1SDimitry Andric LLVMPassBuilderOptionsRef Options, LLVMBool LoopVectorization) { 103fe6060f1SDimitry Andric unwrap(Options)->PTO.LoopVectorization = LoopVectorization; 104fe6060f1SDimitry Andric } 105fe6060f1SDimitry Andric 106fe6060f1SDimitry Andric void LLVMPassBuilderOptionsSetSLPVectorization( 107fe6060f1SDimitry Andric LLVMPassBuilderOptionsRef Options, LLVMBool SLPVectorization) { 108fe6060f1SDimitry Andric unwrap(Options)->PTO.SLPVectorization = SLPVectorization; 109fe6060f1SDimitry Andric } 110fe6060f1SDimitry Andric 111fe6060f1SDimitry Andric void LLVMPassBuilderOptionsSetLoopUnrolling(LLVMPassBuilderOptionsRef Options, 112fe6060f1SDimitry Andric LLVMBool LoopUnrolling) { 113fe6060f1SDimitry Andric unwrap(Options)->PTO.LoopUnrolling = LoopUnrolling; 114fe6060f1SDimitry Andric } 115fe6060f1SDimitry Andric 116fe6060f1SDimitry Andric void LLVMPassBuilderOptionsSetForgetAllSCEVInLoopUnroll( 117fe6060f1SDimitry Andric LLVMPassBuilderOptionsRef Options, LLVMBool ForgetAllSCEVInLoopUnroll) { 118fe6060f1SDimitry Andric unwrap(Options)->PTO.ForgetAllSCEVInLoopUnroll = ForgetAllSCEVInLoopUnroll; 119fe6060f1SDimitry Andric } 120fe6060f1SDimitry Andric 121fe6060f1SDimitry Andric void LLVMPassBuilderOptionsSetLicmMssaOptCap(LLVMPassBuilderOptionsRef Options, 122fe6060f1SDimitry Andric unsigned LicmMssaOptCap) { 123fe6060f1SDimitry Andric unwrap(Options)->PTO.LicmMssaOptCap = LicmMssaOptCap; 124fe6060f1SDimitry Andric } 125fe6060f1SDimitry Andric 126fe6060f1SDimitry Andric void LLVMPassBuilderOptionsSetLicmMssaNoAccForPromotionCap( 127fe6060f1SDimitry Andric LLVMPassBuilderOptionsRef Options, unsigned LicmMssaNoAccForPromotionCap) { 128fe6060f1SDimitry Andric unwrap(Options)->PTO.LicmMssaNoAccForPromotionCap = 129fe6060f1SDimitry Andric LicmMssaNoAccForPromotionCap; 130fe6060f1SDimitry Andric } 131fe6060f1SDimitry Andric 132fe6060f1SDimitry Andric void LLVMPassBuilderOptionsSetCallGraphProfile( 133fe6060f1SDimitry Andric LLVMPassBuilderOptionsRef Options, LLVMBool CallGraphProfile) { 134fe6060f1SDimitry Andric unwrap(Options)->PTO.CallGraphProfile = CallGraphProfile; 135fe6060f1SDimitry Andric } 136fe6060f1SDimitry Andric 137fe6060f1SDimitry Andric void LLVMPassBuilderOptionsSetMergeFunctions(LLVMPassBuilderOptionsRef Options, 138fe6060f1SDimitry Andric LLVMBool MergeFunctions) { 139fe6060f1SDimitry Andric unwrap(Options)->PTO.MergeFunctions = MergeFunctions; 140fe6060f1SDimitry Andric } 141fe6060f1SDimitry Andric 142fe6060f1SDimitry Andric void LLVMDisposePassBuilderOptions(LLVMPassBuilderOptionsRef Options) { 143fe6060f1SDimitry Andric delete unwrap(Options); 144fe6060f1SDimitry Andric } 145