Implement a function-pass plugin for LLVM's new pass manager,
named strength-reduce. It must replace multiplication by a positive
power-of-two integer constant with an equivalent shl. Handle the
constant on either side, preserve the integer type and applicable
nuw/nsw flags (you may need to read about the
meanings of these flags), avoid changing vector multiplications,
and return the correct PreservedAnalyses value. Run it with:
opt -load-pass-plugin ./libStrengthReduce.so \
-passes=strength-reduce -S input.ll -o output.ll
opt -passes=verify -disable-output output.ll
Provide positive tests for at least two integer widths and both operand orders, and negative tests for zero, one, a non-power-of-two, a negative constant, and a vector value. Compare the original and transformed programs' outputs.
You may use an online search or AI conversation bots to learn about how to write an LLVM pass, but please do not try to get an AI to solve the whole assignment for you.
Read about Clang's analysis at https://clang.llvm.org/docs/DataFlowAnalysisIntro.html. Notice the difference in terminology: they use \bottom for \top and vice-versa; also they use the term join instead of "meet" (as discussed in class). Other than these cosmetic differences, the dataflow analysis fundamentals are identical to those discussed in class. Answer the following questions:
Do not use AI for this part
llvm-project/clang/include/clang/Analysis/CFG.h. This consists of:
clang/lib/Analysis/CFG.cpp is the ground truth for why a given AST construct produces the blocks/edges it does (e.g. how &&/||, ?:, loops, try/catch, and switch fallthrough get lowered).
cmake -G Ninja -S llvm -B build \ -DCMAKE_BUILD_TYPE=Release \ -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" \ -DLLVM_TARGETS_TO_BUILD="X86" \ -DCMAKE_C_COMPILER=clang-12 -DCMAKE_CXX_COMPILER=clang++-12 \ -DLLVM_USE_LINKER=lld \ -DLLVM_ENABLE_ASSERTIONS=ONKey flags:
- -S llvm -B build: source root is the llvm/ subdirectory, not the repo root; build is wherever you want the build tree (out-of-tree only — never build inside the source dir). - LLVM_ENABLE_PROJECTS: which subprojects to build alongside core LLVM (clang, clang-tools-extra, lld, compiler-rt, polly, mlir, etc., semicolon-separated). - CMAKE_BUILD_TYPE: Release for speed/size, Debug if you need full debug info to step through code in gdb (much slower build, far more disk — your existing tree uses Debug). - LLVM_USE_LINKER=lld: dramatically faster links than GNU ld/gold, worth it especially for Debug builds. - LLVM_ENABLE_ASSERTIONS=ON: catches internal invariant violations; on by default for Debug, off by default for Release — turn on explicitly if developing/debugging. - Using clang-12/clang++-12 to compile LLVM itself builds noticeably faster than gcc/g++.
cmake --build build # builds everything configured # or, narrower: ninja -C build clangAdd
-j N to ninja/cmake --build ... -- to cap parallelism if RAM-constrained (each parallel Debug compile/link job can use 1.5–2GB+).
clang repo, you can dump a CFG for real code using clang -Xclang -analyze -Xclang -analyzer-checker=debug.DumpCFG -fsyntax-only foo.c, or to render it as a graph (needs Graphviz)
using clang -Xclang -analyze -Xclang -analyzer-checker=debug.ViewCFG -fsyntax-only foo.c. You can also add debug statements in the clang code, e.g., using llvm::dbgs() << "Debug statement: ...", build and run again. Answer the following question:
Do not use AI for this part
llvm-project/clang/unittests/Analysis/FlowSensitive/SingleVarConstantPropagationTest.cpp. You may also look at the code that implements the generic fixed-point algorithm for a dataflow analysis in runTypeErasedDataflowAnalysis() in clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp (headers defined in include/clang/Analysis/FlowSensitive/DataflowAnalysis.h, ...). You may need to execute the code on some example programs to be able to answer the following questions.
ConstantPropagationAnalysis::transfer() which implements the transfer function of this dataflow analysis.LatticeJoinEffect::{Changed,Unchanged} and why are they needed?Do not use AI for this part