Building and using a pass for LLVM 3.8 on OSX

不想你离开。 提交于 2019-12-01 04:56:47

问题


I'm trying to build and apply a pass on OSX using llvm 3.8. I installed llvm 3.8 using brew with this formula: $brew install llvm38 Inside the pass I have the following:

static RegisterPass<SwiftPass> X("pass", "My Pass");

My Makefile to build the pass looks like this:

LIB_NAME = pass$(SUFIX)
LDFLAGS = $(shell $(LLVM_PATH)llvm-config --ldflags)
CXXFLAGS = -g -Wall -fno-rtti -fPIC -std=c++11 -shared -dynamiclib $(shell $(LLVM_PATH)llvm-config  --cxxflags --system-libs --libs core executionengine interpreter mc support nativecodegen)
COMPILER = clang++
all: $(LIB_NAME)
$(LIB_NAME): $(OBJ)
    $(COMPILER) $(CXXFLAGS) $(LDFLAGS) $^ -o $@

If gives me a few clang: warning: -l[some lib]: 'linker' input unused. It also gives me:

clang: warning: argument unused during compilation: '-shared'
clang: warning: argument unused during compilation: '-dynamiclib'

But it outputs a .dylib and .o so I proceed to try it out. So now that I have a pass I can apply it to my .bc file and for this I use a Makefile like this (I omited the definitions for brevity):

$(LLVM_OPT) -load $(PASSFILE) $(PASSNAME) $(NAME).bc -o $(NAME).afterMyPass.bc

This translates to:

opt -load pass.dylib -pass int.bc -o int.afterMyPass.bc

And returns this to me:

opt: Unknown command line argument '-pass'.  Try: '/usr/local/Cellar/llvm38/3.8.0/lib/llvm-3.8/bin/opt -help'
opt: Did you mean '-slsr'?
make: *** [pass] Error 1

My guess is that this is caused by the unused arguments during compilation but I don't know why they are unused in the first place since they are required to build a shared library.

What am I doing wrong and how can I fix this?


回答1:


I have resolved the issue. There are two issues with your Makefile.

First you need to suppress the undefine symbols by passing ld flags if you are flat namespace. Or use dynamic look up for linking while using two-level namespace.

Second issue happens when you resolve the first issue. It causes in flat namespace during execution of opt:

CommandLine Error: Option 'pass-remarks' registered more than once! LLVM ERROR: inconsistency in registered CommandLine options

You don't need to pass --libs to llvm-config. It causes conflict with symbols coming from opt and your pass. In two level name space, it won't give this error. Just that your option is Unknown command line argument like previous error.

My Makefile (I have a commented ldflag also for two-level namespace which is default. LLVM project uses flat namespace, so I have used that):

SUFIX=.so
OBJ=pass.o
LIB_NAME = pass$(SUFIX)
LLVM_PATH=/usr/local/Cellar/llvm38/3.8.0/bin/
LDFLAGS = $(shell $(LLVM_PATH)llvm-config-3.8 --ldflags)
LDFLAGS +=-Wl,-dead_strip -Wl,-flat_namespace -Wl,-undefined -Wl,suppress
#LDFLAGS += -Wl,-dead_strip -Wl,-undefined,dynamic_lookup
CXXFLAGS = -g -Wall -fno-rtti -fPIC -std=c++11 $(shell $(LLVM_PATH)llvm-config-3.8  --cxxflags --system-libs)
COMPILER = clang++
all: $(LIB_NAME)
$(LIB_NAME): $(OBJ)
        $(COMPILER) $(CXXFLAGS) -bundle $(LDFLAGS) $^ -o $@
$(OBJ): pass.cpp
        $(COMPILER) $(CXXFLAGS) -c $^ -o $@

One other minor change that I did is added -bundle just for sake of correctness because it's not shared library but loadable bundle which can't be linked against as a library. If you don't add it, it doesn't matter but your output isn't bundle.

Stackoverflow is causing problem with makefile. While copying replace tabs with spaces. Also refer this: https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/MachOTopics/1-Articles/executing_files.html for why you need to suppress.

I really enjoyed this problem(even though it took me whole 1 and half day to understand and resolve).



来源:https://stackoverflow.com/questions/37210682/building-and-using-a-pass-for-llvm-3-8-on-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!