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

Improve Chapel LLVM support #9567

Merged
merged 5 commits into from May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions compiler/codegen/CForLoop.cpp
Expand Up @@ -45,15 +45,23 @@ static llvm::MDNode* generateLoopMetadata(bool addVectorizeEnableMetadata)
auto tmpNode = llvm::MDNode::getTemporary(ctx, llvm::None);
args.push_back(tmpNode.get());

// llvm.loop.vectorize.enable metadata is only used to either:
// llvm.loop.vectorize.enable metadata is only used by LoopVectorizer to:
// 1) Explicitly disable vectorization of particular loop
// 2) Print warning when vectorization is enabled (using metadata) and vectorization didn't occur
// This means enabling vectorization using this metadata is not necessary for llvm vectorizer to vectorize loop
// It is however required for the Region Vectorizer
if(addVectorizeEnableMetadata)
{
llvm::Metadata *loopVectorizeEnable[] = { llvm::MDString::get(ctx, "llvm.loop.vectorize.enable"),
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(llvm::Type::getInt1Ty(ctx), true))};
args.push_back(llvm::MDNode::get(ctx, loopVectorizeEnable));

#ifdef HAVE_LLVM_RV
// Region Vectorizer needs loop width to be specified
llvm::Metadata *loopVectorWidth[] = { llvm::MDString::get(ctx, "llvm.loop.vectorize.width"),
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), 8))};
args.push_back(llvm::MDNode::get(ctx, loopVectorWidth));
#endif

}

llvm::MDNode *loopMetadata = llvm::MDNode::get(ctx, args);
Expand Down Expand Up @@ -181,7 +189,11 @@ GenRet CForLoop::codegen()

