Skip to content

Commit

Permalink
Update the documentation and implementation of OpcodeCounter
Browse files Browse the repository at this point in the history
In #35, `-O1` was added
to the instructions for building `input_for_cc.bc` (i.e. the sample
input for the `OpcodeCounter` pass). Unfortunately, `-O1` causes all
instructions to be optimized away and hence there's nothing to print for
the `OpcodeCounter` pass. This patch fixes that by restoring the
original instruction for building `input_for_cc.bc` .

In order to work around `optnone` (the original rationale behind adding
`-O1` in the previous patch), `isRequired()` [1] is added to the
implementation of `OpcodeCounter.`

Fixes #38

[1] https://llvm.org/docs/WritingAnLLVMNewPMPass.html#required-passes
  • Loading branch information
banach-space committed Jul 17, 2021
1 parent 133ed1e commit 5ed338e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ need to choose which pass manager you want to use (see
```bash
export LLVM_DIR=<installation/dir/of/llvm/12>
# Generate an LLVM file to analyze
$LLVM_DIR/bin/clang -O1 -emit-llvm -c <source_dir>/inputs/input_for_cc.c -o input_for_cc.bc
$LLVM_DIR/bin/clang -emit-llvm -c <source_dir>/inputs/input_for_cc.c -o input_for_cc.bc
# Run the pass through opt - Legacy PM
$LLVM_DIR/bin/opt -load <build_dir>/lib/libOpcodeCounter.so -legacy-opcode-counter -analyze input_for_cc.bc
# Run the pass through opt - New PM
Expand Down
6 changes: 6 additions & 0 deletions include/OpcodeCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ struct OpcodeCounter : public llvm::AnalysisInfoMixin<OpcodeCounter> {
llvm::FunctionAnalysisManager &);

OpcodeCounter::Result generateOpcodeMap(llvm::Function &F);
// Part of the official API:
// https://llvm.org/docs/WritingAnLLVMNewPMPass.html#required-passes
static bool isRequired() { return true; }

private:
// A special type used by analysis passes to provide an address that
Expand All @@ -46,6 +49,9 @@ class OpcodeCounterPrinter : public llvm::PassInfoMixin<OpcodeCounterPrinter> {
explicit OpcodeCounterPrinter(llvm::raw_ostream &OutS) : OS(OutS) {}
llvm::PreservedAnalyses run(llvm::Function &Func,
llvm::FunctionAnalysisManager &FAM);
// Part of the official API:
// https://llvm.org/docs/WritingAnLLVMNewPMPass.html#required-passes
static bool isRequired() { return true; }

private:
llvm::raw_ostream &OS;
Expand Down

0 comments on commit 5ed338e

Please sign in to comment.