Skip to content

Advanced IR Generation and IT Certification Exams

  • Contact Us
Close Menu

Implementing a new pass – Optimizing IR-3

August 27, 2024August 27, 2024| Donna martinImplementing a new pass – Optimizing IR-3| 0 Comment| 03:59

Categories :
  • Adding debug metadata
  • Adding line numbers
  • Catching an exception
  • Exams of IT
  • ITCertification Exams
  1. Now, we are ready to declare the functions. There is nothing unusual here: first, the function type is created, followed by the functions: Type *VoidTy = Type::getVoidTy(M.getContext());
    PointerType *PtrTy =
    PointerType::getUnqual(M.getContext());
    FunctionType *EnterExitFty =
    FunctionType::get(VoidTy, {PtrTy}, false);
    Function *EnterFn = Function::Create(
    EnterExitFty, GlobalValue::ExternalLinkage,
    “__ppp_enter”, M);
    Function *ExitFn = Function::Create(
    EnterExitFty, GlobalValue::ExternalLinkage,
    “__ppp_exit”, M);
  2. All we need to do now is loop over all the functions of the module and instrument the found functions by calling our instrument() method. Of course, we need to ignore function declarations, which are just prototypes. There can also be functions without a name, which does not work well with our approach. We’ll filter out those functions too: for (auto &F : M.functions()) {
    if (!F.isDeclaration() && F.hasName())
    instrument(F, EnterFn, ExitFn);
    }
  3. Lastly, we must declare that we did not preserve any analysis. This is most likely too pessimistic but we are on the safe side by doing so: return PreservedAnalyses::none();
    }

The functionality of our new pass is now implemented. To be able to use our pass, we need to register it with the PassBuilder object. This can happen in two ways: statically or dynamically. If the plugin is statically linked, then it needs to provide a function called getPluginInfo(). To use dynamic linking, the llvmGetPassPluginInfo() function needs to be provided. In both cases, an instance of the PassPluginLibraryInfo struct is returned, which provides some basic information about a plugin. Most importantly, this structure contains a pointer to the function that registers the pass. Let’s add this to our source.

  1. In the RegisterCB() function, we register a Lambda function that is called when a pass pipeline string is parsed. If the name of the pass is ppprofiler, then we add our pass to the module pass manager. These callbacks will be expanded upon in the next section:

void RegisterCB(PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, ModulePassManager &MPM,
ArrayRef) {
if (Name == “ppprofiler”) {
MPM.addPass(PPProfilerIRPass());
return true;
}
return false;
});
}

  1. The getPPProfilerPluginInfo() function is called when the plugin is statically linked. It returns some basic information about the plugin:

llvm::PassPluginLibraryInfo getPPProfilerPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, “PPProfiler”, “v0.1”,
RegisterCB};
}

  1. Finally, if the plugin is dynamically linked, then the llvmGetPassPluginInfo() function is called when the plugin is loaded. However, when linking this code statically into a tool, you might end up with linker errors because that function could be defined in several source files. The solution is to guard the function with a macro:

ifndef LLVM_PPPROFILER_LINK_INTO_TOOLS
extern “C” LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
return getPPProfilerPluginInfo();
}
endif

With that, we’ve implemented the pass plugin. Before we look at how to use the new plugin, let’s examine what needs to be changed if we want to add the pass plugin to the LLVM source tree.

Post navigation

Previous page Implementing a new pass – Optimizing IR-2

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Post

Tracking variables and their values – Advanced IR Generation-2

October 9, 2023October 9, 2023 Donna martinTracking variables and their values – Advanced IR Generation-2

Again, we have to specify a source location for the variable. An instance of llvm::DILocation [...]

Read MoreRead More

Technical requirements – Optimizing IR

April 4, 2024April 4, 2024 Donna martinTechnical requirements – Optimizing IR

LLVM uses a series of passes to optimize the IR. A pass operates on a [...]

Read MoreRead More

Throwing and catching exceptions – Advanced IR Generation-1

March 7, 2022March 7, 2022 Donna martinThrowing and catching exceptions – Advanced IR Generation-1

With IR generation introduced in the previous chapters, you can already implement most of the [...]

Read MoreRead More

Search

Dropdown Categories

Archives

  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • December 2021
  • November 2021

Meta

  • Log in

Tag Cloud

Back to Top

Cookie Policy | Terms | Privacy | About Us | © Sherrieland