使用LLVM 5.0.1的AAResultsWrapperPass时出现段错误求助
Let's break down what's happening with your LLVM 5.0.1 AliasAnalysis setup and fix those errors step by step.
Your Setup Recap
First, let's restate your code and error context for clarity:
Analysis Usage Definition
void getAnalysisUsage(AnalysisUsage &AU) const override { AU.addRequired<AAResultsWrapperPass>(); }
Retrieving AAResults
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Your code compiles without issues, but running this command triggers an error about an "Unnamed pass" followed by a segmentation fault:
clang -Xclang -load -Xclang /home/zhangjun/tools/llvm/llvm-build/lib/LLVMCPI.so -O0 -c test.c
Root Causes & Fixes
1. Fix the "Unnamed pass" Error First
LLVM requires every custom pass to have a human-readable name, returned by overriding the getPassName() method. The "Unnamed pass" error means you haven't implemented this yet. Add this to your pass class:
const char *getPassName() const override { return "CPI Alias Analysis Pass"; // Replace with your actual pass name }
This will eliminate the unnamed pass warning and help LLVM track your pass correctly in the pipeline.
2. Resolve the Segmentation Fault (Linked to -O0)
The segfault is almost certainly caused by LLVM disabling most analysis/optimization passes at -O0—including the underlying alias analysis implementations (like BasicAA, GlobalsAA) that AAResultsWrapperPass depends on. Without these, getAnalysis() might return an invalid pointer, and accessing getAAResults() will crash your program.
Option A: Switch to -O1 Instead of -O0
The simplest fix is to use -O1, which automatically enables all necessary analysis passes:
clang -Xclang -load -Xclang /home/zhangjun/tools/llvm/llvm-build/lib/LLVMCPI.so -O1 -c test.c
Option B: Force Required AA Passes at -O0
If you must stick with -O0, explicitly add the underlying AA passes to your analysis usage so LLVM knows to run them:
void getAnalysisUsage(AnalysisUsage &AU) const override { // Add the core AA passes that AAResultsWrapperPass relies on AU.addRequired<BasicAAWrapperPass>(); AU.addRequired<GlobalsAAWrapperPass>(); AU.addRequired<AAResultsWrapperPass>(); AU.setPreservesAll(); // Add this if your pass doesn't modify IR }
3. Add Safety Checks for Null Pointers
To avoid silent crashes, add an assertion to verify you're getting a valid AAResults pointer:
auto &aaWrapper = getAnalysis<AAResultsWrapperPass>(); AA = &aaWrapper.getAAResults(); assert(AA && "Failed to retrieve valid AAResults!");
This will catch invalid pointers early and give you a clear error message instead of a segfault.
4. Double-Check Pass Registration
Make sure your custom pass is properly registered with LLVM. For a function pass, your registration code should look like this (in your pass's .cpp file):
static RegisterPass<YourCustomPassClassName> X("cpi-pass", "CPI Analysis Pass Description", false, false);
The first string is the pass's command-line identifier, the second is a human-readable description.
内容的提问来源于stack exchange,提问作者Rambo