llvm::MDNode* loopMetadata = nullptr;
if(fNoVectorize == false && isOrderIndependent()) {
loopMetadata = generateLoopMetadata(false);
bool addVectorizeEnable = true;
#ifdef HAVE_LLVM_RV
addVectorizeEnable = false;
#endif
loopMetadata = generateLoopMetadata(addVectorizeEnable);
info->loopStack.emplace(loopMetadata, true);
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/include/llvmUtil.h
Expand Up @@ -55,6 +55,9 @@ uint64_t getTypeFieldNext(const llvm::DataLayout& layout, llvm::Type* ty, uint64
#define TOOL_OUTPUT_FILE tool_output_file
#endif

// uncomment to enable prototype Region Vectorizer integration
//#define HAVE_LLVM_RV

#endif //HAVE_LLVM

#endif //LLVMUTIL_H
36 changes: 35 additions & 1 deletion compiler/util/clangUtil.cpp
Expand Up @@ -49,7 +49,6 @@
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Verifier.h"
#include "llvm/MC/SubtargetFeature.h"
Expand All @@ -60,6 +59,11 @@
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"

#ifdef HAVE_LLVM_RV
#include "rv/passes.h"
#endif

#endif

Expand Down Expand Up @@ -1368,6 +1372,14 @@ void finishCodegenLLVM() {
}
}

#ifdef HAVE_LLVM_RV
static void registerRVPasses(const llvm::PassManagerBuilder &Builder,
llvm::legacy::PassManagerBase &PM) {

rv::addOuterLoopVectorizer(PM);
}
#endif

static
void configurePMBuilder(PassManagerBuilder &PMBuilder, int optLevel=-1) {
ClangInfo* clangInfo = gGenInfo->clangInfo;
Expand Down Expand Up @@ -1405,6 +1417,16 @@ void configurePMBuilder(PassManagerBuilder &PMBuilder, int optLevel=-1) {
PMBuilder.PrepareForLTO = opts.PrepareForLTO;
PMBuilder.RerollLoops = opts.RerollLoops;


#ifdef HAVE_LLVM_RV
// Enable Region Vectorizer aka Outer Loop Vectorizer
if (!fNoVectorize) {
// This in copied from 'registerRVPasses'
PMBuilder.addExtension(PassManagerBuilder::EP_VectorizerStart,
registerRVPasses);
}
#endif

// TODO: we might need to call TargetMachine's addEarlyAsPossiblePasses
}

Expand Down Expand Up @@ -2477,7 +2499,11 @@ void makeBinaryLLVM(void) {
tmpErr, sys::fs::F_None);
if (tmpErr)
USR_FATAL("Could not open output file %s", preOptFilename.c_str());
#if HAVE_LLVM_VER < 70
WriteBitcodeToFile(info->module, output.os());
#else
WriteBitcodeToFile(*info->module, output.os());
#endif
output.keep();
output.os().flush();
}
Expand Down Expand Up @@ -2570,7 +2596,11 @@ void makeBinaryLLVM(void) {
tmpErr, sys::fs::F_None);
if (tmpErr)
USR_FATAL("Could not open output file %s", opt1Filename.c_str());
#if HAVE_LLVM_VER < 70
WriteBitcodeToFile(info->module, output1.os());
#else
WriteBitcodeToFile(*info->module, output1.os());
#endif
output1.keep();
output1.os().flush();
}
Expand Down Expand Up @@ -2603,7 +2633,11 @@ void makeBinaryLLVM(void) {
tmpErr, sys::fs::F_None);
if (tmpErr)
USR_FATAL("Could not open output file %s", opt2Filename.c_str());
#if HAVE_LLVM_VER < 70
WriteBitcodeToFile(info->module, output2.os());
#else
WriteBitcodeToFile(*info->module, output2.os());
#endif
output2.keep();
output2.os().flush();
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/util/llvmExtractIR.cpp
Expand Up @@ -41,8 +41,13 @@ void extractAndPrintFunctionLLVM(Function *func) {
ValueToValueMapTy VMap;
// Create a new module containing only the definition of the function
// and using external declarations for everything else
#if HAVE_LLVM_VER < 70
auto ownedM = CloneModule(funcModule, VMap,
[=](const GlobalValue *GV) { return GV == func; });
#else
auto ownedM = CloneModule(*funcModule, VMap,
[=](const GlobalValue *GV) { return GV == func; });
#endif
Module& M = *ownedM.get();

// Make sure the function in the module is externally visible
Expand Down
9 changes: 9 additions & 0 deletions third-party/llvm/Makefile
Expand Up @@ -51,6 +51,15 @@ clobber: FORCE
$(LLVM_SRC_FILE):
./unpack-llvm.sh

# Note, for Polly+GPU, we'd need to add
# -DLLVM_TARGETS_TO_BUILD="host;X86;AArch64;NVPTX"
# -DPOLLY_ENABLE_GPGPU_CODEGEN=ON
# to the flags supplied below.

# Note, for Region Vectorizer (RV), we'd need to add
# -DRV_ENABLE_CRT=on
# to the flags supplied below.

$(LLVM_CONFIGURED_HEADER_FILE): $(LLVM_SRC_FILE)
mkdir -p $(LLVM_BUILD_DIR)
@if ./cmake-ok.sh $(CMAKE); then \
Expand Down
6 changes: 5 additions & 1 deletion third-party/llvm/Makefile.include-llvm
Expand Up @@ -12,11 +12,15 @@ endif

LLVM_CLANG_LIBS=-lclangFrontend -lclangSerialization -lclangDriver -lclangCodeGen -lclangParse -lclangSema -lclangAnalysis -lclangEdit -lclangAST -lclangLex -lclangBasic

#Note, uncomment to enable Region Vectorizer RV
#LLVM_RV_LIBS=-lRV -lgensleef
LLVM_RV_LIBS=

LLVM_CXXFLAGS=$(LLVM_CONFIG_CXXFLAGS) -DHAVE_LLVM -Wno-comment
LLVM_CFLAGS=$(LLVM_CONFIG_CFLAGS) -DHAVE_LLVM

LLVM_INCLUDES=-I$(LLVM_INCLUDE_DIR) -I$(LLVM_CLANG_CODEGEN_INCLUDE_DIR)
LLVM_LIBS=-L$(LLVM_LIB_DIR) $(LLVM_CLANG_LIBS) $(LLVM_LLVM_LIBS)
LLVM_LIBS=-L$(LLVM_LIB_DIR) $(LLVM_CLANG_LIBS) $(LLVM_RV_LIBS) $(LLVM_LLVM_LIBS)

CLANG_CC=$(LLVM_BIN_DIR)/clang
CLANG_CXX=$(LLVM_BIN_DIR)/clang++
Expand Down
66 changes: 56 additions & 10 deletions third-party/llvm/update-llvm.sh
@@ -1,24 +1,70 @@
#!/bin/sh
#!/bin/bash

BRANCH=unknown
ENABLE_RV=0

if [ "$#" -eq 0 ]
then
# No branch argument to use, so compute one
MYVERSION=`cat LLVM_VERSION | sed 's/\.//g'`
BRANCH=release_$MYVERSION
else
# Argument supplied, use that branch
BRANCH="$1"
fi

echo About to check out LLVM etc branch:
echo $BRANCH
sleep 1

CLONEARGS="--branch $BRANCH --single-branch --depth=1"

if [ -d llvm ]
then

echo Updating LLVM
cd llvm
git pull
# --rebase
echo Updating CLANG
cd tools/clang
git pull
cd ../../..
git pull --rebase
cd ../..
echo Updating POLLY
cd tools/polly
git pull --rebase
cd ../..
echo Updating RV
if [ -d tools/rv ]
then
cd tools/rv
git pull --rebase
cd ../..
fi
echo Updating compiler-rt
cd runtimes/compiler-rt
git pull --rebase
cd ../..
cd ..

else

echo Checking out current LLVM trunk
git clone http://llvm.org/git/llvm.git
mkdir -p llvm/tools
cd llvm/tools
echo Checking out current CLANG trunk
git clone http://llvm.org/git/clang.git
cd ../..
echo Checking out LLVM $BRANCH
git clone $CLONEARGS https://git.llvm.org/git/llvm.git llvm
echo Checking out CLANG $BRANCH
git clone $CLONEARGS https://git.llvm.org/git/clang.git llvm/tools/clang
echo Checking out POLLY $BRANCH
git clone $CLONEARGS https://git.llvm.org/git/polly.git llvm/tools/polly
if [ "$ENABLE_RV" -ne 0 ]
echo Checking out RV $BRANCH
git clone $CLONEARGS https://github.com/cdl-saarland/rv llvm/tools/rv
fi
echo Checking out compiler-rt $BRANCH
git clone $CLONEARGS https://git.llvm.org/git/compiler-rt.git llvm/runtimes/compiler-rt

echo Applying Chapel patches to LLVM
patch -p0 < llvm-4.0.1-BasicAliasAnalysis-patch.txt
patch -p0 < llvm-4.0.1-ValueTracking-patch.txt


fi