Skip to content

Commit eb79957

Browse files
committed
Merge remote-tracking branch 'origin/sycl-web' into llvmspirv_pulldown
2 parents 5ddad65 + 1fd30a2 commit eb79957

File tree

5 files changed

+75
-9
lines changed

5 files changed

+75
-9
lines changed

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/ADT/APSInt.h"
2828
#include "llvm/ADT/SmallPtrSet.h"
2929
#include "llvm/ADT/SmallVector.h"
30+
#include "llvm/ADT/StringExtras.h"
3031
#include "llvm/Support/FileSystem.h"
3132
#include "llvm/Support/Path.h"
3233
#include "llvm/Support/raw_ostream.h"

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
#include "llvm/ADT/DenseMap.h"
5858
#include "llvm/ADT/SmallSet.h"
59+
#include "llvm/ADT/StringExtras.h"
5960
#include "llvm/Analysis/LoopInfo.h"
6061
#include "llvm/BinaryFormat/Dwarf.h"
6162
#include "llvm/IR/Constants.h"

llvm/lib/IR/ConstantFold.cpp

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) {
111111
if (SrcTy == DestTy)
112112
return V; // no-op cast
113113

114+
#ifndef INTEL_SYCL_OPAQUEPOINTER_READY
115+
// Check to see if we are casting a pointer to an aggregate to a pointer to
116+
// the first element. If so, return the appropriate GEP instruction.
117+
if (PointerType *PTy = dyn_cast<PointerType>(V->getType()))
118+
if (PointerType *DPTy = dyn_cast<PointerType>(DestTy))
119+
if (PTy->getAddressSpace() == DPTy->getAddressSpace() &&
120+
!PTy->isOpaque() && !DPTy->isOpaque() &&
121+
PTy->getNonOpaquePointerElementType()->isSized()) {
122+
SmallVector<Value*, 8> IdxList;
123+
Value *Zero =
124+
Constant::getNullValue(Type::getInt32Ty(DPTy->getContext()));
125+
IdxList.push_back(Zero);
126+
Type *ElTy = PTy->getNonOpaquePointerElementType();
127+
while (ElTy && ElTy != DPTy->getNonOpaquePointerElementType()) {
128+
ElTy = GetElementPtrInst::getTypeAtIndex(ElTy, (uint64_t)0);
129+
IdxList.push_back(Zero);
130+
}
131+
132+
if (ElTy == DPTy->getNonOpaquePointerElementType())
133+
// This GEP is inbounds because all indices are zero.
134+
return ConstantExpr::getInBoundsGetElementPtr(
135+
PTy->getNonOpaquePointerElementType(), V, IdxList);
136+
}
137+
#endif //INTEL_SYCL_OPAQUEPOINTER_READY
138+
114139
// Handle casts from one vector constant to another. We know that the src
115140
// and dest type have the same size (otherwise its an illegal cast).
116141
if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
@@ -2010,6 +2035,12 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
20102035
return InBounds ? PoisonValue::get(GEPTy) : UndefValue::get(GEPTy);
20112036

20122037
auto IsNoOp = [&]() {
2038+
#ifndef INTEL_SYCL_OPAQUEPOINTER_READY
2039+
// For non-opaque pointers having multiple indices will change the result
2040+
// type of the GEP.
2041+
if (!C->getType()->getScalarType()->isOpaquePointerTy() && Idxs.size() != 1)
2042+
return false;
2043+
#endif //INTEL_SYCL_OPAQUEPOINTER_READY
20132044
// Avoid losing inrange information.
20142045
if (InRangeIndex)
20152046
return false;
@@ -2058,10 +2089,41 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
20582089
}
20592090
}
20602091

2061-
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
2092+
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
20622093
if (auto *GEP = dyn_cast<GEPOperator>(CE))
20632094
if (Constant *C = foldGEPOfGEP(GEP, PointeeTy, InBounds, Idxs))
20642095
return C;
2096+
#ifndef INTEL_SYCL_OPAQUEPOINTER_READY
2097+
// Attempt to fold casts to the same type away. For example, folding:
2098+
//
2099+
// i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
2100+
// i64 0, i64 0)
2101+
// into:
2102+
//
2103+
// i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
2104+
//
2105+
// Don't fold if the cast is changing address spaces.
2106+
Constant *Idx0 = cast<Constant>(Idxs[0]);
2107+
if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) {
2108+
PointerType *SrcPtrTy =
2109+
dyn_cast<PointerType>(CE->getOperand(0)->getType());
2110+
PointerType *DstPtrTy = dyn_cast<PointerType>(CE->getType());
2111+
if (SrcPtrTy && DstPtrTy && !SrcPtrTy->isOpaque() &&
2112+
!DstPtrTy->isOpaque()) {
2113+
ArrayType *SrcArrayTy =
2114+
dyn_cast<ArrayType>(SrcPtrTy->getNonOpaquePointerElementType());
2115+
ArrayType *DstArrayTy =
2116+
dyn_cast<ArrayType>(DstPtrTy->getNonOpaquePointerElementType());
2117+
if (SrcArrayTy && DstArrayTy
2118+
&& SrcArrayTy->getElementType() == DstArrayTy->getElementType()
2119+
&& SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
2120+
return ConstantExpr::getGetElementPtr(SrcArrayTy,
2121+
(Constant *)CE->getOperand(0),
2122+
Idxs, InBounds, InRangeIndex);
2123+
}
2124+
}
2125+
#endif //INTEL_SYCL_OPAQUEPOINTER_READY
2126+
}
20652127

20662128
// Check to see if any array indices are not within the corresponding
20672129
// notional array or vector bounds. If so, try to determine if they can be

llvm/tools/sycl-post-link/ModuleSplitter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "llvm/ADT/SetVector.h"
1515
#include "llvm/ADT/SmallPtrSet.h"
16+
#include "llvm/ADT/StringExtras.h"
1617
#include "llvm/IR/Constants.h"
1718
#include "llvm/IR/Function.h"
1819
#include "llvm/IR/InstIterator.h"

sycl/source/detail/sycl_mem_obj_t.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,15 @@ class __SYCL_EXPORT SYCLMemObjT : public SYCLMemObjI {
185185
F(HostPtr);
186186
});
187187
}
188-
189-
if (canReuseHostPtr(HostPtr, RequiredAlign)) {
190-
MUserPtr = HostPtr;
191-
} else {
192-
setAlign(RequiredAlign);
193-
MShadowCopy = allocateHostMem();
194-
MUserPtr = MShadowCopy;
195-
std::memcpy(MUserPtr, HostPtr, MSizeInBytes);
188+
if(HostPtr){
189+
if (canReuseHostPtr(HostPtr, RequiredAlign)) {
190+
MUserPtr = HostPtr;
191+
} else {
192+
setAlign(RequiredAlign);
193+
MShadowCopy = allocateHostMem();
194+
MUserPtr = MShadowCopy;
195+
std::memcpy(MUserPtr, HostPtr, MSizeInBytes);
196+
}
196197
}
197198
}
198199

0 commit comments

Comments
 (0)