Skip to content

Commit

Permalink
Merge pull request #38 from AndrewLitteken/master
Browse files Browse the repository at this point in the history
Segmentation Faults and Fixes to Run main branch on macOS
  • Loading branch information
EPiQC authored Jul 8, 2019
2 parents 98275a9 + 5ea8679 commit 0c99b10
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 104 deletions.
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,26 @@ CFLAGS=-L ../build/Debug+Asserts/lib \

SCAFFOLD=scaffold

UNAME_S := $(shell uname -s)

all: Clang

Clang: llvm build
@cd llvm/tools && /bin/rm -f clang && /bin/ln -s ../../clang;
@cd clang && /bin/rm -f build && /bin/ln -s ../build;
@if [ -z $(USE_GCC) ]; then \
cd build && ../llvm/configure --disable-debug-symbols && make ; \
if [ "$(UNAME_S)" = "Darwin" ]; then \
cd build && ../llvm/configure --disable-debug-symbols && make ENABLE_LIBCPP=1; \
else \
cd build && ../llvm/configure --disable-debug-symbols && make ; \
fi \
else \
mkdir -p build && cd build && ../llvm/configure --disable-debug-symbols CC=gcc CXX=g++ && make ; fi
if [ "$(UNAME_S)" = "Darwin" ]; then \
mkdir -p build && cd build && ../llvm/configure --disable-debug-symbols CC=gcc CXX=g++ && make ENABLE_LIBCPP=1; \
else \
mkdir -p build && cd build && ../llvm/configure --disable-debug-symbols CC=gcc CXX=g++ && make ; \
fi \
fi
@if [ -z `echo ${PATH} | grep ${PWD}/Debug+Asserts/bin` ]; then \
export PATH=${PATH}:${PWD}/Debug+Asserts/bin; \
else true; fi
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,7 @@ llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {

llvm::DIType TC = getTypeOrNull(Ty);
if (TC.Verify() && TC.isForwardDecl())
ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), TC));
ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), (llvm::MDNode *) TC));

// And update the type cache.
TypeCache[Ty.getAsOpaquePtr()] = Res;
Expand Down Expand Up @@ -1818,7 +1818,7 @@ llvm::DIType CGDebugInfo::getOrCreateLimitedType(QualType Ty,
llvm::DIType Res = CreateLimitedTypeNode(Ty, Unit);

if (T.Verify() && T.isForwardDecl())
ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), T));
ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), (llvm::MDNode *) T));

// And update the type cache.
TypeCache[Ty.getAsOpaquePtr()] = Res;
Expand Down
4 changes: 2 additions & 2 deletions llvm/Makefile.rules
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,9 @@ endif
ifeq ($(HOST_OS),Darwin)
DARWIN_VERSION := `sw_vers -productVersion`
# Strip a number like 10.4.7 to 10.4
DARWIN_VERSION := $(shell echo $(DARWIN_VERSION)| sed -E 's/(10.[0-9]).*/\1/')
DARWIN_VERSION := $(shell echo $(DARWIN_VERSION)| sed -E 's/(10.[0-9]+).*/\1/')
# Get "4" out of 10.4 for later pieces in the makefile.
DARWIN_MAJVERS := $(shell echo $(DARWIN_VERSION)| sed -E 's/10.([0-9]).*/\1/')
DARWIN_MAJVERS := $(shell echo $(DARWIN_VERSION)| sed -E 's/10.([0-9]+).*/\1/')

