Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass has no effect #30

Closed
LuoRongLuoRong opened this issue Apr 15, 2022 · 1 comment
Closed

pass has no effect #30

LuoRongLuoRong opened this issue Apr 15, 2022 · 1 comment

Comments

@LuoRongLuoRong
Copy link

I don't know why. I followed the instructions in the readme. But when finally I use the command clang -flegacy-pass-manager -Xclang -load -Xclang build/skeleton/libSkeletonPass.* something.c and get the a.out and run it, what I get is the same as the result produced by a simple command gcc something.c.

1649993630(1)

and here is my code structure:
1649993863(1)

/skeleton/CMakefile.txt:

add_library(SkeletonPass MODULE
    # List your source files here.
    Skeleton.cpp
)

# Use C++11 to compile our pass (i.e., supply -std=c++11).
# target_compile_features(SkeletonPass PRIVATE cxx_range_for cxx_auto_type)

# LLVM is (typically) built with no C++ RTTI. We need to match that;
# otherwise, we'll get linker errors about missing RTTI data.
set_target_properties(SkeletonPass PROPERTIES
    COMPILE_FLAGS "-fno-rtti"
)

# Get proper shared-library behavior (where symbols are not necessarily
# resolved when the shared library is linked) on OS X.
if(APPLE)
    set_target_properties(SkeletonPass PROPERTIES
        LINK_FLAGS "-undefined dynamic_lookup"
    )
endif(APPLE)

/skeleton/Skeleton.cpp

#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
using namespace llvm;

namespace {
  struct SkeletonPass : public FunctionPass {
    static char ID;
    SkeletonPass() : FunctionPass(ID) {}

    virtual bool runOnFunction(Function &F) {
      errs() << "I saw a function called " << F.getName() << "!\n";
      return false;
    }
  };
}

char SkeletonPass::ID = 0;

// Automatically enable the pass.
// http://adriansampson.net/blog/clangpass.html
// static void registerSkeletonPass(const PassManagerBuilder &,
//                          legacy::PassManagerBase &PM) {
//   PM.add(new SkeletonPass());
// }
// static RegisterStandardPasses
//   RegisterMyPass(PassManagerBuilder::EP_EarlyAsPossible,
//                  registerSkeletonPass);

// Register the pass so `opt -skeleton` runs it.
static RegisterPass<SkeletonPass> X("skeleton", "a useless pass"); //注册Pass

/CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(Skeleton)

# support C++14 features used by LLVM 10.0.0
set(CMAKE_CXX_STANDARD 14)

find_package(LLVM REQUIRED CONFIG)
add_definitions(${LLVM_DEFINITIONS})
include_directories(${LLVM_INCLUDE_DIRS})
link_directories(${LLVM_LIBRARY_DIRS})

add_subdirectory(skeleton)  # Use your pass name here.

/src/demo.cpp

#include <stdio.h>
void func2(){
    printf("hello world");
}

void func(){
    func2();
}

int main(){
    func();
    return 0;
}

Actually, when I run make in /build, I then run opt -load ./libSkeletonPass.so -help | grep skeleton to show information of pass. BUT, I get NOTHING. It means that I don't have any pass.

So, I would like to ask for help. SO MUCH THANKS!

@LuoRongLuoRong
Copy link
Author

I found the problem. I didn't follow your instruction completely.
I changed /skeleton/CMakefile.txt. The line target_compile_features(SkeletonPass PRIVATE cxx_range_for cxx_auto_type) should not be omitted.
Then I run commands as follows:

(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton/build$ cmake ..
-- Configuring done
-- Generating done
-- Build files have been written to: /home/fdse/luorong/LLVM/test/llvm-pass-skeleton/build

(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton/build$ make
Consolidate compiler generated dependencies of target SkeletonPass
[ 50%] Building CXX object skeleton/CMakeFiles/SkeletonPass.dir/Skeleton.cpp.o
[100%] Linking CXX shared module libSkeletonPass.so
[100%] Built target SkeletonPass

(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton/build$ cd skeleton/

(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton/build/skeleton$ ls
CMakeFiles  cmake_install.cmake  libSkeletonPass.so  Makefile
(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton/build/skeleton$ opt -load ./libSkeletonPass.so -help | grep skeleton
      --skeleton                                                           - a useless pass

(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton/build/skeleton$ cd ../../src

(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton/src$ clang++ -O0 -g -S -emit-llvm demo.cpp -o demo.ll

(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton/src$ ls
demo.cpp  demo.ll

(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton/src$ opt -load ../build/skeleton/libSkeletonPass.so -skeleton demo.ll -enable-new-pm=0
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.

I saw a function called _Z5func2v!
I saw a function called _Z4funcv!
I saw a function called main!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant