Bug#905397: Unable to build Julia 0.7.0~rc2 due to illegal inttoptr

2018-08-08 Thread Lumin
control: tag -1 +confirmed +patch

We have successfully built Julia with the patched llvm-6.0 .
Please merge the two patches and update the llvm package.

 * llvm-D49832-SCEVPred.patch
 * llvm-rL323946-LSRTy.patch

They come from Julia's commit df451468a14e0b0f7985f8396a6c15ef5a411422
commit 98592fcc61307968f7df1362771534595a1e1c21
Author: Keno Fischer 
Date:   Wed Jul 25 19:29:02 2018 -0400

[SCEV] Don't expand Wrap predicate using inttoptr in ni addrspaces

Summary:
In non-integral address spaces, we're not allowed to introduce inttoptr/ptrtoint
intrinsics. Instead, we need to expand any pointer arithmetic as geps on the
base pointer. Luckily this is a common task for SCEV, so all we have to do here
is hook up the corresponding helper function and add test case.

Fixes PR38290

Reviewers: reames, sanjoy

Subscribers: javed.absar, llvm-commits

Differential Revision: https://reviews.llvm.org/D49832

diff --git a/lib/Analysis/ScalarEvolutionExpander.cpp b/lib/Analysis/ScalarEvolutionExpander.cpp
index 7f76f057216..f441a3647fb 100644
--- a/lib/Analysis/ScalarEvolutionExpander.cpp
+++ b/lib/Analysis/ScalarEvolutionExpander.cpp
@@ -2157,8 +2157,9 @@ Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
   const SCEV *Step = AR->getStepRecurrence(SE);
   const SCEV *Start = AR->getStart();
 
+  Type *ARTy = AR->getType();
   unsigned SrcBits = SE.getTypeSizeInBits(ExitCount->getType());
-  unsigned DstBits = SE.getTypeSizeInBits(AR->getType());
+  unsigned DstBits = SE.getTypeSizeInBits(ARTy);
 
   // The expression {Start,+,Step} has nusw/nssw if
   //   Step < 0, Start - |Step| * Backedge <= Start
@@ -2170,11 +2171,12 @@ Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
   Value *TripCountVal = expandCodeFor(ExitCount, CountTy, Loc);
 
   IntegerType *Ty =
-  IntegerType::get(Loc->getContext(), SE.getTypeSizeInBits(AR->getType()));
+  IntegerType::get(Loc->getContext(), SE.getTypeSizeInBits(ARTy));
+  Type *ARExpandTy = DL.isNonIntegralPointerType(ARTy) ? ARTy : Ty;
 
   Value *StepValue = expandCodeFor(Step, Ty, Loc);
   Value *NegStepValue = expandCodeFor(SE.getNegativeSCEV(Step), Ty, Loc);
-  Value *StartValue = expandCodeFor(Start, Ty, Loc);
+  Value *StartValue = expandCodeFor(Start, ARExpandTy, Loc);
 
   ConstantInt *Zero =
   ConstantInt::get(Loc->getContext(), APInt::getNullValue(DstBits));
@@ -2197,8 +2199,21 @@ Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
   // Compute:
   //   Start + |Step| * Backedge < Start
   //   Start - |Step| * Backedge > Start
-  Value *Add = Builder.CreateAdd(StartValue, MulV);
-  Value *Sub = Builder.CreateSub(StartValue, MulV);
+  Value *Add = nullptr, *Sub = nullptr;
+  if (ARExpandTy->isPointerTy()) {
+PointerType *ARPtrTy = cast(ARExpandTy);
+const SCEV *MulS = SE.getSCEV(MulV);
+const SCEV *const StepArray[2] = {MulS, SE.getNegativeSCEV(MulS)};
+Add = Builder.CreateBitCast(
+expandAddToGEP(&StepArray[0], &StepArray[1], ARPtrTy, Ty, StartValue),
+ARPtrTy);
+Sub = Builder.CreateBitCast(
+expandAddToGEP(&StepArray[1], &StepArray[2], ARPtrTy, Ty, StartValue),
+ARPtrTy);
+  } else {
+Add = Builder.CreateAdd(StartValue, MulV);
+Sub = Builder.CreateSub(StartValue, MulV);
+  }
 
   Value *EndCompareGT = Builder.CreateICmp(
   Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT, Sub, StartValue);
diff --git a/test/Analysis/LoopAccessAnalysis/wrapping-pointer-ni.ll b/test/Analysis/LoopAccessAnalysis/wrapping-pointer-ni.ll
new file mode 100644
index 000..ddcf5e1a195
--- /dev/null
+++ b/test/Analysis/LoopAccessAnalysis/wrapping-pointer-ni.ll
@@ -0,0 +1,73 @@
+; RUN: opt -loop-versioning -S < %s | FileCheck %s -check-prefix=LV
+
+; NB: addrspaces 10-13 are non-integral
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128-ni:10:11:12:13"
+
+; This matches the test case from PR38290
+; Check that we expand the SCEV predicate check using GEP, rather
+; than ptrtoint.
+
+%jl_value_t = type opaque
+%jl_array_t = type { i8 addrspace(13)*, i64, i16, i16, i32 }
+
+declare i64 @julia_steprange_last_4949()
+
+define void @"japi1_align!_9477"(%jl_value_t addrspace(10)**) #0 {
+; LV-LAVEL: L26.lver.check
+; LV: [[OFMul:%[^ ]*]]  = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 4, i64 [[Step:%[^ ]*]])
+; LV-NEXT: [[OFMulResult:%[^ ]*]] = extractvalue { i64, i1 } [[OFMul]], 0
+; LV-NEXT: [[OFMulOverflow:%[^ ]*]] = extractvalue { i64, i1 } [[OFMul]], 1
+; LV-NEXT: [[PosGEP:%[^ ]*]] = getelementptr i32, i32 addrspace(13)* [[Base:%[^ ]*]], i64 [[Step]]
+; LV-NEXT: [[NegGEP:%[^ ]*]] = getelementptr i32, i32 addrspace(13)* [[Base]], i64 [[NegStep:%[^ ]*]]
+; LV-NEXT: icmp ugt i32 addrspace(13)* [[NegGEP]], [[Base]]
+; LV-NEXT: icmp ult i32 addrspace(13)* [[PosGEP]], [[Base]]
+; LV-NOT: inttoptr
+; LV-NOT: ptrtoint
+top:
+  %1 = load %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)** %0,

Bug#905397: Unable to build Julia 0.7.0~rc2 due to illegal inttoptr

2018-08-03 Thread Lumin
Package: llvm-6.0-dev
Version: 1:6.0.1-2
Severity: important
X-Debbugs-CC: pkg-julia-de...@lists.alioth.debian.org

When trying to build Julia 0.7.0~rc2 on expeirmental, I encountered an
error from llvm, as shown in the bottom part of this email.

I haven't ingestigated into this problem but maybe debian's llvm needs this 
patch?
https://github.com/JuliaLang/julia/blob/master/deps/patches/llvm-D49832-SCEVPred.patch

Julia 0.7.0~rc2 is available here g...@salsa.debian.org:julia-team/julia.git
in the "experimental" branch.

```
Statistics  ─  0.335774 seconds
Stdlibs total  ── 66.031648 seconds
Sysimage built. Summary:
Total ───  90.179929 seconds
Base: ───  24.146837 seconds 26.7763%
Stdlibs:   66.031648 seconds 73.2221%
make[3]: Leaving directory '/home/lumin/packages/julia.pkg/julia'
make[3]: Entering directory '/home/lumin/packages/julia.pkg/julia'
 cd /home/lumin/packages/julia.pkg/julia/base && if ! 
/home/lumin/packages/julia.pkg/julia/usr/bin/julia -O3 -C "x86-64" --output-o 
/home/lumin/packages/julia.pkg/julia/usr/lib/x86_64-linux-gnu/julia/sys-o.a.tmp 
 --startup-file=no --warn-overwrite=yes --sysimage 
/home/lumin/packages/julia.pkg/julia/usr/lib/x86_64-linux-gnu/julia/sys.ji 
/home/lumin/packages/julia.pkg/julia/contrib/generate_precompile.jl 
/home/lumin/packages/julia.pkg/julia/usr/lib/x86_64-linux-gnu/julia/sys.ji; 
then echo '*** This error is usually fixed by running `make clean`. If the 
error persists, try `make cleanall`. ***'; false; fi
Generating precompile statements... 751 generated in  30.780778 seconds
Illegal inttoptr
  %scevgep9 = ptrtoint i32 addrspace(13)* %scevgep to i64
Illegal inttoptr
  %scevgep1011 = ptrtoint i32 addrspace(13)* %scevgep10 to i64

signal (6): Aborted
in expression starting at no file:0
gsignal at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
abort at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
runOnFunction at ./src/./src/llvm-gc-invariant-verifier.cpp:178
_ZN4llvm13FPPassManager13runOnFunctionERNS_8FunctionE at 
/usr/lib/x86_64-linux-gnu/libLLVM-6.0.so.1 (unknown line)
_ZN4llvm13FPPassManager11runOnModuleERNS_6ModuleE at 
/usr/lib/x86_64-linux-gnu/libLLVM-6.0.so.1 (unknown line)
_ZN4llvm6legacy15PassManagerImpl3runERNS_6ModuleE at 
/usr/lib/x86_64-linux-gnu/libLLVM-6.0.so.1 (unknown line)
operator() at ./src/./src/jitlayers.cpp:1182 [inlined]
jl_dump_native at ./src/./src/jitlayers.cpp:1191
jl_write_compiler_output at ./src/./src/precompile.c:84
jl_atexit_hook at ./src/./src/init.c:233
main at ./ui/./ui/repl.c:234
__libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
_start at /home/lumin/packages/julia.pkg/julia/usr/bin/julia (unknown line)
Allocations: 56990078 (Pool: 56980033; Big: 10045); GC: 124
Aborted
*** This error is usually fixed by running `make clean`. If the error persists, 
try `make cleanall`. ***
make[3]: *** [Makefile:216: 
/home/lumin/packages/julia.pkg/julia/usr/lib/x86_64-linux-gnu/julia/sys-o.a] 
Error 1
make[3]: Leaving directory '/home/lumin/packages/julia.pkg/julia'
make[2]: *** [Makefile:78: julia-sysimg-release] Error 2
make[2]: Leaving directory '/home/lumin/packages/julia.pkg/julia'
dh_auto_build: make -j4 "INSTALL=install --strip-program=true" prefix=/usr 
sysconfdir=/etc DESTDIR=debian/tmp/ LLVM_CONFIG=/usr/bin/llvm-config-6.0 
LLVM_VER=6.0 MULTIARCH=x86_64-linux-gnu MULTIARCH_INSTALL=1 NO_GIT=1 
"TAGGED_RELEASE_BANNER=Debian ⛬  julia/0.7.0~rc2-1" USE_BLAS64=0 
USE_LLVM_SHLIB=1 USE_SYSTEM_BLAS=1 USE_SYSTEM_CURL=1 USE_SYSTEM_DSFMT=1 
USE_SYSTEM_FFTW=1 USE_SYSTEM_GMP=1 USE_SYSTEM_LAPACK=1 USE_SYSTEM_LIBGIT2=1 
USE_SYSTEM_LIBSSH2=1 USE_SYSTEM_LIBUNWIND=1 USE_SYSTEM_LIBUV=0 
USE_SYSTEM_LLVM=1 USE_SYSTEM_MBEDTLS=1 USE_SYSTEM_MPFR=1 
USE_SYSTEM_OPENSPECFUN=1 USE_SYSTEM_PATCHELF=1 USE_SYSTEM_PCRE=1 
USE_SYSTEM_SUITESPARSE=1 USE_SYSTEM_UTF8PROC=1 VERBOSE=1 MARCH=x86-64 
USE_SYSTEM_OPENLIBM=1 USE_SYSTEM_LIBM=0 LIBBLAS=-lopenblas 
LIBBLASNAME=libopenblas LIBLAPACK=-lopenblas LIBLAPACKNAME=libopenblas returned 
exit code 2
make[1]: *** [debian/rules:109: override_dh_auto_build] Error 2
make[1]: Leaving directory '/home/lumin/packages/julia.pkg/julia'
make: *** [debian/rules:106: build] Error 2
dpkg-buildpackage: error: debian/rules build subprocess returned exit status 2
debuild: fatal error at line 1152:
dpkg-buildpackage -rfakeroot -us -uc -ui -i -j4 failed
```