LoadableModuleOptions := -Wl,-flat_namespace -Wl,-undefined,suppress
SharedLinkOptions := -dynamiclib
Expand Down
14 changes: 8 additions & 6 deletions llvm/include/llvm/Support/CFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace llvm {

template <class Ptr, class USE_iterator> // Predecessor Iterator
class PredIterator : public std::iterator<std::forward_iterator_tag,
Ptr, ptrdiff_t> {
typedef std::iterator<std::forward_iterator_tag, Ptr, ptrdiff_t> super;
Ptr, ptrdiff_t, Ptr*, Ptr*> {
typedef std::iterator<std::forward_iterator_tag, Ptr, ptrdiff_t, Ptr*, Ptr*> super;
typedef PredIterator<Ptr, USE_iterator> Self;
USE_iterator It;

Expand All @@ -40,6 +40,7 @@ class PredIterator : public std::iterator<std::forward_iterator_tag,

public:
typedef typename super::pointer pointer;
typedef typename super::reference reference;

PredIterator() {}
explicit inline PredIterator(Ptr *bb) : It(bb->use_begin()) {
Expand All @@ -50,7 +51,7 @@ class PredIterator : public std::iterator<std::forward_iterator_tag,
inline bool operator==(const Self& x) const { return It == x.It; }
inline bool operator!=(const Self& x) const { return !operator==(x); }

inline pointer operator*() const {
inline reference operator*() const {
assert(!It.atEnd() && "pred_iterator out of range!");
return cast<TerminatorInst>(*It)->getParent();
}
Expand Down Expand Up @@ -100,10 +101,10 @@ inline const_pred_iterator pred_end(const BasicBlock *BB) {

template <class Term_, class BB_> // Successor Iterator
class SuccIterator : public std::iterator<std::bidirectional_iterator_tag,
BB_, ptrdiff_t> {
BB_, ptrdiff_t, BB_*, BB_*> {
const Term_ Term;
unsigned idx;
typedef std::iterator<std::bidirectional_iterator_tag, BB_, ptrdiff_t> super;
typedef std::iterator<std::bidirectional_iterator_tag, BB_, ptrdiff_t, BB_*, BB_*> super;
typedef SuccIterator<Term_, BB_> Self;

inline bool index_is_valid(int idx) {
Expand All @@ -112,6 +113,7 @@ class SuccIterator : public std::iterator<std::bidirectional_iterator_tag,

public:
typedef typename super::pointer pointer;
typedef typename super::reference reference;
// TODO: This can be random access iterator, only operator[] missing.

explicit inline SuccIterator(Term_ T) : Term(T), idx(0) {// begin iterator
Expand Down Expand Up @@ -142,7 +144,7 @@ class SuccIterator : public std::iterator<std::bidirectional_iterator_tag,
inline bool operator==(const Self& x) const { return idx == x.idx; }
inline bool operator!=(const Self& x) const { return !operator==(x); }

inline pointer operator*() const { return Term->getSuccessor(idx); }
inline reference operator*() const { return Term->getSuccessor(idx); }
inline pointer operator->() const { return operator*(); }

inline Self& operator++() { ++idx; return *this; } // Preincrement
Expand Down
22 changes: 10 additions & 12 deletions llvm/lib/Transforms/Scaffold/GenRKQC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "llvm/Transforms/Utils/BasicBlockUtils.h"

using namespace llvm;

namespace {
// We need to use a ModulePass in order to create new Functions
struct GenRKQC : public ModulePass {
Expand Down Expand Up @@ -53,7 +52,7 @@ namespace {
}

void create_a_swap_b(CallInst& I, Function* RKQC_Func, std::string& rkqcName){
if(!RKQC_Func){
if(!RKQC_Func){
std::vector<Type*> ArgTypes(2);
for(int i=0;i<2;i++) ArgTypes[i] = I.getArgOperand(i)->getType();//Type::getInt16Ty(getGlobalContext());
FunctionType *FuncType = FunctionType::get(Type::getVoidTy(getGlobalContext()),
Expand Down Expand Up @@ -89,11 +88,10 @@ namespace {
ReturnInst::Create(getGlobalContext(), 0, BB);
}
std::vector<Value*> Args(2);
for (int i=0; i<3; i++) Args[i] = I.getArgOperand(i);
for (int i=0; i<2; i++) Args[i] = I.getArgOperand(i);
BasicBlock::iterator ii(&I);
ReplaceInstWithInst(I.getParent()->getInstList(), ii,
CallInst::Create(RKQC_Func, ArrayRef<Value*>(Args)));
}
ReplaceInstWithInst(I.getParent()->getInstList(), ii, CallInst::Create(RKQC_Func, ArrayRef<Value*>(Args)));
}


void create_assign_value_of_b_to_a(CallInst& I, Function* RKQC_Func, std::string& rkqcName){
Expand Down Expand Up @@ -336,9 +334,9 @@ namespace {

void visitCallInst(CallInst &I) {
// Determine whether this is an RKQC function
Function *CF = I.getCalledFunction();
Function *CF = I.getCalledFunction();
if (CF->isIntrinsic()){
std::string name = CF->getName();
std::string name = CF->getName();
if(name.find("rkqc")!=std::string::npos) {
// Retrieve name of function
std::size_t func_loc = name.find("rkqc");
Expand All @@ -353,10 +351,10 @@ namespace {
}
else if(rkqcFuncName.find( "assign_value_of_b_to_a") != std::string::npos){
create_assign_value_of_b_to_a(I, RKQC_Func, rkqcName);
}
}
else if(rkqcFuncName.find( "a_eq_a_plus_b") != std::string::npos){
create_a_eq_a_plus_b(I, RKQC_Func, rkqcName);
}
}
else if(rkqcFuncName.find("assign_value_of_0_to_a") != std::string::npos){
create_assign_value_of_0_to_a(I, RKQC_Func, rkqcName);
}
Expand Down Expand Up @@ -403,12 +401,12 @@ namespace {
}

}// endif 'found RKQC'
}
}
} // visitCallInst()
}; // struct RKQCVisitor

virtual bool runOnModule(Module &M) {
RKQCVisitor RV(&M);
RKQCVisitor RV(&M);
RV.visit(M);
return true;
} // runOnModule()
Expand Down
12 changes: 6 additions & 6 deletions llvm/lib/Transforms/Scaffold/ResourceCount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace {
AU.addRequired<CallGraph>();
}

void CountFunctionResources (Function *F, std::map <Function*, unsigned long long* > FunctionResources) const {
void CountFunctionResources (Function *F, std::map <Function*, unsigned long long* > &FunctionResources) const {
// Traverse instruction by instruction
for (inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) {
Instruction *Inst = &*I; // Grab pointer to instruction reference
Expand Down Expand Up @@ -73,9 +73,7 @@ namespace {
arraySize = 1;
}
}

if(intType){

if (intType->isIntegerTy(16)) { // Filter allocation Type (qbit=i16)
FunctionResources[F][0] += arraySize;
}
Expand All @@ -93,7 +91,7 @@ namespace {
else if (CallInst *CI = dyn_cast<CallInst>(Inst)) { // Filter Call Instructions
Function *callee = CI->getCalledFunction();
if (callee->isIntrinsic()) { // Intrinsic (Gate) Functions calls
FunctionResources[F][14]++;
FunctionResources[F][14]++;
if (callee->getName().startswith("llvm.X"))
FunctionResources[F][4]++;
else if (callee->getName().startswith("llvm.Z"))
Expand Down Expand Up @@ -141,7 +139,7 @@ namespace {
// for this call. Look them up and add to this function's numbers.
if (FunctionResources.find(callee) != FunctionResources.end()) {
unsigned long long* callee_numbers = FunctionResources.find(callee)->second;
FunctionResources[F][14] += callee_numbers[14];
FunctionResources[F][14] += callee_numbers[14];
if(callee_numbers[3] > FunctionResources[F][3] - FunctionResources[F][2])
FunctionResources[F][3] = FunctionResources[F][2] + callee_numbers[3];
for (int l=0; l<NCOUNTS; l++)
Expand Down Expand Up @@ -172,9 +170,11 @@ namespace {
Function *F = (*nsccI)->getFunction();
if (F && !F->isDeclaration()) {
// dynamically create array holding gate numbers for this function
unsigned long long* ResourceNumbers = new unsigned long long[NCOUNTS];
unsigned long long* ResourceNumbers = new unsigned long long[NCOUNTS+1];
for (int k=0; k<NCOUNTS+1; k++)
ResourceNumbers[k] = 0;
errs() <<F->getName()<<"\n";
std::make_pair(F, ResourceNumbers);
FunctionResources.insert(std::make_pair(F, ResourceNumbers));
// count the gates of this function
CountFunctionResources(F, FunctionResources);
Expand Down
22 changes: 16 additions & 6 deletions llvm/projects/sample/autoconf/config.guess
Original file line number Diff line number Diff line change
Expand Up @@ -1131,14 +1131,24 @@ EOF
echo ${UNAME_MACHINE}-apple-rhapsody${UNAME_RELEASE}
exit 0 ;;
*:Darwin:*:*)
case `uname -p` in
*86) UNAME_PROCESSOR=i686 ;;
powerpc) UNAME_PROCESSOR=powerpc ;;
esac
echo ${UNAME_PROCESSOR}-apple-darwin${UNAME_RELEASE}
UNAME_PROCESSOR=`uname -p` || UNAME_PROCESSOR=unknown
case $UNAME_PROCESSOR in
i386)
eval $set_cc_for_build
if [ "$CC_FOR_BUILD" != 'no_compiler_found' ]; then
if (echo '#ifdef __LP64__'; echo IS_64BIT_ARCH; echo '#endif') | \
(CCOPTS= $CC_FOR_BUILD -E - 2>/dev/null) | \
grep IS_64BIT_ARCH >/dev/null
then
UNAME_PROCESSOR="x86_64"
fi
fi ;;
unknown) UNAME_PROCESSOR=powerpc ;;
esac
echo ${UNAME_PROCESSOR}-apple-darwin${UNAME_RELEASE}
exit 0 ;;
*:procnto*:*:* | *:QNX:[0123456789]*:*)
UNAME_PROCESSOR=`uname -p`
UNAME_PROCESSOR=`uname -p` || UNAME_PROCESSOR=unknown
if test "$UNAME_PROCESSOR" = "x86"; then
UNAME_PROCESSOR=i386
UNAME_MACHINE=pc
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/bugpoint/ToolRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static int RunProgramRemotelyWithTimeout(const sys::Path &RemoteClientPath,
ErrorFile.close();
}

errs() << OS;
errs() << OS.str();
}

return ReturnCode;
Expand Down
32 changes: 17 additions & 15 deletions scaffold/Scaffold.makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ CTQG=0
ROTATIONS=0

BUILD=$(ROOT)/build/Release+Asserts
#BUILD=$(ROOT)/build

SQCTPATH=$(ROOT)/Rotations/sqct/rotZ
GRIDSYNTHPATH=$(ROOT)/Rotations/gridsynth/gridsynth
ROTATIONPATH=$(GRIDSYNTHPATH) # select rotation decomposition tool
SCRIPTSPATH=$(ROOT)/scripts/ # select path to scripts
Expand All @@ -24,12 +26,15 @@ OPT=$(BUILD)/bin/opt

CC_FLAGS=-c -emit-llvm -I/usr/include -I/usr/include/x86_64-linux-gnu -I/usr/lib/gcc/x86_64-linux-gnu/4.8/include -I$(DIRNAME)

OSX_FLAGS=""

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
SCAFFOLD_LIB=$(ROOT)/build/Release+Asserts/lib/Scaffold.so
endif
ifeq ($(UNAME_S),Darwin)
SCAFFOLD_LIB=$(ROOT)/build/Release+Asserts/lib/Scaffold.dylib
OSX_FLAGS=-isysroot $(shell xcrun --sdk macosx --show-sdk-path)
endif


Expand Down Expand Up @@ -77,8 +82,8 @@ $(FILE)_merged.scaffold: $(FILENAME)
echo "[Scaffold.makefile] Compiling CTQG ..."; \
$(ROOT)/ctqg/CTQG/ctqg $(CFILE).ctqg; \
echo "[Scaffold.makefile] Merging CTQG output back into Scaffold ..."; \
$(PERL) $(ROOT)/ctqg/trans/trans.pl $(CFILE).qasm > trans.qasm; \
mv trans.qasm $(CFILE).qasm; \
$(PERL) $(ROOT)/ctqg/trans/trans.pl $(CFILE).qasm > trans.qasm; \
mv trans.qasm $(CFILE).qasm; \
$(PERL) $(ROOT)/ctqg/trans/merge.pl $(CFILE).qasm; \
else \
cp $(FILENAME) $(FILE)_merged.scaffold; \
Expand All @@ -87,7 +92,7 @@ $(FILE)_merged.scaffold: $(FILENAME)
# Compile Scaffold to LLVM bytecode
$(FILE).ll: $(FILE)_merged.scaffold
@echo "[Scaffold.makefile] Compiling $(FILE)_merged.scaffold ..."
@$(CC) $(FILE)_merged.scaffold $(CC_FLAGS) -o $(FILE).ll
@$(CC) $(FILE)_merged.scaffold $(CC_FLAGS) $(OSX_FLAGS) -o $(FILE).ll

$(FILE)1.ll: $(FILE).ll
@echo "[Scaffold.makefile] Transforming cbits ..."
Expand Down Expand Up @@ -129,15 +134,12 @@ $(FILE)6.ll: $(FILE)4.ll
# Perform Rotation decomposition if requested and rotation decomp tool is built
$(FILE)7.ll: $(FILE)6.ll
@if [ ! -e $(ROTATIONPATH) ]; then \
echo "[Scaffold.makefile]"; \
echo "[Scaffold.makefile] WARNING: gridsynth not found, skipping rotation decomposition ..."; \
echo "[Scaffold.makefile] (Download gridsynth from https://www.mathstat.dal.ca/~selinger/newsynth and place the binary in ScaffCC/Rotations/gridsynth/)"; \
echo "[Scaffold.makefile]"; \
echo "[Scaffold.makefile] Rotation tool not built, skipping rotation decomposition ..."; \
cp $(FILE)6.ll $(FILE)7.ll; \
elif [ $(ROTATIONS) -eq 1 ]; then \
echo "[Scaffold.makefile] Decomposing Rotations ..." && \
export ROTATIONPATH=$(ROTATIONPATH) && \
export PRECISION=$(PRECISION); \
export PRECISION=$(PRECISION); \
$(OPT) -S -load $(SCAFFOLD_LIB) -Rotations $(FILE)6.ll -o $(FILE)7.ll > /dev/null; \
else \
cp $(FILE)6.ll $(FILE)7.ll; \
Expand All @@ -151,16 +153,16 @@ $(FILE)8.ll: $(FILE)7.ll
# Compile RKQC
$(FILE)9.ll: $(FILE)8.ll
@if [ $(RKQC) -eq 1 ]; then \
echo "[Scaffold.makefile] Compiling RKQC Functions ..."; \
$(OPT) -S -load $(SCAFFOLD_LIB) -GenRKQC $(FILE)8.ll -o $(FILE)9.ll > /dev/null 2> $(FILE).errs; \
echo "[Scaffold.makefile] Compiling RKQC Functions ..."; \
$(OPT) -S -load $(SCAFFOLD_LIB) -GenRKQC $(FILE)8.ll -o $(FILE)9.ll > /dev/null 2> $(FILE).errs; \
else \
mv $(FILE)8.ll $(FILE)9.ll; \
fi
fi

# Perform Toffoli decomposition if TOFF is 1
$(FILE)11.ll: $(FILE)9.ll
@if [ $(TOFF) -eq 1 ]; then \
echo "[Scaffold.makefile] Toffoli Decomposition ..."; \
echo "[Scaffold.makefile] Toffoli Decomposition ..."; \
$(OPT) -S -load $(SCAFFOLD_LIB) -ToffoliReplace $(FILE)9.ll -o $(FILE)11.ll > /dev/null; \
else \
cp $(FILE)9.ll $(FILE)11.ll; \
Expand All @@ -173,7 +175,7 @@ $(FILE)12.ll: $(FILE)11.ll

# Generate resource counts from final LLVM output
$(FILE).resources: $(FILE)12.ll
@echo "[Scaffold.makefile] Generating resource count ..."
@echo "[Scaffold.makefile] Generating resource count ..."
@$(OPT) -load $(SCAFFOLD_LIB) -ResourceCount $(FILE)12.ll 2> $(FILE).resources > /dev/null
@echo "[Scaffold.makefile] Resources written to $(FILE).resources ..."

Expand All @@ -198,10 +200,10 @@ $(FILE).qasmf: $(FILE)12.ll
@$(OPT) -S -load $(SCAFFOLD_LIB) -FlattenModule -all 1 $(FILE)12.ll -o $(FILE)12.inlined.ll 2> /dev/null
@$(OPT) -load $(SCAFFOLD_LIB) -gen-qasm $(FILE)12.inlined.ll 2> $(FILE).qasmh > /dev/null
@$(PYTHON) $(ROOT)/scaffold/flatten-qasm.py $(FILE).qasmh
@$(CC) $(FILE)_qasm.scaffold -o $(FILE)_qasm
@$(CC) $(OSX_FLAGS) $(FILE)_qasm.scaffold -o $(FILE)_qasm
@./$(FILE)_qasm > $(FILE).tmp
@cat fdecl.out $(FILE).tmp > $(FILE).qasmf
@echo "[Scaffold.makefile] Flat QASM written to $(FILE).qasmf ..."
@echo "[Scaffold.makefile] Flat QASM written to $(FILE).qasmf ..."

# Generate OpenQASM
$(FILE).qasm: $(FILE)12.ll
Expand Down
7 changes: 6 additions & 1 deletion scripts/regression_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
DIR=$(dirname $0)
ROOT=$DIR/..
OPT=$ROOT/build/Release+Asserts/bin/opt
SCAF=$ROOT/build/Release+Asserts/lib/Scaffold.so
UNAME_S=$(uname -s)
if [ $UNAME_S = "Darwin" ]; then
SCAF=$ROOT/build/Release+Asserts/lib/Scaffold.dylib
else
SCAF=$ROOT/build/Release+Asserts/lib/Scaffold.so
fi

echo -e " Regression Test "
echo -e "==================================="
Expand Down
Loading

0 comments on commit 0c99b10

Please sign in to comment.