I was trying to write a LLVM Pass on mac book. The basic hello world one which i have used on the ubuntu long back. Following the same step but not able to get the desire outcome

mac version:
Darwin blackbox 21.6.0 Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:22 PDT 2022; root:xnu-8020.140.41~1/RELEASE_ARM64_T6000 arm64

I am creating a helloworld code inside
~/path/to/llvm-project/llvm/lib/Transforms/HelloWorld/HelloWorld.cpp
Then inside same location creating CMakelist.txt
add_llvm_library( LLVMHello MODULE
HelloWorld.cpp

PLUGIN_TOOL
opt
)

Now inside Tranform add_directory(HelloWorld)

not getting any error while building

Content of HelloWorld.cpp
is

#include “llvm/ADT/Statistic.h”
#include “llvm/IR/Function.h”
#include “llvm/Pass.h”
#include “llvm/Support/raw_ostream.h”
using namespace llvm;

#define DEBUG_TYPE “hello”

STATISTIC(HelloCounter, “Counts number of functions greeted”);

namespace {
// Hello - The first implementation, without getAnalysisUsage.
struct Hello : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
Hello() : FunctionPass(ID) {}

bool runOnFunction(Function &F) override {
  ++HelloCounter;
  errs() << "Hello: ";
  errs().write_escaped(F.getName()) << '\n';
  return false;
}

};
}

char Hello::ID = 0;
static RegisterPass X(“hello”, “Hello World Pass”);

namespace {
// Hello2 - The second implementation with getAnalysisUsage implemented.
struct Hello2 : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
Hello2() : FunctionPass(ID) {}

bool runOnFunction(Function &F) override {
  ++HelloCounter;
  errs() << "Hello: ";
  errs().write_escaped(F.getName()) << '\n';
  return false;
}

// We don't modify the program, so we preserve all analyses.
void getAnalysisUsage(AnalysisUsage &AU) const override {
  AU.setPreservesAll();
}

};
}

char Hello2::ID = 0;
static RegisterPass
Y(“hello2”, “Hello World Pass (with getAnalysisUsage implemented)”);

opt command I’m using is …/bin/opt -load …/lib/LLVMHello.dylib -passes=hello2 hello.ll

WARNING: You’re attempting to print out a bitcode file.
This is inadvisable as it may cause display problems. If
you REALLY want to taste LLVM bitcode first-hand, you
can force output with the `-f’ option.

…/bin/opt: unknown pass name ‘hello2’

this error I am getting

There is a tutorial for out-of-tree passes:

1 Like

Thanks for your valuable feedback adding the flag --bugpoint-enable-legacy-pm is providing me the desired result.

…/bin/opt hello.ll -load …/lib/LLVMHello.dylib --bugpoint-enable-legacy-pm --hello2 -S -o tmp.ll